1997-01-06 18:59:08 -04:00
|
|
|
/*
|
1999-07-13 12:18:58 -03:00
|
|
|
* cPickle.c,v 1.71 1999/07/11 13:30:34 jim Exp
|
2001-03-17 00:50:51 -04:00
|
|
|
*
|
|
|
|
* Copyright (c) 1996-1998, Digital Creations, Fredericksburg, VA, USA.
|
1998-11-25 12:18:00 -04:00
|
|
|
* All rights reserved.
|
2001-03-17 00:50:51 -04:00
|
|
|
*
|
1998-11-25 12:18:00 -04:00
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are
|
|
|
|
* met:
|
2001-03-17 00:50:51 -04:00
|
|
|
*
|
1998-11-25 12:18:00 -04:00
|
|
|
* o Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions, and the disclaimer that follows.
|
2001-03-17 00:50:51 -04:00
|
|
|
*
|
1998-11-25 12:18:00 -04:00
|
|
|
* o Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions, and the following disclaimer in
|
|
|
|
* the documentation and/or other materials provided with the
|
|
|
|
* distribution.
|
2001-03-17 00:50:51 -04:00
|
|
|
*
|
1998-11-25 12:18:00 -04:00
|
|
|
* o Neither the name of Digital Creations nor the names of its
|
|
|
|
* contributors may be used to endorse or promote products derived
|
|
|
|
* from this software without specific prior written permission.
|
2001-03-17 00:50:51 -04:00
|
|
|
*
|
|
|
|
*
|
1998-11-25 12:18:00 -04:00
|
|
|
* THIS SOFTWARE IS PROVIDED BY DIGITAL CREATIONS AND CONTRIBUTORS *AS
|
|
|
|
* IS* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
|
|
|
|
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
|
|
|
|
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL DIGITAL
|
|
|
|
* CREATIONS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
|
|
|
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
|
|
|
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
|
|
|
|
* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
|
|
|
|
* USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
|
|
|
|
* DAMAGE.
|
2001-03-17 00:50:51 -04:00
|
|
|
*
|
|
|
|
#
|
1998-11-25 12:18:00 -04:00
|
|
|
# If you have questions regarding this software, contact:
|
|
|
|
#
|
|
|
|
# Digital Creations, L.C.
|
|
|
|
# 910 Princess Ann Street
|
|
|
|
# Fredericksburge, Virginia 22401
|
|
|
|
#
|
|
|
|
# info@digicool.com
|
|
|
|
#
|
|
|
|
# (540) 371-6909
|
|
|
|
*/
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2001-03-17 00:50:51 -04:00
|
|
|
static char cPickle_module_documentation[] =
|
1997-08-13 00:14:41 -03:00
|
|
|
"C implementation and optimization of the Python pickle module\n"
|
|
|
|
"\n"
|
1999-07-13 12:18:58 -03:00
|
|
|
"cPickle.c,v 1.71 1999/07/11 13:30:34 jim Exp\n"
|
1997-01-06 18:59:08 -04:00
|
|
|
;
|
|
|
|
|
|
|
|
#include "Python.h"
|
|
|
|
#include "cStringIO.h"
|
|
|
|
|
1997-08-13 00:14:41 -03:00
|
|
|
#ifndef Py_eval_input
|
|
|
|
#include <graminit.h>
|
|
|
|
#define Py_eval_input eval_input
|
1997-08-20 23:30:45 -03:00
|
|
|
#endif /* Py_eval_input */
|
1997-08-13 00:14:41 -03:00
|
|
|
|
1997-01-06 18:59:08 -04:00
|
|
|
#include <errno.h>
|
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
#define UNLESS(E) if (!(E))
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
#define DEL_LIST_SLICE(list, from, to) (PyList_SetSlice(list, from, to, NULL))
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
#define WRITE_BUF_SIZE 256
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2001-04-10 01:22:00 -03:00
|
|
|
/* --------------------------------------------------------------------------
|
|
|
|
NOTES on format codes.
|
|
|
|
XXX much more is needed here
|
|
|
|
|
|
|
|
Integer types
|
2001-04-10 01:35:28 -03:00
|
|
|
BININT1 8-bit unsigned integer; followed by 1 byte.
|
2001-04-10 01:22:00 -03:00
|
|
|
BININT2 16-bit unsigned integer; followed by 2 bytes, little-endian.
|
2001-04-10 01:35:28 -03:00
|
|
|
BININT 32-bit signed integer; followed by 4 bytes, little-endian.
|
|
|
|
INT Integer; natural decimal string conversion, then newline.
|
2001-04-10 01:22:00 -03:00
|
|
|
CAUTION: INT-reading code can't assume that what follows
|
|
|
|
fits in a Python int, because the size of Python ints varies
|
|
|
|
across platforms.
|
2001-04-10 01:35:28 -03:00
|
|
|
LONG Long (unbounded) integer; repr(i), then newline.
|
2001-04-10 01:22:00 -03:00
|
|
|
-------------------------------------------------------------------------- */
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
#define MARK '('
|
|
|
|
#define STOP '.'
|
|
|
|
#define POP '0'
|
|
|
|
#define POP_MARK '1'
|
|
|
|
#define DUP '2'
|
|
|
|
#define FLOAT 'F'
|
|
|
|
#define BINFLOAT 'G'
|
|
|
|
#define INT 'I'
|
|
|
|
#define BININT 'J'
|
|
|
|
#define BININT1 'K'
|
|
|
|
#define LONG 'L'
|
|
|
|
#define BININT2 'M'
|
|
|
|
#define NONE 'N'
|
|
|
|
#define PERSID 'P'
|
|
|
|
#define BINPERSID 'Q'
|
|
|
|
#define REDUCE 'R'
|
|
|
|
#define STRING 'S'
|
|
|
|
#define BINSTRING 'T'
|
|
|
|
#define SHORT_BINSTRING 'U'
|
2000-03-10 19:11:40 -04:00
|
|
|
#define UNICODE 'V'
|
|
|
|
#define BINUNICODE 'X'
|
1997-04-09 14:36:32 -03:00
|
|
|
#define APPEND 'a'
|
|
|
|
#define BUILD 'b'
|
|
|
|
#define GLOBAL 'c'
|
|
|
|
#define DICT 'd'
|
|
|
|
#define EMPTY_DICT '}'
|
|
|
|
#define APPENDS 'e'
|
|
|
|
#define GET 'g'
|
|
|
|
#define BINGET 'h'
|
|
|
|
#define INST 'i'
|
|
|
|
#define LONG_BINGET 'j'
|
|
|
|
#define LIST 'l'
|
|
|
|
#define EMPTY_LIST ']'
|
|
|
|
#define OBJ 'o'
|
|
|
|
#define PUT 'p'
|
|
|
|
#define BINPUT 'q'
|
|
|
|
#define LONG_BINPUT 'r'
|
|
|
|
#define SETITEM 's'
|
|
|
|
#define TUPLE 't'
|
|
|
|
#define EMPTY_TUPLE ')'
|
|
|
|
#define SETITEMS 'u'
|
|
|
|
|
|
|
|
static char MARKv = MARK;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1999-06-09 12:23:31 -03:00
|
|
|
static PyObject *PickleError;
|
1997-01-06 18:59:08 -04:00
|
|
|
static PyObject *PicklingError;
|
1999-06-09 12:23:31 -03:00
|
|
|
static PyObject *UnpickleableError;
|
1997-01-06 18:59:08 -04:00
|
|
|
static PyObject *UnpicklingError;
|
1998-11-25 12:18:00 -04:00
|
|
|
static PyObject *BadPickleGet;
|
|
|
|
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
static PyObject *dispatch_table;
|
|
|
|
static PyObject *safe_constructors;
|
|
|
|
static PyObject *empty_tuple;
|
|
|
|
|
|
|
|
static PyObject *__class___str, *__getinitargs___str, *__dict___str,
|
|
|
|
*__getstate___str, *__setstate___str, *__name___str, *__reduce___str,
|
|
|
|
*write_str, *__safe_for_unpickling___str, *append_str,
|
1997-12-08 11:15:16 -04:00
|
|
|
*read_str, *readline_str, *__main___str, *__basicnew___str,
|
2001-07-17 15:34:03 -03:00
|
|
|
*copy_reg_str, *dispatch_table_str, *safe_constructors_str;
|
1997-04-09 14:36:32 -03:00
|
|
|
|
1998-11-25 12:18:00 -04:00
|
|
|
#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;
|
|
|
|
|
2001-03-17 00:50:51 -04:00
|
|
|
static void
|
1998-11-25 12:18:00 -04:00
|
|
|
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);
|
|
|
|
|
2000-05-03 20:44:39 -03:00
|
|
|
PyObject_Del(self);
|
1998-11-25 12:18:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
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 *
|
2000-07-24 11:43:35 -03:00
|
|
|
Pdata_New(void) {
|
1998-11-25 12:18:00 -04:00
|
|
|
Pdata *self;
|
|
|
|
|
2000-05-03 20:44:39 -03:00
|
|
|
UNLESS (self = PyObject_New(Pdata, &PdataType)) return NULL;
|
1998-11-25 12:18:00 -04:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2001-03-17 00:50:51 -04:00
|
|
|
static int
|
2000-07-24 11:43:35 -03:00
|
|
|
stackUnderflow(void) {
|
1998-11-25 12:18:00 -04:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-03-17 00:50:51 -04:00
|
|
|
static int
|
1998-11-25 12:18:00 -04:00
|
|
|
Pdata_grow(Pdata *self) {
|
|
|
|
if (! self->size) {
|
|
|
|
PyErr_NoMemory();
|
|
|
|
return -1;
|
|
|
|
}
|
2001-03-17 00:50:51 -04:00
|
|
|
self->size *= 2;
|
1998-11-25 12:18:00 -04:00
|
|
|
self->data = realloc(self->data, self->size*sizeof(PyObject*));
|
|
|
|
if (! self->data) {
|
2001-03-17 00:50:51 -04:00
|
|
|
self->size = 0;
|
|
|
|
PyErr_NoMemory();
|
|
|
|
return -1;
|
1998-11-25 12:18:00 -04:00
|
|
|
}
|
|
|
|
return 0;
|
2001-03-17 00:50:51 -04:00
|
|
|
}
|
1998-11-25 12:18:00 -04:00
|
|
|
|
|
|
|
#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;
|
2000-05-09 15:14:50 -03:00
|
|
|
for (i=start, j=0 ; j < l; i++, j++)
|
|
|
|
PyTuple_SET_ITEM(r, j, self->data[i]);
|
1998-11-25 12:18:00 -04:00
|
|
|
|
|
|
|
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;
|
2000-05-09 15:14:50 -03:00
|
|
|
for (i=start, j=0 ; j < l; i++, j++)
|
|
|
|
PyList_SET_ITEM(r, j, self->data[i]);
|
1998-11-25 12:18:00 -04:00
|
|
|
|
|
|
|
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; \
|
|
|
|
} \
|
|
|
|
}
|
|
|
|
|
2000-07-22 20:56:07 -03:00
|
|
|
typedef struct Picklerobject {
|
1997-04-09 14:36:32 -03:00
|
|
|
PyObject_HEAD
|
|
|
|
FILE *fp;
|
|
|
|
PyObject *write;
|
|
|
|
PyObject *file;
|
|
|
|
PyObject *memo;
|
|
|
|
PyObject *arg;
|
|
|
|
PyObject *pers_func;
|
|
|
|
PyObject *inst_pers_func;
|
|
|
|
int bin;
|
1998-08-13 20:13:52 -03:00
|
|
|
int fast; /* Fast mode doesn't save in memo, don't use if circ ref */
|
2000-07-22 20:56:07 -03:00
|
|
|
int (*write_func)(struct Picklerobject *, char *, int);
|
1997-04-09 14:36:32 -03:00
|
|
|
char *write_buf;
|
|
|
|
int buf_size;
|
1997-12-03 21:13:01 -04:00
|
|
|
PyObject *dispatch_table;
|
1997-04-09 14:36:32 -03:00
|
|
|
} Picklerobject;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-12-08 11:15:16 -04:00
|
|
|
staticforward PyTypeObject Picklertype;
|
1997-04-09 14:36:32 -03:00
|
|
|
|
2000-07-22 20:56:07 -03:00
|
|
|
typedef struct Unpicklerobject {
|
1997-12-03 21:13:01 -04:00
|
|
|
PyObject_HEAD
|
|
|
|
FILE *fp;
|
|
|
|
PyObject *file;
|
|
|
|
PyObject *readline;
|
|
|
|
PyObject *read;
|
|
|
|
PyObject *memo;
|
|
|
|
PyObject *arg;
|
1998-11-25 12:18:00 -04:00
|
|
|
Pdata *stack;
|
1997-12-03 21:13:01 -04:00
|
|
|
PyObject *mark;
|
|
|
|
PyObject *pers_func;
|
|
|
|
PyObject *last_string;
|
|
|
|
int *marks;
|
|
|
|
int num_marks;
|
|
|
|
int marks_size;
|
2000-07-22 20:56:07 -03:00
|
|
|
int (*read_func)(struct Unpicklerobject *, char **, int);
|
|
|
|
int (*readline_func)(struct Unpicklerobject *, char **);
|
1997-12-03 21:13:01 -04:00
|
|
|
int buf_size;
|
|
|
|
char *buf;
|
|
|
|
PyObject *safe_constructors;
|
1999-04-19 14:58:18 -03:00
|
|
|
PyObject *find_class;
|
1997-04-09 14:36:32 -03:00
|
|
|
} Unpicklerobject;
|
2001-03-17 00:50:51 -04:00
|
|
|
|
1997-12-08 11:15:16 -04:00
|
|
|
staticforward PyTypeObject Unpicklertype;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2000-07-24 08:36:47 -03:00
|
|
|
/* Forward decls that need the above structs */
|
|
|
|
static int save(Picklerobject *, PyObject *, int);
|
|
|
|
static int put2(Picklerobject *, PyObject *);
|
|
|
|
|
2001-03-17 00:50:51 -04:00
|
|
|
int
|
1997-12-03 21:13:01 -04:00
|
|
|
cPickle_PyMapping_HasKey(PyObject *o, PyObject *key) {
|
1997-04-09 14:36:32 -03:00
|
|
|
PyObject *v;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1998-11-25 12:18:00 -04:00
|
|
|
if ((v = PyObject_GetItem(o,key))) {
|
1997-04-09 14:36:32 -03:00
|
|
|
Py_DECREF(v);
|
|
|
|
return 1;
|
|
|
|
}
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
PyErr_Clear();
|
|
|
|
return 0;
|
|
|
|
}
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:47:47 -03:00
|
|
|
static
|
1997-04-09 14:36:32 -03:00
|
|
|
PyObject *
|
2000-07-22 20:56:07 -03:00
|
|
|
cPickle_ErrFormat(PyObject *ErrType, char *stringformat, char *format, ...)
|
|
|
|
{
|
1997-04-09 14:36:32 -03:00
|
|
|
va_list va;
|
|
|
|
PyObject *args=0, *retval=0;
|
|
|
|
va_start(va, format);
|
2001-03-17 00:50:51 -04:00
|
|
|
|
1998-11-25 12:18:00 -04:00
|
|
|
if (format) args = Py_VaBuildValue(format, va);
|
1997-04-09 14:36:32 -03:00
|
|
|
va_end(va);
|
1998-11-25 12:18:00 -04:00
|
|
|
if (format && ! args) return NULL;
|
|
|
|
if (stringformat && !(retval=PyString_FromString(stringformat))) return NULL;
|
|
|
|
|
|
|
|
if (retval) {
|
|
|
|
if (args) {
|
|
|
|
PyObject *v;
|
|
|
|
v=PyString_Format(retval, args);
|
|
|
|
Py_DECREF(retval);
|
|
|
|
Py_DECREF(args);
|
|
|
|
if (! v) return NULL;
|
|
|
|
retval=v;
|
|
|
|
}
|
1997-04-09 14:36:32 -03:00
|
|
|
}
|
|
|
|
else
|
1998-11-25 12:18:00 -04:00
|
|
|
if (args) retval=args;
|
1997-12-03 21:13:01 -04:00
|
|
|
else {
|
1998-11-25 12:18:00 -04:00
|
|
|
PyErr_SetObject(ErrType,Py_None);
|
|
|
|
return NULL;
|
1997-04-09 14:36:32 -03:00
|
|
|
}
|
|
|
|
PyErr_SetObject(ErrType,retval);
|
|
|
|
Py_DECREF(retval);
|
|
|
|
return NULL;
|
|
|
|
}
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2001-03-17 00:50:51 -04:00
|
|
|
static int
|
1997-04-09 14:36:32 -03:00
|
|
|
write_file(Picklerobject *self, char *s, int n) {
|
2001-03-17 00:50:51 -04:00
|
|
|
size_t nbyteswritten;
|
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
if (s == NULL) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2001-03-17 00:50:51 -04:00
|
|
|
Py_BEGIN_ALLOW_THREADS
|
|
|
|
nbyteswritten = fwrite(s, sizeof(char), n, self->fp);
|
|
|
|
Py_END_ALLOW_THREADS
|
|
|
|
if (nbyteswritten != (size_t)n) {
|
1997-04-09 14:36:32 -03:00
|
|
|
PyErr_SetFromErrno(PyExc_IOError);
|
|
|
|
return -1;
|
|
|
|
}
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
return n;
|
1997-01-06 18:59:08 -04:00
|
|
|
}
|
|
|
|
|
2001-03-17 00:50:51 -04:00
|
|
|
static int
|
1997-04-09 14:36:32 -03:00
|
|
|
write_cStringIO(Picklerobject *self, char *s, int n) {
|
|
|
|
if (s == NULL) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (PycStringIO->cwrite((PyObject *)self->file, s, n) != n) {
|
|
|
|
return -1;
|
|
|
|
}
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
return n;
|
1997-01-06 18:59:08 -04:00
|
|
|
}
|
|
|
|
|
2001-03-17 00:50:51 -04:00
|
|
|
static int
|
1997-08-13 00:14:41 -03:00
|
|
|
write_none(Picklerobject *self, char *s, int n) {
|
|
|
|
if (s == NULL) return 0;
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2001-03-17 00:50:51 -04:00
|
|
|
static int
|
1997-04-09 14:36:32 -03:00
|
|
|
write_other(Picklerobject *self, char *s, int n) {
|
|
|
|
PyObject *py_str = 0, *junk = 0;
|
|
|
|
|
|
|
|
if (s == NULL) {
|
1998-11-25 12:18:00 -04:00
|
|
|
UNLESS (self->buf_size) return 0;
|
2001-03-17 00:50:51 -04:00
|
|
|
UNLESS (py_str =
|
1997-04-09 14:36:32 -03:00
|
|
|
PyString_FromStringAndSize(self->write_buf, self->buf_size))
|
1998-11-25 12:18:00 -04:00
|
|
|
return -1;
|
1997-04-09 14:36:32 -03:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (self->buf_size && (n + self->buf_size) > WRITE_BUF_SIZE) {
|
|
|
|
if (write_other(self, NULL, 0) < 0)
|
1998-11-25 12:18:00 -04:00
|
|
|
return -1;
|
1997-04-09 14:36:32 -03:00
|
|
|
}
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2001-03-17 00:50:51 -04:00
|
|
|
if (n > WRITE_BUF_SIZE) {
|
|
|
|
UNLESS (py_str =
|
1997-04-09 14:36:32 -03:00
|
|
|
PyString_FromStringAndSize(s, n))
|
1998-11-25 12:18:00 -04:00
|
|
|
return -1;
|
1997-04-09 14:36:32 -03:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
memcpy(self->write_buf + self->buf_size, s, n);
|
|
|
|
self->buf_size += n;
|
1998-11-25 12:18:00 -04:00
|
|
|
return n;
|
1997-04-09 14:36:32 -03:00
|
|
|
}
|
1997-01-06 18:59:08 -04:00
|
|
|
}
|
|
|
|
|
1998-11-25 12:18:00 -04:00
|
|
|
if (self->write) {
|
|
|
|
/* object with write method */
|
|
|
|
ARG_TUP(self, py_str);
|
|
|
|
if (self->arg) {
|
|
|
|
junk = PyObject_CallObject(self->write, self->arg);
|
|
|
|
FREE_ARG_TUP(self);
|
|
|
|
}
|
|
|
|
if (junk) Py_DECREF(junk);
|
|
|
|
else return -1;
|
|
|
|
}
|
2001-03-17 00:50:51 -04:00
|
|
|
else
|
1998-11-25 12:18:00 -04:00
|
|
|
PDATA_PUSH(self->file, py_str, -1);
|
2001-03-17 00:50:51 -04:00
|
|
|
|
|
|
|
self->buf_size = 0;
|
1998-11-25 12:18:00 -04:00
|
|
|
return n;
|
1997-01-06 18:59:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-03-17 00:50:51 -04:00
|
|
|
static int
|
1997-04-09 14:36:32 -03:00
|
|
|
read_file(Unpicklerobject *self, char **s, int n) {
|
2001-03-17 00:50:51 -04:00
|
|
|
size_t nbytesread;
|
1997-04-09 14:36:32 -03:00
|
|
|
|
|
|
|
if (self->buf_size == 0) {
|
|
|
|
int size;
|
|
|
|
|
2001-03-17 00:50:51 -04:00
|
|
|
size = ((n < 32) ? 32 : n);
|
1998-11-25 12:18:00 -04:00
|
|
|
UNLESS (self->buf = (char *)malloc(size * sizeof(char))) {
|
1997-04-09 14:36:32 -03:00
|
|
|
PyErr_NoMemory();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
self->buf_size = size;
|
|
|
|
}
|
|
|
|
else if (n > self->buf_size) {
|
1998-11-25 12:18:00 -04:00
|
|
|
UNLESS (self->buf = (char *)realloc(self->buf, n * sizeof(char))) {
|
1997-04-09 14:36:32 -03:00
|
|
|
PyErr_NoMemory();
|
|
|
|
return -1;
|
|
|
|
}
|
2001-03-17 00:50:51 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
self->buf_size = n;
|
1997-01-06 18:59:08 -04:00
|
|
|
}
|
2001-03-17 00:50:51 -04:00
|
|
|
|
|
|
|
Py_BEGIN_ALLOW_THREADS
|
|
|
|
nbytesread = fread(self->buf, sizeof(char), n, self->fp);
|
|
|
|
Py_END_ALLOW_THREADS
|
|
|
|
if (nbytesread != (size_t)n) {
|
1997-04-09 14:36:32 -03:00
|
|
|
if (feof(self->fp)) {
|
|
|
|
PyErr_SetNone(PyExc_EOFError);
|
|
|
|
return -1;
|
|
|
|
}
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
PyErr_SetFromErrno(PyExc_IOError);
|
|
|
|
return -1;
|
|
|
|
}
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
*s = self->buf;
|
|
|
|
|
|
|
|
return n;
|
1997-01-06 18:59:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-03-17 00:50:51 -04:00
|
|
|
static int
|
1997-04-09 14:36:32 -03:00
|
|
|
readline_file(Unpicklerobject *self, char **s) {
|
|
|
|
int i;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
if (self->buf_size == 0) {
|
1998-11-25 12:18:00 -04:00
|
|
|
UNLESS (self->buf = (char *)malloc(40 * sizeof(char))) {
|
1997-04-09 14:36:32 -03:00
|
|
|
PyErr_NoMemory();
|
|
|
|
return -1;
|
|
|
|
}
|
2001-03-17 00:50:51 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
self->buf_size = 40;
|
|
|
|
}
|
|
|
|
|
|
|
|
i = 0;
|
|
|
|
while (1) {
|
|
|
|
for (; i < (self->buf_size - 1); i++) {
|
|
|
|
if (feof(self->fp) || (self->buf[i] = getc(self->fp)) == '\n') {
|
|
|
|
self->buf[i + 1] = '\0';
|
|
|
|
*s = self->buf;
|
|
|
|
return i + 1;
|
|
|
|
}
|
|
|
|
}
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2001-03-17 00:50:51 -04:00
|
|
|
UNLESS (self->buf = (char *)realloc(self->buf,
|
1997-04-09 14:36:32 -03:00
|
|
|
(self->buf_size * 2) * sizeof(char))) {
|
|
|
|
PyErr_NoMemory();
|
|
|
|
return -1;
|
|
|
|
}
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
self->buf_size *= 2;
|
1997-01-06 18:59:08 -04:00
|
|
|
}
|
|
|
|
|
2001-03-17 00:50:51 -04:00
|
|
|
}
|
1997-01-06 18:59:08 -04:00
|
|
|
|
|
|
|
|
2001-03-17 00:50:51 -04:00
|
|
|
static int
|
1997-04-09 14:36:32 -03:00
|
|
|
read_cStringIO(Unpicklerobject *self, char **s, int n) {
|
|
|
|
char *ptr;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
if (PycStringIO->cread((PyObject *)self->file, &ptr, n) != n) {
|
|
|
|
PyErr_SetNone(PyExc_EOFError);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
*s = ptr;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
return n;
|
1997-01-06 18:59:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-03-17 00:50:51 -04:00
|
|
|
static int
|
1997-04-09 14:36:32 -03:00
|
|
|
readline_cStringIO(Unpicklerobject *self, char **s) {
|
|
|
|
int n;
|
|
|
|
char *ptr;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
if ((n = PycStringIO->creadline((PyObject *)self->file, &ptr)) < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
*s = ptr;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
return n;
|
1997-01-06 18:59:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-03-17 00:50:51 -04:00
|
|
|
static int
|
1997-04-09 14:36:32 -03:00
|
|
|
read_other(Unpicklerobject *self, char **s, int n) {
|
1998-11-25 12:18:00 -04:00
|
|
|
PyObject *bytes, *str=0;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1998-11-25 12:18:00 -04:00
|
|
|
UNLESS (bytes = PyInt_FromLong(n)) return -1;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1998-11-25 12:18:00 -04:00
|
|
|
ARG_TUP(self, bytes);
|
|
|
|
if (self->arg) {
|
|
|
|
str = PyObject_CallObject(self->read, self->arg);
|
|
|
|
FREE_ARG_TUP(self);
|
1997-01-06 18:59:08 -04:00
|
|
|
}
|
1998-11-25 12:18:00 -04:00
|
|
|
if (! str) return -1;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
Py_XDECREF(self->last_string);
|
|
|
|
self->last_string = str;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1998-11-25 12:18:00 -04:00
|
|
|
if (! (*s = PyString_AsString(str))) return -1;
|
|
|
|
return n;
|
1997-01-06 18:59:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-03-17 00:50:51 -04:00
|
|
|
static int
|
1997-04-09 14:36:32 -03:00
|
|
|
readline_other(Unpicklerobject *self, char **s) {
|
|
|
|
PyObject *str;
|
|
|
|
int str_size;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1998-11-25 12:18:00 -04:00
|
|
|
UNLESS (str = PyObject_CallObject(self->readline, empty_tuple)) {
|
1997-04-09 14:36:32 -03:00
|
|
|
return -1;
|
|
|
|
}
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1998-11-25 12:18:00 -04:00
|
|
|
if ((str_size = PyString_Size(str)) < 0)
|
|
|
|
return -1;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
Py_XDECREF(self->last_string);
|
|
|
|
self->last_string = str;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1998-11-25 12:18:00 -04:00
|
|
|
if (! (*s = PyString_AsString(str)))
|
|
|
|
return -1;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
return str_size;
|
|
|
|
}
|
1997-01-06 18:59:08 -04:00
|
|
|
|
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
static char *
|
1997-12-03 21:13:01 -04:00
|
|
|
pystrndup(char *s, int l) {
|
1997-04-09 14:36:32 -03:00
|
|
|
char *r;
|
1998-11-25 12:18:00 -04:00
|
|
|
UNLESS (r=malloc((l+1)*sizeof(char))) return (char*)PyErr_NoMemory();
|
1997-04-09 14:36:32 -03:00
|
|
|
memcpy(r,s,l);
|
|
|
|
r[l]=0;
|
|
|
|
return r;
|
1997-01-06 18:59:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
1997-04-09 14:36:32 -03:00
|
|
|
get(Picklerobject *self, PyObject *id) {
|
1998-11-25 12:18:00 -04:00
|
|
|
PyObject *value, *mv;
|
1997-04-09 14:36:32 -03:00
|
|
|
long c_value;
|
|
|
|
char s[30];
|
2000-06-28 19:23:56 -03:00
|
|
|
size_t len;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1998-11-25 12:18:00 -04:00
|
|
|
UNLESS (mv = PyDict_GetItem(self->memo, id)) {
|
|
|
|
PyErr_SetObject(PyExc_KeyError, id);
|
1997-04-09 14:36:32 -03:00
|
|
|
return -1;
|
1998-08-13 20:13:52 -03:00
|
|
|
}
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1998-11-25 12:18:00 -04:00
|
|
|
UNLESS (value = PyTuple_GetItem(mv, 0))
|
1997-04-09 14:36:32 -03:00
|
|
|
return -1;
|
2001-03-17 00:50:51 -04:00
|
|
|
|
1998-11-25 12:18:00 -04:00
|
|
|
UNLESS (PyInt_Check(value)) {
|
|
|
|
PyErr_SetString(PicklingError, "no int where int expected in memo");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
c_value = PyInt_AS_LONG((PyIntObject*)value);
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
if (!self->bin) {
|
|
|
|
s[0] = GET;
|
|
|
|
sprintf(s + 1, "%ld\n", c_value);
|
|
|
|
len = strlen(s);
|
|
|
|
}
|
1998-11-25 12:18:00 -04:00
|
|
|
else if (Pdata_Check(self->file)) {
|
|
|
|
if (write_other(self, NULL, 0) < 0) return -1;
|
|
|
|
PDATA_APPEND(self->file, mv, -1);
|
|
|
|
return 0;
|
|
|
|
}
|
1997-04-09 14:36:32 -03:00
|
|
|
else {
|
|
|
|
if (c_value < 256) {
|
|
|
|
s[0] = BINGET;
|
|
|
|
s[1] = (int)(c_value & 0xff);
|
|
|
|
len = 2;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
s[0] = LONG_BINGET;
|
|
|
|
s[1] = (int)(c_value & 0xff);
|
|
|
|
s[2] = (int)((c_value >> 8) & 0xff);
|
|
|
|
s[3] = (int)((c_value >> 16) & 0xff);
|
|
|
|
s[4] = (int)((c_value >> 24) & 0xff);
|
|
|
|
len = 5;
|
|
|
|
}
|
|
|
|
}
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
if ((*self->write_func)(self, s, len) < 0)
|
|
|
|
return -1;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
return 0;
|
|
|
|
}
|
2001-03-17 00:50:51 -04:00
|
|
|
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
static int
|
|
|
|
put(Picklerobject *self, PyObject *ob) {
|
1998-08-13 20:13:52 -03:00
|
|
|
if (ob->ob_refcnt < 2 || self->fast)
|
1997-04-09 14:36:32 -03:00
|
|
|
return 0;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
return put2(self, ob);
|
|
|
|
}
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2001-03-17 00:50:51 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
static int
|
|
|
|
put2(Picklerobject *self, PyObject *ob) {
|
|
|
|
char c_str[30];
|
2000-06-28 19:23:56 -03:00
|
|
|
int p;
|
|
|
|
size_t len;
|
|
|
|
int res = -1;
|
1997-04-09 14:36:32 -03:00
|
|
|
PyObject *py_ob_id = 0, *memo_len = 0, *t = 0;
|
1998-08-13 20:13:52 -03:00
|
|
|
|
|
|
|
if (self->fast) return 0;
|
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
if ((p = PyDict_Size(self->memo)) < 0)
|
|
|
|
goto finally;
|
|
|
|
|
1998-11-25 12:18:00 -04:00
|
|
|
p++; /* Make sure memo keys are positive! */
|
|
|
|
|
2000-06-28 19:23:56 -03:00
|
|
|
UNLESS (py_ob_id = PyLong_FromVoidPtr(ob))
|
1998-11-25 12:18:00 -04:00
|
|
|
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;
|
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
if (!self->bin) {
|
|
|
|
c_str[0] = PUT;
|
|
|
|
sprintf(c_str + 1, "%d\n", p);
|
|
|
|
len = strlen(c_str);
|
|
|
|
}
|
1998-11-25 12:18:00 -04:00
|
|
|
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;
|
|
|
|
}
|
1997-04-09 14:36:32 -03:00
|
|
|
else {
|
|
|
|
if (p >= 256) {
|
|
|
|
c_str[0] = LONG_BINPUT;
|
|
|
|
c_str[1] = (int)(p & 0xff);
|
|
|
|
c_str[2] = (int)((p >> 8) & 0xff);
|
|
|
|
c_str[3] = (int)((p >> 16) & 0xff);
|
|
|
|
c_str[4] = (int)((p >> 24) & 0xff);
|
|
|
|
len = 5;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
c_str[0] = BINPUT;
|
|
|
|
c_str[1] = p;
|
2001-03-17 00:50:51 -04:00
|
|
|
len = 2;
|
1997-04-09 14:36:32 -03:00
|
|
|
}
|
|
|
|
}
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
if ((*self->write_func)(self, c_str, len) < 0)
|
|
|
|
goto finally;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
res = 0;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
finally:
|
|
|
|
Py_XDECREF(py_ob_id);
|
|
|
|
Py_XDECREF(memo_len);
|
|
|
|
Py_XDECREF(t);
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
return res;
|
1997-01-06 18:59:08 -04:00
|
|
|
}
|
|
|
|
|
1997-12-03 21:13:01 -04:00
|
|
|
#define PyImport_Import cPickle_Import
|
1997-01-06 18:59:08 -04:00
|
|
|
|
|
|
|
static PyObject *
|
1997-12-03 21:13:01 -04:00
|
|
|
PyImport_Import(PyObject *module_name) {
|
|
|
|
static PyObject *silly_list=0, *__builtins___str=0, *__import___str;
|
|
|
|
static PyObject *standard_builtins=0;
|
|
|
|
PyObject *globals=0, *__import__=0, *__builtins__=0, *r=0;
|
|
|
|
|
1998-11-25 12:18:00 -04:00
|
|
|
UNLESS (silly_list) {
|
|
|
|
UNLESS (__import___str=PyString_FromString("__import__"))
|
|
|
|
return NULL;
|
|
|
|
UNLESS (__builtins___str=PyString_FromString("__builtins__"))
|
|
|
|
return NULL;
|
|
|
|
UNLESS (silly_list=Py_BuildValue("[s]","__doc__"))
|
|
|
|
return NULL;
|
1997-12-03 21:13:01 -04:00
|
|
|
}
|
|
|
|
|
1998-11-25 12:18:00 -04:00
|
|
|
if ((globals=PyEval_GetGlobals())) {
|
1997-12-03 21:13:01 -04:00
|
|
|
Py_INCREF(globals);
|
1998-11-25 12:18:00 -04:00
|
|
|
UNLESS (__builtins__=PyObject_GetItem(globals,__builtins___str))
|
|
|
|
goto err;
|
1997-12-03 21:13:01 -04:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
PyErr_Clear();
|
|
|
|
|
1998-11-25 12:18:00 -04:00
|
|
|
UNLESS (standard_builtins ||
|
|
|
|
(standard_builtins=PyImport_ImportModule("__builtin__")))
|
|
|
|
return NULL;
|
2001-03-17 00:50:51 -04:00
|
|
|
|
1997-12-03 21:13:01 -04:00
|
|
|
__builtins__=standard_builtins;
|
|
|
|
Py_INCREF(__builtins__);
|
1998-11-25 12:18:00 -04:00
|
|
|
UNLESS (globals = Py_BuildValue("{sO}", "__builtins__", __builtins__))
|
|
|
|
goto err;
|
1997-12-03 21:13:01 -04:00
|
|
|
}
|
|
|
|
|
1998-11-25 12:18:00 -04:00
|
|
|
if (PyDict_Check(__builtins__)) {
|
|
|
|
UNLESS (__import__=PyObject_GetItem(__builtins__,__import___str)) goto err;
|
1997-12-03 21:13:01 -04:00
|
|
|
}
|
|
|
|
else {
|
1998-11-25 12:18:00 -04:00
|
|
|
UNLESS (__import__=PyObject_GetAttr(__builtins__,__import___str)) goto err;
|
1997-12-03 21:13:01 -04:00
|
|
|
}
|
|
|
|
|
1998-11-25 12:18:00 -04:00
|
|
|
UNLESS (r=PyObject_CallFunction(__import__,"OOOO",
|
|
|
|
module_name, globals, globals, silly_list))
|
1997-12-03 21:13:01 -04:00
|
|
|
goto err;
|
|
|
|
|
|
|
|
Py_DECREF(globals);
|
|
|
|
Py_DECREF(__builtins__);
|
|
|
|
Py_DECREF(__import__);
|
2001-03-17 00:50:51 -04:00
|
|
|
|
1997-12-03 21:13:01 -04:00
|
|
|
return r;
|
|
|
|
err:
|
|
|
|
Py_XDECREF(globals);
|
|
|
|
Py_XDECREF(__builtins__);
|
|
|
|
Py_XDECREF(__import__);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
1998-08-08 16:40:10 -03:00
|
|
|
whichmodule(PyObject *global, PyObject *global_name) {
|
1997-04-09 14:36:32 -03:00
|
|
|
int i, j;
|
|
|
|
PyObject *module = 0, *modules_dict = 0,
|
|
|
|
*global_name_attr = 0, *name = 0;
|
|
|
|
|
1997-09-28 02:38:51 -03:00
|
|
|
module = PyObject_GetAttrString(global, "__module__");
|
|
|
|
if (module) return module;
|
|
|
|
PyErr_Clear();
|
|
|
|
|
1998-11-25 12:18:00 -04:00
|
|
|
UNLESS (modules_dict = PySys_GetObject("modules"))
|
1997-04-09 14:36:32 -03:00
|
|
|
return NULL;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
i = 0;
|
1997-08-13 00:14:41 -03:00
|
|
|
while ((j = PyDict_Next(modules_dict, &i, &name, &module))) {
|
|
|
|
|
1998-11-25 12:18:00 -04:00
|
|
|
if (PyObject_Compare(name, __main___str)==0) continue;
|
2001-03-17 00:50:51 -04:00
|
|
|
|
1998-11-25 12:18:00 -04:00
|
|
|
UNLESS (global_name_attr = PyObject_GetAttr(module, global_name)) {
|
1997-04-09 14:36:32 -03:00
|
|
|
PyErr_Clear();
|
|
|
|
continue;
|
|
|
|
}
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
if (global_name_attr != global) {
|
|
|
|
Py_DECREF(global_name_attr);
|
|
|
|
continue;
|
|
|
|
}
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
Py_DECREF(global_name_attr);
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
break;
|
|
|
|
}
|
1997-08-13 00:14:41 -03:00
|
|
|
|
|
|
|
/* The following implements the rule in pickle.py added in 1.5
|
|
|
|
that used __main__ if no module is found. I don't actually
|
|
|
|
like this rule. jlf
|
|
|
|
*/
|
1998-11-25 12:18:00 -04:00
|
|
|
if (!j) {
|
1997-08-13 00:14:41 -03:00
|
|
|
j=1;
|
|
|
|
name=__main___str;
|
|
|
|
}
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
Py_INCREF(name);
|
|
|
|
return name;
|
|
|
|
}
|
1997-01-06 18:59:08 -04:00
|
|
|
|
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
static int
|
|
|
|
save_none(Picklerobject *self, PyObject *args) {
|
|
|
|
static char none = NONE;
|
2001-03-17 00:50:51 -04:00
|
|
|
if ((*self->write_func)(self, &none, 1) < 0)
|
1997-04-09 14:36:32 -03:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2001-03-17 00:50:51 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
static int
|
|
|
|
save_int(Picklerobject *self, PyObject *args) {
|
|
|
|
char c_str[32];
|
|
|
|
long l = PyInt_AS_LONG((PyIntObject *)args);
|
|
|
|
int len = 0;
|
|
|
|
|
1997-05-13 15:00:44 -03:00
|
|
|
if (!self->bin
|
|
|
|
#if SIZEOF_LONG > 4
|
2001-04-10 01:22:00 -03:00
|
|
|
|| l > 0x7fffffffL
|
|
|
|
|| l < -0x80000000L
|
1997-05-13 15:00:44 -03:00
|
|
|
#endif
|
2001-04-10 01:22:00 -03:00
|
|
|
) {
|
|
|
|
/* Text-mode pickle, or long too big to fit in the 4-byte
|
|
|
|
* signed BININT format: store as a string.
|
|
|
|
*/
|
1997-04-09 14:36:32 -03:00
|
|
|
c_str[0] = INT;
|
|
|
|
sprintf(c_str + 1, "%ld\n", l);
|
|
|
|
if ((*self->write_func)(self, c_str, strlen(c_str)) < 0)
|
|
|
|
return -1;
|
1997-01-06 18:59:08 -04:00
|
|
|
}
|
1997-04-09 14:36:32 -03:00
|
|
|
else {
|
2001-04-10 01:35:28 -03:00
|
|
|
/* Binary pickle and l fits in a signed 4-byte int. */
|
1997-04-09 14:36:32 -03:00
|
|
|
c_str[1] = (int)( l & 0xff);
|
|
|
|
c_str[2] = (int)((l >> 8) & 0xff);
|
|
|
|
c_str[3] = (int)((l >> 16) & 0xff);
|
|
|
|
c_str[4] = (int)((l >> 24) & 0xff);
|
|
|
|
|
|
|
|
if ((c_str[4] == 0) && (c_str[3] == 0)) {
|
|
|
|
if (c_str[2] == 0) {
|
|
|
|
c_str[0] = BININT1;
|
|
|
|
len = 2;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
c_str[0] = BININT2;
|
|
|
|
len = 3;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
c_str[0] = BININT;
|
|
|
|
len = 5;
|
|
|
|
}
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
if ((*self->write_func)(self, c_str, len) < 0)
|
|
|
|
return -1;
|
1997-01-06 18:59:08 -04:00
|
|
|
}
|
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
return 0;
|
|
|
|
}
|
1997-01-06 18:59:08 -04:00
|
|
|
|
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
static int
|
|
|
|
save_long(Picklerobject *self, PyObject *args) {
|
|
|
|
int size, res = -1;
|
|
|
|
PyObject *repr = 0;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
static char l = LONG;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1998-11-25 12:18:00 -04:00
|
|
|
UNLESS (repr = PyObject_Repr(args))
|
1997-04-09 14:36:32 -03:00
|
|
|
goto finally;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
if ((size = PyString_Size(repr)) < 0)
|
|
|
|
goto finally;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
if ((*self->write_func)(self, &l, 1) < 0)
|
|
|
|
goto finally;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2001-03-17 00:50:51 -04:00
|
|
|
if ((*self->write_func)(self,
|
1997-04-09 14:36:32 -03:00
|
|
|
PyString_AS_STRING((PyStringObject *)repr), size) < 0)
|
|
|
|
goto finally;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
if ((*self->write_func)(self, "\n", 1) < 0)
|
|
|
|
goto finally;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
res = 0;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
finally:
|
|
|
|
Py_XDECREF(repr);
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
save_float(Picklerobject *self, PyObject *args) {
|
|
|
|
double x = PyFloat_AS_DOUBLE((PyFloatObject *)args);
|
|
|
|
|
|
|
|
if (self->bin) {
|
1997-08-13 00:14:41 -03:00
|
|
|
int s, e;
|
1997-04-09 14:36:32 -03:00
|
|
|
double f;
|
|
|
|
long fhi, flo;
|
2001-07-17 15:34:03 -03:00
|
|
|
char str[9];
|
|
|
|
unsigned char *p = (unsigned char *)str;
|
1997-04-09 14:36:32 -03:00
|
|
|
|
|
|
|
*p = BINFLOAT;
|
|
|
|
p++;
|
|
|
|
|
|
|
|
if (x < 0) {
|
|
|
|
s = 1;
|
|
|
|
x = -x;
|
1997-01-06 18:59:08 -04:00
|
|
|
}
|
|
|
|
else
|
1997-04-09 14:36:32 -03:00
|
|
|
s = 0;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
f = frexp(x, &e);
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
/* Normalize f to be in the range [1.0, 2.0) */
|
|
|
|
if (0.5 <= f && f < 1.0) {
|
|
|
|
f *= 2.0;
|
|
|
|
e--;
|
|
|
|
}
|
|
|
|
else if (f == 0.0) {
|
|
|
|
e = 0;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
PyErr_SetString(PyExc_SystemError,
|
|
|
|
"frexp() result out of range");
|
|
|
|
return -1;
|
|
|
|
}
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
if (e >= 1024) {
|
|
|
|
/* XXX 1024 itself is reserved for Inf/NaN */
|
|
|
|
PyErr_SetString(PyExc_OverflowError,
|
|
|
|
"float too large to pack with d format");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
else if (e < -1022) {
|
|
|
|
/* Gradual underflow */
|
|
|
|
f = ldexp(f, 1022 + e);
|
|
|
|
e = 0;
|
|
|
|
}
|
1998-11-25 12:18:00 -04:00
|
|
|
else if (!(e == 0 && f == 0.0)) {
|
1997-04-09 14:36:32 -03:00
|
|
|
e += 1023;
|
|
|
|
f -= 1.0; /* Get rid of leading 1 */
|
|
|
|
}
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
/* fhi receives the high 28 bits; flo the low 24 bits (== 52 bits) */
|
|
|
|
f *= 268435456.0; /* 2**28 */
|
|
|
|
fhi = (long) floor(f); /* Truncate */
|
|
|
|
f -= (double)fhi;
|
|
|
|
f *= 16777216.0; /* 2**24 */
|
|
|
|
flo = (long) floor(f + 0.5); /* Round */
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
/* First byte */
|
|
|
|
*p = (s<<7) | (e>>4);
|
|
|
|
p++;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
/* Second byte */
|
2001-07-17 15:34:03 -03:00
|
|
|
*p = (unsigned char) (((e&0xF)<<4) | (fhi>>24));
|
1997-04-09 14:36:32 -03:00
|
|
|
p++;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
/* Third byte */
|
2001-07-17 15:34:03 -03:00
|
|
|
*p = (unsigned char) ((fhi>>16) & 0xFF);
|
1997-04-09 14:36:32 -03:00
|
|
|
p++;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
/* Fourth byte */
|
2001-07-17 15:34:03 -03:00
|
|
|
*p = (unsigned char) ((fhi>>8) & 0xFF);
|
1997-04-09 14:36:32 -03:00
|
|
|
p++;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
/* Fifth byte */
|
2001-07-17 15:34:03 -03:00
|
|
|
*p = (unsigned char) (fhi & 0xFF);
|
1997-04-09 14:36:32 -03:00
|
|
|
p++;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
/* Sixth byte */
|
2001-07-17 15:34:03 -03:00
|
|
|
*p = (unsigned char) ((flo>>16) & 0xFF);
|
1997-04-09 14:36:32 -03:00
|
|
|
p++;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
/* Seventh byte */
|
2001-07-17 15:34:03 -03:00
|
|
|
*p = (unsigned char) ((flo>>8) & 0xFF);
|
1997-04-09 14:36:32 -03:00
|
|
|
p++;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
/* Eighth byte */
|
2001-07-17 15:34:03 -03:00
|
|
|
*p = (unsigned char) (flo & 0xFF);
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
if ((*self->write_func)(self, str, 9) < 0)
|
|
|
|
return -1;
|
|
|
|
}
|
1998-11-25 12:18:00 -04:00
|
|
|
else {
|
1997-04-09 14:36:32 -03:00
|
|
|
char c_str[250];
|
|
|
|
c_str[0] = FLOAT;
|
1998-04-03 17:13:02 -04:00
|
|
|
sprintf(c_str + 1, "%.17g\n", x);
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
if ((*self->write_func)(self, c_str, strlen(c_str)) < 0)
|
|
|
|
return -1;
|
|
|
|
}
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
return 0;
|
|
|
|
}
|
1997-01-06 18:59:08 -04:00
|
|
|
|
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
static int
|
1997-08-13 00:14:41 -03:00
|
|
|
save_string(Picklerobject *self, PyObject *args, int doput) {
|
1997-04-09 14:36:32 -03:00
|
|
|
int size, len;
|
1998-11-25 12:18:00 -04:00
|
|
|
PyObject *repr=0;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1998-11-25 12:18:00 -04:00
|
|
|
if ((size = PyString_Size(args)) < 0)
|
|
|
|
return -1;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
if (!self->bin) {
|
|
|
|
char *repr_str;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
static char string = STRING;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1998-11-25 12:18:00 -04:00
|
|
|
UNLESS (repr = PyObject_Repr(args))
|
1997-04-09 14:36:32 -03:00
|
|
|
return -1;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1998-11-25 12:18:00 -04:00
|
|
|
if ((len = PyString_Size(repr)) < 0)
|
|
|
|
goto err;
|
1997-04-09 14:36:32 -03:00
|
|
|
repr_str = PyString_AS_STRING((PyStringObject *)repr);
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
if ((*self->write_func)(self, &string, 1) < 0)
|
1998-11-25 12:18:00 -04:00
|
|
|
goto err;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
if ((*self->write_func)(self, repr_str, len) < 0)
|
1998-11-25 12:18:00 -04:00
|
|
|
goto err;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
if ((*self->write_func)(self, "\n", 1) < 0)
|
1998-11-25 12:18:00 -04:00
|
|
|
goto err;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
Py_XDECREF(repr);
|
1997-01-06 18:59:08 -04:00
|
|
|
}
|
1997-04-09 14:36:32 -03:00
|
|
|
else {
|
|
|
|
int i;
|
|
|
|
char c_str[5];
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1998-11-25 12:18:00 -04:00
|
|
|
if ((size = PyString_Size(args)) < 0)
|
|
|
|
return -1;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
if (size < 256) {
|
|
|
|
c_str[0] = SHORT_BINSTRING;
|
|
|
|
c_str[1] = size;
|
|
|
|
len = 2;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
c_str[0] = BINSTRING;
|
|
|
|
for (i = 1; i < 5; i++)
|
|
|
|
c_str[i] = (int)(size >> ((i - 1) * 8));
|
|
|
|
len = 5;
|
|
|
|
}
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
if ((*self->write_func)(self, c_str, len) < 0)
|
|
|
|
return -1;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1998-11-25 12:18:00 -04:00
|
|
|
if (size > 128 && Pdata_Check(self->file)) {
|
|
|
|
if (write_other(self, NULL, 0) < 0) return -1;
|
|
|
|
PDATA_APPEND(self->file, args, -1);
|
|
|
|
}
|
|
|
|
else {
|
2001-03-17 00:50:51 -04:00
|
|
|
if ((*self->write_func)(self,
|
1998-11-25 12:18:00 -04:00
|
|
|
PyString_AS_STRING((PyStringObject *)args), size) < 0)
|
1997-04-09 14:36:32 -03:00
|
|
|
return -1;
|
1998-11-25 12:18:00 -04:00
|
|
|
}
|
1997-04-09 14:36:32 -03:00
|
|
|
}
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-08-13 00:14:41 -03:00
|
|
|
if (doput)
|
|
|
|
if (put(self, args) < 0)
|
1998-11-25 12:18:00 -04:00
|
|
|
return -1;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
return 0;
|
1998-11-25 12:18:00 -04:00
|
|
|
|
|
|
|
err:
|
|
|
|
Py_XDECREF(repr);
|
|
|
|
return -1;
|
1997-01-06 18:59:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-12-18 22:08:38 -04:00
|
|
|
/* A copy of PyUnicode_EncodeRawUnicodeEscape() that also translates
|
|
|
|
backslash and newline characters to \uXXXX escapes. */
|
|
|
|
static PyObject *
|
|
|
|
modified_EncodeRawUnicodeEscape(const Py_UNICODE *s, int size)
|
|
|
|
{
|
|
|
|
PyObject *repr;
|
|
|
|
char *p;
|
|
|
|
char *q;
|
|
|
|
|
|
|
|
static const char *hexdigit = "0123456789ABCDEF";
|
|
|
|
|
|
|
|
repr = PyString_FromStringAndSize(NULL, 6 * size);
|
|
|
|
if (repr == NULL)
|
|
|
|
return NULL;
|
|
|
|
if (size == 0)
|
|
|
|
return repr;
|
|
|
|
|
|
|
|
p = q = PyString_AS_STRING(repr);
|
|
|
|
while (size-- > 0) {
|
|
|
|
Py_UNICODE ch = *s++;
|
|
|
|
/* Map 16-bit characters to '\uxxxx' */
|
|
|
|
if (ch >= 256 || ch == '\\' || ch == '\n') {
|
|
|
|
*p++ = '\\';
|
|
|
|
*p++ = 'u';
|
|
|
|
*p++ = hexdigit[(ch >> 12) & 0xf];
|
|
|
|
*p++ = hexdigit[(ch >> 8) & 0xf];
|
|
|
|
*p++ = hexdigit[(ch >> 4) & 0xf];
|
|
|
|
*p++ = hexdigit[ch & 15];
|
|
|
|
}
|
|
|
|
/* Copy everything else as-is */
|
|
|
|
else
|
|
|
|
*p++ = (char) ch;
|
|
|
|
}
|
|
|
|
*p = '\0';
|
|
|
|
if (_PyString_Resize(&repr, p - q))
|
|
|
|
goto onError;
|
|
|
|
|
|
|
|
return repr;
|
|
|
|
|
|
|
|
onError:
|
|
|
|
Py_DECREF(repr);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-03-10 19:11:40 -04:00
|
|
|
static int
|
|
|
|
save_unicode(Picklerobject *self, PyObject *args, int doput) {
|
|
|
|
int size, len;
|
|
|
|
PyObject *repr=0;
|
|
|
|
|
|
|
|
if (!PyUnicode_Check(args))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (!self->bin) {
|
|
|
|
char *repr_str;
|
|
|
|
static char string = UNICODE;
|
|
|
|
|
2000-12-18 22:08:38 -04:00
|
|
|
UNLESS(repr = modified_EncodeRawUnicodeEscape(
|
|
|
|
PyUnicode_AS_UNICODE(args), PyUnicode_GET_SIZE(args)))
|
2000-03-10 19:11:40 -04:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
if ((len = PyString_Size(repr)) < 0)
|
|
|
|
goto err;
|
|
|
|
repr_str = PyString_AS_STRING((PyStringObject *)repr);
|
|
|
|
|
|
|
|
if ((*self->write_func)(self, &string, 1) < 0)
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
if ((*self->write_func)(self, repr_str, len) < 0)
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
if ((*self->write_func)(self, "\n", 1) < 0)
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
Py_XDECREF(repr);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
int i;
|
|
|
|
char c_str[5];
|
|
|
|
|
|
|
|
UNLESS (repr = PyUnicode_AsUTF8String(args))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if ((size = PyString_Size(repr)) < 0)
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
c_str[0] = BINUNICODE;
|
|
|
|
for (i = 1; i < 5; i++)
|
|
|
|
c_str[i] = (int)(size >> ((i - 1) * 8));
|
|
|
|
len = 5;
|
|
|
|
|
|
|
|
if ((*self->write_func)(self, c_str, len) < 0)
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
if (size > 128 && Pdata_Check(self->file)) {
|
|
|
|
if (write_other(self, NULL, 0) < 0)
|
|
|
|
goto err;
|
|
|
|
PDATA_APPEND(self->file, repr, -1);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if ((*self->write_func)(self, PyString_AS_STRING(repr), size) < 0)
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
Py_DECREF(repr);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (doput)
|
|
|
|
if (put(self, args) < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
err:
|
|
|
|
Py_XDECREF(repr);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
static int
|
|
|
|
save_tuple(Picklerobject *self, PyObject *args) {
|
|
|
|
PyObject *element = 0, *py_tuple_id = 0;
|
|
|
|
int len, i, has_key, res = -1;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
static char tuple = TUPLE;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
if ((*self->write_func)(self, &MARKv, 1) < 0)
|
|
|
|
goto finally;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2001-03-17 00:50:51 -04:00
|
|
|
if ((len = PyTuple_Size(args)) < 0)
|
1997-04-09 14:36:32 -03:00
|
|
|
goto finally;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
for (i = 0; i < len; i++) {
|
2001-03-17 00:50:51 -04:00
|
|
|
UNLESS (element = PyTuple_GET_ITEM((PyTupleObject *)args, i))
|
1997-04-09 14:36:32 -03:00
|
|
|
goto finally;
|
2001-03-17 00:50:51 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
if (save(self, element, 0) < 0)
|
|
|
|
goto finally;
|
|
|
|
}
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2000-06-28 19:23:56 -03:00
|
|
|
UNLESS (py_tuple_id = PyLong_FromVoidPtr(args))
|
1997-04-09 14:36:32 -03:00
|
|
|
goto finally;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
if (len) {
|
1997-08-13 00:14:41 -03:00
|
|
|
if ((has_key = cPickle_PyMapping_HasKey(self->memo, py_tuple_id)) < 0)
|
1997-04-09 14:36:32 -03:00
|
|
|
goto finally;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
if (has_key) {
|
|
|
|
if (self->bin) {
|
|
|
|
static char pop_mark = POP_MARK;
|
2001-03-17 00:50:51 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
if ((*self->write_func)(self, &pop_mark, 1) < 0)
|
|
|
|
goto finally;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
static char pop = POP;
|
2001-03-17 00:50:51 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
for (i = 0; i <= len; i++) {
|
|
|
|
if ((*self->write_func)(self, &pop, 1) < 0)
|
|
|
|
goto finally;
|
2001-03-17 00:50:51 -04:00
|
|
|
}
|
1997-04-09 14:36:32 -03:00
|
|
|
}
|
2001-03-17 00:50:51 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
if (get(self, py_tuple_id) < 0)
|
|
|
|
goto finally;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
res = 0;
|
|
|
|
goto finally;
|
|
|
|
}
|
1997-01-06 18:59:08 -04:00
|
|
|
}
|
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
if ((*self->write_func)(self, &tuple, 1) < 0) {
|
|
|
|
goto finally;
|
1997-01-06 18:59:08 -04:00
|
|
|
}
|
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
if (put(self, args) < 0)
|
|
|
|
goto finally;
|
2001-03-17 00:50:51 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
res = 0;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
finally:
|
|
|
|
Py_XDECREF(py_tuple_id);
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
return res;
|
1997-01-06 18:59:08 -04:00
|
|
|
}
|
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
static int
|
|
|
|
save_empty_tuple(Picklerobject *self, PyObject *args) {
|
|
|
|
static char tuple = EMPTY_TUPLE;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
return (*self->write_func)(self, &tuple, 1);
|
|
|
|
}
|
1997-01-06 18:59:08 -04:00
|
|
|
|
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
static int
|
|
|
|
save_list(Picklerobject *self, PyObject *args) {
|
|
|
|
PyObject *element = 0;
|
|
|
|
int s_len, len, i, using_appends, res = -1;
|
|
|
|
char s[3];
|
|
|
|
|
|
|
|
static char append = APPEND, appends = APPENDS;
|
|
|
|
|
1998-11-25 12:18:00 -04:00
|
|
|
if (self->bin) {
|
1997-04-09 14:36:32 -03:00
|
|
|
s[0] = EMPTY_LIST;
|
|
|
|
s_len = 1;
|
2001-03-17 00:50:51 -04:00
|
|
|
}
|
1997-04-09 14:36:32 -03:00
|
|
|
else {
|
|
|
|
s[0] = MARK;
|
|
|
|
s[1] = LIST;
|
|
|
|
s_len = 2;
|
|
|
|
}
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
if ((len = PyList_Size(args)) < 0)
|
|
|
|
goto finally;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
if ((*self->write_func)(self, s, s_len) < 0)
|
|
|
|
goto finally;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-12-03 21:13:01 -04:00
|
|
|
if (len == 0) {
|
1997-04-09 14:36:32 -03:00
|
|
|
if (put(self, args) < 0)
|
|
|
|
goto finally;
|
|
|
|
}
|
1997-12-03 21:13:01 -04:00
|
|
|
else {
|
1997-04-09 14:36:32 -03:00
|
|
|
if (put2(self, args) < 0)
|
|
|
|
goto finally;
|
1997-01-06 18:59:08 -04:00
|
|
|
}
|
|
|
|
|
1997-08-13 00:14:41 -03:00
|
|
|
if ((using_appends = (self->bin && (len > 1))))
|
1997-04-09 14:36:32 -03:00
|
|
|
if ((*self->write_func)(self, &MARKv, 1) < 0)
|
|
|
|
goto finally;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
for (i = 0; i < len; i++) {
|
2001-03-17 00:50:51 -04:00
|
|
|
UNLESS (element = PyList_GET_ITEM((PyListObject *)args, i))
|
1997-04-09 14:36:32 -03:00
|
|
|
goto finally;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2001-03-17 00:50:51 -04:00
|
|
|
if (save(self, element, 0) < 0)
|
|
|
|
goto finally;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
if (!using_appends) {
|
|
|
|
if ((*self->write_func)(self, &append, 1) < 0)
|
|
|
|
goto finally;
|
|
|
|
}
|
|
|
|
}
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
if (using_appends) {
|
|
|
|
if ((*self->write_func)(self, &appends, 1) < 0)
|
|
|
|
goto finally;
|
|
|
|
}
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
res = 0;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
finally:
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
return res;
|
|
|
|
}
|
1997-01-06 18:59:08 -04:00
|
|
|
|
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
static int
|
|
|
|
save_dict(Picklerobject *self, PyObject *args) {
|
|
|
|
PyObject *key = 0, *value = 0;
|
|
|
|
int i, len, res = -1, using_setitems;
|
|
|
|
char s[3];
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
static char setitem = SETITEM, setitems = SETITEMS;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
if (self->bin) {
|
1998-11-25 12:18:00 -04:00
|
|
|
s[0] = EMPTY_DICT;
|
|
|
|
len = 1;
|
1997-04-09 14:36:32 -03:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
s[0] = MARK;
|
|
|
|
s[1] = DICT;
|
|
|
|
len = 2;
|
|
|
|
}
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
if ((*self->write_func)(self, s, len) < 0)
|
|
|
|
goto finally;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
if ((len = PyDict_Size(args)) < 0)
|
|
|
|
goto finally;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-12-03 21:13:01 -04:00
|
|
|
if (len == 0) {
|
1997-04-09 14:36:32 -03:00
|
|
|
if (put(self, args) < 0)
|
|
|
|
goto finally;
|
|
|
|
}
|
1997-12-03 21:13:01 -04:00
|
|
|
else {
|
1997-04-09 14:36:32 -03:00
|
|
|
if (put2(self, args) < 0)
|
|
|
|
goto finally;
|
1997-01-06 18:59:08 -04:00
|
|
|
}
|
|
|
|
|
1997-08-13 00:14:41 -03:00
|
|
|
if ((using_setitems = (self->bin && (PyDict_Size(args) > 1))))
|
1997-04-09 14:36:32 -03:00
|
|
|
if ((*self->write_func)(self, &MARKv, 1) < 0)
|
|
|
|
goto finally;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
i = 0;
|
|
|
|
while (PyDict_Next(args, &i, &key, &value)) {
|
|
|
|
if (save(self, key, 0) < 0)
|
|
|
|
goto finally;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
if (save(self, value, 0) < 0)
|
|
|
|
goto finally;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
if (!using_setitems) {
|
|
|
|
if ((*self->write_func)(self, &setitem, 1) < 0)
|
|
|
|
goto finally;
|
|
|
|
}
|
|
|
|
}
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
if (using_setitems) {
|
|
|
|
if ((*self->write_func)(self, &setitems, 1) < 0)
|
|
|
|
goto finally;
|
|
|
|
}
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
res = 0;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
finally:
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
return res;
|
|
|
|
}
|
1997-01-06 18:59:08 -04:00
|
|
|
|
|
|
|
|
2001-03-17 00:50:51 -04:00
|
|
|
static int
|
1997-04-09 14:36:32 -03:00
|
|
|
save_inst(Picklerobject *self, PyObject *args) {
|
2001-03-17 00:50:51 -04:00
|
|
|
PyObject *class = 0, *module = 0, *name = 0, *state = 0,
|
1997-04-09 14:36:32 -03:00
|
|
|
*getinitargs_func = 0, *getstate_func = 0, *class_args = 0;
|
|
|
|
char *module_str, *name_str;
|
|
|
|
int module_size, name_size, res = -1;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
static char inst = INST, obj = OBJ, build = BUILD;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
if ((*self->write_func)(self, &MARKv, 1) < 0)
|
|
|
|
goto finally;
|
|
|
|
|
1998-11-25 12:18:00 -04:00
|
|
|
UNLESS (class = PyObject_GetAttr(args, __class___str))
|
1997-04-09 14:36:32 -03:00
|
|
|
goto finally;
|
|
|
|
|
|
|
|
if (self->bin) {
|
|
|
|
if (save(self, class, 0) < 0)
|
|
|
|
goto finally;
|
1997-01-06 18:59:08 -04:00
|
|
|
}
|
|
|
|
|
1997-08-13 00:14:41 -03:00
|
|
|
if ((getinitargs_func = PyObject_GetAttr(args, __getinitargs___str))) {
|
1997-04-09 14:36:32 -03:00
|
|
|
PyObject *element = 0;
|
|
|
|
int i, len;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2001-03-17 00:50:51 -04:00
|
|
|
UNLESS (class_args =
|
1997-04-09 14:36:32 -03:00
|
|
|
PyObject_CallObject(getinitargs_func, empty_tuple))
|
|
|
|
goto finally;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2001-03-17 00:50:51 -04:00
|
|
|
if ((len = PyObject_Size(class_args)) < 0)
|
1997-04-09 14:36:32 -03:00
|
|
|
goto finally;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
for (i = 0; i < len; i++) {
|
2001-03-17 00:50:51 -04:00
|
|
|
UNLESS (element = PySequence_GetItem(class_args, i))
|
1997-04-09 14:36:32 -03:00
|
|
|
goto finally;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
if (save(self, element, 0) < 0) {
|
|
|
|
Py_DECREF(element);
|
|
|
|
goto finally;
|
|
|
|
}
|
|
|
|
|
|
|
|
Py_DECREF(element);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
PyErr_Clear();
|
1997-01-06 18:59:08 -04:00
|
|
|
}
|
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
if (!self->bin) {
|
1998-11-25 12:18:00 -04:00
|
|
|
UNLESS (name = ((PyClassObject *)class)->cl_name) {
|
1997-04-09 14:36:32 -03:00
|
|
|
PyErr_SetString(PicklingError, "class has no name");
|
|
|
|
goto finally;
|
|
|
|
}
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1998-11-25 12:18:00 -04:00
|
|
|
UNLESS (module = whichmodule(class, name))
|
1997-04-09 14:36:32 -03:00
|
|
|
goto finally;
|
1998-11-25 12:18:00 -04:00
|
|
|
|
2001-03-17 00:50:51 -04:00
|
|
|
|
1998-11-25 12:18:00 -04:00
|
|
|
if ((module_size = PyString_Size(module)) < 0 ||
|
|
|
|
(name_size = PyString_Size(name)) < 0)
|
|
|
|
goto finally;
|
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
module_str = PyString_AS_STRING((PyStringObject *)module);
|
|
|
|
name_str = PyString_AS_STRING((PyStringObject *)name);
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
if ((*self->write_func)(self, &inst, 1) < 0)
|
|
|
|
goto finally;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
if ((*self->write_func)(self, module_str, module_size) < 0)
|
|
|
|
goto finally;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
if ((*self->write_func)(self, "\n", 1) < 0)
|
|
|
|
goto finally;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
if ((*self->write_func)(self, name_str, name_size) < 0)
|
|
|
|
goto finally;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
if ((*self->write_func)(self, "\n", 1) < 0)
|
|
|
|
goto finally;
|
1997-01-06 18:59:08 -04:00
|
|
|
}
|
1997-04-09 14:36:32 -03:00
|
|
|
else if ((*self->write_func)(self, &obj, 1) < 0) {
|
|
|
|
goto finally;
|
1997-01-06 18:59:08 -04:00
|
|
|
}
|
|
|
|
|
1997-08-13 00:14:41 -03:00
|
|
|
if ((getstate_func = PyObject_GetAttr(args, __getstate___str))) {
|
1998-11-25 12:18:00 -04:00
|
|
|
UNLESS (state = PyObject_CallObject(getstate_func, empty_tuple))
|
1997-04-09 14:36:32 -03:00
|
|
|
goto finally;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
PyErr_Clear();
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1998-11-25 12:18:00 -04:00
|
|
|
UNLESS (state = PyObject_GetAttr(args, __dict___str)) {
|
1997-04-09 14:36:32 -03:00
|
|
|
PyErr_Clear();
|
|
|
|
res = 0;
|
|
|
|
goto finally;
|
|
|
|
}
|
|
|
|
}
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
if (!PyDict_Check(state)) {
|
|
|
|
if (put2(self, args) < 0)
|
|
|
|
goto finally;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (put(self, args) < 0)
|
|
|
|
goto finally;
|
|
|
|
}
|
2001-03-17 00:50:51 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
if (save(self, state, 0) < 0)
|
|
|
|
goto finally;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
if ((*self->write_func)(self, &build, 1) < 0)
|
|
|
|
goto finally;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
res = 0;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
finally:
|
|
|
|
Py_XDECREF(module);
|
|
|
|
Py_XDECREF(class);
|
|
|
|
Py_XDECREF(state);
|
|
|
|
Py_XDECREF(getinitargs_func);
|
|
|
|
Py_XDECREF(getstate_func);
|
|
|
|
Py_XDECREF(class_args);
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
return res;
|
1997-01-06 18:59:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
static int
|
|
|
|
save_global(Picklerobject *self, PyObject *args, PyObject *name) {
|
|
|
|
PyObject *global_name = 0, *module = 0;
|
2001-03-17 00:50:51 -04:00
|
|
|
char *name_str, *module_str;
|
1997-04-09 14:36:32 -03:00
|
|
|
int module_size, name_size, res = -1;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
static char global = GLOBAL;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-12-03 21:13:01 -04:00
|
|
|
if (name) {
|
1997-04-09 14:36:32 -03:00
|
|
|
global_name = name;
|
|
|
|
Py_INCREF(global_name);
|
|
|
|
}
|
1997-12-03 21:13:01 -04:00
|
|
|
else {
|
1998-11-25 12:18:00 -04:00
|
|
|
UNLESS (global_name = PyObject_GetAttr(args, __name___str))
|
1997-04-09 14:36:32 -03:00
|
|
|
goto finally;
|
|
|
|
}
|
|
|
|
|
1998-11-25 12:18:00 -04:00
|
|
|
UNLESS (module = whichmodule(args, global_name))
|
1997-04-09 14:36:32 -03:00
|
|
|
goto finally;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1998-11-25 12:18:00 -04:00
|
|
|
if ((module_size = PyString_Size(module)) < 0 ||
|
|
|
|
(name_size = PyString_Size(global_name)) < 0)
|
|
|
|
goto finally;
|
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
module_str = PyString_AS_STRING((PyStringObject *)module);
|
|
|
|
name_str = PyString_AS_STRING((PyStringObject *)global_name);
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
if ((*self->write_func)(self, &global, 1) < 0)
|
|
|
|
goto finally;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
if ((*self->write_func)(self, module_str, module_size) < 0)
|
|
|
|
goto finally;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
if ((*self->write_func)(self, "\n", 1) < 0)
|
|
|
|
goto finally;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
if ((*self->write_func)(self, name_str, name_size) < 0)
|
|
|
|
goto finally;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
if ((*self->write_func)(self, "\n", 1) < 0)
|
|
|
|
goto finally;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
if (put(self, args) < 0)
|
|
|
|
goto finally;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
res = 0;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
finally:
|
|
|
|
Py_XDECREF(module);
|
|
|
|
Py_XDECREF(global_name);
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
return res;
|
1997-01-06 18:59:08 -04:00
|
|
|
}
|
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
static int
|
|
|
|
save_pers(Picklerobject *self, PyObject *args, PyObject *f) {
|
|
|
|
PyObject *pid = 0;
|
|
|
|
int size, res = -1;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
static char persid = PERSID, binpersid = BINPERSID;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1998-11-25 12:18:00 -04:00
|
|
|
Py_INCREF(args);
|
|
|
|
ARG_TUP(self, args);
|
|
|
|
if (self->arg) {
|
|
|
|
pid = PyObject_CallObject(f, self->arg);
|
|
|
|
FREE_ARG_TUP(self);
|
|
|
|
}
|
|
|
|
if (! pid) return -1;
|
1997-04-09 14:36:32 -03:00
|
|
|
|
|
|
|
if (pid != Py_None) {
|
|
|
|
if (!self->bin) {
|
|
|
|
if (!PyString_Check(pid)) {
|
2001-03-17 00:50:51 -04:00
|
|
|
PyErr_SetString(PicklingError,
|
1997-04-09 14:36:32 -03:00
|
|
|
"persistent id must be string");
|
|
|
|
goto finally;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((*self->write_func)(self, &persid, 1) < 0)
|
|
|
|
goto finally;
|
|
|
|
|
|
|
|
if ((size = PyString_Size(pid)) < 0)
|
|
|
|
goto finally;
|
|
|
|
|
2001-03-17 00:50:51 -04:00
|
|
|
if ((*self->write_func)(self,
|
1997-04-09 14:36:32 -03:00
|
|
|
PyString_AS_STRING((PyStringObject *)pid), size) < 0)
|
|
|
|
goto finally;
|
|
|
|
|
|
|
|
if ((*self->write_func)(self, "\n", 1) < 0)
|
|
|
|
goto finally;
|
2001-03-17 00:50:51 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
res = 1;
|
|
|
|
goto finally;
|
|
|
|
}
|
|
|
|
else if (save(self, pid, 1) >= 0) {
|
|
|
|
if ((*self->write_func)(self, &binpersid, 1) < 0)
|
|
|
|
res = -1;
|
|
|
|
else
|
|
|
|
res = 1;
|
|
|
|
}
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2001-03-17 00:50:51 -04:00
|
|
|
goto finally;
|
1997-04-09 14:36:32 -03:00
|
|
|
}
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
res = 0;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
finally:
|
|
|
|
Py_XDECREF(pid);
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
return res;
|
|
|
|
}
|
1997-01-06 18:59:08 -04:00
|
|
|
|
|
|
|
|
2001-03-17 00:50:51 -04:00
|
|
|
static int
|
1997-04-09 14:36:32 -03:00
|
|
|
save_reduce(Picklerobject *self, PyObject *callable,
|
|
|
|
PyObject *tup, PyObject *state, PyObject *ob) {
|
|
|
|
static char reduce = REDUCE, build = BUILD;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
if (save(self, callable, 0) < 0)
|
|
|
|
return -1;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
if (save(self, tup, 0) < 0)
|
|
|
|
return -1;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
if ((*self->write_func)(self, &reduce, 1) < 0)
|
|
|
|
return -1;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-12-03 21:13:01 -04:00
|
|
|
if (ob != NULL) {
|
1997-04-09 14:36:32 -03:00
|
|
|
if (state && !PyDict_Check(state)) {
|
|
|
|
if (put2(self, ob) < 0)
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (put(self, ob) < 0)
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
2001-03-17 00:50:51 -04:00
|
|
|
|
1997-12-03 21:13:01 -04:00
|
|
|
if (state) {
|
1997-04-09 14:36:32 -03:00
|
|
|
if (save(self, state, 0) < 0)
|
|
|
|
return -1;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
if ((*self->write_func)(self, &build, 1) < 0)
|
|
|
|
return -1;
|
|
|
|
}
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
return 0;
|
|
|
|
}
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
static int
|
|
|
|
save(Picklerobject *self, PyObject *args, int pers_save) {
|
|
|
|
PyTypeObject *type;
|
|
|
|
PyObject *py_ob_id = 0, *__reduce__ = 0, *t = 0, *arg_tup = 0,
|
1997-08-13 00:14:41 -03:00
|
|
|
*callable = 0, *state = 0;
|
1997-04-09 14:36:32 -03:00
|
|
|
int res = -1, tmp, size;
|
|
|
|
|
|
|
|
if (!pers_save && self->pers_func) {
|
|
|
|
if ((tmp = save_pers(self, args, self->pers_func)) != 0) {
|
|
|
|
res = tmp;
|
|
|
|
goto finally;
|
|
|
|
}
|
|
|
|
}
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
if (args == Py_None) {
|
|
|
|
res = save_none(self, args);
|
|
|
|
goto finally;
|
|
|
|
}
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
type = args->ob_type;
|
|
|
|
|
|
|
|
switch (type->tp_name[0]) {
|
|
|
|
case 'i':
|
|
|
|
if (type == &PyInt_Type) {
|
|
|
|
res = save_int(self, args);
|
|
|
|
goto finally;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'l':
|
|
|
|
if (type == &PyLong_Type) {
|
|
|
|
res = save_long(self, args);
|
|
|
|
goto finally;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'f':
|
|
|
|
if (type == &PyFloat_Type) {
|
|
|
|
res = save_float(self, args);
|
|
|
|
goto finally;
|
|
|
|
}
|
1998-11-25 12:18:00 -04:00
|
|
|
break;
|
1997-04-09 14:36:32 -03:00
|
|
|
|
|
|
|
case 't':
|
|
|
|
if (type == &PyTuple_Type && PyTuple_Size(args)==0) {
|
1998-11-25 12:18:00 -04:00
|
|
|
if (self->bin) res = save_empty_tuple(self, args);
|
|
|
|
else res = save_tuple(self, args);
|
1997-04-09 14:36:32 -03:00
|
|
|
goto finally;
|
|
|
|
}
|
1998-11-25 12:18:00 -04:00
|
|
|
break;
|
1997-04-09 14:36:32 -03:00
|
|
|
|
|
|
|
case 's':
|
1998-11-25 12:18:00 -04:00
|
|
|
if ((type == &PyString_Type) && (PyString_GET_SIZE(args) < 2)) {
|
1997-08-13 00:14:41 -03:00
|
|
|
res = save_string(self, args, 0);
|
1997-04-09 14:36:32 -03:00
|
|
|
goto finally;
|
|
|
|
}
|
2000-03-10 19:11:40 -04:00
|
|
|
|
|
|
|
case 'u':
|
|
|
|
if ((type == &PyUnicode_Type) && (PyString_GET_SIZE(args) < 2)) {
|
|
|
|
res = save_unicode(self, args, 0);
|
|
|
|
goto finally;
|
|
|
|
}
|
1997-01-06 18:59:08 -04:00
|
|
|
}
|
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
if (args->ob_refcnt > 1) {
|
|
|
|
int has_key;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2000-06-28 19:23:56 -03:00
|
|
|
UNLESS (py_ob_id = PyLong_FromVoidPtr(args))
|
1997-04-09 14:36:32 -03:00
|
|
|
goto finally;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
if ((has_key = cPickle_PyMapping_HasKey(self->memo, py_ob_id)) < 0)
|
|
|
|
goto finally;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
if (has_key) {
|
|
|
|
if (get(self, py_ob_id) < 0)
|
|
|
|
goto finally;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
res = 0;
|
|
|
|
goto finally;
|
|
|
|
}
|
|
|
|
}
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
switch (type->tp_name[0]) {
|
|
|
|
case 's':
|
|
|
|
if (type == &PyString_Type) {
|
1997-08-13 00:14:41 -03:00
|
|
|
res = save_string(self, args, 1);
|
1997-04-09 14:36:32 -03:00
|
|
|
goto finally;
|
|
|
|
}
|
1998-11-25 12:18:00 -04:00
|
|
|
break;
|
1997-04-09 14:36:32 -03:00
|
|
|
|
2000-03-10 19:11:40 -04:00
|
|
|
case 'u':
|
|
|
|
if (type == &PyUnicode_Type) {
|
|
|
|
res = save_unicode(self, args, 1);
|
|
|
|
goto finally;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
case 't':
|
|
|
|
if (type == &PyTuple_Type) {
|
|
|
|
res = save_tuple(self, args);
|
|
|
|
goto finally;
|
1998-11-25 12:18:00 -04:00
|
|
|
}
|
2001-08-02 01:15:00 -03:00
|
|
|
if (type == &PyType_Type) {
|
|
|
|
res = save_global(self, args, NULL);
|
|
|
|
goto finally;
|
|
|
|
}
|
1998-11-25 12:18:00 -04:00
|
|
|
break;
|
1997-04-09 14:36:32 -03:00
|
|
|
|
|
|
|
case 'l':
|
|
|
|
if (type == &PyList_Type) {
|
|
|
|
res = save_list(self, args);
|
|
|
|
goto finally;
|
|
|
|
}
|
1998-11-25 12:18:00 -04:00
|
|
|
break;
|
1997-04-09 14:36:32 -03:00
|
|
|
|
|
|
|
case 'd':
|
|
|
|
if (type == &PyDict_Type) {
|
|
|
|
res = save_dict(self, args);
|
2001-03-17 00:50:51 -04:00
|
|
|
goto finally;
|
1997-04-09 14:36:32 -03:00
|
|
|
}
|
1998-11-25 12:18:00 -04:00
|
|
|
break;
|
1997-04-09 14:36:32 -03:00
|
|
|
|
|
|
|
case 'i':
|
|
|
|
if (type == &PyInstance_Type) {
|
|
|
|
res = save_inst(self, args);
|
|
|
|
goto finally;
|
|
|
|
}
|
1998-11-25 12:18:00 -04:00
|
|
|
break;
|
1997-04-09 14:36:32 -03:00
|
|
|
|
|
|
|
case 'c':
|
|
|
|
if (type == &PyClass_Type) {
|
|
|
|
res = save_global(self, args, NULL);
|
|
|
|
goto finally;
|
|
|
|
}
|
1998-11-25 12:18:00 -04:00
|
|
|
break;
|
1997-04-09 14:36:32 -03:00
|
|
|
|
|
|
|
case 'f':
|
|
|
|
if (type == &PyFunction_Type) {
|
|
|
|
res = save_global(self, args, NULL);
|
|
|
|
goto finally;
|
|
|
|
}
|
1998-11-25 12:18:00 -04:00
|
|
|
break;
|
1997-04-09 14:36:32 -03:00
|
|
|
|
|
|
|
case 'b':
|
|
|
|
if (type == &PyCFunction_Type) {
|
|
|
|
res = save_global(self, args, NULL);
|
|
|
|
goto finally;
|
|
|
|
}
|
1997-01-06 18:59:08 -04:00
|
|
|
}
|
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
if (!pers_save && self->inst_pers_func) {
|
|
|
|
if ((tmp = save_pers(self, args, self->inst_pers_func)) != 0) {
|
|
|
|
res = tmp;
|
|
|
|
goto finally;
|
|
|
|
}
|
|
|
|
}
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-08-13 00:14:41 -03:00
|
|
|
if ((__reduce__ = PyDict_GetItem(dispatch_table, (PyObject *)type))) {
|
1997-04-09 14:36:32 -03:00
|
|
|
Py_INCREF(__reduce__);
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
Py_INCREF(args);
|
1998-11-25 12:18:00 -04:00
|
|
|
ARG_TUP(self, args);
|
|
|
|
if (self->arg) {
|
|
|
|
t = PyObject_CallObject(__reduce__, self->arg);
|
|
|
|
FREE_ARG_TUP(self);
|
|
|
|
}
|
|
|
|
if (! t) goto finally;
|
2001-03-17 00:50:51 -04:00
|
|
|
}
|
1997-04-09 14:36:32 -03:00
|
|
|
else {
|
|
|
|
PyErr_Clear();
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-08-13 00:14:41 -03:00
|
|
|
if ((__reduce__ = PyObject_GetAttr(args, __reduce___str))) {
|
1998-11-25 12:18:00 -04:00
|
|
|
UNLESS (t = PyObject_CallObject(__reduce__, empty_tuple))
|
1997-04-09 14:36:32 -03:00
|
|
|
goto finally;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
PyErr_Clear();
|
|
|
|
}
|
|
|
|
}
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
if (t) {
|
|
|
|
if (PyString_Check(t)) {
|
|
|
|
res = save_global(self, args, t);
|
|
|
|
goto finally;
|
|
|
|
}
|
2001-03-17 00:50:51 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
if (!PyTuple_Check(t)) {
|
1998-01-19 19:18:18 -04:00
|
|
|
cPickle_ErrFormat(PicklingError, "Value returned by %s must "
|
1997-04-09 14:36:32 -03:00
|
|
|
"be a tuple", "O", __reduce__);
|
|
|
|
goto finally;
|
|
|
|
}
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
size = PyTuple_Size(t);
|
2001-03-17 00:50:51 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
if ((size != 3) && (size != 2)) {
|
2001-03-17 00:50:51 -04:00
|
|
|
cPickle_ErrFormat(PicklingError, "tuple returned by %s must "
|
1997-04-09 14:36:32 -03:00
|
|
|
"contain only two or three elements", "O", __reduce__);
|
|
|
|
goto finally;
|
|
|
|
}
|
2001-03-17 00:50:51 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
callable = PyTuple_GET_ITEM(t, 0);
|
1997-08-13 00:14:41 -03:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
arg_tup = PyTuple_GET_ITEM(t, 1);
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
if (size > 2) {
|
|
|
|
state = PyTuple_GET_ITEM(t, 2);
|
|
|
|
}
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1998-11-25 12:18:00 -04:00
|
|
|
UNLESS (PyTuple_Check(arg_tup) || arg_tup==Py_None) {
|
1998-01-19 19:18:18 -04:00
|
|
|
cPickle_ErrFormat(PicklingError, "Second element of tuple "
|
1997-04-09 14:36:32 -03:00
|
|
|
"returned by %s must be a tuple", "O", __reduce__);
|
|
|
|
goto finally;
|
|
|
|
}
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
res = save_reduce(self, callable, arg_tup, state, args);
|
|
|
|
goto finally;
|
|
|
|
}
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1999-06-09 12:23:31 -03:00
|
|
|
PyErr_SetObject(UnpickleableError, args);
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
finally:
|
|
|
|
Py_XDECREF(py_ob_id);
|
|
|
|
Py_XDECREF(__reduce__);
|
|
|
|
Py_XDECREF(t);
|
2001-03-17 00:50:51 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
return res;
|
|
|
|
}
|
1997-01-06 18:59:08 -04:00
|
|
|
|
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
static int
|
|
|
|
dump(Picklerobject *self, PyObject *args) {
|
|
|
|
static char stop = STOP;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
if (save(self, args, 0) < 0)
|
|
|
|
return -1;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
if ((*self->write_func)(self, &stop, 1) < 0)
|
|
|
|
return -1;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
if ((*self->write_func)(self, NULL, 0) < 0)
|
|
|
|
return -1;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
return 0;
|
|
|
|
}
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
static PyObject *
|
1998-11-25 12:18:00 -04:00
|
|
|
Pickle_clear_memo(Picklerobject *self, PyObject *args) {
|
2000-02-29 09:59:29 -04:00
|
|
|
if (args && ! PyArg_ParseTuple(args,":clear_memo")) return NULL;
|
1998-11-25 12:18:00 -04:00
|
|
|
if (self->memo) PyDict_Clear(self->memo);
|
|
|
|
Py_INCREF(Py_None);
|
|
|
|
return Py_None;
|
|
|
|
}
|
1997-04-09 14:36:32 -03:00
|
|
|
|
1998-11-25 12:18:00 -04:00
|
|
|
static PyObject *
|
|
|
|
Pickle_getvalue(Picklerobject *self, PyObject *args) {
|
|
|
|
int l, i, rsize, ssize, clear=1, lm;
|
1998-12-08 13:37:19 -04:00
|
|
|
long ik;
|
1998-11-25 12:18:00 -04:00
|
|
|
PyObject *k, *r;
|
|
|
|
char *s, *p, *have_get;
|
|
|
|
Pdata *data;
|
|
|
|
|
2000-02-29 09:59:29 -04:00
|
|
|
if (args && ! PyArg_ParseTuple(args,"|i:getvalue",&clear)) return NULL;
|
1998-11-25 12:18:00 -04:00
|
|
|
|
|
|
|
/* 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;
|
|
|
|
}
|
1997-04-09 14:36:32 -03:00
|
|
|
|
1998-11-25 12:18:00 -04:00
|
|
|
/* flush write buffer */
|
|
|
|
if (write_other(self, NULL, 0) < 0) return NULL;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1998-11-25 12:18:00 -04:00
|
|
|
data=(Pdata*)self->file;
|
|
|
|
l=data->length;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1998-11-25 12:18:00 -04:00
|
|
|
/* set up an array to hold get/put status */
|
|
|
|
if ((lm=PyDict_Size(self->memo)) < 0) return NULL;
|
|
|
|
lm++;
|
2001-03-17 00:50:51 -04:00
|
|
|
if (! (have_get=malloc((lm)*sizeof(char)))) return PyErr_NoMemory();
|
1998-11-25 12:18:00 -04:00
|
|
|
memset(have_get,0,lm);
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1998-11-25 12:18:00 -04:00
|
|
|
/* Scan for gets. */
|
|
|
|
for (rsize=0, i=l; --i >= 0; ) {
|
|
|
|
k=data->data[i];
|
2001-03-17 00:50:51 -04:00
|
|
|
|
1998-11-25 12:18:00 -04:00
|
|
|
if (PyString_Check(k)) {
|
|
|
|
rsize += PyString_GET_SIZE(k);
|
|
|
|
}
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1998-11-25 12:18:00 -04:00
|
|
|
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;
|
2001-03-17 00:50:51 -04:00
|
|
|
}
|
1998-11-25 12:18:00 -04:00
|
|
|
if (have_get[ik]) { /* with matching get */
|
|
|
|
if (ik < 256) rsize += 2;
|
|
|
|
else rsize+=5;
|
|
|
|
}
|
|
|
|
}
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1998-11-25 12:18:00 -04:00
|
|
|
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");
|
1997-04-09 14:36:32 -03:00
|
|
|
return NULL;
|
1998-11-25 12:18:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
else { /* put */
|
|
|
|
ik=PyInt_AS_LONG((PyIntObject*)k);
|
|
|
|
if (ik >= lm || ik==0) {
|
|
|
|
PyErr_SetString(PicklingError,
|
|
|
|
"Invalid get data");
|
|
|
|
return NULL;
|
2001-03-17 00:50:51 -04:00
|
|
|
}
|
1998-11-25 12:18:00 -04:00
|
|
|
have_get[ik]=1;
|
|
|
|
if (ik < 256) rsize += 2;
|
|
|
|
else rsize+=5;
|
|
|
|
}
|
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
}
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1998-11-25 12:18:00 -04:00
|
|
|
/* Now generate the result */
|
|
|
|
UNLESS (r=PyString_FromStringAndSize(NULL,rsize)) goto err;
|
|
|
|
s=PyString_AS_STRING((PyStringObject*)r);
|
1997-04-09 14:36:32 -03:00
|
|
|
|
1998-11-25 12:18:00 -04:00
|
|
|
for (i=0; i<l; i++) {
|
|
|
|
k=data->data[i];
|
1997-04-09 14:36:32 -03:00
|
|
|
|
1998-11-25 12:18:00 -04:00
|
|
|
if (PyString_Check(k)) {
|
|
|
|
ssize=PyString_GET_SIZE(k);
|
|
|
|
if (ssize) {
|
|
|
|
p=PyString_AS_STRING((PyStringObject*)k);
|
|
|
|
while (--ssize >= 0) *s++=*p++;
|
|
|
|
}
|
|
|
|
}
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1998-11-25 12:18:00 -04:00
|
|
|
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);
|
|
|
|
}
|
2001-03-17 00:50:51 -04:00
|
|
|
|
1998-11-25 12:18:00 -04:00
|
|
|
free(have_get);
|
|
|
|
return r;
|
|
|
|
err:
|
|
|
|
free(have_get);
|
|
|
|
return NULL;
|
1997-01-06 18:59:08 -04:00
|
|
|
}
|
|
|
|
|
1997-08-13 00:14:41 -03:00
|
|
|
static PyObject *
|
1998-11-25 12:18:00 -04:00
|
|
|
Pickler_dump(Picklerobject *self, PyObject *args) {
|
|
|
|
PyObject *ob;
|
|
|
|
int get=0;
|
|
|
|
|
2000-02-29 09:59:29 -04:00
|
|
|
UNLESS (PyArg_ParseTuple(args, "O|i:dump", &ob, &get))
|
1998-11-25 12:18:00 -04:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (dump(self, ob) < 0)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (get) return Pickle_getvalue(self, NULL);
|
|
|
|
|
|
|
|
Py_INCREF(self);
|
|
|
|
return (PyObject*)self;
|
1997-08-13 00:14:41 -03:00
|
|
|
}
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1998-11-25 12:18:00 -04:00
|
|
|
|
1997-01-06 18:59:08 -04:00
|
|
|
static struct PyMethodDef Pickler_methods[] = {
|
1997-08-13 00:14:41 -03:00
|
|
|
{"dump", (PyCFunction)Pickler_dump, 1,
|
1997-12-03 21:13:01 -04:00
|
|
|
"dump(object) --"
|
|
|
|
"Write an object in pickle format to the object's pickle stream\n"
|
|
|
|
},
|
1997-08-13 00:14:41 -03:00
|
|
|
{"clear_memo", (PyCFunction)Pickle_clear_memo, 1,
|
1997-12-03 21:13:01 -04:00
|
|
|
"clear_memo() -- Clear the picklers memo"},
|
1998-11-25 12:18:00 -04:00
|
|
|
{"getvalue", (PyCFunction)Pickle_getvalue, 1,
|
|
|
|
"getvalue() -- Finish picking a list-based pickle"},
|
1997-04-09 14:36:32 -03:00
|
|
|
{NULL, NULL} /* sentinel */
|
1997-01-06 18:59:08 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
static Picklerobject *
|
1997-04-09 14:36:32 -03:00
|
|
|
newPicklerobject(PyObject *file, int bin) {
|
|
|
|
Picklerobject *self;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2000-05-03 20:44:39 -03:00
|
|
|
UNLESS (self = PyObject_New(Picklerobject, &Picklertype))
|
1997-04-09 14:36:32 -03:00
|
|
|
return NULL;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
|
|
|
self->fp = NULL;
|
|
|
|
self->write = NULL;
|
1997-04-09 14:36:32 -03:00
|
|
|
self->memo = NULL;
|
|
|
|
self->arg = NULL;
|
|
|
|
self->pers_func = NULL;
|
|
|
|
self->inst_pers_func = NULL;
|
|
|
|
self->write_buf = NULL;
|
|
|
|
self->bin = bin;
|
1998-08-13 20:13:52 -03:00
|
|
|
self->fast = 0;
|
1997-04-09 14:36:32 -03:00
|
|
|
self->buf_size = 0;
|
1997-12-03 21:13:01 -04:00
|
|
|
self->dispatch_table = NULL;
|
1997-04-09 14:36:32 -03:00
|
|
|
|
1998-11-25 12:18:00 -04:00
|
|
|
if (file)
|
|
|
|
Py_INCREF(file);
|
|
|
|
else
|
1998-12-04 14:48:44 -04:00
|
|
|
file=Pdata_New();
|
2001-03-17 00:50:51 -04:00
|
|
|
|
|
|
|
UNLESS (self->file = file)
|
2000-04-21 17:49:36 -03:00
|
|
|
goto err;
|
1998-11-25 12:18:00 -04:00
|
|
|
|
2001-03-17 00:50:51 -04:00
|
|
|
UNLESS (self->memo = PyDict_New())
|
2000-04-21 17:49:36 -03:00
|
|
|
goto err;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
if (PyFile_Check(file)) {
|
|
|
|
self->fp = PyFile_AsFile(file);
|
1999-03-29 16:00:14 -04:00
|
|
|
if (self->fp == NULL) {
|
2000-04-21 17:49:36 -03:00
|
|
|
PyErr_SetString(PyExc_ValueError, "I/O operation on closed file");
|
|
|
|
goto err;
|
1999-03-29 16:00:14 -04:00
|
|
|
}
|
1997-04-09 14:36:32 -03:00
|
|
|
self->write_func = write_file;
|
|
|
|
}
|
|
|
|
else if (PycStringIO_OutputCheck(file)) {
|
|
|
|
self->write_func = write_cStringIO;
|
|
|
|
}
|
1997-08-13 00:14:41 -03:00
|
|
|
else if (file == Py_None) {
|
|
|
|
self->write_func = write_none;
|
|
|
|
}
|
1997-04-09 14:36:32 -03:00
|
|
|
else {
|
|
|
|
self->write_func = write_other;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1998-11-25 12:18:00 -04:00
|
|
|
if (! Pdata_Check(file)) {
|
|
|
|
UNLESS (self->write = PyObject_GetAttr(file, write_str)) {
|
1997-04-09 14:36:32 -03:00
|
|
|
PyErr_Clear();
|
|
|
|
PyErr_SetString(PyExc_TypeError, "argument must have 'write' "
|
1998-11-25 12:18:00 -04:00
|
|
|
"attribute");
|
|
|
|
goto err;
|
|
|
|
}
|
1997-04-09 14:36:32 -03:00
|
|
|
}
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2001-03-17 00:50:51 -04:00
|
|
|
UNLESS (self->write_buf =
|
|
|
|
(char *)malloc(WRITE_BUF_SIZE * sizeof(char))) {
|
1997-04-09 14:36:32 -03:00
|
|
|
PyErr_NoMemory();
|
1998-11-25 12:18:00 -04:00
|
|
|
goto err;
|
1997-04-09 14:36:32 -03:00
|
|
|
}
|
|
|
|
}
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1998-11-25 12:18:00 -04:00
|
|
|
if (PyEval_GetRestricted()) {
|
|
|
|
/* Restricted execution, get private tables */
|
|
|
|
PyObject *m;
|
1997-12-03 21:13:01 -04:00
|
|
|
|
1998-11-25 12:18:00 -04:00
|
|
|
UNLESS (m=PyImport_Import(copy_reg_str)) goto err;
|
|
|
|
self->dispatch_table=PyObject_GetAttr(m, dispatch_table_str);
|
|
|
|
Py_DECREF(m);
|
|
|
|
UNLESS (self->dispatch_table) goto err;
|
1997-12-03 21:13:01 -04:00
|
|
|
}
|
|
|
|
else {
|
1998-11-25 12:18:00 -04:00
|
|
|
self->dispatch_table=dispatch_table;
|
|
|
|
Py_INCREF(dispatch_table);
|
1997-12-03 21:13:01 -04:00
|
|
|
}
|
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
return self;
|
1997-12-03 21:13:01 -04:00
|
|
|
|
|
|
|
err:
|
|
|
|
Py_DECREF((PyObject *)self);
|
|
|
|
return NULL;
|
1997-01-06 18:59:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static PyObject *
|
1997-04-09 14:36:32 -03:00
|
|
|
get_Pickler(PyObject *self, PyObject *args) {
|
1998-11-25 12:18:00 -04:00
|
|
|
PyObject *file=NULL;
|
|
|
|
int bin;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1998-11-25 12:18:00 -04:00
|
|
|
bin=1;
|
2000-02-29 09:59:29 -04:00
|
|
|
if (! PyArg_ParseTuple(args, "|i:Pickler", &bin)) {
|
1998-11-25 12:18:00 -04:00
|
|
|
PyErr_Clear();
|
|
|
|
bin=0;
|
2000-02-29 09:59:29 -04:00
|
|
|
if (! PyArg_ParseTuple(args, "O|i:Pickler", &file, &bin))
|
1998-11-25 12:18:00 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
1997-04-09 14:36:32 -03:00
|
|
|
return (PyObject *)newPicklerobject(file, bin);
|
1997-01-06 18:59:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
1997-04-09 14:36:32 -03:00
|
|
|
Pickler_dealloc(Picklerobject *self) {
|
|
|
|
Py_XDECREF(self->write);
|
|
|
|
Py_XDECREF(self->memo);
|
|
|
|
Py_XDECREF(self->arg);
|
|
|
|
Py_XDECREF(self->file);
|
|
|
|
Py_XDECREF(self->pers_func);
|
|
|
|
Py_XDECREF(self->inst_pers_func);
|
1997-12-03 21:13:01 -04:00
|
|
|
Py_XDECREF(self->dispatch_table);
|
1997-04-09 14:36:32 -03:00
|
|
|
|
2001-03-17 00:50:51 -04:00
|
|
|
if (self->write_buf) {
|
1997-04-09 14:36:32 -03:00
|
|
|
free(self->write_buf);
|
|
|
|
}
|
|
|
|
|
2000-05-03 20:44:39 -03:00
|
|
|
PyObject_Del(self);
|
1997-01-06 18:59:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static PyObject *
|
1997-04-09 14:36:32 -03:00
|
|
|
Pickler_getattr(Picklerobject *self, char *name) {
|
1998-11-25 12:18:00 -04:00
|
|
|
|
|
|
|
switch (*name) {
|
|
|
|
case 'p':
|
1997-04-09 14:36:32 -03:00
|
|
|
if (strcmp(name, "persistent_id") == 0) {
|
|
|
|
if (!self->pers_func) {
|
|
|
|
PyErr_SetString(PyExc_AttributeError, name);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
Py_INCREF(self->pers_func);
|
|
|
|
return self->pers_func;
|
1997-01-06 18:59:08 -04:00
|
|
|
}
|
1998-11-25 12:18:00 -04:00
|
|
|
break;
|
|
|
|
case 'm':
|
1997-04-09 14:36:32 -03:00
|
|
|
if (strcmp(name, "memo") == 0) {
|
|
|
|
if (!self->memo) {
|
|
|
|
PyErr_SetString(PyExc_AttributeError, name);
|
|
|
|
return NULL;
|
|
|
|
}
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
Py_INCREF(self->memo);
|
|
|
|
return self->memo;
|
1997-01-06 18:59:08 -04:00
|
|
|
}
|
1998-11-25 12:18:00 -04:00
|
|
|
break;
|
|
|
|
case 'P':
|
1997-04-09 14:36:32 -03:00
|
|
|
if (strcmp(name, "PicklingError") == 0) {
|
|
|
|
Py_INCREF(PicklingError);
|
|
|
|
return PicklingError;
|
|
|
|
}
|
1998-11-25 12:18:00 -04:00
|
|
|
break;
|
|
|
|
case 'b':
|
|
|
|
if (strcmp(name, "binary")==0)
|
1998-08-13 20:13:52 -03:00
|
|
|
return PyInt_FromLong(self->bin);
|
1998-11-25 12:18:00 -04:00
|
|
|
break;
|
|
|
|
case 'f':
|
|
|
|
if (strcmp(name, "fast")==0)
|
1998-08-13 20:13:52 -03:00
|
|
|
return PyInt_FromLong(self->fast);
|
1998-11-25 12:18:00 -04:00
|
|
|
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);
|
1997-01-06 18:59:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-03-17 00:50:51 -04:00
|
|
|
int
|
1997-04-09 14:36:32 -03:00
|
|
|
Pickler_setattr(Picklerobject *self, char *name, PyObject *value) {
|
1998-08-13 20:13:52 -03:00
|
|
|
|
1998-11-25 12:18:00 -04:00
|
|
|
if (! value) {
|
1998-08-13 20:13:52 -03:00
|
|
|
PyErr_SetString(PyExc_TypeError,
|
1998-11-25 12:18:00 -04:00
|
|
|
"attribute deletion is not supported");
|
|
|
|
return -1;
|
1998-08-13 20:13:52 -03:00
|
|
|
}
|
2001-03-17 00:50:51 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
if (strcmp(name, "persistent_id") == 0) {
|
|
|
|
Py_XDECREF(self->pers_func);
|
|
|
|
self->pers_func = value;
|
|
|
|
Py_INCREF(value);
|
|
|
|
return 0;
|
|
|
|
}
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
if (strcmp(name, "inst_persistent_id") == 0) {
|
|
|
|
Py_XDECREF(self->inst_pers_func);
|
|
|
|
self->inst_pers_func = value;
|
|
|
|
Py_INCREF(value);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
1998-08-13 20:13:52 -03:00
|
|
|
if (strcmp(name, "memo") == 0) {
|
1998-11-25 12:18:00 -04:00
|
|
|
if (! PyDict_Check(value)) {
|
|
|
|
PyErr_SetString(PyExc_TypeError, "memo must be a dictionary");
|
|
|
|
return -1;
|
|
|
|
}
|
1998-08-13 20:13:52 -03:00
|
|
|
Py_XDECREF(self->memo);
|
|
|
|
self->memo = value;
|
|
|
|
Py_INCREF(value);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
1998-11-25 12:18:00 -04:00
|
|
|
if (strcmp(name, "binary")==0) {
|
1998-08-13 20:13:52 -03:00
|
|
|
self->bin=PyObject_IsTrue(value);
|
1998-11-25 12:18:00 -04:00
|
|
|
return 0;
|
1998-08-13 20:13:52 -03:00
|
|
|
}
|
|
|
|
|
1998-11-25 12:18:00 -04:00
|
|
|
if (strcmp(name, "fast")==0) {
|
1998-08-13 20:13:52 -03:00
|
|
|
self->fast=PyObject_IsTrue(value);
|
1998-11-25 12:18:00 -04:00
|
|
|
return 0;
|
1998-08-13 20:13:52 -03:00
|
|
|
}
|
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
PyErr_SetString(PyExc_AttributeError, name);
|
|
|
|
return -1;
|
1997-01-06 18:59:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1997-12-03 21:13:01 -04:00
|
|
|
static char Picklertype__doc__[] =
|
|
|
|
"Objects that know how to pickle objects\n"
|
|
|
|
;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-12-08 11:15:16 -04:00
|
|
|
static PyTypeObject Picklertype = {
|
|
|
|
PyObject_HEAD_INIT(NULL)
|
1997-04-09 14:36:32 -03:00
|
|
|
0, /*ob_size*/
|
|
|
|
"Pickler", /*tp_name*/
|
|
|
|
sizeof(Picklerobject), /*tp_basicsize*/
|
|
|
|
0, /*tp_itemsize*/
|
|
|
|
/* methods */
|
|
|
|
(destructor)Pickler_dealloc, /*tp_dealloc*/
|
|
|
|
(printfunc)0, /*tp_print*/
|
|
|
|
(getattrfunc)Pickler_getattr, /*tp_getattr*/
|
|
|
|
(setattrfunc)Pickler_setattr, /*tp_setattr*/
|
|
|
|
(cmpfunc)0, /*tp_compare*/
|
|
|
|
(reprfunc)0, /*tp_repr*/
|
|
|
|
0, /*tp_as_number*/
|
|
|
|
0, /*tp_as_sequence*/
|
|
|
|
0, /*tp_as_mapping*/
|
|
|
|
(hashfunc)0, /*tp_hash*/
|
|
|
|
(ternaryfunc)0, /*tp_call*/
|
|
|
|
(reprfunc)0, /*tp_str*/
|
|
|
|
|
|
|
|
/* Space for future expansion */
|
|
|
|
0L,0L,0L,0L,
|
|
|
|
Picklertype__doc__ /* Documentation string */
|
1997-12-08 11:15:16 -04:00
|
|
|
};
|
1997-04-09 14:36:32 -03:00
|
|
|
|
|
|
|
static PyObject *
|
1999-04-19 14:58:18 -03:00
|
|
|
find_class(PyObject *py_module_name, PyObject *py_global_name, PyObject *fc) {
|
1998-08-08 16:40:10 -03:00
|
|
|
PyObject *global = 0, *module;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1999-04-19 14:58:18 -03:00
|
|
|
if (fc) {
|
|
|
|
if (fc==Py_None) {
|
2001-03-17 00:50:51 -04:00
|
|
|
PyErr_SetString(UnpicklingError,
|
1999-04-19 14:58:18 -03:00
|
|
|
"Global and instance pickles are not supported.");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return PyObject_CallFunction(fc, "OO", py_module_name, py_global_name);
|
|
|
|
}
|
|
|
|
|
1998-08-11 16:52:51 -03:00
|
|
|
module = PySys_GetObject("modules");
|
|
|
|
if (module == NULL)
|
1998-11-25 12:18:00 -04:00
|
|
|
return NULL;
|
1997-04-09 14:36:32 -03:00
|
|
|
|
1998-08-11 16:52:51 -03:00
|
|
|
module = PyDict_GetItem(module, py_module_name);
|
|
|
|
if (module == NULL) {
|
1998-11-25 12:18:00 -04:00
|
|
|
module = PyImport_Import(py_module_name);
|
|
|
|
if (!module)
|
|
|
|
return NULL;
|
|
|
|
global = PyObject_GetAttr(module, py_global_name);
|
|
|
|
Py_DECREF(module);
|
1998-08-11 16:52:51 -03:00
|
|
|
}
|
|
|
|
else
|
1998-11-25 12:18:00 -04:00
|
|
|
global = PyObject_GetAttr(module, py_global_name);
|
1998-08-11 16:52:51 -03:00
|
|
|
if (global == NULL) {
|
1998-11-25 12:18:00 -04:00
|
|
|
char buf[256 + 37];
|
|
|
|
sprintf(buf, "Failed to import class %.128s from module %.128s",
|
|
|
|
PyString_AS_STRING((PyStringObject*)py_global_name),
|
2001-03-17 00:50:51 -04:00
|
|
|
PyString_AS_STRING((PyStringObject*)py_module_name));
|
1998-11-25 12:18:00 -04:00
|
|
|
PyErr_SetString(PyExc_SystemError, buf);
|
|
|
|
return NULL;
|
1998-08-11 16:52:51 -03:00
|
|
|
}
|
1997-12-03 21:13:01 -04:00
|
|
|
return global;
|
1997-01-06 18:59:08 -04:00
|
|
|
}
|
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
static int
|
|
|
|
marker(Unpicklerobject *self) {
|
1997-12-03 21:13:01 -04:00
|
|
|
if (self->num_marks < 1) {
|
1997-04-09 14:36:32 -03:00
|
|
|
PyErr_SetString(UnpicklingError, "could not find MARK");
|
|
|
|
return -1;
|
|
|
|
}
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
return self->marks[--self->num_marks];
|
1997-01-06 18:59:08 -04:00
|
|
|
}
|
|
|
|
|
2001-03-17 00:50:51 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
static int
|
|
|
|
load_none(Unpicklerobject *self) {
|
1998-11-25 12:18:00 -04:00
|
|
|
PDATA_APPEND(self->stack, Py_None, -1);
|
1997-04-09 14:36:32 -03:00
|
|
|
return 0;
|
1997-01-06 18:59:08 -04:00
|
|
|
}
|
|
|
|
|
1999-02-04 10:54:04 -04:00
|
|
|
static int
|
2000-07-24 11:43:35 -03:00
|
|
|
bad_readline(void) {
|
1999-02-04 10:54:04 -04:00
|
|
|
PyErr_SetString(UnpicklingError, "pickle data was truncated");
|
|
|
|
return -1;
|
|
|
|
}
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
static int
|
|
|
|
load_int(Unpicklerobject *self) {
|
|
|
|
PyObject *py_int = 0;
|
|
|
|
char *endptr, *s;
|
|
|
|
int len, res = -1;
|
|
|
|
long l;
|
|
|
|
|
|
|
|
if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
|
1999-02-04 10:54:04 -04:00
|
|
|
if (len < 2) return bad_readline();
|
1998-11-25 12:18:00 -04:00
|
|
|
UNLESS (s=pystrndup(s,len)) return -1;
|
1997-04-09 14:36:32 -03:00
|
|
|
|
|
|
|
errno = 0;
|
|
|
|
l = strtol(s, &endptr, 0);
|
|
|
|
|
|
|
|
if (errno || (*endptr != '\n') || (endptr[1] != '\0')) {
|
|
|
|
/* Hm, maybe we've got something long. Let's try reading
|
1998-11-25 12:18:00 -04:00
|
|
|
it as a Python long object. */
|
1997-04-09 14:36:32 -03:00
|
|
|
errno=0;
|
1998-11-25 12:18:00 -04:00
|
|
|
UNLESS (py_int=PyLong_FromString(s,&endptr,0)) goto finally;
|
1997-04-09 14:36:32 -03:00
|
|
|
|
1998-11-25 12:18:00 -04:00
|
|
|
if ((*endptr != '\n') || (endptr[1] != '\0')) {
|
|
|
|
PyErr_SetString(PyExc_ValueError,
|
|
|
|
"could not convert string to int");
|
|
|
|
goto finally;
|
|
|
|
}
|
1997-04-09 14:36:32 -03:00
|
|
|
}
|
|
|
|
else {
|
1998-11-25 12:18:00 -04:00
|
|
|
UNLESS (py_int = PyInt_FromLong(l)) goto finally;
|
1997-04-09 14:36:32 -03:00
|
|
|
}
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1998-11-25 12:18:00 -04:00
|
|
|
free(s);
|
|
|
|
PDATA_PUSH(self->stack, py_int, -1);
|
|
|
|
return 0;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
finally:
|
|
|
|
free(s);
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
return res;
|
|
|
|
}
|
1997-01-06 18:59:08 -04:00
|
|
|
|
|
|
|
|
2001-03-17 00:50:51 -04:00
|
|
|
static long
|
1997-04-09 14:36:32 -03:00
|
|
|
calc_binint(char *s, int x) {
|
|
|
|
unsigned char c;
|
|
|
|
int i;
|
|
|
|
long l;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
for (i = 0, l = 0L; i < x; i++) {
|
|
|
|
c = (unsigned char)s[i];
|
|
|
|
l |= (long)c << (i * 8);
|
|
|
|
}
|
2001-04-09 22:54:42 -03:00
|
|
|
#if SIZEOF_LONG > 4
|
|
|
|
/* Unlike BININT1 and BININT2, BININT (more accurately BININT4)
|
|
|
|
* is signed, so on a box with longs bigger than 4 bytes we need
|
|
|
|
* to extend a BININT's sign bit to the full width.
|
|
|
|
*/
|
|
|
|
if (x == 4 && l & (1L << 31))
|
|
|
|
l |= (~0L) << 32;
|
|
|
|
#endif
|
1997-04-09 14:36:32 -03:00
|
|
|
return l;
|
1997-01-06 18:59:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
static int
|
|
|
|
load_binintx(Unpicklerobject *self, char *s, int x) {
|
|
|
|
PyObject *py_int = 0;
|
|
|
|
long l;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
l = calc_binint(s, x);
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1998-11-25 12:18:00 -04:00
|
|
|
UNLESS (py_int = PyInt_FromLong(l))
|
1997-04-09 14:36:32 -03:00
|
|
|
return -1;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1998-11-25 12:18:00 -04:00
|
|
|
PDATA_PUSH(self->stack, py_int, -1);
|
1997-04-09 14:36:32 -03:00
|
|
|
return 0;
|
1997-01-06 18:59:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
static int
|
|
|
|
load_binint(Unpicklerobject *self) {
|
|
|
|
char *s;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
if ((*self->read_func)(self, &s, 4) < 0)
|
|
|
|
return -1;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
return load_binintx(self, s, 4);
|
|
|
|
}
|
1997-01-06 18:59:08 -04:00
|
|
|
|
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
static int
|
|
|
|
load_binint1(Unpicklerobject *self) {
|
|
|
|
char *s;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
if ((*self->read_func)(self, &s, 1) < 0)
|
|
|
|
return -1;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
return load_binintx(self, s, 1);
|
|
|
|
}
|
1997-01-06 18:59:08 -04:00
|
|
|
|
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
static int
|
|
|
|
load_binint2(Unpicklerobject *self) {
|
|
|
|
char *s;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
if ((*self->read_func)(self, &s, 2) < 0)
|
|
|
|
return -1;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
return load_binintx(self, s, 2);
|
|
|
|
}
|
2001-03-17 00:50:51 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
static int
|
|
|
|
load_long(Unpicklerobject *self) {
|
|
|
|
PyObject *l = 0;
|
|
|
|
char *end, *s;
|
|
|
|
int len, res = -1;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
|
1999-02-04 10:54:04 -04:00
|
|
|
if (len < 2) return bad_readline();
|
1998-11-25 12:18:00 -04:00
|
|
|
UNLESS (s=pystrndup(s,len)) return -1;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1998-11-25 12:18:00 -04:00
|
|
|
UNLESS (l = PyLong_FromString(s, &end, 0))
|
1997-04-09 14:36:32 -03:00
|
|
|
goto finally;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1998-11-25 12:18:00 -04:00
|
|
|
free(s);
|
|
|
|
PDATA_PUSH(self->stack, l, -1);
|
|
|
|
return 0;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
finally:
|
|
|
|
free(s);
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
return res;
|
1997-01-06 18:59:08 -04:00
|
|
|
}
|
|
|
|
|
2001-03-17 00:50:51 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
static int
|
|
|
|
load_float(Unpicklerobject *self) {
|
|
|
|
PyObject *py_float = 0;
|
|
|
|
char *endptr, *s;
|
|
|
|
int len, res = -1;
|
|
|
|
double d;
|
|
|
|
|
|
|
|
if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
|
1999-02-04 10:54:04 -04:00
|
|
|
if (len < 2) return bad_readline();
|
1998-11-25 12:18:00 -04:00
|
|
|
UNLESS (s=pystrndup(s,len)) return -1;
|
1997-04-09 14:36:32 -03:00
|
|
|
|
|
|
|
errno = 0;
|
|
|
|
d = strtod(s, &endptr);
|
|
|
|
|
|
|
|
if (errno || (endptr[0] != '\n') || (endptr[1] != '\0')) {
|
2001-03-17 00:50:51 -04:00
|
|
|
PyErr_SetString(PyExc_ValueError,
|
1997-04-09 14:36:32 -03:00
|
|
|
"could not convert string to float");
|
|
|
|
goto finally;
|
|
|
|
}
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1998-11-25 12:18:00 -04:00
|
|
|
UNLESS (py_float = PyFloat_FromDouble(d))
|
1997-04-09 14:36:32 -03:00
|
|
|
goto finally;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1998-11-25 12:18:00 -04:00
|
|
|
free(s);
|
|
|
|
PDATA_PUSH(self->stack, py_float, -1);
|
|
|
|
return 0;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
finally:
|
|
|
|
free(s);
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
return res;
|
|
|
|
}
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
static int
|
|
|
|
load_binfloat(Unpicklerobject *self) {
|
|
|
|
PyObject *py_float = 0;
|
1999-06-15 11:36:59 -03:00
|
|
|
int s, e;
|
1997-04-09 14:36:32 -03:00
|
|
|
long fhi, flo;
|
|
|
|
double x;
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
if ((*self->read_func)(self, &p, 8) < 0)
|
|
|
|
return -1;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
/* First byte */
|
|
|
|
s = (*p>>7) & 1;
|
|
|
|
e = (*p & 0x7F) << 4;
|
|
|
|
p++;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
/* Second byte */
|
|
|
|
e |= (*p>>4) & 0xF;
|
|
|
|
fhi = (*p & 0xF) << 24;
|
|
|
|
p++;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
/* Third byte */
|
|
|
|
fhi |= (*p & 0xFF) << 16;
|
|
|
|
p++;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
/* Fourth byte */
|
|
|
|
fhi |= (*p & 0xFF) << 8;
|
|
|
|
p++;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
/* Fifth byte */
|
|
|
|
fhi |= *p & 0xFF;
|
|
|
|
p++;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
/* Sixth byte */
|
|
|
|
flo = (*p & 0xFF) << 16;
|
|
|
|
p++;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
/* Seventh byte */
|
|
|
|
flo |= (*p & 0xFF) << 8;
|
|
|
|
p++;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
/* Eighth byte */
|
|
|
|
flo |= *p & 0xFF;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
x = (double)fhi + (double)flo / 16777216.0; /* 2**24 */
|
|
|
|
x /= 268435456.0; /* 2**28 */
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
/* XXX This sadly ignores Inf/NaN */
|
|
|
|
if (e == 0)
|
|
|
|
e = -1022;
|
|
|
|
else {
|
|
|
|
x += 1.0;
|
|
|
|
e -= 1023;
|
|
|
|
}
|
|
|
|
x = ldexp(x, e);
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
if (s)
|
|
|
|
x = -x;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1998-11-25 12:18:00 -04:00
|
|
|
UNLESS (py_float = PyFloat_FromDouble(x)) return -1;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1998-11-25 12:18:00 -04:00
|
|
|
PDATA_PUSH(self->stack, py_float, -1);
|
|
|
|
return 0;
|
1997-04-09 14:36:32 -03:00
|
|
|
}
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
static int
|
|
|
|
load_string(Unpicklerobject *self) {
|
|
|
|
PyObject *str = 0;
|
1997-12-08 11:15:16 -04:00
|
|
|
int len, res = -1, nslash;
|
|
|
|
char *s, q, *p;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
static PyObject *eval_dict = 0;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
|
1999-02-04 10:54:04 -04:00
|
|
|
if (len < 2) return bad_readline();
|
1998-11-25 12:18:00 -04:00
|
|
|
UNLESS (s=pystrndup(s,len)) return -1;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-12-08 11:15:16 -04:00
|
|
|
/* Check for unquoted quotes (evil strings) */
|
|
|
|
q=*s;
|
1998-11-25 12:18:00 -04:00
|
|
|
if (q != '"' && q != '\'') goto insecure;
|
|
|
|
for (p=s+1, nslash=0; *p; p++) {
|
|
|
|
if (*p==q && nslash%2==0) break;
|
|
|
|
if (*p=='\\') nslash++;
|
|
|
|
else nslash=0;
|
1997-12-08 11:15:16 -04:00
|
|
|
}
|
1998-11-25 12:18:00 -04:00
|
|
|
if (*p==q)
|
1997-12-08 11:15:16 -04:00
|
|
|
{
|
1998-11-25 12:18:00 -04:00
|
|
|
for (p++; *p; p++) if (*p > ' ') goto insecure;
|
1997-12-08 11:15:16 -04:00
|
|
|
}
|
|
|
|
else goto insecure;
|
|
|
|
/********************************************/
|
|
|
|
|
1998-11-25 12:18:00 -04:00
|
|
|
UNLESS (eval_dict)
|
|
|
|
UNLESS (eval_dict = Py_BuildValue("{s{}}", "__builtins__"))
|
1997-04-09 14:36:32 -03:00
|
|
|
goto finally;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1998-11-25 12:18:00 -04:00
|
|
|
UNLESS (str = PyRun_String(s, Py_eval_input, eval_dict, eval_dict))
|
1997-04-09 14:36:32 -03:00
|
|
|
goto finally;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1998-11-25 12:18:00 -04:00
|
|
|
free(s);
|
|
|
|
PDATA_PUSH(self->stack, str, -1);
|
|
|
|
return 0;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
finally:
|
|
|
|
free(s);
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
return res;
|
2001-03-17 00:50:51 -04:00
|
|
|
|
1997-12-08 11:15:16 -04:00
|
|
|
insecure:
|
|
|
|
free(s);
|
|
|
|
PyErr_SetString(PyExc_ValueError,"insecure string pickle");
|
|
|
|
return -1;
|
2001-03-17 00:50:51 -04:00
|
|
|
}
|
1997-01-06 18:59:08 -04:00
|
|
|
|
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
static int
|
|
|
|
load_binstring(Unpicklerobject *self) {
|
|
|
|
PyObject *py_string = 0;
|
|
|
|
long l;
|
|
|
|
char *s;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1998-11-25 12:18:00 -04:00
|
|
|
if ((*self->read_func)(self, &s, 4) < 0) return -1;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
l = calc_binint(s, 4);
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
if ((*self->read_func)(self, &s, l) < 0)
|
1998-11-25 12:18:00 -04:00
|
|
|
return -1;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1998-11-25 12:18:00 -04:00
|
|
|
UNLESS (py_string = PyString_FromStringAndSize(s, l))
|
|
|
|
return -1;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1998-11-25 12:18:00 -04:00
|
|
|
PDATA_PUSH(self->stack, py_string, -1);
|
|
|
|
return 0;
|
1997-01-06 18:59:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
static int
|
|
|
|
load_short_binstring(Unpicklerobject *self) {
|
|
|
|
PyObject *py_string = 0;
|
2001-03-17 00:50:51 -04:00
|
|
|
unsigned char l;
|
1997-04-09 14:36:32 -03:00
|
|
|
char *s;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
if ((*self->read_func)(self, &s, 1) < 0)
|
|
|
|
return -1;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
l = (unsigned char)s[0];
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1998-11-25 12:18:00 -04:00
|
|
|
if ((*self->read_func)(self, &s, l) < 0) return -1;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1998-11-25 12:18:00 -04:00
|
|
|
UNLESS (py_string = PyString_FromStringAndSize(s, l)) return -1;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1998-11-25 12:18:00 -04:00
|
|
|
PDATA_PUSH(self->stack, py_string, -1);
|
|
|
|
return 0;
|
2001-03-17 00:50:51 -04:00
|
|
|
}
|
1997-01-06 18:59:08 -04:00
|
|
|
|
|
|
|
|
2000-03-10 19:11:40 -04:00
|
|
|
static int
|
|
|
|
load_unicode(Unpicklerobject *self) {
|
|
|
|
PyObject *str = 0;
|
|
|
|
int len, res = -1;
|
|
|
|
char *s;
|
|
|
|
|
|
|
|
if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
|
2000-12-18 22:08:38 -04:00
|
|
|
if (len < 1) return bad_readline();
|
2000-03-10 19:11:40 -04:00
|
|
|
|
|
|
|
UNLESS (str = PyUnicode_DecodeRawUnicodeEscape(s, len - 1, NULL))
|
|
|
|
goto finally;
|
|
|
|
|
|
|
|
PDATA_PUSH(self->stack, str, -1);
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
finally:
|
|
|
|
return res;
|
2001-03-17 00:50:51 -04:00
|
|
|
}
|
2000-03-10 19:11:40 -04:00
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
load_binunicode(Unpicklerobject *self) {
|
|
|
|
PyObject *unicode;
|
|
|
|
long l;
|
|
|
|
char *s;
|
|
|
|
|
|
|
|
if ((*self->read_func)(self, &s, 4) < 0) return -1;
|
|
|
|
|
|
|
|
l = calc_binint(s, 4);
|
|
|
|
|
|
|
|
if ((*self->read_func)(self, &s, l) < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
UNLESS (unicode = PyUnicode_DecodeUTF8(s, l, NULL))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
PDATA_PUSH(self->stack, unicode, -1);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
static int
|
|
|
|
load_tuple(Unpicklerobject *self) {
|
1998-11-25 12:18:00 -04:00
|
|
|
PyObject *tup;
|
|
|
|
int i;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1998-11-25 12:18:00 -04:00
|
|
|
if ((i = marker(self)) < 0) return -1;
|
|
|
|
UNLESS (tup=Pdata_popTuple(self->stack, i)) return -1;
|
|
|
|
PDATA_PUSH(self->stack, tup, -1);
|
|
|
|
return 0;
|
1997-04-09 14:36:32 -03:00
|
|
|
}
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
static int
|
|
|
|
load_empty_tuple(Unpicklerobject *self) {
|
1998-11-25 12:18:00 -04:00
|
|
|
PyObject *tup;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1998-11-25 12:18:00 -04:00
|
|
|
UNLESS (tup=PyTuple_New(0)) return -1;
|
|
|
|
PDATA_PUSH(self->stack, tup, -1);
|
|
|
|
return 0;
|
1997-04-09 14:36:32 -03:00
|
|
|
}
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
static int
|
|
|
|
load_empty_list(Unpicklerobject *self) {
|
1998-11-25 12:18:00 -04:00
|
|
|
PyObject *list;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1998-11-25 12:18:00 -04:00
|
|
|
UNLESS (list=PyList_New(0)) return -1;
|
|
|
|
PDATA_PUSH(self->stack, list, -1);
|
|
|
|
return 0;
|
1997-04-09 14:36:32 -03:00
|
|
|
}
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
static int
|
|
|
|
load_empty_dict(Unpicklerobject *self) {
|
1998-11-25 12:18:00 -04:00
|
|
|
PyObject *dict;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1998-11-25 12:18:00 -04:00
|
|
|
UNLESS (dict=PyDict_New()) return -1;
|
|
|
|
PDATA_PUSH(self->stack, dict, -1);
|
|
|
|
return 0;
|
1997-01-06 18:59:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
static int
|
|
|
|
load_list(Unpicklerobject *self) {
|
1998-11-25 12:18:00 -04:00
|
|
|
PyObject *list = 0;
|
|
|
|
int i;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1998-11-25 12:18:00 -04:00
|
|
|
if ((i = marker(self)) < 0) return -1;
|
|
|
|
UNLESS (list=Pdata_popList(self->stack, i)) return -1;
|
|
|
|
PDATA_PUSH(self->stack, list, -1);
|
|
|
|
return 0;
|
1997-04-09 14:36:32 -03:00
|
|
|
}
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
static int
|
|
|
|
load_dict(Unpicklerobject *self) {
|
1998-11-25 12:18:00 -04:00
|
|
|
PyObject *dict, *key, *value;
|
|
|
|
int i, j, k;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1998-11-25 12:18:00 -04:00
|
|
|
if ((i = marker(self)) < 0) return -1;
|
|
|
|
j=self->stack->length;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1998-11-25 12:18:00 -04:00
|
|
|
UNLESS (dict = PyDict_New()) return -1;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1998-11-25 12:18:00 -04:00
|
|
|
for (k = i+1; k < j; k += 2) {
|
|
|
|
key =self->stack->data[k-1];
|
|
|
|
value=self->stack->data[k ];
|
|
|
|
if (PyDict_SetItem(dict, key, value) < 0) {
|
|
|
|
Py_DECREF(dict);
|
|
|
|
return -1;
|
|
|
|
}
|
1997-04-09 14:36:32 -03:00
|
|
|
}
|
1998-11-25 12:18:00 -04:00
|
|
|
Pdata_clear(self->stack, i);
|
|
|
|
PDATA_PUSH(self->stack, dict, -1);
|
|
|
|
return 0;
|
1997-01-06 18:59:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
1997-12-03 21:13:01 -04:00
|
|
|
Instance_New(PyObject *cls, PyObject *args) {
|
1997-04-09 14:36:32 -03:00
|
|
|
int has_key;
|
|
|
|
PyObject *safe=0, *r=0;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-12-03 21:13:01 -04:00
|
|
|
if (PyClass_Check(cls)) {
|
|
|
|
int l;
|
2001-03-17 00:50:51 -04:00
|
|
|
|
2000-07-12 10:05:33 -03:00
|
|
|
if ((l=PyObject_Size(args)) < 0) goto err;
|
1998-11-25 12:18:00 -04:00
|
|
|
UNLESS (l) {
|
|
|
|
PyObject *__getinitargs__;
|
|
|
|
|
|
|
|
UNLESS (__getinitargs__=PyObject_GetAttr(cls, __getinitargs___str)) {
|
|
|
|
/* We have a class with no __getinitargs__, so bypass usual
|
|
|
|
construction */
|
2001-03-22 13:52:17 -04:00
|
|
|
PyObject *inst;
|
1998-11-25 12:18:00 -04:00
|
|
|
|
|
|
|
PyErr_Clear();
|
2001-03-22 13:52:17 -04:00
|
|
|
UNLESS (inst=PyInstance_NewRaw(cls, NULL))
|
1998-11-25 12:18:00 -04:00
|
|
|
goto err;
|
2001-03-22 13:52:17 -04:00
|
|
|
return inst;
|
1998-11-25 12:18:00 -04:00
|
|
|
}
|
|
|
|
Py_DECREF(__getinitargs__);
|
|
|
|
}
|
2001-03-17 00:50:51 -04:00
|
|
|
|
1998-11-25 12:18:00 -04:00
|
|
|
if ((r=PyInstance_New(cls, args, NULL))) return r;
|
1997-12-03 21:13:01 -04:00
|
|
|
else goto err;
|
|
|
|
}
|
2001-03-17 00:50:51 -04:00
|
|
|
|
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
if ((has_key = cPickle_PyMapping_HasKey(safe_constructors, cls)) < 0)
|
1997-01-06 18:59:08 -04:00
|
|
|
goto err;
|
2001-03-17 00:50:51 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
if (!has_key)
|
1998-11-25 12:18:00 -04:00
|
|
|
if (!(safe = PyObject_GetAttr(cls, __safe_for_unpickling___str)) ||
|
1997-04-09 14:36:32 -03:00
|
|
|
!PyObject_IsTrue(safe)) {
|
1998-11-25 12:18:00 -04:00
|
|
|
cPickle_ErrFormat(UnpicklingError,
|
|
|
|
"%s is not safe for unpickling", "O", cls);
|
1997-04-09 14:36:32 -03:00
|
|
|
Py_XDECREF(safe);
|
|
|
|
return NULL;
|
|
|
|
}
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1998-11-25 12:18:00 -04:00
|
|
|
if (args==Py_None) {
|
1997-12-08 11:15:16 -04:00
|
|
|
/* Special case, call cls.__basicnew__() */
|
|
|
|
PyObject *basicnew;
|
2001-03-17 00:50:51 -04:00
|
|
|
|
1998-11-25 12:18:00 -04:00
|
|
|
UNLESS (basicnew=PyObject_GetAttr(cls, __basicnew___str)) return NULL;
|
1997-12-08 11:15:16 -04:00
|
|
|
r=PyObject_CallObject(basicnew, NULL);
|
|
|
|
Py_DECREF(basicnew);
|
1998-11-25 12:18:00 -04:00
|
|
|
if (r) return r;
|
1997-12-08 11:15:16 -04:00
|
|
|
}
|
|
|
|
|
1998-11-25 12:18:00 -04:00
|
|
|
if ((r=PyObject_CallObject(cls, args))) return r;
|
1997-08-13 00:14:41 -03:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
err:
|
1997-01-06 18:59:08 -04:00
|
|
|
{
|
1997-04-09 14:36:32 -03:00
|
|
|
PyObject *tp, *v, *tb;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
PyErr_Fetch(&tp, &v, &tb);
|
1998-11-25 12:18:00 -04:00
|
|
|
if ((r=Py_BuildValue("OOO",v,cls,args))) {
|
|
|
|
Py_XDECREF(v);
|
|
|
|
v=r;
|
1997-04-09 14:36:32 -03:00
|
|
|
}
|
|
|
|
PyErr_Restore(tp,v,tb);
|
1997-01-06 18:59:08 -04:00
|
|
|
}
|
1997-04-09 14:36:32 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
2001-03-17 00:50:51 -04:00
|
|
|
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
static int
|
|
|
|
load_obj(Unpicklerobject *self) {
|
1998-11-25 12:18:00 -04:00
|
|
|
PyObject *class, *tup, *obj=0;
|
|
|
|
int i;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1998-11-25 12:18:00 -04:00
|
|
|
if ((i = marker(self)) < 0) return -1;
|
|
|
|
UNLESS (tup=Pdata_popTuple(self->stack, i+1)) return -1;
|
|
|
|
PDATA_POP(self->stack, class);
|
|
|
|
if (class) {
|
|
|
|
obj = Instance_New(class, tup);
|
|
|
|
Py_DECREF(class);
|
|
|
|
}
|
|
|
|
Py_DECREF(tup);
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1998-11-25 12:18:00 -04:00
|
|
|
if (! obj) return -1;
|
|
|
|
PDATA_PUSH(self->stack, obj, -1);
|
|
|
|
return 0;
|
1997-04-09 14:36:32 -03:00
|
|
|
}
|
1997-01-06 18:59:08 -04:00
|
|
|
|
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
static int
|
|
|
|
load_inst(Unpicklerobject *self) {
|
1999-06-15 11:36:59 -03:00
|
|
|
PyObject *tup, *class=0, *obj=0, *module_name, *class_name;
|
1998-12-08 13:37:19 -04:00
|
|
|
int i, len;
|
1997-04-09 14:36:32 -03:00
|
|
|
char *s;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1998-11-25 12:18:00 -04:00
|
|
|
if ((i = marker(self)) < 0) return -1;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1998-11-25 12:18:00 -04:00
|
|
|
if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
|
1999-02-04 10:54:04 -04:00
|
|
|
if (len < 2) return bad_readline();
|
1998-11-25 12:18:00 -04:00
|
|
|
UNLESS (module_name = PyString_FromStringAndSize(s, len - 1)) return -1;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1998-11-25 12:18:00 -04:00
|
|
|
if ((len = (*self->readline_func)(self, &s)) >= 0) {
|
1999-02-04 10:54:04 -04:00
|
|
|
if (len < 2) return bad_readline();
|
1999-06-15 11:36:59 -03:00
|
|
|
if ((class_name = PyString_FromStringAndSize(s, len - 1))) {
|
1999-04-19 14:58:18 -03:00
|
|
|
class = find_class(module_name, class_name, self->find_class);
|
1998-11-25 12:18:00 -04:00
|
|
|
Py_DECREF(class_name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Py_DECREF(module_name);
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1998-11-25 12:18:00 -04:00
|
|
|
if (! class) return -1;
|
2001-03-17 00:50:51 -04:00
|
|
|
|
1999-06-15 11:36:59 -03:00
|
|
|
if ((tup=Pdata_popTuple(self->stack, i))) {
|
1998-11-25 12:18:00 -04:00
|
|
|
obj = Instance_New(class, tup);
|
|
|
|
Py_DECREF(tup);
|
|
|
|
}
|
|
|
|
Py_DECREF(class);
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1998-11-25 12:18:00 -04:00
|
|
|
if (! obj) return -1;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1998-11-25 12:18:00 -04:00
|
|
|
PDATA_PUSH(self->stack, obj, -1);
|
|
|
|
return 0;
|
1997-04-09 14:36:32 -03:00
|
|
|
}
|
1997-01-06 18:59:08 -04:00
|
|
|
|
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
static int
|
|
|
|
load_global(Unpicklerobject *self) {
|
|
|
|
PyObject *class = 0, *module_name = 0, *class_name = 0;
|
1998-11-25 12:18:00 -04:00
|
|
|
int len;
|
1997-04-09 14:36:32 -03:00
|
|
|
char *s;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1998-11-25 12:18:00 -04:00
|
|
|
if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
|
1999-02-04 10:54:04 -04:00
|
|
|
if (len < 2) return bad_readline();
|
1998-11-25 12:18:00 -04:00
|
|
|
UNLESS (module_name = PyString_FromStringAndSize(s, len - 1)) return -1;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1998-11-25 12:18:00 -04:00
|
|
|
if ((len = (*self->readline_func)(self, &s)) >= 0) {
|
1999-02-04 10:54:04 -04:00
|
|
|
if (len < 2) return bad_readline();
|
1999-06-15 11:36:59 -03:00
|
|
|
if ((class_name = PyString_FromStringAndSize(s, len - 1))) {
|
1999-04-19 14:58:18 -03:00
|
|
|
class = find_class(module_name, class_name, self->find_class);
|
1998-11-25 12:18:00 -04:00
|
|
|
Py_DECREF(class_name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Py_DECREF(module_name);
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1998-11-25 12:18:00 -04:00
|
|
|
if (! class) return -1;
|
|
|
|
PDATA_PUSH(self->stack, class, -1);
|
|
|
|
return 0;
|
1997-01-06 18:59:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
static int
|
|
|
|
load_persid(Unpicklerobject *self) {
|
1998-11-25 12:18:00 -04:00
|
|
|
PyObject *pid = 0;
|
1999-06-15 11:36:59 -03:00
|
|
|
int len;
|
1997-04-09 14:36:32 -03:00
|
|
|
char *s;
|
|
|
|
|
|
|
|
if (self->pers_func) {
|
1998-11-25 12:18:00 -04:00
|
|
|
if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
|
1999-02-04 10:54:04 -04:00
|
|
|
if (len < 2) return bad_readline();
|
2001-03-17 00:50:51 -04:00
|
|
|
|
1998-11-25 12:18:00 -04:00
|
|
|
UNLESS (pid = PyString_FromStringAndSize(s, len - 1)) return -1;
|
|
|
|
|
|
|
|
if (PyList_Check(self->pers_func)) {
|
|
|
|
if (PyList_Append(self->pers_func, pid) < 0) {
|
|
|
|
Py_DECREF(pid);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
ARG_TUP(self, pid);
|
|
|
|
if (self->arg) {
|
|
|
|
pid = PyObject_CallObject(self->pers_func, self->arg);
|
|
|
|
FREE_ARG_TUP(self);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (! pid) return -1;
|
|
|
|
|
|
|
|
PDATA_PUSH(self->stack, pid, -1);
|
|
|
|
return 0;
|
1997-12-03 21:13:01 -04:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
PyErr_SetString(UnpicklingError,
|
1998-11-25 12:18:00 -04:00
|
|
|
"A load persistent id instruction was encountered,\n"
|
|
|
|
"but no persistent_load function was specified.");
|
|
|
|
return -1;
|
1997-04-09 14:36:32 -03:00
|
|
|
}
|
|
|
|
}
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
static int
|
|
|
|
load_binpersid(Unpicklerobject *self) {
|
1998-11-25 12:18:00 -04:00
|
|
|
PyObject *pid = 0;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
if (self->pers_func) {
|
1998-11-25 12:18:00 -04:00
|
|
|
PDATA_POP(self->stack, pid);
|
|
|
|
if (! pid) return -1;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1998-11-25 12:18:00 -04:00
|
|
|
if (PyList_Check(self->pers_func)) {
|
|
|
|
if (PyList_Append(self->pers_func, pid) < 0) {
|
|
|
|
Py_DECREF(pid);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
ARG_TUP(self, pid);
|
|
|
|
if (self->arg) {
|
|
|
|
pid = PyObject_CallObject(self->pers_func, self->arg);
|
|
|
|
FREE_ARG_TUP(self);
|
|
|
|
}
|
|
|
|
if (! pid) return -1;
|
|
|
|
}
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1998-11-25 12:18:00 -04:00
|
|
|
PDATA_PUSH(self->stack, pid, -1);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
PyErr_SetString(UnpicklingError,
|
|
|
|
"A load persistent id instruction was encountered,\n"
|
|
|
|
"but no persistent_load function was specified.");
|
|
|
|
return -1;
|
1997-12-03 21:13:01 -04:00
|
|
|
}
|
1997-04-09 14:36:32 -03:00
|
|
|
}
|
1997-01-06 18:59:08 -04:00
|
|
|
|
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
static int
|
|
|
|
load_pop(Unpicklerobject *self) {
|
|
|
|
int len;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1998-11-25 12:18:00 -04:00
|
|
|
UNLESS ((len=self->stack->length) > 0) return stackUnderflow();
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2001-03-17 00:50:51 -04:00
|
|
|
/* Note that we split the (pickle.py) stack into two stacks,
|
2000-05-09 15:14:50 -03:00
|
|
|
an object stack and a mark stack. We have to be clever and
|
|
|
|
pop the right one. We do this by looking at the top of the
|
|
|
|
mark stack.
|
|
|
|
*/
|
|
|
|
|
2001-03-17 00:50:51 -04:00
|
|
|
if ((self->num_marks > 0) &&
|
1997-04-09 14:36:32 -03:00
|
|
|
(self->marks[self->num_marks - 1] == len))
|
|
|
|
self->num_marks--;
|
2001-03-17 00:50:51 -04:00
|
|
|
else {
|
2000-05-09 15:14:50 -03:00
|
|
|
len--;
|
|
|
|
Py_DECREF(self->stack->data[len]);
|
|
|
|
self->stack->length=len;
|
|
|
|
}
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
return 0;
|
1997-01-06 18:59:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
static int
|
|
|
|
load_pop_mark(Unpicklerobject *self) {
|
1998-12-08 13:37:19 -04:00
|
|
|
int i;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
if ((i = marker(self)) < 0)
|
|
|
|
return -1;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1998-11-25 12:18:00 -04:00
|
|
|
Pdata_clear(self->stack, i);
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
return 0;
|
1997-01-06 18:59:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
static int
|
|
|
|
load_dup(Unpicklerobject *self) {
|
|
|
|
PyObject *last;
|
|
|
|
int len;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1998-11-25 12:18:00 -04:00
|
|
|
if ((len = self->stack->length) <= 0) return stackUnderflow();
|
|
|
|
last=self->stack->data[len-1];
|
|
|
|
Py_INCREF(last);
|
|
|
|
PDATA_PUSH(self->stack, last, -1);
|
1997-04-09 14:36:32 -03:00
|
|
|
return 0;
|
1997-01-06 18:59:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
static int
|
|
|
|
load_get(Unpicklerobject *self) {
|
|
|
|
PyObject *py_str = 0, *value = 0;
|
1999-06-15 11:36:59 -03:00
|
|
|
int len;
|
1997-04-09 14:36:32 -03:00
|
|
|
char *s;
|
1999-07-13 12:18:58 -03:00
|
|
|
int rc;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1998-11-25 12:18:00 -04:00
|
|
|
if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
|
1999-02-04 10:54:04 -04:00
|
|
|
if (len < 2) return bad_readline();
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1998-11-25 12:18:00 -04:00
|
|
|
UNLESS (py_str = PyString_FromStringAndSize(s, len - 1)) return -1;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1998-11-25 12:18:00 -04:00
|
|
|
value = PyDict_GetItem(self->memo, py_str);
|
|
|
|
if (! value) {
|
|
|
|
PyErr_SetObject(BadPickleGet, py_str);
|
1999-07-13 12:18:58 -03:00
|
|
|
rc = -1;
|
|
|
|
} else {
|
|
|
|
PDATA_APPEND(self->stack, value, -1);
|
|
|
|
rc = 0;
|
|
|
|
}
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1999-07-13 12:18:58 -03:00
|
|
|
Py_DECREF(py_str);
|
|
|
|
return rc;
|
1997-01-06 18:59:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
static int
|
|
|
|
load_binget(Unpicklerobject *self) {
|
|
|
|
PyObject *py_key = 0, *value = 0;
|
|
|
|
unsigned char key;
|
|
|
|
char *s;
|
1999-07-13 12:18:58 -03:00
|
|
|
int rc;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1998-11-25 12:18:00 -04:00
|
|
|
if ((*self->read_func)(self, &s, 1) < 0) return -1;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
key = (unsigned char)s[0];
|
1998-11-25 12:18:00 -04:00
|
|
|
UNLESS (py_key = PyInt_FromLong((long)key)) return -1;
|
2001-03-17 00:50:51 -04:00
|
|
|
|
1998-11-25 12:18:00 -04:00
|
|
|
value = PyDict_GetItem(self->memo, py_key);
|
|
|
|
if (! value) {
|
|
|
|
PyErr_SetObject(BadPickleGet, py_key);
|
1999-07-13 12:18:58 -03:00
|
|
|
rc = -1;
|
|
|
|
} else {
|
|
|
|
PDATA_APPEND(self->stack, value, -1);
|
|
|
|
rc = 0;
|
|
|
|
}
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1999-07-13 12:18:58 -03:00
|
|
|
Py_DECREF(py_key);
|
|
|
|
return rc;
|
1997-01-06 18:59:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
static int
|
|
|
|
load_long_binget(Unpicklerobject *self) {
|
|
|
|
PyObject *py_key = 0, *value = 0;
|
2000-07-22 20:56:07 -03:00
|
|
|
unsigned char c;
|
|
|
|
char *s;
|
1997-04-09 14:36:32 -03:00
|
|
|
long key;
|
1999-07-13 12:18:58 -03:00
|
|
|
int rc;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1998-11-25 12:18:00 -04:00
|
|
|
if ((*self->read_func)(self, &s, 4) < 0) return -1;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
c = (unsigned char)s[0];
|
|
|
|
key = (long)c;
|
|
|
|
c = (unsigned char)s[1];
|
|
|
|
key |= (long)c << 8;
|
|
|
|
c = (unsigned char)s[2];
|
|
|
|
key |= (long)c << 16;
|
|
|
|
c = (unsigned char)s[3];
|
|
|
|
key |= (long)c << 24;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1998-11-25 12:18:00 -04:00
|
|
|
UNLESS (py_key = PyInt_FromLong((long)key)) return -1;
|
2001-03-17 00:50:51 -04:00
|
|
|
|
1998-11-25 12:18:00 -04:00
|
|
|
value = PyDict_GetItem(self->memo, py_key);
|
|
|
|
if (! value) {
|
|
|
|
PyErr_SetObject(BadPickleGet, py_key);
|
1999-07-13 12:18:58 -03:00
|
|
|
rc = -1;
|
|
|
|
} else {
|
|
|
|
PDATA_APPEND(self->stack, value, -1);
|
|
|
|
rc = 0;
|
|
|
|
}
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1999-07-13 12:18:58 -03:00
|
|
|
Py_DECREF(py_key);
|
|
|
|
return rc;
|
1997-04-09 14:36:32 -03:00
|
|
|
}
|
1997-01-06 18:59:08 -04:00
|
|
|
|
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
static int
|
|
|
|
load_put(Unpicklerobject *self) {
|
|
|
|
PyObject *py_str = 0, *value = 0;
|
1998-11-25 12:18:00 -04:00
|
|
|
int len, l;
|
1997-04-09 14:36:32 -03:00
|
|
|
char *s;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1998-11-25 12:18:00 -04:00
|
|
|
if ((l = (*self->readline_func)(self, &s)) < 0) return -1;
|
1999-02-08 18:38:25 -04:00
|
|
|
if (l < 2) return bad_readline();
|
1998-11-25 12:18:00 -04:00
|
|
|
UNLESS (len=self->stack->length) return stackUnderflow();
|
|
|
|
UNLESS (py_str = PyString_FromStringAndSize(s, l - 1)) return -1;
|
|
|
|
value=self->stack->data[len-1];
|
|
|
|
l=PyDict_SetItem(self->memo, py_str, value);
|
|
|
|
Py_DECREF(py_str);
|
|
|
|
return l;
|
1997-04-09 14:36:32 -03:00
|
|
|
}
|
1997-01-06 18:59:08 -04:00
|
|
|
|
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
static int
|
|
|
|
load_binput(Unpicklerobject *self) {
|
|
|
|
PyObject *py_key = 0, *value = 0;
|
2000-07-22 20:56:07 -03:00
|
|
|
unsigned char key;
|
|
|
|
char *s;
|
1998-11-25 12:18:00 -04:00
|
|
|
int len;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1998-11-25 12:18:00 -04:00
|
|
|
if ((*self->read_func)(self, &s, 1) < 0) return -1;
|
|
|
|
UNLESS ((len=self->stack->length) > 0) return stackUnderflow();
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
key = (unsigned char)s[0];
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1998-11-25 12:18:00 -04:00
|
|
|
UNLESS (py_key = PyInt_FromLong((long)key)) return -1;
|
|
|
|
value=self->stack->data[len-1];
|
|
|
|
len=PyDict_SetItem(self->memo, py_key, value);
|
|
|
|
Py_DECREF(py_key);
|
|
|
|
return len;
|
1997-04-09 14:36:32 -03:00
|
|
|
}
|
1997-01-06 18:59:08 -04:00
|
|
|
|
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
static int
|
|
|
|
load_long_binput(Unpicklerobject *self) {
|
|
|
|
PyObject *py_key = 0, *value = 0;
|
|
|
|
long key;
|
2000-07-22 20:56:07 -03:00
|
|
|
unsigned char c;
|
|
|
|
char *s;
|
1999-06-15 11:36:59 -03:00
|
|
|
int len;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1998-11-25 12:18:00 -04:00
|
|
|
if ((*self->read_func)(self, &s, 4) < 0) return -1;
|
|
|
|
UNLESS (len=self->stack->length) return stackUnderflow();
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
c = (unsigned char)s[0];
|
|
|
|
key = (long)c;
|
|
|
|
c = (unsigned char)s[1];
|
|
|
|
key |= (long)c << 8;
|
|
|
|
c = (unsigned char)s[2];
|
|
|
|
key |= (long)c << 16;
|
|
|
|
c = (unsigned char)s[3];
|
|
|
|
key |= (long)c << 24;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1998-11-25 12:18:00 -04:00
|
|
|
UNLESS (py_key = PyInt_FromLong(key)) return -1;
|
|
|
|
value=self->stack->data[len-1];
|
|
|
|
len=PyDict_SetItem(self->memo, py_key, value);
|
|
|
|
Py_DECREF(py_key);
|
|
|
|
return len;
|
1997-01-06 18:59:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-03-17 00:50:51 -04:00
|
|
|
static int
|
1997-04-09 14:36:32 -03:00
|
|
|
do_append(Unpicklerobject *self, int x) {
|
|
|
|
PyObject *value = 0, *list = 0, *append_method = 0;
|
|
|
|
int len, i;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1998-11-25 12:18:00 -04:00
|
|
|
UNLESS ((len=self->stack->length) >= x && x > 0) return stackUnderflow();
|
|
|
|
if (len==x) return 0; /* nothing to do */
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1998-11-25 12:18:00 -04:00
|
|
|
list=self->stack->data[x-1];
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
if (PyList_Check(list)) {
|
1998-11-25 12:18:00 -04:00
|
|
|
PyObject *slice;
|
1997-04-09 14:36:32 -03:00
|
|
|
int list_len;
|
2001-03-17 00:50:51 -04:00
|
|
|
|
1998-11-25 12:18:00 -04:00
|
|
|
slice=Pdata_popList(self->stack, x);
|
|
|
|
list_len = PyList_GET_SIZE(list);
|
|
|
|
i=PyList_SetSlice(list, list_len, list_len, slice);
|
1997-04-09 14:36:32 -03:00
|
|
|
Py_DECREF(slice);
|
1998-11-25 12:18:00 -04:00
|
|
|
return i;
|
1997-04-09 14:36:32 -03:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
|
1998-11-25 12:18:00 -04:00
|
|
|
UNLESS (append_method = PyObject_GetAttr(list, append_str))
|
1997-04-09 14:36:32 -03:00
|
|
|
return -1;
|
2001-03-17 00:50:51 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
for (i = x; i < len; i++) {
|
1998-11-25 12:18:00 -04:00
|
|
|
PyObject *junk;
|
|
|
|
|
|
|
|
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);
|
1997-04-09 14:36:32 -03:00
|
|
|
return -1;
|
1998-11-25 12:18:00 -04:00
|
|
|
}
|
|
|
|
Py_DECREF(junk);
|
1997-04-09 14:36:32 -03:00
|
|
|
}
|
1998-11-25 12:18:00 -04:00
|
|
|
self->stack->length=x;
|
|
|
|
Py_DECREF(append_method);
|
1997-04-09 14:36:32 -03:00
|
|
|
}
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
return 0;
|
|
|
|
}
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2001-03-17 00:50:51 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
static int
|
|
|
|
load_append(Unpicklerobject *self) {
|
1998-11-25 12:18:00 -04:00
|
|
|
return do_append(self, self->stack->length - 1);
|
1997-01-06 18:59:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
static int
|
|
|
|
load_appends(Unpicklerobject *self) {
|
|
|
|
return do_append(self, marker(self));
|
|
|
|
}
|
1997-01-06 18:59:08 -04:00
|
|
|
|
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
static int
|
|
|
|
do_setitems(Unpicklerobject *self, int x) {
|
|
|
|
PyObject *value = 0, *key = 0, *dict = 0;
|
1998-11-25 12:18:00 -04:00
|
|
|
int len, i, r=0;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1998-11-25 12:18:00 -04:00
|
|
|
UNLESS ((len=self->stack->length) >= x
|
|
|
|
&& x > 0) return stackUnderflow();
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1998-11-25 12:18:00 -04:00
|
|
|
dict=self->stack->data[x-1];
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1998-11-25 12:18:00 -04:00
|
|
|
for (i = x+1; i < len; i += 2) {
|
|
|
|
key =self->stack->data[i-1];
|
|
|
|
value=self->stack->data[i ];
|
|
|
|
if (PyObject_SetItem(dict, key, value) < 0) {
|
|
|
|
r=-1;
|
|
|
|
break;
|
|
|
|
}
|
1997-01-06 18:59:08 -04:00
|
|
|
}
|
|
|
|
|
1998-11-25 12:18:00 -04:00
|
|
|
Pdata_clear(self->stack, x);
|
1997-04-09 14:36:32 -03:00
|
|
|
|
1998-11-25 12:18:00 -04:00
|
|
|
return r;
|
1997-01-06 18:59:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
static int
|
|
|
|
load_setitem(Unpicklerobject *self) {
|
1998-11-25 12:18:00 -04:00
|
|
|
return do_setitems(self, self->stack->length - 2);
|
1997-04-09 14:36:32 -03:00
|
|
|
}
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
static int
|
|
|
|
load_setitems(Unpicklerobject *self) {
|
|
|
|
return do_setitems(self, marker(self));
|
|
|
|
}
|
1997-01-06 18:59:08 -04:00
|
|
|
|
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
static int
|
|
|
|
load_build(Unpicklerobject *self) {
|
2001-03-17 00:50:51 -04:00
|
|
|
PyObject *value = 0, *inst = 0, *instdict = 0, *d_key = 0, *d_value = 0,
|
1997-04-09 14:36:32 -03:00
|
|
|
*junk = 0, *__setstate__ = 0;
|
1998-12-08 13:37:19 -04:00
|
|
|
int i, r = 0;
|
1998-11-25 12:18:00 -04:00
|
|
|
|
|
|
|
if (self->stack->length < 2) return stackUnderflow();
|
|
|
|
PDATA_POP(self->stack, value);
|
|
|
|
if (! value) return -1;
|
|
|
|
inst=self->stack->data[self->stack->length-1];
|
|
|
|
|
|
|
|
if ((__setstate__ = PyObject_GetAttr(inst, __setstate___str))) {
|
|
|
|
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;
|
|
|
|
}
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1998-11-25 12:18:00 -04:00
|
|
|
PyErr_Clear();
|
|
|
|
if ((instdict = PyObject_GetAttr(inst, __dict___str))) {
|
1997-04-09 14:36:32 -03:00
|
|
|
i = 0;
|
|
|
|
while (PyDict_Next(value, &i, &d_key, &d_value)) {
|
1998-11-25 12:18:00 -04:00
|
|
|
if (PyObject_SetItem(instdict, d_key, d_value) < 0) {
|
|
|
|
r=-1;
|
|
|
|
break;
|
|
|
|
}
|
1997-04-09 14:36:32 -03:00
|
|
|
}
|
1998-11-25 12:18:00 -04:00
|
|
|
Py_DECREF(instdict);
|
1997-04-09 14:36:32 -03:00
|
|
|
}
|
1998-11-25 12:18:00 -04:00
|
|
|
else r=-1;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1998-11-25 12:18:00 -04:00
|
|
|
Py_XDECREF(value);
|
2001-03-17 00:50:51 -04:00
|
|
|
|
1998-11-25 12:18:00 -04:00
|
|
|
return r;
|
1997-04-09 14:36:32 -03:00
|
|
|
}
|
1997-01-06 18:59:08 -04:00
|
|
|
|
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
static int
|
|
|
|
load_mark(Unpicklerobject *self) {
|
1998-11-25 12:18:00 -04:00
|
|
|
int s;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2000-05-09 15:14:50 -03:00
|
|
|
/* Note that we split the (pickle.py) stack into two stacks, an
|
|
|
|
object stack and a mark stack. Here we push a mark onto the
|
2001-03-17 00:50:51 -04:00
|
|
|
mark stack.
|
2000-05-09 15:14:50 -03:00
|
|
|
*/
|
|
|
|
|
1998-11-25 12:18:00 -04:00
|
|
|
if ((self->num_marks + 1) >= self->marks_size) {
|
|
|
|
s=self->marks_size+20;
|
|
|
|
if (s <= self->num_marks) s=self->num_marks + 1;
|
1999-04-12 19:51:20 -03:00
|
|
|
if (self->marks == NULL)
|
1999-01-25 17:43:51 -04:00
|
|
|
self->marks=(int *)malloc(s * sizeof(int));
|
|
|
|
else
|
|
|
|
self->marks=(int *)realloc(self->marks, s * sizeof(int));
|
1998-11-25 12:18:00 -04:00
|
|
|
if (! self->marks) {
|
1997-04-09 14:36:32 -03:00
|
|
|
PyErr_NoMemory();
|
|
|
|
return -1;
|
|
|
|
}
|
1998-11-25 12:18:00 -04:00
|
|
|
self->marks_size = s;
|
1997-04-09 14:36:32 -03:00
|
|
|
}
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1998-11-25 12:18:00 -04:00
|
|
|
self->marks[self->num_marks++] = self->stack->length;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
return 0;
|
|
|
|
}
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
static int
|
|
|
|
load_reduce(Unpicklerobject *self) {
|
|
|
|
PyObject *callable = 0, *arg_tup = 0, *ob = 0;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1998-11-25 12:18:00 -04:00
|
|
|
PDATA_POP(self->stack, arg_tup);
|
|
|
|
if (! arg_tup) return -1;
|
|
|
|
PDATA_POP(self->stack, callable);
|
|
|
|
if (callable) {
|
|
|
|
ob = Instance_New(callable, arg_tup);
|
|
|
|
Py_DECREF(callable);
|
|
|
|
}
|
|
|
|
Py_DECREF(arg_tup);
|
1997-04-09 14:36:32 -03:00
|
|
|
|
1998-11-25 12:18:00 -04:00
|
|
|
if (! ob) return -1;
|
2001-03-17 00:50:51 -04:00
|
|
|
|
1998-11-25 12:18:00 -04:00
|
|
|
PDATA_PUSH(self->stack, ob, -1);
|
|
|
|
return 0;
|
1997-04-09 14:36:32 -03:00
|
|
|
}
|
2001-03-17 00:50:51 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
static PyObject *
|
1997-12-03 21:13:01 -04:00
|
|
|
load(Unpicklerobject *self) {
|
1999-06-15 11:36:59 -03:00
|
|
|
PyObject *err = 0, *val = 0;
|
1997-04-09 14:36:32 -03:00
|
|
|
char *s;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
self->num_marks = 0;
|
1998-11-25 12:18:00 -04:00
|
|
|
if (self->stack->length) Pdata_clear(self->stack, 0);
|
1997-04-09 14:36:32 -03:00
|
|
|
|
|
|
|
while (1) {
|
|
|
|
if ((*self->read_func)(self, &s, 1) < 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
switch (s[0]) {
|
|
|
|
case NONE:
|
|
|
|
if (load_none(self) < 0)
|
|
|
|
break;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case BININT:
|
|
|
|
if (load_binint(self) < 0)
|
|
|
|
break;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case BININT1:
|
|
|
|
if (load_binint1(self) < 0)
|
|
|
|
break;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case BININT2:
|
|
|
|
if (load_binint2(self) < 0)
|
|
|
|
break;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case INT:
|
|
|
|
if (load_int(self) < 0)
|
|
|
|
break;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case LONG:
|
|
|
|
if (load_long(self) < 0)
|
|
|
|
break;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case FLOAT:
|
|
|
|
if (load_float(self) < 0)
|
|
|
|
break;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case BINFLOAT:
|
|
|
|
if (load_binfloat(self) < 0)
|
|
|
|
break;
|
|
|
|
continue;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
case BINSTRING:
|
|
|
|
if (load_binstring(self) < 0)
|
|
|
|
break;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case SHORT_BINSTRING:
|
|
|
|
if (load_short_binstring(self) < 0)
|
|
|
|
break;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case STRING:
|
|
|
|
if (load_string(self) < 0)
|
|
|
|
break;
|
|
|
|
continue;
|
|
|
|
|
2000-03-10 19:11:40 -04:00
|
|
|
case UNICODE:
|
|
|
|
if (load_unicode(self) < 0)
|
|
|
|
break;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case BINUNICODE:
|
|
|
|
if (load_binunicode(self) < 0)
|
|
|
|
break;
|
|
|
|
continue;
|
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
case EMPTY_TUPLE:
|
|
|
|
if (load_empty_tuple(self) < 0)
|
|
|
|
break;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case TUPLE:
|
|
|
|
if (load_tuple(self) < 0)
|
|
|
|
break;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case EMPTY_LIST:
|
|
|
|
if (load_empty_list(self) < 0)
|
|
|
|
break;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case LIST:
|
|
|
|
if (load_list(self) < 0)
|
|
|
|
break;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case EMPTY_DICT:
|
|
|
|
if (load_empty_dict(self) < 0)
|
|
|
|
break;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case DICT:
|
|
|
|
if (load_dict(self) < 0)
|
|
|
|
break;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case OBJ:
|
|
|
|
if (load_obj(self) < 0)
|
|
|
|
break;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case INST:
|
|
|
|
if (load_inst(self) < 0)
|
|
|
|
break;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case GLOBAL:
|
|
|
|
if (load_global(self) < 0)
|
|
|
|
break;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case APPEND:
|
|
|
|
if (load_append(self) < 0)
|
|
|
|
break;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case APPENDS:
|
|
|
|
if (load_appends(self) < 0)
|
|
|
|
break;
|
|
|
|
continue;
|
2001-03-17 00:50:51 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
case BUILD:
|
|
|
|
if (load_build(self) < 0)
|
|
|
|
break;
|
|
|
|
continue;
|
2001-03-17 00:50:51 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
case DUP:
|
|
|
|
if (load_dup(self) < 0)
|
|
|
|
break;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case BINGET:
|
|
|
|
if (load_binget(self) < 0)
|
|
|
|
break;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case LONG_BINGET:
|
|
|
|
if (load_long_binget(self) < 0)
|
|
|
|
break;
|
|
|
|
continue;
|
2001-03-17 00:50:51 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
case GET:
|
|
|
|
if (load_get(self) < 0)
|
|
|
|
break;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case MARK:
|
|
|
|
if (load_mark(self) < 0)
|
|
|
|
break;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case BINPUT:
|
|
|
|
if (load_binput(self) < 0)
|
|
|
|
break;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case LONG_BINPUT:
|
|
|
|
if (load_long_binput(self) < 0)
|
|
|
|
break;
|
|
|
|
continue;
|
2001-03-17 00:50:51 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
case PUT:
|
|
|
|
if (load_put(self) < 0)
|
|
|
|
break;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case POP:
|
|
|
|
if (load_pop(self) < 0)
|
|
|
|
break;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case POP_MARK:
|
|
|
|
if (load_pop_mark(self) < 0)
|
|
|
|
break;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case SETITEM:
|
|
|
|
if (load_setitem(self) < 0)
|
|
|
|
break;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case SETITEMS:
|
|
|
|
if (load_setitems(self) < 0)
|
|
|
|
break;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case STOP:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PERSID:
|
|
|
|
if (load_persid(self) < 0)
|
|
|
|
break;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case BINPERSID:
|
|
|
|
if (load_binpersid(self) < 0)
|
|
|
|
break;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case REDUCE:
|
|
|
|
if (load_reduce(self) < 0)
|
|
|
|
break;
|
|
|
|
continue;
|
|
|
|
|
2001-03-17 00:50:51 -04:00
|
|
|
default:
|
|
|
|
cPickle_ErrFormat(UnpicklingError, "invalid load key, '%s'.",
|
1997-04-09 14:36:32 -03:00
|
|
|
"c", s[0]);
|
1998-11-25 12:18:00 -04:00
|
|
|
return NULL;
|
1997-04-09 14:36:32 -03:00
|
|
|
}
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
break;
|
1997-01-06 18:59:08 -04:00
|
|
|
}
|
|
|
|
|
1998-11-25 12:18:00 -04:00
|
|
|
if ((err = PyErr_Occurred())) {
|
|
|
|
if (err == PyExc_EOFError) {
|
|
|
|
PyErr_SetNone(PyExc_EOFError);
|
2001-03-17 00:50:51 -04:00
|
|
|
}
|
1998-11-25 12:18:00 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2001-03-17 00:50:51 -04:00
|
|
|
PDATA_POP(self->stack, val);
|
1997-04-09 14:36:32 -03:00
|
|
|
return val;
|
1997-01-06 18:59:08 -04:00
|
|
|
}
|
2001-03-17 00:50:51 -04:00
|
|
|
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-12-03 21:13:01 -04:00
|
|
|
/* No-load functions to support noload, which is used to
|
|
|
|
find persistent references. */
|
|
|
|
|
|
|
|
static int
|
|
|
|
noload_obj(Unpicklerobject *self) {
|
1998-12-08 13:37:19 -04:00
|
|
|
int i;
|
1997-12-03 21:13:01 -04:00
|
|
|
|
|
|
|
if ((i = marker(self)) < 0) return -1;
|
1998-11-25 12:18:00 -04:00
|
|
|
return Pdata_clear(self->stack, i+1);
|
1997-12-03 21:13:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
noload_inst(Unpicklerobject *self) {
|
1998-12-08 13:37:19 -04:00
|
|
|
int i;
|
1997-12-03 21:13:01 -04:00
|
|
|
char *s;
|
|
|
|
|
|
|
|
if ((i = marker(self)) < 0) return -1;
|
1998-11-25 12:18:00 -04:00
|
|
|
Pdata_clear(self->stack, i);
|
1997-12-03 21:13:01 -04:00
|
|
|
if ((*self->readline_func)(self, &s) < 0) return -1;
|
|
|
|
if ((*self->readline_func)(self, &s) < 0) return -1;
|
1998-11-25 12:18:00 -04:00
|
|
|
PDATA_APPEND(self->stack, Py_None,-1);
|
|
|
|
return 0;
|
1997-12-03 21:13:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
noload_global(Unpicklerobject *self) {
|
|
|
|
char *s;
|
|
|
|
|
|
|
|
if ((*self->readline_func)(self, &s) < 0) return -1;
|
|
|
|
if ((*self->readline_func)(self, &s) < 0) return -1;
|
1998-11-25 12:18:00 -04:00
|
|
|
PDATA_APPEND(self->stack, Py_None,-1);
|
|
|
|
return 0;
|
1997-12-03 21:13:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
noload_reduce(Unpicklerobject *self) {
|
|
|
|
|
1998-11-25 12:18:00 -04:00
|
|
|
if (self->stack->length < 2) return stackUnderflow();
|
|
|
|
Pdata_clear(self->stack, self->stack->length-2);
|
|
|
|
PDATA_APPEND(self->stack, Py_None,-1);
|
|
|
|
return 0;
|
1997-12-03 21:13:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
noload_build(Unpicklerobject *self) {
|
|
|
|
|
1998-11-25 12:18:00 -04:00
|
|
|
if (self->stack->length < 1) return stackUnderflow();
|
|
|
|
Pdata_clear(self->stack, self->stack->length-1);
|
1998-12-04 14:48:44 -04:00
|
|
|
return 0;
|
1997-12-03 21:13:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
noload(Unpicklerobject *self) {
|
1999-06-15 11:36:59 -03:00
|
|
|
PyObject *err = 0, *val = 0;
|
1997-12-03 21:13:01 -04:00
|
|
|
char *s;
|
|
|
|
|
|
|
|
self->num_marks = 0;
|
1998-11-25 12:18:00 -04:00
|
|
|
Pdata_clear(self->stack, 0);
|
1997-12-03 21:13:01 -04:00
|
|
|
|
|
|
|
while (1) {
|
|
|
|
if ((*self->read_func)(self, &s, 1) < 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
switch (s[0]) {
|
|
|
|
case NONE:
|
|
|
|
if (load_none(self) < 0)
|
|
|
|
break;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case BININT:
|
|
|
|
if (load_binint(self) < 0)
|
|
|
|
break;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case BININT1:
|
|
|
|
if (load_binint1(self) < 0)
|
|
|
|
break;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case BININT2:
|
|
|
|
if (load_binint2(self) < 0)
|
|
|
|
break;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case INT:
|
|
|
|
if (load_int(self) < 0)
|
|
|
|
break;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case LONG:
|
|
|
|
if (load_long(self) < 0)
|
|
|
|
break;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case FLOAT:
|
|
|
|
if (load_float(self) < 0)
|
|
|
|
break;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case BINFLOAT:
|
|
|
|
if (load_binfloat(self) < 0)
|
|
|
|
break;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case BINSTRING:
|
|
|
|
if (load_binstring(self) < 0)
|
|
|
|
break;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case SHORT_BINSTRING:
|
|
|
|
if (load_short_binstring(self) < 0)
|
|
|
|
break;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case STRING:
|
|
|
|
if (load_string(self) < 0)
|
|
|
|
break;
|
|
|
|
continue;
|
|
|
|
|
2000-03-10 19:11:40 -04:00
|
|
|
case UNICODE:
|
|
|
|
if (load_unicode(self) < 0)
|
|
|
|
break;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case BINUNICODE:
|
|
|
|
if (load_binunicode(self) < 0)
|
|
|
|
break;
|
|
|
|
continue;
|
|
|
|
|
1997-12-03 21:13:01 -04:00
|
|
|
case EMPTY_TUPLE:
|
|
|
|
if (load_empty_tuple(self) < 0)
|
|
|
|
break;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case TUPLE:
|
|
|
|
if (load_tuple(self) < 0)
|
|
|
|
break;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case EMPTY_LIST:
|
|
|
|
if (load_empty_list(self) < 0)
|
|
|
|
break;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case LIST:
|
|
|
|
if (load_list(self) < 0)
|
|
|
|
break;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case EMPTY_DICT:
|
|
|
|
if (load_empty_dict(self) < 0)
|
|
|
|
break;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case DICT:
|
|
|
|
if (load_dict(self) < 0)
|
|
|
|
break;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case OBJ:
|
|
|
|
if (noload_obj(self) < 0)
|
|
|
|
break;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case INST:
|
|
|
|
if (noload_inst(self) < 0)
|
|
|
|
break;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case GLOBAL:
|
|
|
|
if (noload_global(self) < 0)
|
|
|
|
break;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case APPEND:
|
|
|
|
if (load_append(self) < 0)
|
|
|
|
break;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case APPENDS:
|
|
|
|
if (load_appends(self) < 0)
|
|
|
|
break;
|
|
|
|
continue;
|
2001-03-17 00:50:51 -04:00
|
|
|
|
1997-12-03 21:13:01 -04:00
|
|
|
case BUILD:
|
|
|
|
if (noload_build(self) < 0)
|
|
|
|
break;
|
|
|
|
continue;
|
2001-03-17 00:50:51 -04:00
|
|
|
|
1997-12-03 21:13:01 -04:00
|
|
|
case DUP:
|
|
|
|
if (load_dup(self) < 0)
|
|
|
|
break;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case BINGET:
|
|
|
|
if (load_binget(self) < 0)
|
|
|
|
break;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case LONG_BINGET:
|
|
|
|
if (load_long_binget(self) < 0)
|
|
|
|
break;
|
|
|
|
continue;
|
2001-03-17 00:50:51 -04:00
|
|
|
|
1997-12-03 21:13:01 -04:00
|
|
|
case GET:
|
|
|
|
if (load_get(self) < 0)
|
|
|
|
break;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case MARK:
|
|
|
|
if (load_mark(self) < 0)
|
|
|
|
break;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case BINPUT:
|
|
|
|
if (load_binput(self) < 0)
|
|
|
|
break;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case LONG_BINPUT:
|
|
|
|
if (load_long_binput(self) < 0)
|
|
|
|
break;
|
|
|
|
continue;
|
2001-03-17 00:50:51 -04:00
|
|
|
|
1997-12-03 21:13:01 -04:00
|
|
|
case PUT:
|
|
|
|
if (load_put(self) < 0)
|
|
|
|
break;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case POP:
|
|
|
|
if (load_pop(self) < 0)
|
|
|
|
break;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case POP_MARK:
|
|
|
|
if (load_pop_mark(self) < 0)
|
|
|
|
break;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case SETITEM:
|
|
|
|
if (load_setitem(self) < 0)
|
|
|
|
break;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case SETITEMS:
|
|
|
|
if (load_setitems(self) < 0)
|
|
|
|
break;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case STOP:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PERSID:
|
|
|
|
if (load_persid(self) < 0)
|
|
|
|
break;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case BINPERSID:
|
|
|
|
if (load_binpersid(self) < 0)
|
|
|
|
break;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case REDUCE:
|
|
|
|
if (noload_reduce(self) < 0)
|
|
|
|
break;
|
|
|
|
continue;
|
|
|
|
|
2001-03-17 00:50:51 -04:00
|
|
|
default:
|
|
|
|
cPickle_ErrFormat(UnpicklingError, "invalid load key, '%s'.",
|
1997-12-03 21:13:01 -04:00
|
|
|
"c", s[0]);
|
1998-11-25 12:18:00 -04:00
|
|
|
return NULL;
|
1997-12-03 21:13:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
1998-11-25 12:18:00 -04:00
|
|
|
if ((err = PyErr_Occurred())) {
|
|
|
|
if (err == PyExc_EOFError) {
|
|
|
|
PyErr_SetNone(PyExc_EOFError);
|
2001-03-17 00:50:51 -04:00
|
|
|
}
|
1998-11-25 12:18:00 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
1997-12-03 21:13:01 -04:00
|
|
|
|
2001-03-17 00:50:51 -04:00
|
|
|
PDATA_POP(self->stack, val);
|
1997-12-03 21:13:01 -04:00
|
|
|
return val;
|
|
|
|
}
|
2001-03-17 00:50:51 -04:00
|
|
|
|
1997-12-03 21:13:01 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
static PyObject *
|
|
|
|
Unpickler_load(Unpicklerobject *self, PyObject *args) {
|
2001-03-17 00:50:51 -04:00
|
|
|
UNLESS (PyArg_ParseTuple(args, ":load"))
|
1997-04-09 14:36:32 -03:00
|
|
|
return NULL;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
return load(self);
|
|
|
|
}
|
|
|
|
|
1997-12-03 21:13:01 -04:00
|
|
|
static PyObject *
|
|
|
|
Unpickler_noload(Unpicklerobject *self, PyObject *args) {
|
2001-03-17 00:50:51 -04:00
|
|
|
UNLESS (PyArg_ParseTuple(args, ":noload"))
|
1997-12-03 21:13:01 -04:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return noload(self);
|
|
|
|
}
|
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
|
|
|
|
static struct PyMethodDef Unpickler_methods[] = {
|
1997-12-03 21:13:01 -04:00
|
|
|
{"load", (PyCFunction)Unpickler_load, 1,
|
|
|
|
"load() -- Load a pickle"
|
|
|
|
},
|
|
|
|
{"noload", (PyCFunction)Unpickler_noload, 1,
|
|
|
|
"noload() -- not load a pickle, but go through most of the motions\n"
|
|
|
|
"\n"
|
|
|
|
"This function can be used to read past a pickle without instantiating\n"
|
|
|
|
"any objects or importing any modules. It can also be used to find all\n"
|
|
|
|
"persistent references without instantiating any objects or importing\n"
|
|
|
|
"any modules.\n"
|
|
|
|
},
|
1997-04-09 14:36:32 -03:00
|
|
|
{NULL, NULL} /* sentinel */
|
1997-01-06 18:59:08 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
static Unpicklerobject *
|
1997-04-09 14:36:32 -03:00
|
|
|
newUnpicklerobject(PyObject *f) {
|
|
|
|
Unpicklerobject *self;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2000-05-03 20:44:39 -03:00
|
|
|
UNLESS (self = PyObject_New(Unpicklerobject, &Unpicklertype))
|
1997-04-09 14:36:32 -03:00
|
|
|
return NULL;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
self->file = NULL;
|
|
|
|
self->arg = NULL;
|
1998-11-25 12:18:00 -04:00
|
|
|
self->stack = (Pdata*)Pdata_New();
|
1997-04-09 14:36:32 -03:00
|
|
|
self->pers_func = NULL;
|
|
|
|
self->last_string = NULL;
|
|
|
|
self->marks = NULL;
|
|
|
|
self->num_marks = 0;
|
|
|
|
self->marks_size = 0;
|
|
|
|
self->buf_size = 0;
|
1997-01-06 18:59:08 -04:00
|
|
|
self->read = NULL;
|
1998-03-05 21:39:39 -04:00
|
|
|
self->readline = NULL;
|
1998-12-10 23:20:00 -04:00
|
|
|
self->safe_constructors = NULL;
|
1999-04-19 14:58:18 -03:00
|
|
|
self->find_class = NULL;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2001-03-17 00:50:51 -04:00
|
|
|
UNLESS (self->memo = PyDict_New())
|
2000-04-21 17:49:36 -03:00
|
|
|
goto err;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
Py_INCREF(f);
|
|
|
|
self->file = f;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
/* Set read, readline based on type of f */
|
|
|
|
if (PyFile_Check(f)) {
|
|
|
|
self->fp = PyFile_AsFile(f);
|
1999-03-29 16:00:14 -04:00
|
|
|
if (self->fp == NULL) {
|
2000-04-21 17:49:36 -03:00
|
|
|
PyErr_SetString(PyExc_ValueError, "I/O operation on closed file");
|
|
|
|
goto err;
|
1999-03-29 16:00:14 -04:00
|
|
|
}
|
1997-04-09 14:36:32 -03:00
|
|
|
self->read_func = read_file;
|
|
|
|
self->readline_func = readline_file;
|
1997-01-06 18:59:08 -04:00
|
|
|
}
|
1997-04-09 14:36:32 -03:00
|
|
|
else if (PycStringIO_InputCheck(f)) {
|
|
|
|
self->fp = NULL;
|
|
|
|
self->read_func = read_cStringIO;
|
|
|
|
self->readline_func = readline_cStringIO;
|
|
|
|
}
|
|
|
|
else {
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
self->fp = NULL;
|
|
|
|
self->read_func = read_other;
|
|
|
|
self->readline_func = readline_other;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1998-11-25 12:18:00 -04:00
|
|
|
UNLESS ((self->readline = PyObject_GetAttr(f, readline_str)) &&
|
1997-12-03 21:13:01 -04:00
|
|
|
(self->read = PyObject_GetAttr(f, read_str))) {
|
1997-04-09 14:36:32 -03:00
|
|
|
PyErr_Clear();
|
|
|
|
PyErr_SetString( PyExc_TypeError, "argument must have 'read' and "
|
|
|
|
"'readline' attributes" );
|
1998-11-25 12:18:00 -04:00
|
|
|
goto err;
|
1997-04-09 14:36:32 -03:00
|
|
|
}
|
|
|
|
}
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1998-11-25 12:18:00 -04:00
|
|
|
if (PyEval_GetRestricted()) {
|
|
|
|
/* Restricted execution, get private tables */
|
|
|
|
PyObject *m;
|
1997-12-03 21:13:01 -04:00
|
|
|
|
1998-11-25 12:18:00 -04:00
|
|
|
UNLESS (m=PyImport_Import(copy_reg_str)) goto err;
|
|
|
|
self->safe_constructors=PyObject_GetAttr(m, safe_constructors_str);
|
|
|
|
Py_DECREF(m);
|
|
|
|
UNLESS (self->safe_constructors) goto err;
|
1997-12-03 21:13:01 -04:00
|
|
|
}
|
|
|
|
else {
|
1998-11-25 12:18:00 -04:00
|
|
|
self->safe_constructors=safe_constructors;
|
|
|
|
Py_INCREF(safe_constructors);
|
1997-12-03 21:13:01 -04:00
|
|
|
}
|
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
return self;
|
1997-12-03 21:13:01 -04:00
|
|
|
|
|
|
|
err:
|
|
|
|
Py_DECREF((PyObject *)self);
|
|
|
|
return NULL;
|
1997-01-06 18:59:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static PyObject *
|
1997-04-09 14:36:32 -03:00
|
|
|
get_Unpickler(PyObject *self, PyObject *args) {
|
|
|
|
PyObject *file;
|
2001-03-17 00:50:51 -04:00
|
|
|
|
2000-02-29 09:59:29 -04:00
|
|
|
UNLESS (PyArg_ParseTuple(args, "O:Unpickler", &file))
|
1997-04-09 14:36:32 -03:00
|
|
|
return NULL;
|
|
|
|
return (PyObject *)newUnpicklerobject(file);
|
1997-01-06 18:59:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
1997-04-09 14:36:32 -03:00
|
|
|
Unpickler_dealloc(Unpicklerobject *self) {
|
|
|
|
Py_XDECREF(self->readline);
|
|
|
|
Py_XDECREF(self->read);
|
|
|
|
Py_XDECREF(self->file);
|
|
|
|
Py_XDECREF(self->memo);
|
|
|
|
Py_XDECREF(self->stack);
|
|
|
|
Py_XDECREF(self->pers_func);
|
|
|
|
Py_XDECREF(self->arg);
|
|
|
|
Py_XDECREF(self->last_string);
|
1997-12-03 21:13:01 -04:00
|
|
|
Py_XDECREF(self->safe_constructors);
|
1997-04-09 14:36:32 -03:00
|
|
|
|
|
|
|
if (self->marks) {
|
|
|
|
free(self->marks);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (self->buf_size) {
|
|
|
|
free(self->buf);
|
|
|
|
}
|
2001-03-17 00:50:51 -04:00
|
|
|
|
2000-05-03 20:44:39 -03:00
|
|
|
PyObject_Del(self);
|
1997-01-06 18:59:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static PyObject *
|
1997-04-09 14:36:32 -03:00
|
|
|
Unpickler_getattr(Unpicklerobject *self, char *name) {
|
|
|
|
if (!strcmp(name, "persistent_load")) {
|
|
|
|
if (!self->pers_func) {
|
|
|
|
PyErr_SetString(PyExc_AttributeError, name);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
Py_INCREF(self->pers_func);
|
|
|
|
return self->pers_func;
|
1997-01-06 18:59:08 -04:00
|
|
|
}
|
|
|
|
|
1999-04-19 14:58:18 -03:00
|
|
|
if (!strcmp(name, "find_global")) {
|
|
|
|
if (!self->find_class) {
|
|
|
|
PyErr_SetString(PyExc_AttributeError, name);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
Py_INCREF(self->find_class);
|
|
|
|
return self->find_class;
|
|
|
|
}
|
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
if (!strcmp(name, "memo")) {
|
|
|
|
if (!self->memo) {
|
|
|
|
PyErr_SetString(PyExc_AttributeError, name);
|
|
|
|
return NULL;
|
|
|
|
}
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
Py_INCREF(self->memo);
|
|
|
|
return self->memo;
|
1997-01-06 18:59:08 -04:00
|
|
|
}
|
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
if (!strcmp(name, "UnpicklingError")) {
|
|
|
|
Py_INCREF(UnpicklingError);
|
|
|
|
return UnpicklingError;
|
|
|
|
}
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
return Py_FindMethod(Unpickler_methods, (PyObject *)self, name);
|
1997-01-06 18:59:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
1997-04-09 14:36:32 -03:00
|
|
|
Unpickler_setattr(Unpicklerobject *self, char *name, PyObject *value) {
|
1998-08-13 20:13:52 -03:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
if (!strcmp(name, "persistent_load")) {
|
|
|
|
Py_XDECREF(self->pers_func);
|
|
|
|
self->pers_func = value;
|
1999-04-19 14:58:18 -03:00
|
|
|
Py_XINCREF(value);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!strcmp(name, "find_global")) {
|
|
|
|
Py_XDECREF(self->find_class);
|
|
|
|
self->find_class = value;
|
|
|
|
Py_XINCREF(value);
|
1997-04-09 14:36:32 -03:00
|
|
|
return 0;
|
|
|
|
}
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1999-04-19 14:58:18 -03:00
|
|
|
if (! value) {
|
|
|
|
PyErr_SetString(PyExc_TypeError,
|
|
|
|
"attribute deletion is not supported");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
1998-08-13 20:13:52 -03:00
|
|
|
if (strcmp(name, "memo") == 0) {
|
1998-11-25 12:18:00 -04:00
|
|
|
if (! PyDict_Check(value)) {
|
|
|
|
PyErr_SetString(PyExc_TypeError, "memo must be a dictionary");
|
|
|
|
return -1;
|
|
|
|
}
|
1998-08-13 20:13:52 -03:00
|
|
|
Py_XDECREF(self->memo);
|
|
|
|
self->memo = value;
|
|
|
|
Py_INCREF(value);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
PyErr_SetString(PyExc_AttributeError, name);
|
|
|
|
return -1;
|
1997-01-06 18:59:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static PyObject *
|
1997-04-09 14:36:32 -03:00
|
|
|
cpm_dump(PyObject *self, PyObject *args) {
|
|
|
|
PyObject *ob, *file, *res = NULL;
|
|
|
|
Picklerobject *pickler = 0;
|
|
|
|
int bin = 0;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1998-11-25 12:18:00 -04:00
|
|
|
UNLESS (PyArg_ParseTuple(args, "OO|i", &ob, &file, &bin))
|
1997-04-09 14:36:32 -03:00
|
|
|
goto finally;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1998-11-25 12:18:00 -04:00
|
|
|
UNLESS (pickler = newPicklerobject(file, bin))
|
1997-04-09 14:36:32 -03:00
|
|
|
goto finally;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
if (dump(pickler, ob) < 0)
|
|
|
|
goto finally;
|
|
|
|
|
|
|
|
Py_INCREF(Py_None);
|
|
|
|
res = Py_None;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
finally:
|
|
|
|
Py_XDECREF(pickler);
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
return res;
|
1997-01-06 18:59:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static PyObject *
|
1997-04-09 14:36:32 -03:00
|
|
|
cpm_dumps(PyObject *self, PyObject *args) {
|
|
|
|
PyObject *ob, *file = 0, *res = NULL;
|
|
|
|
Picklerobject *pickler = 0;
|
|
|
|
int bin = 0;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2000-02-29 09:59:29 -04:00
|
|
|
UNLESS (PyArg_ParseTuple(args, "O|i:dumps", &ob, &bin))
|
1997-04-09 14:36:32 -03:00
|
|
|
goto finally;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1998-11-25 12:18:00 -04:00
|
|
|
UNLESS (file = PycStringIO->NewOutput(128))
|
1997-04-09 14:36:32 -03:00
|
|
|
goto finally;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1998-11-25 12:18:00 -04:00
|
|
|
UNLESS (pickler = newPicklerobject(file, bin))
|
1997-04-09 14:36:32 -03:00
|
|
|
goto finally;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
if (dump(pickler, ob) < 0)
|
|
|
|
goto finally;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
res = PycStringIO->cgetvalue(file);
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
finally:
|
|
|
|
Py_XDECREF(pickler);
|
|
|
|
Py_XDECREF(file);
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
return res;
|
2001-03-17 00:50:51 -04:00
|
|
|
}
|
|
|
|
|
1997-01-06 18:59:08 -04:00
|
|
|
|
|
|
|
static PyObject *
|
1997-04-09 14:36:32 -03:00
|
|
|
cpm_load(PyObject *self, PyObject *args) {
|
|
|
|
Unpicklerobject *unpickler = 0;
|
|
|
|
PyObject *ob, *res = NULL;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2000-02-29 09:59:29 -04:00
|
|
|
UNLESS (PyArg_ParseTuple(args, "O:load", &ob))
|
1997-04-09 14:36:32 -03:00
|
|
|
goto finally;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1998-11-25 12:18:00 -04:00
|
|
|
UNLESS (unpickler = newUnpicklerobject(ob))
|
1997-04-09 14:36:32 -03:00
|
|
|
goto finally;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
res = load(unpickler);
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
finally:
|
|
|
|
Py_XDECREF(unpickler);
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
return res;
|
1997-01-06 18:59:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static PyObject *
|
1997-04-09 14:36:32 -03:00
|
|
|
cpm_loads(PyObject *self, PyObject *args) {
|
|
|
|
PyObject *ob, *file = 0, *res = NULL;
|
|
|
|
Unpicklerobject *unpickler = 0;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2000-02-29 09:59:29 -04:00
|
|
|
UNLESS (PyArg_ParseTuple(args, "S:loads", &ob))
|
1997-04-09 14:36:32 -03:00
|
|
|
goto finally;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1998-11-25 12:18:00 -04:00
|
|
|
UNLESS (file = PycStringIO->NewInput(ob))
|
1997-04-09 14:36:32 -03:00
|
|
|
goto finally;
|
2001-03-17 00:50:51 -04:00
|
|
|
|
1998-11-25 12:18:00 -04:00
|
|
|
UNLESS (unpickler = newUnpicklerobject(file))
|
1997-04-09 14:36:32 -03:00
|
|
|
goto finally;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
res = load(unpickler);
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
finally:
|
|
|
|
Py_XDECREF(file);
|
|
|
|
Py_XDECREF(unpickler);
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
return res;
|
1997-01-06 18:59:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-03-17 00:50:51 -04:00
|
|
|
static char Unpicklertype__doc__[] =
|
1997-12-03 21:13:01 -04:00
|
|
|
"Objects that know how to unpickle";
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-12-08 11:15:16 -04:00
|
|
|
static PyTypeObject Unpicklertype = {
|
|
|
|
PyObject_HEAD_INIT(NULL)
|
1997-04-09 14:36:32 -03:00
|
|
|
0, /*ob_size*/
|
|
|
|
"Unpickler", /*tp_name*/
|
|
|
|
sizeof(Unpicklerobject), /*tp_basicsize*/
|
|
|
|
0, /*tp_itemsize*/
|
|
|
|
/* methods */
|
|
|
|
(destructor)Unpickler_dealloc, /*tp_dealloc*/
|
|
|
|
(printfunc)0, /*tp_print*/
|
|
|
|
(getattrfunc)Unpickler_getattr, /*tp_getattr*/
|
|
|
|
(setattrfunc)Unpickler_setattr, /*tp_setattr*/
|
|
|
|
(cmpfunc)0, /*tp_compare*/
|
|
|
|
(reprfunc)0, /*tp_repr*/
|
|
|
|
0, /*tp_as_number*/
|
|
|
|
0, /*tp_as_sequence*/
|
|
|
|
0, /*tp_as_mapping*/
|
|
|
|
(hashfunc)0, /*tp_hash*/
|
|
|
|
(ternaryfunc)0, /*tp_call*/
|
|
|
|
(reprfunc)0, /*tp_str*/
|
|
|
|
|
|
|
|
/* Space for future expansion */
|
|
|
|
0L,0L,0L,0L,
|
|
|
|
Unpicklertype__doc__ /* Documentation string */
|
1997-12-08 11:15:16 -04:00
|
|
|
};
|
1997-04-09 14:36:32 -03:00
|
|
|
|
|
|
|
static struct PyMethodDef cPickle_methods[] = {
|
1997-12-03 21:13:01 -04:00
|
|
|
{"dump", (PyCFunction)cpm_dump, 1,
|
|
|
|
"dump(object, file, [binary]) --"
|
|
|
|
"Write an object in pickle format to the given file\n"
|
|
|
|
"\n"
|
|
|
|
"If the optional argument, binary, is provided and is true, then the\n"
|
|
|
|
"pickle will be written in binary format, which is more space and\n"
|
|
|
|
"computationally efficient. \n"
|
|
|
|
},
|
|
|
|
{"dumps", (PyCFunction)cpm_dumps, 1,
|
|
|
|
"dumps(object, [binary]) --"
|
|
|
|
"Return a string containing an object in pickle format\n"
|
|
|
|
"\n"
|
|
|
|
"If the optional argument, binary, is provided and is true, then the\n"
|
|
|
|
"pickle will be written in binary format, which is more space and\n"
|
|
|
|
"computationally efficient. \n"
|
|
|
|
},
|
|
|
|
{"load", (PyCFunction)cpm_load, 1,
|
|
|
|
"load(file) -- Load a pickle from the given file"},
|
|
|
|
{"loads", (PyCFunction)cpm_loads, 1,
|
|
|
|
"loads(string) -- Load a pickle from the given string"},
|
|
|
|
{"Pickler", (PyCFunction)get_Pickler, 1,
|
|
|
|
"Pickler(file, [binary]) -- Create a pickler\n"
|
|
|
|
"\n"
|
|
|
|
"If the optional argument, binary, is provided and is true, then\n"
|
|
|
|
"pickles will be written in binary format, which is more space and\n"
|
|
|
|
"computationally efficient. \n"
|
|
|
|
},
|
|
|
|
{"Unpickler", (PyCFunction)get_Unpickler, 1,
|
|
|
|
"Unpickler(file) -- Create an unpickler"},
|
1997-01-06 18:59:08 -04:00
|
|
|
{ NULL, NULL }
|
|
|
|
};
|
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
static int
|
2000-09-07 11:35:37 -03:00
|
|
|
init_stuff(PyObject *module_dict) {
|
2001-07-17 15:34:03 -03:00
|
|
|
PyObject *copy_reg, *t, *r;
|
1997-04-09 14:36:32 -03:00
|
|
|
|
|
|
|
#define INIT_STR(S) UNLESS(S ## _str=PyString_FromString(#S)) return -1;
|
|
|
|
|
|
|
|
INIT_STR(__class__);
|
|
|
|
INIT_STR(__getinitargs__);
|
|
|
|
INIT_STR(__dict__);
|
|
|
|
INIT_STR(__getstate__);
|
|
|
|
INIT_STR(__setstate__);
|
|
|
|
INIT_STR(__name__);
|
1997-08-13 00:14:41 -03:00
|
|
|
INIT_STR(__main__);
|
1997-04-09 14:36:32 -03:00
|
|
|
INIT_STR(__reduce__);
|
|
|
|
INIT_STR(write);
|
|
|
|
INIT_STR(__safe_for_unpickling__);
|
|
|
|
INIT_STR(append);
|
|
|
|
INIT_STR(read);
|
|
|
|
INIT_STR(readline);
|
1997-12-03 21:13:01 -04:00
|
|
|
INIT_STR(copy_reg);
|
|
|
|
INIT_STR(dispatch_table);
|
|
|
|
INIT_STR(safe_constructors);
|
1997-12-08 11:15:16 -04:00
|
|
|
INIT_STR(__basicnew__);
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1998-11-25 12:18:00 -04:00
|
|
|
UNLESS (copy_reg = PyImport_ImportModule("copy_reg"))
|
1997-04-09 14:36:32 -03:00
|
|
|
return -1;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-12-03 21:13:01 -04:00
|
|
|
/* These next few are special because we want to use different
|
|
|
|
ones in restricted mode. */
|
|
|
|
|
1998-11-25 12:18:00 -04:00
|
|
|
UNLESS (dispatch_table = PyObject_GetAttr(copy_reg, dispatch_table_str))
|
1997-04-09 14:36:32 -03:00
|
|
|
return -1;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1998-11-25 12:18:00 -04:00
|
|
|
UNLESS (safe_constructors = PyObject_GetAttr(copy_reg,
|
|
|
|
safe_constructors_str))
|
1997-04-09 14:36:32 -03:00
|
|
|
return -1;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
Py_DECREF(copy_reg);
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-12-03 21:13:01 -04:00
|
|
|
/* Down to here ********************************** */
|
|
|
|
|
1998-11-25 12:18:00 -04:00
|
|
|
UNLESS (empty_tuple = PyTuple_New(0))
|
1997-04-09 14:36:32 -03:00
|
|
|
return -1;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1999-06-09 12:23:31 -03:00
|
|
|
/* Ugh */
|
|
|
|
UNLESS (t=PyImport_ImportModule("__builtin__")) return -1;
|
|
|
|
if (PyDict_SetItemString(module_dict, "__builtins__", t) < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
UNLESS (t=PyDict_New()) return -1;
|
1999-06-15 11:36:59 -03:00
|
|
|
UNLESS (r=PyRun_String(
|
|
|
|
"def __init__(self, *args): self.args=args\n\n"
|
|
|
|
"def __str__(self):\n"
|
|
|
|
" return self.args and ('%s' % self.args[0]) or '(what)'\n",
|
|
|
|
Py_file_input,
|
|
|
|
module_dict, t) ) return -1;
|
|
|
|
Py_DECREF(r);
|
1999-06-09 12:23:31 -03:00
|
|
|
|
|
|
|
UNLESS (PickleError = PyErr_NewException("cPickle.PickleError", NULL, t))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
Py_DECREF(t);
|
|
|
|
|
2001-03-17 00:50:51 -04:00
|
|
|
|
|
|
|
UNLESS (PicklingError = PyErr_NewException("cPickle.PicklingError",
|
1999-06-09 12:23:31 -03:00
|
|
|
PickleError, NULL))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
UNLESS (t=PyDict_New()) return -1;
|
1999-06-15 11:36:59 -03:00
|
|
|
UNLESS (r=PyRun_String(
|
|
|
|
"def __init__(self, *args): self.args=args\n\n"
|
|
|
|
"def __str__(self):\n"
|
|
|
|
" a=self.args\n"
|
|
|
|
" a=a and type(a[0]) or '(what)'\n"
|
|
|
|
" return 'Cannot pickle %s objects' % a\n"
|
|
|
|
, Py_file_input,
|
|
|
|
module_dict, t) ) return -1;
|
|
|
|
Py_DECREF(r);
|
1999-06-09 12:23:31 -03:00
|
|
|
|
|
|
|
UNLESS (UnpickleableError = PyErr_NewException(
|
|
|
|
"cPickle.UnpickleableError", PicklingError, t))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
Py_DECREF(t);
|
|
|
|
|
2001-03-17 00:50:51 -04:00
|
|
|
UNLESS (UnpicklingError = PyErr_NewException("cPickle.UnpicklingError",
|
1999-06-09 12:23:31 -03:00
|
|
|
PickleError, NULL))
|
|
|
|
return -1;
|
|
|
|
|
2001-03-17 00:50:51 -04:00
|
|
|
if (PyDict_SetItemString(module_dict, "PickleError",
|
1999-06-09 12:23:31 -03:00
|
|
|
PickleError) < 0)
|
1997-04-09 14:36:32 -03:00
|
|
|
return -1;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2001-03-17 00:50:51 -04:00
|
|
|
if (PyDict_SetItemString(module_dict, "PicklingError",
|
1997-04-09 14:36:32 -03:00
|
|
|
PicklingError) < 0)
|
|
|
|
return -1;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
if (PyDict_SetItemString(module_dict, "UnpicklingError",
|
|
|
|
UnpicklingError) < 0)
|
|
|
|
return -1;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1999-06-09 12:23:31 -03:00
|
|
|
if (PyDict_SetItemString(module_dict, "UnpickleableError",
|
|
|
|
UnpickleableError) < 0)
|
|
|
|
return -1;
|
|
|
|
|
1998-11-25 12:18:00 -04:00
|
|
|
UNLESS (BadPickleGet = PyString_FromString("cPickle.BadPickleGet"))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (PyDict_SetItemString(module_dict, "BadPickleGet",
|
|
|
|
BadPickleGet) < 0)
|
|
|
|
return -1;
|
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
PycString_IMPORT;
|
2001-03-17 00:50:51 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
return 0;
|
1997-01-06 18:59:08 -04:00
|
|
|
}
|
|
|
|
|
1999-02-04 10:54:04 -04:00
|
|
|
#ifndef DL_EXPORT /* declarations for DLL import/export */
|
|
|
|
#define DL_EXPORT(RTYPE) RTYPE
|
|
|
|
#endif
|
1998-12-04 14:48:44 -04:00
|
|
|
DL_EXPORT(void)
|
2000-07-24 11:43:35 -03:00
|
|
|
initcPickle(void) {
|
2000-09-07 11:35:37 -03:00
|
|
|
PyObject *m, *d, *di, *v, *k;
|
|
|
|
int i;
|
1999-07-13 12:18:58 -03:00
|
|
|
char *rev="1.71";
|
1997-04-09 14:36:32 -03:00
|
|
|
PyObject *format_version;
|
|
|
|
PyObject *compatible_formats;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-12-08 11:15:16 -04:00
|
|
|
Picklertype.ob_type = &PyType_Type;
|
|
|
|
Unpicklertype.ob_type = &PyType_Type;
|
1998-11-25 12:18:00 -04:00
|
|
|
PdataType.ob_type = &PyType_Type;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2001-03-17 00:50:51 -04:00
|
|
|
/* Initialize some pieces. We need to do this before module creation,
|
|
|
|
so we're forced to use a temporary dictionary. :(
|
2000-09-07 11:35:37 -03:00
|
|
|
*/
|
|
|
|
di=PyDict_New();
|
|
|
|
if (!di) return;
|
|
|
|
if (init_stuff(di) < 0) return;
|
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
/* Create the module and add the functions */
|
|
|
|
m = Py_InitModule4("cPickle", cPickle_methods,
|
|
|
|
cPickle_module_documentation,
|
|
|
|
(PyObject*)NULL,PYTHON_API_VERSION);
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
/* Add some symbolic constants to the module */
|
|
|
|
d = PyModule_GetDict(m);
|
1997-12-03 21:13:01 -04:00
|
|
|
PyDict_SetItemString(d,"__version__", v = PyString_FromString(rev));
|
1997-09-03 15:19:40 -03:00
|
|
|
Py_XDECREF(v);
|
1997-01-14 13:45:08 -04:00
|
|
|
|
2000-09-07 11:35:37 -03:00
|
|
|
/* Copy data from di. Waaa. */
|
|
|
|
for (i=0; PyDict_Next(di, &i, &k, &v); ) {
|
|
|
|
if (PyObject_SetItem(d, k, v) < 0) {
|
|
|
|
Py_DECREF(di);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Py_DECREF(di);
|
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
format_version = PyString_FromString("1.3");
|
|
|
|
compatible_formats = Py_BuildValue("[sss]", "1.0", "1.1", "1.2");
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
PyDict_SetItemString(d, "format_version", format_version);
|
|
|
|
PyDict_SetItemString(d, "compatible_formats", compatible_formats);
|
1997-09-03 15:19:40 -03:00
|
|
|
Py_XDECREF(format_version);
|
|
|
|
Py_XDECREF(compatible_formats);
|
1997-01-06 18:59:08 -04:00
|
|
|
}
|