blob: 88f2fc1f8d3e457286de2d91aa1c2e4f1d7db248 [file] [log] [blame]
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001#include "Python.h"
2#include "cStringIO.h"
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003#include "structmember.h"
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005PyDoc_STRVAR(cPickle_module_documentation,
Tim Peters64c04d12003-02-01 06:27:59 +00006"C implementation and optimization of the Python pickle module.");
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007
Guido van Rossum142eeb81997-08-13 03:14:41 +00008#ifndef Py_eval_input
9#include <graminit.h>
10#define Py_eval_input eval_input
Guido van Rossumc6ef2041997-08-21 02:30:45 +000011#endif /* Py_eval_input */
Guido van Rossum142eeb81997-08-13 03:14:41 +000012
Guido van Rossum60456fd1997-04-09 17:36:32 +000013#define DEL_LIST_SLICE(list, from, to) (PyList_SetSlice(list, from, to, NULL))
Guido van Rossum2f4caa41997-01-06 22:59:08 +000014
Guido van Rossum60456fd1997-04-09 17:36:32 +000015#define WRITE_BUF_SIZE 256
16
Tim Peters5bd2a792003-02-01 16:45:06 +000017/* Bump this when new opcodes are added to the pickle protocol. */
Tim Peters8587b3c2003-02-13 15:44:41 +000018#define HIGHEST_PROTOCOL 2
Tim Peters5bd2a792003-02-01 16:45:06 +000019
Tim Peters797ec242003-02-01 06:22:36 +000020/*
21 * Pickle opcodes. These must be kept in synch with pickle.py. Extensive
22 * docs are in pickletools.py.
23 */
Guido van Rossum60456fd1997-04-09 17:36:32 +000024#define MARK '('
25#define STOP '.'
26#define POP '0'
27#define POP_MARK '1'
28#define DUP '2'
29#define FLOAT 'F'
Guido van Rossum60456fd1997-04-09 17:36:32 +000030#define BINFLOAT 'G'
Guido van Rossum60456fd1997-04-09 17:36:32 +000031#define INT 'I'
32#define BININT 'J'
33#define BININT1 'K'
34#define LONG 'L'
35#define BININT2 'M'
36#define NONE 'N'
37#define PERSID 'P'
38#define BINPERSID 'Q'
39#define REDUCE 'R'
40#define STRING 'S'
41#define BINSTRING 'T'
Guido van Rossum2f4caa41997-01-06 22:59:08 +000042#define SHORT_BINSTRING 'U'
Guido van Rossum5fccb7c2000-03-10 23:11:40 +000043#define UNICODE 'V'
44#define BINUNICODE 'X'
Guido van Rossum60456fd1997-04-09 17:36:32 +000045#define APPEND 'a'
46#define BUILD 'b'
47#define GLOBAL 'c'
48#define DICT 'd'
49#define EMPTY_DICT '}'
50#define APPENDS 'e'
51#define GET 'g'
52#define BINGET 'h'
53#define INST 'i'
54#define LONG_BINGET 'j'
55#define LIST 'l'
56#define EMPTY_LIST ']'
57#define OBJ 'o'
58#define PUT 'p'
59#define BINPUT 'q'
60#define LONG_BINPUT 'r'
61#define SETITEM 's'
62#define TUPLE 't'
63#define EMPTY_TUPLE ')'
64#define SETITEMS 'u'
Tim Peters797ec242003-02-01 06:22:36 +000065
66/* Protocol 2. */
67#define PROTO '\x80' /* identify pickle protocol */
68#define NEWOBJ '\x81' /* build object by applying cls.__new__ to argtuple */
69#define EXT1 '\x82' /* push object from extension registry; 1-byte index */
70#define EXT2 '\x83' /* ditto, but 2-byte index */
71#define EXT4 '\x84' /* ditto, but 4-byte index */
72#define TUPLE1 '\x85' /* build 1-tuple from stack top */
73#define TUPLE2 '\x86' /* build 2-tuple from two topmost stack items */
74#define TUPLE3 '\x87' /* build 3-tuple from three topmost stack items */
75#define NEWTRUE '\x88' /* push True */
76#define NEWFALSE '\x89' /* push False */
77#define LONG1 '\x8a' /* push long from < 256 bytes */
78#define LONG4 '\x8b' /* push really big long */
79
80/* There aren't opcodes -- they're ways to pickle bools before protocol 2,
81 * so that unpicklers written before bools were introduced unpickle them
82 * as ints, but unpicklers after can recognize that bools were intended.
83 * Note that protocol 2 added direct ways to pickle bools.
84 */
Jack Jansen3a967022002-06-26 20:40:42 +000085#undef TRUE
Guido van Rossume2763392002-04-05 19:30:08 +000086#define TRUE "I01\n"
Jack Jansen3a967022002-06-26 20:40:42 +000087#undef FALSE
Guido van Rossume2763392002-04-05 19:30:08 +000088#define FALSE "I00\n"
Guido van Rossum77f6a652002-04-03 22:41:51 +000089
Tim Peters1092d642003-02-11 21:06:20 +000090/* Keep in synch with pickle.Pickler._BATCHSIZE. This is how many elements
Tim Peters42f08ac2003-02-11 22:43:24 +000091 * batch_list/dict() pumps out before doing APPENDS/SETITEMS. Nothing will
92 * break if this gets out of synch with pickle.py, but it's unclear that
93 * would help anything either.
Tim Peters1092d642003-02-11 21:06:20 +000094 */
95#define BATCHSIZE 1000
96
Guido van Rossum60456fd1997-04-09 17:36:32 +000097static char MARKv = MARK;
Guido van Rossum2f4caa41997-01-06 22:59:08 +000098
Guido van Rossumc03158b1999-06-09 15:23:31 +000099static PyObject *PickleError;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000100static PyObject *PicklingError;
Guido van Rossumc03158b1999-06-09 15:23:31 +0000101static PyObject *UnpickleableError;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000102static PyObject *UnpicklingError;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000103static PyObject *BadPickleGet;
104
Tim Peters5b7da392003-02-04 00:21:07 +0000105/* As the name says, an empty tuple. */
Guido van Rossum60456fd1997-04-09 17:36:32 +0000106static PyObject *empty_tuple;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000107
Tim Peters5b7da392003-02-04 00:21:07 +0000108/* copy_reg.dispatch_table, {type_object: pickling_function} */
109static PyObject *dispatch_table;
110
111/* For EXT[124] opcodes. */
Guido van Rossumd4b920c2003-02-04 01:54:49 +0000112/* copy_reg._extension_registry, {(module_name, function_name): code} */
Tim Peters5b7da392003-02-04 00:21:07 +0000113static PyObject *extension_registry;
Guido van Rossumd4b920c2003-02-04 01:54:49 +0000114/* copy_reg._inverted_registry, {code: (module_name, function_name)} */
Tim Peters5b7da392003-02-04 00:21:07 +0000115static PyObject *inverted_registry;
Guido van Rossumd4b920c2003-02-04 01:54:49 +0000116/* copy_reg._extension_cache, {code: object} */
Tim Peters5b7da392003-02-04 00:21:07 +0000117static PyObject *extension_cache;
118
Tim Peters731098b2003-02-04 20:56:09 +0000119/* For looking up name pairs in copy_reg._extension_registry. */
120static PyObject *two_tuple;
121
Guido van Rossum60456fd1997-04-09 17:36:32 +0000122static PyObject *__class___str, *__getinitargs___str, *__dict___str,
123 *__getstate___str, *__setstate___str, *__name___str, *__reduce___str,
Guido van Rossumb289b872003-02-19 01:45:13 +0000124 *__reduce_ex___str,
Tim Peters1f1b2d22003-02-01 02:16:37 +0000125 *write_str, *append_str,
Guido van Rossum9716aaa1997-12-08 15:15:16 +0000126 *read_str, *readline_str, *__main___str, *__basicnew___str,
Tim Peters1f1b2d22003-02-01 02:16:37 +0000127 *copy_reg_str, *dispatch_table_str;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000128
Guido van Rossum053b8df1998-11-25 16:18:00 +0000129/*************************************************************************
130 Internal Data type for pickle data. */
131
132typedef struct {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000133 PyObject_HEAD
Tim Peters1d63c9f2003-02-02 20:29:39 +0000134 int length; /* number of initial slots in data currently used */
135 int size; /* number of slots in data allocated */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000136 PyObject **data;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000137} Pdata;
138
Tim Peters84e87f32001-03-17 04:50:51 +0000139static void
Tim Peterscba30e22003-02-01 06:24:36 +0000140Pdata_dealloc(Pdata *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000141{
142 int i;
143 PyObject **p;
Tim Peterscba30e22003-02-01 06:24:36 +0000144
Tim Peters1d63c9f2003-02-02 20:29:39 +0000145 for (i = self->length, p = self->data; --i >= 0; p++) {
146 Py_DECREF(*p);
147 }
148 if (self->data)
149 free(self->data);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000150 PyObject_Del(self);
Guido van Rossum053b8df1998-11-25 16:18:00 +0000151}
152
153static PyTypeObject PdataType = {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000154 PyObject_HEAD_INIT(NULL) 0, "cPickle.Pdata", sizeof(Pdata), 0,
155 (destructor)Pdata_dealloc,
156 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0L,0L,0L,0L, ""
Guido van Rossum053b8df1998-11-25 16:18:00 +0000157};
158
159#define Pdata_Check(O) ((O)->ob_type == &PdataType)
160
161static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +0000162Pdata_New(void)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000163{
164 Pdata *self;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000165
Tim Peters1d63c9f2003-02-02 20:29:39 +0000166 if (!(self = PyObject_New(Pdata, &PdataType)))
167 return NULL;
168 self->size = 8;
169 self->length = 0;
170 self->data = malloc(self->size * sizeof(PyObject*));
171 if (self->data)
172 return (PyObject*)self;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000173 Py_DECREF(self);
174 return PyErr_NoMemory();
Guido van Rossum053b8df1998-11-25 16:18:00 +0000175}
176
Tim Peters84e87f32001-03-17 04:50:51 +0000177static int
Tim Peterscba30e22003-02-01 06:24:36 +0000178stackUnderflow(void)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000179{
180 PyErr_SetString(UnpicklingError, "unpickling stack underflow");
181 return -1;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000182}
183
Tim Peters1d63c9f2003-02-02 20:29:39 +0000184/* Retain only the initial clearto items. If clearto >= the current
185 * number of items, this is a (non-erroneous) NOP.
186 */
Guido van Rossum053b8df1998-11-25 16:18:00 +0000187static int
Tim Peterscba30e22003-02-01 06:24:36 +0000188Pdata_clear(Pdata *self, int clearto)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000189{
190 int i;
191 PyObject **p;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000192
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000193 if (clearto < 0) return stackUnderflow();
194 if (clearto >= self->length) return 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000195
Tim Peters1d63c9f2003-02-02 20:29:39 +0000196 for (i = self->length, p = self->data + clearto;
197 --i >= clearto;
198 p++) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000199 Py_DECREF(*p);
Tim Peters1d63c9f2003-02-02 20:29:39 +0000200 }
201 self->length = clearto;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000202
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000203 return 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000204}
205
Tim Peters84e87f32001-03-17 04:50:51 +0000206static int
Tim Peterscba30e22003-02-01 06:24:36 +0000207Pdata_grow(Pdata *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000208{
Tim Peters1d63c9f2003-02-02 20:29:39 +0000209 int bigger;
210 size_t nbytes;
211
Tim Peters1d63c9f2003-02-02 20:29:39 +0000212 bigger = self->size << 1;
Tim Peterse0a39072003-02-03 15:45:56 +0000213 if (bigger <= 0) /* was 0, or new value overflows */
Tim Peters1d63c9f2003-02-02 20:29:39 +0000214 goto nomemory;
215 if ((int)(size_t)bigger != bigger)
216 goto nomemory;
217 nbytes = (size_t)bigger * sizeof(PyObject *);
218 if (nbytes / sizeof(PyObject *) != (size_t)bigger)
219 goto nomemory;
220 self->data = realloc(self->data, nbytes);
221 if (self->data == NULL)
222 goto nomemory;
223 self->size = bigger;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000224 return 0;
Tim Peters1d63c9f2003-02-02 20:29:39 +0000225
226 nomemory:
227 self->size = 0;
228 PyErr_NoMemory();
229 return -1;
Tim Peters84e87f32001-03-17 04:50:51 +0000230}
Guido van Rossum053b8df1998-11-25 16:18:00 +0000231
Tim Peterse0a39072003-02-03 15:45:56 +0000232/* D is a Pdata*. Pop the topmost element and store it into V, which
233 * must be an lvalue holding PyObject*. On stack underflow, UnpicklingError
Tim Peters1d63c9f2003-02-02 20:29:39 +0000234 * is raised and V is set to NULL. D and V may be evaluated several times.
235 */
236#define PDATA_POP(D, V) { \
Tim Peterse0a39072003-02-03 15:45:56 +0000237 if ((D)->length) \
238 (V) = (D)->data[--((D)->length)]; \
239 else { \
240 PyErr_SetString(UnpicklingError, "bad pickle data"); \
241 (V) = NULL; \
242 } \
Guido van Rossum053b8df1998-11-25 16:18:00 +0000243}
244
Tim Peterse0a39072003-02-03 15:45:56 +0000245/* PDATA_PUSH and PDATA_APPEND both push rvalue PyObject* O on to Pdata*
246 * D. If the Pdata stack can't be grown to hold the new value, both
247 * raise MemoryError and execute "return ER". The difference is in ownership
248 * of O after: _PUSH transfers ownership of O from the caller to the stack
249 * (no incref of O is done, and in case of error O is decrefed), while
250 * _APPEND pushes a new reference.
251 */
252
253/* Push O on stack D, giving ownership of O to the stack. */
254#define PDATA_PUSH(D, O, ER) { \
255 if (((Pdata*)(D))->length == ((Pdata*)(D))->size && \
256 Pdata_grow((Pdata*)(D)) < 0) { \
257 Py_DECREF(O); \
258 return ER; \
259 } \
260 ((Pdata*)(D))->data[((Pdata*)(D))->length++] = (O); \
261}
262
263/* Push O on stack D, pushing a new reference. */
264#define PDATA_APPEND(D, O, ER) { \
265 if (((Pdata*)(D))->length == ((Pdata*)(D))->size && \
266 Pdata_grow((Pdata*)(D)) < 0) \
267 return ER; \
268 Py_INCREF(O); \
269 ((Pdata*)(D))->data[((Pdata*)(D))->length++] = (O); \
270}
271
272
Guido van Rossum053b8df1998-11-25 16:18:00 +0000273static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +0000274Pdata_popTuple(Pdata *self, int start)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000275{
276 PyObject *r;
277 int i, j, l;
Tim Peterscba30e22003-02-01 06:24:36 +0000278
Tim Peters1d63c9f2003-02-02 20:29:39 +0000279 l = self->length-start;
280 r = PyTuple_New(l);
281 if (r == NULL)
282 return NULL;
283 for (i = start, j = 0 ; j < l; i++, j++)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000284 PyTuple_SET_ITEM(r, j, self->data[i]);
Tim Peterscba30e22003-02-01 06:24:36 +0000285
Tim Peters1d63c9f2003-02-02 20:29:39 +0000286 self->length = start;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000287 return r;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000288}
289
290static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +0000291Pdata_popList(Pdata *self, int start)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000292{
293 PyObject *r;
294 int i, j, l;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000295
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000296 l=self->length-start;
297 if (!( r=PyList_New(l))) return NULL;
298 for (i=start, j=0 ; j < l; i++, j++)
299 PyList_SET_ITEM(r, j, self->data[i]);
Guido van Rossum053b8df1998-11-25 16:18:00 +0000300
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000301 self->length=start;
302 return r;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000303}
304
Guido van Rossum053b8df1998-11-25 16:18:00 +0000305/*************************************************************************/
306
307#define ARG_TUP(self, o) { \
308 if (self->arg || (self->arg=PyTuple_New(1))) { \
309 Py_XDECREF(PyTuple_GET_ITEM(self->arg,0)); \
310 PyTuple_SET_ITEM(self->arg,0,o); \
311 } \
312 else { \
313 Py_DECREF(o); \
314 } \
315}
316
317#define FREE_ARG_TUP(self) { \
318 if (self->arg->ob_refcnt > 1) { \
319 Py_DECREF(self->arg); \
320 self->arg=NULL; \
321 } \
322 }
323
Thomas Wouters3b6448f2000-07-22 23:56:07 +0000324typedef struct Picklerobject {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000325 PyObject_HEAD
326 FILE *fp;
327 PyObject *write;
328 PyObject *file;
329 PyObject *memo;
330 PyObject *arg;
331 PyObject *pers_func;
332 PyObject *inst_pers_func;
Tim Peters797ec242003-02-01 06:22:36 +0000333
334 /* pickle protocol number, >= 0 */
335 int proto;
336
337 /* bool, true if proto > 0 */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000338 int bin;
Tim Peters797ec242003-02-01 06:22:36 +0000339
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000340 int fast; /* Fast mode doesn't save in memo, don't use if circ ref */
Martin v. Löwis5a395302002-08-04 08:20:23 +0000341 int nesting;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000342 int (*write_func)(struct Picklerobject *, char *, int);
343 char *write_buf;
344 int buf_size;
345 PyObject *dispatch_table;
346 int fast_container; /* count nested container dumps */
347 PyObject *fast_memo;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000348} Picklerobject;
349
Barry Warsaw52acb492001-12-21 20:04:22 +0000350#ifndef PY_CPICKLE_FAST_LIMIT
351#define PY_CPICKLE_FAST_LIMIT 50
352#endif
Jeremy Hyltona0fb1772001-10-12 04:11:06 +0000353
Jeremy Hylton938ace62002-07-17 16:30:39 +0000354static PyTypeObject Picklertype;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000355
Thomas Wouters3b6448f2000-07-22 23:56:07 +0000356typedef struct Unpicklerobject {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000357 PyObject_HEAD
358 FILE *fp;
359 PyObject *file;
360 PyObject *readline;
361 PyObject *read;
362 PyObject *memo;
363 PyObject *arg;
364 Pdata *stack;
365 PyObject *mark;
366 PyObject *pers_func;
367 PyObject *last_string;
368 int *marks;
369 int num_marks;
370 int marks_size;
371 int (*read_func)(struct Unpicklerobject *, char **, int);
372 int (*readline_func)(struct Unpicklerobject *, char **);
373 int buf_size;
374 char *buf;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000375 PyObject *find_class;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000376} Unpicklerobject;
Tim Peters84e87f32001-03-17 04:50:51 +0000377
Jeremy Hylton938ace62002-07-17 16:30:39 +0000378static PyTypeObject Unpicklertype;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000379
Thomas Wouters4789b3a2000-07-24 11:36:47 +0000380/* Forward decls that need the above structs */
381static int save(Picklerobject *, PyObject *, int);
382static int put2(Picklerobject *, PyObject *);
383
Guido van Rossumd385d591997-04-09 17:47:47 +0000384static
Guido van Rossum60456fd1997-04-09 17:36:32 +0000385PyObject *
Thomas Wouters3b6448f2000-07-22 23:56:07 +0000386cPickle_ErrFormat(PyObject *ErrType, char *stringformat, char *format, ...)
387{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000388 va_list va;
389 PyObject *args=0, *retval=0;
390 va_start(va, format);
Tim Peterscba30e22003-02-01 06:24:36 +0000391
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000392 if (format) args = Py_VaBuildValue(format, va);
393 va_end(va);
394 if (format && ! args) return NULL;
Tim Peterscba30e22003-02-01 06:24:36 +0000395 if (stringformat && !(retval=PyString_FromString(stringformat)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000396 return NULL;
Tim Peterscba30e22003-02-01 06:24:36 +0000397
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000398 if (retval) {
399 if (args) {
400 PyObject *v;
401 v=PyString_Format(retval, args);
402 Py_DECREF(retval);
403 Py_DECREF(args);
404 if (! v) return NULL;
405 retval=v;
406 }
407 }
408 else
409 if (args) retval=args;
410 else {
411 PyErr_SetObject(ErrType,Py_None);
412 return NULL;
413 }
414 PyErr_SetObject(ErrType,retval);
415 Py_DECREF(retval);
416 return NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000417}
418
Tim Peters84e87f32001-03-17 04:50:51 +0000419static int
Tim Peterscba30e22003-02-01 06:24:36 +0000420write_file(Picklerobject *self, char *s, int n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000421{
422 size_t nbyteswritten;
Tim Peters84e87f32001-03-17 04:50:51 +0000423
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000424 if (s == NULL) {
425 return 0;
426 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000427
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000428 Py_BEGIN_ALLOW_THREADS
Jeremy Hylton7fa4bfa2002-07-18 20:58:57 +0000429 nbyteswritten = fwrite(s, sizeof(char), n, self->fp);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000430 Py_END_ALLOW_THREADS
Jeremy Hylton7fa4bfa2002-07-18 20:58:57 +0000431 if (nbyteswritten != (size_t)n) {
432 PyErr_SetFromErrno(PyExc_IOError);
433 return -1;
434 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000435
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000436 return n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000437}
438
Tim Peters84e87f32001-03-17 04:50:51 +0000439static int
Tim Peterscba30e22003-02-01 06:24:36 +0000440write_cStringIO(Picklerobject *self, char *s, int n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000441{
442 if (s == NULL) {
443 return 0;
444 }
Tim Peterscba30e22003-02-01 06:24:36 +0000445
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000446 if (PycStringIO->cwrite((PyObject *)self->file, s, n) != n) {
447 return -1;
448 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000449
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000450 return n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000451}
452
Tim Peters84e87f32001-03-17 04:50:51 +0000453static int
Tim Peterscba30e22003-02-01 06:24:36 +0000454write_none(Picklerobject *self, char *s, int n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000455{
456 if (s == NULL) return 0;
457 return n;
Guido van Rossum142eeb81997-08-13 03:14:41 +0000458}
459
Tim Peters84e87f32001-03-17 04:50:51 +0000460static int
Tim Peterscba30e22003-02-01 06:24:36 +0000461write_other(Picklerobject *self, char *s, int n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000462{
463 PyObject *py_str = 0, *junk = 0;
Tim Peterscba30e22003-02-01 06:24:36 +0000464
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000465 if (s == NULL) {
466 if (!( self->buf_size )) return 0;
Tim Peterscba30e22003-02-01 06:24:36 +0000467 py_str = PyString_FromStringAndSize(self->write_buf,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000468 self->buf_size);
Tim Peterscba30e22003-02-01 06:24:36 +0000469 if (!py_str)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000470 return -1;
471 }
472 else {
473 if (self->buf_size && (n + self->buf_size) > WRITE_BUF_SIZE) {
474 if (write_other(self, NULL, 0) < 0)
475 return -1;
476 }
Tim Peterscba30e22003-02-01 06:24:36 +0000477
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000478 if (n > WRITE_BUF_SIZE) {
479 if (!( py_str =
Tim Peterscba30e22003-02-01 06:24:36 +0000480 PyString_FromStringAndSize(s, n)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000481 return -1;
482 }
483 else {
484 memcpy(self->write_buf + self->buf_size, s, n);
485 self->buf_size += n;
486 return n;
487 }
488 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000489
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000490 if (self->write) {
491 /* object with write method */
492 ARG_TUP(self, py_str);
493 if (self->arg) {
494 junk = PyObject_Call(self->write, self->arg, NULL);
495 FREE_ARG_TUP(self);
496 }
497 if (junk) Py_DECREF(junk);
498 else return -1;
499 }
500 else
501 PDATA_PUSH(self->file, py_str, -1);
Guido van Rossum60456fd1997-04-09 17:36:32 +0000502
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000503 self->buf_size = 0;
504 return n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000505}
506
507
Tim Peters84e87f32001-03-17 04:50:51 +0000508static int
Tim Petersee1a53c2003-02-02 02:57:53 +0000509read_file(Unpicklerobject *self, char **s, int n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000510{
511 size_t nbytesread;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000512
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000513 if (self->buf_size == 0) {
514 int size;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000515
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000516 size = ((n < 32) ? 32 : n);
Tim Petersee1a53c2003-02-02 02:57:53 +0000517 if (!( self->buf = (char *)malloc(size))) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000518 PyErr_NoMemory();
519 return -1;
520 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000521
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000522 self->buf_size = size;
523 }
524 else if (n > self->buf_size) {
Tim Petersee1a53c2003-02-02 02:57:53 +0000525 self->buf = (char *)realloc(self->buf, n);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000526 if (!self->buf) {
527 PyErr_NoMemory();
528 return -1;
529 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000530 self->buf_size = n;
531 }
Tim Peters84e87f32001-03-17 04:50:51 +0000532
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000533 Py_BEGIN_ALLOW_THREADS
534 nbytesread = fread(self->buf, sizeof(char), n, self->fp);
535 Py_END_ALLOW_THREADS
536 if (nbytesread != (size_t)n) {
537 if (feof(self->fp)) {
538 PyErr_SetNone(PyExc_EOFError);
539 return -1;
540 }
Tim Peterscba30e22003-02-01 06:24:36 +0000541
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000542 PyErr_SetFromErrno(PyExc_IOError);
543 return -1;
544 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000545
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000546 *s = self->buf;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000547
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000548 return n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000549}
550
551
Tim Peters84e87f32001-03-17 04:50:51 +0000552static int
Tim Peterscba30e22003-02-01 06:24:36 +0000553readline_file(Unpicklerobject *self, char **s)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000554{
555 int i;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000556
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000557 if (self->buf_size == 0) {
Tim Petersee1a53c2003-02-02 02:57:53 +0000558 if (!( self->buf = (char *)malloc(40))) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000559 PyErr_NoMemory();
560 return -1;
561 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000562 self->buf_size = 40;
563 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000564
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000565 i = 0;
566 while (1) {
Tim Petersee1a53c2003-02-02 02:57:53 +0000567 int bigger;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000568 for (; i < (self->buf_size - 1); i++) {
Tim Peterscba30e22003-02-01 06:24:36 +0000569 if (feof(self->fp) ||
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000570 (self->buf[i] = getc(self->fp)) == '\n') {
571 self->buf[i + 1] = '\0';
572 *s = self->buf;
573 return i + 1;
574 }
575 }
Tim Petersee1a53c2003-02-02 02:57:53 +0000576 bigger = self->buf_size << 1;
577 if (bigger <= 0) { /* overflow */
578 PyErr_NoMemory();
579 return -1;
580 }
581 self->buf = (char *)realloc(self->buf, bigger);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000582 if (!self->buf) {
583 PyErr_NoMemory();
584 return -1;
585 }
Tim Petersee1a53c2003-02-02 02:57:53 +0000586 self->buf_size = bigger;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000587 }
Tim Peters84e87f32001-03-17 04:50:51 +0000588}
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000589
590
Tim Peters84e87f32001-03-17 04:50:51 +0000591static int
Tim Peterscba30e22003-02-01 06:24:36 +0000592read_cStringIO(Unpicklerobject *self, char **s, int n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000593{
594 char *ptr;
Tim Peterscba30e22003-02-01 06:24:36 +0000595
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000596 if (PycStringIO->cread((PyObject *)self->file, &ptr, n) != n) {
597 PyErr_SetNone(PyExc_EOFError);
598 return -1;
599 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000600
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000601 *s = ptr;
Tim Peterscba30e22003-02-01 06:24:36 +0000602
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000603 return n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000604}
605
606
Tim Peters84e87f32001-03-17 04:50:51 +0000607static int
Tim Peterscba30e22003-02-01 06:24:36 +0000608readline_cStringIO(Unpicklerobject *self, char **s)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000609{
610 int n;
611 char *ptr;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000612
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000613 if ((n = PycStringIO->creadline((PyObject *)self->file, &ptr)) < 0) {
614 return -1;
615 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000616
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000617 *s = ptr;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000618
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000619 return n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000620}
621
622
Tim Peters84e87f32001-03-17 04:50:51 +0000623static int
Tim Peterscba30e22003-02-01 06:24:36 +0000624read_other(Unpicklerobject *self, char **s, int n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000625{
626 PyObject *bytes, *str=0;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000627
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000628 if (!( bytes = PyInt_FromLong(n))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000629
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000630 ARG_TUP(self, bytes);
631 if (self->arg) {
632 str = PyObject_Call(self->read, self->arg, NULL);
633 FREE_ARG_TUP(self);
634 }
635 if (! str) return -1;
Tim Peterscba30e22003-02-01 06:24:36 +0000636
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000637 Py_XDECREF(self->last_string);
638 self->last_string = str;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000639
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000640 if (! (*s = PyString_AsString(str))) return -1;
641 return n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000642}
643
644
Tim Peters84e87f32001-03-17 04:50:51 +0000645static int
Tim Peterscba30e22003-02-01 06:24:36 +0000646readline_other(Unpicklerobject *self, char **s)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000647{
648 PyObject *str;
649 int str_size;
Tim Peterscba30e22003-02-01 06:24:36 +0000650
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000651 if (!( str = PyObject_CallObject(self->readline, empty_tuple))) {
652 return -1;
653 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000654
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000655 if ((str_size = PyString_Size(str)) < 0)
656 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000657
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000658 Py_XDECREF(self->last_string);
659 self->last_string = str;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000660
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000661 if (! (*s = PyString_AsString(str)))
662 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000663
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000664 return str_size;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000665}
666
Tim Petersee1a53c2003-02-02 02:57:53 +0000667/* Copy the first n bytes from s into newly malloc'ed memory, plus a
668 * trailing 0 byte. Return a pointer to that, or NULL if out of memory.
669 * The caller is responsible for free()'ing the return value.
670 */
Guido van Rossum60456fd1997-04-09 17:36:32 +0000671static char *
Tim Petersee1a53c2003-02-02 02:57:53 +0000672pystrndup(char *s, int n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000673{
Tim Petersee1a53c2003-02-02 02:57:53 +0000674 char *r = (char *)malloc(n+1);
675 if (r == NULL)
676 return (char*)PyErr_NoMemory();
677 memcpy(r, s, n);
678 r[n] = 0;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000679 return r;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000680}
681
682
683static int
Tim Peterscba30e22003-02-01 06:24:36 +0000684get(Picklerobject *self, PyObject *id)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000685{
686 PyObject *value, *mv;
687 long c_value;
688 char s[30];
689 size_t len;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000690
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000691 if (!( mv = PyDict_GetItem(self->memo, id))) {
692 PyErr_SetObject(PyExc_KeyError, id);
693 return -1;
694 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000695
Tim Peterscba30e22003-02-01 06:24:36 +0000696 if (!( value = PyTuple_GetItem(mv, 0)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000697 return -1;
Tim Peters84e87f32001-03-17 04:50:51 +0000698
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000699 if (!( PyInt_Check(value))) {
700 PyErr_SetString(PicklingError, "no int where int expected in memo");
701 return -1;
702 }
703 c_value = PyInt_AS_LONG((PyIntObject*)value);
Guido van Rossum60456fd1997-04-09 17:36:32 +0000704
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000705 if (!self->bin) {
706 s[0] = GET;
707 PyOS_snprintf(s + 1, sizeof(s) - 1, "%ld\n", c_value);
708 len = strlen(s);
709 }
710 else if (Pdata_Check(self->file)) {
711 if (write_other(self, NULL, 0) < 0) return -1;
712 PDATA_APPEND(self->file, mv, -1);
713 return 0;
714 }
715 else {
716 if (c_value < 256) {
717 s[0] = BINGET;
718 s[1] = (int)(c_value & 0xff);
719 len = 2;
720 }
721 else {
722 s[0] = LONG_BINGET;
723 s[1] = (int)(c_value & 0xff);
724 s[2] = (int)((c_value >> 8) & 0xff);
725 s[3] = (int)((c_value >> 16) & 0xff);
726 s[4] = (int)((c_value >> 24) & 0xff);
727 len = 5;
728 }
729 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000730
Tim Peters0bc93f52003-02-02 18:29:33 +0000731 if (self->write_func(self, s, len) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000732 return -1;
Tim Peterscba30e22003-02-01 06:24:36 +0000733
Jeremy Hyltona0fb1772001-10-12 04:11:06 +0000734 return 0;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000735}
Jeremy Hyltonce616e41998-08-13 23:13:52 +0000736
Guido van Rossum60456fd1997-04-09 17:36:32 +0000737
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000738static int
Tim Peterscba30e22003-02-01 06:24:36 +0000739put(Picklerobject *self, PyObject *ob)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000740{
Tim Peterscba30e22003-02-01 06:24:36 +0000741 if (ob->ob_refcnt < 2 || self->fast)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000742 return 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000743
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000744 return put2(self, ob);
745}
Guido van Rossum053b8df1998-11-25 16:18:00 +0000746
Guido van Rossum053b8df1998-11-25 16:18:00 +0000747
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000748static int
Tim Peterscba30e22003-02-01 06:24:36 +0000749put2(Picklerobject *self, PyObject *ob)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000750{
751 char c_str[30];
752 int p;
753 size_t len;
754 int res = -1;
755 PyObject *py_ob_id = 0, *memo_len = 0, *t = 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000756
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000757 if (self->fast)
758 return 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000759
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000760 if ((p = PyDict_Size(self->memo)) < 0)
761 goto finally;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000762
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000763 /* Make sure memo keys are positive! */
Tim Peters70b02d72003-02-02 17:26:40 +0000764 /* XXX Why?
765 * XXX And does "positive" really mean non-negative?
766 * XXX pickle.py starts with PUT index 0, not 1. This makes for
767 * XXX gratuitous differences between the pickling modules.
768 */
Tim Peterscba30e22003-02-01 06:24:36 +0000769 p++;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000770
Tim Peterscba30e22003-02-01 06:24:36 +0000771 if (!( py_ob_id = PyLong_FromVoidPtr(ob)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000772 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000773
Tim Peterscba30e22003-02-01 06:24:36 +0000774 if (!( memo_len = PyInt_FromLong(p)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000775 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000776
Tim Peterscba30e22003-02-01 06:24:36 +0000777 if (!( t = PyTuple_New(2)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000778 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000779
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000780 PyTuple_SET_ITEM(t, 0, memo_len);
781 Py_INCREF(memo_len);
782 PyTuple_SET_ITEM(t, 1, ob);
783 Py_INCREF(ob);
784
785 if (PyDict_SetItem(self->memo, py_ob_id, t) < 0)
786 goto finally;
787
788 if (!self->bin) {
789 c_str[0] = PUT;
790 PyOS_snprintf(c_str + 1, sizeof(c_str) - 1, "%d\n", p);
791 len = strlen(c_str);
792 }
793 else if (Pdata_Check(self->file)) {
794 if (write_other(self, NULL, 0) < 0) return -1;
795 PDATA_APPEND(self->file, memo_len, -1);
796 res=0; /* Job well done ;) */
797 goto finally;
798 }
799 else {
800 if (p >= 256) {
801 c_str[0] = LONG_BINPUT;
802 c_str[1] = (int)(p & 0xff);
803 c_str[2] = (int)((p >> 8) & 0xff);
804 c_str[3] = (int)((p >> 16) & 0xff);
805 c_str[4] = (int)((p >> 24) & 0xff);
806 len = 5;
807 }
808 else {
809 c_str[0] = BINPUT;
810 c_str[1] = p;
811 len = 2;
812 }
813 }
814
Tim Peters0bc93f52003-02-02 18:29:33 +0000815 if (self->write_func(self, c_str, len) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000816 goto finally;
817
818 res = 0;
819
820 finally:
821 Py_XDECREF(py_ob_id);
822 Py_XDECREF(memo_len);
823 Py_XDECREF(t);
824
825 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000826}
827
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000828static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +0000829whichmodule(PyObject *global, PyObject *global_name)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000830{
831 int i, j;
832 PyObject *module = 0, *modules_dict = 0,
833 *global_name_attr = 0, *name = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000834
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000835 module = PyObject_GetAttrString(global, "__module__");
836 if (module) return module;
837 PyErr_Clear();
Guido van Rossum45188231997-09-28 05:38:51 +0000838
Tim Peterscba30e22003-02-01 06:24:36 +0000839 if (!( modules_dict = PySys_GetObject("modules")))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000840 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000841
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000842 i = 0;
843 while ((j = PyDict_Next(modules_dict, &i, &name, &module))) {
Guido van Rossum142eeb81997-08-13 03:14:41 +0000844
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000845 if (PyObject_Compare(name, __main___str)==0) continue;
Tim Peters84e87f32001-03-17 04:50:51 +0000846
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000847 global_name_attr = PyObject_GetAttr(module, global_name);
848 if (!global_name_attr) {
849 PyErr_Clear();
850 continue;
851 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000852
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000853 if (global_name_attr != global) {
854 Py_DECREF(global_name_attr);
855 continue;
856 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000857
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000858 Py_DECREF(global_name_attr);
Guido van Rossum60456fd1997-04-09 17:36:32 +0000859
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000860 break;
861 }
Guido van Rossum142eeb81997-08-13 03:14:41 +0000862
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000863 /* The following implements the rule in pickle.py added in 1.5
864 that used __main__ if no module is found. I don't actually
865 like this rule. jlf
866 */
867 if (!j) {
868 j=1;
869 name=__main___str;
870 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000871
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000872 Py_INCREF(name);
873 return name;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000874}
875
876
Guido van Rossum60456fd1997-04-09 17:36:32 +0000877static int
Jeremy Hylton499ab6a2001-10-15 21:37:58 +0000878fast_save_enter(Picklerobject *self, PyObject *obj)
879{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000880 /* if fast_container < 0, we're doing an error exit. */
881 if (++self->fast_container >= PY_CPICKLE_FAST_LIMIT) {
882 PyObject *key = NULL;
883 if (self->fast_memo == NULL) {
884 self->fast_memo = PyDict_New();
885 if (self->fast_memo == NULL) {
886 self->fast_container = -1;
887 return 0;
888 }
889 }
890 key = PyLong_FromVoidPtr(obj);
891 if (key == NULL)
892 return 0;
893 if (PyDict_GetItem(self->fast_memo, key)) {
Tim Peters4e52ca82002-12-07 02:43:28 +0000894 Py_DECREF(key);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000895 PyErr_Format(PyExc_ValueError,
Tim Peters92c8bb32003-02-13 23:00:26 +0000896 "fast mode: can't pickle cyclic objects "
897 "including object type %s at %p",
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000898 obj->ob_type->tp_name, obj);
899 self->fast_container = -1;
900 return 0;
901 }
902 if (PyDict_SetItem(self->fast_memo, key, Py_None) < 0) {
Tim Peters4e52ca82002-12-07 02:43:28 +0000903 Py_DECREF(key);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000904 self->fast_container = -1;
905 return 0;
906 }
Tim Peters4e52ca82002-12-07 02:43:28 +0000907 Py_DECREF(key);
Jeremy Hylton499ab6a2001-10-15 21:37:58 +0000908 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000909 return 1;
Jeremy Hylton499ab6a2001-10-15 21:37:58 +0000910}
911
Tim Peterscba30e22003-02-01 06:24:36 +0000912int
Jeremy Hylton499ab6a2001-10-15 21:37:58 +0000913fast_save_leave(Picklerobject *self, PyObject *obj)
914{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000915 if (self->fast_container-- >= PY_CPICKLE_FAST_LIMIT) {
916 PyObject *key = PyLong_FromVoidPtr(obj);
917 if (key == NULL)
918 return 0;
919 if (PyDict_DelItem(self->fast_memo, key) < 0) {
Tim Peters4e52ca82002-12-07 02:43:28 +0000920 Py_DECREF(key);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000921 return 0;
922 }
Tim Peters4e52ca82002-12-07 02:43:28 +0000923 Py_DECREF(key);
Jeremy Hylton499ab6a2001-10-15 21:37:58 +0000924 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000925 return 1;
Jeremy Hylton499ab6a2001-10-15 21:37:58 +0000926}
927
928static int
Tim Peterscba30e22003-02-01 06:24:36 +0000929save_none(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000930{
931 static char none = NONE;
Tim Peters0bc93f52003-02-02 18:29:33 +0000932 if (self->write_func(self, &none, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000933 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000934
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000935 return 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000936}
937
Guido van Rossum77f6a652002-04-03 22:41:51 +0000938static int
Tim Peterscba30e22003-02-01 06:24:36 +0000939save_bool(Picklerobject *self, PyObject *args)
Guido van Rossum77f6a652002-04-03 22:41:51 +0000940{
Guido van Rossume2763392002-04-05 19:30:08 +0000941 static char *buf[2] = {FALSE, TRUE};
942 static char len[2] = {sizeof(FALSE)-1, sizeof(TRUE)-1};
Guido van Rossum77f6a652002-04-03 22:41:51 +0000943 long l = PyInt_AS_LONG((PyIntObject *)args);
944
Tim Peters3c67d792003-02-02 17:59:11 +0000945 if (self->proto >= 2) {
946 char opcode = l ? NEWTRUE : NEWFALSE;
947 if (self->write_func(self, &opcode, 1) < 0)
948 return -1;
949 }
950 else if (self->write_func(self, buf[l], len[l]) < 0)
Guido van Rossum77f6a652002-04-03 22:41:51 +0000951 return -1;
Guido van Rossum77f6a652002-04-03 22:41:51 +0000952 return 0;
953}
Tim Peters84e87f32001-03-17 04:50:51 +0000954
Guido van Rossum60456fd1997-04-09 17:36:32 +0000955static int
Tim Peterscba30e22003-02-01 06:24:36 +0000956save_int(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000957{
958 char c_str[32];
959 long l = PyInt_AS_LONG((PyIntObject *)args);
960 int len = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000961
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000962 if (!self->bin
Guido van Rossumde8d6d71997-05-13 18:00:44 +0000963#if SIZEOF_LONG > 4
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000964 || l > 0x7fffffffL
965 || l < -0x80000000L
Guido van Rossumde8d6d71997-05-13 18:00:44 +0000966#endif
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000967 ) {
968 /* Text-mode pickle, or long too big to fit in the 4-byte
969 * signed BININT format: store as a string.
970 */
971 c_str[0] = INT;
972 PyOS_snprintf(c_str + 1, sizeof(c_str) - 1, "%ld\n", l);
Tim Peters0bc93f52003-02-02 18:29:33 +0000973 if (self->write_func(self, c_str, strlen(c_str)) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000974 return -1;
975 }
976 else {
977 /* Binary pickle and l fits in a signed 4-byte int. */
978 c_str[1] = (int)( l & 0xff);
979 c_str[2] = (int)((l >> 8) & 0xff);
980 c_str[3] = (int)((l >> 16) & 0xff);
981 c_str[4] = (int)((l >> 24) & 0xff);
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000982
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000983 if ((c_str[4] == 0) && (c_str[3] == 0)) {
984 if (c_str[2] == 0) {
985 c_str[0] = BININT1;
986 len = 2;
987 }
988 else {
989 c_str[0] = BININT2;
990 len = 3;
991 }
992 }
993 else {
994 c_str[0] = BININT;
995 len = 5;
996 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000997
Tim Peters0bc93f52003-02-02 18:29:33 +0000998 if (self->write_func(self, c_str, len) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000999 return -1;
1000 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001001
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001002 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001003}
1004
1005
1006static int
Tim Peterscba30e22003-02-01 06:24:36 +00001007save_long(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001008{
Tim Petersee1a53c2003-02-02 02:57:53 +00001009 int size;
1010 int res = -1;
1011 PyObject *repr = NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001012
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001013 static char l = LONG;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001014
Tim Petersee1a53c2003-02-02 02:57:53 +00001015 if (self->proto >= 2) {
1016 /* Linear-time pickling. */
1017 size_t nbits;
1018 size_t nbytes;
1019 unsigned char *pdata;
1020 char c_str[5];
1021 int i;
1022 int sign = _PyLong_Sign(args);
1023
1024 if (sign == 0) {
1025 /* It's 0 -- an empty bytestring. */
1026 c_str[0] = LONG1;
1027 c_str[1] = 0;
1028 i = self->write_func(self, c_str, 2);
1029 if (i < 0) goto finally;
1030 res = 0;
1031 goto finally;
1032 }
1033 nbits = _PyLong_NumBits(args);
1034 if (nbits == (size_t)-1 && PyErr_Occurred())
1035 goto finally;
1036 /* How many bytes do we need? There are nbits >> 3 full
1037 * bytes of data, and nbits & 7 leftover bits. If there
1038 * are any leftover bits, then we clearly need another
1039 * byte. Wnat's not so obvious is that we *probably*
1040 * need another byte even if there aren't any leftovers:
1041 * the most-significant bit of the most-significant byte
1042 * acts like a sign bit, and it's usually got a sense
1043 * opposite of the one we need. The exception is longs
1044 * of the form -(2**(8*j-1)) for j > 0. Such a long is
1045 * its own 256's-complement, so has the right sign bit
1046 * even without the extra byte. That's a pain to check
1047 * for in advance, though, so we always grab an extra
1048 * byte at the start, and cut it back later if possible.
1049 */
1050 nbytes = (nbits >> 3) + 1;
1051 if ((int)nbytes < 0 || (size_t)(int)nbytes != nbytes) {
1052 PyErr_SetString(PyExc_OverflowError, "long too large "
1053 "to pickle");
1054 goto finally;
1055 }
1056 repr = PyString_FromStringAndSize(NULL, (int)nbytes);
1057 if (repr == NULL) goto finally;
1058 pdata = (unsigned char *)PyString_AS_STRING(repr);
1059 i = _PyLong_AsByteArray((PyLongObject *)args,
1060 pdata, nbytes,
1061 1 /* little endian */, 1 /* signed */);
1062 if (i < 0) goto finally;
1063 /* If the long is negative, this may be a byte more than
1064 * needed. This is so iff the MSB is all redundant sign
1065 * bits.
1066 */
1067 if (sign < 0 && nbytes > 1 && pdata[nbytes - 1] == 0xff &&
1068 (pdata[nbytes - 2] & 0x80) != 0)
1069 --nbytes;
1070
1071 if (nbytes < 256) {
1072 c_str[0] = LONG1;
1073 c_str[1] = (char)nbytes;
1074 size = 2;
1075 }
1076 else {
1077 c_str[0] = LONG4;
1078 size = (int)nbytes;
1079 for (i = 1; i < 5; i++) {
1080 c_str[i] = (char)(size & 0xff);
1081 size >>= 8;
1082 }
1083 size = 5;
1084 }
1085 i = self->write_func(self, c_str, size);
1086 if (i < 0) goto finally;
1087 i = self->write_func(self, (char *)pdata, (int)nbytes);
1088 if (i < 0) goto finally;
1089 res = 0;
1090 goto finally;
1091 }
1092
1093 /* proto < 2: write the repr and newline. This is quadratic-time
1094 * (in the number of digits), in both directions.
1095 */
Tim Peterscba30e22003-02-01 06:24:36 +00001096 if (!( repr = PyObject_Repr(args)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001097 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001098
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001099 if ((size = PyString_Size(repr)) < 0)
1100 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001101
Tim Peters0bc93f52003-02-02 18:29:33 +00001102 if (self->write_func(self, &l, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001103 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001104
Tim Peters0bc93f52003-02-02 18:29:33 +00001105 if (self->write_func(self,
1106 PyString_AS_STRING((PyStringObject *)repr),
1107 size) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001108 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001109
Tim Peters0bc93f52003-02-02 18:29:33 +00001110 if (self->write_func(self, "\n", 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001111 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001112
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001113 res = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001114
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001115 finally:
1116 Py_XDECREF(repr);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001117 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001118}
1119
1120
1121static int
Tim Peterscba30e22003-02-01 06:24:36 +00001122save_float(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001123{
1124 double x = PyFloat_AS_DOUBLE((PyFloatObject *)args);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001125
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001126 if (self->bin) {
1127 int s, e;
1128 double f;
1129 long fhi, flo;
1130 char str[9];
1131 unsigned char *p = (unsigned char *)str;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001132
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001133 *p = BINFLOAT;
1134 p++;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001135
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001136 if (x < 0) {
1137 s = 1;
1138 x = -x;
1139 }
1140 else
1141 s = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001142
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001143 f = frexp(x, &e);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001144
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001145 /* Normalize f to be in the range [1.0, 2.0) */
1146 if (0.5 <= f && f < 1.0) {
1147 f *= 2.0;
1148 e--;
1149 }
1150 else if (f == 0.0) {
1151 e = 0;
1152 }
1153 else {
1154 PyErr_SetString(PyExc_SystemError,
1155 "frexp() result out of range");
1156 return -1;
1157 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001158
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001159 if (e >= 1024) {
1160 /* XXX 1024 itself is reserved for Inf/NaN */
1161 PyErr_SetString(PyExc_OverflowError,
1162 "float too large to pack with d format");
1163 return -1;
1164 }
1165 else if (e < -1022) {
1166 /* Gradual underflow */
1167 f = ldexp(f, 1022 + e);
1168 e = 0;
1169 }
1170 else if (!(e == 0 && f == 0.0)) {
1171 e += 1023;
1172 f -= 1.0; /* Get rid of leading 1 */
1173 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001174
Tim Peterscba30e22003-02-01 06:24:36 +00001175 /* fhi receives the high 28 bits;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001176 flo the low 24 bits (== 52 bits) */
1177 f *= 268435456.0; /* 2**28 */
1178 fhi = (long) floor(f); /* Truncate */
1179 f -= (double)fhi;
1180 f *= 16777216.0; /* 2**24 */
1181 flo = (long) floor(f + 0.5); /* Round */
Guido van Rossum60456fd1997-04-09 17:36:32 +00001182
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001183 /* First byte */
1184 *p = (s<<7) | (e>>4);
1185 p++;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001186
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001187 /* Second byte */
1188 *p = (unsigned char) (((e&0xF)<<4) | (fhi>>24));
1189 p++;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001190
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001191 /* Third byte */
1192 *p = (unsigned char) ((fhi>>16) & 0xFF);
1193 p++;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001194
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001195 /* Fourth byte */
1196 *p = (unsigned char) ((fhi>>8) & 0xFF);
1197 p++;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001198
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001199 /* Fifth byte */
1200 *p = (unsigned char) (fhi & 0xFF);
1201 p++;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001202
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001203 /* Sixth byte */
1204 *p = (unsigned char) ((flo>>16) & 0xFF);
1205 p++;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001206
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001207 /* Seventh byte */
1208 *p = (unsigned char) ((flo>>8) & 0xFF);
1209 p++;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001210
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001211 /* Eighth byte */
1212 *p = (unsigned char) (flo & 0xFF);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001213
Tim Peters0bc93f52003-02-02 18:29:33 +00001214 if (self->write_func(self, str, 9) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001215 return -1;
1216 }
1217 else {
1218 char c_str[250];
1219 c_str[0] = FLOAT;
1220 PyOS_snprintf(c_str + 1, sizeof(c_str) - 1, "%.17g\n", x);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001221
Tim Peters0bc93f52003-02-02 18:29:33 +00001222 if (self->write_func(self, c_str, strlen(c_str)) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001223 return -1;
1224 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001225
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001226 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001227}
1228
1229
1230static int
Tim Peterscba30e22003-02-01 06:24:36 +00001231save_string(Picklerobject *self, PyObject *args, int doput)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001232{
1233 int size, len;
1234 PyObject *repr=0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001235
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001236 if ((size = PyString_Size(args)) < 0)
1237 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001238
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001239 if (!self->bin) {
1240 char *repr_str;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001241
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001242 static char string = STRING;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001243
Tim Peterscba30e22003-02-01 06:24:36 +00001244 if (!( repr = PyObject_Repr(args)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001245 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001246
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001247 if ((len = PyString_Size(repr)) < 0)
1248 goto err;
1249 repr_str = PyString_AS_STRING((PyStringObject *)repr);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001250
Tim Peters0bc93f52003-02-02 18:29:33 +00001251 if (self->write_func(self, &string, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001252 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001253
Tim Peters0bc93f52003-02-02 18:29:33 +00001254 if (self->write_func(self, repr_str, len) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001255 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001256
Tim Peters0bc93f52003-02-02 18:29:33 +00001257 if (self->write_func(self, "\n", 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001258 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001259
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001260 Py_XDECREF(repr);
1261 }
1262 else {
1263 int i;
1264 char c_str[5];
Guido van Rossum60456fd1997-04-09 17:36:32 +00001265
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001266 if ((size = PyString_Size(args)) < 0)
1267 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001268
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001269 if (size < 256) {
1270 c_str[0] = SHORT_BINSTRING;
1271 c_str[1] = size;
1272 len = 2;
1273 }
1274 else {
1275 c_str[0] = BINSTRING;
1276 for (i = 1; i < 5; i++)
1277 c_str[i] = (int)(size >> ((i - 1) * 8));
1278 len = 5;
1279 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001280
Tim Peters0bc93f52003-02-02 18:29:33 +00001281 if (self->write_func(self, c_str, len) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001282 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001283
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001284 if (size > 128 && Pdata_Check(self->file)) {
1285 if (write_other(self, NULL, 0) < 0) return -1;
1286 PDATA_APPEND(self->file, args, -1);
1287 }
1288 else {
Tim Peters0bc93f52003-02-02 18:29:33 +00001289 if (self->write_func(self,
1290 PyString_AS_STRING(
1291 (PyStringObject *)args),
1292 size) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001293 return -1;
1294 }
1295 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001296
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001297 if (doput)
1298 if (put(self, args) < 0)
1299 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001300
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001301 return 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +00001302
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001303 err:
1304 Py_XDECREF(repr);
1305 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001306}
1307
1308
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001309#ifdef Py_USING_UNICODE
Guido van Rossumfb10c3f2000-12-19 02:08:38 +00001310/* A copy of PyUnicode_EncodeRawUnicodeEscape() that also translates
1311 backslash and newline characters to \uXXXX escapes. */
1312static PyObject *
1313modified_EncodeRawUnicodeEscape(const Py_UNICODE *s, int size)
1314{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001315 PyObject *repr;
1316 char *p;
1317 char *q;
Guido van Rossumfb10c3f2000-12-19 02:08:38 +00001318
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001319 static const char *hexdigit = "0123456789ABCDEF";
Guido van Rossumfb10c3f2000-12-19 02:08:38 +00001320
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001321 repr = PyString_FromStringAndSize(NULL, 6 * size);
1322 if (repr == NULL)
1323 return NULL;
1324 if (size == 0)
1325 return repr;
1326
1327 p = q = PyString_AS_STRING(repr);
1328 while (size-- > 0) {
1329 Py_UNICODE ch = *s++;
1330 /* Map 16-bit characters to '\uxxxx' */
1331 if (ch >= 256 || ch == '\\' || ch == '\n') {
1332 *p++ = '\\';
1333 *p++ = 'u';
1334 *p++ = hexdigit[(ch >> 12) & 0xf];
1335 *p++ = hexdigit[(ch >> 8) & 0xf];
1336 *p++ = hexdigit[(ch >> 4) & 0xf];
1337 *p++ = hexdigit[ch & 15];
1338 }
1339 /* Copy everything else as-is */
1340 else
1341 *p++ = (char) ch;
1342 }
1343 *p = '\0';
Tim Peters5de98422002-04-27 18:44:32 +00001344 _PyString_Resize(&repr, p - q);
Guido van Rossumfb10c3f2000-12-19 02:08:38 +00001345 return repr;
Guido van Rossumfb10c3f2000-12-19 02:08:38 +00001346}
1347
1348
Guido van Rossum60456fd1997-04-09 17:36:32 +00001349static int
Tim Peterscba30e22003-02-01 06:24:36 +00001350save_unicode(Picklerobject *self, PyObject *args, int doput)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001351{
1352 int size, len;
1353 PyObject *repr=0;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001354
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001355 if (!PyUnicode_Check(args))
1356 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001357
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001358 if (!self->bin) {
1359 char *repr_str;
1360 static char string = UNICODE;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001361
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001362 repr = modified_EncodeRawUnicodeEscape(
1363 PyUnicode_AS_UNICODE(args), PyUnicode_GET_SIZE(args));
Tim Peterscba30e22003-02-01 06:24:36 +00001364 if (!repr)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001365 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001366
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001367 if ((len = PyString_Size(repr)) < 0)
1368 goto err;
1369 repr_str = PyString_AS_STRING((PyStringObject *)repr);
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001370
Tim Peters0bc93f52003-02-02 18:29:33 +00001371 if (self->write_func(self, &string, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001372 goto err;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001373
Tim Peters0bc93f52003-02-02 18:29:33 +00001374 if (self->write_func(self, repr_str, len) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001375 goto err;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001376
Tim Peters0bc93f52003-02-02 18:29:33 +00001377 if (self->write_func(self, "\n", 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001378 goto err;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001379
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001380 Py_XDECREF(repr);
1381 }
1382 else {
1383 int i;
1384 char c_str[5];
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001385
Tim Peterscba30e22003-02-01 06:24:36 +00001386 if (!( repr = PyUnicode_AsUTF8String(args)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001387 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001388
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001389 if ((size = PyString_Size(repr)) < 0)
1390 goto err;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001391
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001392 c_str[0] = BINUNICODE;
1393 for (i = 1; i < 5; i++)
1394 c_str[i] = (int)(size >> ((i - 1) * 8));
1395 len = 5;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001396
Tim Peters0bc93f52003-02-02 18:29:33 +00001397 if (self->write_func(self, c_str, len) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001398 goto err;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001399
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001400 if (size > 128 && Pdata_Check(self->file)) {
1401 if (write_other(self, NULL, 0) < 0)
1402 goto err;
1403 PDATA_APPEND(self->file, repr, -1);
1404 }
1405 else {
Tim Peters0bc93f52003-02-02 18:29:33 +00001406 if (self->write_func(self, PyString_AS_STRING(repr),
1407 size) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001408 goto err;
1409 }
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001410
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001411 Py_DECREF(repr);
1412 }
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001413
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001414 if (doput)
1415 if (put(self, args) < 0)
1416 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001417
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001418 return 0;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001419
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001420 err:
1421 Py_XDECREF(repr);
1422 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001423}
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001424#endif
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001425
Tim Peters1d63c9f2003-02-02 20:29:39 +00001426/* A helper for save_tuple. Push the len elements in tuple t on the stack. */
1427static int
Tim Peters67920142003-02-05 03:46:17 +00001428store_tuple_elements(Picklerobject *self, PyObject *t, int len)
Tim Peters1d63c9f2003-02-02 20:29:39 +00001429{
1430 int i;
1431 int res = -1; /* guilty until proved innocent */
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001432
Tim Peters1d63c9f2003-02-02 20:29:39 +00001433 assert(PyTuple_Size(t) == len);
1434
1435 for (i = 0; i < len; i++) {
1436 PyObject *element = PyTuple_GET_ITEM(t, i);
1437
1438 if (element == NULL)
1439 goto finally;
1440 if (save(self, element, 0) < 0)
1441 goto finally;
1442 }
1443 res = 0;
1444
1445 finally:
1446 return res;
1447}
1448
1449/* Tuples are ubiquitous in the pickle protocols, so many techniques are
1450 * used across protocols to minimize the space needed to pickle them.
Tim Peters67920142003-02-05 03:46:17 +00001451 * Tuples are also the only builtin immutable type that can be recursive
Tim Peters1d63c9f2003-02-02 20:29:39 +00001452 * (a tuple can be reached from itself), and that requires some subtle
1453 * magic so that it works in all cases. IOW, this is a long routine.
1454 */
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001455static int
Tim Peterscba30e22003-02-01 06:24:36 +00001456save_tuple(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001457{
Tim Peters1d63c9f2003-02-02 20:29:39 +00001458 PyObject *py_tuple_id = NULL;
1459 int len, i;
1460 int res = -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001461
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001462 static char tuple = TUPLE;
Tim Peters1d63c9f2003-02-02 20:29:39 +00001463 static char pop = POP;
1464 static char pop_mark = POP_MARK;
1465 static char len2opcode[] = {EMPTY_TUPLE, TUPLE1, TUPLE2, TUPLE3};
Guido van Rossum60456fd1997-04-09 17:36:32 +00001466
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001467 if ((len = PyTuple_Size(args)) < 0)
1468 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001469
Tim Peters1d63c9f2003-02-02 20:29:39 +00001470 if (len == 0) {
1471 char c_str[2];
Tim Peters84e87f32001-03-17 04:50:51 +00001472
Tim Peters1d63c9f2003-02-02 20:29:39 +00001473 if (self->proto) {
1474 c_str[0] = EMPTY_TUPLE;
1475 len = 1;
1476 }
1477 else {
1478 c_str[0] = MARK;
1479 c_str[1] = TUPLE;
1480 len = 2;
1481 }
1482 if (self->write_func(self, c_str, len) >= 0)
1483 res = 0;
1484 /* Don't memoize an empty tuple. */
1485 goto finally;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001486 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001487
Tim Peters1d63c9f2003-02-02 20:29:39 +00001488 /* A non-empty tuple. */
1489
1490 /* id(tuple) isn't in the memo now. If it shows up there after
1491 * saving the tuple elements, the tuple must be recursive, in
1492 * which case we'll pop everything we put on the stack, and fetch
1493 * its value from the memo.
1494 */
1495 py_tuple_id = PyLong_FromVoidPtr(args);
1496 if (py_tuple_id == NULL)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001497 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001498
Tim Peters1d63c9f2003-02-02 20:29:39 +00001499 if (len <= 3 && self->proto >= 2) {
1500 /* Use TUPLE{1,2,3} opcodes. */
Tim Peters67920142003-02-05 03:46:17 +00001501 if (store_tuple_elements(self, args, len) < 0)
Tim Peters1d63c9f2003-02-02 20:29:39 +00001502 goto finally;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001503 if (PyDict_GetItem(self->memo, py_tuple_id)) {
Tim Peters1d63c9f2003-02-02 20:29:39 +00001504 /* pop the len elements */
1505 for (i = 0; i < len; ++i)
1506 if (self->write_func(self, &pop, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001507 goto finally;
Tim Peters1d63c9f2003-02-02 20:29:39 +00001508 /* fetch from memo */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001509 if (get(self, py_tuple_id) < 0)
1510 goto finally;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001511 res = 0;
1512 goto finally;
1513 }
Tim Peters1d63c9f2003-02-02 20:29:39 +00001514 /* Not recursive. */
1515 if (self->write_func(self, len2opcode + len, 1) < 0)
1516 goto finally;
1517 goto memoize;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001518 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001519
Tim Peters1d63c9f2003-02-02 20:29:39 +00001520 /* proto < 2 and len > 0, or proto >= 2 and len > 3.
1521 * Generate MARK elt1 elt2 ... TUPLE
1522 */
1523 if (self->write_func(self, &MARKv, 1) < 0)
1524 goto finally;
1525
Tim Peters67920142003-02-05 03:46:17 +00001526 if (store_tuple_elements(self, args, len) < 0)
Tim Peters1d63c9f2003-02-02 20:29:39 +00001527 goto finally;
1528
1529 if (PyDict_GetItem(self->memo, py_tuple_id)) {
1530 /* pop the stack stuff we pushed */
1531 if (self->bin) {
1532 if (self->write_func(self, &pop_mark, 1) < 0)
1533 goto finally;
1534 }
1535 else {
1536 /* Note that we pop one more than len, to remove
1537 * the MARK too.
1538 */
1539 for (i = 0; i <= len; i++)
1540 if (self->write_func(self, &pop, 1) < 0)
1541 goto finally;
1542 }
1543 /* fetch from memo */
1544 if (get(self, py_tuple_id) >= 0)
1545 res = 0;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001546 goto finally;
1547 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001548
Tim Peters1d63c9f2003-02-02 20:29:39 +00001549 /* Not recursive. */
1550 if (self->write_func(self, &tuple, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001551 goto finally;
Tim Peters84e87f32001-03-17 04:50:51 +00001552
Tim Peters1d63c9f2003-02-02 20:29:39 +00001553 memoize:
1554 if (put(self, args) >= 0)
1555 res = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001556
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001557 finally:
1558 Py_XDECREF(py_tuple_id);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001559 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001560}
1561
Tim Peters1092d642003-02-11 21:06:20 +00001562/* iter is an iterator giving items, and we batch up chunks of
1563 * MARK item item ... item APPENDS
1564 * opcode sequences. Calling code should have arranged to first create an
1565 * empty list, or list-like object, for the APPENDS to operate on.
1566 * Returns 0 on success, <0 on error.
1567 */
1568static int
1569batch_list(Picklerobject *self, PyObject *iter)
1570{
1571 PyObject *obj;
1572 PyObject *slice[BATCHSIZE];
1573 int i, n;
1574
1575 static char append = APPEND;
1576 static char appends = APPENDS;
1577
1578 assert(iter != NULL);
1579
1580 if (self->proto == 0) {
1581 /* APPENDS isn't available; do one at a time. */
1582 for (;;) {
1583 obj = PyIter_Next(iter);
1584 if (obj == NULL) {
1585 if (PyErr_Occurred())
1586 return -1;
1587 break;
1588 }
1589 i = save(self, obj, 0);
1590 Py_DECREF(obj);
1591 if (i < 0)
1592 return -1;
1593 if (self->write_func(self, &append, 1) < 0)
1594 return -1;
Tim Peters1092d642003-02-11 21:06:20 +00001595 }
1596 return 0;
1597 }
1598
1599 /* proto > 0: write in batches of BATCHSIZE. */
1600 do {
1601 /* Get next group of (no more than) BATCHSIZE elements. */
1602 for (n = 0; n < BATCHSIZE; ++n) {
1603 obj = PyIter_Next(iter);
1604 if (obj == NULL) {
1605 if (PyErr_Occurred())
1606 goto BatchFailed;
1607 break;
1608 }
1609 slice[n] = obj;
1610 }
1611
1612 if (n > 1) {
1613 /* Pump out MARK, slice[0:n], APPENDS. */
1614 if (self->write_func(self, &MARKv, 1) < 0)
1615 goto BatchFailed;
1616 for (i = 0; i < n; ++i) {
1617 if (save(self, slice[i], 0) < 0)
1618 goto BatchFailed;
1619 }
1620 if (self->write_func(self, &appends, 1) < 0)
1621 goto BatchFailed;
1622 }
1623 else if (n == 1) {
1624 if (save(self, slice[0], 0) < 0)
1625 goto BatchFailed;
1626 if (self->write_func(self, &append, 1) < 0)
1627 goto BatchFailed;
1628 }
1629
1630 for (i = 0; i < n; ++i) {
1631 Py_DECREF(slice[i]);
1632 }
Tim Peters90975f12003-02-12 05:28:58 +00001633 } while (n == BATCHSIZE);
Tim Peters1092d642003-02-11 21:06:20 +00001634 return 0;
1635
1636BatchFailed:
1637 while (--n >= 0) {
1638 Py_DECREF(slice[n]);
1639 }
1640 return -1;
1641}
1642
Guido van Rossum60456fd1997-04-09 17:36:32 +00001643static int
Tim Peterscba30e22003-02-01 06:24:36 +00001644save_list(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001645{
Tim Peters1092d642003-02-11 21:06:20 +00001646 int res = -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001647 char s[3];
Tim Peters1092d642003-02-11 21:06:20 +00001648 int len;
1649 PyObject *iter;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001650
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001651 if (self->fast && !fast_save_enter(self, args))
1652 goto finally;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00001653
Tim Peters1092d642003-02-11 21:06:20 +00001654 /* Create an empty list. */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001655 if (self->bin) {
1656 s[0] = EMPTY_LIST;
Tim Peters1092d642003-02-11 21:06:20 +00001657 len = 1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001658 }
1659 else {
1660 s[0] = MARK;
1661 s[1] = LIST;
Tim Peters1092d642003-02-11 21:06:20 +00001662 len = 2;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001663 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001664
Tim Peters1092d642003-02-11 21:06:20 +00001665 if (self->write_func(self, s, len) < 0)
1666 goto finally;
1667
1668 /* Get list length, and bow out early if empty. */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001669 if ((len = PyList_Size(args)) < 0)
1670 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001671
Tim Peters1092d642003-02-11 21:06:20 +00001672 /* Memoize. */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001673 if (len == 0) {
Tim Peters90975f12003-02-12 05:28:58 +00001674 if (put(self, args) >= 0)
1675 res = 0;
1676 goto finally;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001677 }
Tim Peters90975f12003-02-12 05:28:58 +00001678 if (put2(self, args) < 0)
1679 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001680
Tim Peters1092d642003-02-11 21:06:20 +00001681 /* Materialize the list elements. */
1682 iter = PyObject_GetIter(args);
1683 if (iter == NULL)
1684 goto finally;
1685 res = batch_list(self, iter);
1686 Py_DECREF(iter);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001687
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001688 finally:
1689 if (self->fast && !fast_save_leave(self, args))
1690 res = -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001691
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001692 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001693}
1694
1695
Tim Peters42f08ac2003-02-11 22:43:24 +00001696/* iter is an iterator giving (key, value) pairs, and we batch up chunks of
1697 * MARK key value ... key value SETITEMS
1698 * opcode sequences. Calling code should have arranged to first create an
1699 * empty dict, or dict-like object, for the SETITEMS to operate on.
1700 * Returns 0 on success, <0 on error.
1701 *
1702 * This is very much like batch_list(). The difference between saving
1703 * elements directly, and picking apart two-tuples, is so long-winded at
1704 * the C level, though, that attempts to combine these routines were too
1705 * ugly to bear.
1706 */
1707static int
1708batch_dict(Picklerobject *self, PyObject *iter)
1709{
1710 PyObject *p;
1711 PyObject *slice[BATCHSIZE];
1712 int i, n;
1713
1714 static char setitem = SETITEM;
1715 static char setitems = SETITEMS;
1716
1717 assert(iter != NULL);
1718
1719 if (self->proto == 0) {
1720 /* SETITEMS isn't available; do one at a time. */
1721 for (;;) {
1722 p = PyIter_Next(iter);
1723 if (p == NULL) {
1724 if (PyErr_Occurred())
1725 return -1;
1726 break;
1727 }
1728 if (!PyTuple_Check(p) || PyTuple_Size(p) != 2) {
1729 PyErr_SetString(PyExc_TypeError, "dict items "
1730 "iterator must return 2-tuples");
1731 return -1;
1732 }
1733 i = save(self, PyTuple_GET_ITEM(p, 0), 0);
1734 if (i >= 0)
1735 i = save(self, PyTuple_GET_ITEM(p, 1), 0);
1736 Py_DECREF(p);
1737 if (i < 0)
1738 return -1;
1739 if (self->write_func(self, &setitem, 1) < 0)
1740 return -1;
Tim Peters42f08ac2003-02-11 22:43:24 +00001741 }
1742 return 0;
1743 }
1744
1745 /* proto > 0: write in batches of BATCHSIZE. */
1746 do {
1747 /* Get next group of (no more than) BATCHSIZE elements. */
1748 for (n = 0; n < BATCHSIZE; ++n) {
1749 p = PyIter_Next(iter);
1750 if (p == NULL) {
1751 if (PyErr_Occurred())
1752 goto BatchFailed;
1753 break;
1754 }
1755 if (!PyTuple_Check(p) || PyTuple_Size(p) != 2) {
1756 PyErr_SetString(PyExc_TypeError, "dict items "
1757 "iterator must return 2-tuples");
1758 goto BatchFailed;
1759 }
1760 slice[n] = p;
1761 }
1762
1763 if (n > 1) {
1764 /* Pump out MARK, slice[0:n], SETITEMS. */
1765 if (self->write_func(self, &MARKv, 1) < 0)
1766 goto BatchFailed;
1767 for (i = 0; i < n; ++i) {
1768 p = slice[i];
1769 if (save(self, PyTuple_GET_ITEM(p, 0), 0) < 0)
1770 goto BatchFailed;
1771 if (save(self, PyTuple_GET_ITEM(p, 1), 0) < 0)
1772 goto BatchFailed;
1773 }
1774 if (self->write_func(self, &setitems, 1) < 0)
1775 goto BatchFailed;
1776 }
1777 else if (n == 1) {
1778 p = slice[0];
1779 if (save(self, PyTuple_GET_ITEM(p, 0), 0) < 0)
1780 goto BatchFailed;
1781 if (save(self, PyTuple_GET_ITEM(p, 1), 0) < 0)
1782 goto BatchFailed;
1783 if (self->write_func(self, &setitem, 1) < 0)
1784 goto BatchFailed;
1785 }
1786
1787 for (i = 0; i < n; ++i) {
1788 Py_DECREF(slice[i]);
1789 }
Tim Peters90975f12003-02-12 05:28:58 +00001790 } while (n == BATCHSIZE);
Tim Peters42f08ac2003-02-11 22:43:24 +00001791 return 0;
1792
1793BatchFailed:
1794 while (--n >= 0) {
1795 Py_DECREF(slice[n]);
1796 }
1797 return -1;
1798}
1799
Guido van Rossum60456fd1997-04-09 17:36:32 +00001800static int
Tim Peterscba30e22003-02-01 06:24:36 +00001801save_dict(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001802{
Tim Peters42f08ac2003-02-11 22:43:24 +00001803 int res = -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001804 char s[3];
Tim Peters42f08ac2003-02-11 22:43:24 +00001805 int len;
1806 PyObject *iter;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001807
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001808 if (self->fast && !fast_save_enter(self, args))
1809 goto finally;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00001810
Tim Peters42f08ac2003-02-11 22:43:24 +00001811 /* Create an empty dict. */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001812 if (self->bin) {
1813 s[0] = EMPTY_DICT;
1814 len = 1;
1815 }
1816 else {
1817 s[0] = MARK;
1818 s[1] = DICT;
1819 len = 2;
1820 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001821
Tim Peters0bc93f52003-02-02 18:29:33 +00001822 if (self->write_func(self, s, len) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001823 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001824
Tim Peters42f08ac2003-02-11 22:43:24 +00001825 /* Get dict size, and bow out early if empty. */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001826 if ((len = PyDict_Size(args)) < 0)
1827 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001828
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001829 if (len == 0) {
Tim Peters90975f12003-02-12 05:28:58 +00001830 if (put(self, args) >= 0)
1831 res = 0;
1832 goto finally;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001833 }
Tim Peters90975f12003-02-12 05:28:58 +00001834 if (put2(self, args) < 0)
1835 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001836
Tim Peters42f08ac2003-02-11 22:43:24 +00001837 /* Materialize the dict items. */
1838 iter = PyObject_CallMethod(args, "iteritems", "()");
1839 if (iter == NULL)
1840 goto finally;
1841 res = batch_dict(self, iter);
1842 Py_DECREF(iter);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001843
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001844 finally:
1845 if (self->fast && !fast_save_leave(self, args))
1846 res = -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001847
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001848 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001849}
1850
1851
Tim Peters84e87f32001-03-17 04:50:51 +00001852static int
Tim Peterscba30e22003-02-01 06:24:36 +00001853save_inst(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001854{
1855 PyObject *class = 0, *module = 0, *name = 0, *state = 0,
1856 *getinitargs_func = 0, *getstate_func = 0, *class_args = 0;
1857 char *module_str, *name_str;
1858 int module_size, name_size, res = -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001859
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001860 static char inst = INST, obj = OBJ, build = BUILD;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001861
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001862 if (self->fast && !fast_save_enter(self, args))
1863 goto finally;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00001864
Tim Peters0bc93f52003-02-02 18:29:33 +00001865 if (self->write_func(self, &MARKv, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001866 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001867
Tim Peterscba30e22003-02-01 06:24:36 +00001868 if (!( class = PyObject_GetAttr(args, __class___str)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001869 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001870
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001871 if (self->bin) {
1872 if (save(self, class, 0) < 0)
1873 goto finally;
1874 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001875
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001876 if ((getinitargs_func = PyObject_GetAttr(args, __getinitargs___str))) {
1877 PyObject *element = 0;
1878 int i, len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001879
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001880 if (!( class_args =
Tim Peterscba30e22003-02-01 06:24:36 +00001881 PyObject_Call(getinitargs_func, empty_tuple, NULL)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001882 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001883
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001884 if ((len = PyObject_Size(class_args)) < 0)
1885 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001886
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001887 for (i = 0; i < len; i++) {
Tim Peterscba30e22003-02-01 06:24:36 +00001888 if (!( element = PySequence_GetItem(class_args, i)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001889 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001890
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001891 if (save(self, element, 0) < 0) {
1892 Py_DECREF(element);
1893 goto finally;
1894 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001895
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001896 Py_DECREF(element);
1897 }
1898 }
1899 else {
1900 PyErr_Clear();
1901 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001902
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001903 if (!self->bin) {
1904 if (!( name = ((PyClassObject *)class)->cl_name )) {
1905 PyErr_SetString(PicklingError, "class has no name");
1906 goto finally;
1907 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001908
Tim Peterscba30e22003-02-01 06:24:36 +00001909 if (!( module = whichmodule(class, name)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001910 goto finally;
Guido van Rossum053b8df1998-11-25 16:18:00 +00001911
Tim Peters84e87f32001-03-17 04:50:51 +00001912
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001913 if ((module_size = PyString_Size(module)) < 0 ||
1914 (name_size = PyString_Size(name)) < 0)
1915 goto finally;
Guido van Rossum053b8df1998-11-25 16:18:00 +00001916
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001917 module_str = PyString_AS_STRING((PyStringObject *)module);
1918 name_str = PyString_AS_STRING((PyStringObject *)name);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001919
Tim Peters0bc93f52003-02-02 18:29:33 +00001920 if (self->write_func(self, &inst, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001921 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001922
Tim Peters0bc93f52003-02-02 18:29:33 +00001923 if (self->write_func(self, module_str, module_size) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001924 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001925
Tim Peters0bc93f52003-02-02 18:29:33 +00001926 if (self->write_func(self, "\n", 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001927 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001928
Tim Peters0bc93f52003-02-02 18:29:33 +00001929 if (self->write_func(self, name_str, name_size) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001930 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001931
Tim Peters0bc93f52003-02-02 18:29:33 +00001932 if (self->write_func(self, "\n", 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001933 goto finally;
1934 }
Tim Peters0bc93f52003-02-02 18:29:33 +00001935 else if (self->write_func(self, &obj, 1) < 0) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001936 goto finally;
1937 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001938
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001939 if ((getstate_func = PyObject_GetAttr(args, __getstate___str))) {
1940 state = PyObject_Call(getstate_func, empty_tuple, NULL);
Tim Peterscba30e22003-02-01 06:24:36 +00001941 if (!state)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001942 goto finally;
1943 }
1944 else {
1945 PyErr_Clear();
Guido van Rossum60456fd1997-04-09 17:36:32 +00001946
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001947 if (!( state = PyObject_GetAttr(args, __dict___str))) {
1948 PyErr_Clear();
1949 res = 0;
1950 goto finally;
1951 }
1952 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001953
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001954 if (!PyDict_Check(state)) {
1955 if (put2(self, args) < 0)
1956 goto finally;
1957 }
1958 else {
1959 if (put(self, args) < 0)
1960 goto finally;
1961 }
Tim Peters84e87f32001-03-17 04:50:51 +00001962
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001963 if (save(self, state, 0) < 0)
1964 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001965
Tim Peters0bc93f52003-02-02 18:29:33 +00001966 if (self->write_func(self, &build, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001967 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001968
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001969 res = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001970
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001971 finally:
1972 if (self->fast && !fast_save_leave(self, args))
1973 res = -1;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00001974
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001975 Py_XDECREF(module);
1976 Py_XDECREF(class);
1977 Py_XDECREF(state);
1978 Py_XDECREF(getinitargs_func);
1979 Py_XDECREF(getstate_func);
1980 Py_XDECREF(class_args);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001981
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001982 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001983}
1984
1985
Guido van Rossum60456fd1997-04-09 17:36:32 +00001986static int
Tim Peterscba30e22003-02-01 06:24:36 +00001987save_global(Picklerobject *self, PyObject *args, PyObject *name)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001988{
Jeremy Hylton9ee91f12002-07-11 22:02:33 +00001989 PyObject *global_name = 0, *module = 0, *mod = 0, *klass = 0;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001990 char *name_str, *module_str;
1991 int module_size, name_size, res = -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001992
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001993 static char global = GLOBAL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001994
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001995 if (name) {
1996 global_name = name;
1997 Py_INCREF(global_name);
1998 }
1999 else {
Tim Peterscba30e22003-02-01 06:24:36 +00002000 if (!( global_name = PyObject_GetAttr(args, __name___str)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002001 goto finally;
2002 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002003
Tim Peterscba30e22003-02-01 06:24:36 +00002004 if (!( module = whichmodule(args, global_name)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002005 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002006
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002007 if ((module_size = PyString_Size(module)) < 0 ||
2008 (name_size = PyString_Size(global_name)) < 0)
2009 goto finally;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002010
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002011 module_str = PyString_AS_STRING((PyStringObject *)module);
2012 name_str = PyString_AS_STRING((PyStringObject *)global_name);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002013
Guido van Rossum75bfd052002-12-24 18:10:07 +00002014 /* XXX This can be doing a relative import. Clearly it shouldn't,
2015 but I don't know how to stop it. :-( */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002016 mod = PyImport_ImportModule(module_str);
2017 if (mod == NULL) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002018 cPickle_ErrFormat(PicklingError,
Tim Petersb9ce7cd2003-02-18 20:50:45 +00002019 "Can't pickle %s: import of module %s "
2020 "failed",
2021 "OS", args, module);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002022 goto finally;
2023 }
Jeremy Hylton0e1f7a82002-07-11 22:01:40 +00002024 klass = PyObject_GetAttrString(mod, name_str);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002025 if (klass == NULL) {
2026 cPickle_ErrFormat(PicklingError,
Tim Petersb9ce7cd2003-02-18 20:50:45 +00002027 "Can't pickle %s: attribute lookup %s.%s "
2028 "failed",
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002029 "OSS", args, module, global_name);
2030 goto finally;
2031 }
2032 if (klass != args) {
Jeremy Hylton0e1f7a82002-07-11 22:01:40 +00002033 Py_DECREF(klass);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002034 cPickle_ErrFormat(PicklingError,
Tim Peters731098b2003-02-04 20:56:09 +00002035 "Can't pickle %s: it's not the same object "
2036 "as %s.%s",
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002037 "OSS", args, module, global_name);
2038 goto finally;
2039 }
Jeremy Hylton0e1f7a82002-07-11 22:01:40 +00002040 Py_DECREF(klass);
Guido van Rossuma92d16a2001-08-18 21:22:07 +00002041
Tim Peters731098b2003-02-04 20:56:09 +00002042 if (self->proto >= 2) {
2043 /* See whether this is in the extension registry, and if
2044 * so generate an EXT opcode.
2045 */
2046 PyObject *py_code; /* extension code as Python object */
Tim Peters3e667d52003-02-04 21:47:44 +00002047 long code; /* extension code as C value */
Tim Peters731098b2003-02-04 20:56:09 +00002048 char c_str[5];
2049 int n;
2050
2051 PyTuple_SET_ITEM(two_tuple, 0, module);
2052 PyTuple_SET_ITEM(two_tuple, 1, global_name);
2053 py_code = PyDict_GetItem(extension_registry, two_tuple);
2054 if (py_code == NULL)
2055 goto gen_global; /* not registered */
2056
2057 /* Verify py_code has the right type and value. */
2058 if (!PyInt_Check(py_code)) {
2059 cPickle_ErrFormat(PicklingError, "Can't pickle %s: "
Tim Peters6288e232003-02-05 03:53:10 +00002060 "extension code %s isn't an integer",
Tim Peters731098b2003-02-04 20:56:09 +00002061 "OO", args, py_code);
2062 goto finally;
2063 }
2064 code = PyInt_AS_LONG(py_code);
2065 if (code <= 0 || code > 0x7fffffffL) {
2066 cPickle_ErrFormat(PicklingError, "Can't pickle %s: "
2067 "extension code %ld is out of range",
2068 "Ol", args, code);
2069 goto finally;
2070 }
2071
2072 /* Generate an EXT opcode. */
2073 if (code <= 0xff) {
2074 c_str[0] = EXT1;
2075 c_str[1] = (char)code;
2076 n = 2;
2077 }
2078 else if (code <= 0xffff) {
2079 c_str[0] = EXT2;
2080 c_str[1] = (char)(code & 0xff);
2081 c_str[2] = (char)((code >> 8) & 0xff);
2082 n = 3;
2083 }
2084 else {
2085 c_str[0] = EXT4;
2086 c_str[1] = (char)(code & 0xff);
2087 c_str[2] = (char)((code >> 8) & 0xff);
2088 c_str[3] = (char)((code >> 16) & 0xff);
2089 c_str[4] = (char)((code >> 24) & 0xff);
2090 n = 5;
2091 }
2092
2093 if (self->write_func(self, c_str, n) >= 0)
2094 res = 0;
2095 goto finally; /* and don't memoize */
2096 }
2097
2098 gen_global:
Tim Peters0bc93f52003-02-02 18:29:33 +00002099 if (self->write_func(self, &global, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002100 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002101
Tim Peters0bc93f52003-02-02 18:29:33 +00002102 if (self->write_func(self, module_str, module_size) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002103 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002104
Tim Peters0bc93f52003-02-02 18:29:33 +00002105 if (self->write_func(self, "\n", 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002106 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002107
Tim Peters0bc93f52003-02-02 18:29:33 +00002108 if (self->write_func(self, name_str, name_size) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002109 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002110
Tim Peters0bc93f52003-02-02 18:29:33 +00002111 if (self->write_func(self, "\n", 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002112 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002113
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002114 if (put(self, args) < 0)
2115 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002116
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002117 res = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002118
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002119 finally:
2120 Py_XDECREF(module);
2121 Py_XDECREF(global_name);
2122 Py_XDECREF(mod);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002123
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002124 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002125}
2126
Guido van Rossum60456fd1997-04-09 17:36:32 +00002127static int
Tim Peterscba30e22003-02-01 06:24:36 +00002128save_pers(Picklerobject *self, PyObject *args, PyObject *f)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002129{
2130 PyObject *pid = 0;
2131 int size, res = -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002132
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002133 static char persid = PERSID, binpersid = BINPERSID;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002134
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002135 Py_INCREF(args);
2136 ARG_TUP(self, args);
2137 if (self->arg) {
2138 pid = PyObject_Call(f, self->arg, NULL);
2139 FREE_ARG_TUP(self);
2140 }
2141 if (! pid) return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002142
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002143 if (pid != Py_None) {
2144 if (!self->bin) {
2145 if (!PyString_Check(pid)) {
2146 PyErr_SetString(PicklingError,
2147 "persistent id must be string");
2148 goto finally;
2149 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002150
Tim Peters0bc93f52003-02-02 18:29:33 +00002151 if (self->write_func(self, &persid, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002152 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002153
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002154 if ((size = PyString_Size(pid)) < 0)
2155 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002156
Tim Peters0bc93f52003-02-02 18:29:33 +00002157 if (self->write_func(self,
2158 PyString_AS_STRING(
2159 (PyStringObject *)pid),
2160 size) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002161 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002162
Tim Peters0bc93f52003-02-02 18:29:33 +00002163 if (self->write_func(self, "\n", 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002164 goto finally;
Tim Peters84e87f32001-03-17 04:50:51 +00002165
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002166 res = 1;
2167 goto finally;
2168 }
2169 else if (save(self, pid, 1) >= 0) {
Tim Peters0bc93f52003-02-02 18:29:33 +00002170 if (self->write_func(self, &binpersid, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002171 res = -1;
2172 else
2173 res = 1;
2174 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002175
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002176 goto finally;
2177 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002178
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002179 res = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002180
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002181 finally:
2182 Py_XDECREF(pid);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002183
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002184 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002185}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002186
Tim Peters71fcda52003-02-14 23:05:28 +00002187/* We're saving ob, and args is the 2-thru-5 tuple returned by the
2188 * appropriate __reduce__ method for ob.
2189 */
Tim Peters84e87f32001-03-17 04:50:51 +00002190static int
Tim Peters71fcda52003-02-14 23:05:28 +00002191save_reduce(Picklerobject *self, PyObject *args, PyObject *ob)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002192{
Tim Peters71fcda52003-02-14 23:05:28 +00002193 PyObject *callable;
2194 PyObject *argtup;
2195 PyObject *state = NULL;
2196 PyObject *listitems = NULL;
2197 PyObject *dictitems = NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002198
Tim Peters71fcda52003-02-14 23:05:28 +00002199 int use_newobj = self->proto >= 2;
2200
2201 static char reduce = REDUCE;
2202 static char build = BUILD;
2203 static char newobj = NEWOBJ;
2204
2205 if (! PyArg_UnpackTuple(args, "save_reduce", 2, 5,
2206 &callable,
2207 &argtup,
2208 &state,
2209 &listitems,
2210 &dictitems))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002211 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002212
Tim Peters71fcda52003-02-14 23:05:28 +00002213 if (state == Py_None)
2214 state = NULL;
2215 if (listitems == Py_None)
2216 listitems = NULL;
2217 if (dictitems == Py_None)
2218 dictitems = NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002219
Tim Peters71fcda52003-02-14 23:05:28 +00002220 /* Protocol 2 special case: if callable's name is __newobj__, use
2221 * NEWOBJ. This consumes a lot of code.
2222 */
2223 if (use_newobj) {
2224 PyObject *temp = PyObject_GetAttr(callable, __name___str);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002225
Tim Peters71fcda52003-02-14 23:05:28 +00002226 if (temp == NULL) {
2227 PyErr_Clear();
2228 use_newobj = 0;
2229 }
2230 else {
2231 use_newobj = PyString_Check(temp) &&
2232 strcmp(PyString_AS_STRING(temp),
2233 "__newobj__") == 0;
2234 Py_DECREF(temp);
2235 }
2236 }
2237 if (use_newobj) {
2238 PyObject *cls;
2239 PyObject *newargtup;
2240 int n, i;
2241
2242 /* Sanity checks. */
2243 n = PyTuple_Size(argtup);
2244 if (n < 1) {
2245 PyErr_SetString(PicklingError, "__newobj__ arglist "
2246 "is empty");
2247 return -1;
2248 }
2249
2250 cls = PyTuple_GET_ITEM(argtup, 0);
2251 if (! PyObject_HasAttrString(cls, "__new__")) {
2252 PyErr_SetString(PicklingError, "args[0] from "
2253 "__newobj__ args has no __new__");
2254 return -1;
2255 }
2256
2257 /* XXX How could ob be NULL? */
2258 if (ob != NULL) {
2259 PyObject *ob_dot_class;
2260
2261 ob_dot_class = PyObject_GetAttr(ob, __class___str);
2262 if (ob_dot_class == NULL)
2263 PyErr_Clear();
2264 i = ob_dot_class != cls; /* true iff a problem */
2265 Py_XDECREF(ob_dot_class);
2266 if (i) {
2267 PyErr_SetString(PicklingError, "args[0] from "
2268 "__newobj__ args has the wrong class");
2269 return -1;
2270 }
2271 }
2272
2273 /* Save the class and its __new__ arguments. */
2274 if (save(self, cls, 0) < 0)
2275 return -1;
2276
2277 newargtup = PyTuple_New(n-1); /* argtup[1:] */
2278 if (newargtup == NULL)
2279 return -1;
2280 for (i = 1; i < n; ++i) {
2281 PyObject *temp = PyTuple_GET_ITEM(argtup, i);
2282 Py_INCREF(temp);
2283 PyTuple_SET_ITEM(newargtup, i-1, temp);
2284 }
2285 i = save(self, newargtup, 0) < 0;
2286 Py_DECREF(newargtup);
2287 if (i < 0)
2288 return -1;
2289
2290 /* Add NEWOBJ opcode. */
2291 if (self->write_func(self, &newobj, 1) < 0)
2292 return -1;
2293 }
2294 else {
2295 /* Not using NEWOBJ. */
2296 if (save(self, callable, 0) < 0 ||
2297 save(self, argtup, 0) < 0 ||
2298 self->write_func(self, &reduce, 1) < 0)
2299 return -1;
2300 }
2301
2302 /* Memoize. */
2303 /* XXX How can ob be NULL? */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002304 if (ob != NULL) {
2305 if (state && !PyDict_Check(state)) {
2306 if (put2(self, ob) < 0)
2307 return -1;
2308 }
Tim Peters71fcda52003-02-14 23:05:28 +00002309 else if (put(self, ob) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002310 return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002311 }
Tim Peters84e87f32001-03-17 04:50:51 +00002312
Guido van Rossum60456fd1997-04-09 17:36:32 +00002313
Tim Peters71fcda52003-02-14 23:05:28 +00002314 if (listitems && batch_list(self, listitems) < 0)
2315 return -1;
2316
2317 if (dictitems && batch_dict(self, dictitems) < 0)
2318 return -1;
2319
2320 if (state) {
2321 if (save(self, state, 0) < 0 ||
2322 self->write_func(self, &build, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002323 return -1;
2324 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002325
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002326 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002327}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002328
Guido van Rossum60456fd1997-04-09 17:36:32 +00002329static int
Tim Peters1d63c9f2003-02-02 20:29:39 +00002330save(Picklerobject *self, PyObject *args, int pers_save)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002331{
2332 PyTypeObject *type;
Tim Peters71fcda52003-02-14 23:05:28 +00002333 PyObject *py_ob_id = 0, *__reduce__ = 0, *t = 0;
2334 PyObject *arg_tup;
2335 int res = -1;
2336 int tmp, size;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002337
Martin v. Löwis5a395302002-08-04 08:20:23 +00002338 if (self->nesting++ > Py_GetRecursionLimit()){
2339 PyErr_SetString(PyExc_RuntimeError,
2340 "maximum recursion depth exceeded");
2341 goto finally;
2342 }
2343
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002344 if (!pers_save && self->pers_func) {
2345 if ((tmp = save_pers(self, args, self->pers_func)) != 0) {
2346 res = tmp;
2347 goto finally;
2348 }
2349 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002350
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002351 if (args == Py_None) {
2352 res = save_none(self, args);
2353 goto finally;
2354 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002355
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002356 type = args->ob_type;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002357
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002358 switch (type->tp_name[0]) {
Guido van Rossum77f6a652002-04-03 22:41:51 +00002359 case 'b':
2360 if (args == Py_False || args == Py_True) {
2361 res = save_bool(self, args);
2362 goto finally;
2363 }
2364 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002365 case 'i':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002366 if (type == &PyInt_Type) {
2367 res = save_int(self, args);
2368 goto finally;
2369 }
2370 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002371
Guido van Rossum60456fd1997-04-09 17:36:32 +00002372 case 'l':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002373 if (type == &PyLong_Type) {
2374 res = save_long(self, args);
2375 goto finally;
2376 }
2377 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002378
Guido van Rossum60456fd1997-04-09 17:36:32 +00002379 case 'f':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002380 if (type == &PyFloat_Type) {
2381 res = save_float(self, args);
2382 goto finally;
2383 }
2384 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002385
Guido van Rossum60456fd1997-04-09 17:36:32 +00002386 case 't':
Tim Peters1d63c9f2003-02-02 20:29:39 +00002387 if (type == &PyTuple_Type && PyTuple_Size(args) == 0) {
2388 res = save_tuple(self, args);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002389 goto finally;
2390 }
2391 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002392
Guido van Rossum60456fd1997-04-09 17:36:32 +00002393 case 's':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002394 if ((type == &PyString_Type) && (PyString_GET_SIZE(args) < 2)) {
2395 res = save_string(self, args, 0);
2396 goto finally;
2397 }
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00002398
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002399#ifdef Py_USING_UNICODE
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00002400 case 'u':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002401 if ((type == &PyUnicode_Type) && (PyString_GET_SIZE(args) < 2)) {
2402 res = save_unicode(self, args, 0);
2403 goto finally;
2404 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002405#endif
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002406 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002407
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002408 if (args->ob_refcnt > 1) {
Tim Peterscba30e22003-02-01 06:24:36 +00002409 if (!( py_ob_id = PyLong_FromVoidPtr(args)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002410 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002411
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002412 if (PyDict_GetItem(self->memo, py_ob_id)) {
2413 if (get(self, py_ob_id) < 0)
2414 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002415
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002416 res = 0;
2417 goto finally;
2418 }
2419 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002420
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002421 switch (type->tp_name[0]) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00002422 case 's':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002423 if (type == &PyString_Type) {
2424 res = save_string(self, args, 1);
2425 goto finally;
2426 }
2427 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002428
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002429#ifdef Py_USING_UNICODE
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00002430 case 'u':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002431 if (type == &PyUnicode_Type) {
2432 res = save_unicode(self, args, 1);
2433 goto finally;
2434 }
2435 break;
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002436#endif
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00002437
Guido van Rossum60456fd1997-04-09 17:36:32 +00002438 case 't':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002439 if (type == &PyTuple_Type) {
2440 res = save_tuple(self, args);
2441 goto finally;
2442 }
2443 if (type == &PyType_Type) {
2444 res = save_global(self, args, NULL);
2445 goto finally;
2446 }
2447 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002448
Guido van Rossum60456fd1997-04-09 17:36:32 +00002449 case 'l':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002450 if (type == &PyList_Type) {
2451 res = save_list(self, args);
2452 goto finally;
2453 }
2454 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002455
2456 case 'd':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002457 if (type == &PyDict_Type) {
2458 res = save_dict(self, args);
2459 goto finally;
2460 }
2461 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002462
2463 case 'i':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002464 if (type == &PyInstance_Type) {
2465 res = save_inst(self, args);
2466 goto finally;
2467 }
2468 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002469
2470 case 'c':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002471 if (type == &PyClass_Type) {
2472 res = save_global(self, args, NULL);
2473 goto finally;
2474 }
2475 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002476
2477 case 'f':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002478 if (type == &PyFunction_Type) {
2479 res = save_global(self, args, NULL);
2480 goto finally;
2481 }
2482 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002483
2484 case 'b':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002485 if (type == &PyCFunction_Type) {
2486 res = save_global(self, args, NULL);
2487 goto finally;
2488 }
2489 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002490
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002491 if (!pers_save && self->inst_pers_func) {
2492 if ((tmp = save_pers(self, args, self->inst_pers_func)) != 0) {
2493 res = tmp;
2494 goto finally;
2495 }
2496 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002497
Jeremy Hylton39c61162002-07-16 19:47:43 +00002498 if (PyType_IsSubtype(type, &PyType_Type)) {
2499 res = save_global(self, args, NULL);
2500 goto finally;
2501 }
2502
Guido van Rossumb289b872003-02-19 01:45:13 +00002503 /* Get a reduction callable, and call it. This may come from
2504 * copy_reg.dispatch_table, the object's __reduce_ex__ method,
2505 * or the object's __reduce__ method.
Tim Peters71fcda52003-02-14 23:05:28 +00002506 */
Tim Peters5aa3da62003-02-13 21:03:57 +00002507 __reduce__ = PyDict_GetItem(dispatch_table, (PyObject *)type);
2508 if (__reduce__ != NULL) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002509 Py_INCREF(__reduce__);
Guido van Rossumb289b872003-02-19 01:45:13 +00002510 Py_INCREF(args);
2511 ARG_TUP(self, args);
2512 if (self->arg) {
2513 t = PyObject_Call(__reduce__, self->arg, NULL);
2514 FREE_ARG_TUP(self);
2515 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002516 }
2517 else {
Guido van Rossumb289b872003-02-19 01:45:13 +00002518 /* Check for a __reduce_ex__ method. */
2519 __reduce__ = PyObject_GetAttr(args, __reduce_ex___str);
2520 if (__reduce__ != NULL) {
2521 t = PyInt_FromLong(self->proto);
2522 if (t != NULL) {
2523 ARG_TUP(self, t);
2524 t = NULL;
2525 if (self->arg) {
2526 t = PyObject_Call(__reduce__,
2527 self->arg, NULL);
2528 FREE_ARG_TUP(self);
2529 }
2530 }
2531 }
2532 else {
Tim Peters5aa3da62003-02-13 21:03:57 +00002533 PyErr_Clear();
Guido van Rossumb289b872003-02-19 01:45:13 +00002534 /* Check for a __reduce__ method. */
2535 __reduce__ = PyObject_GetAttr(args, __reduce___str);
2536 if (__reduce__ != NULL) {
2537 t = PyObject_Call(__reduce__,
2538 empty_tuple, NULL);
2539 }
2540 else {
2541 PyErr_SetObject(UnpickleableError, args);
2542 goto finally;
2543 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002544 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002545 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002546
Tim Peters71fcda52003-02-14 23:05:28 +00002547 if (t == NULL)
2548 goto finally;
Tim Peters84e87f32001-03-17 04:50:51 +00002549
Tim Peters71fcda52003-02-14 23:05:28 +00002550 if (PyString_Check(t)) {
2551 res = save_global(self, args, t);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002552 goto finally;
2553 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002554
Tim Peters71fcda52003-02-14 23:05:28 +00002555 if (! PyTuple_Check(t)) {
2556 cPickle_ErrFormat(PicklingError, "Value returned by "
2557 "%s must be string or tuple",
2558 "O", __reduce__);
2559 goto finally;
2560 }
2561
2562 size = PyTuple_Size(t);
2563 if (size < 2 || size > 5) {
2564 cPickle_ErrFormat(PicklingError, "tuple returned by "
2565 "%s must contain 2 through 5 elements",
2566 "O", __reduce__);
2567 goto finally;
2568 }
2569
2570 arg_tup = PyTuple_GET_ITEM(t, 1);
2571 if (!(PyTuple_Check(arg_tup) || arg_tup == Py_None)) {
2572 cPickle_ErrFormat(PicklingError, "Second element of "
2573 "tuple returned by %s must be a tuple",
2574 "O", __reduce__);
2575 goto finally;
2576 }
2577
2578 res = save_reduce(self, t, args);
Guido van Rossum60456fd1997-04-09 17:36:32 +00002579
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002580 finally:
Martin v. Löwis5a395302002-08-04 08:20:23 +00002581 self->nesting--;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002582 Py_XDECREF(py_ob_id);
2583 Py_XDECREF(__reduce__);
2584 Py_XDECREF(t);
Tim Peters84e87f32001-03-17 04:50:51 +00002585
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002586 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002587}
2588
2589
2590static int
Tim Peterscba30e22003-02-01 06:24:36 +00002591dump(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002592{
2593 static char stop = STOP;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002594
Tim Peters4190fb82003-02-02 16:09:05 +00002595 if (self->proto >= 2) {
2596 char bytes[2];
2597
2598 bytes[0] = PROTO;
Tim Peters87482ea2003-02-02 16:16:30 +00002599 assert(self->proto >= 0 && self->proto < 256);
2600 bytes[1] = (char)self->proto;
Tim Peters4190fb82003-02-02 16:09:05 +00002601 if (self->write_func(self, bytes, 2) < 0)
2602 return -1;
2603 }
2604
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002605 if (save(self, args, 0) < 0)
2606 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002607
Tim Peters4190fb82003-02-02 16:09:05 +00002608 if (self->write_func(self, &stop, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002609 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002610
Tim Peters4190fb82003-02-02 16:09:05 +00002611 if (self->write_func(self, NULL, 0) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002612 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002613
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002614 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002615}
2616
2617static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00002618Pickle_clear_memo(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002619{
Tim Peterscba30e22003-02-01 06:24:36 +00002620 if (self->memo)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002621 PyDict_Clear(self->memo);
2622 Py_INCREF(Py_None);
2623 return Py_None;
2624}
2625
2626static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00002627Pickle_getvalue(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002628{
2629 int l, i, rsize, ssize, clear=1, lm;
2630 long ik;
2631 PyObject *k, *r;
2632 char *s, *p, *have_get;
2633 Pdata *data;
2634
2635 /* Can be called by Python code or C code */
Tim Peterscba30e22003-02-01 06:24:36 +00002636 if (args && !PyArg_ParseTuple(args, "|i:getvalue", &clear))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002637 return NULL;
2638
2639 /* Check to make sure we are based on a list */
2640 if (! Pdata_Check(self->file)) {
2641 PyErr_SetString(PicklingError,
2642 "Attempt to getvalue() a non-list-based pickler");
2643 return NULL;
2644 }
2645
2646 /* flush write buffer */
2647 if (write_other(self, NULL, 0) < 0) return NULL;
2648
2649 data=(Pdata*)self->file;
2650 l=data->length;
2651
2652 /* set up an array to hold get/put status */
Tim Petersac5687a2003-02-02 18:08:34 +00002653 lm = PyDict_Size(self->memo);
2654 if (lm < 0) return NULL;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002655 lm++;
Tim Petersac5687a2003-02-02 18:08:34 +00002656 have_get = malloc(lm);
2657 if (have_get == NULL) return PyErr_NoMemory();
2658 memset(have_get, 0, lm);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002659
2660 /* Scan for gets. */
Tim Petersac5687a2003-02-02 18:08:34 +00002661 for (rsize = 0, i = l; --i >= 0; ) {
2662 k = data->data[i];
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002663
Tim Petersac5687a2003-02-02 18:08:34 +00002664 if (PyString_Check(k))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002665 rsize += PyString_GET_SIZE(k);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002666
2667 else if (PyInt_Check(k)) { /* put */
Tim Petersac5687a2003-02-02 18:08:34 +00002668 ik = PyInt_AS_LONG((PyIntObject*)k);
2669 if (ik >= lm || ik == 0) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002670 PyErr_SetString(PicklingError,
2671 "Invalid get data");
2672 return NULL;
2673 }
Tim Petersac5687a2003-02-02 18:08:34 +00002674 if (have_get[ik]) /* with matching get */
2675 rsize += ik < 256 ? 2 : 5;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002676 }
2677
2678 else if (! (PyTuple_Check(k) &&
2679 PyTuple_GET_SIZE(k) == 2 &&
Tim Petersac5687a2003-02-02 18:08:34 +00002680 PyInt_Check((k = PyTuple_GET_ITEM(k, 0))))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002681 ) {
2682 PyErr_SetString(PicklingError,
2683 "Unexpected data in internal list");
2684 return NULL;
2685 }
2686
2687 else { /* put */
Tim Petersac5687a2003-02-02 18:08:34 +00002688 ik = PyInt_AS_LONG((PyIntObject *)k);
2689 if (ik >= lm || ik == 0) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002690 PyErr_SetString(PicklingError,
2691 "Invalid get data");
2692 return NULL;
2693 }
Tim Petersac5687a2003-02-02 18:08:34 +00002694 have_get[ik] = 1;
2695 rsize += ik < 256 ? 2 : 5;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002696 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002697 }
2698
2699 /* Now generate the result */
Tim Petersac5687a2003-02-02 18:08:34 +00002700 r = PyString_FromStringAndSize(NULL, rsize);
2701 if (r == NULL) goto err;
2702 s = PyString_AS_STRING((PyStringObject *)r);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002703
Tim Petersac5687a2003-02-02 18:08:34 +00002704 for (i = 0; i < l; i++) {
2705 k = data->data[i];
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002706
2707 if (PyString_Check(k)) {
Tim Petersac5687a2003-02-02 18:08:34 +00002708 ssize = PyString_GET_SIZE(k);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002709 if (ssize) {
Tim Petersac5687a2003-02-02 18:08:34 +00002710 p=PyString_AS_STRING((PyStringObject *)k);
2711 while (--ssize >= 0)
2712 *s++ = *p++;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002713 }
2714 }
2715
2716 else if (PyTuple_Check(k)) { /* get */
Tim Petersac5687a2003-02-02 18:08:34 +00002717 ik = PyInt_AS_LONG((PyIntObject *)
2718 PyTuple_GET_ITEM(k, 0));
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002719 if (ik < 256) {
2720 *s++ = BINGET;
2721 *s++ = (int)(ik & 0xff);
2722 }
2723 else {
2724 *s++ = LONG_BINGET;
2725 *s++ = (int)(ik & 0xff);
2726 *s++ = (int)((ik >> 8) & 0xff);
2727 *s++ = (int)((ik >> 16) & 0xff);
2728 *s++ = (int)((ik >> 24) & 0xff);
2729 }
2730 }
2731
2732 else { /* put */
Tim Petersac5687a2003-02-02 18:08:34 +00002733 ik = PyInt_AS_LONG((PyIntObject*)k);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002734
2735 if (have_get[ik]) { /* with matching get */
2736 if (ik < 256) {
2737 *s++ = BINPUT;
2738 *s++ = (int)(ik & 0xff);
2739 }
2740 else {
2741 *s++ = LONG_BINPUT;
2742 *s++ = (int)(ik & 0xff);
2743 *s++ = (int)((ik >> 8) & 0xff);
2744 *s++ = (int)((ik >> 16) & 0xff);
2745 *s++ = (int)((ik >> 24) & 0xff);
2746 }
2747 }
2748 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002749 }
2750
2751 if (clear) {
2752 PyDict_Clear(self->memo);
Tim Petersac5687a2003-02-02 18:08:34 +00002753 Pdata_clear(data, 0);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002754 }
2755
2756 free(have_get);
2757 return r;
2758 err:
2759 free(have_get);
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00002760 return NULL;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002761}
2762
2763static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00002764Pickler_dump(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002765{
2766 PyObject *ob;
2767 int get=0;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002768
Tim Peterscba30e22003-02-01 06:24:36 +00002769 if (!( PyArg_ParseTuple(args, "O|i:dump", &ob, &get)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002770 return NULL;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002771
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002772 if (dump(self, ob) < 0)
2773 return NULL;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002774
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002775 if (get) return Pickle_getvalue(self, NULL);
Guido van Rossum053b8df1998-11-25 16:18:00 +00002776
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002777 /* XXX Why does dump() return self? */
2778 Py_INCREF(self);
2779 return (PyObject*)self;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002780}
2781
2782
Tim Peterscba30e22003-02-01 06:24:36 +00002783static struct PyMethodDef Pickler_methods[] =
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002784{
Neal Norwitzb0493252002-03-31 14:44:22 +00002785 {"dump", (PyCFunction)Pickler_dump, METH_VARARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00002786 PyDoc_STR("dump(object) -- "
2787 "Write an object in pickle format to the object's pickle stream")},
Fred Drake0ebacc82002-05-01 20:36:39 +00002788 {"clear_memo", (PyCFunction)Pickle_clear_memo, METH_NOARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00002789 PyDoc_STR("clear_memo() -- Clear the picklers memo")},
Neal Norwitzb0493252002-03-31 14:44:22 +00002790 {"getvalue", (PyCFunction)Pickle_getvalue, METH_VARARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00002791 PyDoc_STR("getvalue() -- Finish picking a list-based pickle")},
Guido van Rossum60456fd1997-04-09 17:36:32 +00002792 {NULL, NULL} /* sentinel */
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002793};
2794
2795
2796static Picklerobject *
Tim Peters5bd2a792003-02-01 16:45:06 +00002797newPicklerobject(PyObject *file, int proto)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002798{
2799 Picklerobject *self;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002800
Tim Peters5bd2a792003-02-01 16:45:06 +00002801 if (proto < 0)
Tim Peters8587b3c2003-02-13 15:44:41 +00002802 proto = HIGHEST_PROTOCOL;
2803 if (proto > HIGHEST_PROTOCOL) {
Tim Peters5bd2a792003-02-01 16:45:06 +00002804 PyErr_Format(PyExc_ValueError, "pickle protocol %d asked for; "
2805 "the highest available protocol is %d",
Tim Peters8587b3c2003-02-13 15:44:41 +00002806 proto, HIGHEST_PROTOCOL);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002807 return NULL;
Tim Peters5bd2a792003-02-01 16:45:06 +00002808 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002809
Tim Peters5bd2a792003-02-01 16:45:06 +00002810 self = PyObject_New(Picklerobject, &Picklertype);
2811 if (self == NULL)
2812 return NULL;
2813 self->proto = proto;
2814 self->bin = proto > 0;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002815 self->fp = NULL;
2816 self->write = NULL;
2817 self->memo = NULL;
2818 self->arg = NULL;
2819 self->pers_func = NULL;
2820 self->inst_pers_func = NULL;
2821 self->write_buf = NULL;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002822 self->fast = 0;
Martin v. Löwis5a395302002-08-04 08:20:23 +00002823 self->nesting = 0;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002824 self->fast_container = 0;
2825 self->fast_memo = NULL;
2826 self->buf_size = 0;
2827 self->dispatch_table = NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002828
Tim Peters5bd2a792003-02-01 16:45:06 +00002829 self->file = NULL;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002830 if (file)
2831 Py_INCREF(file);
Tim Peters5bd2a792003-02-01 16:45:06 +00002832 else {
2833 file = Pdata_New();
2834 if (file == NULL)
2835 goto err;
2836 }
2837 self->file = file;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002838
Tim Peterscba30e22003-02-01 06:24:36 +00002839 if (!( self->memo = PyDict_New()))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002840 goto err;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002841
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002842 if (PyFile_Check(file)) {
2843 self->fp = PyFile_AsFile(file);
2844 if (self->fp == NULL) {
Tim Peters5bd2a792003-02-01 16:45:06 +00002845 PyErr_SetString(PyExc_ValueError,
2846 "I/O operation on closed file");
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002847 goto err;
2848 }
2849 self->write_func = write_file;
Guido van Rossumc91fcaa1999-03-29 20:00:14 +00002850 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002851 else if (PycStringIO_OutputCheck(file)) {
2852 self->write_func = write_cStringIO;
2853 }
2854 else if (file == Py_None) {
2855 self->write_func = write_none;
2856 }
2857 else {
2858 self->write_func = write_other;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002859
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002860 if (! Pdata_Check(file)) {
2861 self->write = PyObject_GetAttr(file, write_str);
2862 if (!self->write) {
2863 PyErr_Clear();
Tim Peterscba30e22003-02-01 06:24:36 +00002864 PyErr_SetString(PyExc_TypeError,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002865 "argument must have 'write' "
2866 "attribute");
2867 goto err;
2868 }
2869 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002870
Tim Peters5bd2a792003-02-01 16:45:06 +00002871 self->write_buf = (char *)PyMem_Malloc(WRITE_BUF_SIZE);
2872 if (self->write_buf == NULL) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002873 PyErr_NoMemory();
2874 goto err;
2875 }
2876 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002877
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002878 if (PyEval_GetRestricted()) {
2879 /* Restricted execution, get private tables */
Tim Peters5b7da392003-02-04 00:21:07 +00002880 PyObject *m = PyImport_Import(copy_reg_str);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002881
Tim Peters5b7da392003-02-04 00:21:07 +00002882 if (m == NULL)
2883 goto err;
2884 self->dispatch_table = PyObject_GetAttr(m, dispatch_table_str);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002885 Py_DECREF(m);
Tim Peters5b7da392003-02-04 00:21:07 +00002886 if (self->dispatch_table == NULL)
2887 goto err;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002888 }
2889 else {
Tim Peters5b7da392003-02-04 00:21:07 +00002890 self->dispatch_table = dispatch_table;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002891 Py_INCREF(dispatch_table);
2892 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002893
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002894 return self;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002895
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002896 err:
Tim Peters5bd2a792003-02-01 16:45:06 +00002897 Py_DECREF(self);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002898 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002899}
2900
2901
2902static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00002903get_Pickler(PyObject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002904{
2905 PyObject *file = NULL;
Tim Peters5bd2a792003-02-01 16:45:06 +00002906 int proto = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002907
Tim Peters92c8bb32003-02-13 23:00:26 +00002908 /* XXX
2909 * The documented signature is Pickler(file, proto=0), but this
2910 * accepts Pickler() and Pickler(integer) too. The meaning then
2911 * is clear as mud, undocumented, and not supported by pickle.py.
2912 * I'm told Zope uses this, but I haven't traced into this code
2913 * far enough to figure out what it means.
Tim Peters5bd2a792003-02-01 16:45:06 +00002914 */
2915 if (!PyArg_ParseTuple(args, "|i:Pickler", &proto)) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002916 PyErr_Clear();
Tim Peters5bd2a792003-02-01 16:45:06 +00002917 proto = 0;
2918 if (!PyArg_ParseTuple(args, "O|i:Pickler", &file, &proto))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002919 return NULL;
2920 }
Tim Peters5bd2a792003-02-01 16:45:06 +00002921 return (PyObject *)newPicklerobject(file, proto);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002922}
2923
2924
2925static void
Tim Peterscba30e22003-02-01 06:24:36 +00002926Pickler_dealloc(Picklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002927{
2928 Py_XDECREF(self->write);
2929 Py_XDECREF(self->memo);
2930 Py_XDECREF(self->fast_memo);
2931 Py_XDECREF(self->arg);
2932 Py_XDECREF(self->file);
2933 Py_XDECREF(self->pers_func);
2934 Py_XDECREF(self->inst_pers_func);
2935 Py_XDECREF(self->dispatch_table);
Tim Peters5bd2a792003-02-01 16:45:06 +00002936 PyMem_Free(self->write_buf);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002937 PyObject_Del(self);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002938}
2939
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002940static PyObject *
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00002941Pickler_get_pers_func(Picklerobject *p)
2942{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002943 if (p->pers_func == NULL)
2944 PyErr_SetString(PyExc_AttributeError, "persistent_id");
2945 else
2946 Py_INCREF(p->pers_func);
2947 return p->pers_func;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002948}
2949
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00002950static int
2951Pickler_set_pers_func(Picklerobject *p, PyObject *v)
2952{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002953 if (v == NULL) {
2954 PyErr_SetString(PyExc_TypeError,
2955 "attribute deletion is not supported");
2956 return -1;
2957 }
2958 Py_XDECREF(p->pers_func);
2959 Py_INCREF(v);
2960 p->pers_func = v;
2961 return 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002962}
2963
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00002964static int
2965Pickler_set_inst_pers_func(Picklerobject *p, PyObject *v)
2966{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002967 if (v == NULL) {
2968 PyErr_SetString(PyExc_TypeError,
2969 "attribute deletion is not supported");
2970 return -1;
2971 }
2972 Py_XDECREF(p->inst_pers_func);
2973 Py_INCREF(v);
2974 p->inst_pers_func = v;
2975 return 0;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00002976}
2977
2978static PyObject *
2979Pickler_get_memo(Picklerobject *p)
2980{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002981 if (p->memo == NULL)
2982 PyErr_SetString(PyExc_AttributeError, "memo");
2983 else
2984 Py_INCREF(p->memo);
2985 return p->memo;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00002986}
2987
2988static int
2989Pickler_set_memo(Picklerobject *p, PyObject *v)
2990{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002991 if (v == NULL) {
2992 PyErr_SetString(PyExc_TypeError,
2993 "attribute deletion is not supported");
2994 return -1;
2995 }
2996 if (!PyDict_Check(v)) {
2997 PyErr_SetString(PyExc_TypeError, "memo must be a dictionary");
2998 return -1;
2999 }
3000 Py_XDECREF(p->memo);
3001 Py_INCREF(v);
3002 p->memo = v;
3003 return 0;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003004}
3005
3006static PyObject *
3007Pickler_get_error(Picklerobject *p)
3008{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003009 /* why is this an attribute on the Pickler? */
3010 Py_INCREF(PicklingError);
3011 return PicklingError;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003012}
3013
3014static PyMemberDef Pickler_members[] = {
3015 {"binary", T_INT, offsetof(Picklerobject, bin)},
3016 {"fast", T_INT, offsetof(Picklerobject, fast)},
Jeremy Hylton3eb46f32001-10-16 17:10:49 +00003017 {NULL}
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003018};
3019
3020static PyGetSetDef Pickler_getsets[] = {
Tim Peterscba30e22003-02-01 06:24:36 +00003021 {"persistent_id", (getter)Pickler_get_pers_func,
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003022 (setter)Pickler_set_pers_func},
3023 {"inst_persistent_id", NULL, (setter)Pickler_set_inst_pers_func},
3024 {"memo", (getter)Pickler_get_memo, (setter)Pickler_set_memo},
Jeremy Hylton3eb46f32001-10-16 17:10:49 +00003025 {"PicklingError", (getter)Pickler_get_error, NULL},
3026 {NULL}
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003027};
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003028
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003029PyDoc_STRVAR(Picklertype__doc__,
3030"Objects that know how to pickle objects\n");
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003031
Guido van Rossum9716aaa1997-12-08 15:15:16 +00003032static PyTypeObject Picklertype = {
3033 PyObject_HEAD_INIT(NULL)
Guido van Rossum60456fd1997-04-09 17:36:32 +00003034 0, /*ob_size*/
Guido van Rossum14648392001-12-08 18:02:58 +00003035 "cPickle.Pickler", /*tp_name*/
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003036 sizeof(Picklerobject), /*tp_basicsize*/
3037 0,
3038 (destructor)Pickler_dealloc, /* tp_dealloc */
3039 0, /* tp_print */
3040 0, /* tp_getattr */
3041 0, /* tp_setattr */
3042 0, /* tp_compare */
3043 0, /* tp_repr */
3044 0, /* tp_as_number */
3045 0, /* tp_as_sequence */
3046 0, /* tp_as_mapping */
3047 0, /* tp_hash */
3048 0, /* tp_call */
3049 0, /* tp_str */
Jason Tishlerfb8595d2003-01-06 12:41:26 +00003050 PyObject_GenericGetAttr, /* tp_getattro */
3051 PyObject_GenericSetAttr, /* tp_setattro */
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003052 0, /* tp_as_buffer */
3053 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
3054 Picklertype__doc__, /* tp_doc */
3055 0, /* tp_traverse */
3056 0, /* tp_clear */
3057 0, /* tp_richcompare */
3058 0, /* tp_weaklistoffset */
3059 0, /* tp_iter */
3060 0, /* tp_iternext */
3061 Pickler_methods, /* tp_methods */
3062 Pickler_members, /* tp_members */
3063 Pickler_getsets, /* tp_getset */
Guido van Rossum9716aaa1997-12-08 15:15:16 +00003064};
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003065
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003066static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00003067find_class(PyObject *py_module_name, PyObject *py_global_name, PyObject *fc)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003068{
3069 PyObject *global = 0, *module;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003070
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003071 if (fc) {
3072 if (fc==Py_None) {
Tim Peters92c8bb32003-02-13 23:00:26 +00003073 PyErr_SetString(UnpicklingError, "Global and instance "
3074 "pickles are not supported.");
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003075 return NULL;
3076 }
Tim Peterscba30e22003-02-01 06:24:36 +00003077 return PyObject_CallFunction(fc, "OO", py_module_name,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003078 py_global_name);
3079 }
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00003080
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003081 module = PySys_GetObject("modules");
3082 if (module == NULL)
3083 return NULL;
Jeremy Hyltond1055231998-08-11 19:52:51 +00003084
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003085 module = PyDict_GetItem(module, py_module_name);
3086 if (module == NULL) {
3087 module = PyImport_Import(py_module_name);
3088 if (!module)
3089 return NULL;
3090 global = PyObject_GetAttr(module, py_global_name);
3091 Py_DECREF(module);
3092 }
3093 else
3094 global = PyObject_GetAttr(module, py_global_name);
3095 return global;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003096}
3097
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003098static int
Tim Peterscba30e22003-02-01 06:24:36 +00003099marker(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003100{
3101 if (self->num_marks < 1) {
3102 PyErr_SetString(UnpicklingError, "could not find MARK");
3103 return -1;
3104 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003105
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003106 return self->marks[--self->num_marks];
Guido van Rossum60456fd1997-04-09 17:36:32 +00003107}
3108
Tim Peters84e87f32001-03-17 04:50:51 +00003109
Guido van Rossum60456fd1997-04-09 17:36:32 +00003110static int
Tim Peterscba30e22003-02-01 06:24:36 +00003111load_none(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003112{
3113 PDATA_APPEND(self->stack, Py_None, -1);
3114 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003115}
3116
Guido van Rossumf9ffb031999-02-04 14:54:04 +00003117static int
Tim Peterscba30e22003-02-01 06:24:36 +00003118bad_readline(void)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003119{
3120 PyErr_SetString(UnpicklingError, "pickle data was truncated");
3121 return -1;
Guido van Rossumf9ffb031999-02-04 14:54:04 +00003122}
Guido van Rossum60456fd1997-04-09 17:36:32 +00003123
3124static int
Tim Peterscba30e22003-02-01 06:24:36 +00003125load_int(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003126{
3127 PyObject *py_int = 0;
3128 char *endptr, *s;
3129 int len, res = -1;
3130 long l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003131
Tim Peters0bc93f52003-02-02 18:29:33 +00003132 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003133 if (len < 2) return bad_readline();
3134 if (!( s=pystrndup(s,len))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003135
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003136 errno = 0;
3137 l = strtol(s, &endptr, 0);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003138
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003139 if (errno || (*endptr != '\n') || (endptr[1] != '\0')) {
3140 /* Hm, maybe we've got something long. Let's try reading
3141 it as a Python long object. */
3142 errno = 0;
3143 py_int = PyLong_FromString(s, NULL, 0);
3144 if (py_int == NULL) {
3145 PyErr_SetString(PyExc_ValueError,
3146 "could not convert string to int");
3147 goto finally;
3148 }
3149 }
3150 else {
Guido van Rossume2763392002-04-05 19:30:08 +00003151 if (len == 3 && (l == 0 || l == 1)) {
3152 if (!( py_int = PyBool_FromLong(l))) goto finally;
3153 }
3154 else {
3155 if (!( py_int = PyInt_FromLong(l))) goto finally;
3156 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003157 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003158
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003159 free(s);
3160 PDATA_PUSH(self->stack, py_int, -1);
3161 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003162
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003163 finally:
3164 free(s);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003165
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003166 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003167}
3168
Tim Peters3c67d792003-02-02 17:59:11 +00003169static int
3170load_bool(Unpicklerobject *self, PyObject *boolean)
3171{
3172 assert(boolean == Py_True || boolean == Py_False);
Tim Peterse0a39072003-02-03 15:45:56 +00003173 PDATA_APPEND(self->stack, boolean, -1);
Tim Peters3c67d792003-02-02 17:59:11 +00003174 return 0;
3175}
3176
Tim Petersee1a53c2003-02-02 02:57:53 +00003177/* s contains x bytes of a little-endian integer. Return its value as a
3178 * C int. Obscure: when x is 1 or 2, this is an unsigned little-endian
3179 * int, but when x is 4 it's a signed one. This is an historical source
3180 * of x-platform bugs.
3181 */
Tim Peters84e87f32001-03-17 04:50:51 +00003182static long
Tim Petersee1a53c2003-02-02 02:57:53 +00003183calc_binint(char *s, int x)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003184{
3185 unsigned char c;
3186 int i;
3187 long l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003188
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003189 for (i = 0, l = 0L; i < x; i++) {
3190 c = (unsigned char)s[i];
3191 l |= (long)c << (i * 8);
3192 }
Tim Petersbfa18f72001-04-10 01:54:42 +00003193#if SIZEOF_LONG > 4
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003194 /* Unlike BININT1 and BININT2, BININT (more accurately BININT4)
3195 * is signed, so on a box with longs bigger than 4 bytes we need
3196 * to extend a BININT's sign bit to the full width.
3197 */
3198 if (x == 4 && l & (1L << 31))
3199 l |= (~0L) << 32;
Tim Petersbfa18f72001-04-10 01:54:42 +00003200#endif
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003201 return l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003202}
3203
3204
3205static int
Tim Peterscba30e22003-02-01 06:24:36 +00003206load_binintx(Unpicklerobject *self, char *s, int x)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003207{
3208 PyObject *py_int = 0;
3209 long l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003210
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003211 l = calc_binint(s, x);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003212
Tim Peterscba30e22003-02-01 06:24:36 +00003213 if (!( py_int = PyInt_FromLong(l)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003214 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003215
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003216 PDATA_PUSH(self->stack, py_int, -1);
3217 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003218}
3219
3220
3221static int
Tim Peterscba30e22003-02-01 06:24:36 +00003222load_binint(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003223{
3224 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003225
Tim Peters0bc93f52003-02-02 18:29:33 +00003226 if (self->read_func(self, &s, 4) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003227 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003228
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003229 return load_binintx(self, s, 4);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003230}
3231
3232
3233static int
Tim Peterscba30e22003-02-01 06:24:36 +00003234load_binint1(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003235{
3236 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003237
Tim Peters0bc93f52003-02-02 18:29:33 +00003238 if (self->read_func(self, &s, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003239 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003240
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003241 return load_binintx(self, s, 1);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003242}
3243
3244
3245static int
Tim Peterscba30e22003-02-01 06:24:36 +00003246load_binint2(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003247{
3248 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003249
Tim Peters0bc93f52003-02-02 18:29:33 +00003250 if (self->read_func(self, &s, 2) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003251 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003252
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003253 return load_binintx(self, s, 2);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003254}
Tim Peters84e87f32001-03-17 04:50:51 +00003255
Guido van Rossum60456fd1997-04-09 17:36:32 +00003256static int
Tim Peterscba30e22003-02-01 06:24:36 +00003257load_long(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003258{
3259 PyObject *l = 0;
3260 char *end, *s;
3261 int len, res = -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003262
Tim Peters0bc93f52003-02-02 18:29:33 +00003263 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003264 if (len < 2) return bad_readline();
3265 if (!( s=pystrndup(s,len))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003266
Tim Peterscba30e22003-02-01 06:24:36 +00003267 if (!( l = PyLong_FromString(s, &end, 0)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003268 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003269
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003270 free(s);
3271 PDATA_PUSH(self->stack, l, -1);
3272 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003273
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003274 finally:
3275 free(s);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003276
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003277 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003278}
3279
Tim Petersee1a53c2003-02-02 02:57:53 +00003280/* 'size' bytes contain the # of bytes of little-endian 256's-complement
3281 * data following.
3282 */
3283static int
3284load_counted_long(Unpicklerobject *self, int size)
3285{
3286 int i;
3287 char *nbytes;
3288 unsigned char *pdata;
3289 PyObject *along;
3290
3291 assert(size == 1 || size == 4);
3292 i = self->read_func(self, &nbytes, size);
3293 if (i < 0) return -1;
3294
3295 size = calc_binint(nbytes, size);
3296 if (size < 0) {
3297 /* Corrupt or hostile pickle -- we never write one like
3298 * this.
3299 */
Tim Peters0c7c48e2003-02-03 22:07:24 +00003300 PyErr_SetString(UnpicklingError, "LONG pickle has negative "
Tim Petersee1a53c2003-02-02 02:57:53 +00003301 "byte count");
3302 return -1;
3303 }
3304
3305 if (size == 0)
3306 along = PyLong_FromLong(0L);
3307 else {
3308 /* Read the raw little-endian bytes & convert. */
Neal Norwitz529baf22003-02-02 17:08:33 +00003309 i = self->read_func(self, (char **)&pdata, size);
Tim Petersee1a53c2003-02-02 02:57:53 +00003310 if (i < 0) return -1;
3311 along = _PyLong_FromByteArray(pdata, (size_t)size,
3312 1 /* little endian */, 1 /* signed */);
3313 }
3314 if (along == NULL)
3315 return -1;
3316 PDATA_PUSH(self->stack, along, -1);
3317 return 0;
3318}
Tim Peters84e87f32001-03-17 04:50:51 +00003319
Guido van Rossum60456fd1997-04-09 17:36:32 +00003320static int
Tim Peterscba30e22003-02-01 06:24:36 +00003321load_float(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003322{
3323 PyObject *py_float = 0;
3324 char *endptr, *s;
3325 int len, res = -1;
3326 double d;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003327
Tim Peters0bc93f52003-02-02 18:29:33 +00003328 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003329 if (len < 2) return bad_readline();
3330 if (!( s=pystrndup(s,len))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003331
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003332 errno = 0;
3333 d = strtod(s, &endptr);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003334
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003335 if (errno || (endptr[0] != '\n') || (endptr[1] != '\0')) {
3336 PyErr_SetString(PyExc_ValueError,
3337 "could not convert string to float");
3338 goto finally;
3339 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003340
Tim Peterscba30e22003-02-01 06:24:36 +00003341 if (!( py_float = PyFloat_FromDouble(d)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003342 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003343
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003344 free(s);
3345 PDATA_PUSH(self->stack, py_float, -1);
3346 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003347
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003348 finally:
3349 free(s);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003350
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003351 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003352}
3353
Guido van Rossum60456fd1997-04-09 17:36:32 +00003354static int
Tim Peterscba30e22003-02-01 06:24:36 +00003355load_binfloat(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003356{
3357 PyObject *py_float = 0;
3358 int s, e;
3359 long fhi, flo;
3360 double x;
3361 char *p;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003362
Tim Peters0bc93f52003-02-02 18:29:33 +00003363 if (self->read_func(self, &p, 8) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003364 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003365
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003366 /* First byte */
3367 s = (*p>>7) & 1;
3368 e = (*p & 0x7F) << 4;
3369 p++;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003370
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003371 /* Second byte */
3372 e |= (*p>>4) & 0xF;
3373 fhi = (*p & 0xF) << 24;
3374 p++;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003375
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003376 /* Third byte */
3377 fhi |= (*p & 0xFF) << 16;
3378 p++;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003379
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003380 /* Fourth byte */
3381 fhi |= (*p & 0xFF) << 8;
3382 p++;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003383
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003384 /* Fifth byte */
3385 fhi |= *p & 0xFF;
3386 p++;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003387
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003388 /* Sixth byte */
3389 flo = (*p & 0xFF) << 16;
3390 p++;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003391
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003392 /* Seventh byte */
3393 flo |= (*p & 0xFF) << 8;
3394 p++;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003395
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003396 /* Eighth byte */
3397 flo |= *p & 0xFF;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003398
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003399 x = (double)fhi + (double)flo / 16777216.0; /* 2**24 */
3400 x /= 268435456.0; /* 2**28 */
Guido van Rossum60456fd1997-04-09 17:36:32 +00003401
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003402 /* XXX This sadly ignores Inf/NaN */
3403 if (e == 0)
3404 e = -1022;
3405 else {
3406 x += 1.0;
3407 e -= 1023;
3408 }
3409 x = ldexp(x, e);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003410
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003411 if (s)
3412 x = -x;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003413
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003414 if (!( py_float = PyFloat_FromDouble(x))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003415
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003416 PDATA_PUSH(self->stack, py_float, -1);
3417 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003418}
Guido van Rossum60456fd1997-04-09 17:36:32 +00003419
3420static int
Tim Peterscba30e22003-02-01 06:24:36 +00003421load_string(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003422{
3423 PyObject *str = 0;
Martin v. Löwis8a8da792002-08-14 07:46:28 +00003424 int len, res = -1;
3425 char *s, *p;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003426
Tim Peters0bc93f52003-02-02 18:29:33 +00003427 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003428 if (len < 2) return bad_readline();
3429 if (!( s=pystrndup(s,len))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003430
Martin v. Löwis8a8da792002-08-14 07:46:28 +00003431
3432 /* Strip outermost quotes */
3433 while (s[len-1] <= ' ')
3434 len--;
3435 if(s[0]=='"' && s[len-1]=='"'){
3436 s[len-1] = '\0';
3437 p = s + 1 ;
3438 len -= 2;
3439 } else if(s[0]=='\'' && s[len-1]=='\''){
3440 s[len-1] = '\0';
3441 p = s + 1 ;
3442 len -= 2;
3443 } else
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003444 goto insecure;
3445 /********************************************/
Guido van Rossum9716aaa1997-12-08 15:15:16 +00003446
Martin v. Löwis8a8da792002-08-14 07:46:28 +00003447 str = PyString_DecodeEscape(p, len, NULL, 0, NULL);
3448 if (str) {
3449 PDATA_PUSH(self->stack, str, -1);
3450 res = 0;
3451 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003452 free(s);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003453 return res;
Tim Peters84e87f32001-03-17 04:50:51 +00003454
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003455 insecure:
3456 free(s);
3457 PyErr_SetString(PyExc_ValueError,"insecure string pickle");
3458 return -1;
Tim Peters84e87f32001-03-17 04:50:51 +00003459}
Guido van Rossum60456fd1997-04-09 17:36:32 +00003460
3461
3462static int
Tim Peterscba30e22003-02-01 06:24:36 +00003463load_binstring(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003464{
3465 PyObject *py_string = 0;
3466 long l;
3467 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003468
Tim Peters0bc93f52003-02-02 18:29:33 +00003469 if (self->read_func(self, &s, 4) < 0) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003470
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003471 l = calc_binint(s, 4);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003472
Tim Peters0bc93f52003-02-02 18:29:33 +00003473 if (self->read_func(self, &s, l) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003474 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003475
Tim Peterscba30e22003-02-01 06:24:36 +00003476 if (!( py_string = PyString_FromStringAndSize(s, l)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003477 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003478
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003479 PDATA_PUSH(self->stack, py_string, -1);
3480 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003481}
3482
3483
3484static int
Tim Peterscba30e22003-02-01 06:24:36 +00003485load_short_binstring(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003486{
3487 PyObject *py_string = 0;
3488 unsigned char l;
3489 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003490
Tim Peters0bc93f52003-02-02 18:29:33 +00003491 if (self->read_func(self, &s, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003492 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003493
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003494 l = (unsigned char)s[0];
Guido van Rossum60456fd1997-04-09 17:36:32 +00003495
Tim Peters0bc93f52003-02-02 18:29:33 +00003496 if (self->read_func(self, &s, l) < 0) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003497
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003498 if (!( py_string = PyString_FromStringAndSize(s, l))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003499
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003500 PDATA_PUSH(self->stack, py_string, -1);
3501 return 0;
Tim Peters84e87f32001-03-17 04:50:51 +00003502}
Guido van Rossum60456fd1997-04-09 17:36:32 +00003503
3504
Martin v. Löwis339d0f72001-08-17 18:39:25 +00003505#ifdef Py_USING_UNICODE
Guido van Rossum60456fd1997-04-09 17:36:32 +00003506static int
Tim Peterscba30e22003-02-01 06:24:36 +00003507load_unicode(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003508{
3509 PyObject *str = 0;
3510 int len, res = -1;
3511 char *s;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003512
Tim Peters0bc93f52003-02-02 18:29:33 +00003513 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003514 if (len < 1) return bad_readline();
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003515
Tim Peterscba30e22003-02-01 06:24:36 +00003516 if (!( str = PyUnicode_DecodeRawUnicodeEscape(s, len - 1, NULL)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003517 goto finally;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003518
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003519 PDATA_PUSH(self->stack, str, -1);
3520 return 0;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003521
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003522 finally:
3523 return res;
Tim Peters84e87f32001-03-17 04:50:51 +00003524}
Martin v. Löwis339d0f72001-08-17 18:39:25 +00003525#endif
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003526
3527
Martin v. Löwis339d0f72001-08-17 18:39:25 +00003528#ifdef Py_USING_UNICODE
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003529static int
Tim Peterscba30e22003-02-01 06:24:36 +00003530load_binunicode(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003531{
3532 PyObject *unicode;
3533 long l;
3534 char *s;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003535
Tim Peters0bc93f52003-02-02 18:29:33 +00003536 if (self->read_func(self, &s, 4) < 0) return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003537
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003538 l = calc_binint(s, 4);
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003539
Tim Peters0bc93f52003-02-02 18:29:33 +00003540 if (self->read_func(self, &s, l) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003541 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003542
Tim Peterscba30e22003-02-01 06:24:36 +00003543 if (!( unicode = PyUnicode_DecodeUTF8(s, l, NULL)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003544 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003545
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003546 PDATA_PUSH(self->stack, unicode, -1);
3547 return 0;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003548}
Martin v. Löwis339d0f72001-08-17 18:39:25 +00003549#endif
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003550
3551
3552static int
Tim Peterscba30e22003-02-01 06:24:36 +00003553load_tuple(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003554{
3555 PyObject *tup;
3556 int i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003557
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003558 if ((i = marker(self)) < 0) return -1;
3559 if (!( tup=Pdata_popTuple(self->stack, i))) return -1;
3560 PDATA_PUSH(self->stack, tup, -1);
3561 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003562}
3563
3564static int
Tim Peters1d63c9f2003-02-02 20:29:39 +00003565load_counted_tuple(Unpicklerobject *self, int len)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003566{
Tim Peters1d63c9f2003-02-02 20:29:39 +00003567 PyObject *tup = PyTuple_New(len);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003568
Tim Peters1d63c9f2003-02-02 20:29:39 +00003569 if (tup == NULL)
3570 return -1;
3571
3572 while (--len >= 0) {
3573 PyObject *element;
3574
3575 PDATA_POP(self->stack, element);
3576 if (element == NULL)
3577 return -1;
3578 PyTuple_SET_ITEM(tup, len, element);
3579 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003580 PDATA_PUSH(self->stack, tup, -1);
3581 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003582}
3583
3584static int
Tim Peterscba30e22003-02-01 06:24:36 +00003585load_empty_list(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003586{
3587 PyObject *list;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003588
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003589 if (!( list=PyList_New(0))) return -1;
3590 PDATA_PUSH(self->stack, list, -1);
3591 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003592}
3593
3594static int
Tim Peterscba30e22003-02-01 06:24:36 +00003595load_empty_dict(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003596{
3597 PyObject *dict;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003598
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003599 if (!( dict=PyDict_New())) return -1;
3600 PDATA_PUSH(self->stack, dict, -1);
3601 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003602}
3603
3604
3605static int
Tim Peterscba30e22003-02-01 06:24:36 +00003606load_list(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003607{
3608 PyObject *list = 0;
3609 int i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003610
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003611 if ((i = marker(self)) < 0) return -1;
3612 if (!( list=Pdata_popList(self->stack, i))) return -1;
3613 PDATA_PUSH(self->stack, list, -1);
3614 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003615}
3616
3617static int
Tim Peterscba30e22003-02-01 06:24:36 +00003618load_dict(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003619{
3620 PyObject *dict, *key, *value;
3621 int i, j, k;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003622
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003623 if ((i = marker(self)) < 0) return -1;
3624 j=self->stack->length;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003625
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003626 if (!( dict = PyDict_New())) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003627
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003628 for (k = i+1; k < j; k += 2) {
3629 key =self->stack->data[k-1];
3630 value=self->stack->data[k ];
3631 if (PyDict_SetItem(dict, key, value) < 0) {
3632 Py_DECREF(dict);
3633 return -1;
3634 }
3635 }
3636 Pdata_clear(self->stack, i);
3637 PDATA_PUSH(self->stack, dict, -1);
3638 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003639}
3640
3641static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00003642Instance_New(PyObject *cls, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003643{
Tim Peters1f1b2d22003-02-01 02:16:37 +00003644 PyObject *r = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003645
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003646 if (PyClass_Check(cls)) {
3647 int l;
Tim Peters84e87f32001-03-17 04:50:51 +00003648
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003649 if ((l=PyObject_Size(args)) < 0) goto err;
3650 if (!( l )) {
3651 PyObject *__getinitargs__;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003652
Tim Peterscba30e22003-02-01 06:24:36 +00003653 __getinitargs__ = PyObject_GetAttr(cls,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003654 __getinitargs___str);
3655 if (!__getinitargs__) {
Tim Peterscba30e22003-02-01 06:24:36 +00003656 /* We have a class with no __getinitargs__,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003657 so bypass usual construction */
3658 PyObject *inst;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003659
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003660 PyErr_Clear();
Tim Peterscba30e22003-02-01 06:24:36 +00003661 if (!( inst=PyInstance_NewRaw(cls, NULL)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003662 goto err;
3663 return inst;
3664 }
3665 Py_DECREF(__getinitargs__);
3666 }
Tim Peters84e87f32001-03-17 04:50:51 +00003667
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003668 if ((r=PyInstance_New(cls, args, NULL))) return r;
3669 else goto err;
3670 }
Tim Peters84e87f32001-03-17 04:50:51 +00003671
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003672 if (args==Py_None) {
3673 /* Special case, call cls.__basicnew__() */
3674 PyObject *basicnew;
Tim Peters84e87f32001-03-17 04:50:51 +00003675
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003676 basicnew = PyObject_GetAttr(cls, __basicnew___str);
3677 if (!basicnew) return NULL;
3678 r=PyObject_CallObject(basicnew, NULL);
3679 Py_DECREF(basicnew);
3680 if (r) return r;
3681 }
Guido van Rossum9716aaa1997-12-08 15:15:16 +00003682
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003683 if ((r=PyObject_CallObject(cls, args))) return r;
Guido van Rossum142eeb81997-08-13 03:14:41 +00003684
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003685 err:
3686 {
3687 PyObject *tp, *v, *tb;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003688
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003689 PyErr_Fetch(&tp, &v, &tb);
3690 if ((r=Py_BuildValue("OOO",v,cls,args))) {
3691 Py_XDECREF(v);
3692 v=r;
3693 }
3694 PyErr_Restore(tp,v,tb);
3695 }
3696 return NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003697}
Tim Peters84e87f32001-03-17 04:50:51 +00003698
Guido van Rossum60456fd1997-04-09 17:36:32 +00003699
3700static int
Tim Peterscba30e22003-02-01 06:24:36 +00003701load_obj(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003702{
3703 PyObject *class, *tup, *obj=0;
3704 int i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003705
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003706 if ((i = marker(self)) < 0) return -1;
3707 if (!( tup=Pdata_popTuple(self->stack, i+1))) return -1;
3708 PDATA_POP(self->stack, class);
3709 if (class) {
3710 obj = Instance_New(class, tup);
3711 Py_DECREF(class);
3712 }
3713 Py_DECREF(tup);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003714
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003715 if (! obj) return -1;
3716 PDATA_PUSH(self->stack, obj, -1);
3717 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003718}
3719
3720
3721static int
Tim Peterscba30e22003-02-01 06:24:36 +00003722load_inst(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003723{
3724 PyObject *tup, *class=0, *obj=0, *module_name, *class_name;
3725 int i, len;
3726 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003727
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003728 if ((i = marker(self)) < 0) return -1;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003729
Tim Peters0bc93f52003-02-02 18:29:33 +00003730 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003731 if (len < 2) return bad_readline();
3732 module_name = PyString_FromStringAndSize(s, len - 1);
3733 if (!module_name) return -1;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003734
Tim Peters0bc93f52003-02-02 18:29:33 +00003735 if ((len = self->readline_func(self, &s)) >= 0) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003736 if (len < 2) return bad_readline();
3737 if ((class_name = PyString_FromStringAndSize(s, len - 1))) {
Tim Peterscba30e22003-02-01 06:24:36 +00003738 class = find_class(module_name, class_name,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003739 self->find_class);
3740 Py_DECREF(class_name);
3741 }
3742 }
3743 Py_DECREF(module_name);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003744
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003745 if (! class) return -1;
Tim Peters84e87f32001-03-17 04:50:51 +00003746
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003747 if ((tup=Pdata_popTuple(self->stack, i))) {
3748 obj = Instance_New(class, tup);
3749 Py_DECREF(tup);
3750 }
3751 Py_DECREF(class);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003752
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003753 if (! obj) return -1;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003754
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003755 PDATA_PUSH(self->stack, obj, -1);
3756 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003757}
3758
Tim Peterseab7db32003-02-13 18:24:14 +00003759static int
3760load_newobj(Unpicklerobject *self)
3761{
3762 PyObject *args = NULL;
3763 PyObject *clsraw = NULL;
3764 PyTypeObject *cls; /* clsraw cast to its true type */
3765 PyObject *obj;
3766
3767 /* Stack is ... cls argtuple, and we want to call
3768 * cls.__new__(cls, *argtuple).
3769 */
3770 PDATA_POP(self->stack, args);
3771 if (args == NULL) goto Fail;
3772 if (! PyTuple_Check(args)) {
3773 PyErr_SetString(UnpicklingError, "NEWOBJ expected an arg "
3774 "tuple.");
3775 goto Fail;
3776 }
3777
3778 PDATA_POP(self->stack, clsraw);
3779 cls = (PyTypeObject *)clsraw;
3780 if (cls == NULL) goto Fail;
3781 if (! PyType_Check(cls)) {
3782 PyErr_SetString(UnpicklingError, "NEWOBJ class argument "
3783 "isn't a type object");
3784 goto Fail;
3785 }
3786 if (cls->tp_new == NULL) {
3787 PyErr_SetString(UnpicklingError, "NEWOBJ class argument "
3788 "has NULL tp_new");
3789 goto Fail;
3790 }
3791
3792 /* Call __new__. */
3793 obj = cls->tp_new(cls, args, NULL);
3794 if (obj == NULL) goto Fail;
3795
3796 Py_DECREF(args);
3797 Py_DECREF(clsraw);
3798 PDATA_PUSH(self->stack, obj, -1);
3799 return 0;
3800
3801 Fail:
3802 Py_XDECREF(args);
3803 Py_XDECREF(clsraw);
3804 return -1;
3805}
Guido van Rossum60456fd1997-04-09 17:36:32 +00003806
3807static int
Tim Peterscba30e22003-02-01 06:24:36 +00003808load_global(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003809{
3810 PyObject *class = 0, *module_name = 0, *class_name = 0;
3811 int len;
3812 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003813
Tim Peters0bc93f52003-02-02 18:29:33 +00003814 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003815 if (len < 2) return bad_readline();
3816 module_name = PyString_FromStringAndSize(s, len - 1);
3817 if (!module_name) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003818
Tim Peters0bc93f52003-02-02 18:29:33 +00003819 if ((len = self->readline_func(self, &s)) >= 0) {
Tim Peters4e52ca82002-12-07 02:43:28 +00003820 if (len < 2) {
3821 Py_DECREF(module_name);
3822 return bad_readline();
3823 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003824 if ((class_name = PyString_FromStringAndSize(s, len - 1))) {
Tim Peterscba30e22003-02-01 06:24:36 +00003825 class = find_class(module_name, class_name,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003826 self->find_class);
3827 Py_DECREF(class_name);
3828 }
3829 }
3830 Py_DECREF(module_name);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003831
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003832 if (! class) return -1;
3833 PDATA_PUSH(self->stack, class, -1);
3834 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003835}
3836
3837
3838static int
Tim Peterscba30e22003-02-01 06:24:36 +00003839load_persid(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003840{
3841 PyObject *pid = 0;
3842 int len;
3843 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003844
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003845 if (self->pers_func) {
Tim Peters0bc93f52003-02-02 18:29:33 +00003846 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003847 if (len < 2) return bad_readline();
3848
3849 pid = PyString_FromStringAndSize(s, len - 1);
3850 if (!pid) return -1;
3851
3852 if (PyList_Check(self->pers_func)) {
3853 if (PyList_Append(self->pers_func, pid) < 0) {
3854 Py_DECREF(pid);
3855 return -1;
3856 }
3857 }
3858 else {
3859 ARG_TUP(self, pid);
3860 if (self->arg) {
Tim Peterscba30e22003-02-01 06:24:36 +00003861 pid = PyObject_Call(self->pers_func, self->arg,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003862 NULL);
3863 FREE_ARG_TUP(self);
3864 }
3865 }
3866
3867 if (! pid) return -1;
3868
3869 PDATA_PUSH(self->stack, pid, -1);
3870 return 0;
3871 }
3872 else {
3873 PyErr_SetString(UnpicklingError,
3874 "A load persistent id instruction was encountered,\n"
3875 "but no persistent_load function was specified.");
3876 return -1;
3877 }
3878}
3879
3880static int
Tim Peterscba30e22003-02-01 06:24:36 +00003881load_binpersid(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003882{
3883 PyObject *pid = 0;
3884
3885 if (self->pers_func) {
3886 PDATA_POP(self->stack, pid);
3887 if (! pid) return -1;
3888
3889 if (PyList_Check(self->pers_func)) {
3890 if (PyList_Append(self->pers_func, pid) < 0) {
3891 Py_DECREF(pid);
3892 return -1;
3893 }
3894 }
3895 else {
3896 ARG_TUP(self, pid);
3897 if (self->arg) {
3898 pid = PyObject_Call(self->pers_func, self->arg,
3899 NULL);
3900 FREE_ARG_TUP(self);
3901 }
3902 if (! pid) return -1;
3903 }
3904
3905 PDATA_PUSH(self->stack, pid, -1);
3906 return 0;
3907 }
3908 else {
3909 PyErr_SetString(UnpicklingError,
3910 "A load persistent id instruction was encountered,\n"
3911 "but no persistent_load function was specified.");
3912 return -1;
3913 }
3914}
3915
3916
3917static int
Tim Peterscba30e22003-02-01 06:24:36 +00003918load_pop(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003919{
3920 int len;
3921
3922 if (!( (len=self->stack->length) > 0 )) return stackUnderflow();
3923
3924 /* Note that we split the (pickle.py) stack into two stacks,
3925 an object stack and a mark stack. We have to be clever and
3926 pop the right one. We do this by looking at the top of the
3927 mark stack.
3928 */
3929
3930 if ((self->num_marks > 0) &&
3931 (self->marks[self->num_marks - 1] == len))
3932 self->num_marks--;
3933 else {
3934 len--;
3935 Py_DECREF(self->stack->data[len]);
3936 self->stack->length=len;
3937 }
3938
3939 return 0;
3940}
3941
3942
3943static int
Tim Peterscba30e22003-02-01 06:24:36 +00003944load_pop_mark(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003945{
3946 int i;
3947
3948 if ((i = marker(self)) < 0)
3949 return -1;
3950
3951 Pdata_clear(self->stack, i);
3952
3953 return 0;
3954}
3955
3956
3957static int
Tim Peterscba30e22003-02-01 06:24:36 +00003958load_dup(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003959{
3960 PyObject *last;
3961 int len;
3962
3963 if ((len = self->stack->length) <= 0) return stackUnderflow();
3964 last=self->stack->data[len-1];
3965 Py_INCREF(last);
3966 PDATA_PUSH(self->stack, last, -1);
3967 return 0;
3968}
3969
3970
3971static int
Tim Peterscba30e22003-02-01 06:24:36 +00003972load_get(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003973{
3974 PyObject *py_str = 0, *value = 0;
3975 int len;
3976 char *s;
3977 int rc;
3978
Tim Peters0bc93f52003-02-02 18:29:33 +00003979 if ((len = self->readline_func(self, &s)) < 0) return -1;
Guido van Rossumf9ffb031999-02-04 14:54:04 +00003980 if (len < 2) return bad_readline();
Tim Peters84e87f32001-03-17 04:50:51 +00003981
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003982 if (!( py_str = PyString_FromStringAndSize(s, len - 1))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003983
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003984 value = PyDict_GetItem(self->memo, py_str);
3985 if (! value) {
3986 PyErr_SetObject(BadPickleGet, py_str);
3987 rc = -1;
Tim Peters92c8bb32003-02-13 23:00:26 +00003988 }
3989 else {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003990 PDATA_APPEND(self->stack, value, -1);
3991 rc = 0;
3992 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003993
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003994 Py_DECREF(py_str);
3995 return rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003996}
3997
3998
3999static int
Tim Peterscba30e22003-02-01 06:24:36 +00004000load_binget(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004001{
4002 PyObject *py_key = 0, *value = 0;
4003 unsigned char key;
4004 char *s;
4005 int rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004006
Tim Peters0bc93f52003-02-02 18:29:33 +00004007 if (self->read_func(self, &s, 1) < 0) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004008
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004009 key = (unsigned char)s[0];
4010 if (!( py_key = PyInt_FromLong((long)key))) return -1;
Guido van Rossumea2b7152000-05-09 18:14:50 +00004011
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004012 value = PyDict_GetItem(self->memo, py_key);
4013 if (! value) {
4014 PyErr_SetObject(BadPickleGet, py_key);
4015 rc = -1;
Tim Peters92c8bb32003-02-13 23:00:26 +00004016 }
4017 else {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004018 PDATA_APPEND(self->stack, value, -1);
4019 rc = 0;
4020 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00004021
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004022 Py_DECREF(py_key);
4023 return rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004024}
4025
4026
4027static int
Tim Peterscba30e22003-02-01 06:24:36 +00004028load_long_binget(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004029{
4030 PyObject *py_key = 0, *value = 0;
4031 unsigned char c;
4032 char *s;
4033 long key;
4034 int rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004035
Tim Peters0bc93f52003-02-02 18:29:33 +00004036 if (self->read_func(self, &s, 4) < 0) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004037
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004038 c = (unsigned char)s[0];
4039 key = (long)c;
4040 c = (unsigned char)s[1];
4041 key |= (long)c << 8;
4042 c = (unsigned char)s[2];
4043 key |= (long)c << 16;
4044 c = (unsigned char)s[3];
4045 key |= (long)c << 24;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004046
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004047 if (!( py_key = PyInt_FromLong((long)key))) return -1;
4048
4049 value = PyDict_GetItem(self->memo, py_key);
4050 if (! value) {
4051 PyErr_SetObject(BadPickleGet, py_key);
4052 rc = -1;
Tim Peters92c8bb32003-02-13 23:00:26 +00004053 }
4054 else {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004055 PDATA_APPEND(self->stack, value, -1);
4056 rc = 0;
4057 }
4058
4059 Py_DECREF(py_key);
4060 return rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004061}
4062
Tim Peters2d629652003-02-04 05:06:17 +00004063/* Push an object from the extension registry (EXT[124]). nbytes is
4064 * the number of bytes following the opcode, holding the index (code) value.
4065 */
4066static int
4067load_extension(Unpicklerobject *self, int nbytes)
4068{
4069 char *codebytes; /* the nbytes bytes after the opcode */
4070 long code; /* calc_binint returns long */
4071 PyObject *py_code; /* code as a Python int */
4072 PyObject *obj; /* the object to push */
4073 PyObject *pair; /* (module_name, class_name) */
4074 PyObject *module_name, *class_name;
4075
4076 assert(nbytes == 1 || nbytes == 2 || nbytes == 4);
4077 if (self->read_func(self, &codebytes, nbytes) < 0) return -1;
4078 code = calc_binint(codebytes, nbytes);
4079 if (code <= 0) { /* note that 0 is forbidden */
4080 /* Corrupt or hostile pickle. */
4081 PyErr_SetString(UnpicklingError, "EXT specifies code <= 0");
4082 return -1;
4083 }
4084
4085 /* Look for the code in the cache. */
4086 py_code = PyInt_FromLong(code);
4087 if (py_code == NULL) return -1;
4088 obj = PyDict_GetItem(extension_cache, py_code);
4089 if (obj != NULL) {
4090 /* Bingo. */
4091 Py_DECREF(py_code);
4092 PDATA_APPEND(self->stack, obj, -1);
4093 return 0;
4094 }
4095
4096 /* Look up the (module_name, class_name) pair. */
4097 pair = PyDict_GetItem(inverted_registry, py_code);
4098 if (pair == NULL) {
4099 Py_DECREF(py_code);
4100 PyErr_Format(PyExc_ValueError, "unregistered extension "
4101 "code %ld", code);
4102 return -1;
4103 }
4104 /* Since the extension registry is manipulable via Python code,
Tim Petersfa05ce32003-02-04 05:20:32 +00004105 * confirm that pair is really a 2-tuple of strings.
Tim Peters2d629652003-02-04 05:06:17 +00004106 */
4107 if (!PyTuple_Check(pair) || PyTuple_Size(pair) != 2 ||
4108 !PyString_Check(module_name = PyTuple_GET_ITEM(pair, 0)) ||
4109 !PyString_Check(class_name = PyTuple_GET_ITEM(pair, 1))) {
4110 Py_DECREF(py_code);
4111 PyErr_Format(PyExc_ValueError, "_inverted_registry[%ld] "
4112 "isn't a 2-tuple of strings", code);
4113 return -1;
4114 }
4115 /* Load the object. */
4116 obj = find_class(module_name, class_name, self->find_class);
4117 if (obj == NULL) {
4118 Py_DECREF(py_code);
4119 return -1;
4120 }
4121 /* Cache code -> obj. */
4122 code = PyDict_SetItem(extension_cache, py_code, obj);
4123 Py_DECREF(py_code);
4124 if (code < 0) {
4125 Py_DECREF(obj);
4126 return -1;
4127 }
4128 PDATA_PUSH(self->stack, obj, -1);
4129 return 0;
4130}
Guido van Rossum60456fd1997-04-09 17:36:32 +00004131
4132static int
Tim Peterscba30e22003-02-01 06:24:36 +00004133load_put(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004134{
4135 PyObject *py_str = 0, *value = 0;
4136 int len, l;
4137 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004138
Tim Peters0bc93f52003-02-02 18:29:33 +00004139 if ((l = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004140 if (l < 2) return bad_readline();
4141 if (!( len=self->stack->length )) return stackUnderflow();
4142 if (!( py_str = PyString_FromStringAndSize(s, l - 1))) return -1;
4143 value=self->stack->data[len-1];
4144 l=PyDict_SetItem(self->memo, py_str, value);
4145 Py_DECREF(py_str);
4146 return l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004147}
4148
4149
4150static int
Tim Peterscba30e22003-02-01 06:24:36 +00004151load_binput(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004152{
4153 PyObject *py_key = 0, *value = 0;
4154 unsigned char key;
4155 char *s;
4156 int len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004157
Tim Peters0bc93f52003-02-02 18:29:33 +00004158 if (self->read_func(self, &s, 1) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004159 if (!( (len=self->stack->length) > 0 )) return stackUnderflow();
Guido van Rossum60456fd1997-04-09 17:36:32 +00004160
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004161 key = (unsigned char)s[0];
Guido van Rossum053b8df1998-11-25 16:18:00 +00004162
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004163 if (!( py_key = PyInt_FromLong((long)key))) return -1;
4164 value=self->stack->data[len-1];
4165 len=PyDict_SetItem(self->memo, py_key, value);
4166 Py_DECREF(py_key);
4167 return len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004168}
4169
4170
4171static int
Tim Peterscba30e22003-02-01 06:24:36 +00004172load_long_binput(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004173{
4174 PyObject *py_key = 0, *value = 0;
4175 long key;
4176 unsigned char c;
4177 char *s;
4178 int len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004179
Tim Peters0bc93f52003-02-02 18:29:33 +00004180 if (self->read_func(self, &s, 4) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004181 if (!( len=self->stack->length )) return stackUnderflow();
Guido van Rossum60456fd1997-04-09 17:36:32 +00004182
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004183 c = (unsigned char)s[0];
4184 key = (long)c;
4185 c = (unsigned char)s[1];
4186 key |= (long)c << 8;
4187 c = (unsigned char)s[2];
4188 key |= (long)c << 16;
4189 c = (unsigned char)s[3];
4190 key |= (long)c << 24;
Tim Peters84e87f32001-03-17 04:50:51 +00004191
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004192 if (!( py_key = PyInt_FromLong(key))) return -1;
4193 value=self->stack->data[len-1];
4194 len=PyDict_SetItem(self->memo, py_key, value);
4195 Py_DECREF(py_key);
4196 return len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004197}
4198
4199
4200static int
Tim Peterscba30e22003-02-01 06:24:36 +00004201do_append(Unpicklerobject *self, int x)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004202{
4203 PyObject *value = 0, *list = 0, *append_method = 0;
4204 int len, i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004205
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004206 len=self->stack->length;
4207 if (!( len >= x && x > 0 )) return stackUnderflow();
4208 /* nothing to do */
Tim Peterscba30e22003-02-01 06:24:36 +00004209 if (len==x) return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004210
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004211 list=self->stack->data[x-1];
Guido van Rossum60456fd1997-04-09 17:36:32 +00004212
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004213 if (PyList_Check(list)) {
4214 PyObject *slice;
4215 int list_len;
Tim Peters84e87f32001-03-17 04:50:51 +00004216
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004217 slice=Pdata_popList(self->stack, x);
4218 list_len = PyList_GET_SIZE(list);
4219 i=PyList_SetSlice(list, list_len, list_len, slice);
4220 Py_DECREF(slice);
4221 return i;
4222 }
4223 else {
Guido van Rossum60456fd1997-04-09 17:36:32 +00004224
Tim Peterscba30e22003-02-01 06:24:36 +00004225 if (!( append_method = PyObject_GetAttr(list, append_str)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004226 return -1;
4227
4228 for (i = x; i < len; i++) {
4229 PyObject *junk;
4230
4231 value=self->stack->data[i];
4232 junk=0;
4233 ARG_TUP(self, value);
4234 if (self->arg) {
Tim Peterscba30e22003-02-01 06:24:36 +00004235 junk = PyObject_Call(append_method, self->arg,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004236 NULL);
4237 FREE_ARG_TUP(self);
4238 }
4239 if (! junk) {
4240 Pdata_clear(self->stack, i+1);
4241 self->stack->length=x;
4242 Py_DECREF(append_method);
4243 return -1;
4244 }
4245 Py_DECREF(junk);
4246 }
4247 self->stack->length=x;
4248 Py_DECREF(append_method);
4249 }
4250
4251 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004252}
4253
4254
4255static int
Tim Peterscba30e22003-02-01 06:24:36 +00004256load_append(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004257{
4258 return do_append(self, self->stack->length - 1);
Guido van Rossum60456fd1997-04-09 17:36:32 +00004259}
4260
4261
4262static int
Tim Peterscba30e22003-02-01 06:24:36 +00004263load_appends(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004264{
4265 return do_append(self, marker(self));
Guido van Rossum60456fd1997-04-09 17:36:32 +00004266}
4267
4268
4269static int
Tim Peterscba30e22003-02-01 06:24:36 +00004270do_setitems(Unpicklerobject *self, int x)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004271{
4272 PyObject *value = 0, *key = 0, *dict = 0;
4273 int len, i, r=0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004274
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004275 if (!( (len=self->stack->length) >= x
4276 && x > 0 )) return stackUnderflow();
Guido van Rossum60456fd1997-04-09 17:36:32 +00004277
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004278 dict=self->stack->data[x-1];
Guido van Rossum60456fd1997-04-09 17:36:32 +00004279
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004280 for (i = x+1; i < len; i += 2) {
4281 key =self->stack->data[i-1];
4282 value=self->stack->data[i ];
4283 if (PyObject_SetItem(dict, key, value) < 0) {
4284 r=-1;
4285 break;
4286 }
4287 }
4288
4289 Pdata_clear(self->stack, x);
4290
4291 return r;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004292}
4293
4294
Tim Peters84e87f32001-03-17 04:50:51 +00004295static int
Tim Peterscba30e22003-02-01 06:24:36 +00004296load_setitem(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004297{
4298 return do_setitems(self, self->stack->length - 2);
4299}
Guido van Rossum60456fd1997-04-09 17:36:32 +00004300
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004301static int
Tim Peterscba30e22003-02-01 06:24:36 +00004302load_setitems(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004303{
4304 return do_setitems(self, marker(self));
Guido van Rossum60456fd1997-04-09 17:36:32 +00004305}
4306
Tim Peters84e87f32001-03-17 04:50:51 +00004307
Guido van Rossum60456fd1997-04-09 17:36:32 +00004308static int
Tim Peterscba30e22003-02-01 06:24:36 +00004309load_build(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004310{
Tim Peters080c88b2003-02-15 03:01:11 +00004311 PyObject *state, *inst, *slotstate;
4312 PyObject *__setstate__;
4313 PyObject *d_key, *d_value;
4314 int i;
4315 int res = -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004316
Tim Peters080c88b2003-02-15 03:01:11 +00004317 /* Stack is ... instance, state. We want to leave instance at
4318 * the stack top, possibly mutated via instance.__setstate__(state).
4319 */
4320 if (self->stack->length < 2)
4321 return stackUnderflow();
4322 PDATA_POP(self->stack, state);
4323 if (state == NULL)
4324 return -1;
4325 inst = self->stack->data[self->stack->length - 1];
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004326
Tim Peters080c88b2003-02-15 03:01:11 +00004327 __setstate__ = PyObject_GetAttr(inst, __setstate___str);
4328 if (__setstate__ != NULL) {
4329 PyObject *junk = NULL;
4330
4331 /* The explicit __setstate__ is responsible for everything. */
4332 ARG_TUP(self, state);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004333 if (self->arg) {
4334 junk = PyObject_Call(__setstate__, self->arg, NULL);
4335 FREE_ARG_TUP(self);
4336 }
4337 Py_DECREF(__setstate__);
Tim Peters080c88b2003-02-15 03:01:11 +00004338 if (junk == NULL)
4339 return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004340 Py_DECREF(junk);
4341 return 0;
4342 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004343 PyErr_Clear();
Tim Peters080c88b2003-02-15 03:01:11 +00004344
4345 /* A default __setstate__. First see whether state embeds a
4346 * slot state dict too (a proto 2 addition).
4347 */
4348 if (PyTuple_Check(state) && PyTuple_Size(state) == 2) {
4349 PyObject *temp = state;
4350 state = PyTuple_GET_ITEM(temp, 0);
4351 slotstate = PyTuple_GET_ITEM(temp, 1);
4352 Py_INCREF(state);
4353 Py_INCREF(slotstate);
4354 Py_DECREF(temp);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004355 }
Tim Peters080c88b2003-02-15 03:01:11 +00004356 else
4357 slotstate = NULL;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004358
Tim Peters080c88b2003-02-15 03:01:11 +00004359 /* Set inst.__dict__ from the state dict (if any). */
4360 if (state != Py_None) {
4361 PyObject *dict;
4362 if (! PyDict_Check(state)) {
4363 PyErr_SetString(UnpicklingError, "state is not a "
4364 "dictionary");
4365 goto finally;
4366 }
4367 dict = PyObject_GetAttr(inst, __dict___str);
4368 if (dict == NULL)
4369 goto finally;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004370
Tim Peters080c88b2003-02-15 03:01:11 +00004371 i = 0;
4372 while (PyDict_Next(state, &i, &d_key, &d_value)) {
4373 if (PyObject_SetItem(dict, d_key, d_value) < 0)
4374 goto finally;
4375 }
4376 Py_DECREF(dict);
4377 }
4378
4379 /* Also set instance attributes from the slotstate dict (if any). */
4380 if (slotstate != NULL) {
4381 if (! PyDict_Check(slotstate)) {
4382 PyErr_SetString(UnpicklingError, "slot state is not "
4383 "a dictionary");
4384 goto finally;
4385 }
4386 i = 0;
4387 while (PyDict_Next(slotstate, &i, &d_key, &d_value)) {
4388 if (PyObject_SetAttr(inst, d_key, d_value) < 0)
4389 goto finally;
4390 }
4391 }
4392 res = 0;
4393
4394 finally:
4395 Py_DECREF(state);
4396 Py_XDECREF(slotstate);
4397 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004398}
4399
4400
4401static int
Tim Peterscba30e22003-02-01 06:24:36 +00004402load_mark(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004403{
4404 int s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004405
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004406 /* Note that we split the (pickle.py) stack into two stacks, an
4407 object stack and a mark stack. Here we push a mark onto the
4408 mark stack.
4409 */
Guido van Rossum60456fd1997-04-09 17:36:32 +00004410
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004411 if ((self->num_marks + 1) >= self->marks_size) {
4412 s=self->marks_size+20;
4413 if (s <= self->num_marks) s=self->num_marks + 1;
4414 if (self->marks == NULL)
4415 self->marks=(int *)malloc(s * sizeof(int));
4416 else
Tim Peterscba30e22003-02-01 06:24:36 +00004417 self->marks=(int *)realloc(self->marks,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004418 s * sizeof(int));
4419 if (! self->marks) {
4420 PyErr_NoMemory();
4421 return -1;
4422 }
4423 self->marks_size = s;
4424 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00004425
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004426 self->marks[self->num_marks++] = self->stack->length;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004427
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004428 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004429}
4430
Guido van Rossum60456fd1997-04-09 17:36:32 +00004431static int
Tim Peterscba30e22003-02-01 06:24:36 +00004432load_reduce(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004433{
4434 PyObject *callable = 0, *arg_tup = 0, *ob = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004435
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004436 PDATA_POP(self->stack, arg_tup);
4437 if (! arg_tup) return -1;
4438 PDATA_POP(self->stack, callable);
4439 if (callable) {
4440 ob = Instance_New(callable, arg_tup);
4441 Py_DECREF(callable);
4442 }
4443 Py_DECREF(arg_tup);
Guido van Rossum60456fd1997-04-09 17:36:32 +00004444
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004445 if (! ob) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004446
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004447 PDATA_PUSH(self->stack, ob, -1);
4448 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004449}
Tim Peters84e87f32001-03-17 04:50:51 +00004450
Tim Peters4190fb82003-02-02 16:09:05 +00004451/* Just raises an error if we don't know the protocol specified. PROTO
4452 * is the first opcode for protocols >= 2.
4453 */
4454static int
4455load_proto(Unpicklerobject *self)
4456{
4457 int i;
4458 char *protobyte;
4459
4460 i = self->read_func(self, &protobyte, 1);
4461 if (i < 0)
4462 return -1;
4463
4464 i = calc_binint(protobyte, 1);
4465 /* No point checking for < 0, since calc_binint returns an unsigned
4466 * int when chewing on 1 byte.
4467 */
4468 assert(i >= 0);
Tim Peters8587b3c2003-02-13 15:44:41 +00004469 if (i <= HIGHEST_PROTOCOL)
Tim Peters4190fb82003-02-02 16:09:05 +00004470 return 0;
4471
4472 PyErr_Format(PyExc_ValueError, "unsupported pickle protocol: %d", i);
4473 return -1;
4474}
4475
Guido van Rossum60456fd1997-04-09 17:36:32 +00004476static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00004477load(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004478{
4479 PyObject *err = 0, *val = 0;
4480 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004481
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004482 self->num_marks = 0;
4483 if (self->stack->length) Pdata_clear(self->stack, 0);
Guido van Rossum60456fd1997-04-09 17:36:32 +00004484
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004485 while (1) {
Tim Peters0bc93f52003-02-02 18:29:33 +00004486 if (self->read_func(self, &s, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004487 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004488
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004489 switch (s[0]) {
4490 case NONE:
4491 if (load_none(self) < 0)
4492 break;
4493 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004494
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004495 case BININT:
4496 if (load_binint(self) < 0)
4497 break;
4498 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004499
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004500 case BININT1:
4501 if (load_binint1(self) < 0)
4502 break;
4503 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004504
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004505 case BININT2:
4506 if (load_binint2(self) < 0)
4507 break;
4508 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004509
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004510 case INT:
4511 if (load_int(self) < 0)
4512 break;
4513 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004514
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004515 case LONG:
4516 if (load_long(self) < 0)
4517 break;
4518 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004519
Tim Petersee1a53c2003-02-02 02:57:53 +00004520 case LONG1:
4521 if (load_counted_long(self, 1) < 0)
4522 break;
4523 continue;
4524
4525 case LONG4:
4526 if (load_counted_long(self, 4) < 0)
4527 break;
4528 continue;
4529
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004530 case FLOAT:
4531 if (load_float(self) < 0)
4532 break;
4533 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004534
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004535 case BINFLOAT:
4536 if (load_binfloat(self) < 0)
4537 break;
4538 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004539
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004540 case BINSTRING:
4541 if (load_binstring(self) < 0)
4542 break;
4543 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004544
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004545 case SHORT_BINSTRING:
4546 if (load_short_binstring(self) < 0)
4547 break;
4548 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004549
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004550 case STRING:
4551 if (load_string(self) < 0)
4552 break;
4553 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004554
Martin v. Löwis339d0f72001-08-17 18:39:25 +00004555#ifdef Py_USING_UNICODE
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004556 case UNICODE:
4557 if (load_unicode(self) < 0)
4558 break;
4559 continue;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00004560
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004561 case BINUNICODE:
4562 if (load_binunicode(self) < 0)
4563 break;
4564 continue;
Martin v. Löwis339d0f72001-08-17 18:39:25 +00004565#endif
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00004566
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004567 case EMPTY_TUPLE:
Tim Peters1d63c9f2003-02-02 20:29:39 +00004568 if (load_counted_tuple(self, 0) < 0)
4569 break;
4570 continue;
4571
4572 case TUPLE1:
4573 if (load_counted_tuple(self, 1) < 0)
4574 break;
4575 continue;
4576
4577 case TUPLE2:
4578 if (load_counted_tuple(self, 2) < 0)
4579 break;
4580 continue;
4581
4582 case TUPLE3:
4583 if (load_counted_tuple(self, 3) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004584 break;
4585 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004586
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004587 case TUPLE:
4588 if (load_tuple(self) < 0)
4589 break;
4590 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004591
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004592 case EMPTY_LIST:
4593 if (load_empty_list(self) < 0)
4594 break;
4595 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004596
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004597 case LIST:
4598 if (load_list(self) < 0)
4599 break;
4600 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004601
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004602 case EMPTY_DICT:
4603 if (load_empty_dict(self) < 0)
4604 break;
4605 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004606
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004607 case DICT:
4608 if (load_dict(self) < 0)
4609 break;
4610 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004611
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004612 case OBJ:
4613 if (load_obj(self) < 0)
4614 break;
4615 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004616
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004617 case INST:
4618 if (load_inst(self) < 0)
4619 break;
4620 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004621
Tim Peterseab7db32003-02-13 18:24:14 +00004622 case NEWOBJ:
4623 if (load_newobj(self) < 0)
4624 break;
4625 continue;
4626
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004627 case GLOBAL:
4628 if (load_global(self) < 0)
4629 break;
4630 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004631
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004632 case APPEND:
4633 if (load_append(self) < 0)
4634 break;
4635 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004636
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004637 case APPENDS:
4638 if (load_appends(self) < 0)
4639 break;
4640 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00004641
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004642 case BUILD:
4643 if (load_build(self) < 0)
4644 break;
4645 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00004646
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004647 case DUP:
4648 if (load_dup(self) < 0)
4649 break;
4650 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004651
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004652 case BINGET:
4653 if (load_binget(self) < 0)
4654 break;
4655 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004656
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004657 case LONG_BINGET:
4658 if (load_long_binget(self) < 0)
4659 break;
4660 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00004661
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004662 case GET:
4663 if (load_get(self) < 0)
4664 break;
4665 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004666
Tim Peters2d629652003-02-04 05:06:17 +00004667 case EXT1:
4668 if (load_extension(self, 1) < 0)
4669 break;
4670 continue;
4671
4672 case EXT2:
4673 if (load_extension(self, 2) < 0)
4674 break;
4675 continue;
4676
4677 case EXT4:
4678 if (load_extension(self, 4) < 0)
4679 break;
4680 continue;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004681 case MARK:
4682 if (load_mark(self) < 0)
4683 break;
4684 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004685
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004686 case BINPUT:
4687 if (load_binput(self) < 0)
4688 break;
4689 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004690
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004691 case LONG_BINPUT:
4692 if (load_long_binput(self) < 0)
4693 break;
4694 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00004695
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004696 case PUT:
4697 if (load_put(self) < 0)
4698 break;
4699 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004700
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004701 case POP:
4702 if (load_pop(self) < 0)
4703 break;
4704 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004705
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004706 case POP_MARK:
4707 if (load_pop_mark(self) < 0)
4708 break;
4709 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004710
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004711 case SETITEM:
4712 if (load_setitem(self) < 0)
4713 break;
4714 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004715
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004716 case SETITEMS:
4717 if (load_setitems(self) < 0)
4718 break;
4719 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004720
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004721 case STOP:
4722 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004723
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004724 case PERSID:
4725 if (load_persid(self) < 0)
4726 break;
4727 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004728
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004729 case BINPERSID:
4730 if (load_binpersid(self) < 0)
4731 break;
4732 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004733
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004734 case REDUCE:
4735 if (load_reduce(self) < 0)
4736 break;
4737 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004738
Tim Peters4190fb82003-02-02 16:09:05 +00004739 case PROTO:
4740 if (load_proto(self) < 0)
4741 break;
4742 continue;
4743
Tim Peters3c67d792003-02-02 17:59:11 +00004744 case NEWTRUE:
4745 if (load_bool(self, Py_True) < 0)
4746 break;
4747 continue;
4748
4749 case NEWFALSE:
4750 if (load_bool(self, Py_False) < 0)
4751 break;
4752 continue;
4753
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004754 case '\0':
4755 /* end of file */
4756 PyErr_SetNone(PyExc_EOFError);
4757 break;
Tim Peterscba30e22003-02-01 06:24:36 +00004758
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004759 default:
Tim Peterscba30e22003-02-01 06:24:36 +00004760 cPickle_ErrFormat(UnpicklingError,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004761 "invalid load key, '%s'.",
4762 "c", s[0]);
4763 return NULL;
4764 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00004765
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004766 break;
4767 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00004768
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004769 if ((err = PyErr_Occurred())) {
4770 if (err == PyExc_EOFError) {
4771 PyErr_SetNone(PyExc_EOFError);
4772 }
4773 return NULL;
4774 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00004775
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004776 PDATA_POP(self->stack, val);
4777 return val;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004778}
Tim Peters84e87f32001-03-17 04:50:51 +00004779
Guido van Rossum60456fd1997-04-09 17:36:32 +00004780
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004781/* No-load functions to support noload, which is used to
4782 find persistent references. */
4783
4784static int
Tim Peterscba30e22003-02-01 06:24:36 +00004785noload_obj(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004786{
4787 int i;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004788
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004789 if ((i = marker(self)) < 0) return -1;
4790 return Pdata_clear(self->stack, i+1);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004791}
4792
4793
4794static int
Tim Peterscba30e22003-02-01 06:24:36 +00004795noload_inst(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004796{
4797 int i;
4798 char *s;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004799
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004800 if ((i = marker(self)) < 0) return -1;
4801 Pdata_clear(self->stack, i);
Tim Peters0bc93f52003-02-02 18:29:33 +00004802 if (self->readline_func(self, &s) < 0) return -1;
4803 if (self->readline_func(self, &s) < 0) return -1;
Tim Peterseab7db32003-02-13 18:24:14 +00004804 PDATA_APPEND(self->stack, Py_None, -1);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004805 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004806}
4807
4808static int
Tim Peterseab7db32003-02-13 18:24:14 +00004809noload_newobj(Unpicklerobject *self)
4810{
4811 PyObject *obj;
4812
4813 PDATA_POP(self->stack, obj); /* pop argtuple */
4814 if (obj == NULL) return -1;
4815 Py_DECREF(obj);
4816
4817 PDATA_POP(self->stack, obj); /* pop cls */
4818 if (obj == NULL) return -1;
4819 Py_DECREF(obj);
4820
4821 PDATA_APPEND(self->stack, Py_None, -1);
4822 return 0;
4823}
4824
4825static int
Tim Peterscba30e22003-02-01 06:24:36 +00004826noload_global(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004827{
4828 char *s;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004829
Tim Peters0bc93f52003-02-02 18:29:33 +00004830 if (self->readline_func(self, &s) < 0) return -1;
4831 if (self->readline_func(self, &s) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004832 PDATA_APPEND(self->stack, Py_None,-1);
4833 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004834}
4835
4836static int
Tim Peterscba30e22003-02-01 06:24:36 +00004837noload_reduce(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004838{
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004839
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004840 if (self->stack->length < 2) return stackUnderflow();
4841 Pdata_clear(self->stack, self->stack->length-2);
4842 PDATA_APPEND(self->stack, Py_None,-1);
4843 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004844}
4845
4846static int
4847noload_build(Unpicklerobject *self) {
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004848
Guido van Rossum053b8df1998-11-25 16:18:00 +00004849 if (self->stack->length < 1) return stackUnderflow();
4850 Pdata_clear(self->stack, self->stack->length-1);
Guido van Rossum50f385c1998-12-04 18:48:44 +00004851 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004852}
4853
Tim Peters2d629652003-02-04 05:06:17 +00004854static int
4855noload_extension(Unpicklerobject *self, int nbytes)
4856{
4857 char *codebytes;
4858
4859 assert(nbytes == 1 || nbytes == 2 || nbytes == 4);
4860 if (self->read_func(self, &codebytes, nbytes) < 0) return -1;
4861 PDATA_APPEND(self->stack, Py_None, -1);
4862 return 0;
4863}
4864
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004865
4866static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00004867noload(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004868{
4869 PyObject *err = 0, *val = 0;
4870 char *s;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004871
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004872 self->num_marks = 0;
4873 Pdata_clear(self->stack, 0);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004874
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004875 while (1) {
Tim Peters0bc93f52003-02-02 18:29:33 +00004876 if (self->read_func(self, &s, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004877 break;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004878
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004879 switch (s[0]) {
4880 case NONE:
4881 if (load_none(self) < 0)
4882 break;
4883 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004884
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004885 case BININT:
4886 if (load_binint(self) < 0)
4887 break;
4888 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004889
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004890 case BININT1:
4891 if (load_binint1(self) < 0)
4892 break;
4893 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004894
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004895 case BININT2:
4896 if (load_binint2(self) < 0)
4897 break;
4898 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004899
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004900 case INT:
4901 if (load_int(self) < 0)
4902 break;
4903 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004904
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004905 case LONG:
4906 if (load_long(self) < 0)
4907 break;
4908 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004909
Tim Peters4190fb82003-02-02 16:09:05 +00004910 case LONG1:
4911 if (load_counted_long(self, 1) < 0)
4912 break;
4913 continue;
4914
4915 case LONG4:
4916 if (load_counted_long(self, 4) < 0)
4917 break;
4918 continue;
4919
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004920 case FLOAT:
4921 if (load_float(self) < 0)
4922 break;
4923 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004924
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004925 case BINFLOAT:
4926 if (load_binfloat(self) < 0)
4927 break;
4928 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004929
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004930 case BINSTRING:
4931 if (load_binstring(self) < 0)
4932 break;
4933 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004934
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004935 case SHORT_BINSTRING:
4936 if (load_short_binstring(self) < 0)
4937 break;
4938 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004939
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004940 case STRING:
4941 if (load_string(self) < 0)
4942 break;
4943 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004944
Martin v. Löwis339d0f72001-08-17 18:39:25 +00004945#ifdef Py_USING_UNICODE
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004946 case UNICODE:
4947 if (load_unicode(self) < 0)
4948 break;
4949 continue;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00004950
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004951 case BINUNICODE:
4952 if (load_binunicode(self) < 0)
4953 break;
4954 continue;
Martin v. Löwis339d0f72001-08-17 18:39:25 +00004955#endif
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00004956
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004957 case EMPTY_TUPLE:
Tim Peters1d63c9f2003-02-02 20:29:39 +00004958 if (load_counted_tuple(self, 0) < 0)
4959 break;
4960 continue;
4961
4962 case TUPLE1:
4963 if (load_counted_tuple(self, 1) < 0)
4964 break;
4965 continue;
4966
4967 case TUPLE2:
4968 if (load_counted_tuple(self, 2) < 0)
4969 break;
4970 continue;
4971
4972 case TUPLE3:
4973 if (load_counted_tuple(self, 3) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004974 break;
4975 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004976
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004977 case TUPLE:
4978 if (load_tuple(self) < 0)
4979 break;
4980 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004981
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004982 case EMPTY_LIST:
4983 if (load_empty_list(self) < 0)
4984 break;
4985 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004986
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004987 case LIST:
4988 if (load_list(self) < 0)
4989 break;
4990 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004991
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004992 case EMPTY_DICT:
4993 if (load_empty_dict(self) < 0)
4994 break;
4995 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004996
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004997 case DICT:
4998 if (load_dict(self) < 0)
4999 break;
5000 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005001
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005002 case OBJ:
5003 if (noload_obj(self) < 0)
5004 break;
5005 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005006
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005007 case INST:
5008 if (noload_inst(self) < 0)
5009 break;
5010 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005011
Tim Peterseab7db32003-02-13 18:24:14 +00005012 case NEWOBJ:
5013 if (noload_newobj(self) < 0)
5014 break;
5015 continue;
5016
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005017 case GLOBAL:
5018 if (noload_global(self) < 0)
5019 break;
5020 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005021
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005022 case APPEND:
5023 if (load_append(self) < 0)
5024 break;
5025 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005026
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005027 case APPENDS:
5028 if (load_appends(self) < 0)
5029 break;
5030 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00005031
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005032 case BUILD:
5033 if (noload_build(self) < 0)
5034 break;
5035 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00005036
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005037 case DUP:
5038 if (load_dup(self) < 0)
5039 break;
5040 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005041
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005042 case BINGET:
5043 if (load_binget(self) < 0)
5044 break;
5045 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005046
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005047 case LONG_BINGET:
5048 if (load_long_binget(self) < 0)
5049 break;
5050 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00005051
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005052 case GET:
5053 if (load_get(self) < 0)
5054 break;
5055 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005056
Tim Peters2d629652003-02-04 05:06:17 +00005057 case EXT1:
5058 if (noload_extension(self, 1) < 0)
5059 break;
5060 continue;
5061
5062 case EXT2:
5063 if (noload_extension(self, 2) < 0)
5064 break;
5065 continue;
5066
5067 case EXT4:
5068 if (noload_extension(self, 4) < 0)
5069 break;
5070 continue;
5071
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005072 case MARK:
5073 if (load_mark(self) < 0)
5074 break;
5075 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005076
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005077 case BINPUT:
5078 if (load_binput(self) < 0)
5079 break;
5080 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005081
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005082 case LONG_BINPUT:
5083 if (load_long_binput(self) < 0)
5084 break;
5085 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00005086
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005087 case PUT:
5088 if (load_put(self) < 0)
5089 break;
5090 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005091
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005092 case POP:
5093 if (load_pop(self) < 0)
5094 break;
5095 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005096
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005097 case POP_MARK:
5098 if (load_pop_mark(self) < 0)
5099 break;
5100 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005101
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005102 case SETITEM:
5103 if (load_setitem(self) < 0)
5104 break;
5105 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005106
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005107 case SETITEMS:
5108 if (load_setitems(self) < 0)
5109 break;
5110 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005111
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005112 case STOP:
5113 break;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005114
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005115 case PERSID:
5116 if (load_persid(self) < 0)
5117 break;
5118 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005119
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005120 case BINPERSID:
5121 if (load_binpersid(self) < 0)
5122 break;
5123 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005124
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005125 case REDUCE:
5126 if (noload_reduce(self) < 0)
5127 break;
5128 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005129
Tim Peters4190fb82003-02-02 16:09:05 +00005130 case PROTO:
5131 if (load_proto(self) < 0)
5132 break;
5133 continue;
5134
Tim Peters3c67d792003-02-02 17:59:11 +00005135 case NEWTRUE:
5136 if (load_bool(self, Py_True) < 0)
5137 break;
5138 continue;
5139
5140 case NEWFALSE:
5141 if (load_bool(self, Py_False) < 0)
5142 break;
5143 continue;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005144 default:
Tim Peterscba30e22003-02-01 06:24:36 +00005145 cPickle_ErrFormat(UnpicklingError,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005146 "invalid load key, '%s'.",
5147 "c", s[0]);
5148 return NULL;
5149 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005150
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005151 break;
5152 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005153
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005154 if ((err = PyErr_Occurred())) {
5155 if (err == PyExc_EOFError) {
5156 PyErr_SetNone(PyExc_EOFError);
5157 }
5158 return NULL;
5159 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005160
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005161 PDATA_POP(self->stack, val);
5162 return val;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005163}
Tim Peters84e87f32001-03-17 04:50:51 +00005164
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005165
Guido van Rossum60456fd1997-04-09 17:36:32 +00005166static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00005167Unpickler_load(Unpicklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005168{
Tim Peterscba30e22003-02-01 06:24:36 +00005169 if (!( PyArg_ParseTuple(args, ":load")))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005170 return NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005171
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005172 return load(self);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005173}
5174
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005175static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00005176Unpickler_noload(Unpicklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005177{
Tim Peterscba30e22003-02-01 06:24:36 +00005178 if (!( PyArg_ParseTuple(args, ":noload")))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005179 return NULL;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005180
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005181 return noload(self);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005182}
5183
Guido van Rossum60456fd1997-04-09 17:36:32 +00005184
5185static struct PyMethodDef Unpickler_methods[] = {
Neal Norwitzb0493252002-03-31 14:44:22 +00005186 {"load", (PyCFunction)Unpickler_load, METH_VARARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00005187 PyDoc_STR("load() -- Load a pickle")
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005188 },
Neal Norwitzb0493252002-03-31 14:44:22 +00005189 {"noload", (PyCFunction)Unpickler_noload, METH_VARARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00005190 PyDoc_STR(
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005191 "noload() -- not load a pickle, but go through most of the motions\n"
5192 "\n"
5193 "This function can be used to read past a pickle without instantiating\n"
5194 "any objects or importing any modules. It can also be used to find all\n"
5195 "persistent references without instantiating any objects or importing\n"
Neal Norwitz200788c2002-08-13 22:20:41 +00005196 "any modules.\n")
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005197 },
Guido van Rossum60456fd1997-04-09 17:36:32 +00005198 {NULL, NULL} /* sentinel */
5199};
5200
5201
5202static Unpicklerobject *
Tim Peterscba30e22003-02-01 06:24:36 +00005203newUnpicklerobject(PyObject *f)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005204{
5205 Unpicklerobject *self;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005206
Tim Peterscba30e22003-02-01 06:24:36 +00005207 if (!( self = PyObject_New(Unpicklerobject, &Unpicklertype)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005208 return NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005209
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005210 self->file = NULL;
5211 self->arg = NULL;
5212 self->stack = (Pdata*)Pdata_New();
5213 self->pers_func = NULL;
5214 self->last_string = NULL;
5215 self->marks = NULL;
5216 self->num_marks = 0;
5217 self->marks_size = 0;
5218 self->buf_size = 0;
5219 self->read = NULL;
5220 self->readline = NULL;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005221 self->find_class = NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005222
Tim Peterscba30e22003-02-01 06:24:36 +00005223 if (!( self->memo = PyDict_New()))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005224 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005225
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005226 Py_INCREF(f);
5227 self->file = f;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005228
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005229 /* Set read, readline based on type of f */
5230 if (PyFile_Check(f)) {
5231 self->fp = PyFile_AsFile(f);
5232 if (self->fp == NULL) {
Tim Peterscba30e22003-02-01 06:24:36 +00005233 PyErr_SetString(PyExc_ValueError,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005234 "I/O operation on closed file");
5235 goto err;
5236 }
5237 self->read_func = read_file;
5238 self->readline_func = readline_file;
Guido van Rossumc91fcaa1999-03-29 20:00:14 +00005239 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005240 else if (PycStringIO_InputCheck(f)) {
5241 self->fp = NULL;
5242 self->read_func = read_cStringIO;
5243 self->readline_func = readline_cStringIO;
5244 }
5245 else {
Guido van Rossum60456fd1997-04-09 17:36:32 +00005246
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005247 self->fp = NULL;
5248 self->read_func = read_other;
5249 self->readline_func = readline_other;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005250
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005251 if (!( (self->readline = PyObject_GetAttr(f, readline_str)) &&
5252 (self->read = PyObject_GetAttr(f, read_str)))) {
5253 PyErr_Clear();
Tim Peterscba30e22003-02-01 06:24:36 +00005254 PyErr_SetString( PyExc_TypeError,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005255 "argument must have 'read' and "
5256 "'readline' attributes" );
5257 goto err;
5258 }
5259 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00005260
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005261 return self;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005262
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005263 err:
5264 Py_DECREF((PyObject *)self);
5265 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005266}
5267
5268
5269static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00005270get_Unpickler(PyObject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005271{
5272 PyObject *file;
Tim Peters84e87f32001-03-17 04:50:51 +00005273
Tim Peterscba30e22003-02-01 06:24:36 +00005274 if (!( PyArg_ParseTuple(args, "O:Unpickler", &file)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005275 return NULL;
5276 return (PyObject *)newUnpicklerobject(file);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005277}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005278
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005279
Guido van Rossum60456fd1997-04-09 17:36:32 +00005280static void
Tim Peterscba30e22003-02-01 06:24:36 +00005281Unpickler_dealloc(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005282{
5283 Py_XDECREF(self->readline);
5284 Py_XDECREF(self->read);
5285 Py_XDECREF(self->file);
5286 Py_XDECREF(self->memo);
5287 Py_XDECREF(self->stack);
5288 Py_XDECREF(self->pers_func);
5289 Py_XDECREF(self->arg);
5290 Py_XDECREF(self->last_string);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005291
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005292 if (self->marks) {
5293 free(self->marks);
5294 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005295
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005296 if (self->buf_size) {
5297 free(self->buf);
5298 }
Tim Peters84e87f32001-03-17 04:50:51 +00005299
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005300 PyObject_Del(self);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005301}
5302
5303
5304static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00005305Unpickler_getattr(Unpicklerobject *self, char *name)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005306{
5307 if (!strcmp(name, "persistent_load")) {
5308 if (!self->pers_func) {
5309 PyErr_SetString(PyExc_AttributeError, name);
5310 return NULL;
5311 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005312
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005313 Py_INCREF(self->pers_func);
5314 return self->pers_func;
5315 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005316
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005317 if (!strcmp(name, "find_global")) {
5318 if (!self->find_class) {
5319 PyErr_SetString(PyExc_AttributeError, name);
5320 return NULL;
5321 }
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00005322
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005323 Py_INCREF(self->find_class);
5324 return self->find_class;
5325 }
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00005326
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005327 if (!strcmp(name, "memo")) {
5328 if (!self->memo) {
5329 PyErr_SetString(PyExc_AttributeError, name);
5330 return NULL;
5331 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005332
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005333 Py_INCREF(self->memo);
5334 return self->memo;
5335 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005336
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005337 if (!strcmp(name, "UnpicklingError")) {
5338 Py_INCREF(UnpicklingError);
5339 return UnpicklingError;
5340 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005341
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005342 return Py_FindMethod(Unpickler_methods, (PyObject *)self, name);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005343}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005344
Guido van Rossum60456fd1997-04-09 17:36:32 +00005345
5346static int
Tim Peterscba30e22003-02-01 06:24:36 +00005347Unpickler_setattr(Unpicklerobject *self, char *name, PyObject *value)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005348{
Jeremy Hyltonce616e41998-08-13 23:13:52 +00005349
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005350 if (!strcmp(name, "persistent_load")) {
5351 Py_XDECREF(self->pers_func);
5352 self->pers_func = value;
5353 Py_XINCREF(value);
5354 return 0;
5355 }
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00005356
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005357 if (!strcmp(name, "find_global")) {
5358 Py_XDECREF(self->find_class);
5359 self->find_class = value;
5360 Py_XINCREF(value);
5361 return 0;
5362 }
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00005363
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005364 if (! value) {
5365 PyErr_SetString(PyExc_TypeError,
5366 "attribute deletion is not supported");
5367 return -1;
5368 }
Jeremy Hyltonce616e41998-08-13 23:13:52 +00005369
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005370 if (strcmp(name, "memo") == 0) {
5371 if (!PyDict_Check(value)) {
Tim Peterscba30e22003-02-01 06:24:36 +00005372 PyErr_SetString(PyExc_TypeError,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005373 "memo must be a dictionary");
5374 return -1;
5375 }
5376 Py_XDECREF(self->memo);
5377 self->memo = value;
5378 Py_INCREF(value);
5379 return 0;
5380 }
Jeremy Hyltonce616e41998-08-13 23:13:52 +00005381
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005382 PyErr_SetString(PyExc_AttributeError, name);
5383 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005384}
5385
Tim Peters5bd2a792003-02-01 16:45:06 +00005386/* ---------------------------------------------------------------------------
5387 * Module-level functions.
5388 */
Guido van Rossum60456fd1997-04-09 17:36:32 +00005389
Tim Peters5bd2a792003-02-01 16:45:06 +00005390/* dump(obj, file, proto=0). */
Guido van Rossum60456fd1997-04-09 17:36:32 +00005391static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00005392cpm_dump(PyObject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005393{
5394 PyObject *ob, *file, *res = NULL;
5395 Picklerobject *pickler = 0;
Tim Peters5bd2a792003-02-01 16:45:06 +00005396 int proto = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005397
Tim Peters5bd2a792003-02-01 16:45:06 +00005398 if (!( PyArg_ParseTuple(args, "OO|i", &ob, &file, &proto)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005399 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005400
Tim Peters5bd2a792003-02-01 16:45:06 +00005401 if (!( pickler = newPicklerobject(file, proto)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005402 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005403
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005404 if (dump(pickler, ob) < 0)
5405 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005406
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005407 Py_INCREF(Py_None);
5408 res = Py_None;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005409
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005410 finally:
5411 Py_XDECREF(pickler);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005412
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005413 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005414}
5415
5416
Tim Peters5bd2a792003-02-01 16:45:06 +00005417/* dumps(obj, proto=0). */
Guido van Rossum60456fd1997-04-09 17:36:32 +00005418static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00005419cpm_dumps(PyObject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005420{
5421 PyObject *ob, *file = 0, *res = NULL;
5422 Picklerobject *pickler = 0;
Tim Peters5bd2a792003-02-01 16:45:06 +00005423 int proto = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005424
Tim Peters5bd2a792003-02-01 16:45:06 +00005425 if (!( PyArg_ParseTuple(args, "O|i:dumps", &ob, &proto)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005426 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005427
Tim Peterscba30e22003-02-01 06:24:36 +00005428 if (!( file = PycStringIO->NewOutput(128)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005429 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005430
Tim Peters5bd2a792003-02-01 16:45:06 +00005431 if (!( pickler = newPicklerobject(file, proto)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005432 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005433
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005434 if (dump(pickler, ob) < 0)
5435 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005436
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005437 res = PycStringIO->cgetvalue(file);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005438
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005439 finally:
5440 Py_XDECREF(pickler);
5441 Py_XDECREF(file);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005442
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005443 return res;
Tim Peters84e87f32001-03-17 04:50:51 +00005444}
5445
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005446
Tim Peters5bd2a792003-02-01 16:45:06 +00005447/* load(fileobj). */
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005448static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00005449cpm_load(PyObject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005450{
5451 Unpicklerobject *unpickler = 0;
5452 PyObject *ob, *res = NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005453
Tim Peterscba30e22003-02-01 06:24:36 +00005454 if (!( PyArg_ParseTuple(args, "O:load", &ob)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005455 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005456
Tim Peterscba30e22003-02-01 06:24:36 +00005457 if (!( unpickler = newUnpicklerobject(ob)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005458 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005459
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005460 res = load(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005461
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005462 finally:
5463 Py_XDECREF(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005464
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005465 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005466}
5467
5468
Tim Peters5bd2a792003-02-01 16:45:06 +00005469/* loads(string) */
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005470static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00005471cpm_loads(PyObject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005472{
5473 PyObject *ob, *file = 0, *res = NULL;
5474 Unpicklerobject *unpickler = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005475
Tim Peterscba30e22003-02-01 06:24:36 +00005476 if (!( PyArg_ParseTuple(args, "S:loads", &ob)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005477 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005478
Tim Peterscba30e22003-02-01 06:24:36 +00005479 if (!( file = PycStringIO->NewInput(ob)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005480 goto finally;
Tim Peters84e87f32001-03-17 04:50:51 +00005481
Tim Peterscba30e22003-02-01 06:24:36 +00005482 if (!( unpickler = newUnpicklerobject(file)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005483 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005484
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005485 res = load(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005486
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005487 finally:
5488 Py_XDECREF(file);
5489 Py_XDECREF(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005490
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005491 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005492}
5493
5494
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005495PyDoc_STRVAR(Unpicklertype__doc__,
5496"Objects that know how to unpickle");
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005497
Guido van Rossum9716aaa1997-12-08 15:15:16 +00005498static PyTypeObject Unpicklertype = {
5499 PyObject_HEAD_INIT(NULL)
Guido van Rossum60456fd1997-04-09 17:36:32 +00005500 0, /*ob_size*/
Guido van Rossum14648392001-12-08 18:02:58 +00005501 "cPickle.Unpickler", /*tp_name*/
Guido van Rossum60456fd1997-04-09 17:36:32 +00005502 sizeof(Unpicklerobject), /*tp_basicsize*/
5503 0, /*tp_itemsize*/
5504 /* methods */
5505 (destructor)Unpickler_dealloc, /*tp_dealloc*/
5506 (printfunc)0, /*tp_print*/
5507 (getattrfunc)Unpickler_getattr, /*tp_getattr*/
5508 (setattrfunc)Unpickler_setattr, /*tp_setattr*/
5509 (cmpfunc)0, /*tp_compare*/
5510 (reprfunc)0, /*tp_repr*/
5511 0, /*tp_as_number*/
5512 0, /*tp_as_sequence*/
5513 0, /*tp_as_mapping*/
5514 (hashfunc)0, /*tp_hash*/
5515 (ternaryfunc)0, /*tp_call*/
5516 (reprfunc)0, /*tp_str*/
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005517
Guido van Rossum60456fd1997-04-09 17:36:32 +00005518 /* Space for future expansion */
5519 0L,0L,0L,0L,
5520 Unpicklertype__doc__ /* Documentation string */
Guido van Rossum9716aaa1997-12-08 15:15:16 +00005521};
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005522
Guido van Rossum60456fd1997-04-09 17:36:32 +00005523static struct PyMethodDef cPickle_methods[] = {
Neal Norwitzb0493252002-03-31 14:44:22 +00005524 {"dump", (PyCFunction)cpm_dump, METH_VARARGS,
Tim Peters5bd2a792003-02-01 16:45:06 +00005525 PyDoc_STR("dump(object, file, proto=0) -- "
5526 "Write an object in pickle format to the given file.\n"
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005527 "\n"
Tim Peters5bd2a792003-02-01 16:45:06 +00005528 "See the Pickler docstring for the meaning of optional argument proto.")
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005529 },
Tim Peters5bd2a792003-02-01 16:45:06 +00005530
Neal Norwitzb0493252002-03-31 14:44:22 +00005531 {"dumps", (PyCFunction)cpm_dumps, METH_VARARGS,
Tim Peters5bd2a792003-02-01 16:45:06 +00005532 PyDoc_STR("dumps(object, proto=0) -- "
5533 "Return a string containing an object in pickle format.\n"
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005534 "\n"
Tim Peters5bd2a792003-02-01 16:45:06 +00005535 "See the Pickler docstring for the meaning of optional argument proto.")
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005536 },
Tim Peters5bd2a792003-02-01 16:45:06 +00005537
Neal Norwitzb0493252002-03-31 14:44:22 +00005538 {"load", (PyCFunction)cpm_load, METH_VARARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00005539 PyDoc_STR("load(file) -- Load a pickle from the given file")},
Tim Peters5bd2a792003-02-01 16:45:06 +00005540
Neal Norwitzb0493252002-03-31 14:44:22 +00005541 {"loads", (PyCFunction)cpm_loads, METH_VARARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00005542 PyDoc_STR("loads(string) -- Load a pickle from the given string")},
Tim Peters5bd2a792003-02-01 16:45:06 +00005543
Neal Norwitzb0493252002-03-31 14:44:22 +00005544 {"Pickler", (PyCFunction)get_Pickler, METH_VARARGS,
Tim Peters5bd2a792003-02-01 16:45:06 +00005545 PyDoc_STR("Pickler(file, proto=0) -- Create a pickler.\n"
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005546 "\n"
Tim Peters5bd2a792003-02-01 16:45:06 +00005547 "This takes a file-like object for writing a pickle data stream.\n"
5548 "The optional proto argument tells the pickler to use the given\n"
5549 "protocol; supported protocols are 0, 1, 2. The default\n"
5550 "protocol is 0, to be backwards compatible. (Protocol 0 is the\n"
5551 "only protocol that can be written to a file opened in text\n"
5552 "mode and read back successfully. When using a protocol higher\n"
5553 "than 0, make sure the file is opened in binary mode, both when\n"
5554 "pickling and unpickling.)\n"
5555 "\n"
5556 "Protocol 1 is more efficient than protocol 0; protocol 2 is\n"
5557 "more efficient than protocol 1.\n"
5558 "\n"
5559 "Specifying a negative protocol version selects the highest\n"
5560 "protocol version supported. The higher the protocol used, the\n"
5561 "more recent the version of Python needed to read the pickle\n"
5562 "produced.\n"
5563 "\n"
5564 "The file parameter must have a write() method that accepts a single\n"
5565 "string argument. It can thus be an open file object, a StringIO\n"
5566 "object, or any other custom object that meets this interface.\n")
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005567 },
Tim Peters5bd2a792003-02-01 16:45:06 +00005568
Neal Norwitzb0493252002-03-31 14:44:22 +00005569 {"Unpickler", (PyCFunction)get_Unpickler, METH_VARARGS,
Tim Peters5bd2a792003-02-01 16:45:06 +00005570 PyDoc_STR("Unpickler(file) -- Create an unpickler.")},
5571
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005572 { NULL, NULL }
5573};
5574
Guido van Rossum60456fd1997-04-09 17:36:32 +00005575static int
Tim Peterscba30e22003-02-01 06:24:36 +00005576init_stuff(PyObject *module_dict)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005577{
5578 PyObject *copy_reg, *t, *r;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005579
Martin v. Löwis43c9d8a2002-04-01 12:34:33 +00005580#define INIT_STR(S) if (!( S ## _str=PyString_InternFromString(#S))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005581
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005582 INIT_STR(__class__);
5583 INIT_STR(__getinitargs__);
5584 INIT_STR(__dict__);
5585 INIT_STR(__getstate__);
5586 INIT_STR(__setstate__);
5587 INIT_STR(__name__);
5588 INIT_STR(__main__);
5589 INIT_STR(__reduce__);
Guido van Rossumb289b872003-02-19 01:45:13 +00005590 INIT_STR(__reduce_ex__);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005591 INIT_STR(write);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005592 INIT_STR(append);
5593 INIT_STR(read);
5594 INIT_STR(readline);
5595 INIT_STR(copy_reg);
5596 INIT_STR(dispatch_table);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005597 INIT_STR(__basicnew__);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005598
Tim Peterscba30e22003-02-01 06:24:36 +00005599 if (!( copy_reg = PyImport_ImportModule("copy_reg")))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005600 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005601
Tim Peters1f1b2d22003-02-01 02:16:37 +00005602 /* This is special because we want to use a different
5603 one in restricted mode. */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005604 dispatch_table = PyObject_GetAttr(copy_reg, dispatch_table_str);
Tim Peters5b7da392003-02-04 00:21:07 +00005605 if (!dispatch_table) return -1;
5606
5607 extension_registry = PyObject_GetAttrString(copy_reg,
Guido van Rossumd4b920c2003-02-04 01:54:49 +00005608 "_extension_registry");
Tim Peters5b7da392003-02-04 00:21:07 +00005609 if (!extension_registry) return -1;
5610
5611 inverted_registry = PyObject_GetAttrString(copy_reg,
Guido van Rossumd4b920c2003-02-04 01:54:49 +00005612 "_inverted_registry");
Tim Peters5b7da392003-02-04 00:21:07 +00005613 if (!inverted_registry) return -1;
5614
5615 extension_cache = PyObject_GetAttrString(copy_reg,
Guido van Rossumd4b920c2003-02-04 01:54:49 +00005616 "_extension_cache");
Tim Peters5b7da392003-02-04 00:21:07 +00005617 if (!extension_cache) return -1;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005618
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005619 Py_DECREF(copy_reg);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005620
Tim Peters731098b2003-02-04 20:56:09 +00005621 if (!(empty_tuple = PyTuple_New(0)))
5622 return -1;
5623
5624 two_tuple = PyTuple_New(2);
5625 if (two_tuple == NULL)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005626 return -1;
Tim Peters3e667d52003-02-04 21:47:44 +00005627 /* We use this temp container with no regard to refcounts, or to
5628 * keeping containees alive. Exempt from GC, because we don't
5629 * want anything looking at two_tuple() by magic.
5630 */
5631 PyObject_GC_UnTrack(two_tuple);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005632
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005633 /* Ugh */
5634 if (!( t=PyImport_ImportModule("__builtin__"))) return -1;
5635 if (PyDict_SetItemString(module_dict, "__builtins__", t) < 0)
5636 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005637
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005638 if (!( t=PyDict_New())) return -1;
5639 if (!( r=PyRun_String(
5640 "def __init__(self, *args): self.args=args\n\n"
5641 "def __str__(self):\n"
5642 " return self.args and ('%s' % self.args[0]) or '(what)'\n",
5643 Py_file_input,
5644 module_dict, t) )) return -1;
5645 Py_DECREF(r);
Guido van Rossumc03158b1999-06-09 15:23:31 +00005646
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005647 PickleError = PyErr_NewException("cPickle.PickleError", NULL, t);
Tim Peterscba30e22003-02-01 06:24:36 +00005648 if (!PickleError)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005649 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00005650
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005651 Py_DECREF(t);
Guido van Rossumc03158b1999-06-09 15:23:31 +00005652
Tim Peterscba30e22003-02-01 06:24:36 +00005653 PicklingError = PyErr_NewException("cPickle.PicklingError",
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005654 PickleError, NULL);
Tim Peterscba30e22003-02-01 06:24:36 +00005655 if (!PicklingError)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005656 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00005657
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005658 if (!( t=PyDict_New())) return -1;
5659 if (!( r=PyRun_String(
5660 "def __init__(self, *args): self.args=args\n\n"
5661 "def __str__(self):\n"
5662 " a=self.args\n"
5663 " a=a and type(a[0]) or '(what)'\n"
5664 " return 'Cannot pickle %s objects' % a\n"
5665 , Py_file_input,
5666 module_dict, t) )) return -1;
5667 Py_DECREF(r);
Tim Peters84e87f32001-03-17 04:50:51 +00005668
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005669 if (!( UnpickleableError = PyErr_NewException(
Tim Peterscba30e22003-02-01 06:24:36 +00005670 "cPickle.UnpickleableError", PicklingError, t)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005671 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00005672
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005673 Py_DECREF(t);
Guido van Rossumc03158b1999-06-09 15:23:31 +00005674
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005675 if (!( UnpicklingError = PyErr_NewException("cPickle.UnpicklingError",
Tim Peterscba30e22003-02-01 06:24:36 +00005676 PickleError, NULL)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005677 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00005678
Martin v. Löwis658009a2002-09-16 17:26:24 +00005679 if (!( BadPickleGet = PyErr_NewException("cPickle.BadPickleGet",
5680 UnpicklingError, NULL)))
5681 return -1;
Tim Peterscba30e22003-02-01 06:24:36 +00005682
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005683 if (PyDict_SetItemString(module_dict, "PickleError",
5684 PickleError) < 0)
5685 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00005686
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005687 if (PyDict_SetItemString(module_dict, "PicklingError",
5688 PicklingError) < 0)
5689 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00005690
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005691 if (PyDict_SetItemString(module_dict, "UnpicklingError",
5692 UnpicklingError) < 0)
5693 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005694
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005695 if (PyDict_SetItemString(module_dict, "UnpickleableError",
5696 UnpickleableError) < 0)
5697 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005698
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005699 if (PyDict_SetItemString(module_dict, "BadPickleGet",
5700 BadPickleGet) < 0)
5701 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00005702
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005703 PycString_IMPORT;
Guido van Rossum053b8df1998-11-25 16:18:00 +00005704
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005705 return 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005706}
5707
Mark Hammondfe51c6d2002-08-02 02:27:13 +00005708#ifndef PyMODINIT_FUNC /* declarations for DLL import/export */
5709#define PyMODINIT_FUNC void
Guido van Rossumf9ffb031999-02-04 14:54:04 +00005710#endif
Mark Hammondfe51c6d2002-08-02 02:27:13 +00005711PyMODINIT_FUNC
Tim Peterscba30e22003-02-01 06:24:36 +00005712initcPickle(void)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005713{
5714 PyObject *m, *d, *di, *v, *k;
5715 int i;
Tim Peters5b7da392003-02-04 00:21:07 +00005716 char *rev = "1.71"; /* XXX when does this change? */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005717 PyObject *format_version;
5718 PyObject *compatible_formats;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005719
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005720 Picklertype.ob_type = &PyType_Type;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005721 Unpicklertype.ob_type = &PyType_Type;
5722 PdataType.ob_type = &PyType_Type;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005723
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005724 /* Initialize some pieces. We need to do this before module creation,
Tim Peters5b7da392003-02-04 00:21:07 +00005725 * so we're forced to use a temporary dictionary. :(
5726 */
5727 di = PyDict_New();
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005728 if (!di) return;
5729 if (init_stuff(di) < 0) return;
Guido van Rossumebba4202000-09-07 14:35:37 +00005730
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005731 /* Create the module and add the functions */
5732 m = Py_InitModule4("cPickle", cPickle_methods,
5733 cPickle_module_documentation,
5734 (PyObject*)NULL,PYTHON_API_VERSION);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005735
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005736 /* Add some symbolic constants to the module */
5737 d = PyModule_GetDict(m);
Tim Peters5b7da392003-02-04 00:21:07 +00005738 v = PyString_FromString(rev);
5739 PyDict_SetItemString(d, "__version__", v);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005740 Py_XDECREF(v);
Barry Warsaw93d29b61997-01-14 17:45:08 +00005741
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005742 /* Copy data from di. Waaa. */
5743 for (i=0; PyDict_Next(di, &i, &k, &v); ) {
5744 if (PyObject_SetItem(d, k, v) < 0) {
5745 Py_DECREF(di);
5746 return;
5747 }
5748 }
5749 Py_DECREF(di);
Guido van Rossumebba4202000-09-07 14:35:37 +00005750
Tim Peters8587b3c2003-02-13 15:44:41 +00005751 i = PyModule_AddIntConstant(m, "HIGHEST_PROTOCOL", HIGHEST_PROTOCOL);
5752 if (i < 0)
5753 return;
5754
Tim Peters5b7da392003-02-04 00:21:07 +00005755 /* These are purely informational; no code uses them. */
5756 /* File format version we write. */
5757 format_version = PyString_FromString("2.0");
5758 /* Format versions we can read. */
5759 compatible_formats = Py_BuildValue("[sssss]",
5760 "1.0", /* Original protocol 0 */
5761 "1.1", /* Protocol 0 + INST */
5762 "1.2", /* Original protocol 1 */
5763 "1.3", /* Protocol 1 + BINFLOAT */
Tim Peters0dd23aa2003-02-04 00:30:46 +00005764 "2.0"); /* Original protocol 2 */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005765 PyDict_SetItemString(d, "format_version", format_version);
5766 PyDict_SetItemString(d, "compatible_formats", compatible_formats);
5767 Py_XDECREF(format_version);
5768 Py_XDECREF(compatible_formats);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005769}