1997-01-06 18:59:08 -04:00
|
|
|
#include "Python.h"
|
|
|
|
#include "cStringIO.h"
|
Progress on SF bug #466175 and general cleanup.
Add a fast_container member to Picklerobject. If fast is true, then
fast_container counts the depth of nested container calls. If the
depth exceeds FAST_LIMIT (2000), the fast flag is ignored and the
normal checks occur. This approach is much like the approach for
prevent stack overflow for comparison and reprs of recursive objects
(e.g. [[...]]).
- Fast container used for save_list(), save_dict(), and
save_inst().
XXX Not clear which other save_xxx() functions should use it.
Make Picklerobject into new-style types, using PyObject_GenericGetAttr()
and PyObject_GenericSetAttr().
- Use PyMemberDef for binary and fast members
- Use PyGetSetDef for persistent_id, inst_persistent_id, memo, and
PicklingError.
XXX Not all of these seem like they need to use getset, but it's
not clear why the old getattr() and setattr() had such odd
semantics. One change is that the getvalue() attribute will
exist on all Picklers, not just list-based picklers; I think
this is a more rationale interface.
There is a long laundry list of other changes:
- Remove unused #defines for PyList_SET_ITEM() etc.
- Make some of the indentation consistent
- Replace uses of cPickle_PyMapping_HasKey() where the first
argument is self->memo with calls to PyDict_GetItem(), because
self->memo must be a dictionary.
- Don't bother to check if cPickle_PyMapping_HasKey() returns < 0,
because it can only return 0 or 1.
- Replace uses of PyObject_CallObject() with PyObject_Call(), when
we can guarantee that the argument tuple is really a tuple.
Performance impacts of these changes:
- 5% speedup for normal pickling
- No change to fast-mode pickling.
XXX Really need tests for all the features in cPickle that aren't in
pickle.
2001-10-12 01:11:06 -03:00
|
|
|
#include "structmember.h"
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2002-06-13 17:33:02 -03:00
|
|
|
PyDoc_STRVAR(cPickle_module_documentation,
|
2003-02-01 02:27:59 -04:00
|
|
|
"C implementation and optimization of the Python pickle module.");
|
2002-06-13 17:33:02 -03:00
|
|
|
|
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-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
|
|
|
|
2003-02-01 12:45:06 -04:00
|
|
|
/* Bump this when new opcodes are added to the pickle protocol. */
|
2003-02-13 11:44:41 -04:00
|
|
|
#define HIGHEST_PROTOCOL 2
|
2003-02-01 12:45:06 -04:00
|
|
|
|
2003-02-01 02:22:36 -04:00
|
|
|
/*
|
|
|
|
* Pickle opcodes. These must be kept in synch with pickle.py. Extensive
|
|
|
|
* docs are in pickletools.py.
|
|
|
|
*/
|
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'
|
2003-02-01 02:22:36 -04:00
|
|
|
|
|
|
|
/* Protocol 2. */
|
|
|
|
#define PROTO '\x80' /* identify pickle protocol */
|
|
|
|
#define NEWOBJ '\x81' /* build object by applying cls.__new__ to argtuple */
|
|
|
|
#define EXT1 '\x82' /* push object from extension registry; 1-byte index */
|
|
|
|
#define EXT2 '\x83' /* ditto, but 2-byte index */
|
|
|
|
#define EXT4 '\x84' /* ditto, but 4-byte index */
|
|
|
|
#define TUPLE1 '\x85' /* build 1-tuple from stack top */
|
|
|
|
#define TUPLE2 '\x86' /* build 2-tuple from two topmost stack items */
|
|
|
|
#define TUPLE3 '\x87' /* build 3-tuple from three topmost stack items */
|
|
|
|
#define NEWTRUE '\x88' /* push True */
|
|
|
|
#define NEWFALSE '\x89' /* push False */
|
|
|
|
#define LONG1 '\x8a' /* push long from < 256 bytes */
|
|
|
|
#define LONG4 '\x8b' /* push really big long */
|
|
|
|
|
|
|
|
/* There aren't opcodes -- they're ways to pickle bools before protocol 2,
|
|
|
|
* so that unpicklers written before bools were introduced unpickle them
|
|
|
|
* as ints, but unpicklers after can recognize that bools were intended.
|
|
|
|
* Note that protocol 2 added direct ways to pickle bools.
|
|
|
|
*/
|
2002-06-26 17:40:42 -03:00
|
|
|
#undef TRUE
|
2002-04-05 15:30:08 -04:00
|
|
|
#define TRUE "I01\n"
|
2002-06-26 17:40:42 -03:00
|
|
|
#undef FALSE
|
2002-04-05 15:30:08 -04:00
|
|
|
#define FALSE "I00\n"
|
2002-04-03 18:41:51 -04:00
|
|
|
|
2003-02-11 17:06:20 -04:00
|
|
|
/* Keep in synch with pickle.Pickler._BATCHSIZE. This is how many elements
|
2003-02-11 18:43:24 -04:00
|
|
|
* batch_list/dict() pumps out before doing APPENDS/SETITEMS. Nothing will
|
|
|
|
* break if this gets out of synch with pickle.py, but it's unclear that
|
|
|
|
* would help anything either.
|
2003-02-11 17:06:20 -04:00
|
|
|
*/
|
|
|
|
#define BATCHSIZE 1000
|
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
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;
|
|
|
|
|
2003-02-03 20:21:07 -04:00
|
|
|
/* As the name says, an empty tuple. */
|
|
|
|
static PyObject *empty_tuple;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2003-02-03 20:21:07 -04:00
|
|
|
/* copy_reg.dispatch_table, {type_object: pickling_function} */
|
1997-04-09 14:36:32 -03:00
|
|
|
static PyObject *dispatch_table;
|
2003-02-03 20:21:07 -04:00
|
|
|
|
|
|
|
/* For EXT[124] opcodes. */
|
2003-02-03 21:54:49 -04:00
|
|
|
/* copy_reg._extension_registry, {(module_name, function_name): code} */
|
2003-02-03 20:21:07 -04:00
|
|
|
static PyObject *extension_registry;
|
2003-02-03 21:54:49 -04:00
|
|
|
/* copy_reg._inverted_registry, {code: (module_name, function_name)} */
|
2003-02-03 20:21:07 -04:00
|
|
|
static PyObject *inverted_registry;
|
2003-02-03 21:54:49 -04:00
|
|
|
/* copy_reg._extension_cache, {code: object} */
|
2003-02-03 20:21:07 -04:00
|
|
|
static PyObject *extension_cache;
|
1997-04-09 14:36:32 -03:00
|
|
|
|
2003-02-04 16:56:09 -04:00
|
|
|
/* For looking up name pairs in copy_reg._extension_registry. */
|
|
|
|
static PyObject *two_tuple;
|
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
static PyObject *__class___str, *__getinitargs___str, *__dict___str,
|
|
|
|
*__getstate___str, *__setstate___str, *__name___str, *__reduce___str,
|
2003-02-18 21:45:13 -04:00
|
|
|
*__reduce_ex___str,
|
2003-01-31 22:16:37 -04:00
|
|
|
*write_str, *append_str,
|
2006-04-09 22:03:32 -03:00
|
|
|
*read_str, *readline_str, *__main___str,
|
2003-01-31 22:16:37 -04:00
|
|
|
*copy_reg_str, *dispatch_table_str;
|
1997-04-09 14:36:32 -03:00
|
|
|
|
1998-11-25 12:18:00 -04:00
|
|
|
/*************************************************************************
|
|
|
|
Internal Data type for pickle data. */
|
|
|
|
|
|
|
|
typedef struct {
|
2002-04-01 13:40:08 -04:00
|
|
|
PyObject_HEAD
|
2003-02-02 16:29:39 -04:00
|
|
|
int length; /* number of initial slots in data currently used */
|
|
|
|
int size; /* number of slots in data allocated */
|
2002-04-01 13:40:08 -04:00
|
|
|
PyObject **data;
|
1998-11-25 12:18:00 -04:00
|
|
|
} Pdata;
|
|
|
|
|
2001-03-17 00:50:51 -04:00
|
|
|
static void
|
2003-02-01 02:24:36 -04:00
|
|
|
Pdata_dealloc(Pdata *self)
|
2002-04-01 13:40:08 -04:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
PyObject **p;
|
2003-02-01 02:24:36 -04:00
|
|
|
|
2003-02-02 16:29:39 -04:00
|
|
|
for (i = self->length, p = self->data; --i >= 0; p++) {
|
|
|
|
Py_DECREF(*p);
|
|
|
|
}
|
|
|
|
if (self->data)
|
|
|
|
free(self->data);
|
2002-04-01 13:40:08 -04:00
|
|
|
PyObject_Del(self);
|
1998-11-25 12:18:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static PyTypeObject PdataType = {
|
2002-04-01 13:40:08 -04:00
|
|
|
PyObject_HEAD_INIT(NULL) 0, "cPickle.Pdata", sizeof(Pdata), 0,
|
|
|
|
(destructor)Pdata_dealloc,
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0L,0L,0L,0L, ""
|
1998-11-25 12:18:00 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
#define Pdata_Check(O) ((O)->ob_type == &PdataType)
|
|
|
|
|
|
|
|
static PyObject *
|
2003-02-01 02:24:36 -04:00
|
|
|
Pdata_New(void)
|
2002-04-01 13:40:08 -04:00
|
|
|
{
|
|
|
|
Pdata *self;
|
1998-11-25 12:18:00 -04:00
|
|
|
|
2003-02-02 16:29:39 -04:00
|
|
|
if (!(self = PyObject_New(Pdata, &PdataType)))
|
|
|
|
return NULL;
|
|
|
|
self->size = 8;
|
|
|
|
self->length = 0;
|
|
|
|
self->data = malloc(self->size * sizeof(PyObject*));
|
|
|
|
if (self->data)
|
|
|
|
return (PyObject*)self;
|
2002-04-01 13:40:08 -04:00
|
|
|
Py_DECREF(self);
|
|
|
|
return PyErr_NoMemory();
|
1998-11-25 12:18:00 -04:00
|
|
|
}
|
|
|
|
|
2001-03-17 00:50:51 -04:00
|
|
|
static int
|
2003-02-01 02:24:36 -04:00
|
|
|
stackUnderflow(void)
|
2002-04-01 13:40:08 -04:00
|
|
|
{
|
|
|
|
PyErr_SetString(UnpicklingError, "unpickling stack underflow");
|
|
|
|
return -1;
|
1998-11-25 12:18:00 -04:00
|
|
|
}
|
|
|
|
|
2003-02-02 16:29:39 -04:00
|
|
|
/* Retain only the initial clearto items. If clearto >= the current
|
|
|
|
* number of items, this is a (non-erroneous) NOP.
|
|
|
|
*/
|
1998-11-25 12:18:00 -04:00
|
|
|
static int
|
2003-02-01 02:24:36 -04:00
|
|
|
Pdata_clear(Pdata *self, int clearto)
|
2002-04-01 13:40:08 -04:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
PyObject **p;
|
1998-11-25 12:18:00 -04:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
if (clearto < 0) return stackUnderflow();
|
|
|
|
if (clearto >= self->length) return 0;
|
1998-11-25 12:18:00 -04:00
|
|
|
|
2003-02-02 16:29:39 -04:00
|
|
|
for (i = self->length, p = self->data + clearto;
|
|
|
|
--i >= clearto;
|
|
|
|
p++) {
|
2006-07-22 13:20:49 -03:00
|
|
|
Py_CLEAR(*p);
|
2003-02-02 16:29:39 -04:00
|
|
|
}
|
|
|
|
self->length = clearto;
|
1998-11-25 12:18:00 -04:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
return 0;
|
1998-11-25 12:18:00 -04:00
|
|
|
}
|
|
|
|
|
2001-03-17 00:50:51 -04:00
|
|
|
static int
|
2003-02-01 02:24:36 -04:00
|
|
|
Pdata_grow(Pdata *self)
|
2002-04-01 13:40:08 -04:00
|
|
|
{
|
2003-02-02 16:29:39 -04:00
|
|
|
int bigger;
|
|
|
|
size_t nbytes;
|
2006-07-22 13:20:49 -03:00
|
|
|
PyObject **tmp;
|
2003-02-02 16:29:39 -04:00
|
|
|
|
|
|
|
bigger = self->size << 1;
|
2003-02-03 11:45:56 -04:00
|
|
|
if (bigger <= 0) /* was 0, or new value overflows */
|
2003-02-02 16:29:39 -04:00
|
|
|
goto nomemory;
|
|
|
|
if ((int)(size_t)bigger != bigger)
|
|
|
|
goto nomemory;
|
|
|
|
nbytes = (size_t)bigger * sizeof(PyObject *);
|
|
|
|
if (nbytes / sizeof(PyObject *) != (size_t)bigger)
|
|
|
|
goto nomemory;
|
2006-07-22 13:20:49 -03:00
|
|
|
tmp = realloc(self->data, nbytes);
|
|
|
|
if (tmp == NULL)
|
2003-02-02 16:29:39 -04:00
|
|
|
goto nomemory;
|
2006-07-22 13:20:49 -03:00
|
|
|
self->data = tmp;
|
2003-02-02 16:29:39 -04:00
|
|
|
self->size = bigger;
|
2002-04-01 13:40:08 -04:00
|
|
|
return 0;
|
1998-11-25 12:18:00 -04:00
|
|
|
|
2003-02-02 16:29:39 -04:00
|
|
|
nomemory:
|
|
|
|
PyErr_NoMemory();
|
|
|
|
return -1;
|
1998-11-25 12:18:00 -04:00
|
|
|
}
|
|
|
|
|
2003-02-03 11:45:56 -04:00
|
|
|
/* D is a Pdata*. Pop the topmost element and store it into V, which
|
|
|
|
* must be an lvalue holding PyObject*. On stack underflow, UnpicklingError
|
2003-02-02 16:29:39 -04:00
|
|
|
* is raised and V is set to NULL. D and V may be evaluated several times.
|
|
|
|
*/
|
|
|
|
#define PDATA_POP(D, V) { \
|
2003-02-03 11:45:56 -04:00
|
|
|
if ((D)->length) \
|
|
|
|
(V) = (D)->data[--((D)->length)]; \
|
|
|
|
else { \
|
|
|
|
PyErr_SetString(UnpicklingError, "bad pickle data"); \
|
|
|
|
(V) = NULL; \
|
|
|
|
} \
|
|
|
|
}
|
|
|
|
|
|
|
|
/* PDATA_PUSH and PDATA_APPEND both push rvalue PyObject* O on to Pdata*
|
|
|
|
* D. If the Pdata stack can't be grown to hold the new value, both
|
|
|
|
* raise MemoryError and execute "return ER". The difference is in ownership
|
|
|
|
* of O after: _PUSH transfers ownership of O from the caller to the stack
|
|
|
|
* (no incref of O is done, and in case of error O is decrefed), while
|
|
|
|
* _APPEND pushes a new reference.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Push O on stack D, giving ownership of O to the stack. */
|
|
|
|
#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); \
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Push O on stack D, pushing a new reference. */
|
|
|
|
#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); \
|
2003-02-02 16:29:39 -04:00
|
|
|
}
|
1998-11-25 12:18:00 -04:00
|
|
|
|
2003-02-03 11:45:56 -04:00
|
|
|
|
1998-11-25 12:18:00 -04:00
|
|
|
static PyObject *
|
2003-02-01 02:24:36 -04:00
|
|
|
Pdata_popTuple(Pdata *self, int start)
|
2002-04-01 13:40:08 -04:00
|
|
|
{
|
|
|
|
PyObject *r;
|
|
|
|
int i, j, l;
|
2003-02-01 02:24:36 -04:00
|
|
|
|
2003-02-02 16:29:39 -04:00
|
|
|
l = self->length-start;
|
|
|
|
r = PyTuple_New(l);
|
|
|
|
if (r == NULL)
|
|
|
|
return NULL;
|
|
|
|
for (i = start, j = 0 ; j < l; i++, j++)
|
2002-04-01 13:40:08 -04:00
|
|
|
PyTuple_SET_ITEM(r, j, self->data[i]);
|
2003-02-01 02:24:36 -04:00
|
|
|
|
2003-02-02 16:29:39 -04:00
|
|
|
self->length = start;
|
2002-04-01 13:40:08 -04:00
|
|
|
return r;
|
1998-11-25 12:18:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
2003-02-01 02:24:36 -04:00
|
|
|
Pdata_popList(Pdata *self, int start)
|
2002-04-01 13:40:08 -04:00
|
|
|
{
|
|
|
|
PyObject *r;
|
|
|
|
int i, j, l;
|
1998-11-25 12:18:00 -04:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
l=self->length-start;
|
|
|
|
if (!( r=PyList_New(l))) return NULL;
|
|
|
|
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
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
self->length=start;
|
|
|
|
return r;
|
1998-11-25 12:18:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*************************************************************************/
|
|
|
|
|
|
|
|
#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 {
|
2002-04-01 13:40:08 -04:00
|
|
|
PyObject_HEAD
|
|
|
|
FILE *fp;
|
|
|
|
PyObject *write;
|
|
|
|
PyObject *file;
|
|
|
|
PyObject *memo;
|
|
|
|
PyObject *arg;
|
|
|
|
PyObject *pers_func;
|
|
|
|
PyObject *inst_pers_func;
|
2003-02-01 02:22:36 -04:00
|
|
|
|
|
|
|
/* pickle protocol number, >= 0 */
|
|
|
|
int proto;
|
|
|
|
|
|
|
|
/* bool, true if proto > 0 */
|
2002-04-01 13:40:08 -04:00
|
|
|
int bin;
|
2003-02-01 02:22:36 -04:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
int fast; /* Fast mode doesn't save in memo, don't use if circ ref */
|
2002-08-04 05:20:23 -03:00
|
|
|
int nesting;
|
2006-02-15 13:27:45 -04:00
|
|
|
int (*write_func)(struct Picklerobject *, const char *, Py_ssize_t);
|
2002-04-01 13:40:08 -04:00
|
|
|
char *write_buf;
|
|
|
|
int buf_size;
|
|
|
|
PyObject *dispatch_table;
|
|
|
|
int fast_container; /* count nested container dumps */
|
|
|
|
PyObject *fast_memo;
|
1997-04-09 14:36:32 -03:00
|
|
|
} Picklerobject;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2001-12-21 16:04:22 -04:00
|
|
|
#ifndef PY_CPICKLE_FAST_LIMIT
|
|
|
|
#define PY_CPICKLE_FAST_LIMIT 50
|
|
|
|
#endif
|
Progress on SF bug #466175 and general cleanup.
Add a fast_container member to Picklerobject. If fast is true, then
fast_container counts the depth of nested container calls. If the
depth exceeds FAST_LIMIT (2000), the fast flag is ignored and the
normal checks occur. This approach is much like the approach for
prevent stack overflow for comparison and reprs of recursive objects
(e.g. [[...]]).
- Fast container used for save_list(), save_dict(), and
save_inst().
XXX Not clear which other save_xxx() functions should use it.
Make Picklerobject into new-style types, using PyObject_GenericGetAttr()
and PyObject_GenericSetAttr().
- Use PyMemberDef for binary and fast members
- Use PyGetSetDef for persistent_id, inst_persistent_id, memo, and
PicklingError.
XXX Not all of these seem like they need to use getset, but it's
not clear why the old getattr() and setattr() had such odd
semantics. One change is that the getvalue() attribute will
exist on all Picklers, not just list-based picklers; I think
this is a more rationale interface.
There is a long laundry list of other changes:
- Remove unused #defines for PyList_SET_ITEM() etc.
- Make some of the indentation consistent
- Replace uses of cPickle_PyMapping_HasKey() where the first
argument is self->memo with calls to PyDict_GetItem(), because
self->memo must be a dictionary.
- Don't bother to check if cPickle_PyMapping_HasKey() returns < 0,
because it can only return 0 or 1.
- Replace uses of PyObject_CallObject() with PyObject_Call(), when
we can guarantee that the argument tuple is really a tuple.
Performance impacts of these changes:
- 5% speedup for normal pickling
- No change to fast-mode pickling.
XXX Really need tests for all the features in cPickle that aren't in
pickle.
2001-10-12 01:11:06 -03:00
|
|
|
|
2002-07-17 13:30:39 -03:00
|
|
|
static PyTypeObject Picklertype;
|
1997-04-09 14:36:32 -03:00
|
|
|
|
2000-07-22 20:56:07 -03:00
|
|
|
typedef struct Unpicklerobject {
|
2002-04-01 13:40:08 -04:00
|
|
|
PyObject_HEAD
|
|
|
|
FILE *fp;
|
|
|
|
PyObject *file;
|
|
|
|
PyObject *readline;
|
|
|
|
PyObject *read;
|
|
|
|
PyObject *memo;
|
|
|
|
PyObject *arg;
|
|
|
|
Pdata *stack;
|
|
|
|
PyObject *mark;
|
|
|
|
PyObject *pers_func;
|
|
|
|
PyObject *last_string;
|
|
|
|
int *marks;
|
|
|
|
int num_marks;
|
|
|
|
int marks_size;
|
2006-02-15 13:27:45 -04:00
|
|
|
Py_ssize_t (*read_func)(struct Unpicklerobject *, char **, Py_ssize_t);
|
|
|
|
Py_ssize_t (*readline_func)(struct Unpicklerobject *, char **);
|
2002-04-01 13:40:08 -04:00
|
|
|
int buf_size;
|
|
|
|
char *buf;
|
|
|
|
PyObject *find_class;
|
1997-04-09 14:36:32 -03:00
|
|
|
} Unpicklerobject;
|
2001-03-17 00:50:51 -04:00
|
|
|
|
2002-07-17 13:30:39 -03:00
|
|
|
static 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 *);
|
|
|
|
|
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, ...)
|
|
|
|
{
|
2002-04-01 13:40:08 -04:00
|
|
|
va_list va;
|
|
|
|
PyObject *args=0, *retval=0;
|
|
|
|
va_start(va, format);
|
2003-02-01 02:24:36 -04:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
if (format) args = Py_VaBuildValue(format, va);
|
|
|
|
va_end(va);
|
|
|
|
if (format && ! args) return NULL;
|
2003-02-01 02:24:36 -04:00
|
|
|
if (stringformat && !(retval=PyString_FromString(stringformat)))
|
2002-04-01 13:40:08 -04:00
|
|
|
return NULL;
|
2003-02-01 02:24:36 -04:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
if (retval) {
|
|
|
|
if (args) {
|
|
|
|
PyObject *v;
|
|
|
|
v=PyString_Format(retval, args);
|
|
|
|
Py_DECREF(retval);
|
|
|
|
Py_DECREF(args);
|
|
|
|
if (! v) return NULL;
|
|
|
|
retval=v;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
if (args) retval=args;
|
|
|
|
else {
|
|
|
|
PyErr_SetObject(ErrType,Py_None);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
PyErr_SetObject(ErrType,retval);
|
|
|
|
Py_DECREF(retval);
|
|
|
|
return NULL;
|
1997-04-09 14:36:32 -03:00
|
|
|
}
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2001-03-17 00:50:51 -04:00
|
|
|
static int
|
2006-02-15 13:27:45 -04:00
|
|
|
write_file(Picklerobject *self, const char *s, Py_ssize_t n)
|
2002-04-01 13:40:08 -04:00
|
|
|
{
|
|
|
|
size_t nbyteswritten;
|
2001-03-17 00:50:51 -04:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
if (s == NULL) {
|
|
|
|
return 0;
|
|
|
|
}
|
1997-04-09 14:36:32 -03:00
|
|
|
|
2006-02-15 13:27:45 -04:00
|
|
|
if (n > INT_MAX) {
|
|
|
|
/* String too large */
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
Py_BEGIN_ALLOW_THREADS
|
2002-07-18 17:58:57 -03:00
|
|
|
nbyteswritten = fwrite(s, sizeof(char), n, self->fp);
|
2002-04-01 13:40:08 -04:00
|
|
|
Py_END_ALLOW_THREADS
|
2002-07-18 17:58:57 -03:00
|
|
|
if (nbyteswritten != (size_t)n) {
|
|
|
|
PyErr_SetFromErrno(PyExc_IOError);
|
|
|
|
return -1;
|
|
|
|
}
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2006-02-15 13:27:45 -04:00
|
|
|
return (int)n;
|
1997-01-06 18:59:08 -04:00
|
|
|
}
|
|
|
|
|
2001-03-17 00:50:51 -04:00
|
|
|
static int
|
2006-02-15 13:27:45 -04:00
|
|
|
write_cStringIO(Picklerobject *self, const char *s, Py_ssize_t n)
|
2002-04-01 13:40:08 -04:00
|
|
|
{
|
|
|
|
if (s == NULL) {
|
|
|
|
return 0;
|
|
|
|
}
|
2003-02-01 02:24:36 -04:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
if (PycStringIO->cwrite((PyObject *)self->file, s, n) != n) {
|
|
|
|
return -1;
|
|
|
|
}
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2006-02-15 13:27:45 -04:00
|
|
|
return (int)n;
|
1997-01-06 18:59:08 -04:00
|
|
|
}
|
|
|
|
|
2001-03-17 00:50:51 -04:00
|
|
|
static int
|
2006-02-15 13:27:45 -04:00
|
|
|
write_none(Picklerobject *self, const char *s, Py_ssize_t n)
|
2002-04-01 13:40:08 -04:00
|
|
|
{
|
|
|
|
if (s == NULL) return 0;
|
2006-02-15 13:27:45 -04:00
|
|
|
if (n > INT_MAX) return -1;
|
|
|
|
return (int)n;
|
1997-08-13 00:14:41 -03:00
|
|
|
}
|
|
|
|
|
2001-03-17 00:50:51 -04:00
|
|
|
static int
|
2006-02-15 13:27:45 -04:00
|
|
|
write_other(Picklerobject *self, const char *s, Py_ssize_t _n)
|
2002-04-01 13:40:08 -04:00
|
|
|
{
|
|
|
|
PyObject *py_str = 0, *junk = 0;
|
2006-02-15 13:27:45 -04:00
|
|
|
int n;
|
2003-02-01 02:24:36 -04:00
|
|
|
|
2006-02-15 13:27:45 -04:00
|
|
|
if (_n > INT_MAX)
|
|
|
|
return -1;
|
|
|
|
n = (int)_n;
|
2002-04-01 13:40:08 -04:00
|
|
|
if (s == NULL) {
|
|
|
|
if (!( self->buf_size )) return 0;
|
2003-02-01 02:24:36 -04:00
|
|
|
py_str = PyString_FromStringAndSize(self->write_buf,
|
2002-04-01 13:40:08 -04:00
|
|
|
self->buf_size);
|
2003-02-01 02:24:36 -04:00
|
|
|
if (!py_str)
|
2002-04-01 13:40:08 -04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (self->buf_size && (n + self->buf_size) > WRITE_BUF_SIZE) {
|
|
|
|
if (write_other(self, NULL, 0) < 0)
|
|
|
|
return -1;
|
|
|
|
}
|
2003-02-01 02:24:36 -04:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
if (n > WRITE_BUF_SIZE) {
|
|
|
|
if (!( py_str =
|
2003-02-01 02:24:36 -04:00
|
|
|
PyString_FromStringAndSize(s, n)))
|
2002-04-01 13:40:08 -04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
memcpy(self->write_buf + self->buf_size, s, n);
|
|
|
|
self->buf_size += n;
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
}
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
if (self->write) {
|
|
|
|
/* object with write method */
|
|
|
|
ARG_TUP(self, py_str);
|
|
|
|
if (self->arg) {
|
|
|
|
junk = PyObject_Call(self->write, self->arg, NULL);
|
|
|
|
FREE_ARG_TUP(self);
|
|
|
|
}
|
|
|
|
if (junk) Py_DECREF(junk);
|
|
|
|
else return -1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
PDATA_PUSH(self->file, py_str, -1);
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
self->buf_size = 0;
|
|
|
|
return n;
|
|
|
|
}
|
1997-04-09 14:36:32 -03:00
|
|
|
|
|
|
|
|
2006-02-15 13:27:45 -04:00
|
|
|
static Py_ssize_t
|
|
|
|
read_file(Unpicklerobject *self, char **s, Py_ssize_t n)
|
2002-04-01 13:40:08 -04:00
|
|
|
{
|
|
|
|
size_t nbytesread;
|
1997-04-09 14:36:32 -03:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
if (self->buf_size == 0) {
|
|
|
|
int size;
|
2001-03-17 00:50:51 -04:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
size = ((n < 32) ? 32 : n);
|
cPickle.c: Full support for the new LONG1 and LONG4. Added comments.
Assorted code cleanups; e.g., sizeof(char) is 1 by definition, so there's
no need to do things like multiply by sizeof(char) in hairy malloc
arguments. Fixed an undetected-overflow bug in readline_file().
longobject.c: Fixed a really stupid bug in the new _PyLong_NumBits.
pickle.py: Fixed stupid bug in save_long(): When proto is 2, it
wrote LONG1 or LONG4, but forgot to return then -- it went on to
append the proto 1 LONG opcode too.
Fixed equally stupid cancelling bugs in load_long1() and
load_long4(): they *returned* the unpickled long instead of pushing
it on the stack. The return values were ignored. Tests passed
before only because save_long() pickled the long twice.
Fixed bugs in encode_long().
Noted that decode_long() is quadratic-time despite our hopes,
because long(string, 16) is still quadratic-time in len(string).
It's hex() that's linear-time. I don't know a way to make decode_long()
linear-time in Python, short of maybe transforming the 256's-complement
bytes into marshal's funky internal format, and letting marshal decode
that. It would be more valuable to make long(string, 16) linear time.
pickletester.py: Added a global "protocols" vector so tests can try
all the protocols in a sane way. Changed test_ints() and test_unicode()
to do so. Added a new test_long(), but the tail end of it is disabled
because it "takes forever" under pickle.py (but runs very quickly under
cPickle: cPickle proto 2 for longs is linear-time).
2003-02-01 22:57:53 -04:00
|
|
|
if (!( self->buf = (char *)malloc(size))) {
|
2002-04-01 13:40:08 -04:00
|
|
|
PyErr_NoMemory();
|
|
|
|
return -1;
|
|
|
|
}
|
2001-03-17 00:50:51 -04:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
self->buf_size = size;
|
|
|
|
}
|
|
|
|
else if (n > self->buf_size) {
|
cPickle.c: Full support for the new LONG1 and LONG4. Added comments.
Assorted code cleanups; e.g., sizeof(char) is 1 by definition, so there's
no need to do things like multiply by sizeof(char) in hairy malloc
arguments. Fixed an undetected-overflow bug in readline_file().
longobject.c: Fixed a really stupid bug in the new _PyLong_NumBits.
pickle.py: Fixed stupid bug in save_long(): When proto is 2, it
wrote LONG1 or LONG4, but forgot to return then -- it went on to
append the proto 1 LONG opcode too.
Fixed equally stupid cancelling bugs in load_long1() and
load_long4(): they *returned* the unpickled long instead of pushing
it on the stack. The return values were ignored. Tests passed
before only because save_long() pickled the long twice.
Fixed bugs in encode_long().
Noted that decode_long() is quadratic-time despite our hopes,
because long(string, 16) is still quadratic-time in len(string).
It's hex() that's linear-time. I don't know a way to make decode_long()
linear-time in Python, short of maybe transforming the 256's-complement
bytes into marshal's funky internal format, and letting marshal decode
that. It would be more valuable to make long(string, 16) linear time.
pickletester.py: Added a global "protocols" vector so tests can try
all the protocols in a sane way. Changed test_ints() and test_unicode()
to do so. Added a new test_long(), but the tail end of it is disabled
because it "takes forever" under pickle.py (but runs very quickly under
cPickle: cPickle proto 2 for longs is linear-time).
2003-02-01 22:57:53 -04:00
|
|
|
self->buf = (char *)realloc(self->buf, n);
|
2002-04-01 13:40:08 -04:00
|
|
|
if (!self->buf) {
|
|
|
|
PyErr_NoMemory();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
self->buf_size = n;
|
|
|
|
}
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
Py_BEGIN_ALLOW_THREADS
|
|
|
|
nbytesread = fread(self->buf, sizeof(char), n, self->fp);
|
|
|
|
Py_END_ALLOW_THREADS
|
|
|
|
if (nbytesread != (size_t)n) {
|
|
|
|
if (feof(self->fp)) {
|
|
|
|
PyErr_SetNone(PyExc_EOFError);
|
|
|
|
return -1;
|
|
|
|
}
|
2003-02-01 02:24:36 -04:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
PyErr_SetFromErrno(PyExc_IOError);
|
|
|
|
return -1;
|
|
|
|
}
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
*s = self->buf;
|
1997-04-09 14:36:32 -03:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
return n;
|
1997-01-06 18:59:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-02-15 13:27:45 -04:00
|
|
|
static Py_ssize_t
|
2003-02-01 02:24:36 -04:00
|
|
|
readline_file(Unpicklerobject *self, char **s)
|
2002-04-01 13:40:08 -04:00
|
|
|
{
|
|
|
|
int i;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
if (self->buf_size == 0) {
|
cPickle.c: Full support for the new LONG1 and LONG4. Added comments.
Assorted code cleanups; e.g., sizeof(char) is 1 by definition, so there's
no need to do things like multiply by sizeof(char) in hairy malloc
arguments. Fixed an undetected-overflow bug in readline_file().
longobject.c: Fixed a really stupid bug in the new _PyLong_NumBits.
pickle.py: Fixed stupid bug in save_long(): When proto is 2, it
wrote LONG1 or LONG4, but forgot to return then -- it went on to
append the proto 1 LONG opcode too.
Fixed equally stupid cancelling bugs in load_long1() and
load_long4(): they *returned* the unpickled long instead of pushing
it on the stack. The return values were ignored. Tests passed
before only because save_long() pickled the long twice.
Fixed bugs in encode_long().
Noted that decode_long() is quadratic-time despite our hopes,
because long(string, 16) is still quadratic-time in len(string).
It's hex() that's linear-time. I don't know a way to make decode_long()
linear-time in Python, short of maybe transforming the 256's-complement
bytes into marshal's funky internal format, and letting marshal decode
that. It would be more valuable to make long(string, 16) linear time.
pickletester.py: Added a global "protocols" vector so tests can try
all the protocols in a sane way. Changed test_ints() and test_unicode()
to do so. Added a new test_long(), but the tail end of it is disabled
because it "takes forever" under pickle.py (but runs very quickly under
cPickle: cPickle proto 2 for longs is linear-time).
2003-02-01 22:57:53 -04:00
|
|
|
if (!( self->buf = (char *)malloc(40))) {
|
2002-04-01 13:40:08 -04:00
|
|
|
PyErr_NoMemory();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
self->buf_size = 40;
|
|
|
|
}
|
1997-04-09 14:36:32 -03:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
i = 0;
|
|
|
|
while (1) {
|
cPickle.c: Full support for the new LONG1 and LONG4. Added comments.
Assorted code cleanups; e.g., sizeof(char) is 1 by definition, so there's
no need to do things like multiply by sizeof(char) in hairy malloc
arguments. Fixed an undetected-overflow bug in readline_file().
longobject.c: Fixed a really stupid bug in the new _PyLong_NumBits.
pickle.py: Fixed stupid bug in save_long(): When proto is 2, it
wrote LONG1 or LONG4, but forgot to return then -- it went on to
append the proto 1 LONG opcode too.
Fixed equally stupid cancelling bugs in load_long1() and
load_long4(): they *returned* the unpickled long instead of pushing
it on the stack. The return values were ignored. Tests passed
before only because save_long() pickled the long twice.
Fixed bugs in encode_long().
Noted that decode_long() is quadratic-time despite our hopes,
because long(string, 16) is still quadratic-time in len(string).
It's hex() that's linear-time. I don't know a way to make decode_long()
linear-time in Python, short of maybe transforming the 256's-complement
bytes into marshal's funky internal format, and letting marshal decode
that. It would be more valuable to make long(string, 16) linear time.
pickletester.py: Added a global "protocols" vector so tests can try
all the protocols in a sane way. Changed test_ints() and test_unicode()
to do so. Added a new test_long(), but the tail end of it is disabled
because it "takes forever" under pickle.py (but runs very quickly under
cPickle: cPickle proto 2 for longs is linear-time).
2003-02-01 22:57:53 -04:00
|
|
|
int bigger;
|
2002-04-01 13:40:08 -04:00
|
|
|
for (; i < (self->buf_size - 1); i++) {
|
2003-02-01 02:24:36 -04:00
|
|
|
if (feof(self->fp) ||
|
2002-04-01 13:40:08 -04:00
|
|
|
(self->buf[i] = getc(self->fp)) == '\n') {
|
|
|
|
self->buf[i + 1] = '\0';
|
|
|
|
*s = self->buf;
|
|
|
|
return i + 1;
|
|
|
|
}
|
|
|
|
}
|
cPickle.c: Full support for the new LONG1 and LONG4. Added comments.
Assorted code cleanups; e.g., sizeof(char) is 1 by definition, so there's
no need to do things like multiply by sizeof(char) in hairy malloc
arguments. Fixed an undetected-overflow bug in readline_file().
longobject.c: Fixed a really stupid bug in the new _PyLong_NumBits.
pickle.py: Fixed stupid bug in save_long(): When proto is 2, it
wrote LONG1 or LONG4, but forgot to return then -- it went on to
append the proto 1 LONG opcode too.
Fixed equally stupid cancelling bugs in load_long1() and
load_long4(): they *returned* the unpickled long instead of pushing
it on the stack. The return values were ignored. Tests passed
before only because save_long() pickled the long twice.
Fixed bugs in encode_long().
Noted that decode_long() is quadratic-time despite our hopes,
because long(string, 16) is still quadratic-time in len(string).
It's hex() that's linear-time. I don't know a way to make decode_long()
linear-time in Python, short of maybe transforming the 256's-complement
bytes into marshal's funky internal format, and letting marshal decode
that. It would be more valuable to make long(string, 16) linear time.
pickletester.py: Added a global "protocols" vector so tests can try
all the protocols in a sane way. Changed test_ints() and test_unicode()
to do so. Added a new test_long(), but the tail end of it is disabled
because it "takes forever" under pickle.py (but runs very quickly under
cPickle: cPickle proto 2 for longs is linear-time).
2003-02-01 22:57:53 -04:00
|
|
|
bigger = self->buf_size << 1;
|
|
|
|
if (bigger <= 0) { /* overflow */
|
|
|
|
PyErr_NoMemory();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
self->buf = (char *)realloc(self->buf, bigger);
|
2002-04-01 13:40:08 -04:00
|
|
|
if (!self->buf) {
|
|
|
|
PyErr_NoMemory();
|
|
|
|
return -1;
|
|
|
|
}
|
cPickle.c: Full support for the new LONG1 and LONG4. Added comments.
Assorted code cleanups; e.g., sizeof(char) is 1 by definition, so there's
no need to do things like multiply by sizeof(char) in hairy malloc
arguments. Fixed an undetected-overflow bug in readline_file().
longobject.c: Fixed a really stupid bug in the new _PyLong_NumBits.
pickle.py: Fixed stupid bug in save_long(): When proto is 2, it
wrote LONG1 or LONG4, but forgot to return then -- it went on to
append the proto 1 LONG opcode too.
Fixed equally stupid cancelling bugs in load_long1() and
load_long4(): they *returned* the unpickled long instead of pushing
it on the stack. The return values were ignored. Tests passed
before only because save_long() pickled the long twice.
Fixed bugs in encode_long().
Noted that decode_long() is quadratic-time despite our hopes,
because long(string, 16) is still quadratic-time in len(string).
It's hex() that's linear-time. I don't know a way to make decode_long()
linear-time in Python, short of maybe transforming the 256's-complement
bytes into marshal's funky internal format, and letting marshal decode
that. It would be more valuable to make long(string, 16) linear time.
pickletester.py: Added a global "protocols" vector so tests can try
all the protocols in a sane way. Changed test_ints() and test_unicode()
to do so. Added a new test_long(), but the tail end of it is disabled
because it "takes forever" under pickle.py (but runs very quickly under
cPickle: cPickle proto 2 for longs is linear-time).
2003-02-01 22:57:53 -04:00
|
|
|
self->buf_size = bigger;
|
2002-04-01 13:40:08 -04:00
|
|
|
}
|
|
|
|
}
|
1997-01-06 18:59:08 -04:00
|
|
|
|
|
|
|
|
2006-02-15 13:27:45 -04:00
|
|
|
static Py_ssize_t
|
|
|
|
read_cStringIO(Unpicklerobject *self, char **s, Py_ssize_t n)
|
2002-04-01 13:40:08 -04:00
|
|
|
{
|
|
|
|
char *ptr;
|
2003-02-01 02:24:36 -04:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
if (PycStringIO->cread((PyObject *)self->file, &ptr, n) != n) {
|
|
|
|
PyErr_SetNone(PyExc_EOFError);
|
|
|
|
return -1;
|
|
|
|
}
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
*s = ptr;
|
2003-02-01 02:24:36 -04:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
return n;
|
2001-03-17 00:50:51 -04:00
|
|
|
}
|
1997-01-06 18:59:08 -04:00
|
|
|
|
|
|
|
|
2006-02-15 13:27:45 -04:00
|
|
|
static Py_ssize_t
|
2003-02-01 02:24:36 -04:00
|
|
|
readline_cStringIO(Unpicklerobject *self, char **s)
|
2002-04-01 13:40:08 -04:00
|
|
|
{
|
2006-02-15 13:27:45 -04:00
|
|
|
Py_ssize_t n;
|
2002-04-01 13:40:08 -04:00
|
|
|
char *ptr;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
if ((n = PycStringIO->creadline((PyObject *)self->file, &ptr)) < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
1997-04-09 14:36:32 -03:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
*s = ptr;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
return n;
|
1997-01-06 18:59:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-02-15 13:27:45 -04:00
|
|
|
static Py_ssize_t
|
|
|
|
read_other(Unpicklerobject *self, char **s, Py_ssize_t n)
|
2002-04-01 13:40:08 -04:00
|
|
|
{
|
|
|
|
PyObject *bytes, *str=0;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2006-02-15 13:27:45 -04:00
|
|
|
if (!( bytes = PyInt_FromSsize_t(n))) return -1;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
ARG_TUP(self, bytes);
|
|
|
|
if (self->arg) {
|
|
|
|
str = PyObject_Call(self->read, self->arg, NULL);
|
|
|
|
FREE_ARG_TUP(self);
|
|
|
|
}
|
|
|
|
if (! str) return -1;
|
2003-02-01 02:24:36 -04:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
Py_XDECREF(self->last_string);
|
|
|
|
self->last_string = str;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
if (! (*s = PyString_AsString(str))) return -1;
|
|
|
|
return n;
|
1997-01-06 18:59:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-02-15 13:27:45 -04:00
|
|
|
static Py_ssize_t
|
2003-02-01 02:24:36 -04:00
|
|
|
readline_other(Unpicklerobject *self, char **s)
|
2002-04-01 13:40:08 -04:00
|
|
|
{
|
|
|
|
PyObject *str;
|
2006-02-15 13:27:45 -04:00
|
|
|
Py_ssize_t str_size;
|
2003-02-01 02:24:36 -04:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
if (!( str = PyObject_CallObject(self->readline, empty_tuple))) {
|
|
|
|
return -1;
|
|
|
|
}
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
if ((str_size = PyString_Size(str)) < 0)
|
|
|
|
return -1;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
Py_XDECREF(self->last_string);
|
|
|
|
self->last_string = str;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
if (! (*s = PyString_AsString(str)))
|
|
|
|
return -1;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
return str_size;
|
1997-01-06 18:59:08 -04:00
|
|
|
}
|
|
|
|
|
cPickle.c: Full support for the new LONG1 and LONG4. Added comments.
Assorted code cleanups; e.g., sizeof(char) is 1 by definition, so there's
no need to do things like multiply by sizeof(char) in hairy malloc
arguments. Fixed an undetected-overflow bug in readline_file().
longobject.c: Fixed a really stupid bug in the new _PyLong_NumBits.
pickle.py: Fixed stupid bug in save_long(): When proto is 2, it
wrote LONG1 or LONG4, but forgot to return then -- it went on to
append the proto 1 LONG opcode too.
Fixed equally stupid cancelling bugs in load_long1() and
load_long4(): they *returned* the unpickled long instead of pushing
it on the stack. The return values were ignored. Tests passed
before only because save_long() pickled the long twice.
Fixed bugs in encode_long().
Noted that decode_long() is quadratic-time despite our hopes,
because long(string, 16) is still quadratic-time in len(string).
It's hex() that's linear-time. I don't know a way to make decode_long()
linear-time in Python, short of maybe transforming the 256's-complement
bytes into marshal's funky internal format, and letting marshal decode
that. It would be more valuable to make long(string, 16) linear time.
pickletester.py: Added a global "protocols" vector so tests can try
all the protocols in a sane way. Changed test_ints() and test_unicode()
to do so. Added a new test_long(), but the tail end of it is disabled
because it "takes forever" under pickle.py (but runs very quickly under
cPickle: cPickle proto 2 for longs is linear-time).
2003-02-01 22:57:53 -04:00
|
|
|
/* Copy the first n bytes from s into newly malloc'ed memory, plus a
|
|
|
|
* trailing 0 byte. Return a pointer to that, or NULL if out of memory.
|
|
|
|
* The caller is responsible for free()'ing the return value.
|
|
|
|
*/
|
2002-04-01 13:40:08 -04:00
|
|
|
static char *
|
2005-12-10 14:50:16 -04:00
|
|
|
pystrndup(const char *s, int n)
|
2002-04-01 13:40:08 -04:00
|
|
|
{
|
cPickle.c: Full support for the new LONG1 and LONG4. Added comments.
Assorted code cleanups; e.g., sizeof(char) is 1 by definition, so there's
no need to do things like multiply by sizeof(char) in hairy malloc
arguments. Fixed an undetected-overflow bug in readline_file().
longobject.c: Fixed a really stupid bug in the new _PyLong_NumBits.
pickle.py: Fixed stupid bug in save_long(): When proto is 2, it
wrote LONG1 or LONG4, but forgot to return then -- it went on to
append the proto 1 LONG opcode too.
Fixed equally stupid cancelling bugs in load_long1() and
load_long4(): they *returned* the unpickled long instead of pushing
it on the stack. The return values were ignored. Tests passed
before only because save_long() pickled the long twice.
Fixed bugs in encode_long().
Noted that decode_long() is quadratic-time despite our hopes,
because long(string, 16) is still quadratic-time in len(string).
It's hex() that's linear-time. I don't know a way to make decode_long()
linear-time in Python, short of maybe transforming the 256's-complement
bytes into marshal's funky internal format, and letting marshal decode
that. It would be more valuable to make long(string, 16) linear time.
pickletester.py: Added a global "protocols" vector so tests can try
all the protocols in a sane way. Changed test_ints() and test_unicode()
to do so. Added a new test_long(), but the tail end of it is disabled
because it "takes forever" under pickle.py (but runs very quickly under
cPickle: cPickle proto 2 for longs is linear-time).
2003-02-01 22:57:53 -04:00
|
|
|
char *r = (char *)malloc(n+1);
|
|
|
|
if (r == NULL)
|
|
|
|
return (char*)PyErr_NoMemory();
|
|
|
|
memcpy(r, s, n);
|
|
|
|
r[n] = 0;
|
2002-04-01 13:40:08 -04:00
|
|
|
return r;
|
|
|
|
}
|
1997-01-06 18:59:08 -04:00
|
|
|
|
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
static int
|
2003-02-01 02:24:36 -04:00
|
|
|
get(Picklerobject *self, PyObject *id)
|
2002-04-01 13:40:08 -04:00
|
|
|
{
|
|
|
|
PyObject *value, *mv;
|
|
|
|
long c_value;
|
|
|
|
char s[30];
|
|
|
|
size_t len;
|
|
|
|
|
|
|
|
if (!( mv = PyDict_GetItem(self->memo, id))) {
|
|
|
|
PyErr_SetObject(PyExc_KeyError, id);
|
|
|
|
return -1;
|
|
|
|
}
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2003-02-01 02:24:36 -04:00
|
|
|
if (!( value = PyTuple_GetItem(mv, 0)))
|
2002-04-01 13:40:08 -04:00
|
|
|
return -1;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
if (!( 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
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
if (!self->bin) {
|
|
|
|
s[0] = GET;
|
|
|
|
PyOS_snprintf(s + 1, sizeof(s) - 1, "%ld\n", c_value);
|
|
|
|
len = strlen(s);
|
|
|
|
}
|
|
|
|
else if (Pdata_Check(self->file)) {
|
|
|
|
if (write_other(self, NULL, 0) < 0) return -1;
|
|
|
|
PDATA_APPEND(self->file, mv, -1);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
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
|
|
|
|
2003-02-02 14:29:33 -04:00
|
|
|
if (self->write_func(self, s, len) < 0)
|
2002-04-01 13:40:08 -04:00
|
|
|
return -1;
|
2003-02-01 02:24:36 -04:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
return 0;
|
1997-01-06 18:59:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
2003-02-01 02:24:36 -04:00
|
|
|
put(Picklerobject *self, PyObject *ob)
|
2002-04-01 13:40:08 -04:00
|
|
|
{
|
2003-02-01 02:24:36 -04:00
|
|
|
if (ob->ob_refcnt < 2 || self->fast)
|
2002-04-01 13:40:08 -04:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
return put2(self, ob);
|
1997-04-09 14:36:32 -03:00
|
|
|
}
|
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
|
2003-02-01 02:24:36 -04:00
|
|
|
put2(Picklerobject *self, PyObject *ob)
|
2002-04-01 13:40:08 -04:00
|
|
|
{
|
|
|
|
char c_str[30];
|
|
|
|
int p;
|
|
|
|
size_t len;
|
|
|
|
int res = -1;
|
|
|
|
PyObject *py_ob_id = 0, *memo_len = 0, *t = 0;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
if (self->fast)
|
|
|
|
return 0;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
if ((p = PyDict_Size(self->memo)) < 0)
|
|
|
|
goto finally;
|
2001-03-17 00:50:51 -04:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
/* Make sure memo keys are positive! */
|
2003-02-02 13:26:40 -04:00
|
|
|
/* XXX Why?
|
|
|
|
* XXX And does "positive" really mean non-negative?
|
|
|
|
* XXX pickle.py starts with PUT index 0, not 1. This makes for
|
|
|
|
* XXX gratuitous differences between the pickling modules.
|
|
|
|
*/
|
2003-02-01 02:24:36 -04:00
|
|
|
p++;
|
2002-04-01 13:40:08 -04:00
|
|
|
|
2003-02-01 02:24:36 -04:00
|
|
|
if (!( py_ob_id = PyLong_FromVoidPtr(ob)))
|
2002-04-01 13:40:08 -04:00
|
|
|
goto finally;
|
|
|
|
|
2003-02-01 02:24:36 -04:00
|
|
|
if (!( memo_len = PyInt_FromLong(p)))
|
2002-04-01 13:40:08 -04:00
|
|
|
goto finally;
|
|
|
|
|
2003-02-01 02:24:36 -04:00
|
|
|
if (!( t = PyTuple_New(2)))
|
2002-04-01 13:40:08 -04:00
|
|
|
goto finally;
|
|
|
|
|
|
|
|
PyTuple_SET_ITEM(t, 0, memo_len);
|
|
|
|
Py_INCREF(memo_len);
|
|
|
|
PyTuple_SET_ITEM(t, 1, ob);
|
|
|
|
Py_INCREF(ob);
|
|
|
|
|
|
|
|
if (PyDict_SetItem(self->memo, py_ob_id, t) < 0)
|
|
|
|
goto finally;
|
|
|
|
|
|
|
|
if (!self->bin) {
|
|
|
|
c_str[0] = PUT;
|
|
|
|
PyOS_snprintf(c_str + 1, sizeof(c_str) - 1, "%d\n", p);
|
|
|
|
len = strlen(c_str);
|
|
|
|
}
|
|
|
|
else if (Pdata_Check(self->file)) {
|
|
|
|
if (write_other(self, NULL, 0) < 0) return -1;
|
|
|
|
PDATA_APPEND(self->file, memo_len, -1);
|
|
|
|
res=0; /* Job well done ;) */
|
|
|
|
goto finally;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
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;
|
|
|
|
len = 2;
|
|
|
|
}
|
|
|
|
}
|
1998-08-13 20:13:52 -03:00
|
|
|
|
2003-02-02 14:29:33 -04:00
|
|
|
if (self->write_func(self, c_str, len) < 0)
|
2002-04-01 13:40:08 -04:00
|
|
|
goto finally;
|
|
|
|
|
|
|
|
res = 0;
|
|
|
|
|
|
|
|
finally:
|
|
|
|
Py_XDECREF(py_ob_id);
|
|
|
|
Py_XDECREF(memo_len);
|
|
|
|
Py_XDECREF(t);
|
|
|
|
|
|
|
|
return res;
|
1997-01-06 18:59:08 -04:00
|
|
|
}
|
|
|
|
|
1997-12-03 21:13:01 -04:00
|
|
|
static PyObject *
|
2003-02-01 02:24:36 -04:00
|
|
|
whichmodule(PyObject *global, PyObject *global_name)
|
2002-04-01 13:40:08 -04:00
|
|
|
{
|
2006-02-15 13:27:45 -04:00
|
|
|
Py_ssize_t i, j;
|
2002-04-01 13:40:08 -04:00
|
|
|
PyObject *module = 0, *modules_dict = 0,
|
|
|
|
*global_name_attr = 0, *name = 0;
|
1997-04-09 14:36:32 -03:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
module = PyObject_GetAttrString(global, "__module__");
|
2003-06-16 17:19:49 -03:00
|
|
|
if (module)
|
|
|
|
return module;
|
|
|
|
if (PyErr_ExceptionMatches(PyExc_AttributeError))
|
|
|
|
PyErr_Clear();
|
|
|
|
else
|
|
|
|
return NULL;
|
1997-09-28 02:38:51 -03:00
|
|
|
|
2003-02-01 02:24:36 -04:00
|
|
|
if (!( modules_dict = PySys_GetObject("modules")))
|
2002-04-01 13:40:08 -04:00
|
|
|
return NULL;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
i = 0;
|
|
|
|
while ((j = PyDict_Next(modules_dict, &i, &name, &module))) {
|
1997-08-13 00:14:41 -03:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
if (PyObject_Compare(name, __main___str)==0) continue;
|
2001-03-17 00:50:51 -04:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
global_name_attr = PyObject_GetAttr(module, global_name);
|
|
|
|
if (!global_name_attr) {
|
2003-06-16 17:19:49 -03:00
|
|
|
if (PyErr_ExceptionMatches(PyExc_AttributeError))
|
|
|
|
PyErr_Clear();
|
|
|
|
else
|
|
|
|
return NULL;
|
2002-04-01 13:40:08 -04:00
|
|
|
continue;
|
|
|
|
}
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
if (global_name_attr != global) {
|
|
|
|
Py_DECREF(global_name_attr);
|
|
|
|
continue;
|
|
|
|
}
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
Py_DECREF(global_name_attr);
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
break;
|
|
|
|
}
|
1997-08-13 00:14:41 -03:00
|
|
|
|
2002-04-01 13:40:08 -04: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
|
|
|
|
*/
|
|
|
|
if (!j) {
|
|
|
|
j=1;
|
|
|
|
name=__main___str;
|
|
|
|
}
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
Py_INCREF(name);
|
|
|
|
return name;
|
1997-04-09 14:36:32 -03:00
|
|
|
}
|
1997-01-06 18:59:08 -04:00
|
|
|
|
|
|
|
|
2001-10-15 18:37:58 -03:00
|
|
|
static int
|
|
|
|
fast_save_enter(Picklerobject *self, PyObject *obj)
|
|
|
|
{
|
2002-04-01 13:40:08 -04:00
|
|
|
/* if fast_container < 0, we're doing an error exit. */
|
|
|
|
if (++self->fast_container >= PY_CPICKLE_FAST_LIMIT) {
|
|
|
|
PyObject *key = NULL;
|
|
|
|
if (self->fast_memo == NULL) {
|
|
|
|
self->fast_memo = PyDict_New();
|
|
|
|
if (self->fast_memo == NULL) {
|
|
|
|
self->fast_container = -1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
key = PyLong_FromVoidPtr(obj);
|
|
|
|
if (key == NULL)
|
|
|
|
return 0;
|
|
|
|
if (PyDict_GetItem(self->fast_memo, key)) {
|
2002-12-06 22:43:28 -04:00
|
|
|
Py_DECREF(key);
|
2002-04-01 13:40:08 -04:00
|
|
|
PyErr_Format(PyExc_ValueError,
|
2003-02-13 19:00:26 -04:00
|
|
|
"fast mode: can't pickle cyclic objects "
|
|
|
|
"including object type %s at %p",
|
2002-04-01 13:40:08 -04:00
|
|
|
obj->ob_type->tp_name, obj);
|
|
|
|
self->fast_container = -1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (PyDict_SetItem(self->fast_memo, key, Py_None) < 0) {
|
2002-12-06 22:43:28 -04:00
|
|
|
Py_DECREF(key);
|
2002-04-01 13:40:08 -04:00
|
|
|
self->fast_container = -1;
|
|
|
|
return 0;
|
|
|
|
}
|
2002-12-06 22:43:28 -04:00
|
|
|
Py_DECREF(key);
|
2001-10-15 18:37:58 -03:00
|
|
|
}
|
2002-04-01 13:40:08 -04:00
|
|
|
return 1;
|
2001-10-15 18:37:58 -03:00
|
|
|
}
|
|
|
|
|
2003-02-01 02:24:36 -04:00
|
|
|
int
|
2001-10-15 18:37:58 -03:00
|
|
|
fast_save_leave(Picklerobject *self, PyObject *obj)
|
|
|
|
{
|
2002-04-01 13:40:08 -04:00
|
|
|
if (self->fast_container-- >= PY_CPICKLE_FAST_LIMIT) {
|
|
|
|
PyObject *key = PyLong_FromVoidPtr(obj);
|
|
|
|
if (key == NULL)
|
|
|
|
return 0;
|
|
|
|
if (PyDict_DelItem(self->fast_memo, key) < 0) {
|
2002-12-06 22:43:28 -04:00
|
|
|
Py_DECREF(key);
|
2002-04-01 13:40:08 -04:00
|
|
|
return 0;
|
|
|
|
}
|
2002-12-06 22:43:28 -04:00
|
|
|
Py_DECREF(key);
|
2001-10-15 18:37:58 -03:00
|
|
|
}
|
2002-04-01 13:40:08 -04:00
|
|
|
return 1;
|
2001-10-15 18:37:58 -03:00
|
|
|
}
|
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
static int
|
2003-02-01 02:24:36 -04:00
|
|
|
save_none(Picklerobject *self, PyObject *args)
|
2002-04-01 13:40:08 -04:00
|
|
|
{
|
|
|
|
static char none = NONE;
|
2003-02-02 14:29:33 -04:00
|
|
|
if (self->write_func(self, &none, 1) < 0)
|
2002-04-01 13:40:08 -04:00
|
|
|
return -1;
|
1997-04-09 14:36:32 -03:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
return 0;
|
1997-04-09 14:36:32 -03:00
|
|
|
}
|
|
|
|
|
2002-04-03 18:41:51 -04:00
|
|
|
static int
|
2003-02-01 02:24:36 -04:00
|
|
|
save_bool(Picklerobject *self, PyObject *args)
|
2002-04-03 18:41:51 -04:00
|
|
|
{
|
2005-12-10 14:50:16 -04:00
|
|
|
static const char *buf[2] = {FALSE, TRUE};
|
2002-04-05 15:30:08 -04:00
|
|
|
static char len[2] = {sizeof(FALSE)-1, sizeof(TRUE)-1};
|
2002-04-03 18:41:51 -04:00
|
|
|
long l = PyInt_AS_LONG((PyIntObject *)args);
|
|
|
|
|
2003-02-02 13:59:11 -04:00
|
|
|
if (self->proto >= 2) {
|
|
|
|
char opcode = l ? NEWTRUE : NEWFALSE;
|
|
|
|
if (self->write_func(self, &opcode, 1) < 0)
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
else if (self->write_func(self, buf[l], len[l]) < 0)
|
2002-04-03 18:41:51 -04:00
|
|
|
return -1;
|
|
|
|
return 0;
|
|
|
|
}
|
2001-03-17 00:50:51 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
static int
|
2003-02-01 02:24:36 -04:00
|
|
|
save_int(Picklerobject *self, PyObject *args)
|
2002-04-01 13:40:08 -04:00
|
|
|
{
|
|
|
|
char c_str[32];
|
|
|
|
long l = PyInt_AS_LONG((PyIntObject *)args);
|
|
|
|
int len = 0;
|
1997-04-09 14:36:32 -03:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
if (!self->bin
|
1997-05-13 15:00:44 -03:00
|
|
|
#if SIZEOF_LONG > 4
|
2002-04-01 13:40:08 -04:00
|
|
|
|| l > 0x7fffffffL
|
|
|
|
|| l < -0x80000000L
|
1997-05-13 15:00:44 -03:00
|
|
|
#endif
|
2002-04-01 13:40:08 -04:00
|
|
|
) {
|
|
|
|
/* Text-mode pickle, or long too big to fit in the 4-byte
|
|
|
|
* signed BININT format: store as a string.
|
|
|
|
*/
|
|
|
|
c_str[0] = INT;
|
|
|
|
PyOS_snprintf(c_str + 1, sizeof(c_str) - 1, "%ld\n", l);
|
2003-02-02 14:29:33 -04:00
|
|
|
if (self->write_func(self, c_str, strlen(c_str)) < 0)
|
2002-04-01 13:40:08 -04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* Binary pickle and l fits in a signed 4-byte int. */
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2003-02-02 14:29:33 -04:00
|
|
|
if (self->write_func(self, c_str, len) < 0)
|
2002-04-01 13:40:08 -04:00
|
|
|
return -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
|
2003-02-01 02:24:36 -04:00
|
|
|
save_long(Picklerobject *self, PyObject *args)
|
2002-04-01 13:40:08 -04:00
|
|
|
{
|
cPickle.c: Full support for the new LONG1 and LONG4. Added comments.
Assorted code cleanups; e.g., sizeof(char) is 1 by definition, so there's
no need to do things like multiply by sizeof(char) in hairy malloc
arguments. Fixed an undetected-overflow bug in readline_file().
longobject.c: Fixed a really stupid bug in the new _PyLong_NumBits.
pickle.py: Fixed stupid bug in save_long(): When proto is 2, it
wrote LONG1 or LONG4, but forgot to return then -- it went on to
append the proto 1 LONG opcode too.
Fixed equally stupid cancelling bugs in load_long1() and
load_long4(): they *returned* the unpickled long instead of pushing
it on the stack. The return values were ignored. Tests passed
before only because save_long() pickled the long twice.
Fixed bugs in encode_long().
Noted that decode_long() is quadratic-time despite our hopes,
because long(string, 16) is still quadratic-time in len(string).
It's hex() that's linear-time. I don't know a way to make decode_long()
linear-time in Python, short of maybe transforming the 256's-complement
bytes into marshal's funky internal format, and letting marshal decode
that. It would be more valuable to make long(string, 16) linear time.
pickletester.py: Added a global "protocols" vector so tests can try
all the protocols in a sane way. Changed test_ints() and test_unicode()
to do so. Added a new test_long(), but the tail end of it is disabled
because it "takes forever" under pickle.py (but runs very quickly under
cPickle: cPickle proto 2 for longs is linear-time).
2003-02-01 22:57:53 -04:00
|
|
|
int size;
|
|
|
|
int res = -1;
|
|
|
|
PyObject *repr = NULL;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
static char l = LONG;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
cPickle.c: Full support for the new LONG1 and LONG4. Added comments.
Assorted code cleanups; e.g., sizeof(char) is 1 by definition, so there's
no need to do things like multiply by sizeof(char) in hairy malloc
arguments. Fixed an undetected-overflow bug in readline_file().
longobject.c: Fixed a really stupid bug in the new _PyLong_NumBits.
pickle.py: Fixed stupid bug in save_long(): When proto is 2, it
wrote LONG1 or LONG4, but forgot to return then -- it went on to
append the proto 1 LONG opcode too.
Fixed equally stupid cancelling bugs in load_long1() and
load_long4(): they *returned* the unpickled long instead of pushing
it on the stack. The return values were ignored. Tests passed
before only because save_long() pickled the long twice.
Fixed bugs in encode_long().
Noted that decode_long() is quadratic-time despite our hopes,
because long(string, 16) is still quadratic-time in len(string).
It's hex() that's linear-time. I don't know a way to make decode_long()
linear-time in Python, short of maybe transforming the 256's-complement
bytes into marshal's funky internal format, and letting marshal decode
that. It would be more valuable to make long(string, 16) linear time.
pickletester.py: Added a global "protocols" vector so tests can try
all the protocols in a sane way. Changed test_ints() and test_unicode()
to do so. Added a new test_long(), but the tail end of it is disabled
because it "takes forever" under pickle.py (but runs very quickly under
cPickle: cPickle proto 2 for longs is linear-time).
2003-02-01 22:57:53 -04:00
|
|
|
if (self->proto >= 2) {
|
|
|
|
/* Linear-time pickling. */
|
|
|
|
size_t nbits;
|
|
|
|
size_t nbytes;
|
|
|
|
unsigned char *pdata;
|
|
|
|
char c_str[5];
|
|
|
|
int i;
|
|
|
|
int sign = _PyLong_Sign(args);
|
|
|
|
|
|
|
|
if (sign == 0) {
|
|
|
|
/* It's 0 -- an empty bytestring. */
|
|
|
|
c_str[0] = LONG1;
|
|
|
|
c_str[1] = 0;
|
|
|
|
i = self->write_func(self, c_str, 2);
|
|
|
|
if (i < 0) goto finally;
|
|
|
|
res = 0;
|
|
|
|
goto finally;
|
|
|
|
}
|
|
|
|
nbits = _PyLong_NumBits(args);
|
|
|
|
if (nbits == (size_t)-1 && PyErr_Occurred())
|
|
|
|
goto finally;
|
|
|
|
/* How many bytes do we need? There are nbits >> 3 full
|
|
|
|
* bytes of data, and nbits & 7 leftover bits. If there
|
|
|
|
* are any leftover bits, then we clearly need another
|
|
|
|
* byte. Wnat's not so obvious is that we *probably*
|
|
|
|
* need another byte even if there aren't any leftovers:
|
|
|
|
* the most-significant bit of the most-significant byte
|
|
|
|
* acts like a sign bit, and it's usually got a sense
|
|
|
|
* opposite of the one we need. The exception is longs
|
|
|
|
* of the form -(2**(8*j-1)) for j > 0. Such a long is
|
|
|
|
* its own 256's-complement, so has the right sign bit
|
|
|
|
* even without the extra byte. That's a pain to check
|
|
|
|
* for in advance, though, so we always grab an extra
|
|
|
|
* byte at the start, and cut it back later if possible.
|
|
|
|
*/
|
|
|
|
nbytes = (nbits >> 3) + 1;
|
|
|
|
if ((int)nbytes < 0 || (size_t)(int)nbytes != nbytes) {
|
|
|
|
PyErr_SetString(PyExc_OverflowError, "long too large "
|
|
|
|
"to pickle");
|
|
|
|
goto finally;
|
|
|
|
}
|
|
|
|
repr = PyString_FromStringAndSize(NULL, (int)nbytes);
|
|
|
|
if (repr == NULL) goto finally;
|
|
|
|
pdata = (unsigned char *)PyString_AS_STRING(repr);
|
|
|
|
i = _PyLong_AsByteArray((PyLongObject *)args,
|
|
|
|
pdata, nbytes,
|
|
|
|
1 /* little endian */, 1 /* signed */);
|
|
|
|
if (i < 0) goto finally;
|
|
|
|
/* If the long is negative, this may be a byte more than
|
|
|
|
* needed. This is so iff the MSB is all redundant sign
|
|
|
|
* bits.
|
|
|
|
*/
|
|
|
|
if (sign < 0 && nbytes > 1 && pdata[nbytes - 1] == 0xff &&
|
|
|
|
(pdata[nbytes - 2] & 0x80) != 0)
|
|
|
|
--nbytes;
|
|
|
|
|
|
|
|
if (nbytes < 256) {
|
|
|
|
c_str[0] = LONG1;
|
|
|
|
c_str[1] = (char)nbytes;
|
|
|
|
size = 2;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
c_str[0] = LONG4;
|
|
|
|
size = (int)nbytes;
|
|
|
|
for (i = 1; i < 5; i++) {
|
|
|
|
c_str[i] = (char)(size & 0xff);
|
|
|
|
size >>= 8;
|
|
|
|
}
|
|
|
|
size = 5;
|
|
|
|
}
|
|
|
|
i = self->write_func(self, c_str, size);
|
|
|
|
if (i < 0) goto finally;
|
|
|
|
i = self->write_func(self, (char *)pdata, (int)nbytes);
|
|
|
|
if (i < 0) goto finally;
|
|
|
|
res = 0;
|
|
|
|
goto finally;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* proto < 2: write the repr and newline. This is quadratic-time
|
|
|
|
* (in the number of digits), in both directions.
|
|
|
|
*/
|
2003-02-01 02:24:36 -04:00
|
|
|
if (!( repr = PyObject_Repr(args)))
|
2002-04-01 13:40:08 -04:00
|
|
|
goto finally;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
if ((size = PyString_Size(repr)) < 0)
|
|
|
|
goto finally;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2003-02-02 14:29:33 -04:00
|
|
|
if (self->write_func(self, &l, 1) < 0)
|
2002-04-01 13:40:08 -04:00
|
|
|
goto finally;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2003-02-02 14:29:33 -04:00
|
|
|
if (self->write_func(self,
|
|
|
|
PyString_AS_STRING((PyStringObject *)repr),
|
|
|
|
size) < 0)
|
2002-04-01 13:40:08 -04:00
|
|
|
goto finally;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2003-02-02 14:29:33 -04:00
|
|
|
if (self->write_func(self, "\n", 1) < 0)
|
2002-04-01 13:40:08 -04:00
|
|
|
goto finally;
|
1997-04-09 14:36:32 -03:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
res = 0;
|
1997-04-09 14:36:32 -03:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
finally:
|
|
|
|
Py_XDECREF(repr);
|
|
|
|
return res;
|
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
|
2003-02-01 02:24:36 -04:00
|
|
|
save_float(Picklerobject *self, PyObject *args)
|
2002-04-01 13:40:08 -04:00
|
|
|
{
|
|
|
|
double x = PyFloat_AS_DOUBLE((PyFloatObject *)args);
|
|
|
|
|
|
|
|
if (self->bin) {
|
|
|
|
char str[9];
|
2003-03-20 16:53:32 -04:00
|
|
|
str[0] = BINFLOAT;
|
|
|
|
if (_PyFloat_Pack8(x, (unsigned char *)&str[1], 0) < 0)
|
2002-04-01 13:40:08 -04:00
|
|
|
return -1;
|
2003-02-02 14:29:33 -04:00
|
|
|
if (self->write_func(self, str, 9) < 0)
|
2002-04-01 13:40:08 -04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
char c_str[250];
|
|
|
|
c_str[0] = FLOAT;
|
2006-04-30 08:13:56 -03:00
|
|
|
PyOS_ascii_formatd(c_str + 1, sizeof(c_str) - 2, "%.17g", x);
|
|
|
|
/* Extend the formatted string with a newline character */
|
|
|
|
strcat(c_str, "\n");
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2003-02-02 14:29:33 -04:00
|
|
|
if (self->write_func(self, c_str, strlen(c_str)) < 0)
|
2002-04-01 13:40:08 -04:00
|
|
|
return -1;
|
|
|
|
}
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
return 0;
|
|
|
|
}
|
1997-01-06 18:59:08 -04:00
|
|
|
|
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
static int
|
2003-02-01 02:24:36 -04:00
|
|
|
save_string(Picklerobject *self, PyObject *args, int doput)
|
2002-04-01 13:40:08 -04:00
|
|
|
{
|
|
|
|
int size, len;
|
|
|
|
PyObject *repr=0;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
if ((size = PyString_Size(args)) < 0)
|
|
|
|
return -1;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
if (!self->bin) {
|
|
|
|
char *repr_str;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
static char string = STRING;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2003-02-01 02:24:36 -04:00
|
|
|
if (!( repr = PyObject_Repr(args)))
|
2002-04-01 13:40:08 -04:00
|
|
|
return -1;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
if ((len = PyString_Size(repr)) < 0)
|
|
|
|
goto err;
|
|
|
|
repr_str = PyString_AS_STRING((PyStringObject *)repr);
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2003-02-02 14:29:33 -04:00
|
|
|
if (self->write_func(self, &string, 1) < 0)
|
2002-04-01 13:40:08 -04:00
|
|
|
goto err;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2003-02-02 14:29:33 -04:00
|
|
|
if (self->write_func(self, repr_str, len) < 0)
|
2002-04-01 13:40:08 -04:00
|
|
|
goto err;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2003-02-02 14:29:33 -04:00
|
|
|
if (self->write_func(self, "\n", 1) < 0)
|
2002-04-01 13:40:08 -04:00
|
|
|
goto err;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
Py_XDECREF(repr);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
int i;
|
|
|
|
char c_str[5];
|
|
|
|
|
|
|
|
if ((size = PyString_Size(args)) < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2003-02-02 14:29:33 -04:00
|
|
|
if (self->write_func(self, c_str, len) < 0)
|
2002-04-01 13:40:08 -04:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (size > 128 && Pdata_Check(self->file)) {
|
|
|
|
if (write_other(self, NULL, 0) < 0) return -1;
|
|
|
|
PDATA_APPEND(self->file, args, -1);
|
|
|
|
}
|
|
|
|
else {
|
2003-02-02 14:29:33 -04:00
|
|
|
if (self->write_func(self,
|
|
|
|
PyString_AS_STRING(
|
|
|
|
(PyStringObject *)args),
|
|
|
|
size) < 0)
|
2002-04-01 13:40:08 -04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
if (doput)
|
|
|
|
if (put(self, args) < 0)
|
|
|
|
return -1;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
return 0;
|
1998-11-25 12:18:00 -04:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
err:
|
|
|
|
Py_XDECREF(repr);
|
|
|
|
return -1;
|
1997-01-06 18:59:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-08-17 15:39:25 -03:00
|
|
|
#ifdef Py_USING_UNICODE
|
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)
|
|
|
|
{
|
2002-04-01 13:40:08 -04:00
|
|
|
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';
|
2002-04-27 15:44:32 -03:00
|
|
|
_PyString_Resize(&repr, p - q);
|
2000-12-18 22:08:38 -04:00
|
|
|
return repr;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-03-10 19:11:40 -04:00
|
|
|
static int
|
2003-02-01 02:24:36 -04:00
|
|
|
save_unicode(Picklerobject *self, PyObject *args, int doput)
|
2002-04-01 13:40:08 -04:00
|
|
|
{
|
|
|
|
int size, len;
|
|
|
|
PyObject *repr=0;
|
2000-03-10 19:11:40 -04:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
if (!PyUnicode_Check(args))
|
|
|
|
return -1;
|
2000-03-10 19:11:40 -04:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
if (!self->bin) {
|
|
|
|
char *repr_str;
|
|
|
|
static char string = UNICODE;
|
2000-03-10 19:11:40 -04:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
repr = modified_EncodeRawUnicodeEscape(
|
|
|
|
PyUnicode_AS_UNICODE(args), PyUnicode_GET_SIZE(args));
|
2003-02-01 02:24:36 -04:00
|
|
|
if (!repr)
|
2002-04-01 13:40:08 -04:00
|
|
|
return -1;
|
2000-03-10 19:11:40 -04:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
if ((len = PyString_Size(repr)) < 0)
|
|
|
|
goto err;
|
|
|
|
repr_str = PyString_AS_STRING((PyStringObject *)repr);
|
2000-03-10 19:11:40 -04:00
|
|
|
|
2003-02-02 14:29:33 -04:00
|
|
|
if (self->write_func(self, &string, 1) < 0)
|
2002-04-01 13:40:08 -04:00
|
|
|
goto err;
|
2000-03-10 19:11:40 -04:00
|
|
|
|
2003-02-02 14:29:33 -04:00
|
|
|
if (self->write_func(self, repr_str, len) < 0)
|
2002-04-01 13:40:08 -04:00
|
|
|
goto err;
|
2000-03-10 19:11:40 -04:00
|
|
|
|
2003-02-02 14:29:33 -04:00
|
|
|
if (self->write_func(self, "\n", 1) < 0)
|
2002-04-01 13:40:08 -04:00
|
|
|
goto err;
|
2000-03-10 19:11:40 -04:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
Py_XDECREF(repr);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
int i;
|
|
|
|
char c_str[5];
|
|
|
|
|
2003-02-01 02:24:36 -04:00
|
|
|
if (!( repr = PyUnicode_AsUTF8String(args)))
|
2002-04-01 13:40:08 -04:00
|
|
|
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;
|
|
|
|
|
2003-02-02 14:29:33 -04:00
|
|
|
if (self->write_func(self, c_str, len) < 0)
|
2002-04-01 13:40:08 -04:00
|
|
|
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 {
|
2003-02-02 14:29:33 -04:00
|
|
|
if (self->write_func(self, PyString_AS_STRING(repr),
|
|
|
|
size) < 0)
|
2002-04-01 13:40:08 -04:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
Py_DECREF(repr);
|
|
|
|
}
|
2000-03-10 19:11:40 -04:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
if (doput)
|
|
|
|
if (put(self, args) < 0)
|
|
|
|
return -1;
|
2000-03-10 19:11:40 -04:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
return 0;
|
2000-03-10 19:11:40 -04:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
err:
|
|
|
|
Py_XDECREF(repr);
|
|
|
|
return -1;
|
2000-03-10 19:11:40 -04:00
|
|
|
}
|
2001-08-17 15:39:25 -03:00
|
|
|
#endif
|
2000-03-10 19:11:40 -04:00
|
|
|
|
2003-02-02 16:29:39 -04:00
|
|
|
/* A helper for save_tuple. Push the len elements in tuple t on the stack. */
|
|
|
|
static int
|
2003-02-04 23:46:17 -04:00
|
|
|
store_tuple_elements(Picklerobject *self, PyObject *t, int len)
|
2003-02-02 16:29:39 -04:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int res = -1; /* guilty until proved innocent */
|
|
|
|
|
|
|
|
assert(PyTuple_Size(t) == len);
|
2000-03-10 19:11:40 -04:00
|
|
|
|
2003-02-02 16:29:39 -04:00
|
|
|
for (i = 0; i < len; i++) {
|
|
|
|
PyObject *element = PyTuple_GET_ITEM(t, i);
|
|
|
|
|
|
|
|
if (element == NULL)
|
|
|
|
goto finally;
|
|
|
|
if (save(self, element, 0) < 0)
|
|
|
|
goto finally;
|
|
|
|
}
|
|
|
|
res = 0;
|
|
|
|
|
|
|
|
finally:
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Tuples are ubiquitous in the pickle protocols, so many techniques are
|
|
|
|
* used across protocols to minimize the space needed to pickle them.
|
2003-02-04 23:46:17 -04:00
|
|
|
* Tuples are also the only builtin immutable type that can be recursive
|
2003-02-02 16:29:39 -04:00
|
|
|
* (a tuple can be reached from itself), and that requires some subtle
|
|
|
|
* magic so that it works in all cases. IOW, this is a long routine.
|
|
|
|
*/
|
1997-04-09 14:36:32 -03:00
|
|
|
static int
|
2003-02-01 02:24:36 -04:00
|
|
|
save_tuple(Picklerobject *self, PyObject *args)
|
2002-04-01 13:40:08 -04:00
|
|
|
{
|
2003-02-02 16:29:39 -04:00
|
|
|
PyObject *py_tuple_id = NULL;
|
|
|
|
int len, i;
|
|
|
|
int res = -1;
|
2001-03-17 00:50:51 -04:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
static char tuple = TUPLE;
|
2003-02-02 16:29:39 -04:00
|
|
|
static char pop = POP;
|
|
|
|
static char pop_mark = POP_MARK;
|
|
|
|
static char len2opcode[] = {EMPTY_TUPLE, TUPLE1, TUPLE2, TUPLE3};
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
if ((len = PyTuple_Size(args)) < 0)
|
|
|
|
goto finally;
|
2001-03-17 00:50:51 -04:00
|
|
|
|
2003-02-02 16:29:39 -04:00
|
|
|
if (len == 0) {
|
|
|
|
char c_str[2];
|
2001-03-17 00:50:51 -04:00
|
|
|
|
2003-02-02 16:29:39 -04:00
|
|
|
if (self->proto) {
|
|
|
|
c_str[0] = EMPTY_TUPLE;
|
|
|
|
len = 1;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
c_str[0] = MARK;
|
|
|
|
c_str[1] = TUPLE;
|
|
|
|
len = 2;
|
|
|
|
}
|
|
|
|
if (self->write_func(self, c_str, len) >= 0)
|
|
|
|
res = 0;
|
|
|
|
/* Don't memoize an empty tuple. */
|
|
|
|
goto finally;
|
2002-04-01 13:40:08 -04:00
|
|
|
}
|
2001-03-17 00:50:51 -04:00
|
|
|
|
2003-02-02 16:29:39 -04:00
|
|
|
/* A non-empty tuple. */
|
|
|
|
|
|
|
|
/* id(tuple) isn't in the memo now. If it shows up there after
|
|
|
|
* saving the tuple elements, the tuple must be recursive, in
|
|
|
|
* which case we'll pop everything we put on the stack, and fetch
|
|
|
|
* its value from the memo.
|
|
|
|
*/
|
|
|
|
py_tuple_id = PyLong_FromVoidPtr(args);
|
|
|
|
if (py_tuple_id == NULL)
|
2002-04-01 13:40:08 -04:00
|
|
|
goto finally;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2003-02-02 16:29:39 -04:00
|
|
|
if (len <= 3 && self->proto >= 2) {
|
|
|
|
/* Use TUPLE{1,2,3} opcodes. */
|
2003-02-04 23:46:17 -04:00
|
|
|
if (store_tuple_elements(self, args, len) < 0)
|
2003-02-02 16:29:39 -04:00
|
|
|
goto finally;
|
2002-04-01 13:40:08 -04:00
|
|
|
if (PyDict_GetItem(self->memo, py_tuple_id)) {
|
2003-02-02 16:29:39 -04:00
|
|
|
/* pop the len elements */
|
|
|
|
for (i = 0; i < len; ++i)
|
|
|
|
if (self->write_func(self, &pop, 1) < 0)
|
2002-04-01 13:40:08 -04:00
|
|
|
goto finally;
|
2003-02-02 16:29:39 -04:00
|
|
|
/* fetch from memo */
|
2002-04-01 13:40:08 -04:00
|
|
|
if (get(self, py_tuple_id) < 0)
|
|
|
|
goto finally;
|
|
|
|
res = 0;
|
|
|
|
goto finally;
|
|
|
|
}
|
2003-02-02 16:29:39 -04:00
|
|
|
/* Not recursive. */
|
|
|
|
if (self->write_func(self, len2opcode + len, 1) < 0)
|
|
|
|
goto finally;
|
|
|
|
goto memoize;
|
2002-04-01 13:40:08 -04:00
|
|
|
}
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2003-02-02 16:29:39 -04:00
|
|
|
/* proto < 2 and len > 0, or proto >= 2 and len > 3.
|
|
|
|
* Generate MARK elt1 elt2 ... TUPLE
|
|
|
|
*/
|
|
|
|
if (self->write_func(self, &MARKv, 1) < 0)
|
|
|
|
goto finally;
|
|
|
|
|
2003-02-04 23:46:17 -04:00
|
|
|
if (store_tuple_elements(self, args, len) < 0)
|
2003-02-02 16:29:39 -04:00
|
|
|
goto finally;
|
|
|
|
|
|
|
|
if (PyDict_GetItem(self->memo, py_tuple_id)) {
|
|
|
|
/* pop the stack stuff we pushed */
|
|
|
|
if (self->bin) {
|
|
|
|
if (self->write_func(self, &pop_mark, 1) < 0)
|
|
|
|
goto finally;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* Note that we pop one more than len, to remove
|
|
|
|
* the MARK too.
|
|
|
|
*/
|
|
|
|
for (i = 0; i <= len; i++)
|
|
|
|
if (self->write_func(self, &pop, 1) < 0)
|
|
|
|
goto finally;
|
|
|
|
}
|
|
|
|
/* fetch from memo */
|
|
|
|
if (get(self, py_tuple_id) >= 0)
|
|
|
|
res = 0;
|
2002-04-01 13:40:08 -04:00
|
|
|
goto finally;
|
|
|
|
}
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2003-02-02 16:29:39 -04:00
|
|
|
/* Not recursive. */
|
|
|
|
if (self->write_func(self, &tuple, 1) < 0)
|
2002-04-01 13:40:08 -04:00
|
|
|
goto finally;
|
2001-03-17 00:50:51 -04:00
|
|
|
|
2003-02-02 16:29:39 -04:00
|
|
|
memoize:
|
|
|
|
if (put(self, args) >= 0)
|
|
|
|
res = 0;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
finally:
|
|
|
|
Py_XDECREF(py_tuple_id);
|
|
|
|
return res;
|
1997-01-06 18:59:08 -04:00
|
|
|
}
|
|
|
|
|
2003-02-11 17:06:20 -04:00
|
|
|
/* iter is an iterator giving items, and we batch up chunks of
|
|
|
|
* MARK item item ... item APPENDS
|
|
|
|
* opcode sequences. Calling code should have arranged to first create an
|
|
|
|
* empty list, or list-like object, for the APPENDS to operate on.
|
|
|
|
* Returns 0 on success, <0 on error.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
batch_list(Picklerobject *self, PyObject *iter)
|
|
|
|
{
|
|
|
|
PyObject *obj;
|
|
|
|
PyObject *slice[BATCHSIZE];
|
|
|
|
int i, n;
|
|
|
|
|
|
|
|
static char append = APPEND;
|
|
|
|
static char appends = APPENDS;
|
|
|
|
|
|
|
|
assert(iter != NULL);
|
|
|
|
|
|
|
|
if (self->proto == 0) {
|
|
|
|
/* APPENDS isn't available; do one at a time. */
|
|
|
|
for (;;) {
|
|
|
|
obj = PyIter_Next(iter);
|
|
|
|
if (obj == NULL) {
|
|
|
|
if (PyErr_Occurred())
|
|
|
|
return -1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
i = save(self, obj, 0);
|
|
|
|
Py_DECREF(obj);
|
|
|
|
if (i < 0)
|
|
|
|
return -1;
|
|
|
|
if (self->write_func(self, &append, 1) < 0)
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* proto > 0: write in batches of BATCHSIZE. */
|
|
|
|
do {
|
|
|
|
/* Get next group of (no more than) BATCHSIZE elements. */
|
|
|
|
for (n = 0; n < BATCHSIZE; ++n) {
|
|
|
|
obj = PyIter_Next(iter);
|
|
|
|
if (obj == NULL) {
|
|
|
|
if (PyErr_Occurred())
|
|
|
|
goto BatchFailed;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
slice[n] = obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (n > 1) {
|
|
|
|
/* Pump out MARK, slice[0:n], APPENDS. */
|
|
|
|
if (self->write_func(self, &MARKv, 1) < 0)
|
|
|
|
goto BatchFailed;
|
|
|
|
for (i = 0; i < n; ++i) {
|
|
|
|
if (save(self, slice[i], 0) < 0)
|
|
|
|
goto BatchFailed;
|
|
|
|
}
|
|
|
|
if (self->write_func(self, &appends, 1) < 0)
|
|
|
|
goto BatchFailed;
|
|
|
|
}
|
|
|
|
else if (n == 1) {
|
|
|
|
if (save(self, slice[0], 0) < 0)
|
|
|
|
goto BatchFailed;
|
|
|
|
if (self->write_func(self, &append, 1) < 0)
|
|
|
|
goto BatchFailed;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < n; ++i) {
|
|
|
|
Py_DECREF(slice[i]);
|
|
|
|
}
|
2003-02-12 01:28:58 -04:00
|
|
|
} while (n == BATCHSIZE);
|
2003-02-11 17:06:20 -04:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
BatchFailed:
|
|
|
|
while (--n >= 0) {
|
|
|
|
Py_DECREF(slice[n]);
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
static int
|
2003-02-01 02:24:36 -04:00
|
|
|
save_list(Picklerobject *self, PyObject *args)
|
2002-04-01 13:40:08 -04:00
|
|
|
{
|
2003-02-11 17:06:20 -04:00
|
|
|
int res = -1;
|
2002-04-01 13:40:08 -04:00
|
|
|
char s[3];
|
2003-02-11 17:06:20 -04:00
|
|
|
int len;
|
|
|
|
PyObject *iter;
|
1997-04-09 14:36:32 -03:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
if (self->fast && !fast_save_enter(self, args))
|
|
|
|
goto finally;
|
Progress on SF bug #466175 and general cleanup.
Add a fast_container member to Picklerobject. If fast is true, then
fast_container counts the depth of nested container calls. If the
depth exceeds FAST_LIMIT (2000), the fast flag is ignored and the
normal checks occur. This approach is much like the approach for
prevent stack overflow for comparison and reprs of recursive objects
(e.g. [[...]]).
- Fast container used for save_list(), save_dict(), and
save_inst().
XXX Not clear which other save_xxx() functions should use it.
Make Picklerobject into new-style types, using PyObject_GenericGetAttr()
and PyObject_GenericSetAttr().
- Use PyMemberDef for binary and fast members
- Use PyGetSetDef for persistent_id, inst_persistent_id, memo, and
PicklingError.
XXX Not all of these seem like they need to use getset, but it's
not clear why the old getattr() and setattr() had such odd
semantics. One change is that the getvalue() attribute will
exist on all Picklers, not just list-based picklers; I think
this is a more rationale interface.
There is a long laundry list of other changes:
- Remove unused #defines for PyList_SET_ITEM() etc.
- Make some of the indentation consistent
- Replace uses of cPickle_PyMapping_HasKey() where the first
argument is self->memo with calls to PyDict_GetItem(), because
self->memo must be a dictionary.
- Don't bother to check if cPickle_PyMapping_HasKey() returns < 0,
because it can only return 0 or 1.
- Replace uses of PyObject_CallObject() with PyObject_Call(), when
we can guarantee that the argument tuple is really a tuple.
Performance impacts of these changes:
- 5% speedup for normal pickling
- No change to fast-mode pickling.
XXX Really need tests for all the features in cPickle that aren't in
pickle.
2001-10-12 01:11:06 -03:00
|
|
|
|
2003-02-11 17:06:20 -04:00
|
|
|
/* Create an empty list. */
|
2002-04-01 13:40:08 -04:00
|
|
|
if (self->bin) {
|
|
|
|
s[0] = EMPTY_LIST;
|
2003-02-11 17:06:20 -04:00
|
|
|
len = 1;
|
2002-04-01 13:40:08 -04:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
s[0] = MARK;
|
|
|
|
s[1] = LIST;
|
2003-02-11 17:06:20 -04:00
|
|
|
len = 2;
|
2002-04-01 13:40:08 -04:00
|
|
|
}
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2003-02-11 17:06:20 -04:00
|
|
|
if (self->write_func(self, s, len) < 0)
|
2002-04-01 13:40:08 -04:00
|
|
|
goto finally;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2003-02-11 17:06:20 -04:00
|
|
|
/* Get list length, and bow out early if empty. */
|
|
|
|
if ((len = PyList_Size(args)) < 0)
|
2002-04-01 13:40:08 -04:00
|
|
|
goto finally;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2003-02-11 17:06:20 -04:00
|
|
|
/* Memoize. */
|
2002-04-01 13:40:08 -04:00
|
|
|
if (len == 0) {
|
2003-02-12 01:28:58 -04:00
|
|
|
if (put(self, args) >= 0)
|
|
|
|
res = 0;
|
|
|
|
goto finally;
|
2002-04-01 13:40:08 -04:00
|
|
|
}
|
2003-02-12 01:28:58 -04:00
|
|
|
if (put2(self, args) < 0)
|
|
|
|
goto finally;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2003-02-11 17:06:20 -04:00
|
|
|
/* Materialize the list elements. */
|
|
|
|
iter = PyObject_GetIter(args);
|
|
|
|
if (iter == NULL)
|
|
|
|
goto finally;
|
|
|
|
res = batch_list(self, iter);
|
|
|
|
Py_DECREF(iter);
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
finally:
|
|
|
|
if (self->fast && !fast_save_leave(self, args))
|
|
|
|
res = -1;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
return res;
|
1997-04-09 14:36:32 -03:00
|
|
|
}
|
1997-01-06 18:59:08 -04:00
|
|
|
|
|
|
|
|
2003-02-11 18:43:24 -04:00
|
|
|
/* iter is an iterator giving (key, value) pairs, and we batch up chunks of
|
|
|
|
* MARK key value ... key value SETITEMS
|
|
|
|
* opcode sequences. Calling code should have arranged to first create an
|
|
|
|
* empty dict, or dict-like object, for the SETITEMS to operate on.
|
|
|
|
* Returns 0 on success, <0 on error.
|
|
|
|
*
|
|
|
|
* This is very much like batch_list(). The difference between saving
|
|
|
|
* elements directly, and picking apart two-tuples, is so long-winded at
|
|
|
|
* the C level, though, that attempts to combine these routines were too
|
|
|
|
* ugly to bear.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
batch_dict(Picklerobject *self, PyObject *iter)
|
|
|
|
{
|
|
|
|
PyObject *p;
|
|
|
|
PyObject *slice[BATCHSIZE];
|
|
|
|
int i, n;
|
|
|
|
|
|
|
|
static char setitem = SETITEM;
|
|
|
|
static char setitems = SETITEMS;
|
|
|
|
|
|
|
|
assert(iter != NULL);
|
|
|
|
|
|
|
|
if (self->proto == 0) {
|
|
|
|
/* SETITEMS isn't available; do one at a time. */
|
|
|
|
for (;;) {
|
|
|
|
p = PyIter_Next(iter);
|
|
|
|
if (p == NULL) {
|
|
|
|
if (PyErr_Occurred())
|
|
|
|
return -1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!PyTuple_Check(p) || PyTuple_Size(p) != 2) {
|
|
|
|
PyErr_SetString(PyExc_TypeError, "dict items "
|
|
|
|
"iterator must return 2-tuples");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
i = save(self, PyTuple_GET_ITEM(p, 0), 0);
|
|
|
|
if (i >= 0)
|
|
|
|
i = save(self, PyTuple_GET_ITEM(p, 1), 0);
|
|
|
|
Py_DECREF(p);
|
|
|
|
if (i < 0)
|
|
|
|
return -1;
|
|
|
|
if (self->write_func(self, &setitem, 1) < 0)
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* proto > 0: write in batches of BATCHSIZE. */
|
|
|
|
do {
|
|
|
|
/* Get next group of (no more than) BATCHSIZE elements. */
|
|
|
|
for (n = 0; n < BATCHSIZE; ++n) {
|
|
|
|
p = PyIter_Next(iter);
|
|
|
|
if (p == NULL) {
|
|
|
|
if (PyErr_Occurred())
|
|
|
|
goto BatchFailed;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!PyTuple_Check(p) || PyTuple_Size(p) != 2) {
|
|
|
|
PyErr_SetString(PyExc_TypeError, "dict items "
|
|
|
|
"iterator must return 2-tuples");
|
|
|
|
goto BatchFailed;
|
|
|
|
}
|
|
|
|
slice[n] = p;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (n > 1) {
|
|
|
|
/* Pump out MARK, slice[0:n], SETITEMS. */
|
|
|
|
if (self->write_func(self, &MARKv, 1) < 0)
|
|
|
|
goto BatchFailed;
|
|
|
|
for (i = 0; i < n; ++i) {
|
|
|
|
p = slice[i];
|
|
|
|
if (save(self, PyTuple_GET_ITEM(p, 0), 0) < 0)
|
|
|
|
goto BatchFailed;
|
|
|
|
if (save(self, PyTuple_GET_ITEM(p, 1), 0) < 0)
|
|
|
|
goto BatchFailed;
|
|
|
|
}
|
|
|
|
if (self->write_func(self, &setitems, 1) < 0)
|
|
|
|
goto BatchFailed;
|
|
|
|
}
|
|
|
|
else if (n == 1) {
|
|
|
|
p = slice[0];
|
|
|
|
if (save(self, PyTuple_GET_ITEM(p, 0), 0) < 0)
|
|
|
|
goto BatchFailed;
|
|
|
|
if (save(self, PyTuple_GET_ITEM(p, 1), 0) < 0)
|
|
|
|
goto BatchFailed;
|
|
|
|
if (self->write_func(self, &setitem, 1) < 0)
|
|
|
|
goto BatchFailed;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < n; ++i) {
|
|
|
|
Py_DECREF(slice[i]);
|
|
|
|
}
|
2003-02-12 01:28:58 -04:00
|
|
|
} while (n == BATCHSIZE);
|
2003-02-11 18:43:24 -04:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
BatchFailed:
|
|
|
|
while (--n >= 0) {
|
|
|
|
Py_DECREF(slice[n]);
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
static int
|
2003-02-01 02:24:36 -04:00
|
|
|
save_dict(Picklerobject *self, PyObject *args)
|
2002-04-01 13:40:08 -04:00
|
|
|
{
|
2003-02-11 18:43:24 -04:00
|
|
|
int res = -1;
|
2002-04-01 13:40:08 -04:00
|
|
|
char s[3];
|
2003-02-11 18:43:24 -04:00
|
|
|
int len;
|
|
|
|
PyObject *iter;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
if (self->fast && !fast_save_enter(self, args))
|
|
|
|
goto finally;
|
Progress on SF bug #466175 and general cleanup.
Add a fast_container member to Picklerobject. If fast is true, then
fast_container counts the depth of nested container calls. If the
depth exceeds FAST_LIMIT (2000), the fast flag is ignored and the
normal checks occur. This approach is much like the approach for
prevent stack overflow for comparison and reprs of recursive objects
(e.g. [[...]]).
- Fast container used for save_list(), save_dict(), and
save_inst().
XXX Not clear which other save_xxx() functions should use it.
Make Picklerobject into new-style types, using PyObject_GenericGetAttr()
and PyObject_GenericSetAttr().
- Use PyMemberDef for binary and fast members
- Use PyGetSetDef for persistent_id, inst_persistent_id, memo, and
PicklingError.
XXX Not all of these seem like they need to use getset, but it's
not clear why the old getattr() and setattr() had such odd
semantics. One change is that the getvalue() attribute will
exist on all Picklers, not just list-based picklers; I think
this is a more rationale interface.
There is a long laundry list of other changes:
- Remove unused #defines for PyList_SET_ITEM() etc.
- Make some of the indentation consistent
- Replace uses of cPickle_PyMapping_HasKey() where the first
argument is self->memo with calls to PyDict_GetItem(), because
self->memo must be a dictionary.
- Don't bother to check if cPickle_PyMapping_HasKey() returns < 0,
because it can only return 0 or 1.
- Replace uses of PyObject_CallObject() with PyObject_Call(), when
we can guarantee that the argument tuple is really a tuple.
Performance impacts of these changes:
- 5% speedup for normal pickling
- No change to fast-mode pickling.
XXX Really need tests for all the features in cPickle that aren't in
pickle.
2001-10-12 01:11:06 -03:00
|
|
|
|
2003-02-11 18:43:24 -04:00
|
|
|
/* Create an empty dict. */
|
2002-04-01 13:40:08 -04:00
|
|
|
if (self->bin) {
|
|
|
|
s[0] = EMPTY_DICT;
|
|
|
|
len = 1;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
s[0] = MARK;
|
|
|
|
s[1] = DICT;
|
|
|
|
len = 2;
|
|
|
|
}
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2003-02-02 14:29:33 -04:00
|
|
|
if (self->write_func(self, s, len) < 0)
|
2002-04-01 13:40:08 -04:00
|
|
|
goto finally;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2003-02-11 18:43:24 -04:00
|
|
|
/* Get dict size, and bow out early if empty. */
|
2002-04-01 13:40:08 -04:00
|
|
|
if ((len = PyDict_Size(args)) < 0)
|
|
|
|
goto finally;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
if (len == 0) {
|
2003-02-12 01:28:58 -04:00
|
|
|
if (put(self, args) >= 0)
|
|
|
|
res = 0;
|
|
|
|
goto finally;
|
2002-04-01 13:40:08 -04:00
|
|
|
}
|
2003-02-12 01:28:58 -04:00
|
|
|
if (put2(self, args) < 0)
|
|
|
|
goto finally;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2003-02-11 18:43:24 -04:00
|
|
|
/* Materialize the dict items. */
|
|
|
|
iter = PyObject_CallMethod(args, "iteritems", "()");
|
|
|
|
if (iter == NULL)
|
|
|
|
goto finally;
|
|
|
|
res = batch_dict(self, iter);
|
|
|
|
Py_DECREF(iter);
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
finally:
|
|
|
|
if (self->fast && !fast_save_leave(self, args))
|
|
|
|
res = -1;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
return res;
|
1997-04-09 14:36:32 -03:00
|
|
|
}
|
1997-01-06 18:59:08 -04:00
|
|
|
|
|
|
|
|
2001-03-17 00:50:51 -04:00
|
|
|
static int
|
2003-02-01 02:24:36 -04:00
|
|
|
save_inst(Picklerobject *self, PyObject *args)
|
2002-04-01 13:40:08 -04:00
|
|
|
{
|
|
|
|
PyObject *class = 0, *module = 0, *name = 0, *state = 0,
|
|
|
|
*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
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
static char inst = INST, obj = OBJ, build = BUILD;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
if (self->fast && !fast_save_enter(self, args))
|
|
|
|
goto finally;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2003-02-02 14:29:33 -04:00
|
|
|
if (self->write_func(self, &MARKv, 1) < 0)
|
2002-04-01 13:40:08 -04:00
|
|
|
goto finally;
|
1997-04-09 14:36:32 -03:00
|
|
|
|
2003-02-01 02:24:36 -04:00
|
|
|
if (!( class = PyObject_GetAttr(args, __class___str)))
|
2002-04-01 13:40:08 -04:00
|
|
|
goto finally;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
if (self->bin) {
|
|
|
|
if (save(self, class, 0) < 0)
|
|
|
|
goto finally;
|
|
|
|
}
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
if ((getinitargs_func = PyObject_GetAttr(args, __getinitargs___str))) {
|
|
|
|
PyObject *element = 0;
|
|
|
|
int i, len;
|
1998-11-25 12:18:00 -04:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
if (!( class_args =
|
2003-02-01 02:24:36 -04:00
|
|
|
PyObject_Call(getinitargs_func, empty_tuple, NULL)))
|
2002-04-01 13:40:08 -04:00
|
|
|
goto finally;
|
2001-03-17 00:50:51 -04:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
if ((len = PyObject_Size(class_args)) < 0)
|
|
|
|
goto finally;
|
1998-11-25 12:18:00 -04:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
for (i = 0; i < len; i++) {
|
2003-02-01 02:24:36 -04:00
|
|
|
if (!( element = PySequence_GetItem(class_args, i)))
|
2002-04-01 13:40:08 -04:00
|
|
|
goto finally;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
if (save(self, element, 0) < 0) {
|
|
|
|
Py_DECREF(element);
|
|
|
|
goto finally;
|
|
|
|
}
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
Py_DECREF(element);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2003-06-16 17:19:49 -03:00
|
|
|
if (PyErr_ExceptionMatches(PyExc_AttributeError))
|
|
|
|
PyErr_Clear();
|
|
|
|
else
|
|
|
|
goto finally;
|
2002-04-01 13:40:08 -04:00
|
|
|
}
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
if (!self->bin) {
|
|
|
|
if (!( name = ((PyClassObject *)class)->cl_name )) {
|
|
|
|
PyErr_SetString(PicklingError, "class has no name");
|
|
|
|
goto finally;
|
|
|
|
}
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2003-02-01 02:24:36 -04:00
|
|
|
if (!( module = whichmodule(class, name)))
|
2002-04-01 13:40:08 -04:00
|
|
|
goto finally;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
if ((module_size = PyString_Size(module)) < 0 ||
|
|
|
|
(name_size = PyString_Size(name)) < 0)
|
|
|
|
goto finally;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
module_str = PyString_AS_STRING((PyStringObject *)module);
|
|
|
|
name_str = PyString_AS_STRING((PyStringObject *)name);
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2003-02-02 14:29:33 -04:00
|
|
|
if (self->write_func(self, &inst, 1) < 0)
|
2002-04-01 13:40:08 -04:00
|
|
|
goto finally;
|
2001-03-17 00:50:51 -04:00
|
|
|
|
2003-02-02 14:29:33 -04:00
|
|
|
if (self->write_func(self, module_str, module_size) < 0)
|
2002-04-01 13:40:08 -04:00
|
|
|
goto finally;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2003-02-02 14:29:33 -04:00
|
|
|
if (self->write_func(self, "\n", 1) < 0)
|
2002-04-01 13:40:08 -04:00
|
|
|
goto finally;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2003-02-02 14:29:33 -04:00
|
|
|
if (self->write_func(self, name_str, name_size) < 0)
|
2002-04-01 13:40:08 -04:00
|
|
|
goto finally;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2003-02-02 14:29:33 -04:00
|
|
|
if (self->write_func(self, "\n", 1) < 0)
|
2002-04-01 13:40:08 -04:00
|
|
|
goto finally;
|
|
|
|
}
|
2003-02-02 14:29:33 -04:00
|
|
|
else if (self->write_func(self, &obj, 1) < 0) {
|
2002-04-01 13:40:08 -04:00
|
|
|
goto finally;
|
|
|
|
}
|
Progress on SF bug #466175 and general cleanup.
Add a fast_container member to Picklerobject. If fast is true, then
fast_container counts the depth of nested container calls. If the
depth exceeds FAST_LIMIT (2000), the fast flag is ignored and the
normal checks occur. This approach is much like the approach for
prevent stack overflow for comparison and reprs of recursive objects
(e.g. [[...]]).
- Fast container used for save_list(), save_dict(), and
save_inst().
XXX Not clear which other save_xxx() functions should use it.
Make Picklerobject into new-style types, using PyObject_GenericGetAttr()
and PyObject_GenericSetAttr().
- Use PyMemberDef for binary and fast members
- Use PyGetSetDef for persistent_id, inst_persistent_id, memo, and
PicklingError.
XXX Not all of these seem like they need to use getset, but it's
not clear why the old getattr() and setattr() had such odd
semantics. One change is that the getvalue() attribute will
exist on all Picklers, not just list-based picklers; I think
this is a more rationale interface.
There is a long laundry list of other changes:
- Remove unused #defines for PyList_SET_ITEM() etc.
- Make some of the indentation consistent
- Replace uses of cPickle_PyMapping_HasKey() where the first
argument is self->memo with calls to PyDict_GetItem(), because
self->memo must be a dictionary.
- Don't bother to check if cPickle_PyMapping_HasKey() returns < 0,
because it can only return 0 or 1.
- Replace uses of PyObject_CallObject() with PyObject_Call(), when
we can guarantee that the argument tuple is really a tuple.
Performance impacts of these changes:
- 5% speedup for normal pickling
- No change to fast-mode pickling.
XXX Really need tests for all the features in cPickle that aren't in
pickle.
2001-10-12 01:11:06 -03:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
if ((getstate_func = PyObject_GetAttr(args, __getstate___str))) {
|
|
|
|
state = PyObject_Call(getstate_func, empty_tuple, NULL);
|
2003-02-01 02:24:36 -04:00
|
|
|
if (!state)
|
2002-04-01 13:40:08 -04:00
|
|
|
goto finally;
|
|
|
|
}
|
|
|
|
else {
|
2003-06-16 17:19:49 -03:00
|
|
|
if (PyErr_ExceptionMatches(PyExc_AttributeError))
|
|
|
|
PyErr_Clear();
|
|
|
|
else
|
|
|
|
goto finally;
|
2002-04-01 13:40:08 -04:00
|
|
|
|
|
|
|
if (!( state = PyObject_GetAttr(args, __dict___str))) {
|
2003-06-16 17:19:49 -03:00
|
|
|
if (PyErr_ExceptionMatches(PyExc_AttributeError))
|
|
|
|
PyErr_Clear();
|
|
|
|
else
|
|
|
|
goto finally;
|
2002-04-01 13:40:08 -04:00
|
|
|
res = 0;
|
|
|
|
goto finally;
|
|
|
|
}
|
|
|
|
}
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
if (!PyDict_Check(state)) {
|
|
|
|
if (put2(self, args) < 0)
|
|
|
|
goto finally;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (put(self, args) < 0)
|
|
|
|
goto finally;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (save(self, state, 0) < 0)
|
|
|
|
goto finally;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2003-02-02 14:29:33 -04:00
|
|
|
if (self->write_func(self, &build, 1) < 0)
|
2002-04-01 13:40:08 -04:00
|
|
|
goto finally;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
res = 0;
|
|
|
|
|
|
|
|
finally:
|
|
|
|
if (self->fast && !fast_save_leave(self, args))
|
|
|
|
res = -1;
|
|
|
|
|
|
|
|
Py_XDECREF(module);
|
|
|
|
Py_XDECREF(class);
|
|
|
|
Py_XDECREF(state);
|
|
|
|
Py_XDECREF(getinitargs_func);
|
|
|
|
Py_XDECREF(getstate_func);
|
|
|
|
Py_XDECREF(class_args);
|
|
|
|
|
|
|
|
return res;
|
1997-01-06 18:59:08 -04:00
|
|
|
}
|
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
static int
|
2003-02-01 02:24:36 -04:00
|
|
|
save_global(Picklerobject *self, PyObject *args, PyObject *name)
|
2002-04-01 13:40:08 -04:00
|
|
|
{
|
2002-07-11 19:02:33 -03:00
|
|
|
PyObject *global_name = 0, *module = 0, *mod = 0, *klass = 0;
|
2002-04-01 13:40:08 -04:00
|
|
|
char *name_str, *module_str;
|
|
|
|
int module_size, name_size, res = -1;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
static char global = GLOBAL;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
if (name) {
|
|
|
|
global_name = name;
|
|
|
|
Py_INCREF(global_name);
|
|
|
|
}
|
|
|
|
else {
|
2003-02-01 02:24:36 -04:00
|
|
|
if (!( global_name = PyObject_GetAttr(args, __name___str)))
|
2002-04-01 13:40:08 -04:00
|
|
|
goto finally;
|
|
|
|
}
|
1997-04-09 14:36:32 -03:00
|
|
|
|
2003-02-01 02:24:36 -04:00
|
|
|
if (!( module = whichmodule(args, global_name)))
|
2002-04-01 13:40:08 -04:00
|
|
|
goto finally;
|
1997-04-09 14:36:32 -03:00
|
|
|
|
2002-04-01 13:40:08 -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
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
module_str = PyString_AS_STRING((PyStringObject *)module);
|
|
|
|
name_str = PyString_AS_STRING((PyStringObject *)global_name);
|
1997-04-09 14:36:32 -03:00
|
|
|
|
2002-12-24 14:10:07 -04:00
|
|
|
/* XXX This can be doing a relative import. Clearly it shouldn't,
|
|
|
|
but I don't know how to stop it. :-( */
|
2002-04-01 13:40:08 -04:00
|
|
|
mod = PyImport_ImportModule(module_str);
|
|
|
|
if (mod == NULL) {
|
|
|
|
cPickle_ErrFormat(PicklingError,
|
2003-02-18 16:50:45 -04:00
|
|
|
"Can't pickle %s: import of module %s "
|
|
|
|
"failed",
|
|
|
|
"OS", args, module);
|
2002-04-01 13:40:08 -04:00
|
|
|
goto finally;
|
|
|
|
}
|
2002-07-11 19:01:40 -03:00
|
|
|
klass = PyObject_GetAttrString(mod, name_str);
|
2002-04-01 13:40:08 -04:00
|
|
|
if (klass == NULL) {
|
|
|
|
cPickle_ErrFormat(PicklingError,
|
2003-02-18 16:50:45 -04:00
|
|
|
"Can't pickle %s: attribute lookup %s.%s "
|
|
|
|
"failed",
|
2002-04-01 13:40:08 -04:00
|
|
|
"OSS", args, module, global_name);
|
|
|
|
goto finally;
|
|
|
|
}
|
|
|
|
if (klass != args) {
|
2002-07-11 19:01:40 -03:00
|
|
|
Py_DECREF(klass);
|
2002-04-01 13:40:08 -04:00
|
|
|
cPickle_ErrFormat(PicklingError,
|
2003-02-04 16:56:09 -04:00
|
|
|
"Can't pickle %s: it's not the same object "
|
|
|
|
"as %s.%s",
|
2002-04-01 13:40:08 -04:00
|
|
|
"OSS", args, module, global_name);
|
|
|
|
goto finally;
|
|
|
|
}
|
2002-07-11 19:01:40 -03:00
|
|
|
Py_DECREF(klass);
|
2002-04-01 13:40:08 -04:00
|
|
|
|
2003-02-04 16:56:09 -04:00
|
|
|
if (self->proto >= 2) {
|
|
|
|
/* See whether this is in the extension registry, and if
|
|
|
|
* so generate an EXT opcode.
|
|
|
|
*/
|
|
|
|
PyObject *py_code; /* extension code as Python object */
|
2003-02-04 17:47:44 -04:00
|
|
|
long code; /* extension code as C value */
|
2003-02-04 16:56:09 -04:00
|
|
|
char c_str[5];
|
|
|
|
int n;
|
|
|
|
|
|
|
|
PyTuple_SET_ITEM(two_tuple, 0, module);
|
|
|
|
PyTuple_SET_ITEM(two_tuple, 1, global_name);
|
|
|
|
py_code = PyDict_GetItem(extension_registry, two_tuple);
|
|
|
|
if (py_code == NULL)
|
|
|
|
goto gen_global; /* not registered */
|
|
|
|
|
|
|
|
/* Verify py_code has the right type and value. */
|
|
|
|
if (!PyInt_Check(py_code)) {
|
|
|
|
cPickle_ErrFormat(PicklingError, "Can't pickle %s: "
|
2003-02-04 23:53:10 -04:00
|
|
|
"extension code %s isn't an integer",
|
2003-02-04 16:56:09 -04:00
|
|
|
"OO", args, py_code);
|
|
|
|
goto finally;
|
|
|
|
}
|
|
|
|
code = PyInt_AS_LONG(py_code);
|
|
|
|
if (code <= 0 || code > 0x7fffffffL) {
|
|
|
|
cPickle_ErrFormat(PicklingError, "Can't pickle %s: "
|
|
|
|
"extension code %ld is out of range",
|
|
|
|
"Ol", args, code);
|
|
|
|
goto finally;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Generate an EXT opcode. */
|
|
|
|
if (code <= 0xff) {
|
|
|
|
c_str[0] = EXT1;
|
|
|
|
c_str[1] = (char)code;
|
|
|
|
n = 2;
|
|
|
|
}
|
|
|
|
else if (code <= 0xffff) {
|
|
|
|
c_str[0] = EXT2;
|
|
|
|
c_str[1] = (char)(code & 0xff);
|
|
|
|
c_str[2] = (char)((code >> 8) & 0xff);
|
|
|
|
n = 3;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
c_str[0] = EXT4;
|
|
|
|
c_str[1] = (char)(code & 0xff);
|
|
|
|
c_str[2] = (char)((code >> 8) & 0xff);
|
|
|
|
c_str[3] = (char)((code >> 16) & 0xff);
|
|
|
|
c_str[4] = (char)((code >> 24) & 0xff);
|
|
|
|
n = 5;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (self->write_func(self, c_str, n) >= 0)
|
|
|
|
res = 0;
|
|
|
|
goto finally; /* and don't memoize */
|
|
|
|
}
|
|
|
|
|
|
|
|
gen_global:
|
2003-02-02 14:29:33 -04:00
|
|
|
if (self->write_func(self, &global, 1) < 0)
|
2002-04-01 13:40:08 -04:00
|
|
|
goto finally;
|
1997-04-09 14:36:32 -03:00
|
|
|
|
2003-02-02 14:29:33 -04:00
|
|
|
if (self->write_func(self, module_str, module_size) < 0)
|
2002-04-01 13:40:08 -04:00
|
|
|
goto finally;
|
2001-03-17 00:50:51 -04:00
|
|
|
|
2003-02-02 14:29:33 -04:00
|
|
|
if (self->write_func(self, "\n", 1) < 0)
|
2002-04-01 13:40:08 -04:00
|
|
|
goto finally;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2003-02-02 14:29:33 -04:00
|
|
|
if (self->write_func(self, name_str, name_size) < 0)
|
2002-04-01 13:40:08 -04:00
|
|
|
goto finally;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2003-02-02 14:29:33 -04:00
|
|
|
if (self->write_func(self, "\n", 1) < 0)
|
2002-04-01 13:40:08 -04:00
|
|
|
goto finally;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
if (put(self, args) < 0)
|
|
|
|
goto finally;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
res = 0;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
finally:
|
|
|
|
Py_XDECREF(module);
|
|
|
|
Py_XDECREF(global_name);
|
|
|
|
Py_XDECREF(mod);
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2001-03-17 00:50:51 -04:00
|
|
|
static int
|
2003-02-01 02:24:36 -04:00
|
|
|
save_pers(Picklerobject *self, PyObject *args, PyObject *f)
|
2002-04-01 13:40:08 -04:00
|
|
|
{
|
|
|
|
PyObject *pid = 0;
|
|
|
|
int size, res = -1;
|
|
|
|
|
|
|
|
static char persid = PERSID, binpersid = BINPERSID;
|
|
|
|
|
|
|
|
Py_INCREF(args);
|
|
|
|
ARG_TUP(self, args);
|
|
|
|
if (self->arg) {
|
|
|
|
pid = PyObject_Call(f, self->arg, NULL);
|
|
|
|
FREE_ARG_TUP(self);
|
|
|
|
}
|
|
|
|
if (! pid) return -1;
|
|
|
|
|
|
|
|
if (pid != Py_None) {
|
|
|
|
if (!self->bin) {
|
|
|
|
if (!PyString_Check(pid)) {
|
|
|
|
PyErr_SetString(PicklingError,
|
|
|
|
"persistent id must be string");
|
|
|
|
goto finally;
|
|
|
|
}
|
|
|
|
|
2003-02-02 14:29:33 -04:00
|
|
|
if (self->write_func(self, &persid, 1) < 0)
|
2002-04-01 13:40:08 -04:00
|
|
|
goto finally;
|
|
|
|
|
|
|
|
if ((size = PyString_Size(pid)) < 0)
|
|
|
|
goto finally;
|
|
|
|
|
2003-02-02 14:29:33 -04:00
|
|
|
if (self->write_func(self,
|
|
|
|
PyString_AS_STRING(
|
|
|
|
(PyStringObject *)pid),
|
|
|
|
size) < 0)
|
2002-04-01 13:40:08 -04:00
|
|
|
goto finally;
|
|
|
|
|
2003-02-02 14:29:33 -04:00
|
|
|
if (self->write_func(self, "\n", 1) < 0)
|
2002-04-01 13:40:08 -04:00
|
|
|
goto finally;
|
|
|
|
|
|
|
|
res = 1;
|
|
|
|
goto finally;
|
|
|
|
}
|
|
|
|
else if (save(self, pid, 1) >= 0) {
|
2003-02-02 14:29:33 -04:00
|
|
|
if (self->write_func(self, &binpersid, 1) < 0)
|
2002-04-01 13:40:08 -04:00
|
|
|
res = -1;
|
|
|
|
else
|
|
|
|
res = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
goto finally;
|
|
|
|
}
|
|
|
|
|
|
|
|
res = 0;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
finally:
|
|
|
|
Py_XDECREF(pid);
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
return res;
|
|
|
|
}
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2003-02-14 19:05:28 -04:00
|
|
|
/* We're saving ob, and args is the 2-thru-5 tuple returned by the
|
|
|
|
* appropriate __reduce__ method for ob.
|
|
|
|
*/
|
2002-04-01 13:40:08 -04:00
|
|
|
static int
|
2003-02-14 19:05:28 -04:00
|
|
|
save_reduce(Picklerobject *self, PyObject *args, PyObject *ob)
|
|
|
|
{
|
|
|
|
PyObject *callable;
|
|
|
|
PyObject *argtup;
|
|
|
|
PyObject *state = NULL;
|
|
|
|
PyObject *listitems = NULL;
|
|
|
|
PyObject *dictitems = NULL;
|
|
|
|
|
|
|
|
int use_newobj = self->proto >= 2;
|
|
|
|
|
|
|
|
static char reduce = REDUCE;
|
|
|
|
static char build = BUILD;
|
|
|
|
static char newobj = NEWOBJ;
|
|
|
|
|
|
|
|
if (! PyArg_UnpackTuple(args, "save_reduce", 2, 5,
|
|
|
|
&callable,
|
|
|
|
&argtup,
|
|
|
|
&state,
|
|
|
|
&listitems,
|
|
|
|
&dictitems))
|
2002-04-01 13:40:08 -04:00
|
|
|
return -1;
|
|
|
|
|
2004-12-07 03:05:57 -04:00
|
|
|
if (!PyTuple_Check(argtup)) {
|
|
|
|
PyErr_SetString(PicklingError,
|
|
|
|
"args from reduce() should be a tuple");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2003-02-14 19:05:28 -04:00
|
|
|
if (state == Py_None)
|
|
|
|
state = NULL;
|
|
|
|
if (listitems == Py_None)
|
|
|
|
listitems = NULL;
|
|
|
|
if (dictitems == Py_None)
|
|
|
|
dictitems = NULL;
|
2002-04-01 13:40:08 -04:00
|
|
|
|
2003-02-14 19:05:28 -04:00
|
|
|
/* Protocol 2 special case: if callable's name is __newobj__, use
|
|
|
|
* NEWOBJ. This consumes a lot of code.
|
|
|
|
*/
|
|
|
|
if (use_newobj) {
|
|
|
|
PyObject *temp = PyObject_GetAttr(callable, __name___str);
|
|
|
|
|
|
|
|
if (temp == NULL) {
|
2003-06-16 17:19:49 -03:00
|
|
|
if (PyErr_ExceptionMatches(PyExc_AttributeError))
|
|
|
|
PyErr_Clear();
|
|
|
|
else
|
|
|
|
return -1;
|
2003-02-14 19:05:28 -04:00
|
|
|
use_newobj = 0;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
use_newobj = PyString_Check(temp) &&
|
|
|
|
strcmp(PyString_AS_STRING(temp),
|
|
|
|
"__newobj__") == 0;
|
|
|
|
Py_DECREF(temp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (use_newobj) {
|
|
|
|
PyObject *cls;
|
|
|
|
PyObject *newargtup;
|
|
|
|
int n, i;
|
|
|
|
|
|
|
|
/* Sanity checks. */
|
|
|
|
n = PyTuple_Size(argtup);
|
|
|
|
if (n < 1) {
|
|
|
|
PyErr_SetString(PicklingError, "__newobj__ arglist "
|
|
|
|
"is empty");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
cls = PyTuple_GET_ITEM(argtup, 0);
|
|
|
|
if (! PyObject_HasAttrString(cls, "__new__")) {
|
|
|
|
PyErr_SetString(PicklingError, "args[0] from "
|
|
|
|
"__newobj__ args has no __new__");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* XXX How could ob be NULL? */
|
|
|
|
if (ob != NULL) {
|
|
|
|
PyObject *ob_dot_class;
|
2002-04-01 13:40:08 -04:00
|
|
|
|
2003-02-14 19:05:28 -04:00
|
|
|
ob_dot_class = PyObject_GetAttr(ob, __class___str);
|
2003-06-16 17:19:49 -03:00
|
|
|
if (ob_dot_class == NULL) {
|
|
|
|
if (PyErr_ExceptionMatches(
|
|
|
|
PyExc_AttributeError))
|
|
|
|
PyErr_Clear();
|
|
|
|
else
|
|
|
|
return -1;
|
|
|
|
}
|
2003-02-14 19:05:28 -04:00
|
|
|
i = ob_dot_class != cls; /* true iff a problem */
|
|
|
|
Py_XDECREF(ob_dot_class);
|
|
|
|
if (i) {
|
|
|
|
PyErr_SetString(PicklingError, "args[0] from "
|
|
|
|
"__newobj__ args has the wrong class");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Save the class and its __new__ arguments. */
|
|
|
|
if (save(self, cls, 0) < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
newargtup = PyTuple_New(n-1); /* argtup[1:] */
|
|
|
|
if (newargtup == NULL)
|
|
|
|
return -1;
|
|
|
|
for (i = 1; i < n; ++i) {
|
|
|
|
PyObject *temp = PyTuple_GET_ITEM(argtup, i);
|
|
|
|
Py_INCREF(temp);
|
|
|
|
PyTuple_SET_ITEM(newargtup, i-1, temp);
|
|
|
|
}
|
|
|
|
i = save(self, newargtup, 0) < 0;
|
|
|
|
Py_DECREF(newargtup);
|
|
|
|
if (i < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
/* Add NEWOBJ opcode. */
|
|
|
|
if (self->write_func(self, &newobj, 1) < 0)
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* Not using NEWOBJ. */
|
|
|
|
if (save(self, callable, 0) < 0 ||
|
|
|
|
save(self, argtup, 0) < 0 ||
|
|
|
|
self->write_func(self, &reduce, 1) < 0)
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Memoize. */
|
|
|
|
/* XXX How can ob be NULL? */
|
2002-04-01 13:40:08 -04:00
|
|
|
if (ob != NULL) {
|
|
|
|
if (state && !PyDict_Check(state)) {
|
|
|
|
if (put2(self, ob) < 0)
|
|
|
|
return -1;
|
|
|
|
}
|
2003-02-14 19:05:28 -04:00
|
|
|
else if (put(self, ob) < 0)
|
2002-04-01 13:40:08 -04:00
|
|
|
return -1;
|
|
|
|
}
|
2001-03-17 00:50:51 -04:00
|
|
|
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2003-02-14 19:05:28 -04:00
|
|
|
if (listitems && batch_list(self, listitems) < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (dictitems && batch_dict(self, dictitems) < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (state) {
|
|
|
|
if (save(self, state, 0) < 0 ||
|
|
|
|
self->write_func(self, &build, 1) < 0)
|
2002-04-01 13:40:08 -04:00
|
|
|
return -1;
|
|
|
|
}
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
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
|
2003-02-02 16:29:39 -04:00
|
|
|
save(Picklerobject *self, PyObject *args, int pers_save)
|
2002-04-01 13:40:08 -04:00
|
|
|
{
|
|
|
|
PyTypeObject *type;
|
2003-02-14 19:05:28 -04:00
|
|
|
PyObject *py_ob_id = 0, *__reduce__ = 0, *t = 0;
|
|
|
|
PyObject *arg_tup;
|
|
|
|
int res = -1;
|
|
|
|
int tmp, size;
|
2002-04-01 13:40:08 -04:00
|
|
|
|
2002-08-04 05:20:23 -03:00
|
|
|
if (self->nesting++ > Py_GetRecursionLimit()){
|
|
|
|
PyErr_SetString(PyExc_RuntimeError,
|
|
|
|
"maximum recursion depth exceeded");
|
|
|
|
goto finally;
|
|
|
|
}
|
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
if (!pers_save && self->pers_func) {
|
|
|
|
if ((tmp = save_pers(self, args, self->pers_func)) != 0) {
|
|
|
|
res = tmp;
|
|
|
|
goto finally;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (args == Py_None) {
|
|
|
|
res = save_none(self, args);
|
|
|
|
goto finally;
|
|
|
|
}
|
|
|
|
|
|
|
|
type = args->ob_type;
|
|
|
|
|
|
|
|
switch (type->tp_name[0]) {
|
2002-04-03 18:41:51 -04:00
|
|
|
case 'b':
|
|
|
|
if (args == Py_False || args == Py_True) {
|
|
|
|
res = save_bool(self, args);
|
|
|
|
goto finally;
|
|
|
|
}
|
|
|
|
break;
|
1997-04-09 14:36:32 -03:00
|
|
|
case 'i':
|
2002-04-01 13:40:08 -04:00
|
|
|
if (type == &PyInt_Type) {
|
|
|
|
res = save_int(self, args);
|
|
|
|
goto finally;
|
|
|
|
}
|
|
|
|
break;
|
1997-04-09 14:36:32 -03:00
|
|
|
|
|
|
|
case 'l':
|
2002-04-01 13:40:08 -04:00
|
|
|
if (type == &PyLong_Type) {
|
|
|
|
res = save_long(self, args);
|
|
|
|
goto finally;
|
|
|
|
}
|
|
|
|
break;
|
1997-04-09 14:36:32 -03:00
|
|
|
|
|
|
|
case 'f':
|
2002-04-01 13:40:08 -04:00
|
|
|
if (type == &PyFloat_Type) {
|
|
|
|
res = save_float(self, args);
|
|
|
|
goto finally;
|
|
|
|
}
|
|
|
|
break;
|
1997-04-09 14:36:32 -03:00
|
|
|
|
|
|
|
case 't':
|
2003-02-02 16:29:39 -04:00
|
|
|
if (type == &PyTuple_Type && PyTuple_Size(args) == 0) {
|
|
|
|
res = save_tuple(self, args);
|
2002-04-01 13:40:08 -04:00
|
|
|
goto finally;
|
|
|
|
}
|
|
|
|
break;
|
1997-04-09 14:36:32 -03:00
|
|
|
|
|
|
|
case 's':
|
2002-04-01 13:40:08 -04:00
|
|
|
if ((type == &PyString_Type) && (PyString_GET_SIZE(args) < 2)) {
|
|
|
|
res = save_string(self, args, 0);
|
|
|
|
goto finally;
|
|
|
|
}
|
2000-03-10 19:11:40 -04:00
|
|
|
|
2001-08-17 15:39:25 -03:00
|
|
|
#ifdef Py_USING_UNICODE
|
2000-03-10 19:11:40 -04:00
|
|
|
case 'u':
|
2002-04-01 13:40:08 -04:00
|
|
|
if ((type == &PyUnicode_Type) && (PyString_GET_SIZE(args) < 2)) {
|
|
|
|
res = save_unicode(self, args, 0);
|
|
|
|
goto finally;
|
|
|
|
}
|
2001-08-17 15:39:25 -03:00
|
|
|
#endif
|
2002-04-01 13:40:08 -04:00
|
|
|
}
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
if (args->ob_refcnt > 1) {
|
2003-02-01 02:24:36 -04:00
|
|
|
if (!( py_ob_id = PyLong_FromVoidPtr(args)))
|
2002-04-01 13:40:08 -04:00
|
|
|
goto finally;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
if (PyDict_GetItem(self->memo, py_ob_id)) {
|
|
|
|
if (get(self, py_ob_id) < 0)
|
|
|
|
goto finally;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
res = 0;
|
|
|
|
goto finally;
|
|
|
|
}
|
|
|
|
}
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
switch (type->tp_name[0]) {
|
1997-04-09 14:36:32 -03:00
|
|
|
case 's':
|
2002-04-01 13:40:08 -04:00
|
|
|
if (type == &PyString_Type) {
|
|
|
|
res = save_string(self, args, 1);
|
|
|
|
goto finally;
|
|
|
|
}
|
|
|
|
break;
|
1997-04-09 14:36:32 -03:00
|
|
|
|
2001-08-17 15:39:25 -03:00
|
|
|
#ifdef Py_USING_UNICODE
|
2000-03-10 19:11:40 -04:00
|
|
|
case 'u':
|
2002-04-01 13:40:08 -04:00
|
|
|
if (type == &PyUnicode_Type) {
|
|
|
|
res = save_unicode(self, args, 1);
|
|
|
|
goto finally;
|
|
|
|
}
|
|
|
|
break;
|
2001-08-17 15:39:25 -03:00
|
|
|
#endif
|
2000-03-10 19:11:40 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
case 't':
|
2002-04-01 13:40:08 -04:00
|
|
|
if (type == &PyTuple_Type) {
|
|
|
|
res = save_tuple(self, args);
|
|
|
|
goto finally;
|
|
|
|
}
|
|
|
|
if (type == &PyType_Type) {
|
|
|
|
res = save_global(self, args, NULL);
|
|
|
|
goto finally;
|
|
|
|
}
|
|
|
|
break;
|
1997-04-09 14:36:32 -03:00
|
|
|
|
|
|
|
case 'l':
|
2002-04-01 13:40:08 -04:00
|
|
|
if (type == &PyList_Type) {
|
|
|
|
res = save_list(self, args);
|
|
|
|
goto finally;
|
|
|
|
}
|
|
|
|
break;
|
1997-04-09 14:36:32 -03:00
|
|
|
|
|
|
|
case 'd':
|
2002-04-01 13:40:08 -04:00
|
|
|
if (type == &PyDict_Type) {
|
|
|
|
res = save_dict(self, args);
|
|
|
|
goto finally;
|
|
|
|
}
|
|
|
|
break;
|
1997-04-09 14:36:32 -03:00
|
|
|
|
|
|
|
case 'i':
|
2002-04-01 13:40:08 -04:00
|
|
|
if (type == &PyInstance_Type) {
|
|
|
|
res = save_inst(self, args);
|
|
|
|
goto finally;
|
|
|
|
}
|
|
|
|
break;
|
1997-04-09 14:36:32 -03:00
|
|
|
|
|
|
|
case 'c':
|
2002-04-01 13:40:08 -04:00
|
|
|
if (type == &PyClass_Type) {
|
|
|
|
res = save_global(self, args, NULL);
|
|
|
|
goto finally;
|
|
|
|
}
|
|
|
|
break;
|
1997-04-09 14:36:32 -03:00
|
|
|
|
|
|
|
case 'f':
|
2002-04-01 13:40:08 -04:00
|
|
|
if (type == &PyFunction_Type) {
|
|
|
|
res = save_global(self, args, NULL);
|
2004-02-26 12:21:45 -04:00
|
|
|
if (res && PyErr_ExceptionMatches(PickleError)) {
|
|
|
|
/* fall back to reduce */
|
|
|
|
PyErr_Clear();
|
|
|
|
break;
|
|
|
|
}
|
2002-04-01 13:40:08 -04:00
|
|
|
goto finally;
|
|
|
|
}
|
|
|
|
break;
|
1997-04-09 14:36:32 -03:00
|
|
|
|
|
|
|
case 'b':
|
2002-04-01 13:40:08 -04:00
|
|
|
if (type == &PyCFunction_Type) {
|
|
|
|
res = save_global(self, args, NULL);
|
|
|
|
goto finally;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!pers_save && self->inst_pers_func) {
|
|
|
|
if ((tmp = save_pers(self, args, self->inst_pers_func)) != 0) {
|
|
|
|
res = tmp;
|
|
|
|
goto finally;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-07-16 16:47:43 -03:00
|
|
|
if (PyType_IsSubtype(type, &PyType_Type)) {
|
|
|
|
res = save_global(self, args, NULL);
|
|
|
|
goto finally;
|
|
|
|
}
|
|
|
|
|
2003-02-18 21:45:13 -04:00
|
|
|
/* Get a reduction callable, and call it. This may come from
|
|
|
|
* copy_reg.dispatch_table, the object's __reduce_ex__ method,
|
|
|
|
* or the object's __reduce__ method.
|
2003-02-14 19:05:28 -04:00
|
|
|
*/
|
2003-02-13 17:03:57 -04:00
|
|
|
__reduce__ = PyDict_GetItem(dispatch_table, (PyObject *)type);
|
|
|
|
if (__reduce__ != NULL) {
|
2002-04-01 13:40:08 -04:00
|
|
|
Py_INCREF(__reduce__);
|
2003-02-18 21:45:13 -04:00
|
|
|
Py_INCREF(args);
|
|
|
|
ARG_TUP(self, args);
|
|
|
|
if (self->arg) {
|
|
|
|
t = PyObject_Call(__reduce__, self->arg, NULL);
|
|
|
|
FREE_ARG_TUP(self);
|
|
|
|
}
|
2002-04-01 13:40:08 -04:00
|
|
|
}
|
|
|
|
else {
|
2003-02-18 21:45:13 -04:00
|
|
|
/* Check for a __reduce_ex__ method. */
|
|
|
|
__reduce__ = PyObject_GetAttr(args, __reduce_ex___str);
|
|
|
|
if (__reduce__ != NULL) {
|
|
|
|
t = PyInt_FromLong(self->proto);
|
|
|
|
if (t != NULL) {
|
|
|
|
ARG_TUP(self, t);
|
|
|
|
t = NULL;
|
|
|
|
if (self->arg) {
|
|
|
|
t = PyObject_Call(__reduce__,
|
|
|
|
self->arg, NULL);
|
|
|
|
FREE_ARG_TUP(self);
|
|
|
|
}
|
|
|
|
}
|
2002-04-01 13:40:08 -04:00
|
|
|
}
|
2003-02-18 21:45:13 -04:00
|
|
|
else {
|
2003-06-16 17:19:49 -03:00
|
|
|
if (PyErr_ExceptionMatches(PyExc_AttributeError))
|
|
|
|
PyErr_Clear();
|
|
|
|
else
|
|
|
|
goto finally;
|
2003-02-18 21:45:13 -04:00
|
|
|
/* Check for a __reduce__ method. */
|
|
|
|
__reduce__ = PyObject_GetAttr(args, __reduce___str);
|
|
|
|
if (__reduce__ != NULL) {
|
|
|
|
t = PyObject_Call(__reduce__,
|
|
|
|
empty_tuple, NULL);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
PyErr_SetObject(UnpickleableError, args);
|
|
|
|
goto finally;
|
|
|
|
}
|
2002-04-01 13:40:08 -04:00
|
|
|
}
|
2003-02-14 19:05:28 -04:00
|
|
|
}
|
2002-04-01 13:40:08 -04:00
|
|
|
|
2003-02-14 19:05:28 -04:00
|
|
|
if (t == NULL)
|
|
|
|
goto finally;
|
2002-04-01 13:40:08 -04:00
|
|
|
|
2003-02-14 19:05:28 -04:00
|
|
|
if (PyString_Check(t)) {
|
|
|
|
res = save_global(self, args, t);
|
|
|
|
goto finally;
|
|
|
|
}
|
2002-04-01 13:40:08 -04:00
|
|
|
|
2003-02-14 19:05:28 -04:00
|
|
|
if (! PyTuple_Check(t)) {
|
|
|
|
cPickle_ErrFormat(PicklingError, "Value returned by "
|
|
|
|
"%s must be string or tuple",
|
2003-02-13 17:03:57 -04:00
|
|
|
"O", __reduce__);
|
2003-02-14 19:05:28 -04:00
|
|
|
goto finally;
|
|
|
|
}
|
2002-04-01 13:40:08 -04:00
|
|
|
|
2003-02-14 19:05:28 -04:00
|
|
|
size = PyTuple_Size(t);
|
|
|
|
if (size < 2 || size > 5) {
|
|
|
|
cPickle_ErrFormat(PicklingError, "tuple returned by "
|
|
|
|
"%s must contain 2 through 5 elements",
|
|
|
|
"O", __reduce__);
|
2002-04-01 13:40:08 -04:00
|
|
|
goto finally;
|
|
|
|
}
|
|
|
|
|
2003-02-14 19:05:28 -04:00
|
|
|
arg_tup = PyTuple_GET_ITEM(t, 1);
|
|
|
|
if (!(PyTuple_Check(arg_tup) || arg_tup == Py_None)) {
|
|
|
|
cPickle_ErrFormat(PicklingError, "Second element of "
|
|
|
|
"tuple returned by %s must be a tuple",
|
|
|
|
"O", __reduce__);
|
|
|
|
goto finally;
|
|
|
|
}
|
|
|
|
|
|
|
|
res = save_reduce(self, t, args);
|
2002-04-01 13:40:08 -04:00
|
|
|
|
|
|
|
finally:
|
2002-08-04 05:20:23 -03:00
|
|
|
self->nesting--;
|
2002-04-01 13:40:08 -04:00
|
|
|
Py_XDECREF(py_ob_id);
|
|
|
|
Py_XDECREF(__reduce__);
|
|
|
|
Py_XDECREF(t);
|
|
|
|
|
|
|
|
return res;
|
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
|
2003-02-01 02:24:36 -04:00
|
|
|
dump(Picklerobject *self, PyObject *args)
|
2002-04-01 13:40:08 -04:00
|
|
|
{
|
|
|
|
static char stop = STOP;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2003-02-02 12:09:05 -04:00
|
|
|
if (self->proto >= 2) {
|
|
|
|
char bytes[2];
|
|
|
|
|
|
|
|
bytes[0] = PROTO;
|
2003-02-02 12:16:30 -04:00
|
|
|
assert(self->proto >= 0 && self->proto < 256);
|
|
|
|
bytes[1] = (char)self->proto;
|
2003-02-02 12:09:05 -04:00
|
|
|
if (self->write_func(self, bytes, 2) < 0)
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
if (save(self, args, 0) < 0)
|
|
|
|
return -1;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2003-02-02 12:09:05 -04:00
|
|
|
if (self->write_func(self, &stop, 1) < 0)
|
2002-04-01 13:40:08 -04:00
|
|
|
return -1;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2003-02-02 12:09:05 -04:00
|
|
|
if (self->write_func(self, NULL, 0) < 0)
|
2002-04-01 13:40:08 -04:00
|
|
|
return -1;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
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 PyObject *
|
2003-02-01 02:24:36 -04:00
|
|
|
Pickle_clear_memo(Picklerobject *self, PyObject *args)
|
2002-04-01 13:40:08 -04:00
|
|
|
{
|
2003-02-01 02:24:36 -04:00
|
|
|
if (self->memo)
|
2002-04-01 13:40:08 -04:00
|
|
|
PyDict_Clear(self->memo);
|
|
|
|
Py_INCREF(Py_None);
|
|
|
|
return Py_None;
|
1998-11-25 12:18:00 -04:00
|
|
|
}
|
1997-04-09 14:36:32 -03:00
|
|
|
|
1998-11-25 12:18:00 -04:00
|
|
|
static PyObject *
|
2003-02-01 02:24:36 -04:00
|
|
|
Pickle_getvalue(Picklerobject *self, PyObject *args)
|
2002-04-01 13:40:08 -04:00
|
|
|
{
|
|
|
|
int l, i, rsize, ssize, clear=1, lm;
|
|
|
|
long ik;
|
|
|
|
PyObject *k, *r;
|
|
|
|
char *s, *p, *have_get;
|
|
|
|
Pdata *data;
|
|
|
|
|
|
|
|
/* Can be called by Python code or C code */
|
2003-02-01 02:24:36 -04:00
|
|
|
if (args && !PyArg_ParseTuple(args, "|i:getvalue", &clear))
|
2002-04-01 13:40:08 -04:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/* 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
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
/* flush write buffer */
|
|
|
|
if (write_other(self, NULL, 0) < 0) return NULL;
|
|
|
|
|
|
|
|
data=(Pdata*)self->file;
|
|
|
|
l=data->length;
|
|
|
|
|
|
|
|
/* set up an array to hold get/put status */
|
2003-02-02 14:08:34 -04:00
|
|
|
lm = PyDict_Size(self->memo);
|
|
|
|
if (lm < 0) return NULL;
|
2002-04-01 13:40:08 -04:00
|
|
|
lm++;
|
2003-02-02 14:08:34 -04:00
|
|
|
have_get = malloc(lm);
|
|
|
|
if (have_get == NULL) return PyErr_NoMemory();
|
|
|
|
memset(have_get, 0, lm);
|
2002-04-01 13:40:08 -04:00
|
|
|
|
|
|
|
/* Scan for gets. */
|
2003-02-02 14:08:34 -04:00
|
|
|
for (rsize = 0, i = l; --i >= 0; ) {
|
|
|
|
k = data->data[i];
|
2002-04-01 13:40:08 -04:00
|
|
|
|
2003-02-02 14:08:34 -04:00
|
|
|
if (PyString_Check(k))
|
2002-04-01 13:40:08 -04:00
|
|
|
rsize += PyString_GET_SIZE(k);
|
|
|
|
|
|
|
|
else if (PyInt_Check(k)) { /* put */
|
2003-02-02 14:08:34 -04:00
|
|
|
ik = PyInt_AS_LONG((PyIntObject*)k);
|
|
|
|
if (ik >= lm || ik == 0) {
|
2002-04-01 13:40:08 -04:00
|
|
|
PyErr_SetString(PicklingError,
|
|
|
|
"Invalid get data");
|
2006-07-23 04:57:11 -03:00
|
|
|
goto err;
|
2002-04-01 13:40:08 -04:00
|
|
|
}
|
2003-02-02 14:08:34 -04:00
|
|
|
if (have_get[ik]) /* with matching get */
|
|
|
|
rsize += ik < 256 ? 2 : 5;
|
2002-04-01 13:40:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
else if (! (PyTuple_Check(k) &&
|
|
|
|
PyTuple_GET_SIZE(k) == 2 &&
|
2003-02-02 14:08:34 -04:00
|
|
|
PyInt_Check((k = PyTuple_GET_ITEM(k, 0))))
|
2002-04-01 13:40:08 -04:00
|
|
|
) {
|
|
|
|
PyErr_SetString(PicklingError,
|
|
|
|
"Unexpected data in internal list");
|
2006-07-23 04:57:11 -03:00
|
|
|
goto err;
|
2002-04-01 13:40:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
else { /* put */
|
2003-02-02 14:08:34 -04:00
|
|
|
ik = PyInt_AS_LONG((PyIntObject *)k);
|
|
|
|
if (ik >= lm || ik == 0) {
|
2002-04-01 13:40:08 -04:00
|
|
|
PyErr_SetString(PicklingError,
|
|
|
|
"Invalid get data");
|
|
|
|
return NULL;
|
|
|
|
}
|
2003-02-02 14:08:34 -04:00
|
|
|
have_get[ik] = 1;
|
|
|
|
rsize += ik < 256 ? 2 : 5;
|
2002-04-01 13:40:08 -04:00
|
|
|
}
|
|
|
|
}
|
2001-03-17 00:50:51 -04:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
/* Now generate the result */
|
2003-02-02 14:08:34 -04:00
|
|
|
r = PyString_FromStringAndSize(NULL, rsize);
|
|
|
|
if (r == NULL) goto err;
|
|
|
|
s = PyString_AS_STRING((PyStringObject *)r);
|
2002-04-01 13:40:08 -04:00
|
|
|
|
2003-02-02 14:08:34 -04:00
|
|
|
for (i = 0; i < l; i++) {
|
|
|
|
k = data->data[i];
|
2002-04-01 13:40:08 -04:00
|
|
|
|
|
|
|
if (PyString_Check(k)) {
|
2003-02-02 14:08:34 -04:00
|
|
|
ssize = PyString_GET_SIZE(k);
|
2002-04-01 13:40:08 -04:00
|
|
|
if (ssize) {
|
2003-02-02 14:08:34 -04:00
|
|
|
p=PyString_AS_STRING((PyStringObject *)k);
|
|
|
|
while (--ssize >= 0)
|
|
|
|
*s++ = *p++;
|
2002-04-01 13:40:08 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (PyTuple_Check(k)) { /* get */
|
2003-02-02 14:08:34 -04:00
|
|
|
ik = PyInt_AS_LONG((PyIntObject *)
|
|
|
|
PyTuple_GET_ITEM(k, 0));
|
2002-04-01 13:40:08 -04:00
|
|
|
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 */
|
2003-02-02 14:08:34 -04:00
|
|
|
ik = PyInt_AS_LONG((PyIntObject*)k);
|
2002-04-01 13:40:08 -04:00
|
|
|
|
|
|
|
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);
|
2003-02-02 14:08:34 -04:00
|
|
|
Pdata_clear(data, 0);
|
2002-04-01 13:40:08 -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 *
|
2003-02-01 02:24:36 -04:00
|
|
|
Pickler_dump(Picklerobject *self, PyObject *args)
|
2002-04-01 13:40:08 -04:00
|
|
|
{
|
|
|
|
PyObject *ob;
|
|
|
|
int get=0;
|
1998-11-25 12:18:00 -04:00
|
|
|
|
2003-02-01 02:24:36 -04:00
|
|
|
if (!( PyArg_ParseTuple(args, "O|i:dump", &ob, &get)))
|
2002-04-01 13:40:08 -04:00
|
|
|
return NULL;
|
1998-11-25 12:18:00 -04:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
if (dump(self, ob) < 0)
|
|
|
|
return NULL;
|
1998-11-25 12:18:00 -04:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
if (get) return Pickle_getvalue(self, NULL);
|
1998-11-25 12:18:00 -04:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
/* XXX Why does dump() return self? */
|
|
|
|
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
|
|
|
|
2003-02-01 02:24:36 -04:00
|
|
|
static struct PyMethodDef Pickler_methods[] =
|
2002-04-01 13:40:08 -04:00
|
|
|
{
|
2002-03-31 10:44:22 -04:00
|
|
|
{"dump", (PyCFunction)Pickler_dump, METH_VARARGS,
|
2002-08-13 19:20:41 -03:00
|
|
|
PyDoc_STR("dump(object) -- "
|
|
|
|
"Write an object in pickle format to the object's pickle stream")},
|
2002-05-01 17:36:39 -03:00
|
|
|
{"clear_memo", (PyCFunction)Pickle_clear_memo, METH_NOARGS,
|
2002-08-13 19:20:41 -03:00
|
|
|
PyDoc_STR("clear_memo() -- Clear the picklers memo")},
|
2002-03-31 10:44:22 -04:00
|
|
|
{"getvalue", (PyCFunction)Pickle_getvalue, METH_VARARGS,
|
2002-08-13 19:20:41 -03:00
|
|
|
PyDoc_STR("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 *
|
2003-02-01 12:45:06 -04:00
|
|
|
newPicklerobject(PyObject *file, int proto)
|
2002-04-01 13:40:08 -04:00
|
|
|
{
|
|
|
|
Picklerobject *self;
|
|
|
|
|
2003-02-01 12:45:06 -04:00
|
|
|
if (proto < 0)
|
2003-02-13 11:44:41 -04:00
|
|
|
proto = HIGHEST_PROTOCOL;
|
|
|
|
if (proto > HIGHEST_PROTOCOL) {
|
2003-02-01 12:45:06 -04:00
|
|
|
PyErr_Format(PyExc_ValueError, "pickle protocol %d asked for; "
|
|
|
|
"the highest available protocol is %d",
|
2003-02-13 11:44:41 -04:00
|
|
|
proto, HIGHEST_PROTOCOL);
|
2002-04-01 13:40:08 -04:00
|
|
|
return NULL;
|
2003-02-01 12:45:06 -04:00
|
|
|
}
|
2002-04-01 13:40:08 -04:00
|
|
|
|
2003-04-09 18:05:12 -03:00
|
|
|
self = PyObject_GC_New(Picklerobject, &Picklertype);
|
2003-02-01 12:45:06 -04:00
|
|
|
if (self == NULL)
|
|
|
|
return NULL;
|
|
|
|
self->proto = proto;
|
|
|
|
self->bin = proto > 0;
|
2002-04-01 13:40:08 -04:00
|
|
|
self->fp = NULL;
|
|
|
|
self->write = NULL;
|
|
|
|
self->memo = NULL;
|
|
|
|
self->arg = NULL;
|
|
|
|
self->pers_func = NULL;
|
|
|
|
self->inst_pers_func = NULL;
|
|
|
|
self->write_buf = NULL;
|
|
|
|
self->fast = 0;
|
2002-08-04 05:20:23 -03:00
|
|
|
self->nesting = 0;
|
2002-04-01 13:40:08 -04:00
|
|
|
self->fast_container = 0;
|
|
|
|
self->fast_memo = NULL;
|
|
|
|
self->buf_size = 0;
|
|
|
|
self->dispatch_table = NULL;
|
|
|
|
|
2003-02-01 12:45:06 -04:00
|
|
|
self->file = NULL;
|
2002-04-01 13:40:08 -04:00
|
|
|
if (file)
|
|
|
|
Py_INCREF(file);
|
2003-02-01 12:45:06 -04:00
|
|
|
else {
|
|
|
|
file = Pdata_New();
|
|
|
|
if (file == NULL)
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
self->file = file;
|
2002-04-01 13:40:08 -04:00
|
|
|
|
2003-02-01 02:24:36 -04:00
|
|
|
if (!( self->memo = PyDict_New()))
|
2002-04-01 13:40:08 -04:00
|
|
|
goto err;
|
|
|
|
|
|
|
|
if (PyFile_Check(file)) {
|
|
|
|
self->fp = PyFile_AsFile(file);
|
|
|
|
if (self->fp == NULL) {
|
2003-02-01 12:45:06 -04:00
|
|
|
PyErr_SetString(PyExc_ValueError,
|
|
|
|
"I/O operation on closed file");
|
2002-04-01 13:40:08 -04:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
self->write_func = write_file;
|
|
|
|
}
|
|
|
|
else if (PycStringIO_OutputCheck(file)) {
|
|
|
|
self->write_func = write_cStringIO;
|
|
|
|
}
|
|
|
|
else if (file == Py_None) {
|
|
|
|
self->write_func = write_none;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
self->write_func = write_other;
|
|
|
|
|
|
|
|
if (! Pdata_Check(file)) {
|
|
|
|
self->write = PyObject_GetAttr(file, write_str);
|
|
|
|
if (!self->write) {
|
|
|
|
PyErr_Clear();
|
2003-02-01 02:24:36 -04:00
|
|
|
PyErr_SetString(PyExc_TypeError,
|
2002-04-01 13:40:08 -04:00
|
|
|
"argument must have 'write' "
|
|
|
|
"attribute");
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-02-01 12:45:06 -04:00
|
|
|
self->write_buf = (char *)PyMem_Malloc(WRITE_BUF_SIZE);
|
|
|
|
if (self->write_buf == NULL) {
|
2002-04-01 13:40:08 -04:00
|
|
|
PyErr_NoMemory();
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (PyEval_GetRestricted()) {
|
|
|
|
/* Restricted execution, get private tables */
|
2003-02-03 20:21:07 -04:00
|
|
|
PyObject *m = PyImport_Import(copy_reg_str);
|
2002-04-01 13:40:08 -04:00
|
|
|
|
2003-02-03 20:21:07 -04:00
|
|
|
if (m == NULL)
|
|
|
|
goto err;
|
|
|
|
self->dispatch_table = PyObject_GetAttr(m, dispatch_table_str);
|
2002-04-01 13:40:08 -04:00
|
|
|
Py_DECREF(m);
|
2003-02-03 20:21:07 -04:00
|
|
|
if (self->dispatch_table == NULL)
|
|
|
|
goto err;
|
2002-04-01 13:40:08 -04:00
|
|
|
}
|
|
|
|
else {
|
2003-02-03 20:21:07 -04:00
|
|
|
self->dispatch_table = dispatch_table;
|
2002-04-01 13:40:08 -04:00
|
|
|
Py_INCREF(dispatch_table);
|
|
|
|
}
|
2003-04-09 18:05:12 -03:00
|
|
|
PyObject_GC_Track(self);
|
2002-04-01 13:40:08 -04:00
|
|
|
|
|
|
|
return self;
|
|
|
|
|
|
|
|
err:
|
2003-02-01 12:45:06 -04:00
|
|
|
Py_DECREF(self);
|
2002-04-01 13:40:08 -04:00
|
|
|
return NULL;
|
1997-01-06 18:59:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static PyObject *
|
2004-07-27 02:22:33 -03:00
|
|
|
get_Pickler(PyObject *self, PyObject *args, PyObject *kwds)
|
2002-04-01 13:40:08 -04:00
|
|
|
{
|
2006-02-27 12:46:16 -04:00
|
|
|
static char *kwlist[] = {"file", "protocol", NULL};
|
2002-04-01 13:40:08 -04:00
|
|
|
PyObject *file = NULL;
|
2003-02-01 12:45:06 -04:00
|
|
|
int proto = 0;
|
2002-04-01 13:40:08 -04:00
|
|
|
|
2003-02-13 19:00:26 -04:00
|
|
|
/* XXX
|
2004-07-27 02:22:33 -03:00
|
|
|
* The documented signature is Pickler(file, protocol=0), but this
|
2003-02-13 19:00:26 -04:00
|
|
|
* accepts Pickler() and Pickler(integer) too. The meaning then
|
|
|
|
* is clear as mud, undocumented, and not supported by pickle.py.
|
|
|
|
* I'm told Zope uses this, but I haven't traced into this code
|
|
|
|
* far enough to figure out what it means.
|
2003-02-01 12:45:06 -04:00
|
|
|
*/
|
|
|
|
if (!PyArg_ParseTuple(args, "|i:Pickler", &proto)) {
|
2002-04-01 13:40:08 -04:00
|
|
|
PyErr_Clear();
|
2003-02-01 12:45:06 -04:00
|
|
|
proto = 0;
|
2004-07-27 02:22:33 -03:00
|
|
|
if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|i:Pickler",
|
|
|
|
kwlist, &file, &proto))
|
2002-04-01 13:40:08 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
2003-02-01 12:45:06 -04:00
|
|
|
return (PyObject *)newPicklerobject(file, proto);
|
1997-01-06 18:59:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
2003-02-01 02:24:36 -04:00
|
|
|
Pickler_dealloc(Picklerobject *self)
|
2002-04-01 13:40:08 -04:00
|
|
|
{
|
2003-04-09 18:05:12 -03:00
|
|
|
PyObject_GC_UnTrack(self);
|
2002-04-01 13:40:08 -04:00
|
|
|
Py_XDECREF(self->write);
|
|
|
|
Py_XDECREF(self->memo);
|
|
|
|
Py_XDECREF(self->fast_memo);
|
|
|
|
Py_XDECREF(self->arg);
|
|
|
|
Py_XDECREF(self->file);
|
|
|
|
Py_XDECREF(self->pers_func);
|
|
|
|
Py_XDECREF(self->inst_pers_func);
|
|
|
|
Py_XDECREF(self->dispatch_table);
|
2003-02-01 12:45:06 -04:00
|
|
|
PyMem_Free(self->write_buf);
|
2003-05-21 18:29:48 -03:00
|
|
|
self->ob_type->tp_free((PyObject *)self);
|
2003-04-09 18:05:12 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
Pickler_traverse(Picklerobject *self, visitproc visit, void *arg)
|
|
|
|
{
|
2006-04-15 18:47:09 -03:00
|
|
|
Py_VISIT(self->write);
|
|
|
|
Py_VISIT(self->memo);
|
|
|
|
Py_VISIT(self->fast_memo);
|
|
|
|
Py_VISIT(self->arg);
|
|
|
|
Py_VISIT(self->file);
|
|
|
|
Py_VISIT(self->pers_func);
|
|
|
|
Py_VISIT(self->inst_pers_func);
|
|
|
|
Py_VISIT(self->dispatch_table);
|
2003-04-09 18:05:12 -03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
Pickler_clear(Picklerobject *self)
|
|
|
|
{
|
2006-04-15 14:28:34 -03:00
|
|
|
Py_CLEAR(self->write);
|
|
|
|
Py_CLEAR(self->memo);
|
|
|
|
Py_CLEAR(self->fast_memo);
|
|
|
|
Py_CLEAR(self->arg);
|
|
|
|
Py_CLEAR(self->file);
|
|
|
|
Py_CLEAR(self->pers_func);
|
|
|
|
Py_CLEAR(self->inst_pers_func);
|
|
|
|
Py_CLEAR(self->dispatch_table);
|
2003-04-09 18:05:12 -03:00
|
|
|
return 0;
|
1997-01-06 18:59:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
Progress on SF bug #466175 and general cleanup.
Add a fast_container member to Picklerobject. If fast is true, then
fast_container counts the depth of nested container calls. If the
depth exceeds FAST_LIMIT (2000), the fast flag is ignored and the
normal checks occur. This approach is much like the approach for
prevent stack overflow for comparison and reprs of recursive objects
(e.g. [[...]]).
- Fast container used for save_list(), save_dict(), and
save_inst().
XXX Not clear which other save_xxx() functions should use it.
Make Picklerobject into new-style types, using PyObject_GenericGetAttr()
and PyObject_GenericSetAttr().
- Use PyMemberDef for binary and fast members
- Use PyGetSetDef for persistent_id, inst_persistent_id, memo, and
PicklingError.
XXX Not all of these seem like they need to use getset, but it's
not clear why the old getattr() and setattr() had such odd
semantics. One change is that the getvalue() attribute will
exist on all Picklers, not just list-based picklers; I think
this is a more rationale interface.
There is a long laundry list of other changes:
- Remove unused #defines for PyList_SET_ITEM() etc.
- Make some of the indentation consistent
- Replace uses of cPickle_PyMapping_HasKey() where the first
argument is self->memo with calls to PyDict_GetItem(), because
self->memo must be a dictionary.
- Don't bother to check if cPickle_PyMapping_HasKey() returns < 0,
because it can only return 0 or 1.
- Replace uses of PyObject_CallObject() with PyObject_Call(), when
we can guarantee that the argument tuple is really a tuple.
Performance impacts of these changes:
- 5% speedup for normal pickling
- No change to fast-mode pickling.
XXX Really need tests for all the features in cPickle that aren't in
pickle.
2001-10-12 01:11:06 -03:00
|
|
|
Pickler_get_pers_func(Picklerobject *p)
|
|
|
|
{
|
2002-04-01 13:40:08 -04:00
|
|
|
if (p->pers_func == NULL)
|
|
|
|
PyErr_SetString(PyExc_AttributeError, "persistent_id");
|
|
|
|
else
|
|
|
|
Py_INCREF(p->pers_func);
|
|
|
|
return p->pers_func;
|
1997-01-06 18:59:08 -04:00
|
|
|
}
|
|
|
|
|
Progress on SF bug #466175 and general cleanup.
Add a fast_container member to Picklerobject. If fast is true, then
fast_container counts the depth of nested container calls. If the
depth exceeds FAST_LIMIT (2000), the fast flag is ignored and the
normal checks occur. This approach is much like the approach for
prevent stack overflow for comparison and reprs of recursive objects
(e.g. [[...]]).
- Fast container used for save_list(), save_dict(), and
save_inst().
XXX Not clear which other save_xxx() functions should use it.
Make Picklerobject into new-style types, using PyObject_GenericGetAttr()
and PyObject_GenericSetAttr().
- Use PyMemberDef for binary and fast members
- Use PyGetSetDef for persistent_id, inst_persistent_id, memo, and
PicklingError.
XXX Not all of these seem like they need to use getset, but it's
not clear why the old getattr() and setattr() had such odd
semantics. One change is that the getvalue() attribute will
exist on all Picklers, not just list-based picklers; I think
this is a more rationale interface.
There is a long laundry list of other changes:
- Remove unused #defines for PyList_SET_ITEM() etc.
- Make some of the indentation consistent
- Replace uses of cPickle_PyMapping_HasKey() where the first
argument is self->memo with calls to PyDict_GetItem(), because
self->memo must be a dictionary.
- Don't bother to check if cPickle_PyMapping_HasKey() returns < 0,
because it can only return 0 or 1.
- Replace uses of PyObject_CallObject() with PyObject_Call(), when
we can guarantee that the argument tuple is really a tuple.
Performance impacts of these changes:
- 5% speedup for normal pickling
- No change to fast-mode pickling.
XXX Really need tests for all the features in cPickle that aren't in
pickle.
2001-10-12 01:11:06 -03:00
|
|
|
static int
|
|
|
|
Pickler_set_pers_func(Picklerobject *p, PyObject *v)
|
|
|
|
{
|
2002-04-01 13:40:08 -04:00
|
|
|
if (v == NULL) {
|
|
|
|
PyErr_SetString(PyExc_TypeError,
|
|
|
|
"attribute deletion is not supported");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
Py_XDECREF(p->pers_func);
|
|
|
|
Py_INCREF(v);
|
|
|
|
p->pers_func = v;
|
|
|
|
return 0;
|
Progress on SF bug #466175 and general cleanup.
Add a fast_container member to Picklerobject. If fast is true, then
fast_container counts the depth of nested container calls. If the
depth exceeds FAST_LIMIT (2000), the fast flag is ignored and the
normal checks occur. This approach is much like the approach for
prevent stack overflow for comparison and reprs of recursive objects
(e.g. [[...]]).
- Fast container used for save_list(), save_dict(), and
save_inst().
XXX Not clear which other save_xxx() functions should use it.
Make Picklerobject into new-style types, using PyObject_GenericGetAttr()
and PyObject_GenericSetAttr().
- Use PyMemberDef for binary and fast members
- Use PyGetSetDef for persistent_id, inst_persistent_id, memo, and
PicklingError.
XXX Not all of these seem like they need to use getset, but it's
not clear why the old getattr() and setattr() had such odd
semantics. One change is that the getvalue() attribute will
exist on all Picklers, not just list-based picklers; I think
this is a more rationale interface.
There is a long laundry list of other changes:
- Remove unused #defines for PyList_SET_ITEM() etc.
- Make some of the indentation consistent
- Replace uses of cPickle_PyMapping_HasKey() where the first
argument is self->memo with calls to PyDict_GetItem(), because
self->memo must be a dictionary.
- Don't bother to check if cPickle_PyMapping_HasKey() returns < 0,
because it can only return 0 or 1.
- Replace uses of PyObject_CallObject() with PyObject_Call(), when
we can guarantee that the argument tuple is really a tuple.
Performance impacts of these changes:
- 5% speedup for normal pickling
- No change to fast-mode pickling.
XXX Really need tests for all the features in cPickle that aren't in
pickle.
2001-10-12 01:11:06 -03:00
|
|
|
}
|
2001-03-17 00:50:51 -04:00
|
|
|
|
Progress on SF bug #466175 and general cleanup.
Add a fast_container member to Picklerobject. If fast is true, then
fast_container counts the depth of nested container calls. If the
depth exceeds FAST_LIMIT (2000), the fast flag is ignored and the
normal checks occur. This approach is much like the approach for
prevent stack overflow for comparison and reprs of recursive objects
(e.g. [[...]]).
- Fast container used for save_list(), save_dict(), and
save_inst().
XXX Not clear which other save_xxx() functions should use it.
Make Picklerobject into new-style types, using PyObject_GenericGetAttr()
and PyObject_GenericSetAttr().
- Use PyMemberDef for binary and fast members
- Use PyGetSetDef for persistent_id, inst_persistent_id, memo, and
PicklingError.
XXX Not all of these seem like they need to use getset, but it's
not clear why the old getattr() and setattr() had such odd
semantics. One change is that the getvalue() attribute will
exist on all Picklers, not just list-based picklers; I think
this is a more rationale interface.
There is a long laundry list of other changes:
- Remove unused #defines for PyList_SET_ITEM() etc.
- Make some of the indentation consistent
- Replace uses of cPickle_PyMapping_HasKey() where the first
argument is self->memo with calls to PyDict_GetItem(), because
self->memo must be a dictionary.
- Don't bother to check if cPickle_PyMapping_HasKey() returns < 0,
because it can only return 0 or 1.
- Replace uses of PyObject_CallObject() with PyObject_Call(), when
we can guarantee that the argument tuple is really a tuple.
Performance impacts of these changes:
- 5% speedup for normal pickling
- No change to fast-mode pickling.
XXX Really need tests for all the features in cPickle that aren't in
pickle.
2001-10-12 01:11:06 -03:00
|
|
|
static int
|
|
|
|
Pickler_set_inst_pers_func(Picklerobject *p, PyObject *v)
|
|
|
|
{
|
2002-04-01 13:40:08 -04:00
|
|
|
if (v == NULL) {
|
|
|
|
PyErr_SetString(PyExc_TypeError,
|
|
|
|
"attribute deletion is not supported");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
Py_XDECREF(p->inst_pers_func);
|
|
|
|
Py_INCREF(v);
|
|
|
|
p->inst_pers_func = v;
|
|
|
|
return 0;
|
Progress on SF bug #466175 and general cleanup.
Add a fast_container member to Picklerobject. If fast is true, then
fast_container counts the depth of nested container calls. If the
depth exceeds FAST_LIMIT (2000), the fast flag is ignored and the
normal checks occur. This approach is much like the approach for
prevent stack overflow for comparison and reprs of recursive objects
(e.g. [[...]]).
- Fast container used for save_list(), save_dict(), and
save_inst().
XXX Not clear which other save_xxx() functions should use it.
Make Picklerobject into new-style types, using PyObject_GenericGetAttr()
and PyObject_GenericSetAttr().
- Use PyMemberDef for binary and fast members
- Use PyGetSetDef for persistent_id, inst_persistent_id, memo, and
PicklingError.
XXX Not all of these seem like they need to use getset, but it's
not clear why the old getattr() and setattr() had such odd
semantics. One change is that the getvalue() attribute will
exist on all Picklers, not just list-based picklers; I think
this is a more rationale interface.
There is a long laundry list of other changes:
- Remove unused #defines for PyList_SET_ITEM() etc.
- Make some of the indentation consistent
- Replace uses of cPickle_PyMapping_HasKey() where the first
argument is self->memo with calls to PyDict_GetItem(), because
self->memo must be a dictionary.
- Don't bother to check if cPickle_PyMapping_HasKey() returns < 0,
because it can only return 0 or 1.
- Replace uses of PyObject_CallObject() with PyObject_Call(), when
we can guarantee that the argument tuple is really a tuple.
Performance impacts of these changes:
- 5% speedup for normal pickling
- No change to fast-mode pickling.
XXX Really need tests for all the features in cPickle that aren't in
pickle.
2001-10-12 01:11:06 -03:00
|
|
|
}
|
1997-04-09 14:36:32 -03:00
|
|
|
|
Progress on SF bug #466175 and general cleanup.
Add a fast_container member to Picklerobject. If fast is true, then
fast_container counts the depth of nested container calls. If the
depth exceeds FAST_LIMIT (2000), the fast flag is ignored and the
normal checks occur. This approach is much like the approach for
prevent stack overflow for comparison and reprs of recursive objects
(e.g. [[...]]).
- Fast container used for save_list(), save_dict(), and
save_inst().
XXX Not clear which other save_xxx() functions should use it.
Make Picklerobject into new-style types, using PyObject_GenericGetAttr()
and PyObject_GenericSetAttr().
- Use PyMemberDef for binary and fast members
- Use PyGetSetDef for persistent_id, inst_persistent_id, memo, and
PicklingError.
XXX Not all of these seem like they need to use getset, but it's
not clear why the old getattr() and setattr() had such odd
semantics. One change is that the getvalue() attribute will
exist on all Picklers, not just list-based picklers; I think
this is a more rationale interface.
There is a long laundry list of other changes:
- Remove unused #defines for PyList_SET_ITEM() etc.
- Make some of the indentation consistent
- Replace uses of cPickle_PyMapping_HasKey() where the first
argument is self->memo with calls to PyDict_GetItem(), because
self->memo must be a dictionary.
- Don't bother to check if cPickle_PyMapping_HasKey() returns < 0,
because it can only return 0 or 1.
- Replace uses of PyObject_CallObject() with PyObject_Call(), when
we can guarantee that the argument tuple is really a tuple.
Performance impacts of these changes:
- 5% speedup for normal pickling
- No change to fast-mode pickling.
XXX Really need tests for all the features in cPickle that aren't in
pickle.
2001-10-12 01:11:06 -03:00
|
|
|
static PyObject *
|
|
|
|
Pickler_get_memo(Picklerobject *p)
|
|
|
|
{
|
2002-04-01 13:40:08 -04:00
|
|
|
if (p->memo == NULL)
|
|
|
|
PyErr_SetString(PyExc_AttributeError, "memo");
|
|
|
|
else
|
|
|
|
Py_INCREF(p->memo);
|
|
|
|
return p->memo;
|
Progress on SF bug #466175 and general cleanup.
Add a fast_container member to Picklerobject. If fast is true, then
fast_container counts the depth of nested container calls. If the
depth exceeds FAST_LIMIT (2000), the fast flag is ignored and the
normal checks occur. This approach is much like the approach for
prevent stack overflow for comparison and reprs of recursive objects
(e.g. [[...]]).
- Fast container used for save_list(), save_dict(), and
save_inst().
XXX Not clear which other save_xxx() functions should use it.
Make Picklerobject into new-style types, using PyObject_GenericGetAttr()
and PyObject_GenericSetAttr().
- Use PyMemberDef for binary and fast members
- Use PyGetSetDef for persistent_id, inst_persistent_id, memo, and
PicklingError.
XXX Not all of these seem like they need to use getset, but it's
not clear why the old getattr() and setattr() had such odd
semantics. One change is that the getvalue() attribute will
exist on all Picklers, not just list-based picklers; I think
this is a more rationale interface.
There is a long laundry list of other changes:
- Remove unused #defines for PyList_SET_ITEM() etc.
- Make some of the indentation consistent
- Replace uses of cPickle_PyMapping_HasKey() where the first
argument is self->memo with calls to PyDict_GetItem(), because
self->memo must be a dictionary.
- Don't bother to check if cPickle_PyMapping_HasKey() returns < 0,
because it can only return 0 or 1.
- Replace uses of PyObject_CallObject() with PyObject_Call(), when
we can guarantee that the argument tuple is really a tuple.
Performance impacts of these changes:
- 5% speedup for normal pickling
- No change to fast-mode pickling.
XXX Really need tests for all the features in cPickle that aren't in
pickle.
2001-10-12 01:11:06 -03:00
|
|
|
}
|
1998-08-13 20:13:52 -03:00
|
|
|
|
Progress on SF bug #466175 and general cleanup.
Add a fast_container member to Picklerobject. If fast is true, then
fast_container counts the depth of nested container calls. If the
depth exceeds FAST_LIMIT (2000), the fast flag is ignored and the
normal checks occur. This approach is much like the approach for
prevent stack overflow for comparison and reprs of recursive objects
(e.g. [[...]]).
- Fast container used for save_list(), save_dict(), and
save_inst().
XXX Not clear which other save_xxx() functions should use it.
Make Picklerobject into new-style types, using PyObject_GenericGetAttr()
and PyObject_GenericSetAttr().
- Use PyMemberDef for binary and fast members
- Use PyGetSetDef for persistent_id, inst_persistent_id, memo, and
PicklingError.
XXX Not all of these seem like they need to use getset, but it's
not clear why the old getattr() and setattr() had such odd
semantics. One change is that the getvalue() attribute will
exist on all Picklers, not just list-based picklers; I think
this is a more rationale interface.
There is a long laundry list of other changes:
- Remove unused #defines for PyList_SET_ITEM() etc.
- Make some of the indentation consistent
- Replace uses of cPickle_PyMapping_HasKey() where the first
argument is self->memo with calls to PyDict_GetItem(), because
self->memo must be a dictionary.
- Don't bother to check if cPickle_PyMapping_HasKey() returns < 0,
because it can only return 0 or 1.
- Replace uses of PyObject_CallObject() with PyObject_Call(), when
we can guarantee that the argument tuple is really a tuple.
Performance impacts of these changes:
- 5% speedup for normal pickling
- No change to fast-mode pickling.
XXX Really need tests for all the features in cPickle that aren't in
pickle.
2001-10-12 01:11:06 -03:00
|
|
|
static int
|
|
|
|
Pickler_set_memo(Picklerobject *p, PyObject *v)
|
|
|
|
{
|
2002-04-01 13:40:08 -04:00
|
|
|
if (v == NULL) {
|
|
|
|
PyErr_SetString(PyExc_TypeError,
|
|
|
|
"attribute deletion is not supported");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (!PyDict_Check(v)) {
|
|
|
|
PyErr_SetString(PyExc_TypeError, "memo must be a dictionary");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
Py_XDECREF(p->memo);
|
|
|
|
Py_INCREF(v);
|
|
|
|
p->memo = v;
|
|
|
|
return 0;
|
Progress on SF bug #466175 and general cleanup.
Add a fast_container member to Picklerobject. If fast is true, then
fast_container counts the depth of nested container calls. If the
depth exceeds FAST_LIMIT (2000), the fast flag is ignored and the
normal checks occur. This approach is much like the approach for
prevent stack overflow for comparison and reprs of recursive objects
(e.g. [[...]]).
- Fast container used for save_list(), save_dict(), and
save_inst().
XXX Not clear which other save_xxx() functions should use it.
Make Picklerobject into new-style types, using PyObject_GenericGetAttr()
and PyObject_GenericSetAttr().
- Use PyMemberDef for binary and fast members
- Use PyGetSetDef for persistent_id, inst_persistent_id, memo, and
PicklingError.
XXX Not all of these seem like they need to use getset, but it's
not clear why the old getattr() and setattr() had such odd
semantics. One change is that the getvalue() attribute will
exist on all Picklers, not just list-based picklers; I think
this is a more rationale interface.
There is a long laundry list of other changes:
- Remove unused #defines for PyList_SET_ITEM() etc.
- Make some of the indentation consistent
- Replace uses of cPickle_PyMapping_HasKey() where the first
argument is self->memo with calls to PyDict_GetItem(), because
self->memo must be a dictionary.
- Don't bother to check if cPickle_PyMapping_HasKey() returns < 0,
because it can only return 0 or 1.
- Replace uses of PyObject_CallObject() with PyObject_Call(), when
we can guarantee that the argument tuple is really a tuple.
Performance impacts of these changes:
- 5% speedup for normal pickling
- No change to fast-mode pickling.
XXX Really need tests for all the features in cPickle that aren't in
pickle.
2001-10-12 01:11:06 -03:00
|
|
|
}
|
1998-08-13 20:13:52 -03:00
|
|
|
|
Progress on SF bug #466175 and general cleanup.
Add a fast_container member to Picklerobject. If fast is true, then
fast_container counts the depth of nested container calls. If the
depth exceeds FAST_LIMIT (2000), the fast flag is ignored and the
normal checks occur. This approach is much like the approach for
prevent stack overflow for comparison and reprs of recursive objects
(e.g. [[...]]).
- Fast container used for save_list(), save_dict(), and
save_inst().
XXX Not clear which other save_xxx() functions should use it.
Make Picklerobject into new-style types, using PyObject_GenericGetAttr()
and PyObject_GenericSetAttr().
- Use PyMemberDef for binary and fast members
- Use PyGetSetDef for persistent_id, inst_persistent_id, memo, and
PicklingError.
XXX Not all of these seem like they need to use getset, but it's
not clear why the old getattr() and setattr() had such odd
semantics. One change is that the getvalue() attribute will
exist on all Picklers, not just list-based picklers; I think
this is a more rationale interface.
There is a long laundry list of other changes:
- Remove unused #defines for PyList_SET_ITEM() etc.
- Make some of the indentation consistent
- Replace uses of cPickle_PyMapping_HasKey() where the first
argument is self->memo with calls to PyDict_GetItem(), because
self->memo must be a dictionary.
- Don't bother to check if cPickle_PyMapping_HasKey() returns < 0,
because it can only return 0 or 1.
- Replace uses of PyObject_CallObject() with PyObject_Call(), when
we can guarantee that the argument tuple is really a tuple.
Performance impacts of these changes:
- 5% speedup for normal pickling
- No change to fast-mode pickling.
XXX Really need tests for all the features in cPickle that aren't in
pickle.
2001-10-12 01:11:06 -03:00
|
|
|
static PyObject *
|
|
|
|
Pickler_get_error(Picklerobject *p)
|
|
|
|
{
|
2002-04-01 13:40:08 -04:00
|
|
|
/* why is this an attribute on the Pickler? */
|
|
|
|
Py_INCREF(PicklingError);
|
|
|
|
return PicklingError;
|
1997-01-06 18:59:08 -04:00
|
|
|
}
|
|
|
|
|
Progress on SF bug #466175 and general cleanup.
Add a fast_container member to Picklerobject. If fast is true, then
fast_container counts the depth of nested container calls. If the
depth exceeds FAST_LIMIT (2000), the fast flag is ignored and the
normal checks occur. This approach is much like the approach for
prevent stack overflow for comparison and reprs of recursive objects
(e.g. [[...]]).
- Fast container used for save_list(), save_dict(), and
save_inst().
XXX Not clear which other save_xxx() functions should use it.
Make Picklerobject into new-style types, using PyObject_GenericGetAttr()
and PyObject_GenericSetAttr().
- Use PyMemberDef for binary and fast members
- Use PyGetSetDef for persistent_id, inst_persistent_id, memo, and
PicklingError.
XXX Not all of these seem like they need to use getset, but it's
not clear why the old getattr() and setattr() had such odd
semantics. One change is that the getvalue() attribute will
exist on all Picklers, not just list-based picklers; I think
this is a more rationale interface.
There is a long laundry list of other changes:
- Remove unused #defines for PyList_SET_ITEM() etc.
- Make some of the indentation consistent
- Replace uses of cPickle_PyMapping_HasKey() where the first
argument is self->memo with calls to PyDict_GetItem(), because
self->memo must be a dictionary.
- Don't bother to check if cPickle_PyMapping_HasKey() returns < 0,
because it can only return 0 or 1.
- Replace uses of PyObject_CallObject() with PyObject_Call(), when
we can guarantee that the argument tuple is really a tuple.
Performance impacts of these changes:
- 5% speedup for normal pickling
- No change to fast-mode pickling.
XXX Really need tests for all the features in cPickle that aren't in
pickle.
2001-10-12 01:11:06 -03:00
|
|
|
static PyMemberDef Pickler_members[] = {
|
|
|
|
{"binary", T_INT, offsetof(Picklerobject, bin)},
|
|
|
|
{"fast", T_INT, offsetof(Picklerobject, fast)},
|
2001-10-16 14:10:49 -03:00
|
|
|
{NULL}
|
Progress on SF bug #466175 and general cleanup.
Add a fast_container member to Picklerobject. If fast is true, then
fast_container counts the depth of nested container calls. If the
depth exceeds FAST_LIMIT (2000), the fast flag is ignored and the
normal checks occur. This approach is much like the approach for
prevent stack overflow for comparison and reprs of recursive objects
(e.g. [[...]]).
- Fast container used for save_list(), save_dict(), and
save_inst().
XXX Not clear which other save_xxx() functions should use it.
Make Picklerobject into new-style types, using PyObject_GenericGetAttr()
and PyObject_GenericSetAttr().
- Use PyMemberDef for binary and fast members
- Use PyGetSetDef for persistent_id, inst_persistent_id, memo, and
PicklingError.
XXX Not all of these seem like they need to use getset, but it's
not clear why the old getattr() and setattr() had such odd
semantics. One change is that the getvalue() attribute will
exist on all Picklers, not just list-based picklers; I think
this is a more rationale interface.
There is a long laundry list of other changes:
- Remove unused #defines for PyList_SET_ITEM() etc.
- Make some of the indentation consistent
- Replace uses of cPickle_PyMapping_HasKey() where the first
argument is self->memo with calls to PyDict_GetItem(), because
self->memo must be a dictionary.
- Don't bother to check if cPickle_PyMapping_HasKey() returns < 0,
because it can only return 0 or 1.
- Replace uses of PyObject_CallObject() with PyObject_Call(), when
we can guarantee that the argument tuple is really a tuple.
Performance impacts of these changes:
- 5% speedup for normal pickling
- No change to fast-mode pickling.
XXX Really need tests for all the features in cPickle that aren't in
pickle.
2001-10-12 01:11:06 -03:00
|
|
|
};
|
|
|
|
|
|
|
|
static PyGetSetDef Pickler_getsets[] = {
|
2003-02-01 02:24:36 -04:00
|
|
|
{"persistent_id", (getter)Pickler_get_pers_func,
|
Progress on SF bug #466175 and general cleanup.
Add a fast_container member to Picklerobject. If fast is true, then
fast_container counts the depth of nested container calls. If the
depth exceeds FAST_LIMIT (2000), the fast flag is ignored and the
normal checks occur. This approach is much like the approach for
prevent stack overflow for comparison and reprs of recursive objects
(e.g. [[...]]).
- Fast container used for save_list(), save_dict(), and
save_inst().
XXX Not clear which other save_xxx() functions should use it.
Make Picklerobject into new-style types, using PyObject_GenericGetAttr()
and PyObject_GenericSetAttr().
- Use PyMemberDef for binary and fast members
- Use PyGetSetDef for persistent_id, inst_persistent_id, memo, and
PicklingError.
XXX Not all of these seem like they need to use getset, but it's
not clear why the old getattr() and setattr() had such odd
semantics. One change is that the getvalue() attribute will
exist on all Picklers, not just list-based picklers; I think
this is a more rationale interface.
There is a long laundry list of other changes:
- Remove unused #defines for PyList_SET_ITEM() etc.
- Make some of the indentation consistent
- Replace uses of cPickle_PyMapping_HasKey() where the first
argument is self->memo with calls to PyDict_GetItem(), because
self->memo must be a dictionary.
- Don't bother to check if cPickle_PyMapping_HasKey() returns < 0,
because it can only return 0 or 1.
- Replace uses of PyObject_CallObject() with PyObject_Call(), when
we can guarantee that the argument tuple is really a tuple.
Performance impacts of these changes:
- 5% speedup for normal pickling
- No change to fast-mode pickling.
XXX Really need tests for all the features in cPickle that aren't in
pickle.
2001-10-12 01:11:06 -03:00
|
|
|
(setter)Pickler_set_pers_func},
|
|
|
|
{"inst_persistent_id", NULL, (setter)Pickler_set_inst_pers_func},
|
|
|
|
{"memo", (getter)Pickler_get_memo, (setter)Pickler_set_memo},
|
2001-10-16 14:10:49 -03:00
|
|
|
{"PicklingError", (getter)Pickler_get_error, NULL},
|
|
|
|
{NULL}
|
Progress on SF bug #466175 and general cleanup.
Add a fast_container member to Picklerobject. If fast is true, then
fast_container counts the depth of nested container calls. If the
depth exceeds FAST_LIMIT (2000), the fast flag is ignored and the
normal checks occur. This approach is much like the approach for
prevent stack overflow for comparison and reprs of recursive objects
(e.g. [[...]]).
- Fast container used for save_list(), save_dict(), and
save_inst().
XXX Not clear which other save_xxx() functions should use it.
Make Picklerobject into new-style types, using PyObject_GenericGetAttr()
and PyObject_GenericSetAttr().
- Use PyMemberDef for binary and fast members
- Use PyGetSetDef for persistent_id, inst_persistent_id, memo, and
PicklingError.
XXX Not all of these seem like they need to use getset, but it's
not clear why the old getattr() and setattr() had such odd
semantics. One change is that the getvalue() attribute will
exist on all Picklers, not just list-based picklers; I think
this is a more rationale interface.
There is a long laundry list of other changes:
- Remove unused #defines for PyList_SET_ITEM() etc.
- Make some of the indentation consistent
- Replace uses of cPickle_PyMapping_HasKey() where the first
argument is self->memo with calls to PyDict_GetItem(), because
self->memo must be a dictionary.
- Don't bother to check if cPickle_PyMapping_HasKey() returns < 0,
because it can only return 0 or 1.
- Replace uses of PyObject_CallObject() with PyObject_Call(), when
we can guarantee that the argument tuple is really a tuple.
Performance impacts of these changes:
- 5% speedup for normal pickling
- No change to fast-mode pickling.
XXX Really need tests for all the features in cPickle that aren't in
pickle.
2001-10-12 01:11:06 -03:00
|
|
|
};
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2002-06-13 17:33:02 -03:00
|
|
|
PyDoc_STRVAR(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*/
|
2001-12-08 14:02:58 -04:00
|
|
|
"cPickle.Pickler", /*tp_name*/
|
Progress on SF bug #466175 and general cleanup.
Add a fast_container member to Picklerobject. If fast is true, then
fast_container counts the depth of nested container calls. If the
depth exceeds FAST_LIMIT (2000), the fast flag is ignored and the
normal checks occur. This approach is much like the approach for
prevent stack overflow for comparison and reprs of recursive objects
(e.g. [[...]]).
- Fast container used for save_list(), save_dict(), and
save_inst().
XXX Not clear which other save_xxx() functions should use it.
Make Picklerobject into new-style types, using PyObject_GenericGetAttr()
and PyObject_GenericSetAttr().
- Use PyMemberDef for binary and fast members
- Use PyGetSetDef for persistent_id, inst_persistent_id, memo, and
PicklingError.
XXX Not all of these seem like they need to use getset, but it's
not clear why the old getattr() and setattr() had such odd
semantics. One change is that the getvalue() attribute will
exist on all Picklers, not just list-based picklers; I think
this is a more rationale interface.
There is a long laundry list of other changes:
- Remove unused #defines for PyList_SET_ITEM() etc.
- Make some of the indentation consistent
- Replace uses of cPickle_PyMapping_HasKey() where the first
argument is self->memo with calls to PyDict_GetItem(), because
self->memo must be a dictionary.
- Don't bother to check if cPickle_PyMapping_HasKey() returns < 0,
because it can only return 0 or 1.
- Replace uses of PyObject_CallObject() with PyObject_Call(), when
we can guarantee that the argument tuple is really a tuple.
Performance impacts of these changes:
- 5% speedup for normal pickling
- No change to fast-mode pickling.
XXX Really need tests for all the features in cPickle that aren't in
pickle.
2001-10-12 01:11:06 -03:00
|
|
|
sizeof(Picklerobject), /*tp_basicsize*/
|
|
|
|
0,
|
|
|
|
(destructor)Pickler_dealloc, /* tp_dealloc */
|
|
|
|
0, /* tp_print */
|
|
|
|
0, /* tp_getattr */
|
|
|
|
0, /* tp_setattr */
|
|
|
|
0, /* tp_compare */
|
|
|
|
0, /* tp_repr */
|
|
|
|
0, /* tp_as_number */
|
|
|
|
0, /* tp_as_sequence */
|
|
|
|
0, /* tp_as_mapping */
|
|
|
|
0, /* tp_hash */
|
|
|
|
0, /* tp_call */
|
|
|
|
0, /* tp_str */
|
2003-01-06 08:41:26 -04:00
|
|
|
PyObject_GenericGetAttr, /* tp_getattro */
|
|
|
|
PyObject_GenericSetAttr, /* tp_setattro */
|
Progress on SF bug #466175 and general cleanup.
Add a fast_container member to Picklerobject. If fast is true, then
fast_container counts the depth of nested container calls. If the
depth exceeds FAST_LIMIT (2000), the fast flag is ignored and the
normal checks occur. This approach is much like the approach for
prevent stack overflow for comparison and reprs of recursive objects
(e.g. [[...]]).
- Fast container used for save_list(), save_dict(), and
save_inst().
XXX Not clear which other save_xxx() functions should use it.
Make Picklerobject into new-style types, using PyObject_GenericGetAttr()
and PyObject_GenericSetAttr().
- Use PyMemberDef for binary and fast members
- Use PyGetSetDef for persistent_id, inst_persistent_id, memo, and
PicklingError.
XXX Not all of these seem like they need to use getset, but it's
not clear why the old getattr() and setattr() had such odd
semantics. One change is that the getvalue() attribute will
exist on all Picklers, not just list-based picklers; I think
this is a more rationale interface.
There is a long laundry list of other changes:
- Remove unused #defines for PyList_SET_ITEM() etc.
- Make some of the indentation consistent
- Replace uses of cPickle_PyMapping_HasKey() where the first
argument is self->memo with calls to PyDict_GetItem(), because
self->memo must be a dictionary.
- Don't bother to check if cPickle_PyMapping_HasKey() returns < 0,
because it can only return 0 or 1.
- Replace uses of PyObject_CallObject() with PyObject_Call(), when
we can guarantee that the argument tuple is really a tuple.
Performance impacts of these changes:
- 5% speedup for normal pickling
- No change to fast-mode pickling.
XXX Really need tests for all the features in cPickle that aren't in
pickle.
2001-10-12 01:11:06 -03:00
|
|
|
0, /* tp_as_buffer */
|
2003-04-09 18:05:12 -03:00
|
|
|
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
|
Progress on SF bug #466175 and general cleanup.
Add a fast_container member to Picklerobject. If fast is true, then
fast_container counts the depth of nested container calls. If the
depth exceeds FAST_LIMIT (2000), the fast flag is ignored and the
normal checks occur. This approach is much like the approach for
prevent stack overflow for comparison and reprs of recursive objects
(e.g. [[...]]).
- Fast container used for save_list(), save_dict(), and
save_inst().
XXX Not clear which other save_xxx() functions should use it.
Make Picklerobject into new-style types, using PyObject_GenericGetAttr()
and PyObject_GenericSetAttr().
- Use PyMemberDef for binary and fast members
- Use PyGetSetDef for persistent_id, inst_persistent_id, memo, and
PicklingError.
XXX Not all of these seem like they need to use getset, but it's
not clear why the old getattr() and setattr() had such odd
semantics. One change is that the getvalue() attribute will
exist on all Picklers, not just list-based picklers; I think
this is a more rationale interface.
There is a long laundry list of other changes:
- Remove unused #defines for PyList_SET_ITEM() etc.
- Make some of the indentation consistent
- Replace uses of cPickle_PyMapping_HasKey() where the first
argument is self->memo with calls to PyDict_GetItem(), because
self->memo must be a dictionary.
- Don't bother to check if cPickle_PyMapping_HasKey() returns < 0,
because it can only return 0 or 1.
- Replace uses of PyObject_CallObject() with PyObject_Call(), when
we can guarantee that the argument tuple is really a tuple.
Performance impacts of these changes:
- 5% speedup for normal pickling
- No change to fast-mode pickling.
XXX Really need tests for all the features in cPickle that aren't in
pickle.
2001-10-12 01:11:06 -03:00
|
|
|
Picklertype__doc__, /* tp_doc */
|
2003-04-09 18:05:12 -03:00
|
|
|
(traverseproc)Pickler_traverse, /* tp_traverse */
|
|
|
|
(inquiry)Pickler_clear, /* tp_clear */
|
Progress on SF bug #466175 and general cleanup.
Add a fast_container member to Picklerobject. If fast is true, then
fast_container counts the depth of nested container calls. If the
depth exceeds FAST_LIMIT (2000), the fast flag is ignored and the
normal checks occur. This approach is much like the approach for
prevent stack overflow for comparison and reprs of recursive objects
(e.g. [[...]]).
- Fast container used for save_list(), save_dict(), and
save_inst().
XXX Not clear which other save_xxx() functions should use it.
Make Picklerobject into new-style types, using PyObject_GenericGetAttr()
and PyObject_GenericSetAttr().
- Use PyMemberDef for binary and fast members
- Use PyGetSetDef for persistent_id, inst_persistent_id, memo, and
PicklingError.
XXX Not all of these seem like they need to use getset, but it's
not clear why the old getattr() and setattr() had such odd
semantics. One change is that the getvalue() attribute will
exist on all Picklers, not just list-based picklers; I think
this is a more rationale interface.
There is a long laundry list of other changes:
- Remove unused #defines for PyList_SET_ITEM() etc.
- Make some of the indentation consistent
- Replace uses of cPickle_PyMapping_HasKey() where the first
argument is self->memo with calls to PyDict_GetItem(), because
self->memo must be a dictionary.
- Don't bother to check if cPickle_PyMapping_HasKey() returns < 0,
because it can only return 0 or 1.
- Replace uses of PyObject_CallObject() with PyObject_Call(), when
we can guarantee that the argument tuple is really a tuple.
Performance impacts of these changes:
- 5% speedup for normal pickling
- No change to fast-mode pickling.
XXX Really need tests for all the features in cPickle that aren't in
pickle.
2001-10-12 01:11:06 -03:00
|
|
|
0, /* tp_richcompare */
|
|
|
|
0, /* tp_weaklistoffset */
|
|
|
|
0, /* tp_iter */
|
|
|
|
0, /* tp_iternext */
|
|
|
|
Pickler_methods, /* tp_methods */
|
|
|
|
Pickler_members, /* tp_members */
|
|
|
|
Pickler_getsets, /* tp_getset */
|
1997-12-08 11:15:16 -04:00
|
|
|
};
|
1997-04-09 14:36:32 -03:00
|
|
|
|
|
|
|
static PyObject *
|
2003-02-01 02:24:36 -04:00
|
|
|
find_class(PyObject *py_module_name, PyObject *py_global_name, PyObject *fc)
|
2002-04-01 13:40:08 -04:00
|
|
|
{
|
|
|
|
PyObject *global = 0, *module;
|
|
|
|
|
|
|
|
if (fc) {
|
|
|
|
if (fc==Py_None) {
|
2003-02-13 19:00:26 -04:00
|
|
|
PyErr_SetString(UnpicklingError, "Global and instance "
|
|
|
|
"pickles are not supported.");
|
2002-04-01 13:40:08 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
2006-05-25 16:15:31 -03:00
|
|
|
return PyObject_CallFunctionObjArgs(fc, py_module_name,
|
|
|
|
py_global_name, NULL);
|
2002-04-01 13:40:08 -04:00
|
|
|
}
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
module = PySys_GetObject("modules");
|
|
|
|
if (module == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
module = PyDict_GetItem(module, py_module_name);
|
|
|
|
if (module == NULL) {
|
|
|
|
module = PyImport_Import(py_module_name);
|
|
|
|
if (!module)
|
|
|
|
return NULL;
|
|
|
|
global = PyObject_GetAttr(module, py_global_name);
|
|
|
|
Py_DECREF(module);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
global = PyObject_GetAttr(module, py_global_name);
|
|
|
|
return global;
|
1997-01-06 18:59:08 -04:00
|
|
|
}
|
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
static int
|
2003-02-01 02:24:36 -04:00
|
|
|
marker(Unpicklerobject *self)
|
2002-04-01 13:40:08 -04:00
|
|
|
{
|
|
|
|
if (self->num_marks < 1) {
|
|
|
|
PyErr_SetString(UnpicklingError, "could not find MARK");
|
|
|
|
return -1;
|
|
|
|
}
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2002-04-01 13:40:08 -04: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
|
2003-02-01 02:24:36 -04:00
|
|
|
load_none(Unpicklerobject *self)
|
2002-04-01 13:40:08 -04:00
|
|
|
{
|
|
|
|
PDATA_APPEND(self->stack, Py_None, -1);
|
|
|
|
return 0;
|
1997-01-06 18:59:08 -04:00
|
|
|
}
|
|
|
|
|
1999-02-04 10:54:04 -04:00
|
|
|
static int
|
2003-02-01 02:24:36 -04:00
|
|
|
bad_readline(void)
|
2002-04-01 13:40:08 -04:00
|
|
|
{
|
|
|
|
PyErr_SetString(UnpicklingError, "pickle data was truncated");
|
|
|
|
return -1;
|
1999-02-04 10:54:04 -04:00
|
|
|
}
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
static int
|
2003-02-01 02:24:36 -04:00
|
|
|
load_int(Unpicklerobject *self)
|
2002-04-01 13:40:08 -04:00
|
|
|
{
|
|
|
|
PyObject *py_int = 0;
|
|
|
|
char *endptr, *s;
|
|
|
|
int len, res = -1;
|
|
|
|
long l;
|
1997-04-09 14:36:32 -03:00
|
|
|
|
2003-02-02 14:29:33 -04:00
|
|
|
if ((len = self->readline_func(self, &s)) < 0) return -1;
|
2002-04-01 13:40:08 -04:00
|
|
|
if (len < 2) return bad_readline();
|
|
|
|
if (!( s=pystrndup(s,len))) return -1;
|
|
|
|
|
|
|
|
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
|
|
|
|
it as a Python long object. */
|
|
|
|
errno = 0;
|
|
|
|
py_int = PyLong_FromString(s, NULL, 0);
|
|
|
|
if (py_int == NULL) {
|
|
|
|
PyErr_SetString(PyExc_ValueError,
|
|
|
|
"could not convert string to int");
|
|
|
|
goto finally;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2002-04-05 15:30:08 -04:00
|
|
|
if (len == 3 && (l == 0 || l == 1)) {
|
|
|
|
if (!( py_int = PyBool_FromLong(l))) goto finally;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (!( py_int = PyInt_FromLong(l))) goto finally;
|
|
|
|
}
|
2002-04-01 13:40:08 -04:00
|
|
|
}
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
free(s);
|
|
|
|
PDATA_PUSH(self->stack, py_int, -1);
|
|
|
|
return 0;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
finally:
|
|
|
|
free(s);
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
return res;
|
1997-04-09 14:36:32 -03:00
|
|
|
}
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2003-02-02 13:59:11 -04:00
|
|
|
static int
|
|
|
|
load_bool(Unpicklerobject *self, PyObject *boolean)
|
|
|
|
{
|
|
|
|
assert(boolean == Py_True || boolean == Py_False);
|
2003-02-03 11:45:56 -04:00
|
|
|
PDATA_APPEND(self->stack, boolean, -1);
|
2003-02-02 13:59:11 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
cPickle.c: Full support for the new LONG1 and LONG4. Added comments.
Assorted code cleanups; e.g., sizeof(char) is 1 by definition, so there's
no need to do things like multiply by sizeof(char) in hairy malloc
arguments. Fixed an undetected-overflow bug in readline_file().
longobject.c: Fixed a really stupid bug in the new _PyLong_NumBits.
pickle.py: Fixed stupid bug in save_long(): When proto is 2, it
wrote LONG1 or LONG4, but forgot to return then -- it went on to
append the proto 1 LONG opcode too.
Fixed equally stupid cancelling bugs in load_long1() and
load_long4(): they *returned* the unpickled long instead of pushing
it on the stack. The return values were ignored. Tests passed
before only because save_long() pickled the long twice.
Fixed bugs in encode_long().
Noted that decode_long() is quadratic-time despite our hopes,
because long(string, 16) is still quadratic-time in len(string).
It's hex() that's linear-time. I don't know a way to make decode_long()
linear-time in Python, short of maybe transforming the 256's-complement
bytes into marshal's funky internal format, and letting marshal decode
that. It would be more valuable to make long(string, 16) linear time.
pickletester.py: Added a global "protocols" vector so tests can try
all the protocols in a sane way. Changed test_ints() and test_unicode()
to do so. Added a new test_long(), but the tail end of it is disabled
because it "takes forever" under pickle.py (but runs very quickly under
cPickle: cPickle proto 2 for longs is linear-time).
2003-02-01 22:57:53 -04:00
|
|
|
/* s contains x bytes of a little-endian integer. Return its value as a
|
|
|
|
* C int. Obscure: when x is 1 or 2, this is an unsigned little-endian
|
|
|
|
* int, but when x is 4 it's a signed one. This is an historical source
|
|
|
|
* of x-platform bugs.
|
|
|
|
*/
|
2002-04-01 13:40:08 -04:00
|
|
|
static long
|
cPickle.c: Full support for the new LONG1 and LONG4. Added comments.
Assorted code cleanups; e.g., sizeof(char) is 1 by definition, so there's
no need to do things like multiply by sizeof(char) in hairy malloc
arguments. Fixed an undetected-overflow bug in readline_file().
longobject.c: Fixed a really stupid bug in the new _PyLong_NumBits.
pickle.py: Fixed stupid bug in save_long(): When proto is 2, it
wrote LONG1 or LONG4, but forgot to return then -- it went on to
append the proto 1 LONG opcode too.
Fixed equally stupid cancelling bugs in load_long1() and
load_long4(): they *returned* the unpickled long instead of pushing
it on the stack. The return values were ignored. Tests passed
before only because save_long() pickled the long twice.
Fixed bugs in encode_long().
Noted that decode_long() is quadratic-time despite our hopes,
because long(string, 16) is still quadratic-time in len(string).
It's hex() that's linear-time. I don't know a way to make decode_long()
linear-time in Python, short of maybe transforming the 256's-complement
bytes into marshal's funky internal format, and letting marshal decode
that. It would be more valuable to make long(string, 16) linear time.
pickletester.py: Added a global "protocols" vector so tests can try
all the protocols in a sane way. Changed test_ints() and test_unicode()
to do so. Added a new test_long(), but the tail end of it is disabled
because it "takes forever" under pickle.py (but runs very quickly under
cPickle: cPickle proto 2 for longs is linear-time).
2003-02-01 22:57:53 -04:00
|
|
|
calc_binint(char *s, int x)
|
2002-04-01 13:40:08 -04:00
|
|
|
{
|
|
|
|
unsigned char c;
|
|
|
|
int i;
|
|
|
|
long l;
|
|
|
|
|
|
|
|
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
|
2002-04-01 13:40:08 -04:00
|
|
|
/* 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;
|
2001-04-09 22:54:42 -03:00
|
|
|
#endif
|
2002-04-01 13:40:08 -04:00
|
|
|
return l;
|
1997-01-06 18:59:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
static int
|
2003-02-01 02:24:36 -04:00
|
|
|
load_binintx(Unpicklerobject *self, char *s, int x)
|
2002-04-01 13:40:08 -04:00
|
|
|
{
|
|
|
|
PyObject *py_int = 0;
|
|
|
|
long l;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
l = calc_binint(s, x);
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2003-02-01 02:24:36 -04:00
|
|
|
if (!( py_int = PyInt_FromLong(l)))
|
2002-04-01 13:40:08 -04:00
|
|
|
return -1;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
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
|
|
|
static int
|
2003-02-01 02:24:36 -04:00
|
|
|
load_binint(Unpicklerobject *self)
|
2002-04-01 13:40:08 -04:00
|
|
|
{
|
|
|
|
char *s;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2003-02-02 14:29:33 -04:00
|
|
|
if (self->read_func(self, &s, 4) < 0)
|
2002-04-01 13:40:08 -04:00
|
|
|
return -1;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
return load_binintx(self, s, 4);
|
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
|
2003-02-01 02:24:36 -04:00
|
|
|
load_binint1(Unpicklerobject *self)
|
2002-04-01 13:40:08 -04:00
|
|
|
{
|
|
|
|
char *s;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2003-02-02 14:29:33 -04:00
|
|
|
if (self->read_func(self, &s, 1) < 0)
|
2002-04-01 13:40:08 -04:00
|
|
|
return -1;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
return load_binintx(self, s, 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
|
2003-02-01 02:24:36 -04:00
|
|
|
load_binint2(Unpicklerobject *self)
|
2002-04-01 13:40:08 -04:00
|
|
|
{
|
|
|
|
char *s;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2003-02-02 14:29:33 -04:00
|
|
|
if (self->read_func(self, &s, 2) < 0)
|
2002-04-01 13:40:08 -04:00
|
|
|
return -1;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
return load_binintx(self, s, 2);
|
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 int
|
2003-02-01 02:24:36 -04:00
|
|
|
load_long(Unpicklerobject *self)
|
2002-04-01 13:40:08 -04:00
|
|
|
{
|
|
|
|
PyObject *l = 0;
|
|
|
|
char *end, *s;
|
|
|
|
int len, res = -1;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2003-02-02 14:29:33 -04:00
|
|
|
if ((len = self->readline_func(self, &s)) < 0) return -1;
|
2002-04-01 13:40:08 -04:00
|
|
|
if (len < 2) return bad_readline();
|
|
|
|
if (!( s=pystrndup(s,len))) return -1;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2003-02-01 02:24:36 -04:00
|
|
|
if (!( l = PyLong_FromString(s, &end, 0)))
|
2002-04-01 13:40:08 -04:00
|
|
|
goto finally;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
free(s);
|
|
|
|
PDATA_PUSH(self->stack, l, -1);
|
|
|
|
return 0;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
finally:
|
|
|
|
free(s);
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
return res;
|
1997-01-06 18:59:08 -04:00
|
|
|
}
|
|
|
|
|
cPickle.c: Full support for the new LONG1 and LONG4. Added comments.
Assorted code cleanups; e.g., sizeof(char) is 1 by definition, so there's
no need to do things like multiply by sizeof(char) in hairy malloc
arguments. Fixed an undetected-overflow bug in readline_file().
longobject.c: Fixed a really stupid bug in the new _PyLong_NumBits.
pickle.py: Fixed stupid bug in save_long(): When proto is 2, it
wrote LONG1 or LONG4, but forgot to return then -- it went on to
append the proto 1 LONG opcode too.
Fixed equally stupid cancelling bugs in load_long1() and
load_long4(): they *returned* the unpickled long instead of pushing
it on the stack. The return values were ignored. Tests passed
before only because save_long() pickled the long twice.
Fixed bugs in encode_long().
Noted that decode_long() is quadratic-time despite our hopes,
because long(string, 16) is still quadratic-time in len(string).
It's hex() that's linear-time. I don't know a way to make decode_long()
linear-time in Python, short of maybe transforming the 256's-complement
bytes into marshal's funky internal format, and letting marshal decode
that. It would be more valuable to make long(string, 16) linear time.
pickletester.py: Added a global "protocols" vector so tests can try
all the protocols in a sane way. Changed test_ints() and test_unicode()
to do so. Added a new test_long(), but the tail end of it is disabled
because it "takes forever" under pickle.py (but runs very quickly under
cPickle: cPickle proto 2 for longs is linear-time).
2003-02-01 22:57:53 -04:00
|
|
|
/* 'size' bytes contain the # of bytes of little-endian 256's-complement
|
|
|
|
* data following.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
load_counted_long(Unpicklerobject *self, int size)
|
|
|
|
{
|
2006-02-15 13:27:45 -04:00
|
|
|
Py_ssize_t i;
|
cPickle.c: Full support for the new LONG1 and LONG4. Added comments.
Assorted code cleanups; e.g., sizeof(char) is 1 by definition, so there's
no need to do things like multiply by sizeof(char) in hairy malloc
arguments. Fixed an undetected-overflow bug in readline_file().
longobject.c: Fixed a really stupid bug in the new _PyLong_NumBits.
pickle.py: Fixed stupid bug in save_long(): When proto is 2, it
wrote LONG1 or LONG4, but forgot to return then -- it went on to
append the proto 1 LONG opcode too.
Fixed equally stupid cancelling bugs in load_long1() and
load_long4(): they *returned* the unpickled long instead of pushing
it on the stack. The return values were ignored. Tests passed
before only because save_long() pickled the long twice.
Fixed bugs in encode_long().
Noted that decode_long() is quadratic-time despite our hopes,
because long(string, 16) is still quadratic-time in len(string).
It's hex() that's linear-time. I don't know a way to make decode_long()
linear-time in Python, short of maybe transforming the 256's-complement
bytes into marshal's funky internal format, and letting marshal decode
that. It would be more valuable to make long(string, 16) linear time.
pickletester.py: Added a global "protocols" vector so tests can try
all the protocols in a sane way. Changed test_ints() and test_unicode()
to do so. Added a new test_long(), but the tail end of it is disabled
because it "takes forever" under pickle.py (but runs very quickly under
cPickle: cPickle proto 2 for longs is linear-time).
2003-02-01 22:57:53 -04:00
|
|
|
char *nbytes;
|
|
|
|
unsigned char *pdata;
|
|
|
|
PyObject *along;
|
|
|
|
|
|
|
|
assert(size == 1 || size == 4);
|
|
|
|
i = self->read_func(self, &nbytes, size);
|
|
|
|
if (i < 0) return -1;
|
|
|
|
|
|
|
|
size = calc_binint(nbytes, size);
|
|
|
|
if (size < 0) {
|
|
|
|
/* Corrupt or hostile pickle -- we never write one like
|
|
|
|
* this.
|
|
|
|
*/
|
2003-02-03 18:07:24 -04:00
|
|
|
PyErr_SetString(UnpicklingError, "LONG pickle has negative "
|
cPickle.c: Full support for the new LONG1 and LONG4. Added comments.
Assorted code cleanups; e.g., sizeof(char) is 1 by definition, so there's
no need to do things like multiply by sizeof(char) in hairy malloc
arguments. Fixed an undetected-overflow bug in readline_file().
longobject.c: Fixed a really stupid bug in the new _PyLong_NumBits.
pickle.py: Fixed stupid bug in save_long(): When proto is 2, it
wrote LONG1 or LONG4, but forgot to return then -- it went on to
append the proto 1 LONG opcode too.
Fixed equally stupid cancelling bugs in load_long1() and
load_long4(): they *returned* the unpickled long instead of pushing
it on the stack. The return values were ignored. Tests passed
before only because save_long() pickled the long twice.
Fixed bugs in encode_long().
Noted that decode_long() is quadratic-time despite our hopes,
because long(string, 16) is still quadratic-time in len(string).
It's hex() that's linear-time. I don't know a way to make decode_long()
linear-time in Python, short of maybe transforming the 256's-complement
bytes into marshal's funky internal format, and letting marshal decode
that. It would be more valuable to make long(string, 16) linear time.
pickletester.py: Added a global "protocols" vector so tests can try
all the protocols in a sane way. Changed test_ints() and test_unicode()
to do so. Added a new test_long(), but the tail end of it is disabled
because it "takes forever" under pickle.py (but runs very quickly under
cPickle: cPickle proto 2 for longs is linear-time).
2003-02-01 22:57:53 -04:00
|
|
|
"byte count");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (size == 0)
|
|
|
|
along = PyLong_FromLong(0L);
|
|
|
|
else {
|
|
|
|
/* Read the raw little-endian bytes & convert. */
|
2003-02-02 13:08:33 -04:00
|
|
|
i = self->read_func(self, (char **)&pdata, size);
|
cPickle.c: Full support for the new LONG1 and LONG4. Added comments.
Assorted code cleanups; e.g., sizeof(char) is 1 by definition, so there's
no need to do things like multiply by sizeof(char) in hairy malloc
arguments. Fixed an undetected-overflow bug in readline_file().
longobject.c: Fixed a really stupid bug in the new _PyLong_NumBits.
pickle.py: Fixed stupid bug in save_long(): When proto is 2, it
wrote LONG1 or LONG4, but forgot to return then -- it went on to
append the proto 1 LONG opcode too.
Fixed equally stupid cancelling bugs in load_long1() and
load_long4(): they *returned* the unpickled long instead of pushing
it on the stack. The return values were ignored. Tests passed
before only because save_long() pickled the long twice.
Fixed bugs in encode_long().
Noted that decode_long() is quadratic-time despite our hopes,
because long(string, 16) is still quadratic-time in len(string).
It's hex() that's linear-time. I don't know a way to make decode_long()
linear-time in Python, short of maybe transforming the 256's-complement
bytes into marshal's funky internal format, and letting marshal decode
that. It would be more valuable to make long(string, 16) linear time.
pickletester.py: Added a global "protocols" vector so tests can try
all the protocols in a sane way. Changed test_ints() and test_unicode()
to do so. Added a new test_long(), but the tail end of it is disabled
because it "takes forever" under pickle.py (but runs very quickly under
cPickle: cPickle proto 2 for longs is linear-time).
2003-02-01 22:57:53 -04:00
|
|
|
if (i < 0) return -1;
|
|
|
|
along = _PyLong_FromByteArray(pdata, (size_t)size,
|
|
|
|
1 /* little endian */, 1 /* signed */);
|
|
|
|
}
|
|
|
|
if (along == NULL)
|
|
|
|
return -1;
|
|
|
|
PDATA_PUSH(self->stack, along, -1);
|
|
|
|
return 0;
|
|
|
|
}
|
2001-03-17 00:50:51 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
static int
|
2003-02-01 02:24:36 -04:00
|
|
|
load_float(Unpicklerobject *self)
|
2002-04-01 13:40:08 -04:00
|
|
|
{
|
|
|
|
PyObject *py_float = 0;
|
|
|
|
char *endptr, *s;
|
|
|
|
int len, res = -1;
|
|
|
|
double d;
|
1997-04-09 14:36:32 -03:00
|
|
|
|
2003-02-02 14:29:33 -04:00
|
|
|
if ((len = self->readline_func(self, &s)) < 0) return -1;
|
2002-04-01 13:40:08 -04:00
|
|
|
if (len < 2) return bad_readline();
|
|
|
|
if (!( s=pystrndup(s,len))) return -1;
|
1997-04-09 14:36:32 -03:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
errno = 0;
|
2004-06-08 15:52:54 -03:00
|
|
|
d = PyOS_ascii_strtod(s, &endptr);
|
1997-04-09 14:36:32 -03:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
if (errno || (endptr[0] != '\n') || (endptr[1] != '\0')) {
|
|
|
|
PyErr_SetString(PyExc_ValueError,
|
|
|
|
"could not convert string to float");
|
|
|
|
goto finally;
|
|
|
|
}
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2003-02-01 02:24:36 -04:00
|
|
|
if (!( py_float = PyFloat_FromDouble(d)))
|
2002-04-01 13:40:08 -04:00
|
|
|
goto finally;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
free(s);
|
|
|
|
PDATA_PUSH(self->stack, py_float, -1);
|
|
|
|
return 0;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
finally:
|
|
|
|
free(s);
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
return res;
|
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
|
2003-02-01 02:24:36 -04:00
|
|
|
load_binfloat(Unpicklerobject *self)
|
2002-04-01 13:40:08 -04:00
|
|
|
{
|
2003-03-20 16:53:32 -04:00
|
|
|
PyObject *py_float;
|
2002-04-01 13:40:08 -04:00
|
|
|
double x;
|
|
|
|
char *p;
|
|
|
|
|
2003-02-02 14:29:33 -04:00
|
|
|
if (self->read_func(self, &p, 8) < 0)
|
2002-04-01 13:40:08 -04:00
|
|
|
return -1;
|
|
|
|
|
2003-03-20 16:53:32 -04:00
|
|
|
x = _PyFloat_Unpack8((unsigned char *)p, 0);
|
|
|
|
if (x == -1.0 && PyErr_Occurred())
|
|
|
|
return -1;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2003-03-20 16:53:32 -04:00
|
|
|
py_float = PyFloat_FromDouble(x);
|
|
|
|
if (py_float == NULL)
|
|
|
|
return -1;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2002-04-01 13:40:08 -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
|
2003-02-01 02:24:36 -04:00
|
|
|
load_string(Unpicklerobject *self)
|
2002-04-01 13:40:08 -04:00
|
|
|
{
|
|
|
|
PyObject *str = 0;
|
2002-08-14 04:46:28 -03:00
|
|
|
int len, res = -1;
|
|
|
|
char *s, *p;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2003-02-02 14:29:33 -04:00
|
|
|
if ((len = self->readline_func(self, &s)) < 0) return -1;
|
2002-04-01 13:40:08 -04:00
|
|
|
if (len < 2) return bad_readline();
|
|
|
|
if (!( s=pystrndup(s,len))) return -1;
|
|
|
|
|
2002-08-14 04:46:28 -03:00
|
|
|
|
|
|
|
/* Strip outermost quotes */
|
|
|
|
while (s[len-1] <= ' ')
|
|
|
|
len--;
|
|
|
|
if(s[0]=='"' && s[len-1]=='"'){
|
|
|
|
s[len-1] = '\0';
|
|
|
|
p = s + 1 ;
|
|
|
|
len -= 2;
|
|
|
|
} else if(s[0]=='\'' && s[len-1]=='\''){
|
|
|
|
s[len-1] = '\0';
|
|
|
|
p = s + 1 ;
|
|
|
|
len -= 2;
|
|
|
|
} else
|
2002-04-01 13:40:08 -04:00
|
|
|
goto insecure;
|
|
|
|
/********************************************/
|
1997-12-08 11:15:16 -04:00
|
|
|
|
2002-08-14 04:46:28 -03:00
|
|
|
str = PyString_DecodeEscape(p, len, NULL, 0, NULL);
|
2006-08-02 01:27:11 -03:00
|
|
|
free(s);
|
2002-08-14 04:46:28 -03:00
|
|
|
if (str) {
|
|
|
|
PDATA_PUSH(self->stack, str, -1);
|
|
|
|
res = 0;
|
|
|
|
}
|
2002-04-01 13:40:08 -04:00
|
|
|
return res;
|
2001-03-17 00:50:51 -04:00
|
|
|
|
2002-04-01 13:40:08 -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
|
2003-02-01 02:24:36 -04:00
|
|
|
load_binstring(Unpicklerobject *self)
|
2002-04-01 13:40:08 -04:00
|
|
|
{
|
|
|
|
PyObject *py_string = 0;
|
|
|
|
long l;
|
|
|
|
char *s;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2003-02-02 14:29:33 -04:00
|
|
|
if (self->read_func(self, &s, 4) < 0) return -1;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
l = calc_binint(s, 4);
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2003-02-02 14:29:33 -04:00
|
|
|
if (self->read_func(self, &s, l) < 0)
|
2002-04-01 13:40:08 -04:00
|
|
|
return -1;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2003-02-01 02:24:36 -04:00
|
|
|
if (!( py_string = PyString_FromStringAndSize(s, l)))
|
2002-04-01 13:40:08 -04:00
|
|
|
return -1;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2002-04-01 13:40:08 -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
|
2003-02-01 02:24:36 -04:00
|
|
|
load_short_binstring(Unpicklerobject *self)
|
2002-04-01 13:40:08 -04:00
|
|
|
{
|
|
|
|
PyObject *py_string = 0;
|
|
|
|
unsigned char l;
|
|
|
|
char *s;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2003-02-02 14:29:33 -04:00
|
|
|
if (self->read_func(self, &s, 1) < 0)
|
2002-04-01 13:40:08 -04:00
|
|
|
return -1;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
l = (unsigned char)s[0];
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2003-02-02 14:29:33 -04:00
|
|
|
if (self->read_func(self, &s, l) < 0) return -1;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
if (!( py_string = PyString_FromStringAndSize(s, l))) return -1;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2002-04-01 13:40:08 -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
|
|
|
|
|
|
|
|
2001-08-17 15:39:25 -03:00
|
|
|
#ifdef Py_USING_UNICODE
|
2000-03-10 19:11:40 -04:00
|
|
|
static int
|
2003-02-01 02:24:36 -04:00
|
|
|
load_unicode(Unpicklerobject *self)
|
2002-04-01 13:40:08 -04:00
|
|
|
{
|
|
|
|
PyObject *str = 0;
|
|
|
|
int len, res = -1;
|
|
|
|
char *s;
|
2000-03-10 19:11:40 -04:00
|
|
|
|
2003-02-02 14:29:33 -04:00
|
|
|
if ((len = self->readline_func(self, &s)) < 0) return -1;
|
2002-04-01 13:40:08 -04:00
|
|
|
if (len < 1) return bad_readline();
|
2000-03-10 19:11:40 -04:00
|
|
|
|
2003-02-01 02:24:36 -04:00
|
|
|
if (!( str = PyUnicode_DecodeRawUnicodeEscape(s, len - 1, NULL)))
|
2002-04-01 13:40:08 -04:00
|
|
|
goto finally;
|
2000-03-10 19:11:40 -04:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
PDATA_PUSH(self->stack, str, -1);
|
|
|
|
return 0;
|
2000-03-10 19:11:40 -04:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
finally:
|
|
|
|
return res;
|
2001-03-17 00:50:51 -04:00
|
|
|
}
|
2001-08-17 15:39:25 -03:00
|
|
|
#endif
|
2000-03-10 19:11:40 -04:00
|
|
|
|
|
|
|
|
2001-08-17 15:39:25 -03:00
|
|
|
#ifdef Py_USING_UNICODE
|
2000-03-10 19:11:40 -04:00
|
|
|
static int
|
2003-02-01 02:24:36 -04:00
|
|
|
load_binunicode(Unpicklerobject *self)
|
2002-04-01 13:40:08 -04:00
|
|
|
{
|
|
|
|
PyObject *unicode;
|
|
|
|
long l;
|
|
|
|
char *s;
|
2000-03-10 19:11:40 -04:00
|
|
|
|
2003-02-02 14:29:33 -04:00
|
|
|
if (self->read_func(self, &s, 4) < 0) return -1;
|
2000-03-10 19:11:40 -04:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
l = calc_binint(s, 4);
|
2000-03-10 19:11:40 -04:00
|
|
|
|
2003-02-02 14:29:33 -04:00
|
|
|
if (self->read_func(self, &s, l) < 0)
|
2002-04-01 13:40:08 -04:00
|
|
|
return -1;
|
2000-03-10 19:11:40 -04:00
|
|
|
|
2003-02-01 02:24:36 -04:00
|
|
|
if (!( unicode = PyUnicode_DecodeUTF8(s, l, NULL)))
|
2002-04-01 13:40:08 -04:00
|
|
|
return -1;
|
2000-03-10 19:11:40 -04:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
PDATA_PUSH(self->stack, unicode, -1);
|
|
|
|
return 0;
|
2000-03-10 19:11:40 -04:00
|
|
|
}
|
2001-08-17 15:39:25 -03:00
|
|
|
#endif
|
2000-03-10 19:11:40 -04:00
|
|
|
|
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
static int
|
2003-02-01 02:24:36 -04:00
|
|
|
load_tuple(Unpicklerobject *self)
|
2002-04-01 13:40:08 -04:00
|
|
|
{
|
|
|
|
PyObject *tup;
|
|
|
|
int i;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
if ((i = marker(self)) < 0) return -1;
|
|
|
|
if (!( 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
|
2003-02-02 16:29:39 -04:00
|
|
|
load_counted_tuple(Unpicklerobject *self, int len)
|
2002-04-01 13:40:08 -04:00
|
|
|
{
|
2003-02-02 16:29:39 -04:00
|
|
|
PyObject *tup = PyTuple_New(len);
|
|
|
|
|
|
|
|
if (tup == NULL)
|
|
|
|
return -1;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2003-02-02 16:29:39 -04:00
|
|
|
while (--len >= 0) {
|
|
|
|
PyObject *element;
|
|
|
|
|
|
|
|
PDATA_POP(self->stack, element);
|
|
|
|
if (element == NULL)
|
|
|
|
return -1;
|
|
|
|
PyTuple_SET_ITEM(tup, len, element);
|
|
|
|
}
|
2002-04-01 13:40:08 -04:00
|
|
|
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
|
2003-02-01 02:24:36 -04:00
|
|
|
load_empty_list(Unpicklerobject *self)
|
2002-04-01 13:40:08 -04:00
|
|
|
{
|
|
|
|
PyObject *list;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
if (!( 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
|
2003-02-01 02:24:36 -04:00
|
|
|
load_empty_dict(Unpicklerobject *self)
|
2002-04-01 13:40:08 -04:00
|
|
|
{
|
|
|
|
PyObject *dict;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
if (!( 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
|
2003-02-01 02:24:36 -04:00
|
|
|
load_list(Unpicklerobject *self)
|
2002-04-01 13:40:08 -04:00
|
|
|
{
|
|
|
|
PyObject *list = 0;
|
|
|
|
int i;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
if ((i = marker(self)) < 0) return -1;
|
|
|
|
if (!( 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
|
2003-02-01 02:24:36 -04:00
|
|
|
load_dict(Unpicklerobject *self)
|
2002-04-01 13:40:08 -04:00
|
|
|
{
|
|
|
|
PyObject *dict, *key, *value;
|
|
|
|
int i, j, k;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
if ((i = marker(self)) < 0) return -1;
|
|
|
|
j=self->stack->length;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
if (!( dict = PyDict_New())) return -1;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2002-04-01 13:40:08 -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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Pdata_clear(self->stack, i);
|
|
|
|
PDATA_PUSH(self->stack, dict, -1);
|
|
|
|
return 0;
|
1997-01-06 18:59:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
2003-02-01 02:24:36 -04:00
|
|
|
Instance_New(PyObject *cls, PyObject *args)
|
2002-04-01 13:40:08 -04:00
|
|
|
{
|
2003-01-31 22:16:37 -04:00
|
|
|
PyObject *r = 0;
|
2002-04-01 13:40:08 -04:00
|
|
|
|
|
|
|
if (PyClass_Check(cls)) {
|
|
|
|
int l;
|
|
|
|
|
|
|
|
if ((l=PyObject_Size(args)) < 0) goto err;
|
|
|
|
if (!( l )) {
|
|
|
|
PyObject *__getinitargs__;
|
|
|
|
|
2003-02-01 02:24:36 -04:00
|
|
|
__getinitargs__ = PyObject_GetAttr(cls,
|
2003-06-16 17:19:49 -03:00
|
|
|
__getinitargs___str);
|
2002-04-01 13:40:08 -04:00
|
|
|
if (!__getinitargs__) {
|
2003-02-01 02:24:36 -04:00
|
|
|
/* We have a class with no __getinitargs__,
|
2002-04-01 13:40:08 -04:00
|
|
|
so bypass usual construction */
|
|
|
|
PyObject *inst;
|
|
|
|
|
|
|
|
PyErr_Clear();
|
2003-02-01 02:24:36 -04:00
|
|
|
if (!( inst=PyInstance_NewRaw(cls, NULL)))
|
2002-04-01 13:40:08 -04:00
|
|
|
goto err;
|
|
|
|
return inst;
|
|
|
|
}
|
|
|
|
Py_DECREF(__getinitargs__);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((r=PyInstance_New(cls, args, NULL))) return r;
|
|
|
|
else goto err;
|
|
|
|
}
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
if ((r=PyObject_CallObject(cls, args))) return r;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
err:
|
|
|
|
{
|
2006-06-28 03:28:31 -03:00
|
|
|
PyObject *tp, *v, *tb, *tmp_value;
|
2002-04-01 13:40:08 -04:00
|
|
|
|
|
|
|
PyErr_Fetch(&tp, &v, &tb);
|
2006-06-28 03:28:31 -03:00
|
|
|
tmp_value = v;
|
|
|
|
/* NULL occurs when there was a KeyboardInterrupt */
|
|
|
|
if (tmp_value == NULL)
|
|
|
|
tmp_value = Py_None;
|
|
|
|
if ((r = PyTuple_Pack(3, tmp_value, cls, args))) {
|
2002-04-01 13:40:08 -04:00
|
|
|
Py_XDECREF(v);
|
|
|
|
v=r;
|
|
|
|
}
|
|
|
|
PyErr_Restore(tp,v,tb);
|
|
|
|
}
|
|
|
|
return NULL;
|
1997-04-09 14:36:32 -03:00
|
|
|
}
|
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
|
2003-02-01 02:24:36 -04:00
|
|
|
load_obj(Unpicklerobject *self)
|
2002-04-01 13:40:08 -04:00
|
|
|
{
|
|
|
|
PyObject *class, *tup, *obj=0;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if ((i = marker(self)) < 0) return -1;
|
|
|
|
if (!( 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
|
|
|
|
2002-04-01 13:40:08 -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
|
2003-02-01 02:24:36 -04:00
|
|
|
load_inst(Unpicklerobject *self)
|
2002-04-01 13:40:08 -04:00
|
|
|
{
|
|
|
|
PyObject *tup, *class=0, *obj=0, *module_name, *class_name;
|
|
|
|
int i, len;
|
|
|
|
char *s;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
if ((i = marker(self)) < 0) return -1;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2003-02-02 14:29:33 -04:00
|
|
|
if ((len = self->readline_func(self, &s)) < 0) return -1;
|
2002-04-01 13:40:08 -04:00
|
|
|
if (len < 2) return bad_readline();
|
|
|
|
module_name = PyString_FromStringAndSize(s, len - 1);
|
|
|
|
if (!module_name) return -1;
|
|
|
|
|
2003-02-02 14:29:33 -04:00
|
|
|
if ((len = self->readline_func(self, &s)) >= 0) {
|
2002-04-01 13:40:08 -04:00
|
|
|
if (len < 2) return bad_readline();
|
|
|
|
if ((class_name = PyString_FromStringAndSize(s, len - 1))) {
|
2003-02-01 02:24:36 -04:00
|
|
|
class = find_class(module_name, class_name,
|
2002-04-01 13:40:08 -04:00
|
|
|
self->find_class);
|
|
|
|
Py_DECREF(class_name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Py_DECREF(module_name);
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
if (! class) return -1;
|
2001-03-17 00:50:51 -04:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
if ((tup=Pdata_popTuple(self->stack, i))) {
|
|
|
|
obj = Instance_New(class, tup);
|
|
|
|
Py_DECREF(tup);
|
|
|
|
}
|
|
|
|
Py_DECREF(class);
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
if (! obj) return -1;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2002-04-01 13:40:08 -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
|
|
|
|
2003-02-13 14:24:14 -04:00
|
|
|
static int
|
|
|
|
load_newobj(Unpicklerobject *self)
|
|
|
|
{
|
|
|
|
PyObject *args = NULL;
|
|
|
|
PyObject *clsraw = NULL;
|
|
|
|
PyTypeObject *cls; /* clsraw cast to its true type */
|
|
|
|
PyObject *obj;
|
|
|
|
|
|
|
|
/* Stack is ... cls argtuple, and we want to call
|
|
|
|
* cls.__new__(cls, *argtuple).
|
|
|
|
*/
|
|
|
|
PDATA_POP(self->stack, args);
|
|
|
|
if (args == NULL) goto Fail;
|
|
|
|
if (! PyTuple_Check(args)) {
|
|
|
|
PyErr_SetString(UnpicklingError, "NEWOBJ expected an arg "
|
|
|
|
"tuple.");
|
|
|
|
goto Fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
PDATA_POP(self->stack, clsraw);
|
|
|
|
cls = (PyTypeObject *)clsraw;
|
|
|
|
if (cls == NULL) goto Fail;
|
|
|
|
if (! PyType_Check(cls)) {
|
|
|
|
PyErr_SetString(UnpicklingError, "NEWOBJ class argument "
|
|
|
|
"isn't a type object");
|
|
|
|
goto Fail;
|
|
|
|
}
|
|
|
|
if (cls->tp_new == NULL) {
|
|
|
|
PyErr_SetString(UnpicklingError, "NEWOBJ class argument "
|
|
|
|
"has NULL tp_new");
|
|
|
|
goto Fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Call __new__. */
|
|
|
|
obj = cls->tp_new(cls, args, NULL);
|
|
|
|
if (obj == NULL) goto Fail;
|
|
|
|
|
|
|
|
Py_DECREF(args);
|
|
|
|
Py_DECREF(clsraw);
|
|
|
|
PDATA_PUSH(self->stack, obj, -1);
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
Fail:
|
|
|
|
Py_XDECREF(args);
|
|
|
|
Py_XDECREF(clsraw);
|
|
|
|
return -1;
|
|
|
|
}
|
1997-01-06 18:59:08 -04:00
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
static int
|
2003-02-01 02:24:36 -04:00
|
|
|
load_global(Unpicklerobject *self)
|
2002-04-01 13:40:08 -04:00
|
|
|
{
|
|
|
|
PyObject *class = 0, *module_name = 0, *class_name = 0;
|
|
|
|
int len;
|
|
|
|
char *s;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2003-02-02 14:29:33 -04:00
|
|
|
if ((len = self->readline_func(self, &s)) < 0) return -1;
|
2002-04-01 13:40:08 -04:00
|
|
|
if (len < 2) return bad_readline();
|
|
|
|
module_name = PyString_FromStringAndSize(s, len - 1);
|
|
|
|
if (!module_name) return -1;
|
|
|
|
|
2003-02-02 14:29:33 -04:00
|
|
|
if ((len = self->readline_func(self, &s)) >= 0) {
|
2002-12-06 22:43:28 -04:00
|
|
|
if (len < 2) {
|
|
|
|
Py_DECREF(module_name);
|
|
|
|
return bad_readline();
|
|
|
|
}
|
2002-04-01 13:40:08 -04:00
|
|
|
if ((class_name = PyString_FromStringAndSize(s, len - 1))) {
|
2003-02-01 02:24:36 -04:00
|
|
|
class = find_class(module_name, class_name,
|
2002-04-01 13:40:08 -04:00
|
|
|
self->find_class);
|
|
|
|
Py_DECREF(class_name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Py_DECREF(module_name);
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2002-04-01 13:40:08 -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
|
2003-02-01 02:24:36 -04:00
|
|
|
load_persid(Unpicklerobject *self)
|
2002-04-01 13:40:08 -04:00
|
|
|
{
|
|
|
|
PyObject *pid = 0;
|
|
|
|
int len;
|
|
|
|
char *s;
|
|
|
|
|
|
|
|
if (self->pers_func) {
|
2003-02-02 14:29:33 -04:00
|
|
|
if ((len = self->readline_func(self, &s)) < 0) return -1;
|
2002-04-01 13:40:08 -04:00
|
|
|
if (len < 2) return bad_readline();
|
|
|
|
|
|
|
|
pid = PyString_FromStringAndSize(s, len - 1);
|
|
|
|
if (!pid) 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) {
|
2003-02-01 02:24:36 -04:00
|
|
|
pid = PyObject_Call(self->pers_func, self->arg,
|
2002-04-01 13:40:08 -04:00
|
|
|
NULL);
|
|
|
|
FREE_ARG_TUP(self);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (! pid) return -1;
|
|
|
|
|
|
|
|
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-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
|
2003-02-01 02:24:36 -04:00
|
|
|
load_binpersid(Unpicklerobject *self)
|
2002-04-01 13:40:08 -04:00
|
|
|
{
|
|
|
|
PyObject *pid = 0;
|
|
|
|
|
|
|
|
if (self->pers_func) {
|
|
|
|
PDATA_POP(self->stack, pid);
|
|
|
|
if (! pid) 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_Call(self->pers_func, self->arg,
|
|
|
|
NULL);
|
|
|
|
FREE_ARG_TUP(self);
|
|
|
|
}
|
|
|
|
if (! pid) return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
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-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
|
2003-02-01 02:24:36 -04:00
|
|
|
load_pop(Unpicklerobject *self)
|
2002-04-01 13:40:08 -04:00
|
|
|
{
|
|
|
|
int len;
|
|
|
|
|
|
|
|
if (!( (len=self->stack->length) > 0 )) return stackUnderflow();
|
|
|
|
|
|
|
|
/* Note that we split the (pickle.py) stack into two stacks,
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if ((self->num_marks > 0) &&
|
|
|
|
(self->marks[self->num_marks - 1] == len))
|
|
|
|
self->num_marks--;
|
|
|
|
else {
|
|
|
|
len--;
|
|
|
|
Py_DECREF(self->stack->data[len]);
|
|
|
|
self->stack->length=len;
|
|
|
|
}
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
return 0;
|
1997-01-06 18:59:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
static int
|
2003-02-01 02:24:36 -04:00
|
|
|
load_pop_mark(Unpicklerobject *self)
|
2002-04-01 13:40:08 -04:00
|
|
|
{
|
|
|
|
int i;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
if ((i = marker(self)) < 0)
|
|
|
|
return -1;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
Pdata_clear(self->stack, i);
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
return 0;
|
1997-01-06 18:59:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
static int
|
2003-02-01 02:24:36 -04:00
|
|
|
load_dup(Unpicklerobject *self)
|
2002-04-01 13:40:08 -04:00
|
|
|
{
|
|
|
|
PyObject *last;
|
|
|
|
int len;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2002-04-01 13:40:08 -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);
|
|
|
|
return 0;
|
1997-01-06 18:59:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
static int
|
2003-02-01 02:24:36 -04:00
|
|
|
load_get(Unpicklerobject *self)
|
2002-04-01 13:40:08 -04:00
|
|
|
{
|
|
|
|
PyObject *py_str = 0, *value = 0;
|
|
|
|
int len;
|
|
|
|
char *s;
|
|
|
|
int rc;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2003-02-02 14:29:33 -04:00
|
|
|
if ((len = self->readline_func(self, &s)) < 0) return -1;
|
2002-04-01 13:40:08 -04:00
|
|
|
if (len < 2) return bad_readline();
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
if (!( py_str = PyString_FromStringAndSize(s, len - 1))) return -1;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
value = PyDict_GetItem(self->memo, py_str);
|
|
|
|
if (! value) {
|
|
|
|
PyErr_SetObject(BadPickleGet, py_str);
|
|
|
|
rc = -1;
|
2003-02-13 19:00:26 -04:00
|
|
|
}
|
|
|
|
else {
|
2002-04-01 13:40:08 -04:00
|
|
|
PDATA_APPEND(self->stack, value, -1);
|
|
|
|
rc = 0;
|
|
|
|
}
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2002-04-01 13:40:08 -04: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
|
2003-02-01 02:24:36 -04:00
|
|
|
load_binget(Unpicklerobject *self)
|
2002-04-01 13:40:08 -04:00
|
|
|
{
|
|
|
|
PyObject *py_key = 0, *value = 0;
|
|
|
|
unsigned char key;
|
|
|
|
char *s;
|
|
|
|
int rc;
|
|
|
|
|
2003-02-02 14:29:33 -04:00
|
|
|
if (self->read_func(self, &s, 1) < 0) return -1;
|
2002-04-01 13:40:08 -04:00
|
|
|
|
|
|
|
key = (unsigned char)s[0];
|
|
|
|
if (!( py_key = PyInt_FromLong((long)key))) return -1;
|
|
|
|
|
|
|
|
value = PyDict_GetItem(self->memo, py_key);
|
|
|
|
if (! value) {
|
|
|
|
PyErr_SetObject(BadPickleGet, py_key);
|
|
|
|
rc = -1;
|
2003-02-13 19:00:26 -04:00
|
|
|
}
|
|
|
|
else {
|
2002-04-01 13:40:08 -04:00
|
|
|
PDATA_APPEND(self->stack, value, -1);
|
|
|
|
rc = 0;
|
|
|
|
}
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2002-04-01 13:40:08 -04: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
|
2003-02-01 02:24:36 -04:00
|
|
|
load_long_binget(Unpicklerobject *self)
|
2002-04-01 13:40:08 -04:00
|
|
|
{
|
|
|
|
PyObject *py_key = 0, *value = 0;
|
|
|
|
unsigned char c;
|
|
|
|
char *s;
|
|
|
|
long key;
|
|
|
|
int rc;
|
|
|
|
|
2003-02-02 14:29:33 -04:00
|
|
|
if (self->read_func(self, &s, 4) < 0) return -1;
|
2002-04-01 13:40:08 -04: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;
|
|
|
|
|
|
|
|
if (!( py_key = PyInt_FromLong((long)key))) return -1;
|
|
|
|
|
|
|
|
value = PyDict_GetItem(self->memo, py_key);
|
|
|
|
if (! value) {
|
|
|
|
PyErr_SetObject(BadPickleGet, py_key);
|
|
|
|
rc = -1;
|
2003-02-13 19:00:26 -04:00
|
|
|
}
|
|
|
|
else {
|
2002-04-01 13:40:08 -04:00
|
|
|
PDATA_APPEND(self->stack, value, -1);
|
|
|
|
rc = 0;
|
|
|
|
}
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
Py_DECREF(py_key);
|
|
|
|
return rc;
|
|
|
|
}
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2003-02-04 01:06:17 -04:00
|
|
|
/* Push an object from the extension registry (EXT[124]). nbytes is
|
|
|
|
* the number of bytes following the opcode, holding the index (code) value.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
load_extension(Unpicklerobject *self, int nbytes)
|
|
|
|
{
|
|
|
|
char *codebytes; /* the nbytes bytes after the opcode */
|
|
|
|
long code; /* calc_binint returns long */
|
|
|
|
PyObject *py_code; /* code as a Python int */
|
|
|
|
PyObject *obj; /* the object to push */
|
|
|
|
PyObject *pair; /* (module_name, class_name) */
|
|
|
|
PyObject *module_name, *class_name;
|
|
|
|
|
|
|
|
assert(nbytes == 1 || nbytes == 2 || nbytes == 4);
|
|
|
|
if (self->read_func(self, &codebytes, nbytes) < 0) return -1;
|
|
|
|
code = calc_binint(codebytes, nbytes);
|
|
|
|
if (code <= 0) { /* note that 0 is forbidden */
|
|
|
|
/* Corrupt or hostile pickle. */
|
|
|
|
PyErr_SetString(UnpicklingError, "EXT specifies code <= 0");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Look for the code in the cache. */
|
|
|
|
py_code = PyInt_FromLong(code);
|
|
|
|
if (py_code == NULL) return -1;
|
|
|
|
obj = PyDict_GetItem(extension_cache, py_code);
|
|
|
|
if (obj != NULL) {
|
|
|
|
/* Bingo. */
|
|
|
|
Py_DECREF(py_code);
|
|
|
|
PDATA_APPEND(self->stack, obj, -1);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Look up the (module_name, class_name) pair. */
|
|
|
|
pair = PyDict_GetItem(inverted_registry, py_code);
|
|
|
|
if (pair == NULL) {
|
|
|
|
Py_DECREF(py_code);
|
|
|
|
PyErr_Format(PyExc_ValueError, "unregistered extension "
|
|
|
|
"code %ld", code);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
/* Since the extension registry is manipulable via Python code,
|
2003-02-04 01:20:32 -04:00
|
|
|
* confirm that pair is really a 2-tuple of strings.
|
2003-02-04 01:06:17 -04:00
|
|
|
*/
|
|
|
|
if (!PyTuple_Check(pair) || PyTuple_Size(pair) != 2 ||
|
|
|
|
!PyString_Check(module_name = PyTuple_GET_ITEM(pair, 0)) ||
|
|
|
|
!PyString_Check(class_name = PyTuple_GET_ITEM(pair, 1))) {
|
|
|
|
Py_DECREF(py_code);
|
|
|
|
PyErr_Format(PyExc_ValueError, "_inverted_registry[%ld] "
|
|
|
|
"isn't a 2-tuple of strings", code);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
/* Load the object. */
|
|
|
|
obj = find_class(module_name, class_name, self->find_class);
|
|
|
|
if (obj == NULL) {
|
|
|
|
Py_DECREF(py_code);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
/* Cache code -> obj. */
|
|
|
|
code = PyDict_SetItem(extension_cache, py_code, obj);
|
|
|
|
Py_DECREF(py_code);
|
|
|
|
if (code < 0) {
|
|
|
|
Py_DECREF(obj);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
PDATA_PUSH(self->stack, obj, -1);
|
|
|
|
return 0;
|
|
|
|
}
|
2001-03-17 00:50:51 -04:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
static int
|
2003-02-01 02:24:36 -04:00
|
|
|
load_put(Unpicklerobject *self)
|
2002-04-01 13:40:08 -04:00
|
|
|
{
|
|
|
|
PyObject *py_str = 0, *value = 0;
|
|
|
|
int len, l;
|
|
|
|
char *s;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2003-02-02 14:29:33 -04:00
|
|
|
if ((l = self->readline_func(self, &s)) < 0) return -1;
|
2002-04-01 13:40:08 -04:00
|
|
|
if (l < 2) return bad_readline();
|
|
|
|
if (!( len=self->stack->length )) return stackUnderflow();
|
|
|
|
if (!( 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
|
2003-02-01 02:24:36 -04:00
|
|
|
load_binput(Unpicklerobject *self)
|
2002-04-01 13:40:08 -04:00
|
|
|
{
|
|
|
|
PyObject *py_key = 0, *value = 0;
|
|
|
|
unsigned char key;
|
|
|
|
char *s;
|
|
|
|
int len;
|
|
|
|
|
2003-02-02 14:29:33 -04:00
|
|
|
if (self->read_func(self, &s, 1) < 0) return -1;
|
2002-04-01 13:40:08 -04:00
|
|
|
if (!( (len=self->stack->length) > 0 )) return stackUnderflow();
|
|
|
|
|
|
|
|
key = (unsigned char)s[0];
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
if (!( 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
|
2003-02-01 02:24:36 -04:00
|
|
|
load_long_binput(Unpicklerobject *self)
|
2002-04-01 13:40:08 -04:00
|
|
|
{
|
|
|
|
PyObject *py_key = 0, *value = 0;
|
|
|
|
long key;
|
|
|
|
unsigned char c;
|
|
|
|
char *s;
|
|
|
|
int len;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2003-02-02 14:29:33 -04:00
|
|
|
if (self->read_func(self, &s, 4) < 0) return -1;
|
2002-04-01 13:40:08 -04:00
|
|
|
if (!( len=self->stack->length )) return stackUnderflow();
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2002-04-01 13:40:08 -04: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
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
if (!( 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-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
|
2003-02-01 02:24:36 -04:00
|
|
|
do_append(Unpicklerobject *self, int x)
|
2002-04-01 13:40:08 -04:00
|
|
|
{
|
|
|
|
PyObject *value = 0, *list = 0, *append_method = 0;
|
|
|
|
int len, i;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
len=self->stack->length;
|
|
|
|
if (!( len >= x && x > 0 )) return stackUnderflow();
|
|
|
|
/* nothing to do */
|
2003-02-01 02:24:36 -04:00
|
|
|
if (len==x) return 0;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
list=self->stack->data[x-1];
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
if (PyList_Check(list)) {
|
|
|
|
PyObject *slice;
|
|
|
|
int list_len;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
slice=Pdata_popList(self->stack, x);
|
2006-07-22 13:20:49 -03:00
|
|
|
if (! slice) return -1;
|
2002-04-01 13:40:08 -04:00
|
|
|
list_len = PyList_GET_SIZE(list);
|
|
|
|
i=PyList_SetSlice(list, list_len, list_len, slice);
|
|
|
|
Py_DECREF(slice);
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
|
2003-02-01 02:24:36 -04:00
|
|
|
if (!( append_method = PyObject_GetAttr(list, append_str)))
|
2002-04-01 13:40:08 -04:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
for (i = x; i < len; i++) {
|
|
|
|
PyObject *junk;
|
|
|
|
|
|
|
|
value=self->stack->data[i];
|
|
|
|
junk=0;
|
|
|
|
ARG_TUP(self, value);
|
|
|
|
if (self->arg) {
|
2003-02-01 02:24:36 -04:00
|
|
|
junk = PyObject_Call(append_method, self->arg,
|
2002-04-01 13:40:08 -04:00
|
|
|
NULL);
|
|
|
|
FREE_ARG_TUP(self);
|
|
|
|
}
|
|
|
|
if (! junk) {
|
|
|
|
Pdata_clear(self->stack, i+1);
|
|
|
|
self->stack->length=x;
|
|
|
|
Py_DECREF(append_method);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
Py_DECREF(junk);
|
|
|
|
}
|
|
|
|
self->stack->length=x;
|
|
|
|
Py_DECREF(append_method);
|
|
|
|
}
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
return 0;
|
1997-04-09 14:36:32 -03:00
|
|
|
}
|
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
|
2003-02-01 02:24:36 -04:00
|
|
|
load_append(Unpicklerobject *self)
|
2002-04-01 13:40:08 -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
|
2003-02-01 02:24:36 -04:00
|
|
|
load_appends(Unpicklerobject *self)
|
2002-04-01 13:40:08 -04:00
|
|
|
{
|
|
|
|
return do_append(self, marker(self));
|
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
|
2003-02-01 02:24:36 -04:00
|
|
|
do_setitems(Unpicklerobject *self, int x)
|
2002-04-01 13:40:08 -04:00
|
|
|
{
|
|
|
|
PyObject *value = 0, *key = 0, *dict = 0;
|
|
|
|
int len, i, r=0;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
if (!( (len=self->stack->length) >= x
|
|
|
|
&& x > 0 )) return stackUnderflow();
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
dict=self->stack->data[x-1];
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2002-04-01 13:40:08 -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
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
Pdata_clear(self->stack, x);
|
1997-04-09 14:36:32 -03:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
return r;
|
1997-01-06 18:59:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
static int
|
2003-02-01 02:24:36 -04:00
|
|
|
load_setitem(Unpicklerobject *self)
|
2002-04-01 13:40:08 -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
|
2003-02-01 02:24:36 -04:00
|
|
|
load_setitems(Unpicklerobject *self)
|
2002-04-01 13:40:08 -04:00
|
|
|
{
|
|
|
|
return do_setitems(self, marker(self));
|
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
|
2003-02-01 02:24:36 -04:00
|
|
|
load_build(Unpicklerobject *self)
|
2002-04-01 13:40:08 -04:00
|
|
|
{
|
2003-02-14 23:01:11 -04:00
|
|
|
PyObject *state, *inst, *slotstate;
|
|
|
|
PyObject *__setstate__;
|
|
|
|
PyObject *d_key, *d_value;
|
2006-02-15 13:27:45 -04:00
|
|
|
Py_ssize_t i;
|
2003-02-14 23:01:11 -04:00
|
|
|
int res = -1;
|
2002-04-01 13:40:08 -04:00
|
|
|
|
2003-02-14 23:01:11 -04:00
|
|
|
/* Stack is ... instance, state. We want to leave instance at
|
|
|
|
* the stack top, possibly mutated via instance.__setstate__(state).
|
|
|
|
*/
|
|
|
|
if (self->stack->length < 2)
|
|
|
|
return stackUnderflow();
|
|
|
|
PDATA_POP(self->stack, state);
|
|
|
|
if (state == NULL)
|
|
|
|
return -1;
|
|
|
|
inst = self->stack->data[self->stack->length - 1];
|
2002-04-01 13:40:08 -04:00
|
|
|
|
2003-02-14 23:01:11 -04:00
|
|
|
__setstate__ = PyObject_GetAttr(inst, __setstate___str);
|
|
|
|
if (__setstate__ != NULL) {
|
|
|
|
PyObject *junk = NULL;
|
|
|
|
|
|
|
|
/* The explicit __setstate__ is responsible for everything. */
|
|
|
|
ARG_TUP(self, state);
|
2002-04-01 13:40:08 -04:00
|
|
|
if (self->arg) {
|
|
|
|
junk = PyObject_Call(__setstate__, self->arg, NULL);
|
|
|
|
FREE_ARG_TUP(self);
|
|
|
|
}
|
|
|
|
Py_DECREF(__setstate__);
|
2003-02-14 23:01:11 -04:00
|
|
|
if (junk == NULL)
|
|
|
|
return -1;
|
2002-04-01 13:40:08 -04:00
|
|
|
Py_DECREF(junk);
|
|
|
|
return 0;
|
|
|
|
}
|
2003-06-16 17:19:49 -03:00
|
|
|
if (!PyErr_ExceptionMatches(PyExc_AttributeError))
|
|
|
|
return -1;
|
2002-04-01 13:40:08 -04:00
|
|
|
PyErr_Clear();
|
2003-02-14 23:01:11 -04:00
|
|
|
|
|
|
|
/* A default __setstate__. First see whether state embeds a
|
|
|
|
* slot state dict too (a proto 2 addition).
|
|
|
|
*/
|
|
|
|
if (PyTuple_Check(state) && PyTuple_Size(state) == 2) {
|
|
|
|
PyObject *temp = state;
|
|
|
|
state = PyTuple_GET_ITEM(temp, 0);
|
|
|
|
slotstate = PyTuple_GET_ITEM(temp, 1);
|
|
|
|
Py_INCREF(state);
|
|
|
|
Py_INCREF(slotstate);
|
|
|
|
Py_DECREF(temp);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
slotstate = NULL;
|
|
|
|
|
|
|
|
/* Set inst.__dict__ from the state dict (if any). */
|
|
|
|
if (state != Py_None) {
|
|
|
|
PyObject *dict;
|
|
|
|
if (! PyDict_Check(state)) {
|
|
|
|
PyErr_SetString(UnpicklingError, "state is not a "
|
|
|
|
"dictionary");
|
|
|
|
goto finally;
|
|
|
|
}
|
|
|
|
dict = PyObject_GetAttr(inst, __dict___str);
|
|
|
|
if (dict == NULL)
|
|
|
|
goto finally;
|
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
i = 0;
|
2003-02-14 23:01:11 -04:00
|
|
|
while (PyDict_Next(state, &i, &d_key, &d_value)) {
|
|
|
|
if (PyObject_SetItem(dict, d_key, d_value) < 0)
|
|
|
|
goto finally;
|
2002-04-01 13:40:08 -04:00
|
|
|
}
|
2003-02-14 23:01:11 -04:00
|
|
|
Py_DECREF(dict);
|
2002-04-01 13:40:08 -04:00
|
|
|
}
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2003-02-14 23:01:11 -04:00
|
|
|
/* Also set instance attributes from the slotstate dict (if any). */
|
|
|
|
if (slotstate != NULL) {
|
|
|
|
if (! PyDict_Check(slotstate)) {
|
|
|
|
PyErr_SetString(UnpicklingError, "slot state is not "
|
|
|
|
"a dictionary");
|
|
|
|
goto finally;
|
|
|
|
}
|
|
|
|
i = 0;
|
|
|
|
while (PyDict_Next(slotstate, &i, &d_key, &d_value)) {
|
|
|
|
if (PyObject_SetAttr(inst, d_key, d_value) < 0)
|
|
|
|
goto finally;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
res = 0;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2003-02-14 23:01:11 -04:00
|
|
|
finally:
|
|
|
|
Py_DECREF(state);
|
|
|
|
Py_XDECREF(slotstate);
|
|
|
|
return res;
|
2002-04-01 13:40:08 -04:00
|
|
|
}
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2000-05-09 15:14:50 -03:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
static int
|
2003-02-01 02:24:36 -04:00
|
|
|
load_mark(Unpicklerobject *self)
|
2002-04-01 13:40:08 -04:00
|
|
|
{
|
|
|
|
int s;
|
|
|
|
|
|
|
|
/* 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
|
|
|
|
mark stack.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if ((self->num_marks + 1) >= self->marks_size) {
|
|
|
|
s=self->marks_size+20;
|
|
|
|
if (s <= self->num_marks) s=self->num_marks + 1;
|
|
|
|
if (self->marks == NULL)
|
|
|
|
self->marks=(int *)malloc(s * sizeof(int));
|
|
|
|
else
|
2003-02-01 02:24:36 -04:00
|
|
|
self->marks=(int *)realloc(self->marks,
|
2002-04-01 13:40:08 -04:00
|
|
|
s * sizeof(int));
|
|
|
|
if (! self->marks) {
|
|
|
|
PyErr_NoMemory();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
self->marks_size = s;
|
|
|
|
}
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
self->marks[self->num_marks++] = self->stack->length;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
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
|
2003-02-01 02:24:36 -04:00
|
|
|
load_reduce(Unpicklerobject *self)
|
2002-04-01 13:40:08 -04:00
|
|
|
{
|
|
|
|
PyObject *callable = 0, *arg_tup = 0, *ob = 0;
|
|
|
|
|
|
|
|
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
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
if (! ob) return -1;
|
2001-03-17 00:50:51 -04:00
|
|
|
|
2002-04-01 13:40:08 -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
|
|
|
|
2003-02-02 12:09:05 -04:00
|
|
|
/* Just raises an error if we don't know the protocol specified. PROTO
|
|
|
|
* is the first opcode for protocols >= 2.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
load_proto(Unpicklerobject *self)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
char *protobyte;
|
|
|
|
|
|
|
|
i = self->read_func(self, &protobyte, 1);
|
|
|
|
if (i < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
i = calc_binint(protobyte, 1);
|
|
|
|
/* No point checking for < 0, since calc_binint returns an unsigned
|
|
|
|
* int when chewing on 1 byte.
|
|
|
|
*/
|
|
|
|
assert(i >= 0);
|
2003-02-13 11:44:41 -04:00
|
|
|
if (i <= HIGHEST_PROTOCOL)
|
2003-02-02 12:09:05 -04:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
PyErr_Format(PyExc_ValueError, "unsupported pickle protocol: %d", i);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
static PyObject *
|
2003-02-01 02:24:36 -04:00
|
|
|
load(Unpicklerobject *self)
|
2002-04-01 13:40:08 -04:00
|
|
|
{
|
|
|
|
PyObject *err = 0, *val = 0;
|
|
|
|
char *s;
|
|
|
|
|
|
|
|
self->num_marks = 0;
|
|
|
|
if (self->stack->length) Pdata_clear(self->stack, 0);
|
|
|
|
|
|
|
|
while (1) {
|
2003-02-02 14:29:33 -04:00
|
|
|
if (self->read_func(self, &s, 1) < 0)
|
2002-04-01 13:40:08 -04:00
|
|
|
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;
|
|
|
|
|
cPickle.c: Full support for the new LONG1 and LONG4. Added comments.
Assorted code cleanups; e.g., sizeof(char) is 1 by definition, so there's
no need to do things like multiply by sizeof(char) in hairy malloc
arguments. Fixed an undetected-overflow bug in readline_file().
longobject.c: Fixed a really stupid bug in the new _PyLong_NumBits.
pickle.py: Fixed stupid bug in save_long(): When proto is 2, it
wrote LONG1 or LONG4, but forgot to return then -- it went on to
append the proto 1 LONG opcode too.
Fixed equally stupid cancelling bugs in load_long1() and
load_long4(): they *returned* the unpickled long instead of pushing
it on the stack. The return values were ignored. Tests passed
before only because save_long() pickled the long twice.
Fixed bugs in encode_long().
Noted that decode_long() is quadratic-time despite our hopes,
because long(string, 16) is still quadratic-time in len(string).
It's hex() that's linear-time. I don't know a way to make decode_long()
linear-time in Python, short of maybe transforming the 256's-complement
bytes into marshal's funky internal format, and letting marshal decode
that. It would be more valuable to make long(string, 16) linear time.
pickletester.py: Added a global "protocols" vector so tests can try
all the protocols in a sane way. Changed test_ints() and test_unicode()
to do so. Added a new test_long(), but the tail end of it is disabled
because it "takes forever" under pickle.py (but runs very quickly under
cPickle: cPickle proto 2 for longs is linear-time).
2003-02-01 22:57:53 -04:00
|
|
|
case LONG1:
|
|
|
|
if (load_counted_long(self, 1) < 0)
|
|
|
|
break;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case LONG4:
|
|
|
|
if (load_counted_long(self, 4) < 0)
|
|
|
|
break;
|
|
|
|
continue;
|
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
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;
|
1997-04-09 14:36:32 -03:00
|
|
|
|
2001-08-17 15:39:25 -03:00
|
|
|
#ifdef Py_USING_UNICODE
|
2002-04-01 13:40:08 -04:00
|
|
|
case UNICODE:
|
|
|
|
if (load_unicode(self) < 0)
|
|
|
|
break;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case BINUNICODE:
|
|
|
|
if (load_binunicode(self) < 0)
|
|
|
|
break;
|
|
|
|
continue;
|
2001-08-17 15:39:25 -03:00
|
|
|
#endif
|
2000-03-10 19:11:40 -04:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
case EMPTY_TUPLE:
|
2003-02-02 16:29:39 -04:00
|
|
|
if (load_counted_tuple(self, 0) < 0)
|
|
|
|
break;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case TUPLE1:
|
|
|
|
if (load_counted_tuple(self, 1) < 0)
|
|
|
|
break;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case TUPLE2:
|
|
|
|
if (load_counted_tuple(self, 2) < 0)
|
|
|
|
break;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case TUPLE3:
|
|
|
|
if (load_counted_tuple(self, 3) < 0)
|
2002-04-01 13:40:08 -04:00
|
|
|
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;
|
|
|
|
|
2003-02-13 14:24:14 -04:00
|
|
|
case NEWOBJ:
|
|
|
|
if (load_newobj(self) < 0)
|
|
|
|
break;
|
|
|
|
continue;
|
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
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;
|
|
|
|
|
|
|
|
case BUILD:
|
|
|
|
if (load_build(self) < 0)
|
|
|
|
break;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
case GET:
|
|
|
|
if (load_get(self) < 0)
|
|
|
|
break;
|
|
|
|
continue;
|
|
|
|
|
2003-02-04 01:06:17 -04:00
|
|
|
case EXT1:
|
|
|
|
if (load_extension(self, 1) < 0)
|
|
|
|
break;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case EXT2:
|
|
|
|
if (load_extension(self, 2) < 0)
|
|
|
|
break;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case EXT4:
|
|
|
|
if (load_extension(self, 4) < 0)
|
|
|
|
break;
|
|
|
|
continue;
|
2002-04-01 13:40:08 -04:00
|
|
|
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;
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
2003-02-02 12:09:05 -04:00
|
|
|
case PROTO:
|
|
|
|
if (load_proto(self) < 0)
|
|
|
|
break;
|
|
|
|
continue;
|
|
|
|
|
2003-02-02 13:59:11 -04:00
|
|
|
case NEWTRUE:
|
|
|
|
if (load_bool(self, Py_True) < 0)
|
|
|
|
break;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case NEWFALSE:
|
|
|
|
if (load_bool(self, Py_False) < 0)
|
|
|
|
break;
|
|
|
|
continue;
|
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
case '\0':
|
|
|
|
/* end of file */
|
|
|
|
PyErr_SetNone(PyExc_EOFError);
|
|
|
|
break;
|
2003-02-01 02:24:36 -04:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
default:
|
2003-02-01 02:24:36 -04:00
|
|
|
cPickle_ErrFormat(UnpicklingError,
|
2002-04-01 13:40:08 -04:00
|
|
|
"invalid load key, '%s'.",
|
|
|
|
"c", s[0]);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
if ((err = PyErr_Occurred())) {
|
|
|
|
if (err == PyExc_EOFError) {
|
|
|
|
PyErr_SetNone(PyExc_EOFError);
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
PDATA_POP(self->stack, val);
|
|
|
|
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
|
2003-02-01 02:24:36 -04:00
|
|
|
noload_obj(Unpicklerobject *self)
|
2002-04-01 13:40:08 -04:00
|
|
|
{
|
|
|
|
int i;
|
1997-12-03 21:13:01 -04:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
if ((i = marker(self)) < 0) return -1;
|
|
|
|
return Pdata_clear(self->stack, i+1);
|
1997-12-03 21:13:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
2003-02-01 02:24:36 -04:00
|
|
|
noload_inst(Unpicklerobject *self)
|
2002-04-01 13:40:08 -04:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
char *s;
|
|
|
|
|
|
|
|
if ((i = marker(self)) < 0) return -1;
|
|
|
|
Pdata_clear(self->stack, i);
|
2003-02-02 14:29:33 -04:00
|
|
|
if (self->readline_func(self, &s) < 0) return -1;
|
|
|
|
if (self->readline_func(self, &s) < 0) return -1;
|
2003-02-13 14:24:14 -04:00
|
|
|
PDATA_APPEND(self->stack, Py_None, -1);
|
2002-04-01 13:40:08 -04:00
|
|
|
return 0;
|
1997-12-03 21:13:01 -04:00
|
|
|
}
|
|
|
|
|
2003-02-13 14:24:14 -04:00
|
|
|
static int
|
|
|
|
noload_newobj(Unpicklerobject *self)
|
|
|
|
{
|
|
|
|
PyObject *obj;
|
|
|
|
|
|
|
|
PDATA_POP(self->stack, obj); /* pop argtuple */
|
|
|
|
if (obj == NULL) return -1;
|
|
|
|
Py_DECREF(obj);
|
|
|
|
|
|
|
|
PDATA_POP(self->stack, obj); /* pop cls */
|
|
|
|
if (obj == NULL) return -1;
|
|
|
|
Py_DECREF(obj);
|
|
|
|
|
|
|
|
PDATA_APPEND(self->stack, Py_None, -1);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
1997-12-03 21:13:01 -04:00
|
|
|
static int
|
2003-02-01 02:24:36 -04:00
|
|
|
noload_global(Unpicklerobject *self)
|
2002-04-01 13:40:08 -04:00
|
|
|
{
|
|
|
|
char *s;
|
1997-12-03 21:13:01 -04:00
|
|
|
|
2003-02-02 14:29:33 -04:00
|
|
|
if (self->readline_func(self, &s) < 0) return -1;
|
|
|
|
if (self->readline_func(self, &s) < 0) return -1;
|
2002-04-01 13:40:08 -04:00
|
|
|
PDATA_APPEND(self->stack, Py_None,-1);
|
|
|
|
return 0;
|
1997-12-03 21:13:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2003-02-01 02:24:36 -04:00
|
|
|
noload_reduce(Unpicklerobject *self)
|
2002-04-01 13:40:08 -04:00
|
|
|
{
|
1997-12-03 21:13:01 -04:00
|
|
|
|
2002-04-01 13:40:08 -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
|
|
|
}
|
|
|
|
|
2003-02-04 01:06:17 -04:00
|
|
|
static int
|
|
|
|
noload_extension(Unpicklerobject *self, int nbytes)
|
|
|
|
{
|
|
|
|
char *codebytes;
|
|
|
|
|
|
|
|
assert(nbytes == 1 || nbytes == 2 || nbytes == 4);
|
|
|
|
if (self->read_func(self, &codebytes, nbytes) < 0) return -1;
|
|
|
|
PDATA_APPEND(self->stack, Py_None, -1);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
1997-12-03 21:13:01 -04:00
|
|
|
|
|
|
|
static PyObject *
|
2003-02-01 02:24:36 -04:00
|
|
|
noload(Unpicklerobject *self)
|
2002-04-01 13:40:08 -04:00
|
|
|
{
|
|
|
|
PyObject *err = 0, *val = 0;
|
|
|
|
char *s;
|
|
|
|
|
|
|
|
self->num_marks = 0;
|
|
|
|
Pdata_clear(self->stack, 0);
|
|
|
|
|
|
|
|
while (1) {
|
2003-02-02 14:29:33 -04:00
|
|
|
if (self->read_func(self, &s, 1) < 0)
|
2002-04-01 13:40:08 -04:00
|
|
|
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;
|
|
|
|
|
2003-02-02 12:09:05 -04:00
|
|
|
case LONG1:
|
|
|
|
if (load_counted_long(self, 1) < 0)
|
|
|
|
break;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case LONG4:
|
|
|
|
if (load_counted_long(self, 4) < 0)
|
|
|
|
break;
|
|
|
|
continue;
|
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
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;
|
1997-12-03 21:13:01 -04:00
|
|
|
|
2001-08-17 15:39:25 -03:00
|
|
|
#ifdef Py_USING_UNICODE
|
2002-04-01 13:40:08 -04:00
|
|
|
case UNICODE:
|
|
|
|
if (load_unicode(self) < 0)
|
|
|
|
break;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case BINUNICODE:
|
|
|
|
if (load_binunicode(self) < 0)
|
|
|
|
break;
|
|
|
|
continue;
|
2001-08-17 15:39:25 -03:00
|
|
|
#endif
|
2000-03-10 19:11:40 -04:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
case EMPTY_TUPLE:
|
2003-02-02 16:29:39 -04:00
|
|
|
if (load_counted_tuple(self, 0) < 0)
|
|
|
|
break;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case TUPLE1:
|
|
|
|
if (load_counted_tuple(self, 1) < 0)
|
|
|
|
break;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case TUPLE2:
|
|
|
|
if (load_counted_tuple(self, 2) < 0)
|
|
|
|
break;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case TUPLE3:
|
|
|
|
if (load_counted_tuple(self, 3) < 0)
|
2002-04-01 13:40:08 -04:00
|
|
|
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;
|
|
|
|
|
2003-02-13 14:24:14 -04:00
|
|
|
case NEWOBJ:
|
|
|
|
if (noload_newobj(self) < 0)
|
|
|
|
break;
|
|
|
|
continue;
|
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
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;
|
|
|
|
|
|
|
|
case BUILD:
|
|
|
|
if (noload_build(self) < 0)
|
|
|
|
break;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
case GET:
|
|
|
|
if (load_get(self) < 0)
|
|
|
|
break;
|
|
|
|
continue;
|
|
|
|
|
2003-02-04 01:06:17 -04:00
|
|
|
case EXT1:
|
|
|
|
if (noload_extension(self, 1) < 0)
|
|
|
|
break;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case EXT2:
|
|
|
|
if (noload_extension(self, 2) < 0)
|
|
|
|
break;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case EXT4:
|
|
|
|
if (noload_extension(self, 4) < 0)
|
|
|
|
break;
|
|
|
|
continue;
|
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
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;
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
2003-02-02 12:09:05 -04:00
|
|
|
case PROTO:
|
|
|
|
if (load_proto(self) < 0)
|
|
|
|
break;
|
|
|
|
continue;
|
|
|
|
|
2003-02-02 13:59:11 -04:00
|
|
|
case NEWTRUE:
|
|
|
|
if (load_bool(self, Py_True) < 0)
|
|
|
|
break;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case NEWFALSE:
|
|
|
|
if (load_bool(self, Py_False) < 0)
|
|
|
|
break;
|
|
|
|
continue;
|
2002-04-01 13:40:08 -04:00
|
|
|
default:
|
2003-02-01 02:24:36 -04:00
|
|
|
cPickle_ErrFormat(UnpicklingError,
|
2002-04-01 13:40:08 -04:00
|
|
|
"invalid load key, '%s'.",
|
|
|
|
"c", s[0]);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((err = PyErr_Occurred())) {
|
|
|
|
if (err == PyExc_EOFError) {
|
|
|
|
PyErr_SetNone(PyExc_EOFError);
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
PDATA_POP(self->stack, val);
|
|
|
|
return val;
|
1997-12-03 21:13:01 -04:00
|
|
|
}
|
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 *
|
2006-05-29 18:04:52 -03:00
|
|
|
Unpickler_load(Unpicklerobject *self, PyObject *unused)
|
2002-04-01 13:40:08 -04:00
|
|
|
{
|
|
|
|
return load(self);
|
1997-04-09 14:36:32 -03:00
|
|
|
}
|
|
|
|
|
1997-12-03 21:13:01 -04:00
|
|
|
static PyObject *
|
2006-05-29 18:04:52 -03:00
|
|
|
Unpickler_noload(Unpicklerobject *self, PyObject *unused)
|
2002-04-01 13:40:08 -04:00
|
|
|
{
|
|
|
|
return noload(self);
|
1997-12-03 21:13:01 -04:00
|
|
|
}
|
|
|
|
|
1997-04-09 14:36:32 -03:00
|
|
|
|
|
|
|
static struct PyMethodDef Unpickler_methods[] = {
|
2006-05-29 18:04:52 -03:00
|
|
|
{"load", (PyCFunction)Unpickler_load, METH_NOARGS,
|
2002-08-13 19:20:41 -03:00
|
|
|
PyDoc_STR("load() -- Load a pickle")
|
1997-12-03 21:13:01 -04:00
|
|
|
},
|
2006-05-29 18:04:52 -03:00
|
|
|
{"noload", (PyCFunction)Unpickler_noload, METH_NOARGS,
|
2002-08-13 19:20:41 -03:00
|
|
|
PyDoc_STR(
|
1997-12-03 21:13:01 -04:00
|
|
|
"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"
|
2002-08-13 19:20:41 -03:00
|
|
|
"any modules.\n")
|
1997-12-03 21:13:01 -04:00
|
|
|
},
|
1997-04-09 14:36:32 -03:00
|
|
|
{NULL, NULL} /* sentinel */
|
1997-01-06 18:59:08 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
static Unpicklerobject *
|
2003-02-01 02:24:36 -04:00
|
|
|
newUnpicklerobject(PyObject *f)
|
2002-04-01 13:40:08 -04:00
|
|
|
{
|
|
|
|
Unpicklerobject *self;
|
|
|
|
|
2003-04-09 18:25:30 -03:00
|
|
|
if (!( self = PyObject_GC_New(Unpicklerobject, &Unpicklertype)))
|
2002-04-01 13:40:08 -04:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
self->file = NULL;
|
|
|
|
self->arg = NULL;
|
|
|
|
self->stack = (Pdata*)Pdata_New();
|
|
|
|
self->pers_func = NULL;
|
|
|
|
self->last_string = NULL;
|
|
|
|
self->marks = NULL;
|
|
|
|
self->num_marks = 0;
|
|
|
|
self->marks_size = 0;
|
|
|
|
self->buf_size = 0;
|
|
|
|
self->read = NULL;
|
|
|
|
self->readline = NULL;
|
|
|
|
self->find_class = NULL;
|
|
|
|
|
2003-02-01 02:24:36 -04:00
|
|
|
if (!( self->memo = PyDict_New()))
|
2002-04-01 13:40:08 -04:00
|
|
|
goto err;
|
|
|
|
|
2006-07-22 13:20:49 -03:00
|
|
|
if (!self->stack)
|
|
|
|
goto err;
|
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
Py_INCREF(f);
|
|
|
|
self->file = f;
|
|
|
|
|
|
|
|
/* Set read, readline based on type of f */
|
|
|
|
if (PyFile_Check(f)) {
|
|
|
|
self->fp = PyFile_AsFile(f);
|
|
|
|
if (self->fp == NULL) {
|
2003-02-01 02:24:36 -04:00
|
|
|
PyErr_SetString(PyExc_ValueError,
|
2002-04-01 13:40:08 -04:00
|
|
|
"I/O operation on closed file");
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
self->read_func = read_file;
|
|
|
|
self->readline_func = readline_file;
|
|
|
|
}
|
|
|
|
else if (PycStringIO_InputCheck(f)) {
|
|
|
|
self->fp = NULL;
|
|
|
|
self->read_func = read_cStringIO;
|
|
|
|
self->readline_func = readline_cStringIO;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
|
|
|
|
self->fp = NULL;
|
|
|
|
self->read_func = read_other;
|
|
|
|
self->readline_func = readline_other;
|
|
|
|
|
|
|
|
if (!( (self->readline = PyObject_GetAttr(f, readline_str)) &&
|
|
|
|
(self->read = PyObject_GetAttr(f, read_str)))) {
|
|
|
|
PyErr_Clear();
|
2003-02-01 02:24:36 -04:00
|
|
|
PyErr_SetString( PyExc_TypeError,
|
2002-04-01 13:40:08 -04:00
|
|
|
"argument must have 'read' and "
|
|
|
|
"'readline' attributes" );
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
}
|
2003-04-09 18:25:30 -03:00
|
|
|
PyObject_GC_Track(self);
|
2002-04-01 13:40:08 -04:00
|
|
|
|
|
|
|
return self;
|
|
|
|
|
|
|
|
err:
|
|
|
|
Py_DECREF((PyObject *)self);
|
|
|
|
return NULL;
|
1997-01-06 18:59:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static PyObject *
|
2006-05-29 18:04:52 -03:00
|
|
|
get_Unpickler(PyObject *self, PyObject *file)
|
2002-04-01 13:40:08 -04:00
|
|
|
{
|
|
|
|
return (PyObject *)newUnpicklerobject(file);
|
1997-01-06 18:59:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
2003-02-01 02:24:36 -04:00
|
|
|
Unpickler_dealloc(Unpicklerobject *self)
|
2002-04-01 13:40:08 -04:00
|
|
|
{
|
2003-04-09 18:25:30 -03:00
|
|
|
PyObject_GC_UnTrack((PyObject *)self);
|
2002-04-01 13:40:08 -04:00
|
|
|
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);
|
2003-07-11 16:42:49 -03:00
|
|
|
Py_XDECREF(self->find_class);
|
2002-04-01 13:40:08 -04:00
|
|
|
|
|
|
|
if (self->marks) {
|
|
|
|
free(self->marks);
|
|
|
|
}
|
1997-04-09 14:36:32 -03:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
if (self->buf_size) {
|
|
|
|
free(self->buf);
|
|
|
|
}
|
2001-03-17 00:50:51 -04:00
|
|
|
|
2003-05-21 18:29:48 -03:00
|
|
|
self->ob_type->tp_free((PyObject *)self);
|
1997-01-06 18:59:08 -04:00
|
|
|
}
|
|
|
|
|
2003-04-09 18:25:30 -03:00
|
|
|
static int
|
|
|
|
Unpickler_traverse(Unpicklerobject *self, visitproc visit, void *arg)
|
|
|
|
{
|
2006-04-15 18:47:09 -03:00
|
|
|
Py_VISIT(self->readline);
|
|
|
|
Py_VISIT(self->read);
|
|
|
|
Py_VISIT(self->file);
|
|
|
|
Py_VISIT(self->memo);
|
|
|
|
Py_VISIT(self->stack);
|
|
|
|
Py_VISIT(self->pers_func);
|
|
|
|
Py_VISIT(self->arg);
|
|
|
|
Py_VISIT(self->last_string);
|
|
|
|
Py_VISIT(self->find_class);
|
2003-04-09 18:25:30 -03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
Unpickler_clear(Unpicklerobject *self)
|
|
|
|
{
|
2006-04-15 14:28:34 -03:00
|
|
|
Py_CLEAR(self->readline);
|
|
|
|
Py_CLEAR(self->read);
|
|
|
|
Py_CLEAR(self->file);
|
|
|
|
Py_CLEAR(self->memo);
|
|
|
|
Py_CLEAR(self->stack);
|
|
|
|
Py_CLEAR(self->pers_func);
|
|
|
|
Py_CLEAR(self->arg);
|
|
|
|
Py_CLEAR(self->last_string);
|
|
|
|
Py_CLEAR(self->find_class);
|
2003-04-09 18:25:30 -03:00
|
|
|
return 0;
|
|
|
|
}
|
1997-01-06 18:59:08 -04:00
|
|
|
|
|
|
|
static PyObject *
|
2003-02-01 02:24:36 -04:00
|
|
|
Unpickler_getattr(Unpicklerobject *self, char *name)
|
2002-04-01 13:40:08 -04:00
|
|
|
{
|
|
|
|
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
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
if (!strcmp(name, "find_global")) {
|
|
|
|
if (!self->find_class) {
|
|
|
|
PyErr_SetString(PyExc_AttributeError, name);
|
|
|
|
return NULL;
|
|
|
|
}
|
1999-04-19 14:58:18 -03:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
Py_INCREF(self->find_class);
|
|
|
|
return self->find_class;
|
|
|
|
}
|
1999-04-19 14:58:18 -03:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
if (!strcmp(name, "memo")) {
|
|
|
|
if (!self->memo) {
|
|
|
|
PyErr_SetString(PyExc_AttributeError, name);
|
|
|
|
return NULL;
|
|
|
|
}
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
Py_INCREF(self->memo);
|
|
|
|
return self->memo;
|
|
|
|
}
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
if (!strcmp(name, "UnpicklingError")) {
|
|
|
|
Py_INCREF(UnpicklingError);
|
|
|
|
return UnpicklingError;
|
|
|
|
}
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
return Py_FindMethod(Unpickler_methods, (PyObject *)self, name);
|
1997-01-06 18:59:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
2003-02-01 02:24:36 -04:00
|
|
|
Unpickler_setattr(Unpicklerobject *self, char *name, PyObject *value)
|
2002-04-01 13:40:08 -04:00
|
|
|
{
|
|
|
|
|
|
|
|
if (!strcmp(name, "persistent_load")) {
|
|
|
|
Py_XDECREF(self->pers_func);
|
|
|
|
self->pers_func = value;
|
|
|
|
Py_XINCREF(value);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!strcmp(name, "find_global")) {
|
|
|
|
Py_XDECREF(self->find_class);
|
|
|
|
self->find_class = value;
|
|
|
|
Py_XINCREF(value);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (! value) {
|
|
|
|
PyErr_SetString(PyExc_TypeError,
|
|
|
|
"attribute deletion is not supported");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strcmp(name, "memo") == 0) {
|
|
|
|
if (!PyDict_Check(value)) {
|
2003-02-01 02:24:36 -04:00
|
|
|
PyErr_SetString(PyExc_TypeError,
|
2002-04-01 13:40:08 -04:00
|
|
|
"memo must be a dictionary");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
Py_XDECREF(self->memo);
|
|
|
|
self->memo = value;
|
|
|
|
Py_INCREF(value);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
PyErr_SetString(PyExc_AttributeError, name);
|
|
|
|
return -1;
|
1997-01-06 18:59:08 -04:00
|
|
|
}
|
|
|
|
|
2003-02-01 12:45:06 -04:00
|
|
|
/* ---------------------------------------------------------------------------
|
|
|
|
* Module-level functions.
|
|
|
|
*/
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2004-07-27 02:22:33 -03:00
|
|
|
/* dump(obj, file, protocol=0). */
|
1997-01-06 18:59:08 -04:00
|
|
|
static PyObject *
|
2004-07-27 02:22:33 -03:00
|
|
|
cpm_dump(PyObject *self, PyObject *args, PyObject *kwds)
|
2002-04-01 13:40:08 -04:00
|
|
|
{
|
2006-02-27 12:46:16 -04:00
|
|
|
static char *kwlist[] = {"obj", "file", "protocol", NULL};
|
2002-04-01 13:40:08 -04:00
|
|
|
PyObject *ob, *file, *res = NULL;
|
|
|
|
Picklerobject *pickler = 0;
|
2003-02-01 12:45:06 -04:00
|
|
|
int proto = 0;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2004-07-27 02:22:33 -03:00
|
|
|
if (!( PyArg_ParseTupleAndKeywords(args, kwds, "OO|i", kwlist,
|
|
|
|
&ob, &file, &proto)))
|
2002-04-01 13:40:08 -04:00
|
|
|
goto finally;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2003-02-01 12:45:06 -04:00
|
|
|
if (!( pickler = newPicklerobject(file, proto)))
|
2002-04-01 13:40:08 -04:00
|
|
|
goto finally;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
if (dump(pickler, ob) < 0)
|
|
|
|
goto finally;
|
1997-04-09 14:36:32 -03:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
Py_INCREF(Py_None);
|
|
|
|
res = Py_None;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
finally:
|
|
|
|
Py_XDECREF(pickler);
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
return res;
|
1997-01-06 18:59:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-07-27 02:22:33 -03:00
|
|
|
/* dumps(obj, protocol=0). */
|
1997-01-06 18:59:08 -04:00
|
|
|
static PyObject *
|
2004-07-27 02:22:33 -03:00
|
|
|
cpm_dumps(PyObject *self, PyObject *args, PyObject *kwds)
|
2002-04-01 13:40:08 -04:00
|
|
|
{
|
2006-02-27 12:46:16 -04:00
|
|
|
static char *kwlist[] = {"obj", "protocol", NULL};
|
2002-04-01 13:40:08 -04:00
|
|
|
PyObject *ob, *file = 0, *res = NULL;
|
|
|
|
Picklerobject *pickler = 0;
|
2003-02-01 12:45:06 -04:00
|
|
|
int proto = 0;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2004-07-27 02:22:33 -03:00
|
|
|
if (!( PyArg_ParseTupleAndKeywords(args, kwds, "O|i:dumps", kwlist,
|
|
|
|
&ob, &proto)))
|
2002-04-01 13:40:08 -04:00
|
|
|
goto finally;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2003-02-01 02:24:36 -04:00
|
|
|
if (!( file = PycStringIO->NewOutput(128)))
|
2002-04-01 13:40:08 -04:00
|
|
|
goto finally;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2003-02-01 12:45:06 -04:00
|
|
|
if (!( pickler = newPicklerobject(file, proto)))
|
2002-04-01 13:40:08 -04:00
|
|
|
goto finally;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
if (dump(pickler, ob) < 0)
|
|
|
|
goto finally;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
res = PycStringIO->cgetvalue(file);
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
finally:
|
|
|
|
Py_XDECREF(pickler);
|
|
|
|
Py_XDECREF(file);
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
return res;
|
2001-03-17 00:50:51 -04:00
|
|
|
}
|
|
|
|
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2003-02-01 12:45:06 -04:00
|
|
|
/* load(fileobj). */
|
1997-01-06 18:59:08 -04:00
|
|
|
static PyObject *
|
2006-05-29 18:04:52 -03:00
|
|
|
cpm_load(PyObject *self, PyObject *ob)
|
2002-04-01 13:40:08 -04:00
|
|
|
{
|
|
|
|
Unpicklerobject *unpickler = 0;
|
2006-05-29 18:04:52 -03:00
|
|
|
PyObject *res = NULL;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2003-02-01 02:24:36 -04:00
|
|
|
if (!( unpickler = newUnpicklerobject(ob)))
|
2002-04-01 13:40:08 -04:00
|
|
|
goto finally;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
res = load(unpickler);
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
finally:
|
|
|
|
Py_XDECREF(unpickler);
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
return res;
|
1997-01-06 18:59:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-02-01 12:45:06 -04:00
|
|
|
/* loads(string) */
|
1997-01-06 18:59:08 -04:00
|
|
|
static PyObject *
|
2003-02-01 02:24:36 -04:00
|
|
|
cpm_loads(PyObject *self, PyObject *args)
|
2002-04-01 13:40:08 -04:00
|
|
|
{
|
|
|
|
PyObject *ob, *file = 0, *res = NULL;
|
|
|
|
Unpicklerobject *unpickler = 0;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2003-02-01 02:24:36 -04:00
|
|
|
if (!( PyArg_ParseTuple(args, "S:loads", &ob)))
|
2002-04-01 13:40:08 -04:00
|
|
|
goto finally;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2003-02-01 02:24:36 -04:00
|
|
|
if (!( file = PycStringIO->NewInput(ob)))
|
2002-04-01 13:40:08 -04:00
|
|
|
goto finally;
|
2001-03-17 00:50:51 -04:00
|
|
|
|
2003-02-01 02:24:36 -04:00
|
|
|
if (!( unpickler = newUnpicklerobject(file)))
|
2002-04-01 13:40:08 -04:00
|
|
|
goto finally;
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
res = load(unpickler);
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
finally:
|
|
|
|
Py_XDECREF(file);
|
|
|
|
Py_XDECREF(unpickler);
|
1997-01-06 18:59:08 -04:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
return res;
|
1997-01-06 18:59:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-06-13 17:33:02 -03:00
|
|
|
PyDoc_STRVAR(Unpicklertype__doc__,
|
|
|
|
"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)
|
2003-04-09 18:25:30 -03:00
|
|
|
0, /*ob_size*/
|
|
|
|
"cPickle.Unpickler", /*tp_name*/
|
|
|
|
sizeof(Unpicklerobject), /*tp_basicsize*/
|
|
|
|
0,
|
|
|
|
(destructor)Unpickler_dealloc, /* tp_dealloc */
|
|
|
|
0, /* tp_print */
|
|
|
|
(getattrfunc)Unpickler_getattr, /* tp_getattr */
|
|
|
|
(setattrfunc)Unpickler_setattr, /* tp_setattr */
|
|
|
|
0, /* tp_compare */
|
|
|
|
0, /* tp_repr */
|
|
|
|
0, /* tp_as_number */
|
|
|
|
0, /* tp_as_sequence */
|
|
|
|
0, /* tp_as_mapping */
|
|
|
|
0, /* tp_hash */
|
|
|
|
0, /* tp_call */
|
|
|
|
0, /* tp_str */
|
|
|
|
0, /* tp_getattro */
|
|
|
|
0, /* tp_setattro */
|
|
|
|
0, /* tp_as_buffer */
|
|
|
|
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
|
|
|
|
Unpicklertype__doc__, /* tp_doc */
|
|
|
|
(traverseproc)Unpickler_traverse, /* tp_traverse */
|
|
|
|
(inquiry)Unpickler_clear, /* tp_clear */
|
1997-12-08 11:15:16 -04:00
|
|
|
};
|
1997-04-09 14:36:32 -03:00
|
|
|
|
|
|
|
static struct PyMethodDef cPickle_methods[] = {
|
2004-07-27 02:22:33 -03:00
|
|
|
{"dump", (PyCFunction)cpm_dump, METH_VARARGS | METH_KEYWORDS,
|
|
|
|
PyDoc_STR("dump(obj, file, protocol=0) -- "
|
2003-02-01 12:45:06 -04:00
|
|
|
"Write an object in pickle format to the given file.\n"
|
1997-12-03 21:13:01 -04:00
|
|
|
"\n"
|
2003-02-01 12:45:06 -04:00
|
|
|
"See the Pickler docstring for the meaning of optional argument proto.")
|
1997-12-03 21:13:01 -04:00
|
|
|
},
|
2003-02-01 12:45:06 -04:00
|
|
|
|
2004-07-27 02:22:33 -03:00
|
|
|
{"dumps", (PyCFunction)cpm_dumps, METH_VARARGS | METH_KEYWORDS,
|
|
|
|
PyDoc_STR("dumps(obj, protocol=0) -- "
|
2003-02-01 12:45:06 -04:00
|
|
|
"Return a string containing an object in pickle format.\n"
|
1997-12-03 21:13:01 -04:00
|
|
|
"\n"
|
2003-02-01 12:45:06 -04:00
|
|
|
"See the Pickler docstring for the meaning of optional argument proto.")
|
1997-12-03 21:13:01 -04:00
|
|
|
},
|
2003-02-01 12:45:06 -04:00
|
|
|
|
2006-05-29 18:04:52 -03:00
|
|
|
{"load", (PyCFunction)cpm_load, METH_O,
|
2002-08-13 19:20:41 -03:00
|
|
|
PyDoc_STR("load(file) -- Load a pickle from the given file")},
|
2003-02-01 12:45:06 -04:00
|
|
|
|
2002-03-31 10:44:22 -04:00
|
|
|
{"loads", (PyCFunction)cpm_loads, METH_VARARGS,
|
2002-08-13 19:20:41 -03:00
|
|
|
PyDoc_STR("loads(string) -- Load a pickle from the given string")},
|
2003-02-01 12:45:06 -04:00
|
|
|
|
2004-07-27 02:22:33 -03:00
|
|
|
{"Pickler", (PyCFunction)get_Pickler, METH_VARARGS | METH_KEYWORDS,
|
|
|
|
PyDoc_STR("Pickler(file, protocol=0) -- Create a pickler.\n"
|
2003-02-01 12:45:06 -04:00
|
|
|
"\n"
|
|
|
|
"This takes a file-like object for writing a pickle data stream.\n"
|
|
|
|
"The optional proto argument tells the pickler to use the given\n"
|
|
|
|
"protocol; supported protocols are 0, 1, 2. The default\n"
|
|
|
|
"protocol is 0, to be backwards compatible. (Protocol 0 is the\n"
|
|
|
|
"only protocol that can be written to a file opened in text\n"
|
|
|
|
"mode and read back successfully. When using a protocol higher\n"
|
|
|
|
"than 0, make sure the file is opened in binary mode, both when\n"
|
|
|
|
"pickling and unpickling.)\n"
|
|
|
|
"\n"
|
|
|
|
"Protocol 1 is more efficient than protocol 0; protocol 2 is\n"
|
|
|
|
"more efficient than protocol 1.\n"
|
|
|
|
"\n"
|
|
|
|
"Specifying a negative protocol version selects the highest\n"
|
|
|
|
"protocol version supported. The higher the protocol used, the\n"
|
|
|
|
"more recent the version of Python needed to read the pickle\n"
|
|
|
|
"produced.\n"
|
1997-12-03 21:13:01 -04:00
|
|
|
"\n"
|
2003-02-01 12:45:06 -04:00
|
|
|
"The file parameter must have a write() method that accepts a single\n"
|
|
|
|
"string argument. It can thus be an open file object, a StringIO\n"
|
|
|
|
"object, or any other custom object that meets this interface.\n")
|
1997-12-03 21:13:01 -04:00
|
|
|
},
|
2003-02-01 12:45:06 -04:00
|
|
|
|
2006-05-29 18:04:52 -03:00
|
|
|
{"Unpickler", (PyCFunction)get_Unpickler, METH_O,
|
2003-02-01 12:45:06 -04:00
|
|
|
PyDoc_STR("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
|
2003-02-01 02:24:36 -04:00
|
|
|
init_stuff(PyObject *module_dict)
|
2002-04-01 13:40:08 -04:00
|
|
|
{
|
|
|
|
PyObject *copy_reg, *t, *r;
|
1997-04-09 14:36:32 -03:00
|
|
|
|
2002-04-01 08:34:33 -04:00
|
|
|
#define INIT_STR(S) if (!( S ## _str=PyString_InternFromString(#S))) return -1;
|
1997-04-09 14:36:32 -03:00
|
|
|
|
2003-05-21 18:29:48 -03:00
|
|
|
if (PyType_Ready(&Unpicklertype) < 0)
|
|
|
|
return -1;
|
|
|
|
if (PyType_Ready(&Picklertype) < 0)
|
|
|
|
return -1;
|
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
INIT_STR(__class__);
|
|
|
|
INIT_STR(__getinitargs__);
|
|
|
|
INIT_STR(__dict__);
|
|
|
|
INIT_STR(__getstate__);
|
|
|
|
INIT_STR(__setstate__);
|
|
|
|
INIT_STR(__name__);
|
|
|
|
INIT_STR(__main__);
|
|
|
|
INIT_STR(__reduce__);
|
2003-02-18 21:45:13 -04:00
|
|
|
INIT_STR(__reduce_ex__);
|
2002-04-01 13:40:08 -04:00
|
|
|
INIT_STR(write);
|
|
|
|
INIT_STR(append);
|
|
|
|
INIT_STR(read);
|
|
|
|
INIT_STR(readline);
|
|
|
|
INIT_STR(copy_reg);
|
|
|
|
INIT_STR(dispatch_table);
|
|
|
|
|
2003-02-01 02:24:36 -04:00
|
|
|
if (!( copy_reg = PyImport_ImportModule("copy_reg")))
|
2002-04-01 13:40:08 -04:00
|
|
|
return -1;
|
|
|
|
|
2003-01-31 22:16:37 -04:00
|
|
|
/* This is special because we want to use a different
|
|
|
|
one in restricted mode. */
|
2002-04-01 13:40:08 -04:00
|
|
|
dispatch_table = PyObject_GetAttr(copy_reg, dispatch_table_str);
|
2003-02-03 20:21:07 -04:00
|
|
|
if (!dispatch_table) return -1;
|
|
|
|
|
|
|
|
extension_registry = PyObject_GetAttrString(copy_reg,
|
2003-02-03 21:54:49 -04:00
|
|
|
"_extension_registry");
|
2003-02-03 20:21:07 -04:00
|
|
|
if (!extension_registry) return -1;
|
|
|
|
|
|
|
|
inverted_registry = PyObject_GetAttrString(copy_reg,
|
2003-02-03 21:54:49 -04:00
|
|
|
"_inverted_registry");
|
2003-02-03 20:21:07 -04:00
|
|
|
if (!inverted_registry) return -1;
|
|
|
|
|
|
|
|
extension_cache = PyObject_GetAttrString(copy_reg,
|
2003-02-03 21:54:49 -04:00
|
|
|
"_extension_cache");
|
2003-02-03 20:21:07 -04:00
|
|
|
if (!extension_cache) return -1;
|
2002-04-01 13:40:08 -04:00
|
|
|
|
|
|
|
Py_DECREF(copy_reg);
|
|
|
|
|
2003-02-04 16:56:09 -04:00
|
|
|
if (!(empty_tuple = PyTuple_New(0)))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
two_tuple = PyTuple_New(2);
|
|
|
|
if (two_tuple == NULL)
|
2002-04-01 13:40:08 -04:00
|
|
|
return -1;
|
2003-02-04 17:47:44 -04:00
|
|
|
/* We use this temp container with no regard to refcounts, or to
|
|
|
|
* keeping containees alive. Exempt from GC, because we don't
|
|
|
|
* want anything looking at two_tuple() by magic.
|
|
|
|
*/
|
|
|
|
PyObject_GC_UnTrack(two_tuple);
|
2002-04-01 13:40:08 -04:00
|
|
|
|
|
|
|
/* Ugh */
|
|
|
|
if (!( t=PyImport_ImportModule("__builtin__"))) return -1;
|
|
|
|
if (PyDict_SetItemString(module_dict, "__builtins__", t) < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (!( t=PyDict_New())) return -1;
|
|
|
|
if (!( r=PyRun_String(
|
|
|
|
"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);
|
|
|
|
|
|
|
|
PickleError = PyErr_NewException("cPickle.PickleError", NULL, t);
|
2003-02-01 02:24:36 -04:00
|
|
|
if (!PickleError)
|
2002-04-01 13:40:08 -04:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
Py_DECREF(t);
|
|
|
|
|
2003-02-01 02:24:36 -04:00
|
|
|
PicklingError = PyErr_NewException("cPickle.PicklingError",
|
2002-04-01 13:40:08 -04:00
|
|
|
PickleError, NULL);
|
2003-02-01 02:24:36 -04:00
|
|
|
if (!PicklingError)
|
2002-04-01 13:40:08 -04:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (!( t=PyDict_New())) return -1;
|
|
|
|
if (!( r=PyRun_String(
|
|
|
|
"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);
|
|
|
|
|
|
|
|
if (!( UnpickleableError = PyErr_NewException(
|
2003-02-01 02:24:36 -04:00
|
|
|
"cPickle.UnpickleableError", PicklingError, t)))
|
2002-04-01 13:40:08 -04:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
Py_DECREF(t);
|
|
|
|
|
|
|
|
if (!( UnpicklingError = PyErr_NewException("cPickle.UnpicklingError",
|
2003-02-01 02:24:36 -04:00
|
|
|
PickleError, NULL)))
|
2002-04-01 13:40:08 -04:00
|
|
|
return -1;
|
|
|
|
|
2002-09-16 14:26:24 -03:00
|
|
|
if (!( BadPickleGet = PyErr_NewException("cPickle.BadPickleGet",
|
|
|
|
UnpicklingError, NULL)))
|
|
|
|
return -1;
|
2003-02-01 02:24:36 -04:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
if (PyDict_SetItemString(module_dict, "PickleError",
|
|
|
|
PickleError) < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (PyDict_SetItemString(module_dict, "PicklingError",
|
|
|
|
PicklingError) < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (PyDict_SetItemString(module_dict, "UnpicklingError",
|
|
|
|
UnpicklingError) < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (PyDict_SetItemString(module_dict, "UnpickleableError",
|
|
|
|
UnpickleableError) < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (PyDict_SetItemString(module_dict, "BadPickleGet",
|
|
|
|
BadPickleGet) < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
PycString_IMPORT;
|
1999-06-09 12:23:31 -03:00
|
|
|
|
2002-04-01 13:40:08 -04:00
|
|
|
return 0;
|
1997-01-06 18:59:08 -04:00
|
|
|
}
|
|
|
|
|
2002-08-01 23:27:13 -03:00
|
|
|
#ifndef PyMODINIT_FUNC /* declarations for DLL import/export */
|
|
|
|
#define PyMODINIT_FUNC void
|
1999-02-04 10:54:04 -04:00
|
|
|
#endif
|
2002-08-01 23:27:13 -03:00
|
|
|
PyMODINIT_FUNC
|
2003-02-01 02:24:36 -04:00
|
|
|
initcPickle(void)
|
2002-04-01 13:40:08 -04:00
|
|
|
{
|
|
|
|
PyObject *m, *d, *di, *v, *k;
|
2006-02-15 13:27:45 -04:00
|
|
|
Py_ssize_t i;
|
2003-02-03 20:21:07 -04:00
|
|
|
char *rev = "1.71"; /* XXX when does this change? */
|
2002-04-01 13:40:08 -04:00
|
|
|
PyObject *format_version;
|
|
|
|
PyObject *compatible_formats;
|
|
|
|
|
|
|
|
Picklertype.ob_type = &PyType_Type;
|
|
|
|
Unpicklertype.ob_type = &PyType_Type;
|
|
|
|
PdataType.ob_type = &PyType_Type;
|
|
|
|
|
|
|
|
/* Initialize some pieces. We need to do this before module creation,
|
2003-02-03 20:21:07 -04:00
|
|
|
* so we're forced to use a temporary dictionary. :(
|
|
|
|
*/
|
|
|
|
di = PyDict_New();
|
2002-04-01 13:40:08 -04:00
|
|
|
if (!di) return;
|
|
|
|
if (init_stuff(di) < 0) return;
|
|
|
|
|
|
|
|
/* Create the module and add the functions */
|
|
|
|
m = Py_InitModule4("cPickle", cPickle_methods,
|
|
|
|
cPickle_module_documentation,
|
|
|
|
(PyObject*)NULL,PYTHON_API_VERSION);
|
2006-01-19 02:09:39 -04:00
|
|
|
if (m == NULL)
|
|
|
|
return;
|
2002-04-01 13:40:08 -04:00
|
|
|
|
|
|
|
/* Add some symbolic constants to the module */
|
|
|
|
d = PyModule_GetDict(m);
|
2003-02-03 20:21:07 -04:00
|
|
|
v = PyString_FromString(rev);
|
|
|
|
PyDict_SetItemString(d, "__version__", v);
|
2002-04-01 13:40:08 -04:00
|
|
|
Py_XDECREF(v);
|
|
|
|
|
|
|
|
/* 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);
|
|
|
|
|
2003-02-13 11:44:41 -04:00
|
|
|
i = PyModule_AddIntConstant(m, "HIGHEST_PROTOCOL", HIGHEST_PROTOCOL);
|
|
|
|
if (i < 0)
|
|
|
|
return;
|
|
|
|
|
2003-02-03 20:21:07 -04:00
|
|
|
/* These are purely informational; no code uses them. */
|
|
|
|
/* File format version we write. */
|
|
|
|
format_version = PyString_FromString("2.0");
|
|
|
|
/* Format versions we can read. */
|
|
|
|
compatible_formats = Py_BuildValue("[sssss]",
|
|
|
|
"1.0", /* Original protocol 0 */
|
|
|
|
"1.1", /* Protocol 0 + INST */
|
|
|
|
"1.2", /* Original protocol 1 */
|
|
|
|
"1.3", /* Protocol 1 + BINFLOAT */
|
2003-02-03 20:30:46 -04:00
|
|
|
"2.0"); /* Original protocol 2 */
|
2002-04-01 13:40:08 -04:00
|
|
|
PyDict_SetItemString(d, "format_version", format_version);
|
|
|
|
PyDict_SetItemString(d, "compatible_formats", compatible_formats);
|
|
|
|
Py_XDECREF(format_version);
|
|
|
|
Py_XDECREF(compatible_formats);
|
1997-01-06 18:59:08 -04:00
|
|
|
}
|