blob: fabc3cde6db8eb96eeb46127aed3a0e8dea12020 [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,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000126 *read_str, *readline_str, *__main___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++) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000199 Py_CLEAR(*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;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000211 PyObject **tmp;
Tim Peters1d63c9f2003-02-02 20:29:39 +0000212
Tim Peters1d63c9f2003-02-02 20:29:39 +0000213 bigger = self->size << 1;
Tim Peterse0a39072003-02-03 15:45:56 +0000214 if (bigger <= 0) /* was 0, or new value overflows */
Tim Peters1d63c9f2003-02-02 20:29:39 +0000215 goto nomemory;
216 if ((int)(size_t)bigger != bigger)
217 goto nomemory;
218 nbytes = (size_t)bigger * sizeof(PyObject *);
219 if (nbytes / sizeof(PyObject *) != (size_t)bigger)
220 goto nomemory;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000221 tmp = realloc(self->data, nbytes);
222 if (tmp == NULL)
Tim Peters1d63c9f2003-02-02 20:29:39 +0000223 goto nomemory;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000224 self->data = tmp;
Tim Peters1d63c9f2003-02-02 20:29:39 +0000225 self->size = bigger;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000226 return 0;
Tim Peters1d63c9f2003-02-02 20:29:39 +0000227
228 nomemory:
Tim Peters1d63c9f2003-02-02 20:29:39 +0000229 PyErr_NoMemory();
230 return -1;
Tim Peters84e87f32001-03-17 04:50:51 +0000231}
Guido van Rossum053b8df1998-11-25 16:18:00 +0000232
Tim Peterse0a39072003-02-03 15:45:56 +0000233/* D is a Pdata*. Pop the topmost element and store it into V, which
234 * must be an lvalue holding PyObject*. On stack underflow, UnpicklingError
Tim Peters1d63c9f2003-02-02 20:29:39 +0000235 * is raised and V is set to NULL. D and V may be evaluated several times.
236 */
237#define PDATA_POP(D, V) { \
Tim Peterse0a39072003-02-03 15:45:56 +0000238 if ((D)->length) \
239 (V) = (D)->data[--((D)->length)]; \
240 else { \
241 PyErr_SetString(UnpicklingError, "bad pickle data"); \
242 (V) = NULL; \
243 } \
Guido van Rossum053b8df1998-11-25 16:18:00 +0000244}
245
Tim Peterse0a39072003-02-03 15:45:56 +0000246/* PDATA_PUSH and PDATA_APPEND both push rvalue PyObject* O on to Pdata*
247 * D. If the Pdata stack can't be grown to hold the new value, both
248 * raise MemoryError and execute "return ER". The difference is in ownership
249 * of O after: _PUSH transfers ownership of O from the caller to the stack
250 * (no incref of O is done, and in case of error O is decrefed), while
251 * _APPEND pushes a new reference.
252 */
253
254/* Push O on stack D, giving ownership of O to the stack. */
255#define PDATA_PUSH(D, O, ER) { \
256 if (((Pdata*)(D))->length == ((Pdata*)(D))->size && \
257 Pdata_grow((Pdata*)(D)) < 0) { \
258 Py_DECREF(O); \
259 return ER; \
260 } \
261 ((Pdata*)(D))->data[((Pdata*)(D))->length++] = (O); \
262}
263
264/* Push O on stack D, pushing a new reference. */
265#define PDATA_APPEND(D, O, ER) { \
266 if (((Pdata*)(D))->length == ((Pdata*)(D))->size && \
267 Pdata_grow((Pdata*)(D)) < 0) \
268 return ER; \
269 Py_INCREF(O); \
270 ((Pdata*)(D))->data[((Pdata*)(D))->length++] = (O); \
271}
272
273
Guido van Rossum053b8df1998-11-25 16:18:00 +0000274static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +0000275Pdata_popTuple(Pdata *self, int start)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000276{
277 PyObject *r;
278 int i, j, l;
Tim Peterscba30e22003-02-01 06:24:36 +0000279
Tim Peters1d63c9f2003-02-02 20:29:39 +0000280 l = self->length-start;
281 r = PyTuple_New(l);
282 if (r == NULL)
283 return NULL;
284 for (i = start, j = 0 ; j < l; i++, j++)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000285 PyTuple_SET_ITEM(r, j, self->data[i]);
Tim Peterscba30e22003-02-01 06:24:36 +0000286
Tim Peters1d63c9f2003-02-02 20:29:39 +0000287 self->length = start;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000288 return r;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000289}
290
291static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +0000292Pdata_popList(Pdata *self, int start)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000293{
294 PyObject *r;
295 int i, j, l;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000296
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000297 l=self->length-start;
298 if (!( r=PyList_New(l))) return NULL;
299 for (i=start, j=0 ; j < l; i++, j++)
300 PyList_SET_ITEM(r, j, self->data[i]);
Guido van Rossum053b8df1998-11-25 16:18:00 +0000301
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000302 self->length=start;
303 return r;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000304}
305
Guido van Rossum053b8df1998-11-25 16:18:00 +0000306/*************************************************************************/
307
308#define ARG_TUP(self, o) { \
309 if (self->arg || (self->arg=PyTuple_New(1))) { \
310 Py_XDECREF(PyTuple_GET_ITEM(self->arg,0)); \
311 PyTuple_SET_ITEM(self->arg,0,o); \
312 } \
313 else { \
314 Py_DECREF(o); \
315 } \
316}
317
318#define FREE_ARG_TUP(self) { \
319 if (self->arg->ob_refcnt > 1) { \
320 Py_DECREF(self->arg); \
321 self->arg=NULL; \
322 } \
323 }
324
Thomas Wouters3b6448f2000-07-22 23:56:07 +0000325typedef struct Picklerobject {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000326 PyObject_HEAD
327 FILE *fp;
328 PyObject *write;
329 PyObject *file;
330 PyObject *memo;
331 PyObject *arg;
332 PyObject *pers_func;
333 PyObject *inst_pers_func;
Tim Peters797ec242003-02-01 06:22:36 +0000334
335 /* pickle protocol number, >= 0 */
336 int proto;
337
338 /* bool, true if proto > 0 */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000339 int bin;
Tim Peters797ec242003-02-01 06:22:36 +0000340
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000341 int fast; /* Fast mode doesn't save in memo, don't use if circ ref */
Martin v. Löwis5a395302002-08-04 08:20:23 +0000342 int nesting;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000343 int (*write_func)(struct Picklerobject *, const char *, Py_ssize_t);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000344 char *write_buf;
345 int buf_size;
346 PyObject *dispatch_table;
347 int fast_container; /* count nested container dumps */
348 PyObject *fast_memo;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000349} Picklerobject;
350
Barry Warsaw52acb492001-12-21 20:04:22 +0000351#ifndef PY_CPICKLE_FAST_LIMIT
352#define PY_CPICKLE_FAST_LIMIT 50
353#endif
Jeremy Hyltona0fb1772001-10-12 04:11:06 +0000354
Jeremy Hylton938ace62002-07-17 16:30:39 +0000355static PyTypeObject Picklertype;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000356
Thomas Wouters3b6448f2000-07-22 23:56:07 +0000357typedef struct Unpicklerobject {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000358 PyObject_HEAD
359 FILE *fp;
360 PyObject *file;
361 PyObject *readline;
362 PyObject *read;
363 PyObject *memo;
364 PyObject *arg;
365 Pdata *stack;
366 PyObject *mark;
367 PyObject *pers_func;
368 PyObject *last_string;
369 int *marks;
370 int num_marks;
371 int marks_size;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000372 Py_ssize_t (*read_func)(struct Unpicklerobject *, char **, Py_ssize_t);
373 Py_ssize_t (*readline_func)(struct Unpicklerobject *, char **);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000374 int buf_size;
375 char *buf;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000376 PyObject *find_class;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000377} Unpicklerobject;
Tim Peters84e87f32001-03-17 04:50:51 +0000378
Jeremy Hylton938ace62002-07-17 16:30:39 +0000379static PyTypeObject Unpicklertype;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000380
Thomas Wouters4789b3a2000-07-24 11:36:47 +0000381/* Forward decls that need the above structs */
382static int save(Picklerobject *, PyObject *, int);
383static int put2(Picklerobject *, PyObject *);
384
Guido van Rossumd385d591997-04-09 17:47:47 +0000385static
Guido van Rossum60456fd1997-04-09 17:36:32 +0000386PyObject *
Thomas Wouters3b6448f2000-07-22 23:56:07 +0000387cPickle_ErrFormat(PyObject *ErrType, char *stringformat, char *format, ...)
388{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000389 va_list va;
390 PyObject *args=0, *retval=0;
391 va_start(va, format);
Tim Peterscba30e22003-02-01 06:24:36 +0000392
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000393 if (format) args = Py_VaBuildValue(format, va);
394 va_end(va);
395 if (format && ! args) return NULL;
Tim Peterscba30e22003-02-01 06:24:36 +0000396 if (stringformat && !(retval=PyString_FromString(stringformat)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000397 return NULL;
Tim Peterscba30e22003-02-01 06:24:36 +0000398
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000399 if (retval) {
400 if (args) {
401 PyObject *v;
402 v=PyString_Format(retval, args);
403 Py_DECREF(retval);
404 Py_DECREF(args);
405 if (! v) return NULL;
406 retval=v;
407 }
408 }
409 else
410 if (args) retval=args;
411 else {
412 PyErr_SetObject(ErrType,Py_None);
413 return NULL;
414 }
415 PyErr_SetObject(ErrType,retval);
416 Py_DECREF(retval);
417 return NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000418}
419
Tim Peters84e87f32001-03-17 04:50:51 +0000420static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000421write_file(Picklerobject *self, const char *s, Py_ssize_t n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000422{
423 size_t nbyteswritten;
Tim Peters84e87f32001-03-17 04:50:51 +0000424
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000425 if (s == NULL) {
426 return 0;
427 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000428
Martin v. Löwis18e16552006-02-15 17:27:45 +0000429 if (n > INT_MAX) {
430 /* String too large */
431 return -1;
432 }
433
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000434 Py_BEGIN_ALLOW_THREADS
Jeremy Hylton7fa4bfa2002-07-18 20:58:57 +0000435 nbyteswritten = fwrite(s, sizeof(char), n, self->fp);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000436 Py_END_ALLOW_THREADS
Jeremy Hylton7fa4bfa2002-07-18 20:58:57 +0000437 if (nbyteswritten != (size_t)n) {
438 PyErr_SetFromErrno(PyExc_IOError);
439 return -1;
440 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000441
Martin v. Löwis18e16552006-02-15 17:27:45 +0000442 return (int)n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000443}
444
Tim Peters84e87f32001-03-17 04:50:51 +0000445static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000446write_cStringIO(Picklerobject *self, const char *s, Py_ssize_t n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000447{
448 if (s == NULL) {
449 return 0;
450 }
Tim Peterscba30e22003-02-01 06:24:36 +0000451
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000452 if (PycStringIO->cwrite((PyObject *)self->file, s, n) != n) {
453 return -1;
454 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000455
Martin v. Löwis18e16552006-02-15 17:27:45 +0000456 return (int)n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000457}
458
Tim Peters84e87f32001-03-17 04:50:51 +0000459static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000460write_none(Picklerobject *self, const char *s, Py_ssize_t n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000461{
462 if (s == NULL) return 0;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000463 if (n > INT_MAX) return -1;
464 return (int)n;
Guido van Rossum142eeb81997-08-13 03:14:41 +0000465}
466
Tim Peters84e87f32001-03-17 04:50:51 +0000467static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000468write_other(Picklerobject *self, const char *s, Py_ssize_t _n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000469{
470 PyObject *py_str = 0, *junk = 0;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000471 int n;
Tim Peterscba30e22003-02-01 06:24:36 +0000472
Martin v. Löwis18e16552006-02-15 17:27:45 +0000473 if (_n > INT_MAX)
474 return -1;
475 n = (int)_n;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000476 if (s == NULL) {
477 if (!( self->buf_size )) return 0;
Tim Peterscba30e22003-02-01 06:24:36 +0000478 py_str = PyString_FromStringAndSize(self->write_buf,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000479 self->buf_size);
Tim Peterscba30e22003-02-01 06:24:36 +0000480 if (!py_str)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000481 return -1;
482 }
483 else {
484 if (self->buf_size && (n + self->buf_size) > WRITE_BUF_SIZE) {
485 if (write_other(self, NULL, 0) < 0)
486 return -1;
487 }
Tim Peterscba30e22003-02-01 06:24:36 +0000488
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000489 if (n > WRITE_BUF_SIZE) {
490 if (!( py_str =
Tim Peterscba30e22003-02-01 06:24:36 +0000491 PyString_FromStringAndSize(s, n)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000492 return -1;
493 }
494 else {
495 memcpy(self->write_buf + self->buf_size, s, n);
496 self->buf_size += n;
497 return n;
498 }
499 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000500
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000501 if (self->write) {
502 /* object with write method */
503 ARG_TUP(self, py_str);
504 if (self->arg) {
505 junk = PyObject_Call(self->write, self->arg, NULL);
506 FREE_ARG_TUP(self);
507 }
508 if (junk) Py_DECREF(junk);
509 else return -1;
510 }
511 else
512 PDATA_PUSH(self->file, py_str, -1);
Guido van Rossum60456fd1997-04-09 17:36:32 +0000513
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000514 self->buf_size = 0;
515 return n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000516}
517
518
Martin v. Löwis18e16552006-02-15 17:27:45 +0000519static Py_ssize_t
520read_file(Unpicklerobject *self, char **s, Py_ssize_t n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000521{
522 size_t nbytesread;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000523
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000524 if (self->buf_size == 0) {
525 int size;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000526
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000527 size = ((n < 32) ? 32 : n);
Tim Petersee1a53c2003-02-02 02:57:53 +0000528 if (!( self->buf = (char *)malloc(size))) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000529 PyErr_NoMemory();
530 return -1;
531 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000532
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000533 self->buf_size = size;
534 }
535 else if (n > self->buf_size) {
Tim Petersee1a53c2003-02-02 02:57:53 +0000536 self->buf = (char *)realloc(self->buf, n);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000537 if (!self->buf) {
538 PyErr_NoMemory();
539 return -1;
540 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000541 self->buf_size = n;
542 }
Tim Peters84e87f32001-03-17 04:50:51 +0000543
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000544 Py_BEGIN_ALLOW_THREADS
545 nbytesread = fread(self->buf, sizeof(char), n, self->fp);
546 Py_END_ALLOW_THREADS
547 if (nbytesread != (size_t)n) {
548 if (feof(self->fp)) {
549 PyErr_SetNone(PyExc_EOFError);
550 return -1;
551 }
Tim Peterscba30e22003-02-01 06:24:36 +0000552
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000553 PyErr_SetFromErrno(PyExc_IOError);
554 return -1;
555 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000556
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000557 *s = self->buf;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000558
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000559 return n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000560}
561
562
Martin v. Löwis18e16552006-02-15 17:27:45 +0000563static Py_ssize_t
Tim Peterscba30e22003-02-01 06:24:36 +0000564readline_file(Unpicklerobject *self, char **s)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000565{
566 int i;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000567
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000568 if (self->buf_size == 0) {
Tim Petersee1a53c2003-02-02 02:57:53 +0000569 if (!( self->buf = (char *)malloc(40))) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000570 PyErr_NoMemory();
571 return -1;
572 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000573 self->buf_size = 40;
574 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000575
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000576 i = 0;
577 while (1) {
Tim Petersee1a53c2003-02-02 02:57:53 +0000578 int bigger;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000579 for (; i < (self->buf_size - 1); i++) {
Tim Peterscba30e22003-02-01 06:24:36 +0000580 if (feof(self->fp) ||
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000581 (self->buf[i] = getc(self->fp)) == '\n') {
582 self->buf[i + 1] = '\0';
583 *s = self->buf;
584 return i + 1;
585 }
586 }
Tim Petersee1a53c2003-02-02 02:57:53 +0000587 bigger = self->buf_size << 1;
588 if (bigger <= 0) { /* overflow */
589 PyErr_NoMemory();
590 return -1;
591 }
592 self->buf = (char *)realloc(self->buf, bigger);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000593 if (!self->buf) {
594 PyErr_NoMemory();
595 return -1;
596 }
Tim Petersee1a53c2003-02-02 02:57:53 +0000597 self->buf_size = bigger;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000598 }
Tim Peters84e87f32001-03-17 04:50:51 +0000599}
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000600
601
Martin v. Löwis18e16552006-02-15 17:27:45 +0000602static Py_ssize_t
603read_cStringIO(Unpicklerobject *self, char **s, Py_ssize_t n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000604{
605 char *ptr;
Tim Peterscba30e22003-02-01 06:24:36 +0000606
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000607 if (PycStringIO->cread((PyObject *)self->file, &ptr, n) != n) {
608 PyErr_SetNone(PyExc_EOFError);
609 return -1;
610 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000611
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000612 *s = ptr;
Tim Peterscba30e22003-02-01 06:24:36 +0000613
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000614 return n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000615}
616
617
Martin v. Löwis18e16552006-02-15 17:27:45 +0000618static Py_ssize_t
Tim Peterscba30e22003-02-01 06:24:36 +0000619readline_cStringIO(Unpicklerobject *self, char **s)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000620{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000621 Py_ssize_t n;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000622 char *ptr;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000623
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000624 if ((n = PycStringIO->creadline((PyObject *)self->file, &ptr)) < 0) {
625 return -1;
626 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000627
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000628 *s = ptr;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000629
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000630 return n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000631}
632
633
Martin v. Löwis18e16552006-02-15 17:27:45 +0000634static Py_ssize_t
635read_other(Unpicklerobject *self, char **s, Py_ssize_t n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000636{
637 PyObject *bytes, *str=0;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000638
Martin v. Löwis18e16552006-02-15 17:27:45 +0000639 if (!( bytes = PyInt_FromSsize_t(n))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000640
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000641 ARG_TUP(self, bytes);
642 if (self->arg) {
643 str = PyObject_Call(self->read, self->arg, NULL);
644 FREE_ARG_TUP(self);
645 }
646 if (! str) return -1;
Tim Peterscba30e22003-02-01 06:24:36 +0000647
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000648 Py_XDECREF(self->last_string);
649 self->last_string = str;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000650
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000651 if (! (*s = PyString_AsString(str))) return -1;
652 return n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000653}
654
655
Martin v. Löwis18e16552006-02-15 17:27:45 +0000656static Py_ssize_t
Tim Peterscba30e22003-02-01 06:24:36 +0000657readline_other(Unpicklerobject *self, char **s)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000658{
659 PyObject *str;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000660 Py_ssize_t str_size;
Tim Peterscba30e22003-02-01 06:24:36 +0000661
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000662 if (!( str = PyObject_CallObject(self->readline, empty_tuple))) {
663 return -1;
664 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000665
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000666 if ((str_size = PyString_Size(str)) < 0)
667 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000668
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000669 Py_XDECREF(self->last_string);
670 self->last_string = str;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000671
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000672 if (! (*s = PyString_AsString(str)))
673 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000674
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000675 return str_size;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000676}
677
Tim Petersee1a53c2003-02-02 02:57:53 +0000678/* Copy the first n bytes from s into newly malloc'ed memory, plus a
679 * trailing 0 byte. Return a pointer to that, or NULL if out of memory.
680 * The caller is responsible for free()'ing the return value.
681 */
Guido van Rossum60456fd1997-04-09 17:36:32 +0000682static char *
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000683pystrndup(const char *s, int n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000684{
Tim Petersee1a53c2003-02-02 02:57:53 +0000685 char *r = (char *)malloc(n+1);
686 if (r == NULL)
687 return (char*)PyErr_NoMemory();
688 memcpy(r, s, n);
689 r[n] = 0;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000690 return r;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000691}
692
693
694static int
Tim Peterscba30e22003-02-01 06:24:36 +0000695get(Picklerobject *self, PyObject *id)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000696{
697 PyObject *value, *mv;
698 long c_value;
699 char s[30];
700 size_t len;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000701
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000702 if (!( mv = PyDict_GetItem(self->memo, id))) {
703 PyErr_SetObject(PyExc_KeyError, id);
704 return -1;
705 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000706
Tim Peterscba30e22003-02-01 06:24:36 +0000707 if (!( value = PyTuple_GetItem(mv, 0)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000708 return -1;
Tim Peters84e87f32001-03-17 04:50:51 +0000709
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000710 if (!( PyInt_Check(value))) {
711 PyErr_SetString(PicklingError, "no int where int expected in memo");
712 return -1;
713 }
714 c_value = PyInt_AS_LONG((PyIntObject*)value);
Guido van Rossum60456fd1997-04-09 17:36:32 +0000715
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000716 if (!self->bin) {
717 s[0] = GET;
718 PyOS_snprintf(s + 1, sizeof(s) - 1, "%ld\n", c_value);
719 len = strlen(s);
720 }
721 else if (Pdata_Check(self->file)) {
722 if (write_other(self, NULL, 0) < 0) return -1;
723 PDATA_APPEND(self->file, mv, -1);
724 return 0;
725 }
726 else {
727 if (c_value < 256) {
728 s[0] = BINGET;
729 s[1] = (int)(c_value & 0xff);
730 len = 2;
731 }
732 else {
733 s[0] = LONG_BINGET;
734 s[1] = (int)(c_value & 0xff);
735 s[2] = (int)((c_value >> 8) & 0xff);
736 s[3] = (int)((c_value >> 16) & 0xff);
737 s[4] = (int)((c_value >> 24) & 0xff);
738 len = 5;
739 }
740 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000741
Tim Peters0bc93f52003-02-02 18:29:33 +0000742 if (self->write_func(self, s, len) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000743 return -1;
Tim Peterscba30e22003-02-01 06:24:36 +0000744
Jeremy Hyltona0fb1772001-10-12 04:11:06 +0000745 return 0;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000746}
Jeremy Hyltonce616e41998-08-13 23:13:52 +0000747
Guido van Rossum60456fd1997-04-09 17:36:32 +0000748
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000749static int
Tim Peterscba30e22003-02-01 06:24:36 +0000750put(Picklerobject *self, PyObject *ob)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000751{
Tim Peterscba30e22003-02-01 06:24:36 +0000752 if (ob->ob_refcnt < 2 || self->fast)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000753 return 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000754
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000755 return put2(self, ob);
756}
Guido van Rossum053b8df1998-11-25 16:18:00 +0000757
Guido van Rossum053b8df1998-11-25 16:18:00 +0000758
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000759static int
Tim Peterscba30e22003-02-01 06:24:36 +0000760put2(Picklerobject *self, PyObject *ob)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000761{
762 char c_str[30];
763 int p;
764 size_t len;
765 int res = -1;
766 PyObject *py_ob_id = 0, *memo_len = 0, *t = 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000767
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000768 if (self->fast)
769 return 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000770
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000771 if ((p = PyDict_Size(self->memo)) < 0)
772 goto finally;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000773
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000774 /* Make sure memo keys are positive! */
Tim Peters70b02d72003-02-02 17:26:40 +0000775 /* XXX Why?
776 * XXX And does "positive" really mean non-negative?
777 * XXX pickle.py starts with PUT index 0, not 1. This makes for
778 * XXX gratuitous differences between the pickling modules.
779 */
Tim Peterscba30e22003-02-01 06:24:36 +0000780 p++;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000781
Tim Peterscba30e22003-02-01 06:24:36 +0000782 if (!( py_ob_id = PyLong_FromVoidPtr(ob)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000783 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000784
Tim Peterscba30e22003-02-01 06:24:36 +0000785 if (!( memo_len = PyInt_FromLong(p)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000786 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000787
Tim Peterscba30e22003-02-01 06:24:36 +0000788 if (!( t = PyTuple_New(2)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000789 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000790
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000791 PyTuple_SET_ITEM(t, 0, memo_len);
792 Py_INCREF(memo_len);
793 PyTuple_SET_ITEM(t, 1, ob);
794 Py_INCREF(ob);
795
796 if (PyDict_SetItem(self->memo, py_ob_id, t) < 0)
797 goto finally;
798
799 if (!self->bin) {
800 c_str[0] = PUT;
801 PyOS_snprintf(c_str + 1, sizeof(c_str) - 1, "%d\n", p);
802 len = strlen(c_str);
803 }
804 else if (Pdata_Check(self->file)) {
805 if (write_other(self, NULL, 0) < 0) return -1;
806 PDATA_APPEND(self->file, memo_len, -1);
807 res=0; /* Job well done ;) */
808 goto finally;
809 }
810 else {
811 if (p >= 256) {
812 c_str[0] = LONG_BINPUT;
813 c_str[1] = (int)(p & 0xff);
814 c_str[2] = (int)((p >> 8) & 0xff);
815 c_str[3] = (int)((p >> 16) & 0xff);
816 c_str[4] = (int)((p >> 24) & 0xff);
817 len = 5;
818 }
819 else {
820 c_str[0] = BINPUT;
821 c_str[1] = p;
822 len = 2;
823 }
824 }
825
Tim Peters0bc93f52003-02-02 18:29:33 +0000826 if (self->write_func(self, c_str, len) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000827 goto finally;
828
829 res = 0;
830
831 finally:
832 Py_XDECREF(py_ob_id);
833 Py_XDECREF(memo_len);
834 Py_XDECREF(t);
835
836 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000837}
838
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000839static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +0000840whichmodule(PyObject *global, PyObject *global_name)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000841{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000842 Py_ssize_t i, j;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000843 PyObject *module = 0, *modules_dict = 0,
844 *global_name_attr = 0, *name = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000845
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000846 module = PyObject_GetAttrString(global, "__module__");
Jeremy Hyltonf00368f2003-06-16 20:19:49 +0000847 if (module)
848 return module;
849 if (PyErr_ExceptionMatches(PyExc_AttributeError))
850 PyErr_Clear();
851 else
852 return NULL;
Guido van Rossum45188231997-09-28 05:38:51 +0000853
Tim Peterscba30e22003-02-01 06:24:36 +0000854 if (!( modules_dict = PySys_GetObject("modules")))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000855 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000856
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000857 i = 0;
858 while ((j = PyDict_Next(modules_dict, &i, &name, &module))) {
Guido van Rossum142eeb81997-08-13 03:14:41 +0000859
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000860 if (PyObject_Compare(name, __main___str)==0) continue;
Tim Peters84e87f32001-03-17 04:50:51 +0000861
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000862 global_name_attr = PyObject_GetAttr(module, global_name);
863 if (!global_name_attr) {
Jeremy Hyltonf00368f2003-06-16 20:19:49 +0000864 if (PyErr_ExceptionMatches(PyExc_AttributeError))
865 PyErr_Clear();
866 else
867 return NULL;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000868 continue;
869 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000870
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000871 if (global_name_attr != global) {
872 Py_DECREF(global_name_attr);
873 continue;
874 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000875
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000876 Py_DECREF(global_name_attr);
Guido van Rossum60456fd1997-04-09 17:36:32 +0000877
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000878 break;
879 }
Guido van Rossum142eeb81997-08-13 03:14:41 +0000880
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000881 /* The following implements the rule in pickle.py added in 1.5
882 that used __main__ if no module is found. I don't actually
883 like this rule. jlf
884 */
885 if (!j) {
886 j=1;
887 name=__main___str;
888 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000889
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000890 Py_INCREF(name);
891 return name;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000892}
893
894
Guido van Rossum60456fd1997-04-09 17:36:32 +0000895static int
Jeremy Hylton499ab6a2001-10-15 21:37:58 +0000896fast_save_enter(Picklerobject *self, PyObject *obj)
897{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000898 /* if fast_container < 0, we're doing an error exit. */
899 if (++self->fast_container >= PY_CPICKLE_FAST_LIMIT) {
900 PyObject *key = NULL;
901 if (self->fast_memo == NULL) {
902 self->fast_memo = PyDict_New();
903 if (self->fast_memo == NULL) {
904 self->fast_container = -1;
905 return 0;
906 }
907 }
908 key = PyLong_FromVoidPtr(obj);
909 if (key == NULL)
910 return 0;
911 if (PyDict_GetItem(self->fast_memo, key)) {
Tim Peters4e52ca82002-12-07 02:43:28 +0000912 Py_DECREF(key);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000913 PyErr_Format(PyExc_ValueError,
Tim Peters92c8bb32003-02-13 23:00:26 +0000914 "fast mode: can't pickle cyclic objects "
915 "including object type %s at %p",
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000916 obj->ob_type->tp_name, obj);
917 self->fast_container = -1;
918 return 0;
919 }
920 if (PyDict_SetItem(self->fast_memo, key, Py_None) < 0) {
Tim Peters4e52ca82002-12-07 02:43:28 +0000921 Py_DECREF(key);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000922 self->fast_container = -1;
923 return 0;
924 }
Tim Peters4e52ca82002-12-07 02:43:28 +0000925 Py_DECREF(key);
Jeremy Hylton499ab6a2001-10-15 21:37:58 +0000926 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000927 return 1;
Jeremy Hylton499ab6a2001-10-15 21:37:58 +0000928}
929
Tim Peterscba30e22003-02-01 06:24:36 +0000930int
Jeremy Hylton499ab6a2001-10-15 21:37:58 +0000931fast_save_leave(Picklerobject *self, PyObject *obj)
932{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000933 if (self->fast_container-- >= PY_CPICKLE_FAST_LIMIT) {
934 PyObject *key = PyLong_FromVoidPtr(obj);
935 if (key == NULL)
936 return 0;
937 if (PyDict_DelItem(self->fast_memo, key) < 0) {
Tim Peters4e52ca82002-12-07 02:43:28 +0000938 Py_DECREF(key);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000939 return 0;
940 }
Tim Peters4e52ca82002-12-07 02:43:28 +0000941 Py_DECREF(key);
Jeremy Hylton499ab6a2001-10-15 21:37:58 +0000942 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000943 return 1;
Jeremy Hylton499ab6a2001-10-15 21:37:58 +0000944}
945
946static int
Tim Peterscba30e22003-02-01 06:24:36 +0000947save_none(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000948{
949 static char none = NONE;
Tim Peters0bc93f52003-02-02 18:29:33 +0000950 if (self->write_func(self, &none, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000951 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000952
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000953 return 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000954}
955
Guido van Rossum77f6a652002-04-03 22:41:51 +0000956static int
Tim Peterscba30e22003-02-01 06:24:36 +0000957save_bool(Picklerobject *self, PyObject *args)
Guido van Rossum77f6a652002-04-03 22:41:51 +0000958{
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000959 static const char *buf[2] = {FALSE, TRUE};
Guido van Rossume2763392002-04-05 19:30:08 +0000960 static char len[2] = {sizeof(FALSE)-1, sizeof(TRUE)-1};
Guido van Rossum77f6a652002-04-03 22:41:51 +0000961 long l = PyInt_AS_LONG((PyIntObject *)args);
962
Tim Peters3c67d792003-02-02 17:59:11 +0000963 if (self->proto >= 2) {
964 char opcode = l ? NEWTRUE : NEWFALSE;
965 if (self->write_func(self, &opcode, 1) < 0)
966 return -1;
967 }
968 else if (self->write_func(self, buf[l], len[l]) < 0)
Guido van Rossum77f6a652002-04-03 22:41:51 +0000969 return -1;
Guido van Rossum77f6a652002-04-03 22:41:51 +0000970 return 0;
971}
Tim Peters84e87f32001-03-17 04:50:51 +0000972
Guido van Rossum60456fd1997-04-09 17:36:32 +0000973static int
Tim Peterscba30e22003-02-01 06:24:36 +0000974save_int(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000975{
976 char c_str[32];
977 long l = PyInt_AS_LONG((PyIntObject *)args);
978 int len = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000979
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000980 if (!self->bin
Guido van Rossumde8d6d71997-05-13 18:00:44 +0000981#if SIZEOF_LONG > 4
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000982 || l > 0x7fffffffL
983 || l < -0x80000000L
Guido van Rossumde8d6d71997-05-13 18:00:44 +0000984#endif
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000985 ) {
986 /* Text-mode pickle, or long too big to fit in the 4-byte
987 * signed BININT format: store as a string.
988 */
989 c_str[0] = INT;
990 PyOS_snprintf(c_str + 1, sizeof(c_str) - 1, "%ld\n", l);
Tim Peters0bc93f52003-02-02 18:29:33 +0000991 if (self->write_func(self, c_str, strlen(c_str)) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000992 return -1;
993 }
994 else {
995 /* Binary pickle and l fits in a signed 4-byte int. */
996 c_str[1] = (int)( l & 0xff);
997 c_str[2] = (int)((l >> 8) & 0xff);
998 c_str[3] = (int)((l >> 16) & 0xff);
999 c_str[4] = (int)((l >> 24) & 0xff);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001000
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001001 if ((c_str[4] == 0) && (c_str[3] == 0)) {
1002 if (c_str[2] == 0) {
1003 c_str[0] = BININT1;
1004 len = 2;
1005 }
1006 else {
1007 c_str[0] = BININT2;
1008 len = 3;
1009 }
1010 }
1011 else {
1012 c_str[0] = BININT;
1013 len = 5;
1014 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001015
Tim Peters0bc93f52003-02-02 18:29:33 +00001016 if (self->write_func(self, c_str, len) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001017 return -1;
1018 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001019
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001020 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001021}
1022
1023
1024static int
Tim Peterscba30e22003-02-01 06:24:36 +00001025save_long(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001026{
Tim Petersee1a53c2003-02-02 02:57:53 +00001027 int size;
1028 int res = -1;
1029 PyObject *repr = NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001030
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001031 static char l = LONG;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001032
Tim Petersee1a53c2003-02-02 02:57:53 +00001033 if (self->proto >= 2) {
1034 /* Linear-time pickling. */
1035 size_t nbits;
1036 size_t nbytes;
1037 unsigned char *pdata;
1038 char c_str[5];
1039 int i;
1040 int sign = _PyLong_Sign(args);
1041
1042 if (sign == 0) {
1043 /* It's 0 -- an empty bytestring. */
1044 c_str[0] = LONG1;
1045 c_str[1] = 0;
1046 i = self->write_func(self, c_str, 2);
1047 if (i < 0) goto finally;
1048 res = 0;
1049 goto finally;
1050 }
1051 nbits = _PyLong_NumBits(args);
1052 if (nbits == (size_t)-1 && PyErr_Occurred())
1053 goto finally;
1054 /* How many bytes do we need? There are nbits >> 3 full
1055 * bytes of data, and nbits & 7 leftover bits. If there
1056 * are any leftover bits, then we clearly need another
1057 * byte. Wnat's not so obvious is that we *probably*
1058 * need another byte even if there aren't any leftovers:
1059 * the most-significant bit of the most-significant byte
1060 * acts like a sign bit, and it's usually got a sense
1061 * opposite of the one we need. The exception is longs
1062 * of the form -(2**(8*j-1)) for j > 0. Such a long is
1063 * its own 256's-complement, so has the right sign bit
1064 * even without the extra byte. That's a pain to check
1065 * for in advance, though, so we always grab an extra
1066 * byte at the start, and cut it back later if possible.
1067 */
1068 nbytes = (nbits >> 3) + 1;
1069 if ((int)nbytes < 0 || (size_t)(int)nbytes != nbytes) {
1070 PyErr_SetString(PyExc_OverflowError, "long too large "
1071 "to pickle");
1072 goto finally;
1073 }
1074 repr = PyString_FromStringAndSize(NULL, (int)nbytes);
1075 if (repr == NULL) goto finally;
1076 pdata = (unsigned char *)PyString_AS_STRING(repr);
1077 i = _PyLong_AsByteArray((PyLongObject *)args,
1078 pdata, nbytes,
1079 1 /* little endian */, 1 /* signed */);
1080 if (i < 0) goto finally;
1081 /* If the long is negative, this may be a byte more than
1082 * needed. This is so iff the MSB is all redundant sign
1083 * bits.
1084 */
1085 if (sign < 0 && nbytes > 1 && pdata[nbytes - 1] == 0xff &&
1086 (pdata[nbytes - 2] & 0x80) != 0)
1087 --nbytes;
1088
1089 if (nbytes < 256) {
1090 c_str[0] = LONG1;
1091 c_str[1] = (char)nbytes;
1092 size = 2;
1093 }
1094 else {
1095 c_str[0] = LONG4;
1096 size = (int)nbytes;
1097 for (i = 1; i < 5; i++) {
1098 c_str[i] = (char)(size & 0xff);
1099 size >>= 8;
1100 }
1101 size = 5;
1102 }
1103 i = self->write_func(self, c_str, size);
1104 if (i < 0) goto finally;
1105 i = self->write_func(self, (char *)pdata, (int)nbytes);
1106 if (i < 0) goto finally;
1107 res = 0;
1108 goto finally;
1109 }
1110
1111 /* proto < 2: write the repr and newline. This is quadratic-time
1112 * (in the number of digits), in both directions.
1113 */
Tim Peterscba30e22003-02-01 06:24:36 +00001114 if (!( repr = PyObject_Repr(args)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001115 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001116
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001117 if ((size = PyString_Size(repr)) < 0)
1118 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001119
Tim Peters0bc93f52003-02-02 18:29:33 +00001120 if (self->write_func(self, &l, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001121 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001122
Tim Peters0bc93f52003-02-02 18:29:33 +00001123 if (self->write_func(self,
1124 PyString_AS_STRING((PyStringObject *)repr),
1125 size) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001126 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001127
Tim Peters0bc93f52003-02-02 18:29:33 +00001128 if (self->write_func(self, "\n", 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001129 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001130
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001131 res = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001132
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001133 finally:
1134 Py_XDECREF(repr);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001135 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001136}
1137
1138
1139static int
Tim Peterscba30e22003-02-01 06:24:36 +00001140save_float(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001141{
1142 double x = PyFloat_AS_DOUBLE((PyFloatObject *)args);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001143
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001144 if (self->bin) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001145 char str[9];
Tim Peters9905b942003-03-20 20:53:32 +00001146 str[0] = BINFLOAT;
1147 if (_PyFloat_Pack8(x, (unsigned char *)&str[1], 0) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001148 return -1;
Tim Peters0bc93f52003-02-02 18:29:33 +00001149 if (self->write_func(self, str, 9) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001150 return -1;
1151 }
1152 else {
1153 char c_str[250];
1154 c_str[0] = FLOAT;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001155 PyOS_ascii_formatd(c_str + 1, sizeof(c_str) - 2, "%.17g", x);
1156 /* Extend the formatted string with a newline character */
1157 strcat(c_str, "\n");
Guido van Rossum60456fd1997-04-09 17:36:32 +00001158
Tim Peters0bc93f52003-02-02 18:29:33 +00001159 if (self->write_func(self, c_str, strlen(c_str)) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001160 return -1;
1161 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001162
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001163 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001164}
1165
1166
1167static int
Tim Peterscba30e22003-02-01 06:24:36 +00001168save_string(Picklerobject *self, PyObject *args, int doput)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001169{
1170 int size, len;
1171 PyObject *repr=0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001172
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001173 if ((size = PyString_Size(args)) < 0)
1174 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001175
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001176 if (!self->bin) {
1177 char *repr_str;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001178
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001179 static char string = STRING;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001180
Tim Peterscba30e22003-02-01 06:24:36 +00001181 if (!( repr = PyObject_Repr(args)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001182 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001183
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001184 if ((len = PyString_Size(repr)) < 0)
1185 goto err;
1186 repr_str = PyString_AS_STRING((PyStringObject *)repr);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001187
Tim Peters0bc93f52003-02-02 18:29:33 +00001188 if (self->write_func(self, &string, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001189 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001190
Tim Peters0bc93f52003-02-02 18:29:33 +00001191 if (self->write_func(self, repr_str, len) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001192 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001193
Tim Peters0bc93f52003-02-02 18:29:33 +00001194 if (self->write_func(self, "\n", 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001195 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001196
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001197 Py_XDECREF(repr);
1198 }
1199 else {
1200 int i;
1201 char c_str[5];
Guido van Rossum60456fd1997-04-09 17:36:32 +00001202
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001203 if ((size = PyString_Size(args)) < 0)
1204 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001205
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001206 if (size < 256) {
1207 c_str[0] = SHORT_BINSTRING;
1208 c_str[1] = size;
1209 len = 2;
1210 }
1211 else {
1212 c_str[0] = BINSTRING;
1213 for (i = 1; i < 5; i++)
1214 c_str[i] = (int)(size >> ((i - 1) * 8));
1215 len = 5;
1216 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001217
Tim Peters0bc93f52003-02-02 18:29:33 +00001218 if (self->write_func(self, c_str, len) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001219 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001220
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001221 if (size > 128 && Pdata_Check(self->file)) {
1222 if (write_other(self, NULL, 0) < 0) return -1;
1223 PDATA_APPEND(self->file, args, -1);
1224 }
1225 else {
Tim Peters0bc93f52003-02-02 18:29:33 +00001226 if (self->write_func(self,
1227 PyString_AS_STRING(
1228 (PyStringObject *)args),
1229 size) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001230 return -1;
1231 }
1232 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001233
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001234 if (doput)
1235 if (put(self, args) < 0)
1236 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001237
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001238 return 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +00001239
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001240 err:
1241 Py_XDECREF(repr);
1242 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001243}
1244
1245
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001246#ifdef Py_USING_UNICODE
Guido van Rossumfb10c3f2000-12-19 02:08:38 +00001247/* A copy of PyUnicode_EncodeRawUnicodeEscape() that also translates
1248 backslash and newline characters to \uXXXX escapes. */
1249static PyObject *
1250modified_EncodeRawUnicodeEscape(const Py_UNICODE *s, int size)
1251{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001252 PyObject *repr;
1253 char *p;
1254 char *q;
Guido van Rossumfb10c3f2000-12-19 02:08:38 +00001255
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001256 static const char *hexdigit = "0123456789ABCDEF";
Guido van Rossumfb10c3f2000-12-19 02:08:38 +00001257
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001258 repr = PyString_FromStringAndSize(NULL, 6 * size);
1259 if (repr == NULL)
1260 return NULL;
1261 if (size == 0)
1262 return repr;
1263
1264 p = q = PyString_AS_STRING(repr);
1265 while (size-- > 0) {
1266 Py_UNICODE ch = *s++;
1267 /* Map 16-bit characters to '\uxxxx' */
1268 if (ch >= 256 || ch == '\\' || ch == '\n') {
1269 *p++ = '\\';
1270 *p++ = 'u';
1271 *p++ = hexdigit[(ch >> 12) & 0xf];
1272 *p++ = hexdigit[(ch >> 8) & 0xf];
1273 *p++ = hexdigit[(ch >> 4) & 0xf];
1274 *p++ = hexdigit[ch & 15];
1275 }
1276 /* Copy everything else as-is */
1277 else
1278 *p++ = (char) ch;
1279 }
1280 *p = '\0';
Tim Peters5de98422002-04-27 18:44:32 +00001281 _PyString_Resize(&repr, p - q);
Guido van Rossumfb10c3f2000-12-19 02:08:38 +00001282 return repr;
Guido van Rossumfb10c3f2000-12-19 02:08:38 +00001283}
1284
1285
Guido van Rossum60456fd1997-04-09 17:36:32 +00001286static int
Tim Peterscba30e22003-02-01 06:24:36 +00001287save_unicode(Picklerobject *self, PyObject *args, int doput)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001288{
1289 int size, len;
1290 PyObject *repr=0;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001291
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001292 if (!PyUnicode_Check(args))
1293 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001294
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001295 if (!self->bin) {
1296 char *repr_str;
1297 static char string = UNICODE;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001298
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001299 repr = modified_EncodeRawUnicodeEscape(
1300 PyUnicode_AS_UNICODE(args), PyUnicode_GET_SIZE(args));
Tim Peterscba30e22003-02-01 06:24:36 +00001301 if (!repr)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001302 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001303
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001304 if ((len = PyString_Size(repr)) < 0)
1305 goto err;
1306 repr_str = PyString_AS_STRING((PyStringObject *)repr);
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001307
Tim Peters0bc93f52003-02-02 18:29:33 +00001308 if (self->write_func(self, &string, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001309 goto err;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001310
Tim Peters0bc93f52003-02-02 18:29:33 +00001311 if (self->write_func(self, repr_str, len) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001312 goto err;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001313
Tim Peters0bc93f52003-02-02 18:29:33 +00001314 if (self->write_func(self, "\n", 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001315 goto err;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001316
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001317 Py_XDECREF(repr);
1318 }
1319 else {
1320 int i;
1321 char c_str[5];
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001322
Tim Peterscba30e22003-02-01 06:24:36 +00001323 if (!( repr = PyUnicode_AsUTF8String(args)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001324 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001325
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001326 if ((size = PyString_Size(repr)) < 0)
1327 goto err;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001328
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001329 c_str[0] = BINUNICODE;
1330 for (i = 1; i < 5; i++)
1331 c_str[i] = (int)(size >> ((i - 1) * 8));
1332 len = 5;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001333
Tim Peters0bc93f52003-02-02 18:29:33 +00001334 if (self->write_func(self, c_str, len) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001335 goto err;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001336
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001337 if (size > 128 && Pdata_Check(self->file)) {
1338 if (write_other(self, NULL, 0) < 0)
1339 goto err;
1340 PDATA_APPEND(self->file, repr, -1);
1341 }
1342 else {
Tim Peters0bc93f52003-02-02 18:29:33 +00001343 if (self->write_func(self, PyString_AS_STRING(repr),
1344 size) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001345 goto err;
1346 }
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001347
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001348 Py_DECREF(repr);
1349 }
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001350
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001351 if (doput)
1352 if (put(self, args) < 0)
1353 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001354
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001355 return 0;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001356
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001357 err:
1358 Py_XDECREF(repr);
1359 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001360}
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001361#endif
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001362
Tim Peters1d63c9f2003-02-02 20:29:39 +00001363/* A helper for save_tuple. Push the len elements in tuple t on the stack. */
1364static int
Tim Peters67920142003-02-05 03:46:17 +00001365store_tuple_elements(Picklerobject *self, PyObject *t, int len)
Tim Peters1d63c9f2003-02-02 20:29:39 +00001366{
1367 int i;
1368 int res = -1; /* guilty until proved innocent */
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001369
Tim Peters1d63c9f2003-02-02 20:29:39 +00001370 assert(PyTuple_Size(t) == len);
1371
1372 for (i = 0; i < len; i++) {
1373 PyObject *element = PyTuple_GET_ITEM(t, i);
1374
1375 if (element == NULL)
1376 goto finally;
1377 if (save(self, element, 0) < 0)
1378 goto finally;
1379 }
1380 res = 0;
1381
1382 finally:
1383 return res;
1384}
1385
1386/* Tuples are ubiquitous in the pickle protocols, so many techniques are
1387 * used across protocols to minimize the space needed to pickle them.
Tim Peters67920142003-02-05 03:46:17 +00001388 * Tuples are also the only builtin immutable type that can be recursive
Tim Peters1d63c9f2003-02-02 20:29:39 +00001389 * (a tuple can be reached from itself), and that requires some subtle
1390 * magic so that it works in all cases. IOW, this is a long routine.
1391 */
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001392static int
Tim Peterscba30e22003-02-01 06:24:36 +00001393save_tuple(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001394{
Tim Peters1d63c9f2003-02-02 20:29:39 +00001395 PyObject *py_tuple_id = NULL;
1396 int len, i;
1397 int res = -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001398
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001399 static char tuple = TUPLE;
Tim Peters1d63c9f2003-02-02 20:29:39 +00001400 static char pop = POP;
1401 static char pop_mark = POP_MARK;
1402 static char len2opcode[] = {EMPTY_TUPLE, TUPLE1, TUPLE2, TUPLE3};
Guido van Rossum60456fd1997-04-09 17:36:32 +00001403
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001404 if ((len = PyTuple_Size(args)) < 0)
1405 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001406
Tim Peters1d63c9f2003-02-02 20:29:39 +00001407 if (len == 0) {
1408 char c_str[2];
Tim Peters84e87f32001-03-17 04:50:51 +00001409
Tim Peters1d63c9f2003-02-02 20:29:39 +00001410 if (self->proto) {
1411 c_str[0] = EMPTY_TUPLE;
1412 len = 1;
1413 }
1414 else {
1415 c_str[0] = MARK;
1416 c_str[1] = TUPLE;
1417 len = 2;
1418 }
1419 if (self->write_func(self, c_str, len) >= 0)
1420 res = 0;
1421 /* Don't memoize an empty tuple. */
1422 goto finally;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001423 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001424
Tim Peters1d63c9f2003-02-02 20:29:39 +00001425 /* A non-empty tuple. */
1426
1427 /* id(tuple) isn't in the memo now. If it shows up there after
1428 * saving the tuple elements, the tuple must be recursive, in
1429 * which case we'll pop everything we put on the stack, and fetch
1430 * its value from the memo.
1431 */
1432 py_tuple_id = PyLong_FromVoidPtr(args);
1433 if (py_tuple_id == NULL)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001434 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001435
Tim Peters1d63c9f2003-02-02 20:29:39 +00001436 if (len <= 3 && self->proto >= 2) {
1437 /* Use TUPLE{1,2,3} opcodes. */
Tim Peters67920142003-02-05 03:46:17 +00001438 if (store_tuple_elements(self, args, len) < 0)
Tim Peters1d63c9f2003-02-02 20:29:39 +00001439 goto finally;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001440 if (PyDict_GetItem(self->memo, py_tuple_id)) {
Tim Peters1d63c9f2003-02-02 20:29:39 +00001441 /* pop the len elements */
1442 for (i = 0; i < len; ++i)
1443 if (self->write_func(self, &pop, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001444 goto finally;
Tim Peters1d63c9f2003-02-02 20:29:39 +00001445 /* fetch from memo */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001446 if (get(self, py_tuple_id) < 0)
1447 goto finally;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001448 res = 0;
1449 goto finally;
1450 }
Tim Peters1d63c9f2003-02-02 20:29:39 +00001451 /* Not recursive. */
1452 if (self->write_func(self, len2opcode + len, 1) < 0)
1453 goto finally;
1454 goto memoize;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001455 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001456
Tim Peters1d63c9f2003-02-02 20:29:39 +00001457 /* proto < 2 and len > 0, or proto >= 2 and len > 3.
1458 * Generate MARK elt1 elt2 ... TUPLE
1459 */
1460 if (self->write_func(self, &MARKv, 1) < 0)
1461 goto finally;
1462
Tim Peters67920142003-02-05 03:46:17 +00001463 if (store_tuple_elements(self, args, len) < 0)
Tim Peters1d63c9f2003-02-02 20:29:39 +00001464 goto finally;
1465
1466 if (PyDict_GetItem(self->memo, py_tuple_id)) {
1467 /* pop the stack stuff we pushed */
1468 if (self->bin) {
1469 if (self->write_func(self, &pop_mark, 1) < 0)
1470 goto finally;
1471 }
1472 else {
1473 /* Note that we pop one more than len, to remove
1474 * the MARK too.
1475 */
1476 for (i = 0; i <= len; i++)
1477 if (self->write_func(self, &pop, 1) < 0)
1478 goto finally;
1479 }
1480 /* fetch from memo */
1481 if (get(self, py_tuple_id) >= 0)
1482 res = 0;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001483 goto finally;
1484 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001485
Tim Peters1d63c9f2003-02-02 20:29:39 +00001486 /* Not recursive. */
1487 if (self->write_func(self, &tuple, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001488 goto finally;
Tim Peters84e87f32001-03-17 04:50:51 +00001489
Tim Peters1d63c9f2003-02-02 20:29:39 +00001490 memoize:
1491 if (put(self, args) >= 0)
1492 res = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001493
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001494 finally:
1495 Py_XDECREF(py_tuple_id);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001496 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001497}
1498
Tim Peters1092d642003-02-11 21:06:20 +00001499/* iter is an iterator giving items, and we batch up chunks of
1500 * MARK item item ... item APPENDS
1501 * opcode sequences. Calling code should have arranged to first create an
1502 * empty list, or list-like object, for the APPENDS to operate on.
1503 * Returns 0 on success, <0 on error.
1504 */
1505static int
1506batch_list(Picklerobject *self, PyObject *iter)
1507{
1508 PyObject *obj;
1509 PyObject *slice[BATCHSIZE];
1510 int i, n;
1511
1512 static char append = APPEND;
1513 static char appends = APPENDS;
1514
1515 assert(iter != NULL);
1516
1517 if (self->proto == 0) {
1518 /* APPENDS isn't available; do one at a time. */
1519 for (;;) {
1520 obj = PyIter_Next(iter);
1521 if (obj == NULL) {
1522 if (PyErr_Occurred())
1523 return -1;
1524 break;
1525 }
1526 i = save(self, obj, 0);
1527 Py_DECREF(obj);
1528 if (i < 0)
1529 return -1;
1530 if (self->write_func(self, &append, 1) < 0)
1531 return -1;
Tim Peters1092d642003-02-11 21:06:20 +00001532 }
1533 return 0;
1534 }
1535
1536 /* proto > 0: write in batches of BATCHSIZE. */
1537 do {
1538 /* Get next group of (no more than) BATCHSIZE elements. */
1539 for (n = 0; n < BATCHSIZE; ++n) {
1540 obj = PyIter_Next(iter);
1541 if (obj == NULL) {
1542 if (PyErr_Occurred())
1543 goto BatchFailed;
1544 break;
1545 }
1546 slice[n] = obj;
1547 }
1548
1549 if (n > 1) {
1550 /* Pump out MARK, slice[0:n], APPENDS. */
1551 if (self->write_func(self, &MARKv, 1) < 0)
1552 goto BatchFailed;
1553 for (i = 0; i < n; ++i) {
1554 if (save(self, slice[i], 0) < 0)
1555 goto BatchFailed;
1556 }
1557 if (self->write_func(self, &appends, 1) < 0)
1558 goto BatchFailed;
1559 }
1560 else if (n == 1) {
1561 if (save(self, slice[0], 0) < 0)
1562 goto BatchFailed;
1563 if (self->write_func(self, &append, 1) < 0)
1564 goto BatchFailed;
1565 }
1566
1567 for (i = 0; i < n; ++i) {
1568 Py_DECREF(slice[i]);
1569 }
Tim Peters90975f12003-02-12 05:28:58 +00001570 } while (n == BATCHSIZE);
Tim Peters1092d642003-02-11 21:06:20 +00001571 return 0;
1572
1573BatchFailed:
1574 while (--n >= 0) {
1575 Py_DECREF(slice[n]);
1576 }
1577 return -1;
1578}
1579
Guido van Rossum60456fd1997-04-09 17:36:32 +00001580static int
Tim Peterscba30e22003-02-01 06:24:36 +00001581save_list(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001582{
Tim Peters1092d642003-02-11 21:06:20 +00001583 int res = -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001584 char s[3];
Tim Peters1092d642003-02-11 21:06:20 +00001585 int len;
1586 PyObject *iter;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001587
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001588 if (self->fast && !fast_save_enter(self, args))
1589 goto finally;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00001590
Tim Peters1092d642003-02-11 21:06:20 +00001591 /* Create an empty list. */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001592 if (self->bin) {
1593 s[0] = EMPTY_LIST;
Tim Peters1092d642003-02-11 21:06:20 +00001594 len = 1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001595 }
1596 else {
1597 s[0] = MARK;
1598 s[1] = LIST;
Tim Peters1092d642003-02-11 21:06:20 +00001599 len = 2;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001600 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001601
Tim Peters1092d642003-02-11 21:06:20 +00001602 if (self->write_func(self, s, len) < 0)
1603 goto finally;
1604
1605 /* Get list length, and bow out early if empty. */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001606 if ((len = PyList_Size(args)) < 0)
1607 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001608
Tim Peters1092d642003-02-11 21:06:20 +00001609 /* Memoize. */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001610 if (len == 0) {
Tim Peters90975f12003-02-12 05:28:58 +00001611 if (put(self, args) >= 0)
1612 res = 0;
1613 goto finally;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001614 }
Tim Peters90975f12003-02-12 05:28:58 +00001615 if (put2(self, args) < 0)
1616 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001617
Tim Peters1092d642003-02-11 21:06:20 +00001618 /* Materialize the list elements. */
1619 iter = PyObject_GetIter(args);
1620 if (iter == NULL)
1621 goto finally;
1622 res = batch_list(self, iter);
1623 Py_DECREF(iter);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001624
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001625 finally:
1626 if (self->fast && !fast_save_leave(self, args))
1627 res = -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001628
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001629 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001630}
1631
1632
Tim Peters42f08ac2003-02-11 22:43:24 +00001633/* iter is an iterator giving (key, value) pairs, and we batch up chunks of
1634 * MARK key value ... key value SETITEMS
1635 * opcode sequences. Calling code should have arranged to first create an
1636 * empty dict, or dict-like object, for the SETITEMS to operate on.
1637 * Returns 0 on success, <0 on error.
1638 *
1639 * This is very much like batch_list(). The difference between saving
1640 * elements directly, and picking apart two-tuples, is so long-winded at
1641 * the C level, though, that attempts to combine these routines were too
1642 * ugly to bear.
1643 */
1644static int
1645batch_dict(Picklerobject *self, PyObject *iter)
1646{
1647 PyObject *p;
1648 PyObject *slice[BATCHSIZE];
1649 int i, n;
1650
1651 static char setitem = SETITEM;
1652 static char setitems = SETITEMS;
1653
1654 assert(iter != NULL);
1655
1656 if (self->proto == 0) {
1657 /* SETITEMS isn't available; do one at a time. */
1658 for (;;) {
1659 p = PyIter_Next(iter);
1660 if (p == NULL) {
1661 if (PyErr_Occurred())
1662 return -1;
1663 break;
1664 }
1665 if (!PyTuple_Check(p) || PyTuple_Size(p) != 2) {
1666 PyErr_SetString(PyExc_TypeError, "dict items "
1667 "iterator must return 2-tuples");
1668 return -1;
1669 }
1670 i = save(self, PyTuple_GET_ITEM(p, 0), 0);
1671 if (i >= 0)
1672 i = save(self, PyTuple_GET_ITEM(p, 1), 0);
1673 Py_DECREF(p);
1674 if (i < 0)
1675 return -1;
1676 if (self->write_func(self, &setitem, 1) < 0)
1677 return -1;
Tim Peters42f08ac2003-02-11 22:43:24 +00001678 }
1679 return 0;
1680 }
1681
1682 /* proto > 0: write in batches of BATCHSIZE. */
1683 do {
1684 /* Get next group of (no more than) BATCHSIZE elements. */
1685 for (n = 0; n < BATCHSIZE; ++n) {
1686 p = PyIter_Next(iter);
1687 if (p == NULL) {
1688 if (PyErr_Occurred())
1689 goto BatchFailed;
1690 break;
1691 }
1692 if (!PyTuple_Check(p) || PyTuple_Size(p) != 2) {
1693 PyErr_SetString(PyExc_TypeError, "dict items "
1694 "iterator must return 2-tuples");
1695 goto BatchFailed;
1696 }
1697 slice[n] = p;
1698 }
1699
1700 if (n > 1) {
1701 /* Pump out MARK, slice[0:n], SETITEMS. */
1702 if (self->write_func(self, &MARKv, 1) < 0)
1703 goto BatchFailed;
1704 for (i = 0; i < n; ++i) {
1705 p = slice[i];
1706 if (save(self, PyTuple_GET_ITEM(p, 0), 0) < 0)
1707 goto BatchFailed;
1708 if (save(self, PyTuple_GET_ITEM(p, 1), 0) < 0)
1709 goto BatchFailed;
1710 }
1711 if (self->write_func(self, &setitems, 1) < 0)
1712 goto BatchFailed;
1713 }
1714 else if (n == 1) {
1715 p = slice[0];
1716 if (save(self, PyTuple_GET_ITEM(p, 0), 0) < 0)
1717 goto BatchFailed;
1718 if (save(self, PyTuple_GET_ITEM(p, 1), 0) < 0)
1719 goto BatchFailed;
1720 if (self->write_func(self, &setitem, 1) < 0)
1721 goto BatchFailed;
1722 }
1723
1724 for (i = 0; i < n; ++i) {
1725 Py_DECREF(slice[i]);
1726 }
Tim Peters90975f12003-02-12 05:28:58 +00001727 } while (n == BATCHSIZE);
Tim Peters42f08ac2003-02-11 22:43:24 +00001728 return 0;
1729
1730BatchFailed:
1731 while (--n >= 0) {
1732 Py_DECREF(slice[n]);
1733 }
1734 return -1;
1735}
1736
Guido van Rossum60456fd1997-04-09 17:36:32 +00001737static int
Tim Peterscba30e22003-02-01 06:24:36 +00001738save_dict(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001739{
Tim Peters42f08ac2003-02-11 22:43:24 +00001740 int res = -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001741 char s[3];
Tim Peters42f08ac2003-02-11 22:43:24 +00001742 int len;
1743 PyObject *iter;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001744
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001745 if (self->fast && !fast_save_enter(self, args))
1746 goto finally;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00001747
Tim Peters42f08ac2003-02-11 22:43:24 +00001748 /* Create an empty dict. */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001749 if (self->bin) {
1750 s[0] = EMPTY_DICT;
1751 len = 1;
1752 }
1753 else {
1754 s[0] = MARK;
1755 s[1] = DICT;
1756 len = 2;
1757 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001758
Tim Peters0bc93f52003-02-02 18:29:33 +00001759 if (self->write_func(self, s, len) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001760 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001761
Tim Peters42f08ac2003-02-11 22:43:24 +00001762 /* Get dict size, and bow out early if empty. */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001763 if ((len = PyDict_Size(args)) < 0)
1764 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001765
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001766 if (len == 0) {
Tim Peters90975f12003-02-12 05:28:58 +00001767 if (put(self, args) >= 0)
1768 res = 0;
1769 goto finally;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001770 }
Tim Peters90975f12003-02-12 05:28:58 +00001771 if (put2(self, args) < 0)
1772 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001773
Tim Peters42f08ac2003-02-11 22:43:24 +00001774 /* Materialize the dict items. */
1775 iter = PyObject_CallMethod(args, "iteritems", "()");
1776 if (iter == NULL)
1777 goto finally;
1778 res = batch_dict(self, iter);
1779 Py_DECREF(iter);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001780
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001781 finally:
1782 if (self->fast && !fast_save_leave(self, args))
1783 res = -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001784
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001785 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001786}
1787
1788
Guido van Rossum60456fd1997-04-09 17:36:32 +00001789static int
Tim Peterscba30e22003-02-01 06:24:36 +00001790save_global(Picklerobject *self, PyObject *args, PyObject *name)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001791{
Jeremy Hylton9ee91f12002-07-11 22:02:33 +00001792 PyObject *global_name = 0, *module = 0, *mod = 0, *klass = 0;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001793 char *name_str, *module_str;
1794 int module_size, name_size, res = -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001795
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001796 static char global = GLOBAL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001797
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001798 if (name) {
1799 global_name = name;
1800 Py_INCREF(global_name);
1801 }
1802 else {
Tim Peterscba30e22003-02-01 06:24:36 +00001803 if (!( global_name = PyObject_GetAttr(args, __name___str)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001804 goto finally;
1805 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001806
Tim Peterscba30e22003-02-01 06:24:36 +00001807 if (!( module = whichmodule(args, global_name)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001808 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001809
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001810 if ((module_size = PyString_Size(module)) < 0 ||
1811 (name_size = PyString_Size(global_name)) < 0)
1812 goto finally;
Guido van Rossum053b8df1998-11-25 16:18:00 +00001813
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001814 module_str = PyString_AS_STRING((PyStringObject *)module);
1815 name_str = PyString_AS_STRING((PyStringObject *)global_name);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001816
Guido van Rossum75bfd052002-12-24 18:10:07 +00001817 /* XXX This can be doing a relative import. Clearly it shouldn't,
1818 but I don't know how to stop it. :-( */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001819 mod = PyImport_ImportModule(module_str);
1820 if (mod == NULL) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001821 cPickle_ErrFormat(PicklingError,
Tim Petersb9ce7cd2003-02-18 20:50:45 +00001822 "Can't pickle %s: import of module %s "
1823 "failed",
1824 "OS", args, module);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001825 goto finally;
1826 }
Jeremy Hylton0e1f7a82002-07-11 22:01:40 +00001827 klass = PyObject_GetAttrString(mod, name_str);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001828 if (klass == NULL) {
1829 cPickle_ErrFormat(PicklingError,
Tim Petersb9ce7cd2003-02-18 20:50:45 +00001830 "Can't pickle %s: attribute lookup %s.%s "
1831 "failed",
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001832 "OSS", args, module, global_name);
1833 goto finally;
1834 }
1835 if (klass != args) {
Jeremy Hylton0e1f7a82002-07-11 22:01:40 +00001836 Py_DECREF(klass);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001837 cPickle_ErrFormat(PicklingError,
Tim Peters731098b2003-02-04 20:56:09 +00001838 "Can't pickle %s: it's not the same object "
1839 "as %s.%s",
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001840 "OSS", args, module, global_name);
1841 goto finally;
1842 }
Jeremy Hylton0e1f7a82002-07-11 22:01:40 +00001843 Py_DECREF(klass);
Guido van Rossuma92d16a2001-08-18 21:22:07 +00001844
Tim Peters731098b2003-02-04 20:56:09 +00001845 if (self->proto >= 2) {
1846 /* See whether this is in the extension registry, and if
1847 * so generate an EXT opcode.
1848 */
1849 PyObject *py_code; /* extension code as Python object */
Tim Peters3e667d52003-02-04 21:47:44 +00001850 long code; /* extension code as C value */
Tim Peters731098b2003-02-04 20:56:09 +00001851 char c_str[5];
1852 int n;
1853
1854 PyTuple_SET_ITEM(two_tuple, 0, module);
1855 PyTuple_SET_ITEM(two_tuple, 1, global_name);
1856 py_code = PyDict_GetItem(extension_registry, two_tuple);
1857 if (py_code == NULL)
1858 goto gen_global; /* not registered */
1859
1860 /* Verify py_code has the right type and value. */
1861 if (!PyInt_Check(py_code)) {
1862 cPickle_ErrFormat(PicklingError, "Can't pickle %s: "
Tim Peters6288e232003-02-05 03:53:10 +00001863 "extension code %s isn't an integer",
Tim Peters731098b2003-02-04 20:56:09 +00001864 "OO", args, py_code);
1865 goto finally;
1866 }
1867 code = PyInt_AS_LONG(py_code);
1868 if (code <= 0 || code > 0x7fffffffL) {
1869 cPickle_ErrFormat(PicklingError, "Can't pickle %s: "
1870 "extension code %ld is out of range",
1871 "Ol", args, code);
1872 goto finally;
1873 }
1874
1875 /* Generate an EXT opcode. */
1876 if (code <= 0xff) {
1877 c_str[0] = EXT1;
1878 c_str[1] = (char)code;
1879 n = 2;
1880 }
1881 else if (code <= 0xffff) {
1882 c_str[0] = EXT2;
1883 c_str[1] = (char)(code & 0xff);
1884 c_str[2] = (char)((code >> 8) & 0xff);
1885 n = 3;
1886 }
1887 else {
1888 c_str[0] = EXT4;
1889 c_str[1] = (char)(code & 0xff);
1890 c_str[2] = (char)((code >> 8) & 0xff);
1891 c_str[3] = (char)((code >> 16) & 0xff);
1892 c_str[4] = (char)((code >> 24) & 0xff);
1893 n = 5;
1894 }
1895
1896 if (self->write_func(self, c_str, n) >= 0)
1897 res = 0;
1898 goto finally; /* and don't memoize */
1899 }
1900
1901 gen_global:
Tim Peters0bc93f52003-02-02 18:29:33 +00001902 if (self->write_func(self, &global, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001903 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001904
Tim Peters0bc93f52003-02-02 18:29:33 +00001905 if (self->write_func(self, module_str, module_size) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001906 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001907
Tim Peters0bc93f52003-02-02 18:29:33 +00001908 if (self->write_func(self, "\n", 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001909 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001910
Tim Peters0bc93f52003-02-02 18:29:33 +00001911 if (self->write_func(self, name_str, name_size) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001912 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001913
Tim Peters0bc93f52003-02-02 18:29:33 +00001914 if (self->write_func(self, "\n", 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001915 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001916
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001917 if (put(self, args) < 0)
1918 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001919
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001920 res = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001921
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001922 finally:
1923 Py_XDECREF(module);
1924 Py_XDECREF(global_name);
1925 Py_XDECREF(mod);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001926
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001927 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001928}
1929
Guido van Rossum60456fd1997-04-09 17:36:32 +00001930static int
Tim Peterscba30e22003-02-01 06:24:36 +00001931save_pers(Picklerobject *self, PyObject *args, PyObject *f)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001932{
1933 PyObject *pid = 0;
1934 int size, res = -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001935
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001936 static char persid = PERSID, binpersid = BINPERSID;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001937
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001938 Py_INCREF(args);
1939 ARG_TUP(self, args);
1940 if (self->arg) {
1941 pid = PyObject_Call(f, self->arg, NULL);
1942 FREE_ARG_TUP(self);
1943 }
1944 if (! pid) return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001945
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001946 if (pid != Py_None) {
1947 if (!self->bin) {
1948 if (!PyString_Check(pid)) {
1949 PyErr_SetString(PicklingError,
1950 "persistent id must be string");
1951 goto finally;
1952 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001953
Tim Peters0bc93f52003-02-02 18:29:33 +00001954 if (self->write_func(self, &persid, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001955 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001956
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001957 if ((size = PyString_Size(pid)) < 0)
1958 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001959
Tim Peters0bc93f52003-02-02 18:29:33 +00001960 if (self->write_func(self,
1961 PyString_AS_STRING(
1962 (PyStringObject *)pid),
1963 size) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001964 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001965
Tim Peters0bc93f52003-02-02 18:29:33 +00001966 if (self->write_func(self, "\n", 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001967 goto finally;
Tim Peters84e87f32001-03-17 04:50:51 +00001968
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001969 res = 1;
1970 goto finally;
1971 }
1972 else if (save(self, pid, 1) >= 0) {
Tim Peters0bc93f52003-02-02 18:29:33 +00001973 if (self->write_func(self, &binpersid, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001974 res = -1;
1975 else
1976 res = 1;
1977 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001978
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001979 goto finally;
1980 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001981
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001982 res = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001983
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001984 finally:
1985 Py_XDECREF(pid);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001986
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001987 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001988}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001989
Tim Peters71fcda52003-02-14 23:05:28 +00001990/* We're saving ob, and args is the 2-thru-5 tuple returned by the
1991 * appropriate __reduce__ method for ob.
1992 */
Tim Peters84e87f32001-03-17 04:50:51 +00001993static int
Tim Peters71fcda52003-02-14 23:05:28 +00001994save_reduce(Picklerobject *self, PyObject *args, PyObject *ob)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001995{
Tim Peters71fcda52003-02-14 23:05:28 +00001996 PyObject *callable;
1997 PyObject *argtup;
1998 PyObject *state = NULL;
1999 PyObject *listitems = NULL;
2000 PyObject *dictitems = NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002001
Tim Peters71fcda52003-02-14 23:05:28 +00002002 int use_newobj = self->proto >= 2;
2003
2004 static char reduce = REDUCE;
2005 static char build = BUILD;
2006 static char newobj = NEWOBJ;
2007
2008 if (! PyArg_UnpackTuple(args, "save_reduce", 2, 5,
2009 &callable,
2010 &argtup,
2011 &state,
2012 &listitems,
2013 &dictitems))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002014 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002015
Raymond Hettingera6b45cc2004-12-07 07:05:57 +00002016 if (!PyTuple_Check(argtup)) {
2017 PyErr_SetString(PicklingError,
2018 "args from reduce() should be a tuple");
2019 return -1;
2020 }
2021
Tim Peters71fcda52003-02-14 23:05:28 +00002022 if (state == Py_None)
2023 state = NULL;
2024 if (listitems == Py_None)
2025 listitems = NULL;
2026 if (dictitems == Py_None)
2027 dictitems = NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002028
Tim Peters71fcda52003-02-14 23:05:28 +00002029 /* Protocol 2 special case: if callable's name is __newobj__, use
2030 * NEWOBJ. This consumes a lot of code.
2031 */
2032 if (use_newobj) {
2033 PyObject *temp = PyObject_GetAttr(callable, __name___str);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002034
Tim Peters71fcda52003-02-14 23:05:28 +00002035 if (temp == NULL) {
Jeremy Hyltonf00368f2003-06-16 20:19:49 +00002036 if (PyErr_ExceptionMatches(PyExc_AttributeError))
2037 PyErr_Clear();
2038 else
2039 return -1;
Tim Peters71fcda52003-02-14 23:05:28 +00002040 use_newobj = 0;
2041 }
2042 else {
2043 use_newobj = PyString_Check(temp) &&
2044 strcmp(PyString_AS_STRING(temp),
2045 "__newobj__") == 0;
2046 Py_DECREF(temp);
2047 }
2048 }
2049 if (use_newobj) {
2050 PyObject *cls;
2051 PyObject *newargtup;
2052 int n, i;
2053
2054 /* Sanity checks. */
2055 n = PyTuple_Size(argtup);
2056 if (n < 1) {
2057 PyErr_SetString(PicklingError, "__newobj__ arglist "
2058 "is empty");
2059 return -1;
2060 }
2061
2062 cls = PyTuple_GET_ITEM(argtup, 0);
2063 if (! PyObject_HasAttrString(cls, "__new__")) {
2064 PyErr_SetString(PicklingError, "args[0] from "
2065 "__newobj__ args has no __new__");
2066 return -1;
2067 }
2068
2069 /* XXX How could ob be NULL? */
2070 if (ob != NULL) {
2071 PyObject *ob_dot_class;
2072
2073 ob_dot_class = PyObject_GetAttr(ob, __class___str);
Jeremy Hyltonf00368f2003-06-16 20:19:49 +00002074 if (ob_dot_class == NULL) {
2075 if (PyErr_ExceptionMatches(
2076 PyExc_AttributeError))
2077 PyErr_Clear();
2078 else
2079 return -1;
2080 }
Tim Peters71fcda52003-02-14 23:05:28 +00002081 i = ob_dot_class != cls; /* true iff a problem */
2082 Py_XDECREF(ob_dot_class);
2083 if (i) {
2084 PyErr_SetString(PicklingError, "args[0] from "
2085 "__newobj__ args has the wrong class");
2086 return -1;
2087 }
2088 }
2089
2090 /* Save the class and its __new__ arguments. */
2091 if (save(self, cls, 0) < 0)
2092 return -1;
2093
2094 newargtup = PyTuple_New(n-1); /* argtup[1:] */
2095 if (newargtup == NULL)
2096 return -1;
2097 for (i = 1; i < n; ++i) {
2098 PyObject *temp = PyTuple_GET_ITEM(argtup, i);
2099 Py_INCREF(temp);
2100 PyTuple_SET_ITEM(newargtup, i-1, temp);
2101 }
2102 i = save(self, newargtup, 0) < 0;
2103 Py_DECREF(newargtup);
2104 if (i < 0)
2105 return -1;
2106
2107 /* Add NEWOBJ opcode. */
2108 if (self->write_func(self, &newobj, 1) < 0)
2109 return -1;
2110 }
2111 else {
2112 /* Not using NEWOBJ. */
2113 if (save(self, callable, 0) < 0 ||
2114 save(self, argtup, 0) < 0 ||
2115 self->write_func(self, &reduce, 1) < 0)
2116 return -1;
2117 }
2118
2119 /* Memoize. */
2120 /* XXX How can ob be NULL? */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002121 if (ob != NULL) {
2122 if (state && !PyDict_Check(state)) {
2123 if (put2(self, ob) < 0)
2124 return -1;
2125 }
Tim Peters71fcda52003-02-14 23:05:28 +00002126 else if (put(self, ob) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002127 return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002128 }
Tim Peters84e87f32001-03-17 04:50:51 +00002129
Guido van Rossum60456fd1997-04-09 17:36:32 +00002130
Tim Peters71fcda52003-02-14 23:05:28 +00002131 if (listitems && batch_list(self, listitems) < 0)
2132 return -1;
2133
2134 if (dictitems && batch_dict(self, dictitems) < 0)
2135 return -1;
2136
2137 if (state) {
2138 if (save(self, state, 0) < 0 ||
2139 self->write_func(self, &build, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002140 return -1;
2141 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002142
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002143 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002144}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002145
Guido van Rossum60456fd1997-04-09 17:36:32 +00002146static int
Tim Peters1d63c9f2003-02-02 20:29:39 +00002147save(Picklerobject *self, PyObject *args, int pers_save)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002148{
2149 PyTypeObject *type;
Tim Peters71fcda52003-02-14 23:05:28 +00002150 PyObject *py_ob_id = 0, *__reduce__ = 0, *t = 0;
2151 PyObject *arg_tup;
2152 int res = -1;
2153 int tmp, size;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002154
Martin v. Löwis5a395302002-08-04 08:20:23 +00002155 if (self->nesting++ > Py_GetRecursionLimit()){
2156 PyErr_SetString(PyExc_RuntimeError,
2157 "maximum recursion depth exceeded");
2158 goto finally;
2159 }
2160
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002161 if (!pers_save && self->pers_func) {
2162 if ((tmp = save_pers(self, args, self->pers_func)) != 0) {
2163 res = tmp;
2164 goto finally;
2165 }
2166 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002167
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002168 if (args == Py_None) {
2169 res = save_none(self, args);
2170 goto finally;
2171 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002172
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002173 type = args->ob_type;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002174
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002175 switch (type->tp_name[0]) {
Guido van Rossum77f6a652002-04-03 22:41:51 +00002176 case 'b':
2177 if (args == Py_False || args == Py_True) {
2178 res = save_bool(self, args);
2179 goto finally;
2180 }
2181 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002182 case 'i':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002183 if (type == &PyInt_Type) {
2184 res = save_int(self, args);
2185 goto finally;
2186 }
2187 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002188
Guido van Rossum60456fd1997-04-09 17:36:32 +00002189 case 'l':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002190 if (type == &PyLong_Type) {
2191 res = save_long(self, args);
2192 goto finally;
2193 }
2194 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002195
Guido van Rossum60456fd1997-04-09 17:36:32 +00002196 case 'f':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002197 if (type == &PyFloat_Type) {
2198 res = save_float(self, args);
2199 goto finally;
2200 }
2201 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002202
Guido van Rossum60456fd1997-04-09 17:36:32 +00002203 case 't':
Tim Peters1d63c9f2003-02-02 20:29:39 +00002204 if (type == &PyTuple_Type && PyTuple_Size(args) == 0) {
2205 res = save_tuple(self, args);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002206 goto finally;
2207 }
2208 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002209
Guido van Rossum60456fd1997-04-09 17:36:32 +00002210 case 's':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002211 if ((type == &PyString_Type) && (PyString_GET_SIZE(args) < 2)) {
2212 res = save_string(self, args, 0);
2213 goto finally;
2214 }
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00002215
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002216#ifdef Py_USING_UNICODE
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00002217 case 'u':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002218 if ((type == &PyUnicode_Type) && (PyString_GET_SIZE(args) < 2)) {
2219 res = save_unicode(self, args, 0);
2220 goto finally;
2221 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002222#endif
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002223 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002224
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002225 if (args->ob_refcnt > 1) {
Tim Peterscba30e22003-02-01 06:24:36 +00002226 if (!( py_ob_id = PyLong_FromVoidPtr(args)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002227 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002228
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002229 if (PyDict_GetItem(self->memo, py_ob_id)) {
2230 if (get(self, py_ob_id) < 0)
2231 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002232
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002233 res = 0;
2234 goto finally;
2235 }
2236 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002237
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002238 switch (type->tp_name[0]) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00002239 case 's':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002240 if (type == &PyString_Type) {
2241 res = save_string(self, args, 1);
2242 goto finally;
2243 }
2244 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002245
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002246#ifdef Py_USING_UNICODE
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00002247 case 'u':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002248 if (type == &PyUnicode_Type) {
2249 res = save_unicode(self, args, 1);
2250 goto finally;
2251 }
2252 break;
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002253#endif
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00002254
Guido van Rossum60456fd1997-04-09 17:36:32 +00002255 case 't':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002256 if (type == &PyTuple_Type) {
2257 res = save_tuple(self, args);
2258 goto finally;
2259 }
2260 if (type == &PyType_Type) {
2261 res = save_global(self, args, NULL);
2262 goto finally;
2263 }
2264 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002265
Guido van Rossum60456fd1997-04-09 17:36:32 +00002266 case 'l':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002267 if (type == &PyList_Type) {
2268 res = save_list(self, args);
2269 goto finally;
2270 }
2271 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002272
2273 case 'd':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002274 if (type == &PyDict_Type) {
2275 res = save_dict(self, args);
2276 goto finally;
2277 }
2278 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002279
Guido van Rossum512ab9f2006-08-17 22:28:49 +00002280 case 'i':
2281 break;
2282
2283 case 'c':
2284 break;
2285
Guido van Rossum60456fd1997-04-09 17:36:32 +00002286 case 'f':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002287 if (type == &PyFunction_Type) {
2288 res = save_global(self, args, NULL);
Christian Tismer2460c622004-02-26 16:21:45 +00002289 if (res && PyErr_ExceptionMatches(PickleError)) {
2290 /* fall back to reduce */
2291 PyErr_Clear();
2292 break;
2293 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002294 goto finally;
2295 }
2296 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002297
2298 case 'b':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002299 if (type == &PyCFunction_Type) {
2300 res = save_global(self, args, NULL);
2301 goto finally;
2302 }
2303 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002304
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002305 if (!pers_save && self->inst_pers_func) {
2306 if ((tmp = save_pers(self, args, self->inst_pers_func)) != 0) {
2307 res = tmp;
2308 goto finally;
2309 }
2310 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002311
Jeremy Hylton39c61162002-07-16 19:47:43 +00002312 if (PyType_IsSubtype(type, &PyType_Type)) {
2313 res = save_global(self, args, NULL);
2314 goto finally;
2315 }
2316
Guido van Rossumb289b872003-02-19 01:45:13 +00002317 /* Get a reduction callable, and call it. This may come from
2318 * copy_reg.dispatch_table, the object's __reduce_ex__ method,
2319 * or the object's __reduce__ method.
Tim Peters71fcda52003-02-14 23:05:28 +00002320 */
Tim Peters5aa3da62003-02-13 21:03:57 +00002321 __reduce__ = PyDict_GetItem(dispatch_table, (PyObject *)type);
2322 if (__reduce__ != NULL) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002323 Py_INCREF(__reduce__);
Guido van Rossumb289b872003-02-19 01:45:13 +00002324 Py_INCREF(args);
2325 ARG_TUP(self, args);
2326 if (self->arg) {
2327 t = PyObject_Call(__reduce__, self->arg, NULL);
2328 FREE_ARG_TUP(self);
2329 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002330 }
2331 else {
Guido van Rossumb289b872003-02-19 01:45:13 +00002332 /* Check for a __reduce_ex__ method. */
2333 __reduce__ = PyObject_GetAttr(args, __reduce_ex___str);
2334 if (__reduce__ != NULL) {
2335 t = PyInt_FromLong(self->proto);
2336 if (t != NULL) {
2337 ARG_TUP(self, t);
2338 t = NULL;
2339 if (self->arg) {
2340 t = PyObject_Call(__reduce__,
2341 self->arg, NULL);
2342 FREE_ARG_TUP(self);
2343 }
2344 }
2345 }
2346 else {
Jeremy Hyltonf00368f2003-06-16 20:19:49 +00002347 if (PyErr_ExceptionMatches(PyExc_AttributeError))
2348 PyErr_Clear();
2349 else
2350 goto finally;
Guido van Rossumb289b872003-02-19 01:45:13 +00002351 /* Check for a __reduce__ method. */
2352 __reduce__ = PyObject_GetAttr(args, __reduce___str);
2353 if (__reduce__ != NULL) {
2354 t = PyObject_Call(__reduce__,
2355 empty_tuple, NULL);
2356 }
2357 else {
2358 PyErr_SetObject(UnpickleableError, args);
2359 goto finally;
2360 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002361 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002362 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002363
Tim Peters71fcda52003-02-14 23:05:28 +00002364 if (t == NULL)
2365 goto finally;
Tim Peters84e87f32001-03-17 04:50:51 +00002366
Tim Peters71fcda52003-02-14 23:05:28 +00002367 if (PyString_Check(t)) {
2368 res = save_global(self, args, t);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002369 goto finally;
2370 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002371
Tim Peters71fcda52003-02-14 23:05:28 +00002372 if (! PyTuple_Check(t)) {
2373 cPickle_ErrFormat(PicklingError, "Value returned by "
2374 "%s must be string or tuple",
2375 "O", __reduce__);
2376 goto finally;
2377 }
2378
2379 size = PyTuple_Size(t);
2380 if (size < 2 || size > 5) {
2381 cPickle_ErrFormat(PicklingError, "tuple returned by "
2382 "%s must contain 2 through 5 elements",
2383 "O", __reduce__);
2384 goto finally;
2385 }
2386
2387 arg_tup = PyTuple_GET_ITEM(t, 1);
2388 if (!(PyTuple_Check(arg_tup) || arg_tup == Py_None)) {
2389 cPickle_ErrFormat(PicklingError, "Second element of "
2390 "tuple returned by %s must be a tuple",
2391 "O", __reduce__);
2392 goto finally;
2393 }
2394
2395 res = save_reduce(self, t, args);
Guido van Rossum60456fd1997-04-09 17:36:32 +00002396
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002397 finally:
Martin v. Löwis5a395302002-08-04 08:20:23 +00002398 self->nesting--;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002399 Py_XDECREF(py_ob_id);
2400 Py_XDECREF(__reduce__);
2401 Py_XDECREF(t);
Tim Peters84e87f32001-03-17 04:50:51 +00002402
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002403 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002404}
2405
2406
2407static int
Tim Peterscba30e22003-02-01 06:24:36 +00002408dump(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002409{
2410 static char stop = STOP;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002411
Tim Peters4190fb82003-02-02 16:09:05 +00002412 if (self->proto >= 2) {
2413 char bytes[2];
2414
2415 bytes[0] = PROTO;
Tim Peters87482ea2003-02-02 16:16:30 +00002416 assert(self->proto >= 0 && self->proto < 256);
2417 bytes[1] = (char)self->proto;
Tim Peters4190fb82003-02-02 16:09:05 +00002418 if (self->write_func(self, bytes, 2) < 0)
2419 return -1;
2420 }
2421
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002422 if (save(self, args, 0) < 0)
2423 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002424
Tim Peters4190fb82003-02-02 16:09:05 +00002425 if (self->write_func(self, &stop, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002426 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002427
Tim Peters4190fb82003-02-02 16:09:05 +00002428 if (self->write_func(self, NULL, 0) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002429 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002430
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002431 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002432}
2433
2434static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00002435Pickle_clear_memo(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002436{
Tim Peterscba30e22003-02-01 06:24:36 +00002437 if (self->memo)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002438 PyDict_Clear(self->memo);
2439 Py_INCREF(Py_None);
2440 return Py_None;
2441}
2442
2443static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00002444Pickle_getvalue(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002445{
2446 int l, i, rsize, ssize, clear=1, lm;
2447 long ik;
2448 PyObject *k, *r;
2449 char *s, *p, *have_get;
2450 Pdata *data;
2451
2452 /* Can be called by Python code or C code */
Tim Peterscba30e22003-02-01 06:24:36 +00002453 if (args && !PyArg_ParseTuple(args, "|i:getvalue", &clear))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002454 return NULL;
2455
2456 /* Check to make sure we are based on a list */
2457 if (! Pdata_Check(self->file)) {
2458 PyErr_SetString(PicklingError,
2459 "Attempt to getvalue() a non-list-based pickler");
2460 return NULL;
2461 }
2462
2463 /* flush write buffer */
2464 if (write_other(self, NULL, 0) < 0) return NULL;
2465
2466 data=(Pdata*)self->file;
2467 l=data->length;
2468
2469 /* set up an array to hold get/put status */
Tim Petersac5687a2003-02-02 18:08:34 +00002470 lm = PyDict_Size(self->memo);
2471 if (lm < 0) return NULL;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002472 lm++;
Tim Petersac5687a2003-02-02 18:08:34 +00002473 have_get = malloc(lm);
2474 if (have_get == NULL) return PyErr_NoMemory();
2475 memset(have_get, 0, lm);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002476
2477 /* Scan for gets. */
Tim Petersac5687a2003-02-02 18:08:34 +00002478 for (rsize = 0, i = l; --i >= 0; ) {
2479 k = data->data[i];
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002480
Tim Petersac5687a2003-02-02 18:08:34 +00002481 if (PyString_Check(k))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002482 rsize += PyString_GET_SIZE(k);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002483
2484 else if (PyInt_Check(k)) { /* put */
Tim Petersac5687a2003-02-02 18:08:34 +00002485 ik = PyInt_AS_LONG((PyIntObject*)k);
2486 if (ik >= lm || ik == 0) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002487 PyErr_SetString(PicklingError,
2488 "Invalid get data");
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002489 goto err;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002490 }
Tim Petersac5687a2003-02-02 18:08:34 +00002491 if (have_get[ik]) /* with matching get */
2492 rsize += ik < 256 ? 2 : 5;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002493 }
2494
2495 else if (! (PyTuple_Check(k) &&
2496 PyTuple_GET_SIZE(k) == 2 &&
Tim Petersac5687a2003-02-02 18:08:34 +00002497 PyInt_Check((k = PyTuple_GET_ITEM(k, 0))))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002498 ) {
2499 PyErr_SetString(PicklingError,
2500 "Unexpected data in internal list");
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002501 goto err;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002502 }
2503
2504 else { /* put */
Tim Petersac5687a2003-02-02 18:08:34 +00002505 ik = PyInt_AS_LONG((PyIntObject *)k);
2506 if (ik >= lm || ik == 0) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002507 PyErr_SetString(PicklingError,
2508 "Invalid get data");
2509 return NULL;
2510 }
Tim Petersac5687a2003-02-02 18:08:34 +00002511 have_get[ik] = 1;
2512 rsize += ik < 256 ? 2 : 5;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002513 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002514 }
2515
2516 /* Now generate the result */
Tim Petersac5687a2003-02-02 18:08:34 +00002517 r = PyString_FromStringAndSize(NULL, rsize);
2518 if (r == NULL) goto err;
2519 s = PyString_AS_STRING((PyStringObject *)r);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002520
Tim Petersac5687a2003-02-02 18:08:34 +00002521 for (i = 0; i < l; i++) {
2522 k = data->data[i];
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002523
2524 if (PyString_Check(k)) {
Tim Petersac5687a2003-02-02 18:08:34 +00002525 ssize = PyString_GET_SIZE(k);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002526 if (ssize) {
Tim Petersac5687a2003-02-02 18:08:34 +00002527 p=PyString_AS_STRING((PyStringObject *)k);
2528 while (--ssize >= 0)
2529 *s++ = *p++;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002530 }
2531 }
2532
2533 else if (PyTuple_Check(k)) { /* get */
Tim Petersac5687a2003-02-02 18:08:34 +00002534 ik = PyInt_AS_LONG((PyIntObject *)
2535 PyTuple_GET_ITEM(k, 0));
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002536 if (ik < 256) {
2537 *s++ = BINGET;
2538 *s++ = (int)(ik & 0xff);
2539 }
2540 else {
2541 *s++ = LONG_BINGET;
2542 *s++ = (int)(ik & 0xff);
2543 *s++ = (int)((ik >> 8) & 0xff);
2544 *s++ = (int)((ik >> 16) & 0xff);
2545 *s++ = (int)((ik >> 24) & 0xff);
2546 }
2547 }
2548
2549 else { /* put */
Tim Petersac5687a2003-02-02 18:08:34 +00002550 ik = PyInt_AS_LONG((PyIntObject*)k);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002551
2552 if (have_get[ik]) { /* with matching get */
2553 if (ik < 256) {
2554 *s++ = BINPUT;
2555 *s++ = (int)(ik & 0xff);
2556 }
2557 else {
2558 *s++ = LONG_BINPUT;
2559 *s++ = (int)(ik & 0xff);
2560 *s++ = (int)((ik >> 8) & 0xff);
2561 *s++ = (int)((ik >> 16) & 0xff);
2562 *s++ = (int)((ik >> 24) & 0xff);
2563 }
2564 }
2565 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002566 }
2567
2568 if (clear) {
2569 PyDict_Clear(self->memo);
Tim Petersac5687a2003-02-02 18:08:34 +00002570 Pdata_clear(data, 0);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002571 }
2572
2573 free(have_get);
2574 return r;
2575 err:
2576 free(have_get);
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00002577 return NULL;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002578}
2579
2580static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00002581Pickler_dump(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002582{
2583 PyObject *ob;
2584 int get=0;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002585
Tim Peterscba30e22003-02-01 06:24:36 +00002586 if (!( PyArg_ParseTuple(args, "O|i:dump", &ob, &get)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002587 return NULL;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002588
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002589 if (dump(self, ob) < 0)
2590 return NULL;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002591
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002592 if (get) return Pickle_getvalue(self, NULL);
Guido van Rossum053b8df1998-11-25 16:18:00 +00002593
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002594 /* XXX Why does dump() return self? */
2595 Py_INCREF(self);
2596 return (PyObject*)self;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002597}
2598
2599
Tim Peterscba30e22003-02-01 06:24:36 +00002600static struct PyMethodDef Pickler_methods[] =
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002601{
Neal Norwitzb0493252002-03-31 14:44:22 +00002602 {"dump", (PyCFunction)Pickler_dump, METH_VARARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00002603 PyDoc_STR("dump(object) -- "
2604 "Write an object in pickle format to the object's pickle stream")},
Fred Drake0ebacc82002-05-01 20:36:39 +00002605 {"clear_memo", (PyCFunction)Pickle_clear_memo, METH_NOARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00002606 PyDoc_STR("clear_memo() -- Clear the picklers memo")},
Neal Norwitzb0493252002-03-31 14:44:22 +00002607 {"getvalue", (PyCFunction)Pickle_getvalue, METH_VARARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00002608 PyDoc_STR("getvalue() -- Finish picking a list-based pickle")},
Guido van Rossum60456fd1997-04-09 17:36:32 +00002609 {NULL, NULL} /* sentinel */
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002610};
2611
2612
2613static Picklerobject *
Tim Peters5bd2a792003-02-01 16:45:06 +00002614newPicklerobject(PyObject *file, int proto)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002615{
2616 Picklerobject *self;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002617
Tim Peters5bd2a792003-02-01 16:45:06 +00002618 if (proto < 0)
Tim Peters8587b3c2003-02-13 15:44:41 +00002619 proto = HIGHEST_PROTOCOL;
2620 if (proto > HIGHEST_PROTOCOL) {
Tim Peters5bd2a792003-02-01 16:45:06 +00002621 PyErr_Format(PyExc_ValueError, "pickle protocol %d asked for; "
2622 "the highest available protocol is %d",
Tim Peters8587b3c2003-02-13 15:44:41 +00002623 proto, HIGHEST_PROTOCOL);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002624 return NULL;
Tim Peters5bd2a792003-02-01 16:45:06 +00002625 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002626
Jeremy Hylton4cf63192003-04-09 21:05:12 +00002627 self = PyObject_GC_New(Picklerobject, &Picklertype);
Tim Peters5bd2a792003-02-01 16:45:06 +00002628 if (self == NULL)
2629 return NULL;
2630 self->proto = proto;
2631 self->bin = proto > 0;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002632 self->fp = NULL;
2633 self->write = NULL;
2634 self->memo = NULL;
2635 self->arg = NULL;
2636 self->pers_func = NULL;
2637 self->inst_pers_func = NULL;
2638 self->write_buf = NULL;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002639 self->fast = 0;
Martin v. Löwis5a395302002-08-04 08:20:23 +00002640 self->nesting = 0;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002641 self->fast_container = 0;
2642 self->fast_memo = NULL;
2643 self->buf_size = 0;
2644 self->dispatch_table = NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002645
Tim Peters5bd2a792003-02-01 16:45:06 +00002646 self->file = NULL;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002647 if (file)
2648 Py_INCREF(file);
Tim Peters5bd2a792003-02-01 16:45:06 +00002649 else {
2650 file = Pdata_New();
2651 if (file == NULL)
2652 goto err;
2653 }
2654 self->file = file;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002655
Tim Peterscba30e22003-02-01 06:24:36 +00002656 if (!( self->memo = PyDict_New()))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002657 goto err;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002658
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002659 if (PyFile_Check(file)) {
2660 self->fp = PyFile_AsFile(file);
2661 if (self->fp == NULL) {
Tim Peters5bd2a792003-02-01 16:45:06 +00002662 PyErr_SetString(PyExc_ValueError,
2663 "I/O operation on closed file");
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002664 goto err;
2665 }
2666 self->write_func = write_file;
Guido van Rossumc91fcaa1999-03-29 20:00:14 +00002667 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002668 else if (PycStringIO_OutputCheck(file)) {
2669 self->write_func = write_cStringIO;
2670 }
2671 else if (file == Py_None) {
2672 self->write_func = write_none;
2673 }
2674 else {
2675 self->write_func = write_other;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002676
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002677 if (! Pdata_Check(file)) {
2678 self->write = PyObject_GetAttr(file, write_str);
2679 if (!self->write) {
2680 PyErr_Clear();
Tim Peterscba30e22003-02-01 06:24:36 +00002681 PyErr_SetString(PyExc_TypeError,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002682 "argument must have 'write' "
2683 "attribute");
2684 goto err;
2685 }
2686 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002687
Tim Peters5bd2a792003-02-01 16:45:06 +00002688 self->write_buf = (char *)PyMem_Malloc(WRITE_BUF_SIZE);
2689 if (self->write_buf == NULL) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002690 PyErr_NoMemory();
2691 goto err;
2692 }
2693 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002694
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002695 if (PyEval_GetRestricted()) {
2696 /* Restricted execution, get private tables */
Tim Peters5b7da392003-02-04 00:21:07 +00002697 PyObject *m = PyImport_Import(copy_reg_str);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002698
Tim Peters5b7da392003-02-04 00:21:07 +00002699 if (m == NULL)
2700 goto err;
2701 self->dispatch_table = PyObject_GetAttr(m, dispatch_table_str);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002702 Py_DECREF(m);
Tim Peters5b7da392003-02-04 00:21:07 +00002703 if (self->dispatch_table == NULL)
2704 goto err;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002705 }
2706 else {
Tim Peters5b7da392003-02-04 00:21:07 +00002707 self->dispatch_table = dispatch_table;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002708 Py_INCREF(dispatch_table);
2709 }
Jeremy Hylton4cf63192003-04-09 21:05:12 +00002710 PyObject_GC_Track(self);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002711
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002712 return self;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002713
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002714 err:
Tim Peters5bd2a792003-02-01 16:45:06 +00002715 Py_DECREF(self);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002716 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002717}
2718
2719
2720static PyObject *
Martin v. Löwis544f1192004-07-27 05:22:33 +00002721get_Pickler(PyObject *self, PyObject *args, PyObject *kwds)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002722{
Martin v. Löwis15e62742006-02-27 16:46:16 +00002723 static char *kwlist[] = {"file", "protocol", NULL};
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002724 PyObject *file = NULL;
Tim Peters5bd2a792003-02-01 16:45:06 +00002725 int proto = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002726
Tim Peters92c8bb32003-02-13 23:00:26 +00002727 /* XXX
Martin v. Löwis544f1192004-07-27 05:22:33 +00002728 * The documented signature is Pickler(file, protocol=0), but this
Tim Peters92c8bb32003-02-13 23:00:26 +00002729 * accepts Pickler() and Pickler(integer) too. The meaning then
2730 * is clear as mud, undocumented, and not supported by pickle.py.
2731 * I'm told Zope uses this, but I haven't traced into this code
2732 * far enough to figure out what it means.
Tim Peters5bd2a792003-02-01 16:45:06 +00002733 */
2734 if (!PyArg_ParseTuple(args, "|i:Pickler", &proto)) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002735 PyErr_Clear();
Tim Peters5bd2a792003-02-01 16:45:06 +00002736 proto = 0;
Martin v. Löwis544f1192004-07-27 05:22:33 +00002737 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|i:Pickler",
2738 kwlist, &file, &proto))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002739 return NULL;
2740 }
Tim Peters5bd2a792003-02-01 16:45:06 +00002741 return (PyObject *)newPicklerobject(file, proto);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002742}
2743
2744
2745static void
Tim Peterscba30e22003-02-01 06:24:36 +00002746Pickler_dealloc(Picklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002747{
Jeremy Hylton4cf63192003-04-09 21:05:12 +00002748 PyObject_GC_UnTrack(self);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002749 Py_XDECREF(self->write);
2750 Py_XDECREF(self->memo);
2751 Py_XDECREF(self->fast_memo);
2752 Py_XDECREF(self->arg);
2753 Py_XDECREF(self->file);
2754 Py_XDECREF(self->pers_func);
2755 Py_XDECREF(self->inst_pers_func);
2756 Py_XDECREF(self->dispatch_table);
Tim Peters5bd2a792003-02-01 16:45:06 +00002757 PyMem_Free(self->write_buf);
Tim Peters3cfe7542003-05-21 21:29:48 +00002758 self->ob_type->tp_free((PyObject *)self);
Jeremy Hylton4cf63192003-04-09 21:05:12 +00002759}
2760
2761static int
2762Pickler_traverse(Picklerobject *self, visitproc visit, void *arg)
2763{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002764 Py_VISIT(self->write);
2765 Py_VISIT(self->memo);
2766 Py_VISIT(self->fast_memo);
2767 Py_VISIT(self->arg);
2768 Py_VISIT(self->file);
2769 Py_VISIT(self->pers_func);
2770 Py_VISIT(self->inst_pers_func);
2771 Py_VISIT(self->dispatch_table);
Jeremy Hylton4cf63192003-04-09 21:05:12 +00002772 return 0;
2773}
2774
2775static int
2776Pickler_clear(Picklerobject *self)
2777{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002778 Py_CLEAR(self->write);
2779 Py_CLEAR(self->memo);
2780 Py_CLEAR(self->fast_memo);
2781 Py_CLEAR(self->arg);
2782 Py_CLEAR(self->file);
2783 Py_CLEAR(self->pers_func);
2784 Py_CLEAR(self->inst_pers_func);
2785 Py_CLEAR(self->dispatch_table);
Jeremy Hylton4cf63192003-04-09 21:05:12 +00002786 return 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002787}
2788
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002789static PyObject *
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00002790Pickler_get_pers_func(Picklerobject *p)
2791{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002792 if (p->pers_func == NULL)
2793 PyErr_SetString(PyExc_AttributeError, "persistent_id");
2794 else
2795 Py_INCREF(p->pers_func);
2796 return p->pers_func;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002797}
2798
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00002799static int
2800Pickler_set_pers_func(Picklerobject *p, PyObject *v)
2801{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002802 if (v == NULL) {
2803 PyErr_SetString(PyExc_TypeError,
2804 "attribute deletion is not supported");
2805 return -1;
2806 }
2807 Py_XDECREF(p->pers_func);
2808 Py_INCREF(v);
2809 p->pers_func = v;
2810 return 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002811}
2812
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00002813static int
2814Pickler_set_inst_pers_func(Picklerobject *p, PyObject *v)
2815{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002816 if (v == NULL) {
2817 PyErr_SetString(PyExc_TypeError,
2818 "attribute deletion is not supported");
2819 return -1;
2820 }
2821 Py_XDECREF(p->inst_pers_func);
2822 Py_INCREF(v);
2823 p->inst_pers_func = v;
2824 return 0;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00002825}
2826
2827static PyObject *
2828Pickler_get_memo(Picklerobject *p)
2829{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002830 if (p->memo == NULL)
2831 PyErr_SetString(PyExc_AttributeError, "memo");
2832 else
2833 Py_INCREF(p->memo);
2834 return p->memo;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00002835}
2836
2837static int
2838Pickler_set_memo(Picklerobject *p, PyObject *v)
2839{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002840 if (v == NULL) {
2841 PyErr_SetString(PyExc_TypeError,
2842 "attribute deletion is not supported");
2843 return -1;
2844 }
2845 if (!PyDict_Check(v)) {
2846 PyErr_SetString(PyExc_TypeError, "memo must be a dictionary");
2847 return -1;
2848 }
2849 Py_XDECREF(p->memo);
2850 Py_INCREF(v);
2851 p->memo = v;
2852 return 0;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00002853}
2854
2855static PyObject *
2856Pickler_get_error(Picklerobject *p)
2857{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002858 /* why is this an attribute on the Pickler? */
2859 Py_INCREF(PicklingError);
2860 return PicklingError;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00002861}
2862
2863static PyMemberDef Pickler_members[] = {
2864 {"binary", T_INT, offsetof(Picklerobject, bin)},
2865 {"fast", T_INT, offsetof(Picklerobject, fast)},
Jeremy Hylton3eb46f32001-10-16 17:10:49 +00002866 {NULL}
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00002867};
2868
2869static PyGetSetDef Pickler_getsets[] = {
Tim Peterscba30e22003-02-01 06:24:36 +00002870 {"persistent_id", (getter)Pickler_get_pers_func,
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00002871 (setter)Pickler_set_pers_func},
2872 {"inst_persistent_id", NULL, (setter)Pickler_set_inst_pers_func},
2873 {"memo", (getter)Pickler_get_memo, (setter)Pickler_set_memo},
Jeremy Hylton3eb46f32001-10-16 17:10:49 +00002874 {"PicklingError", (getter)Pickler_get_error, NULL},
2875 {NULL}
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00002876};
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002877
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002878PyDoc_STRVAR(Picklertype__doc__,
2879"Objects that know how to pickle objects\n");
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002880
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002881static PyTypeObject Picklertype = {
2882 PyObject_HEAD_INIT(NULL)
Guido van Rossum60456fd1997-04-09 17:36:32 +00002883 0, /*ob_size*/
Guido van Rossum14648392001-12-08 18:02:58 +00002884 "cPickle.Pickler", /*tp_name*/
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00002885 sizeof(Picklerobject), /*tp_basicsize*/
2886 0,
2887 (destructor)Pickler_dealloc, /* tp_dealloc */
2888 0, /* tp_print */
2889 0, /* tp_getattr */
2890 0, /* tp_setattr */
2891 0, /* tp_compare */
2892 0, /* tp_repr */
2893 0, /* tp_as_number */
2894 0, /* tp_as_sequence */
2895 0, /* tp_as_mapping */
2896 0, /* tp_hash */
2897 0, /* tp_call */
2898 0, /* tp_str */
Jason Tishlerfb8595d2003-01-06 12:41:26 +00002899 PyObject_GenericGetAttr, /* tp_getattro */
2900 PyObject_GenericSetAttr, /* tp_setattro */
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00002901 0, /* tp_as_buffer */
Jeremy Hylton4cf63192003-04-09 21:05:12 +00002902 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00002903 Picklertype__doc__, /* tp_doc */
Jeremy Hylton4cf63192003-04-09 21:05:12 +00002904 (traverseproc)Pickler_traverse, /* tp_traverse */
2905 (inquiry)Pickler_clear, /* tp_clear */
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00002906 0, /* tp_richcompare */
2907 0, /* tp_weaklistoffset */
2908 0, /* tp_iter */
2909 0, /* tp_iternext */
2910 Pickler_methods, /* tp_methods */
2911 Pickler_members, /* tp_members */
2912 Pickler_getsets, /* tp_getset */
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002913};
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002914
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002915static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00002916find_class(PyObject *py_module_name, PyObject *py_global_name, PyObject *fc)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002917{
2918 PyObject *global = 0, *module;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002919
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002920 if (fc) {
2921 if (fc==Py_None) {
Tim Peters92c8bb32003-02-13 23:00:26 +00002922 PyErr_SetString(UnpicklingError, "Global and instance "
2923 "pickles are not supported.");
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002924 return NULL;
2925 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00002926 return PyObject_CallFunctionObjArgs(fc, py_module_name,
2927 py_global_name, NULL);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002928 }
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00002929
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002930 module = PySys_GetObject("modules");
2931 if (module == NULL)
2932 return NULL;
Jeremy Hyltond1055231998-08-11 19:52:51 +00002933
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002934 module = PyDict_GetItem(module, py_module_name);
2935 if (module == NULL) {
2936 module = PyImport_Import(py_module_name);
2937 if (!module)
2938 return NULL;
2939 global = PyObject_GetAttr(module, py_global_name);
2940 Py_DECREF(module);
2941 }
2942 else
2943 global = PyObject_GetAttr(module, py_global_name);
2944 return global;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002945}
2946
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002947static int
Tim Peterscba30e22003-02-01 06:24:36 +00002948marker(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002949{
2950 if (self->num_marks < 1) {
2951 PyErr_SetString(UnpicklingError, "could not find MARK");
2952 return -1;
2953 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002954
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002955 return self->marks[--self->num_marks];
Guido van Rossum60456fd1997-04-09 17:36:32 +00002956}
2957
Tim Peters84e87f32001-03-17 04:50:51 +00002958
Guido van Rossum60456fd1997-04-09 17:36:32 +00002959static int
Tim Peterscba30e22003-02-01 06:24:36 +00002960load_none(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002961{
2962 PDATA_APPEND(self->stack, Py_None, -1);
2963 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002964}
2965
Guido van Rossumf9ffb031999-02-04 14:54:04 +00002966static int
Tim Peterscba30e22003-02-01 06:24:36 +00002967bad_readline(void)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002968{
2969 PyErr_SetString(UnpicklingError, "pickle data was truncated");
2970 return -1;
Guido van Rossumf9ffb031999-02-04 14:54:04 +00002971}
Guido van Rossum60456fd1997-04-09 17:36:32 +00002972
2973static int
Tim Peterscba30e22003-02-01 06:24:36 +00002974load_int(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002975{
2976 PyObject *py_int = 0;
2977 char *endptr, *s;
2978 int len, res = -1;
2979 long l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002980
Tim Peters0bc93f52003-02-02 18:29:33 +00002981 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002982 if (len < 2) return bad_readline();
2983 if (!( s=pystrndup(s,len))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002984
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002985 errno = 0;
2986 l = strtol(s, &endptr, 0);
Guido van Rossum60456fd1997-04-09 17:36:32 +00002987
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002988 if (errno || (*endptr != '\n') || (endptr[1] != '\0')) {
2989 /* Hm, maybe we've got something long. Let's try reading
2990 it as a Python long object. */
2991 errno = 0;
2992 py_int = PyLong_FromString(s, NULL, 0);
2993 if (py_int == NULL) {
2994 PyErr_SetString(PyExc_ValueError,
2995 "could not convert string to int");
2996 goto finally;
2997 }
2998 }
2999 else {
Guido van Rossume2763392002-04-05 19:30:08 +00003000 if (len == 3 && (l == 0 || l == 1)) {
3001 if (!( py_int = PyBool_FromLong(l))) goto finally;
3002 }
3003 else {
3004 if (!( py_int = PyInt_FromLong(l))) goto finally;
3005 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003006 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003007
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003008 free(s);
3009 PDATA_PUSH(self->stack, py_int, -1);
3010 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003011
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003012 finally:
3013 free(s);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003014
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003015 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003016}
3017
Tim Peters3c67d792003-02-02 17:59:11 +00003018static int
3019load_bool(Unpicklerobject *self, PyObject *boolean)
3020{
3021 assert(boolean == Py_True || boolean == Py_False);
Tim Peterse0a39072003-02-03 15:45:56 +00003022 PDATA_APPEND(self->stack, boolean, -1);
Tim Peters3c67d792003-02-02 17:59:11 +00003023 return 0;
3024}
3025
Tim Petersee1a53c2003-02-02 02:57:53 +00003026/* s contains x bytes of a little-endian integer. Return its value as a
3027 * C int. Obscure: when x is 1 or 2, this is an unsigned little-endian
3028 * int, but when x is 4 it's a signed one. This is an historical source
3029 * of x-platform bugs.
3030 */
Tim Peters84e87f32001-03-17 04:50:51 +00003031static long
Tim Petersee1a53c2003-02-02 02:57:53 +00003032calc_binint(char *s, int x)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003033{
3034 unsigned char c;
3035 int i;
3036 long l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003037
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003038 for (i = 0, l = 0L; i < x; i++) {
3039 c = (unsigned char)s[i];
3040 l |= (long)c << (i * 8);
3041 }
Tim Petersbfa18f72001-04-10 01:54:42 +00003042#if SIZEOF_LONG > 4
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003043 /* Unlike BININT1 and BININT2, BININT (more accurately BININT4)
3044 * is signed, so on a box with longs bigger than 4 bytes we need
3045 * to extend a BININT's sign bit to the full width.
3046 */
3047 if (x == 4 && l & (1L << 31))
3048 l |= (~0L) << 32;
Tim Petersbfa18f72001-04-10 01:54:42 +00003049#endif
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003050 return l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003051}
3052
3053
3054static int
Tim Peterscba30e22003-02-01 06:24:36 +00003055load_binintx(Unpicklerobject *self, char *s, int x)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003056{
3057 PyObject *py_int = 0;
3058 long l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003059
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003060 l = calc_binint(s, x);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003061
Tim Peterscba30e22003-02-01 06:24:36 +00003062 if (!( py_int = PyInt_FromLong(l)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003063 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003064
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003065 PDATA_PUSH(self->stack, py_int, -1);
3066 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003067}
3068
3069
3070static int
Tim Peterscba30e22003-02-01 06:24:36 +00003071load_binint(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003072{
3073 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003074
Tim Peters0bc93f52003-02-02 18:29:33 +00003075 if (self->read_func(self, &s, 4) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003076 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003077
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003078 return load_binintx(self, s, 4);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003079}
3080
3081
3082static int
Tim Peterscba30e22003-02-01 06:24:36 +00003083load_binint1(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003084{
3085 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003086
Tim Peters0bc93f52003-02-02 18:29:33 +00003087 if (self->read_func(self, &s, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003088 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003089
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003090 return load_binintx(self, s, 1);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003091}
3092
3093
3094static int
Tim Peterscba30e22003-02-01 06:24:36 +00003095load_binint2(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003096{
3097 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003098
Tim Peters0bc93f52003-02-02 18:29:33 +00003099 if (self->read_func(self, &s, 2) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003100 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003101
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003102 return load_binintx(self, s, 2);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003103}
Tim Peters84e87f32001-03-17 04:50:51 +00003104
Guido van Rossum60456fd1997-04-09 17:36:32 +00003105static int
Tim Peterscba30e22003-02-01 06:24:36 +00003106load_long(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003107{
3108 PyObject *l = 0;
3109 char *end, *s;
3110 int len, res = -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003111
Tim Peters0bc93f52003-02-02 18:29:33 +00003112 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003113 if (len < 2) return bad_readline();
3114 if (!( s=pystrndup(s,len))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003115
Tim Peterscba30e22003-02-01 06:24:36 +00003116 if (!( l = PyLong_FromString(s, &end, 0)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003117 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003118
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003119 free(s);
3120 PDATA_PUSH(self->stack, l, -1);
3121 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003122
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003123 finally:
3124 free(s);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003125
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003126 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003127}
3128
Tim Petersee1a53c2003-02-02 02:57:53 +00003129/* 'size' bytes contain the # of bytes of little-endian 256's-complement
3130 * data following.
3131 */
3132static int
3133load_counted_long(Unpicklerobject *self, int size)
3134{
Martin v. Löwis18e16552006-02-15 17:27:45 +00003135 Py_ssize_t i;
Tim Petersee1a53c2003-02-02 02:57:53 +00003136 char *nbytes;
3137 unsigned char *pdata;
3138 PyObject *along;
3139
3140 assert(size == 1 || size == 4);
3141 i = self->read_func(self, &nbytes, size);
3142 if (i < 0) return -1;
3143
3144 size = calc_binint(nbytes, size);
3145 if (size < 0) {
3146 /* Corrupt or hostile pickle -- we never write one like
3147 * this.
3148 */
Tim Peters0c7c48e2003-02-03 22:07:24 +00003149 PyErr_SetString(UnpicklingError, "LONG pickle has negative "
Tim Petersee1a53c2003-02-02 02:57:53 +00003150 "byte count");
3151 return -1;
3152 }
3153
3154 if (size == 0)
3155 along = PyLong_FromLong(0L);
3156 else {
3157 /* Read the raw little-endian bytes & convert. */
Neal Norwitz529baf22003-02-02 17:08:33 +00003158 i = self->read_func(self, (char **)&pdata, size);
Tim Petersee1a53c2003-02-02 02:57:53 +00003159 if (i < 0) return -1;
3160 along = _PyLong_FromByteArray(pdata, (size_t)size,
3161 1 /* little endian */, 1 /* signed */);
3162 }
3163 if (along == NULL)
3164 return -1;
3165 PDATA_PUSH(self->stack, along, -1);
3166 return 0;
3167}
Tim Peters84e87f32001-03-17 04:50:51 +00003168
Guido van Rossum60456fd1997-04-09 17:36:32 +00003169static int
Tim Peterscba30e22003-02-01 06:24:36 +00003170load_float(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003171{
3172 PyObject *py_float = 0;
3173 char *endptr, *s;
3174 int len, res = -1;
3175 double d;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003176
Tim Peters0bc93f52003-02-02 18:29:33 +00003177 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003178 if (len < 2) return bad_readline();
3179 if (!( s=pystrndup(s,len))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003180
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003181 errno = 0;
Martin v. Löwis737ea822004-06-08 18:52:54 +00003182 d = PyOS_ascii_strtod(s, &endptr);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003183
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003184 if (errno || (endptr[0] != '\n') || (endptr[1] != '\0')) {
3185 PyErr_SetString(PyExc_ValueError,
3186 "could not convert string to float");
3187 goto finally;
3188 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003189
Tim Peterscba30e22003-02-01 06:24:36 +00003190 if (!( py_float = PyFloat_FromDouble(d)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003191 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003192
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003193 free(s);
3194 PDATA_PUSH(self->stack, py_float, -1);
3195 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003196
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003197 finally:
3198 free(s);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003199
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003200 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003201}
3202
Guido van Rossum60456fd1997-04-09 17:36:32 +00003203static int
Tim Peterscba30e22003-02-01 06:24:36 +00003204load_binfloat(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003205{
Tim Peters9905b942003-03-20 20:53:32 +00003206 PyObject *py_float;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003207 double x;
3208 char *p;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003209
Tim Peters0bc93f52003-02-02 18:29:33 +00003210 if (self->read_func(self, &p, 8) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003211 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003212
Tim Peters9905b942003-03-20 20:53:32 +00003213 x = _PyFloat_Unpack8((unsigned char *)p, 0);
3214 if (x == -1.0 && PyErr_Occurred())
3215 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003216
Tim Peters9905b942003-03-20 20:53:32 +00003217 py_float = PyFloat_FromDouble(x);
3218 if (py_float == NULL)
3219 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003220
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003221 PDATA_PUSH(self->stack, py_float, -1);
3222 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003223}
Guido van Rossum60456fd1997-04-09 17:36:32 +00003224
3225static int
Tim Peterscba30e22003-02-01 06:24:36 +00003226load_string(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003227{
3228 PyObject *str = 0;
Martin v. Löwis8a8da792002-08-14 07:46:28 +00003229 int len, res = -1;
3230 char *s, *p;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003231
Tim Peters0bc93f52003-02-02 18:29:33 +00003232 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003233 if (len < 2) return bad_readline();
3234 if (!( s=pystrndup(s,len))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003235
Martin v. Löwis8a8da792002-08-14 07:46:28 +00003236
3237 /* Strip outermost quotes */
3238 while (s[len-1] <= ' ')
3239 len--;
3240 if(s[0]=='"' && s[len-1]=='"'){
3241 s[len-1] = '\0';
3242 p = s + 1 ;
3243 len -= 2;
3244 } else if(s[0]=='\'' && s[len-1]=='\''){
3245 s[len-1] = '\0';
3246 p = s + 1 ;
3247 len -= 2;
3248 } else
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003249 goto insecure;
3250 /********************************************/
Guido van Rossum9716aaa1997-12-08 15:15:16 +00003251
Martin v. Löwis8a8da792002-08-14 07:46:28 +00003252 str = PyString_DecodeEscape(p, len, NULL, 0, NULL);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003253 free(s);
Martin v. Löwis8a8da792002-08-14 07:46:28 +00003254 if (str) {
3255 PDATA_PUSH(self->stack, str, -1);
3256 res = 0;
3257 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003258 return res;
Tim Peters84e87f32001-03-17 04:50:51 +00003259
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003260 insecure:
3261 free(s);
3262 PyErr_SetString(PyExc_ValueError,"insecure string pickle");
3263 return -1;
Tim Peters84e87f32001-03-17 04:50:51 +00003264}
Guido van Rossum60456fd1997-04-09 17:36:32 +00003265
3266
3267static int
Tim Peterscba30e22003-02-01 06:24:36 +00003268load_binstring(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003269{
3270 PyObject *py_string = 0;
3271 long l;
3272 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003273
Tim Peters0bc93f52003-02-02 18:29:33 +00003274 if (self->read_func(self, &s, 4) < 0) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003275
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003276 l = calc_binint(s, 4);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003277
Tim Peters0bc93f52003-02-02 18:29:33 +00003278 if (self->read_func(self, &s, l) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003279 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003280
Tim Peterscba30e22003-02-01 06:24:36 +00003281 if (!( py_string = PyString_FromStringAndSize(s, l)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003282 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003283
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003284 PDATA_PUSH(self->stack, py_string, -1);
3285 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003286}
3287
3288
3289static int
Tim Peterscba30e22003-02-01 06:24:36 +00003290load_short_binstring(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003291{
3292 PyObject *py_string = 0;
3293 unsigned char l;
3294 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003295
Tim Peters0bc93f52003-02-02 18:29:33 +00003296 if (self->read_func(self, &s, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003297 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003298
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003299 l = (unsigned char)s[0];
Guido van Rossum60456fd1997-04-09 17:36:32 +00003300
Tim Peters0bc93f52003-02-02 18:29:33 +00003301 if (self->read_func(self, &s, l) < 0) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003302
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003303 if (!( py_string = PyString_FromStringAndSize(s, l))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003304
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003305 PDATA_PUSH(self->stack, py_string, -1);
3306 return 0;
Tim Peters84e87f32001-03-17 04:50:51 +00003307}
Guido van Rossum60456fd1997-04-09 17:36:32 +00003308
3309
Martin v. Löwis339d0f72001-08-17 18:39:25 +00003310#ifdef Py_USING_UNICODE
Guido van Rossum60456fd1997-04-09 17:36:32 +00003311static int
Tim Peterscba30e22003-02-01 06:24:36 +00003312load_unicode(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003313{
3314 PyObject *str = 0;
3315 int len, res = -1;
3316 char *s;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003317
Tim Peters0bc93f52003-02-02 18:29:33 +00003318 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003319 if (len < 1) return bad_readline();
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003320
Tim Peterscba30e22003-02-01 06:24:36 +00003321 if (!( str = PyUnicode_DecodeRawUnicodeEscape(s, len - 1, NULL)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003322 goto finally;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003323
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003324 PDATA_PUSH(self->stack, str, -1);
3325 return 0;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003326
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003327 finally:
3328 return res;
Tim Peters84e87f32001-03-17 04:50:51 +00003329}
Martin v. Löwis339d0f72001-08-17 18:39:25 +00003330#endif
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003331
3332
Martin v. Löwis339d0f72001-08-17 18:39:25 +00003333#ifdef Py_USING_UNICODE
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003334static int
Tim Peterscba30e22003-02-01 06:24:36 +00003335load_binunicode(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003336{
3337 PyObject *unicode;
3338 long l;
3339 char *s;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003340
Tim Peters0bc93f52003-02-02 18:29:33 +00003341 if (self->read_func(self, &s, 4) < 0) return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003342
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003343 l = calc_binint(s, 4);
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003344
Tim Peters0bc93f52003-02-02 18:29:33 +00003345 if (self->read_func(self, &s, l) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003346 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003347
Tim Peterscba30e22003-02-01 06:24:36 +00003348 if (!( unicode = PyUnicode_DecodeUTF8(s, l, NULL)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003349 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003350
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003351 PDATA_PUSH(self->stack, unicode, -1);
3352 return 0;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003353}
Martin v. Löwis339d0f72001-08-17 18:39:25 +00003354#endif
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003355
3356
3357static int
Tim Peterscba30e22003-02-01 06:24:36 +00003358load_tuple(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003359{
3360 PyObject *tup;
3361 int i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003362
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003363 if ((i = marker(self)) < 0) return -1;
3364 if (!( tup=Pdata_popTuple(self->stack, i))) return -1;
3365 PDATA_PUSH(self->stack, tup, -1);
3366 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003367}
3368
3369static int
Tim Peters1d63c9f2003-02-02 20:29:39 +00003370load_counted_tuple(Unpicklerobject *self, int len)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003371{
Tim Peters1d63c9f2003-02-02 20:29:39 +00003372 PyObject *tup = PyTuple_New(len);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003373
Tim Peters1d63c9f2003-02-02 20:29:39 +00003374 if (tup == NULL)
3375 return -1;
3376
3377 while (--len >= 0) {
3378 PyObject *element;
3379
3380 PDATA_POP(self->stack, element);
3381 if (element == NULL)
3382 return -1;
3383 PyTuple_SET_ITEM(tup, len, element);
3384 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003385 PDATA_PUSH(self->stack, tup, -1);
3386 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003387}
3388
3389static int
Tim Peterscba30e22003-02-01 06:24:36 +00003390load_empty_list(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003391{
3392 PyObject *list;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003393
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003394 if (!( list=PyList_New(0))) return -1;
3395 PDATA_PUSH(self->stack, list, -1);
3396 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003397}
3398
3399static int
Tim Peterscba30e22003-02-01 06:24:36 +00003400load_empty_dict(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003401{
3402 PyObject *dict;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003403
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003404 if (!( dict=PyDict_New())) return -1;
3405 PDATA_PUSH(self->stack, dict, -1);
3406 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003407}
3408
3409
3410static int
Tim Peterscba30e22003-02-01 06:24:36 +00003411load_list(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003412{
3413 PyObject *list = 0;
3414 int i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003415
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003416 if ((i = marker(self)) < 0) return -1;
3417 if (!( list=Pdata_popList(self->stack, i))) return -1;
3418 PDATA_PUSH(self->stack, list, -1);
3419 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003420}
3421
3422static int
Tim Peterscba30e22003-02-01 06:24:36 +00003423load_dict(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003424{
3425 PyObject *dict, *key, *value;
3426 int i, j, k;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003427
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003428 if ((i = marker(self)) < 0) return -1;
3429 j=self->stack->length;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003430
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003431 if (!( dict = PyDict_New())) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003432
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003433 for (k = i+1; k < j; k += 2) {
3434 key =self->stack->data[k-1];
3435 value=self->stack->data[k ];
3436 if (PyDict_SetItem(dict, key, value) < 0) {
3437 Py_DECREF(dict);
3438 return -1;
3439 }
3440 }
3441 Pdata_clear(self->stack, i);
3442 PDATA_PUSH(self->stack, dict, -1);
3443 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003444}
3445
Guido van Rossum512ab9f2006-08-17 22:28:49 +00003446static PyObject *
3447Instance_New(PyObject *cls, PyObject *args)
3448{
3449 PyObject *r = 0;
3450
3451 if ((r=PyObject_CallObject(cls, args))) return r;
3452
3453 {
3454 PyObject *tp, *v, *tb, *tmp_value;
3455
3456 PyErr_Fetch(&tp, &v, &tb);
3457 tmp_value = v;
3458 /* NULL occurs when there was a KeyboardInterrupt */
3459 if (tmp_value == NULL)
3460 tmp_value = Py_None;
3461 if ((r = PyTuple_Pack(3, tmp_value, cls, args))) {
3462 Py_XDECREF(v);
3463 v=r;
3464 }
3465 PyErr_Restore(tp,v,tb);
3466 }
3467 return NULL;
3468}
3469
Guido van Rossum60456fd1997-04-09 17:36:32 +00003470
3471static int
Tim Peterscba30e22003-02-01 06:24:36 +00003472load_obj(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003473{
3474 PyObject *class, *tup, *obj=0;
3475 int i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003476
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003477 if ((i = marker(self)) < 0) return -1;
3478 if (!( tup=Pdata_popTuple(self->stack, i+1))) return -1;
3479 PDATA_POP(self->stack, class);
Guido van Rossum512ab9f2006-08-17 22:28:49 +00003480 if (class) {
3481 obj = Instance_New(class, tup);
3482 Py_DECREF(class);
3483 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003484 Py_DECREF(tup);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003485
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003486 if (! obj) return -1;
3487 PDATA_PUSH(self->stack, obj, -1);
3488 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003489}
3490
3491
3492static int
Tim Peterscba30e22003-02-01 06:24:36 +00003493load_inst(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003494{
3495 PyObject *tup, *class=0, *obj=0, *module_name, *class_name;
3496 int i, len;
3497 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003498
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003499 if ((i = marker(self)) < 0) return -1;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003500
Tim Peters0bc93f52003-02-02 18:29:33 +00003501 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003502 if (len < 2) return bad_readline();
3503 module_name = PyString_FromStringAndSize(s, len - 1);
3504 if (!module_name) return -1;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003505
Tim Peters0bc93f52003-02-02 18:29:33 +00003506 if ((len = self->readline_func(self, &s)) >= 0) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003507 if (len < 2) return bad_readline();
3508 if ((class_name = PyString_FromStringAndSize(s, len - 1))) {
Tim Peterscba30e22003-02-01 06:24:36 +00003509 class = find_class(module_name, class_name,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003510 self->find_class);
3511 Py_DECREF(class_name);
3512 }
3513 }
3514 Py_DECREF(module_name);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003515
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003516 if (! class) return -1;
Tim Peters84e87f32001-03-17 04:50:51 +00003517
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003518 if ((tup=Pdata_popTuple(self->stack, i))) {
Guido van Rossum512ab9f2006-08-17 22:28:49 +00003519 obj = Instance_New(class, tup);
3520 Py_DECREF(tup);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003521 }
3522 Py_DECREF(class);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003523
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003524 if (! obj) return -1;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003525
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003526 PDATA_PUSH(self->stack, obj, -1);
3527 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003528}
3529
Tim Peterseab7db32003-02-13 18:24:14 +00003530static int
3531load_newobj(Unpicklerobject *self)
3532{
3533 PyObject *args = NULL;
3534 PyObject *clsraw = NULL;
3535 PyTypeObject *cls; /* clsraw cast to its true type */
3536 PyObject *obj;
3537
3538 /* Stack is ... cls argtuple, and we want to call
3539 * cls.__new__(cls, *argtuple).
3540 */
3541 PDATA_POP(self->stack, args);
3542 if (args == NULL) goto Fail;
3543 if (! PyTuple_Check(args)) {
3544 PyErr_SetString(UnpicklingError, "NEWOBJ expected an arg "
3545 "tuple.");
3546 goto Fail;
3547 }
3548
3549 PDATA_POP(self->stack, clsraw);
3550 cls = (PyTypeObject *)clsraw;
3551 if (cls == NULL) goto Fail;
3552 if (! PyType_Check(cls)) {
3553 PyErr_SetString(UnpicklingError, "NEWOBJ class argument "
3554 "isn't a type object");
3555 goto Fail;
3556 }
3557 if (cls->tp_new == NULL) {
3558 PyErr_SetString(UnpicklingError, "NEWOBJ class argument "
3559 "has NULL tp_new");
3560 goto Fail;
3561 }
3562
3563 /* Call __new__. */
3564 obj = cls->tp_new(cls, args, NULL);
3565 if (obj == NULL) goto Fail;
3566
3567 Py_DECREF(args);
3568 Py_DECREF(clsraw);
3569 PDATA_PUSH(self->stack, obj, -1);
3570 return 0;
3571
3572 Fail:
3573 Py_XDECREF(args);
3574 Py_XDECREF(clsraw);
3575 return -1;
3576}
Guido van Rossum60456fd1997-04-09 17:36:32 +00003577
3578static int
Tim Peterscba30e22003-02-01 06:24:36 +00003579load_global(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003580{
3581 PyObject *class = 0, *module_name = 0, *class_name = 0;
3582 int len;
3583 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003584
Tim Peters0bc93f52003-02-02 18:29:33 +00003585 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003586 if (len < 2) return bad_readline();
3587 module_name = PyString_FromStringAndSize(s, len - 1);
3588 if (!module_name) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003589
Tim Peters0bc93f52003-02-02 18:29:33 +00003590 if ((len = self->readline_func(self, &s)) >= 0) {
Tim Peters4e52ca82002-12-07 02:43:28 +00003591 if (len < 2) {
3592 Py_DECREF(module_name);
3593 return bad_readline();
3594 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003595 if ((class_name = PyString_FromStringAndSize(s, len - 1))) {
Tim Peterscba30e22003-02-01 06:24:36 +00003596 class = find_class(module_name, class_name,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003597 self->find_class);
3598 Py_DECREF(class_name);
3599 }
3600 }
3601 Py_DECREF(module_name);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003602
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003603 if (! class) return -1;
3604 PDATA_PUSH(self->stack, class, -1);
3605 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003606}
3607
3608
3609static int
Tim Peterscba30e22003-02-01 06:24:36 +00003610load_persid(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003611{
3612 PyObject *pid = 0;
3613 int len;
3614 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003615
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003616 if (self->pers_func) {
Tim Peters0bc93f52003-02-02 18:29:33 +00003617 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003618 if (len < 2) return bad_readline();
3619
3620 pid = PyString_FromStringAndSize(s, len - 1);
3621 if (!pid) return -1;
3622
3623 if (PyList_Check(self->pers_func)) {
3624 if (PyList_Append(self->pers_func, pid) < 0) {
3625 Py_DECREF(pid);
3626 return -1;
3627 }
3628 }
3629 else {
3630 ARG_TUP(self, pid);
3631 if (self->arg) {
Tim Peterscba30e22003-02-01 06:24:36 +00003632 pid = PyObject_Call(self->pers_func, self->arg,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003633 NULL);
3634 FREE_ARG_TUP(self);
3635 }
3636 }
3637
3638 if (! pid) return -1;
3639
3640 PDATA_PUSH(self->stack, pid, -1);
3641 return 0;
3642 }
3643 else {
3644 PyErr_SetString(UnpicklingError,
3645 "A load persistent id instruction was encountered,\n"
3646 "but no persistent_load function was specified.");
3647 return -1;
3648 }
3649}
3650
3651static int
Tim Peterscba30e22003-02-01 06:24:36 +00003652load_binpersid(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003653{
3654 PyObject *pid = 0;
3655
3656 if (self->pers_func) {
3657 PDATA_POP(self->stack, pid);
3658 if (! pid) return -1;
3659
3660 if (PyList_Check(self->pers_func)) {
3661 if (PyList_Append(self->pers_func, pid) < 0) {
3662 Py_DECREF(pid);
3663 return -1;
3664 }
3665 }
3666 else {
3667 ARG_TUP(self, pid);
3668 if (self->arg) {
3669 pid = PyObject_Call(self->pers_func, self->arg,
3670 NULL);
3671 FREE_ARG_TUP(self);
3672 }
3673 if (! pid) return -1;
3674 }
3675
3676 PDATA_PUSH(self->stack, pid, -1);
3677 return 0;
3678 }
3679 else {
3680 PyErr_SetString(UnpicklingError,
3681 "A load persistent id instruction was encountered,\n"
3682 "but no persistent_load function was specified.");
3683 return -1;
3684 }
3685}
3686
3687
3688static int
Tim Peterscba30e22003-02-01 06:24:36 +00003689load_pop(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003690{
3691 int len;
3692
3693 if (!( (len=self->stack->length) > 0 )) return stackUnderflow();
3694
3695 /* Note that we split the (pickle.py) stack into two stacks,
3696 an object stack and a mark stack. We have to be clever and
3697 pop the right one. We do this by looking at the top of the
3698 mark stack.
3699 */
3700
3701 if ((self->num_marks > 0) &&
3702 (self->marks[self->num_marks - 1] == len))
3703 self->num_marks--;
3704 else {
3705 len--;
3706 Py_DECREF(self->stack->data[len]);
3707 self->stack->length=len;
3708 }
3709
3710 return 0;
3711}
3712
3713
3714static int
Tim Peterscba30e22003-02-01 06:24:36 +00003715load_pop_mark(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003716{
3717 int i;
3718
3719 if ((i = marker(self)) < 0)
3720 return -1;
3721
3722 Pdata_clear(self->stack, i);
3723
3724 return 0;
3725}
3726
3727
3728static int
Tim Peterscba30e22003-02-01 06:24:36 +00003729load_dup(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003730{
3731 PyObject *last;
3732 int len;
3733
3734 if ((len = self->stack->length) <= 0) return stackUnderflow();
3735 last=self->stack->data[len-1];
3736 Py_INCREF(last);
3737 PDATA_PUSH(self->stack, last, -1);
3738 return 0;
3739}
3740
3741
3742static int
Tim Peterscba30e22003-02-01 06:24:36 +00003743load_get(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003744{
3745 PyObject *py_str = 0, *value = 0;
3746 int len;
3747 char *s;
3748 int rc;
3749
Tim Peters0bc93f52003-02-02 18:29:33 +00003750 if ((len = self->readline_func(self, &s)) < 0) return -1;
Guido van Rossumf9ffb031999-02-04 14:54:04 +00003751 if (len < 2) return bad_readline();
Tim Peters84e87f32001-03-17 04:50:51 +00003752
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003753 if (!( py_str = PyString_FromStringAndSize(s, len - 1))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003754
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003755 value = PyDict_GetItem(self->memo, py_str);
3756 if (! value) {
3757 PyErr_SetObject(BadPickleGet, py_str);
3758 rc = -1;
Tim Peters92c8bb32003-02-13 23:00:26 +00003759 }
3760 else {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003761 PDATA_APPEND(self->stack, value, -1);
3762 rc = 0;
3763 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003764
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003765 Py_DECREF(py_str);
3766 return rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003767}
3768
3769
3770static int
Tim Peterscba30e22003-02-01 06:24:36 +00003771load_binget(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003772{
3773 PyObject *py_key = 0, *value = 0;
3774 unsigned char key;
3775 char *s;
3776 int rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003777
Tim Peters0bc93f52003-02-02 18:29:33 +00003778 if (self->read_func(self, &s, 1) < 0) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003779
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003780 key = (unsigned char)s[0];
3781 if (!( py_key = PyInt_FromLong((long)key))) return -1;
Guido van Rossumea2b7152000-05-09 18:14:50 +00003782
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003783 value = PyDict_GetItem(self->memo, py_key);
3784 if (! value) {
3785 PyErr_SetObject(BadPickleGet, py_key);
3786 rc = -1;
Tim Peters92c8bb32003-02-13 23:00:26 +00003787 }
3788 else {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003789 PDATA_APPEND(self->stack, value, -1);
3790 rc = 0;
3791 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003792
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003793 Py_DECREF(py_key);
3794 return rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003795}
3796
3797
3798static int
Tim Peterscba30e22003-02-01 06:24:36 +00003799load_long_binget(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003800{
3801 PyObject *py_key = 0, *value = 0;
3802 unsigned char c;
3803 char *s;
3804 long key;
3805 int rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003806
Tim Peters0bc93f52003-02-02 18:29:33 +00003807 if (self->read_func(self, &s, 4) < 0) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003808
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003809 c = (unsigned char)s[0];
3810 key = (long)c;
3811 c = (unsigned char)s[1];
3812 key |= (long)c << 8;
3813 c = (unsigned char)s[2];
3814 key |= (long)c << 16;
3815 c = (unsigned char)s[3];
3816 key |= (long)c << 24;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003817
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003818 if (!( py_key = PyInt_FromLong((long)key))) return -1;
3819
3820 value = PyDict_GetItem(self->memo, py_key);
3821 if (! value) {
3822 PyErr_SetObject(BadPickleGet, py_key);
3823 rc = -1;
Tim Peters92c8bb32003-02-13 23:00:26 +00003824 }
3825 else {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003826 PDATA_APPEND(self->stack, value, -1);
3827 rc = 0;
3828 }
3829
3830 Py_DECREF(py_key);
3831 return rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003832}
3833
Tim Peters2d629652003-02-04 05:06:17 +00003834/* Push an object from the extension registry (EXT[124]). nbytes is
3835 * the number of bytes following the opcode, holding the index (code) value.
3836 */
3837static int
3838load_extension(Unpicklerobject *self, int nbytes)
3839{
3840 char *codebytes; /* the nbytes bytes after the opcode */
3841 long code; /* calc_binint returns long */
3842 PyObject *py_code; /* code as a Python int */
3843 PyObject *obj; /* the object to push */
3844 PyObject *pair; /* (module_name, class_name) */
3845 PyObject *module_name, *class_name;
3846
3847 assert(nbytes == 1 || nbytes == 2 || nbytes == 4);
3848 if (self->read_func(self, &codebytes, nbytes) < 0) return -1;
3849 code = calc_binint(codebytes, nbytes);
3850 if (code <= 0) { /* note that 0 is forbidden */
3851 /* Corrupt or hostile pickle. */
3852 PyErr_SetString(UnpicklingError, "EXT specifies code <= 0");
3853 return -1;
3854 }
3855
3856 /* Look for the code in the cache. */
3857 py_code = PyInt_FromLong(code);
3858 if (py_code == NULL) return -1;
3859 obj = PyDict_GetItem(extension_cache, py_code);
3860 if (obj != NULL) {
3861 /* Bingo. */
3862 Py_DECREF(py_code);
3863 PDATA_APPEND(self->stack, obj, -1);
3864 return 0;
3865 }
3866
3867 /* Look up the (module_name, class_name) pair. */
3868 pair = PyDict_GetItem(inverted_registry, py_code);
3869 if (pair == NULL) {
3870 Py_DECREF(py_code);
3871 PyErr_Format(PyExc_ValueError, "unregistered extension "
3872 "code %ld", code);
3873 return -1;
3874 }
3875 /* Since the extension registry is manipulable via Python code,
Tim Petersfa05ce32003-02-04 05:20:32 +00003876 * confirm that pair is really a 2-tuple of strings.
Tim Peters2d629652003-02-04 05:06:17 +00003877 */
3878 if (!PyTuple_Check(pair) || PyTuple_Size(pair) != 2 ||
3879 !PyString_Check(module_name = PyTuple_GET_ITEM(pair, 0)) ||
3880 !PyString_Check(class_name = PyTuple_GET_ITEM(pair, 1))) {
3881 Py_DECREF(py_code);
3882 PyErr_Format(PyExc_ValueError, "_inverted_registry[%ld] "
3883 "isn't a 2-tuple of strings", code);
3884 return -1;
3885 }
3886 /* Load the object. */
3887 obj = find_class(module_name, class_name, self->find_class);
3888 if (obj == NULL) {
3889 Py_DECREF(py_code);
3890 return -1;
3891 }
3892 /* Cache code -> obj. */
3893 code = PyDict_SetItem(extension_cache, py_code, obj);
3894 Py_DECREF(py_code);
3895 if (code < 0) {
3896 Py_DECREF(obj);
3897 return -1;
3898 }
3899 PDATA_PUSH(self->stack, obj, -1);
3900 return 0;
3901}
Guido van Rossum60456fd1997-04-09 17:36:32 +00003902
3903static int
Tim Peterscba30e22003-02-01 06:24:36 +00003904load_put(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003905{
3906 PyObject *py_str = 0, *value = 0;
3907 int len, l;
3908 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003909
Tim Peters0bc93f52003-02-02 18:29:33 +00003910 if ((l = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003911 if (l < 2) return bad_readline();
3912 if (!( len=self->stack->length )) return stackUnderflow();
3913 if (!( py_str = PyString_FromStringAndSize(s, l - 1))) return -1;
3914 value=self->stack->data[len-1];
3915 l=PyDict_SetItem(self->memo, py_str, value);
3916 Py_DECREF(py_str);
3917 return l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003918}
3919
3920
3921static int
Tim Peterscba30e22003-02-01 06:24:36 +00003922load_binput(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003923{
3924 PyObject *py_key = 0, *value = 0;
3925 unsigned char key;
3926 char *s;
3927 int len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003928
Tim Peters0bc93f52003-02-02 18:29:33 +00003929 if (self->read_func(self, &s, 1) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003930 if (!( (len=self->stack->length) > 0 )) return stackUnderflow();
Guido van Rossum60456fd1997-04-09 17:36:32 +00003931
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003932 key = (unsigned char)s[0];
Guido van Rossum053b8df1998-11-25 16:18:00 +00003933
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003934 if (!( py_key = PyInt_FromLong((long)key))) return -1;
3935 value=self->stack->data[len-1];
3936 len=PyDict_SetItem(self->memo, py_key, value);
3937 Py_DECREF(py_key);
3938 return len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003939}
3940
3941
3942static int
Tim Peterscba30e22003-02-01 06:24:36 +00003943load_long_binput(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003944{
3945 PyObject *py_key = 0, *value = 0;
3946 long key;
3947 unsigned char c;
3948 char *s;
3949 int len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003950
Tim Peters0bc93f52003-02-02 18:29:33 +00003951 if (self->read_func(self, &s, 4) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003952 if (!( len=self->stack->length )) return stackUnderflow();
Guido van Rossum60456fd1997-04-09 17:36:32 +00003953
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003954 c = (unsigned char)s[0];
3955 key = (long)c;
3956 c = (unsigned char)s[1];
3957 key |= (long)c << 8;
3958 c = (unsigned char)s[2];
3959 key |= (long)c << 16;
3960 c = (unsigned char)s[3];
3961 key |= (long)c << 24;
Tim Peters84e87f32001-03-17 04:50:51 +00003962
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003963 if (!( py_key = PyInt_FromLong(key))) return -1;
3964 value=self->stack->data[len-1];
3965 len=PyDict_SetItem(self->memo, py_key, value);
3966 Py_DECREF(py_key);
3967 return len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003968}
3969
3970
3971static int
Tim Peterscba30e22003-02-01 06:24:36 +00003972do_append(Unpicklerobject *self, int x)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003973{
3974 PyObject *value = 0, *list = 0, *append_method = 0;
3975 int len, i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003976
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003977 len=self->stack->length;
3978 if (!( len >= x && x > 0 )) return stackUnderflow();
3979 /* nothing to do */
Tim Peterscba30e22003-02-01 06:24:36 +00003980 if (len==x) return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003981
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003982 list=self->stack->data[x-1];
Guido van Rossum60456fd1997-04-09 17:36:32 +00003983
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003984 if (PyList_Check(list)) {
3985 PyObject *slice;
3986 int list_len;
Tim Peters84e87f32001-03-17 04:50:51 +00003987
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003988 slice=Pdata_popList(self->stack, x);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003989 if (! slice) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003990 list_len = PyList_GET_SIZE(list);
3991 i=PyList_SetSlice(list, list_len, list_len, slice);
3992 Py_DECREF(slice);
3993 return i;
3994 }
3995 else {
Guido van Rossum60456fd1997-04-09 17:36:32 +00003996
Tim Peterscba30e22003-02-01 06:24:36 +00003997 if (!( append_method = PyObject_GetAttr(list, append_str)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003998 return -1;
3999
4000 for (i = x; i < len; i++) {
4001 PyObject *junk;
4002
4003 value=self->stack->data[i];
4004 junk=0;
4005 ARG_TUP(self, value);
4006 if (self->arg) {
Tim Peterscba30e22003-02-01 06:24:36 +00004007 junk = PyObject_Call(append_method, self->arg,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004008 NULL);
4009 FREE_ARG_TUP(self);
4010 }
4011 if (! junk) {
4012 Pdata_clear(self->stack, i+1);
4013 self->stack->length=x;
4014 Py_DECREF(append_method);
4015 return -1;
4016 }
4017 Py_DECREF(junk);
4018 }
4019 self->stack->length=x;
4020 Py_DECREF(append_method);
4021 }
4022
4023 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004024}
4025
4026
4027static int
Tim Peterscba30e22003-02-01 06:24:36 +00004028load_append(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004029{
4030 return do_append(self, self->stack->length - 1);
Guido van Rossum60456fd1997-04-09 17:36:32 +00004031}
4032
4033
4034static int
Tim Peterscba30e22003-02-01 06:24:36 +00004035load_appends(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004036{
4037 return do_append(self, marker(self));
Guido van Rossum60456fd1997-04-09 17:36:32 +00004038}
4039
4040
4041static int
Tim Peterscba30e22003-02-01 06:24:36 +00004042do_setitems(Unpicklerobject *self, int x)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004043{
4044 PyObject *value = 0, *key = 0, *dict = 0;
4045 int len, i, r=0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004046
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004047 if (!( (len=self->stack->length) >= x
4048 && x > 0 )) return stackUnderflow();
Guido van Rossum60456fd1997-04-09 17:36:32 +00004049
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004050 dict=self->stack->data[x-1];
Guido van Rossum60456fd1997-04-09 17:36:32 +00004051
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004052 for (i = x+1; i < len; i += 2) {
4053 key =self->stack->data[i-1];
4054 value=self->stack->data[i ];
4055 if (PyObject_SetItem(dict, key, value) < 0) {
4056 r=-1;
4057 break;
4058 }
4059 }
4060
4061 Pdata_clear(self->stack, x);
4062
4063 return r;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004064}
4065
4066
Tim Peters84e87f32001-03-17 04:50:51 +00004067static int
Tim Peterscba30e22003-02-01 06:24:36 +00004068load_setitem(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004069{
4070 return do_setitems(self, self->stack->length - 2);
4071}
Guido van Rossum60456fd1997-04-09 17:36:32 +00004072
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004073static int
Tim Peterscba30e22003-02-01 06:24:36 +00004074load_setitems(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004075{
4076 return do_setitems(self, marker(self));
Guido van Rossum60456fd1997-04-09 17:36:32 +00004077}
4078
Tim Peters84e87f32001-03-17 04:50:51 +00004079
Guido van Rossum60456fd1997-04-09 17:36:32 +00004080static int
Tim Peterscba30e22003-02-01 06:24:36 +00004081load_build(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004082{
Tim Peters080c88b2003-02-15 03:01:11 +00004083 PyObject *state, *inst, *slotstate;
4084 PyObject *__setstate__;
4085 PyObject *d_key, *d_value;
Martin v. Löwis18e16552006-02-15 17:27:45 +00004086 Py_ssize_t i;
Tim Peters080c88b2003-02-15 03:01:11 +00004087 int res = -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004088
Tim Peters080c88b2003-02-15 03:01:11 +00004089 /* Stack is ... instance, state. We want to leave instance at
4090 * the stack top, possibly mutated via instance.__setstate__(state).
4091 */
4092 if (self->stack->length < 2)
4093 return stackUnderflow();
4094 PDATA_POP(self->stack, state);
4095 if (state == NULL)
4096 return -1;
4097 inst = self->stack->data[self->stack->length - 1];
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004098
Tim Peters080c88b2003-02-15 03:01:11 +00004099 __setstate__ = PyObject_GetAttr(inst, __setstate___str);
4100 if (__setstate__ != NULL) {
4101 PyObject *junk = NULL;
4102
4103 /* The explicit __setstate__ is responsible for everything. */
4104 ARG_TUP(self, state);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004105 if (self->arg) {
4106 junk = PyObject_Call(__setstate__, self->arg, NULL);
4107 FREE_ARG_TUP(self);
4108 }
4109 Py_DECREF(__setstate__);
Tim Peters080c88b2003-02-15 03:01:11 +00004110 if (junk == NULL)
4111 return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004112 Py_DECREF(junk);
4113 return 0;
4114 }
Jeremy Hyltonf00368f2003-06-16 20:19:49 +00004115 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4116 return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004117 PyErr_Clear();
Tim Peters080c88b2003-02-15 03:01:11 +00004118
4119 /* A default __setstate__. First see whether state embeds a
4120 * slot state dict too (a proto 2 addition).
4121 */
4122 if (PyTuple_Check(state) && PyTuple_Size(state) == 2) {
4123 PyObject *temp = state;
4124 state = PyTuple_GET_ITEM(temp, 0);
4125 slotstate = PyTuple_GET_ITEM(temp, 1);
4126 Py_INCREF(state);
4127 Py_INCREF(slotstate);
4128 Py_DECREF(temp);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004129 }
Tim Peters080c88b2003-02-15 03:01:11 +00004130 else
4131 slotstate = NULL;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004132
Tim Peters080c88b2003-02-15 03:01:11 +00004133 /* Set inst.__dict__ from the state dict (if any). */
4134 if (state != Py_None) {
4135 PyObject *dict;
4136 if (! PyDict_Check(state)) {
4137 PyErr_SetString(UnpicklingError, "state is not a "
4138 "dictionary");
4139 goto finally;
4140 }
4141 dict = PyObject_GetAttr(inst, __dict___str);
4142 if (dict == NULL)
4143 goto finally;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004144
Tim Peters080c88b2003-02-15 03:01:11 +00004145 i = 0;
4146 while (PyDict_Next(state, &i, &d_key, &d_value)) {
4147 if (PyObject_SetItem(dict, d_key, d_value) < 0)
4148 goto finally;
4149 }
4150 Py_DECREF(dict);
4151 }
4152
4153 /* Also set instance attributes from the slotstate dict (if any). */
4154 if (slotstate != NULL) {
4155 if (! PyDict_Check(slotstate)) {
4156 PyErr_SetString(UnpicklingError, "slot state is not "
4157 "a dictionary");
4158 goto finally;
4159 }
4160 i = 0;
4161 while (PyDict_Next(slotstate, &i, &d_key, &d_value)) {
4162 if (PyObject_SetAttr(inst, d_key, d_value) < 0)
4163 goto finally;
4164 }
4165 }
4166 res = 0;
4167
4168 finally:
4169 Py_DECREF(state);
4170 Py_XDECREF(slotstate);
4171 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004172}
4173
4174
4175static int
Tim Peterscba30e22003-02-01 06:24:36 +00004176load_mark(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004177{
4178 int s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004179
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004180 /* Note that we split the (pickle.py) stack into two stacks, an
4181 object stack and a mark stack. Here we push a mark onto the
4182 mark stack.
4183 */
Guido van Rossum60456fd1997-04-09 17:36:32 +00004184
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004185 if ((self->num_marks + 1) >= self->marks_size) {
4186 s=self->marks_size+20;
4187 if (s <= self->num_marks) s=self->num_marks + 1;
4188 if (self->marks == NULL)
4189 self->marks=(int *)malloc(s * sizeof(int));
4190 else
Tim Peterscba30e22003-02-01 06:24:36 +00004191 self->marks=(int *)realloc(self->marks,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004192 s * sizeof(int));
4193 if (! self->marks) {
4194 PyErr_NoMemory();
4195 return -1;
4196 }
4197 self->marks_size = s;
4198 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00004199
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004200 self->marks[self->num_marks++] = self->stack->length;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004201
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004202 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004203}
4204
Guido van Rossum60456fd1997-04-09 17:36:32 +00004205static int
Tim Peterscba30e22003-02-01 06:24:36 +00004206load_reduce(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004207{
4208 PyObject *callable = 0, *arg_tup = 0, *ob = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004209
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004210 PDATA_POP(self->stack, arg_tup);
4211 if (! arg_tup) return -1;
4212 PDATA_POP(self->stack, callable);
Guido van Rossum512ab9f2006-08-17 22:28:49 +00004213 if (callable) {
4214 ob = Instance_New(callable, arg_tup);
4215 Py_DECREF(callable);
4216 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004217 Py_DECREF(arg_tup);
Guido van Rossum60456fd1997-04-09 17:36:32 +00004218
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004219 if (! ob) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004220
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004221 PDATA_PUSH(self->stack, ob, -1);
4222 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004223}
Tim Peters84e87f32001-03-17 04:50:51 +00004224
Tim Peters4190fb82003-02-02 16:09:05 +00004225/* Just raises an error if we don't know the protocol specified. PROTO
4226 * is the first opcode for protocols >= 2.
4227 */
4228static int
4229load_proto(Unpicklerobject *self)
4230{
4231 int i;
4232 char *protobyte;
4233
4234 i = self->read_func(self, &protobyte, 1);
4235 if (i < 0)
4236 return -1;
4237
4238 i = calc_binint(protobyte, 1);
4239 /* No point checking for < 0, since calc_binint returns an unsigned
4240 * int when chewing on 1 byte.
4241 */
4242 assert(i >= 0);
Tim Peters8587b3c2003-02-13 15:44:41 +00004243 if (i <= HIGHEST_PROTOCOL)
Tim Peters4190fb82003-02-02 16:09:05 +00004244 return 0;
4245
4246 PyErr_Format(PyExc_ValueError, "unsupported pickle protocol: %d", i);
4247 return -1;
4248}
4249
Guido van Rossum60456fd1997-04-09 17:36:32 +00004250static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00004251load(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004252{
4253 PyObject *err = 0, *val = 0;
4254 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004255
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004256 self->num_marks = 0;
4257 if (self->stack->length) Pdata_clear(self->stack, 0);
Guido van Rossum60456fd1997-04-09 17:36:32 +00004258
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004259 while (1) {
Tim Peters0bc93f52003-02-02 18:29:33 +00004260 if (self->read_func(self, &s, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004261 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004262
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004263 switch (s[0]) {
4264 case NONE:
4265 if (load_none(self) < 0)
4266 break;
4267 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004268
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004269 case BININT:
4270 if (load_binint(self) < 0)
4271 break;
4272 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004273
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004274 case BININT1:
4275 if (load_binint1(self) < 0)
4276 break;
4277 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004278
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004279 case BININT2:
4280 if (load_binint2(self) < 0)
4281 break;
4282 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004283
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004284 case INT:
4285 if (load_int(self) < 0)
4286 break;
4287 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004288
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004289 case LONG:
4290 if (load_long(self) < 0)
4291 break;
4292 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004293
Tim Petersee1a53c2003-02-02 02:57:53 +00004294 case LONG1:
4295 if (load_counted_long(self, 1) < 0)
4296 break;
4297 continue;
4298
4299 case LONG4:
4300 if (load_counted_long(self, 4) < 0)
4301 break;
4302 continue;
4303
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004304 case FLOAT:
4305 if (load_float(self) < 0)
4306 break;
4307 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004308
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004309 case BINFLOAT:
4310 if (load_binfloat(self) < 0)
4311 break;
4312 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004313
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004314 case BINSTRING:
4315 if (load_binstring(self) < 0)
4316 break;
4317 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004318
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004319 case SHORT_BINSTRING:
4320 if (load_short_binstring(self) < 0)
4321 break;
4322 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004323
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004324 case STRING:
4325 if (load_string(self) < 0)
4326 break;
4327 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004328
Martin v. Löwis339d0f72001-08-17 18:39:25 +00004329#ifdef Py_USING_UNICODE
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004330 case UNICODE:
4331 if (load_unicode(self) < 0)
4332 break;
4333 continue;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00004334
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004335 case BINUNICODE:
4336 if (load_binunicode(self) < 0)
4337 break;
4338 continue;
Martin v. Löwis339d0f72001-08-17 18:39:25 +00004339#endif
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00004340
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004341 case EMPTY_TUPLE:
Tim Peters1d63c9f2003-02-02 20:29:39 +00004342 if (load_counted_tuple(self, 0) < 0)
4343 break;
4344 continue;
4345
4346 case TUPLE1:
4347 if (load_counted_tuple(self, 1) < 0)
4348 break;
4349 continue;
4350
4351 case TUPLE2:
4352 if (load_counted_tuple(self, 2) < 0)
4353 break;
4354 continue;
4355
4356 case TUPLE3:
4357 if (load_counted_tuple(self, 3) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004358 break;
4359 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004360
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004361 case TUPLE:
4362 if (load_tuple(self) < 0)
4363 break;
4364 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004365
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004366 case EMPTY_LIST:
4367 if (load_empty_list(self) < 0)
4368 break;
4369 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004370
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004371 case LIST:
4372 if (load_list(self) < 0)
4373 break;
4374 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004375
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004376 case EMPTY_DICT:
4377 if (load_empty_dict(self) < 0)
4378 break;
4379 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004380
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004381 case DICT:
4382 if (load_dict(self) < 0)
4383 break;
4384 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004385
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004386 case OBJ:
4387 if (load_obj(self) < 0)
4388 break;
4389 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004390
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004391 case INST:
4392 if (load_inst(self) < 0)
4393 break;
4394 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004395
Tim Peterseab7db32003-02-13 18:24:14 +00004396 case NEWOBJ:
4397 if (load_newobj(self) < 0)
4398 break;
4399 continue;
4400
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004401 case GLOBAL:
4402 if (load_global(self) < 0)
4403 break;
4404 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004405
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004406 case APPEND:
4407 if (load_append(self) < 0)
4408 break;
4409 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004410
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004411 case APPENDS:
4412 if (load_appends(self) < 0)
4413 break;
4414 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00004415
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004416 case BUILD:
4417 if (load_build(self) < 0)
4418 break;
4419 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00004420
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004421 case DUP:
4422 if (load_dup(self) < 0)
4423 break;
4424 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004425
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004426 case BINGET:
4427 if (load_binget(self) < 0)
4428 break;
4429 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004430
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004431 case LONG_BINGET:
4432 if (load_long_binget(self) < 0)
4433 break;
4434 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00004435
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004436 case GET:
4437 if (load_get(self) < 0)
4438 break;
4439 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004440
Tim Peters2d629652003-02-04 05:06:17 +00004441 case EXT1:
4442 if (load_extension(self, 1) < 0)
4443 break;
4444 continue;
4445
4446 case EXT2:
4447 if (load_extension(self, 2) < 0)
4448 break;
4449 continue;
4450
4451 case EXT4:
4452 if (load_extension(self, 4) < 0)
4453 break;
4454 continue;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004455 case MARK:
4456 if (load_mark(self) < 0)
4457 break;
4458 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004459
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004460 case BINPUT:
4461 if (load_binput(self) < 0)
4462 break;
4463 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004464
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004465 case LONG_BINPUT:
4466 if (load_long_binput(self) < 0)
4467 break;
4468 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00004469
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004470 case PUT:
4471 if (load_put(self) < 0)
4472 break;
4473 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004474
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004475 case POP:
4476 if (load_pop(self) < 0)
4477 break;
4478 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004479
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004480 case POP_MARK:
4481 if (load_pop_mark(self) < 0)
4482 break;
4483 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004484
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004485 case SETITEM:
4486 if (load_setitem(self) < 0)
4487 break;
4488 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004489
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004490 case SETITEMS:
4491 if (load_setitems(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 STOP:
4496 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004497
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004498 case PERSID:
4499 if (load_persid(self) < 0)
4500 break;
4501 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004502
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004503 case BINPERSID:
4504 if (load_binpersid(self) < 0)
4505 break;
4506 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004507
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004508 case REDUCE:
4509 if (load_reduce(self) < 0)
4510 break;
4511 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004512
Tim Peters4190fb82003-02-02 16:09:05 +00004513 case PROTO:
4514 if (load_proto(self) < 0)
4515 break;
4516 continue;
4517
Tim Peters3c67d792003-02-02 17:59:11 +00004518 case NEWTRUE:
4519 if (load_bool(self, Py_True) < 0)
4520 break;
4521 continue;
4522
4523 case NEWFALSE:
4524 if (load_bool(self, Py_False) < 0)
4525 break;
4526 continue;
4527
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004528 case '\0':
4529 /* end of file */
4530 PyErr_SetNone(PyExc_EOFError);
4531 break;
Tim Peterscba30e22003-02-01 06:24:36 +00004532
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004533 default:
Tim Peterscba30e22003-02-01 06:24:36 +00004534 cPickle_ErrFormat(UnpicklingError,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004535 "invalid load key, '%s'.",
4536 "c", s[0]);
4537 return NULL;
4538 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00004539
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004540 break;
4541 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00004542
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004543 if ((err = PyErr_Occurred())) {
4544 if (err == PyExc_EOFError) {
4545 PyErr_SetNone(PyExc_EOFError);
4546 }
4547 return NULL;
4548 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00004549
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004550 PDATA_POP(self->stack, val);
4551 return val;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004552}
Tim Peters84e87f32001-03-17 04:50:51 +00004553
Guido van Rossum60456fd1997-04-09 17:36:32 +00004554
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004555/* No-load functions to support noload, which is used to
4556 find persistent references. */
4557
4558static int
Tim Peterscba30e22003-02-01 06:24:36 +00004559noload_obj(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004560{
4561 int i;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004562
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004563 if ((i = marker(self)) < 0) return -1;
4564 return Pdata_clear(self->stack, i+1);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004565}
4566
4567
4568static int
Tim Peterscba30e22003-02-01 06:24:36 +00004569noload_inst(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004570{
4571 int i;
4572 char *s;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004573
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004574 if ((i = marker(self)) < 0) return -1;
4575 Pdata_clear(self->stack, i);
Tim Peters0bc93f52003-02-02 18:29:33 +00004576 if (self->readline_func(self, &s) < 0) return -1;
4577 if (self->readline_func(self, &s) < 0) return -1;
Tim Peterseab7db32003-02-13 18:24:14 +00004578 PDATA_APPEND(self->stack, Py_None, -1);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004579 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004580}
4581
4582static int
Tim Peterseab7db32003-02-13 18:24:14 +00004583noload_newobj(Unpicklerobject *self)
4584{
4585 PyObject *obj;
4586
4587 PDATA_POP(self->stack, obj); /* pop argtuple */
4588 if (obj == NULL) return -1;
4589 Py_DECREF(obj);
4590
4591 PDATA_POP(self->stack, obj); /* pop cls */
4592 if (obj == NULL) return -1;
4593 Py_DECREF(obj);
4594
4595 PDATA_APPEND(self->stack, Py_None, -1);
4596 return 0;
4597}
4598
4599static int
Tim Peterscba30e22003-02-01 06:24:36 +00004600noload_global(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004601{
4602 char *s;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004603
Tim Peters0bc93f52003-02-02 18:29:33 +00004604 if (self->readline_func(self, &s) < 0) return -1;
4605 if (self->readline_func(self, &s) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004606 PDATA_APPEND(self->stack, Py_None,-1);
4607 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004608}
4609
4610static int
Tim Peterscba30e22003-02-01 06:24:36 +00004611noload_reduce(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004612{
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004613
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004614 if (self->stack->length < 2) return stackUnderflow();
4615 Pdata_clear(self->stack, self->stack->length-2);
4616 PDATA_APPEND(self->stack, Py_None,-1);
4617 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004618}
4619
4620static int
4621noload_build(Unpicklerobject *self) {
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004622
Guido van Rossum053b8df1998-11-25 16:18:00 +00004623 if (self->stack->length < 1) return stackUnderflow();
4624 Pdata_clear(self->stack, self->stack->length-1);
Guido van Rossum50f385c1998-12-04 18:48:44 +00004625 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004626}
4627
Tim Peters2d629652003-02-04 05:06:17 +00004628static int
4629noload_extension(Unpicklerobject *self, int nbytes)
4630{
4631 char *codebytes;
4632
4633 assert(nbytes == 1 || nbytes == 2 || nbytes == 4);
4634 if (self->read_func(self, &codebytes, nbytes) < 0) return -1;
4635 PDATA_APPEND(self->stack, Py_None, -1);
4636 return 0;
4637}
4638
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004639
4640static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00004641noload(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004642{
4643 PyObject *err = 0, *val = 0;
4644 char *s;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004645
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004646 self->num_marks = 0;
4647 Pdata_clear(self->stack, 0);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004648
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004649 while (1) {
Tim Peters0bc93f52003-02-02 18:29:33 +00004650 if (self->read_func(self, &s, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004651 break;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004652
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004653 switch (s[0]) {
4654 case NONE:
4655 if (load_none(self) < 0)
4656 break;
4657 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004658
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004659 case BININT:
4660 if (load_binint(self) < 0)
4661 break;
4662 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004663
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004664 case BININT1:
4665 if (load_binint1(self) < 0)
4666 break;
4667 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004668
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004669 case BININT2:
4670 if (load_binint2(self) < 0)
4671 break;
4672 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004673
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004674 case INT:
4675 if (load_int(self) < 0)
4676 break;
4677 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004678
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004679 case LONG:
4680 if (load_long(self) < 0)
4681 break;
4682 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004683
Tim Peters4190fb82003-02-02 16:09:05 +00004684 case LONG1:
4685 if (load_counted_long(self, 1) < 0)
4686 break;
4687 continue;
4688
4689 case LONG4:
4690 if (load_counted_long(self, 4) < 0)
4691 break;
4692 continue;
4693
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004694 case FLOAT:
4695 if (load_float(self) < 0)
4696 break;
4697 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004698
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004699 case BINFLOAT:
4700 if (load_binfloat(self) < 0)
4701 break;
4702 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004703
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004704 case BINSTRING:
4705 if (load_binstring(self) < 0)
4706 break;
4707 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004708
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004709 case SHORT_BINSTRING:
4710 if (load_short_binstring(self) < 0)
4711 break;
4712 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004713
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004714 case STRING:
4715 if (load_string(self) < 0)
4716 break;
4717 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004718
Martin v. Löwis339d0f72001-08-17 18:39:25 +00004719#ifdef Py_USING_UNICODE
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004720 case UNICODE:
4721 if (load_unicode(self) < 0)
4722 break;
4723 continue;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00004724
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004725 case BINUNICODE:
4726 if (load_binunicode(self) < 0)
4727 break;
4728 continue;
Martin v. Löwis339d0f72001-08-17 18:39:25 +00004729#endif
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00004730
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004731 case EMPTY_TUPLE:
Tim Peters1d63c9f2003-02-02 20:29:39 +00004732 if (load_counted_tuple(self, 0) < 0)
4733 break;
4734 continue;
4735
4736 case TUPLE1:
4737 if (load_counted_tuple(self, 1) < 0)
4738 break;
4739 continue;
4740
4741 case TUPLE2:
4742 if (load_counted_tuple(self, 2) < 0)
4743 break;
4744 continue;
4745
4746 case TUPLE3:
4747 if (load_counted_tuple(self, 3) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004748 break;
4749 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004750
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004751 case TUPLE:
4752 if (load_tuple(self) < 0)
4753 break;
4754 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004755
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004756 case EMPTY_LIST:
4757 if (load_empty_list(self) < 0)
4758 break;
4759 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004760
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004761 case LIST:
4762 if (load_list(self) < 0)
4763 break;
4764 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004765
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004766 case EMPTY_DICT:
4767 if (load_empty_dict(self) < 0)
4768 break;
4769 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004770
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004771 case DICT:
4772 if (load_dict(self) < 0)
4773 break;
4774 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004775
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004776 case OBJ:
4777 if (noload_obj(self) < 0)
4778 break;
4779 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004780
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004781 case INST:
4782 if (noload_inst(self) < 0)
4783 break;
4784 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004785
Tim Peterseab7db32003-02-13 18:24:14 +00004786 case NEWOBJ:
4787 if (noload_newobj(self) < 0)
4788 break;
4789 continue;
4790
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004791 case GLOBAL:
4792 if (noload_global(self) < 0)
4793 break;
4794 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004795
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004796 case APPEND:
4797 if (load_append(self) < 0)
4798 break;
4799 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004800
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004801 case APPENDS:
4802 if (load_appends(self) < 0)
4803 break;
4804 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00004805
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004806 case BUILD:
4807 if (noload_build(self) < 0)
4808 break;
4809 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00004810
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004811 case DUP:
4812 if (load_dup(self) < 0)
4813 break;
4814 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004815
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004816 case BINGET:
4817 if (load_binget(self) < 0)
4818 break;
4819 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004820
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004821 case LONG_BINGET:
4822 if (load_long_binget(self) < 0)
4823 break;
4824 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00004825
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004826 case GET:
4827 if (load_get(self) < 0)
4828 break;
4829 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004830
Tim Peters2d629652003-02-04 05:06:17 +00004831 case EXT1:
4832 if (noload_extension(self, 1) < 0)
4833 break;
4834 continue;
4835
4836 case EXT2:
4837 if (noload_extension(self, 2) < 0)
4838 break;
4839 continue;
4840
4841 case EXT4:
4842 if (noload_extension(self, 4) < 0)
4843 break;
4844 continue;
4845
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004846 case MARK:
4847 if (load_mark(self) < 0)
4848 break;
4849 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004850
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004851 case BINPUT:
4852 if (load_binput(self) < 0)
4853 break;
4854 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004855
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004856 case LONG_BINPUT:
4857 if (load_long_binput(self) < 0)
4858 break;
4859 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00004860
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004861 case PUT:
4862 if (load_put(self) < 0)
4863 break;
4864 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004865
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004866 case POP:
4867 if (load_pop(self) < 0)
4868 break;
4869 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004870
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004871 case POP_MARK:
4872 if (load_pop_mark(self) < 0)
4873 break;
4874 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004875
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004876 case SETITEM:
4877 if (load_setitem(self) < 0)
4878 break;
4879 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004880
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004881 case SETITEMS:
4882 if (load_setitems(self) < 0)
4883 break;
4884 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004885
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004886 case STOP:
4887 break;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004888
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004889 case PERSID:
4890 if (load_persid(self) < 0)
4891 break;
4892 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004893
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004894 case BINPERSID:
4895 if (load_binpersid(self) < 0)
4896 break;
4897 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004898
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004899 case REDUCE:
4900 if (noload_reduce(self) < 0)
4901 break;
4902 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004903
Tim Peters4190fb82003-02-02 16:09:05 +00004904 case PROTO:
4905 if (load_proto(self) < 0)
4906 break;
4907 continue;
4908
Tim Peters3c67d792003-02-02 17:59:11 +00004909 case NEWTRUE:
4910 if (load_bool(self, Py_True) < 0)
4911 break;
4912 continue;
4913
4914 case NEWFALSE:
4915 if (load_bool(self, Py_False) < 0)
4916 break;
4917 continue;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004918 default:
Tim Peterscba30e22003-02-01 06:24:36 +00004919 cPickle_ErrFormat(UnpicklingError,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004920 "invalid load key, '%s'.",
4921 "c", s[0]);
4922 return NULL;
4923 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004924
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004925 break;
4926 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004927
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004928 if ((err = PyErr_Occurred())) {
4929 if (err == PyExc_EOFError) {
4930 PyErr_SetNone(PyExc_EOFError);
4931 }
4932 return NULL;
4933 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004934
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004935 PDATA_POP(self->stack, val);
4936 return val;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004937}
Tim Peters84e87f32001-03-17 04:50:51 +00004938
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004939
Guido van Rossum60456fd1997-04-09 17:36:32 +00004940static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00004941Unpickler_load(Unpicklerobject *self, PyObject *unused)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004942{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004943 return load(self);
Guido van Rossum60456fd1997-04-09 17:36:32 +00004944}
4945
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004946static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00004947Unpickler_noload(Unpicklerobject *self, PyObject *unused)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004948{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004949 return noload(self);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004950}
4951
Guido van Rossum60456fd1997-04-09 17:36:32 +00004952
4953static struct PyMethodDef Unpickler_methods[] = {
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00004954 {"load", (PyCFunction)Unpickler_load, METH_NOARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00004955 PyDoc_STR("load() -- Load a pickle")
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004956 },
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00004957 {"noload", (PyCFunction)Unpickler_noload, METH_NOARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00004958 PyDoc_STR(
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004959 "noload() -- not load a pickle, but go through most of the motions\n"
4960 "\n"
4961 "This function can be used to read past a pickle without instantiating\n"
4962 "any objects or importing any modules. It can also be used to find all\n"
4963 "persistent references without instantiating any objects or importing\n"
Neal Norwitz200788c2002-08-13 22:20:41 +00004964 "any modules.\n")
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004965 },
Guido van Rossum60456fd1997-04-09 17:36:32 +00004966 {NULL, NULL} /* sentinel */
4967};
4968
4969
4970static Unpicklerobject *
Tim Peterscba30e22003-02-01 06:24:36 +00004971newUnpicklerobject(PyObject *f)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004972{
4973 Unpicklerobject *self;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004974
Jeremy Hylton7b5ce7f2003-04-09 21:25:30 +00004975 if (!( self = PyObject_GC_New(Unpicklerobject, &Unpicklertype)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004976 return NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004977
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004978 self->file = NULL;
4979 self->arg = NULL;
4980 self->stack = (Pdata*)Pdata_New();
4981 self->pers_func = NULL;
4982 self->last_string = NULL;
4983 self->marks = NULL;
4984 self->num_marks = 0;
4985 self->marks_size = 0;
4986 self->buf_size = 0;
4987 self->read = NULL;
4988 self->readline = NULL;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004989 self->find_class = NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004990
Tim Peterscba30e22003-02-01 06:24:36 +00004991 if (!( self->memo = PyDict_New()))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004992 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004993
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004994 if (!self->stack)
4995 goto err;
4996
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004997 Py_INCREF(f);
4998 self->file = f;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004999
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005000 /* Set read, readline based on type of f */
5001 if (PyFile_Check(f)) {
5002 self->fp = PyFile_AsFile(f);
5003 if (self->fp == NULL) {
Tim Peterscba30e22003-02-01 06:24:36 +00005004 PyErr_SetString(PyExc_ValueError,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005005 "I/O operation on closed file");
5006 goto err;
5007 }
5008 self->read_func = read_file;
5009 self->readline_func = readline_file;
Guido van Rossumc91fcaa1999-03-29 20:00:14 +00005010 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005011 else if (PycStringIO_InputCheck(f)) {
5012 self->fp = NULL;
5013 self->read_func = read_cStringIO;
5014 self->readline_func = readline_cStringIO;
5015 }
5016 else {
Guido van Rossum60456fd1997-04-09 17:36:32 +00005017
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005018 self->fp = NULL;
5019 self->read_func = read_other;
5020 self->readline_func = readline_other;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005021
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005022 if (!( (self->readline = PyObject_GetAttr(f, readline_str)) &&
5023 (self->read = PyObject_GetAttr(f, read_str)))) {
5024 PyErr_Clear();
Tim Peterscba30e22003-02-01 06:24:36 +00005025 PyErr_SetString( PyExc_TypeError,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005026 "argument must have 'read' and "
5027 "'readline' attributes" );
5028 goto err;
5029 }
5030 }
Jeremy Hylton7b5ce7f2003-04-09 21:25:30 +00005031 PyObject_GC_Track(self);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005032
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005033 return self;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005034
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005035 err:
5036 Py_DECREF((PyObject *)self);
5037 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005038}
5039
5040
5041static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00005042get_Unpickler(PyObject *self, PyObject *file)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005043{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005044 return (PyObject *)newUnpicklerobject(file);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005045}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005046
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005047
Guido van Rossum60456fd1997-04-09 17:36:32 +00005048static void
Tim Peterscba30e22003-02-01 06:24:36 +00005049Unpickler_dealloc(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005050{
Jeremy Hylton7b5ce7f2003-04-09 21:25:30 +00005051 PyObject_GC_UnTrack((PyObject *)self);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005052 Py_XDECREF(self->readline);
5053 Py_XDECREF(self->read);
5054 Py_XDECREF(self->file);
5055 Py_XDECREF(self->memo);
5056 Py_XDECREF(self->stack);
5057 Py_XDECREF(self->pers_func);
5058 Py_XDECREF(self->arg);
5059 Py_XDECREF(self->last_string);
Jeremy Hyltonfff093f2003-07-11 19:42:49 +00005060 Py_XDECREF(self->find_class);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005061
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005062 if (self->marks) {
5063 free(self->marks);
5064 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005065
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005066 if (self->buf_size) {
5067 free(self->buf);
5068 }
Tim Peters84e87f32001-03-17 04:50:51 +00005069
Tim Peters3cfe7542003-05-21 21:29:48 +00005070 self->ob_type->tp_free((PyObject *)self);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005071}
5072
Jeremy Hylton7b5ce7f2003-04-09 21:25:30 +00005073static int
5074Unpickler_traverse(Unpicklerobject *self, visitproc visit, void *arg)
5075{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005076 Py_VISIT(self->readline);
5077 Py_VISIT(self->read);
5078 Py_VISIT(self->file);
5079 Py_VISIT(self->memo);
5080 Py_VISIT(self->stack);
5081 Py_VISIT(self->pers_func);
5082 Py_VISIT(self->arg);
5083 Py_VISIT(self->last_string);
5084 Py_VISIT(self->find_class);
Jeremy Hylton7b5ce7f2003-04-09 21:25:30 +00005085 return 0;
5086}
5087
5088static int
5089Unpickler_clear(Unpicklerobject *self)
5090{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005091 Py_CLEAR(self->readline);
5092 Py_CLEAR(self->read);
5093 Py_CLEAR(self->file);
5094 Py_CLEAR(self->memo);
5095 Py_CLEAR(self->stack);
5096 Py_CLEAR(self->pers_func);
5097 Py_CLEAR(self->arg);
5098 Py_CLEAR(self->last_string);
5099 Py_CLEAR(self->find_class);
Jeremy Hylton7b5ce7f2003-04-09 21:25:30 +00005100 return 0;
5101}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005102
5103static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00005104Unpickler_getattr(Unpicklerobject *self, char *name)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005105{
5106 if (!strcmp(name, "persistent_load")) {
5107 if (!self->pers_func) {
5108 PyErr_SetString(PyExc_AttributeError, name);
5109 return NULL;
5110 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005111
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005112 Py_INCREF(self->pers_func);
5113 return self->pers_func;
5114 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005115
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005116 if (!strcmp(name, "find_global")) {
5117 if (!self->find_class) {
5118 PyErr_SetString(PyExc_AttributeError, name);
5119 return NULL;
5120 }
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00005121
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005122 Py_INCREF(self->find_class);
5123 return self->find_class;
5124 }
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00005125
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005126 if (!strcmp(name, "memo")) {
5127 if (!self->memo) {
5128 PyErr_SetString(PyExc_AttributeError, name);
5129 return NULL;
5130 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005131
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005132 Py_INCREF(self->memo);
5133 return self->memo;
5134 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005135
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005136 if (!strcmp(name, "UnpicklingError")) {
5137 Py_INCREF(UnpicklingError);
5138 return UnpicklingError;
5139 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005140
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005141 return Py_FindMethod(Unpickler_methods, (PyObject *)self, name);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005142}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005143
Guido van Rossum60456fd1997-04-09 17:36:32 +00005144
5145static int
Tim Peterscba30e22003-02-01 06:24:36 +00005146Unpickler_setattr(Unpicklerobject *self, char *name, PyObject *value)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005147{
Jeremy Hyltonce616e41998-08-13 23:13:52 +00005148
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005149 if (!strcmp(name, "persistent_load")) {
5150 Py_XDECREF(self->pers_func);
5151 self->pers_func = value;
5152 Py_XINCREF(value);
5153 return 0;
5154 }
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00005155
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005156 if (!strcmp(name, "find_global")) {
5157 Py_XDECREF(self->find_class);
5158 self->find_class = value;
5159 Py_XINCREF(value);
5160 return 0;
5161 }
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00005162
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005163 if (! value) {
5164 PyErr_SetString(PyExc_TypeError,
5165 "attribute deletion is not supported");
5166 return -1;
5167 }
Jeremy Hyltonce616e41998-08-13 23:13:52 +00005168
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005169 if (strcmp(name, "memo") == 0) {
5170 if (!PyDict_Check(value)) {
Tim Peterscba30e22003-02-01 06:24:36 +00005171 PyErr_SetString(PyExc_TypeError,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005172 "memo must be a dictionary");
5173 return -1;
5174 }
5175 Py_XDECREF(self->memo);
5176 self->memo = value;
5177 Py_INCREF(value);
5178 return 0;
5179 }
Jeremy Hyltonce616e41998-08-13 23:13:52 +00005180
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005181 PyErr_SetString(PyExc_AttributeError, name);
5182 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005183}
5184
Tim Peters5bd2a792003-02-01 16:45:06 +00005185/* ---------------------------------------------------------------------------
5186 * Module-level functions.
5187 */
Guido van Rossum60456fd1997-04-09 17:36:32 +00005188
Martin v. Löwis544f1192004-07-27 05:22:33 +00005189/* dump(obj, file, protocol=0). */
Guido van Rossum60456fd1997-04-09 17:36:32 +00005190static PyObject *
Martin v. Löwis544f1192004-07-27 05:22:33 +00005191cpm_dump(PyObject *self, PyObject *args, PyObject *kwds)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005192{
Martin v. Löwis15e62742006-02-27 16:46:16 +00005193 static char *kwlist[] = {"obj", "file", "protocol", NULL};
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005194 PyObject *ob, *file, *res = NULL;
5195 Picklerobject *pickler = 0;
Tim Peters5bd2a792003-02-01 16:45:06 +00005196 int proto = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005197
Martin v. Löwis544f1192004-07-27 05:22:33 +00005198 if (!( PyArg_ParseTupleAndKeywords(args, kwds, "OO|i", kwlist,
5199 &ob, &file, &proto)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005200 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005201
Tim Peters5bd2a792003-02-01 16:45:06 +00005202 if (!( pickler = newPicklerobject(file, proto)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005203 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005204
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005205 if (dump(pickler, ob) < 0)
5206 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005207
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005208 Py_INCREF(Py_None);
5209 res = Py_None;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005210
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005211 finally:
5212 Py_XDECREF(pickler);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005213
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005214 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005215}
5216
5217
Martin v. Löwis544f1192004-07-27 05:22:33 +00005218/* dumps(obj, protocol=0). */
Guido van Rossum60456fd1997-04-09 17:36:32 +00005219static PyObject *
Martin v. Löwis544f1192004-07-27 05:22:33 +00005220cpm_dumps(PyObject *self, PyObject *args, PyObject *kwds)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005221{
Martin v. Löwis15e62742006-02-27 16:46:16 +00005222 static char *kwlist[] = {"obj", "protocol", NULL};
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005223 PyObject *ob, *file = 0, *res = NULL;
5224 Picklerobject *pickler = 0;
Tim Peters5bd2a792003-02-01 16:45:06 +00005225 int proto = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005226
Martin v. Löwis544f1192004-07-27 05:22:33 +00005227 if (!( PyArg_ParseTupleAndKeywords(args, kwds, "O|i:dumps", kwlist,
5228 &ob, &proto)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005229 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005230
Tim Peterscba30e22003-02-01 06:24:36 +00005231 if (!( file = PycStringIO->NewOutput(128)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005232 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005233
Tim Peters5bd2a792003-02-01 16:45:06 +00005234 if (!( pickler = newPicklerobject(file, proto)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005235 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005236
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005237 if (dump(pickler, ob) < 0)
5238 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005239
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005240 res = PycStringIO->cgetvalue(file);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005241
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005242 finally:
5243 Py_XDECREF(pickler);
5244 Py_XDECREF(file);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005245
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005246 return res;
Tim Peters84e87f32001-03-17 04:50:51 +00005247}
5248
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005249
Tim Peters5bd2a792003-02-01 16:45:06 +00005250/* load(fileobj). */
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005251static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00005252cpm_load(PyObject *self, PyObject *ob)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005253{
5254 Unpicklerobject *unpickler = 0;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00005255 PyObject *res = NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005256
Tim Peterscba30e22003-02-01 06:24:36 +00005257 if (!( unpickler = newUnpicklerobject(ob)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005258 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005259
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005260 res = load(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005261
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005262 finally:
5263 Py_XDECREF(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005264
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005265 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005266}
5267
5268
Tim Peters5bd2a792003-02-01 16:45:06 +00005269/* loads(string) */
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005270static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00005271cpm_loads(PyObject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005272{
5273 PyObject *ob, *file = 0, *res = NULL;
5274 Unpicklerobject *unpickler = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005275
Tim Peterscba30e22003-02-01 06:24:36 +00005276 if (!( PyArg_ParseTuple(args, "S:loads", &ob)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005277 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005278
Tim Peterscba30e22003-02-01 06:24:36 +00005279 if (!( file = PycStringIO->NewInput(ob)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005280 goto finally;
Tim Peters84e87f32001-03-17 04:50:51 +00005281
Tim Peterscba30e22003-02-01 06:24:36 +00005282 if (!( unpickler = newUnpicklerobject(file)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005283 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005284
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005285 res = load(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005286
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005287 finally:
5288 Py_XDECREF(file);
5289 Py_XDECREF(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005290
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005291 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005292}
5293
5294
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005295PyDoc_STRVAR(Unpicklertype__doc__,
5296"Objects that know how to unpickle");
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005297
Guido van Rossum9716aaa1997-12-08 15:15:16 +00005298static PyTypeObject Unpicklertype = {
5299 PyObject_HEAD_INIT(NULL)
Jeremy Hylton7b5ce7f2003-04-09 21:25:30 +00005300 0, /*ob_size*/
5301 "cPickle.Unpickler", /*tp_name*/
5302 sizeof(Unpicklerobject), /*tp_basicsize*/
5303 0,
5304 (destructor)Unpickler_dealloc, /* tp_dealloc */
5305 0, /* tp_print */
5306 (getattrfunc)Unpickler_getattr, /* tp_getattr */
5307 (setattrfunc)Unpickler_setattr, /* tp_setattr */
5308 0, /* tp_compare */
5309 0, /* tp_repr */
5310 0, /* tp_as_number */
5311 0, /* tp_as_sequence */
5312 0, /* tp_as_mapping */
5313 0, /* tp_hash */
5314 0, /* tp_call */
5315 0, /* tp_str */
5316 0, /* tp_getattro */
5317 0, /* tp_setattro */
5318 0, /* tp_as_buffer */
5319 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
5320 Unpicklertype__doc__, /* tp_doc */
5321 (traverseproc)Unpickler_traverse, /* tp_traverse */
5322 (inquiry)Unpickler_clear, /* tp_clear */
Guido van Rossum9716aaa1997-12-08 15:15:16 +00005323};
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005324
Guido van Rossum60456fd1997-04-09 17:36:32 +00005325static struct PyMethodDef cPickle_methods[] = {
Martin v. Löwis544f1192004-07-27 05:22:33 +00005326 {"dump", (PyCFunction)cpm_dump, METH_VARARGS | METH_KEYWORDS,
5327 PyDoc_STR("dump(obj, file, protocol=0) -- "
Tim Peters5bd2a792003-02-01 16:45:06 +00005328 "Write an object in pickle format to the given file.\n"
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005329 "\n"
Tim Peters5bd2a792003-02-01 16:45:06 +00005330 "See the Pickler docstring for the meaning of optional argument proto.")
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005331 },
Tim Peters5bd2a792003-02-01 16:45:06 +00005332
Martin v. Löwis544f1192004-07-27 05:22:33 +00005333 {"dumps", (PyCFunction)cpm_dumps, METH_VARARGS | METH_KEYWORDS,
5334 PyDoc_STR("dumps(obj, protocol=0) -- "
Tim Peters5bd2a792003-02-01 16:45:06 +00005335 "Return a string containing an object in pickle format.\n"
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005336 "\n"
Tim Peters5bd2a792003-02-01 16:45:06 +00005337 "See the Pickler docstring for the meaning of optional argument proto.")
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005338 },
Tim Peters5bd2a792003-02-01 16:45:06 +00005339
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00005340 {"load", (PyCFunction)cpm_load, METH_O,
Neal Norwitz200788c2002-08-13 22:20:41 +00005341 PyDoc_STR("load(file) -- Load a pickle from the given file")},
Tim Peters5bd2a792003-02-01 16:45:06 +00005342
Neal Norwitzb0493252002-03-31 14:44:22 +00005343 {"loads", (PyCFunction)cpm_loads, METH_VARARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00005344 PyDoc_STR("loads(string) -- Load a pickle from the given string")},
Tim Peters5bd2a792003-02-01 16:45:06 +00005345
Martin v. Löwis544f1192004-07-27 05:22:33 +00005346 {"Pickler", (PyCFunction)get_Pickler, METH_VARARGS | METH_KEYWORDS,
5347 PyDoc_STR("Pickler(file, protocol=0) -- Create a pickler.\n"
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005348 "\n"
Tim Peters5bd2a792003-02-01 16:45:06 +00005349 "This takes a file-like object for writing a pickle data stream.\n"
5350 "The optional proto argument tells the pickler to use the given\n"
5351 "protocol; supported protocols are 0, 1, 2. The default\n"
5352 "protocol is 0, to be backwards compatible. (Protocol 0 is the\n"
5353 "only protocol that can be written to a file opened in text\n"
5354 "mode and read back successfully. When using a protocol higher\n"
5355 "than 0, make sure the file is opened in binary mode, both when\n"
5356 "pickling and unpickling.)\n"
5357 "\n"
5358 "Protocol 1 is more efficient than protocol 0; protocol 2 is\n"
5359 "more efficient than protocol 1.\n"
5360 "\n"
5361 "Specifying a negative protocol version selects the highest\n"
5362 "protocol version supported. The higher the protocol used, the\n"
5363 "more recent the version of Python needed to read the pickle\n"
5364 "produced.\n"
5365 "\n"
5366 "The file parameter must have a write() method that accepts a single\n"
5367 "string argument. It can thus be an open file object, a StringIO\n"
5368 "object, or any other custom object that meets this interface.\n")
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005369 },
Tim Peters5bd2a792003-02-01 16:45:06 +00005370
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00005371 {"Unpickler", (PyCFunction)get_Unpickler, METH_O,
Tim Peters5bd2a792003-02-01 16:45:06 +00005372 PyDoc_STR("Unpickler(file) -- Create an unpickler.")},
5373
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005374 { NULL, NULL }
5375};
5376
Guido van Rossum60456fd1997-04-09 17:36:32 +00005377static int
Tim Peterscba30e22003-02-01 06:24:36 +00005378init_stuff(PyObject *module_dict)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005379{
5380 PyObject *copy_reg, *t, *r;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005381
Martin v. Löwis43c9d8a2002-04-01 12:34:33 +00005382#define INIT_STR(S) if (!( S ## _str=PyString_InternFromString(#S))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005383
Tim Peters3cfe7542003-05-21 21:29:48 +00005384 if (PyType_Ready(&Unpicklertype) < 0)
5385 return -1;
5386 if (PyType_Ready(&Picklertype) < 0)
5387 return -1;
5388
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005389 INIT_STR(__class__);
5390 INIT_STR(__getinitargs__);
5391 INIT_STR(__dict__);
5392 INIT_STR(__getstate__);
5393 INIT_STR(__setstate__);
5394 INIT_STR(__name__);
5395 INIT_STR(__main__);
5396 INIT_STR(__reduce__);
Guido van Rossumb289b872003-02-19 01:45:13 +00005397 INIT_STR(__reduce_ex__);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005398 INIT_STR(write);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005399 INIT_STR(append);
5400 INIT_STR(read);
5401 INIT_STR(readline);
5402 INIT_STR(copy_reg);
5403 INIT_STR(dispatch_table);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005404
Tim Peterscba30e22003-02-01 06:24:36 +00005405 if (!( copy_reg = PyImport_ImportModule("copy_reg")))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005406 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005407
Tim Peters1f1b2d22003-02-01 02:16:37 +00005408 /* This is special because we want to use a different
5409 one in restricted mode. */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005410 dispatch_table = PyObject_GetAttr(copy_reg, dispatch_table_str);
Tim Peters5b7da392003-02-04 00:21:07 +00005411 if (!dispatch_table) return -1;
5412
5413 extension_registry = PyObject_GetAttrString(copy_reg,
Guido van Rossumd4b920c2003-02-04 01:54:49 +00005414 "_extension_registry");
Tim Peters5b7da392003-02-04 00:21:07 +00005415 if (!extension_registry) return -1;
5416
5417 inverted_registry = PyObject_GetAttrString(copy_reg,
Guido van Rossumd4b920c2003-02-04 01:54:49 +00005418 "_inverted_registry");
Tim Peters5b7da392003-02-04 00:21:07 +00005419 if (!inverted_registry) return -1;
5420
5421 extension_cache = PyObject_GetAttrString(copy_reg,
Guido van Rossumd4b920c2003-02-04 01:54:49 +00005422 "_extension_cache");
Tim Peters5b7da392003-02-04 00:21:07 +00005423 if (!extension_cache) return -1;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005424
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005425 Py_DECREF(copy_reg);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005426
Tim Peters731098b2003-02-04 20:56:09 +00005427 if (!(empty_tuple = PyTuple_New(0)))
5428 return -1;
5429
5430 two_tuple = PyTuple_New(2);
5431 if (two_tuple == NULL)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005432 return -1;
Tim Peters3e667d52003-02-04 21:47:44 +00005433 /* We use this temp container with no regard to refcounts, or to
5434 * keeping containees alive. Exempt from GC, because we don't
5435 * want anything looking at two_tuple() by magic.
5436 */
5437 PyObject_GC_UnTrack(two_tuple);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005438
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005439 /* Ugh */
5440 if (!( t=PyImport_ImportModule("__builtin__"))) return -1;
5441 if (PyDict_SetItemString(module_dict, "__builtins__", t) < 0)
5442 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005443
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005444 if (!( t=PyDict_New())) return -1;
5445 if (!( r=PyRun_String(
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005446 "def __str__(self):\n"
5447 " return self.args and ('%s' % self.args[0]) or '(what)'\n",
5448 Py_file_input,
5449 module_dict, t) )) return -1;
5450 Py_DECREF(r);
Guido van Rossumc03158b1999-06-09 15:23:31 +00005451
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005452 PickleError = PyErr_NewException("cPickle.PickleError", NULL, t);
Tim Peterscba30e22003-02-01 06:24:36 +00005453 if (!PickleError)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005454 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00005455
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005456 Py_DECREF(t);
Guido van Rossumc03158b1999-06-09 15:23:31 +00005457
Tim Peterscba30e22003-02-01 06:24:36 +00005458 PicklingError = PyErr_NewException("cPickle.PicklingError",
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005459 PickleError, NULL);
Tim Peterscba30e22003-02-01 06:24:36 +00005460 if (!PicklingError)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005461 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00005462
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005463 if (!( t=PyDict_New())) return -1;
5464 if (!( r=PyRun_String(
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005465 "def __str__(self):\n"
5466 " a=self.args\n"
5467 " a=a and type(a[0]) or '(what)'\n"
5468 " return 'Cannot pickle %s objects' % a\n"
5469 , Py_file_input,
5470 module_dict, t) )) return -1;
5471 Py_DECREF(r);
Tim Peters84e87f32001-03-17 04:50:51 +00005472
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005473 if (!( UnpickleableError = PyErr_NewException(
Tim Peterscba30e22003-02-01 06:24:36 +00005474 "cPickle.UnpickleableError", PicklingError, t)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005475 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00005476
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005477 Py_DECREF(t);
Guido van Rossumc03158b1999-06-09 15:23:31 +00005478
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005479 if (!( UnpicklingError = PyErr_NewException("cPickle.UnpicklingError",
Tim Peterscba30e22003-02-01 06:24:36 +00005480 PickleError, NULL)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005481 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00005482
Martin v. Löwis658009a2002-09-16 17:26:24 +00005483 if (!( BadPickleGet = PyErr_NewException("cPickle.BadPickleGet",
5484 UnpicklingError, NULL)))
5485 return -1;
Tim Peterscba30e22003-02-01 06:24:36 +00005486
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005487 if (PyDict_SetItemString(module_dict, "PickleError",
5488 PickleError) < 0)
5489 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00005490
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005491 if (PyDict_SetItemString(module_dict, "PicklingError",
5492 PicklingError) < 0)
5493 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00005494
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005495 if (PyDict_SetItemString(module_dict, "UnpicklingError",
5496 UnpicklingError) < 0)
5497 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005498
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005499 if (PyDict_SetItemString(module_dict, "UnpickleableError",
5500 UnpickleableError) < 0)
5501 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005502
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005503 if (PyDict_SetItemString(module_dict, "BadPickleGet",
5504 BadPickleGet) < 0)
5505 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00005506
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005507 PycString_IMPORT;
Guido van Rossum053b8df1998-11-25 16:18:00 +00005508
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005509 return 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005510}
5511
Mark Hammondfe51c6d2002-08-02 02:27:13 +00005512#ifndef PyMODINIT_FUNC /* declarations for DLL import/export */
5513#define PyMODINIT_FUNC void
Guido van Rossumf9ffb031999-02-04 14:54:04 +00005514#endif
Mark Hammondfe51c6d2002-08-02 02:27:13 +00005515PyMODINIT_FUNC
Tim Peterscba30e22003-02-01 06:24:36 +00005516initcPickle(void)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005517{
5518 PyObject *m, *d, *di, *v, *k;
Martin v. Löwis18e16552006-02-15 17:27:45 +00005519 Py_ssize_t i;
Tim Peters5b7da392003-02-04 00:21:07 +00005520 char *rev = "1.71"; /* XXX when does this change? */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005521 PyObject *format_version;
5522 PyObject *compatible_formats;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005523
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005524 Picklertype.ob_type = &PyType_Type;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005525 Unpicklertype.ob_type = &PyType_Type;
5526 PdataType.ob_type = &PyType_Type;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005527
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005528 /* Initialize some pieces. We need to do this before module creation,
Tim Peters5b7da392003-02-04 00:21:07 +00005529 * so we're forced to use a temporary dictionary. :(
5530 */
5531 di = PyDict_New();
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005532 if (!di) return;
5533 if (init_stuff(di) < 0) return;
Guido van Rossumebba4202000-09-07 14:35:37 +00005534
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005535 /* Create the module and add the functions */
5536 m = Py_InitModule4("cPickle", cPickle_methods,
5537 cPickle_module_documentation,
5538 (PyObject*)NULL,PYTHON_API_VERSION);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00005539 if (m == NULL)
5540 return;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005541
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005542 /* Add some symbolic constants to the module */
5543 d = PyModule_GetDict(m);
Tim Peters5b7da392003-02-04 00:21:07 +00005544 v = PyString_FromString(rev);
5545 PyDict_SetItemString(d, "__version__", v);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005546 Py_XDECREF(v);
Barry Warsaw93d29b61997-01-14 17:45:08 +00005547
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005548 /* Copy data from di. Waaa. */
5549 for (i=0; PyDict_Next(di, &i, &k, &v); ) {
5550 if (PyObject_SetItem(d, k, v) < 0) {
5551 Py_DECREF(di);
5552 return;
5553 }
5554 }
5555 Py_DECREF(di);
Guido van Rossumebba4202000-09-07 14:35:37 +00005556
Tim Peters8587b3c2003-02-13 15:44:41 +00005557 i = PyModule_AddIntConstant(m, "HIGHEST_PROTOCOL", HIGHEST_PROTOCOL);
5558 if (i < 0)
5559 return;
5560
Tim Peters5b7da392003-02-04 00:21:07 +00005561 /* These are purely informational; no code uses them. */
5562 /* File format version we write. */
5563 format_version = PyString_FromString("2.0");
5564 /* Format versions we can read. */
5565 compatible_formats = Py_BuildValue("[sssss]",
5566 "1.0", /* Original protocol 0 */
5567 "1.1", /* Protocol 0 + INST */
5568 "1.2", /* Original protocol 1 */
5569 "1.3", /* Protocol 1 + BINFLOAT */
Tim Peters0dd23aa2003-02-04 00:30:46 +00005570 "2.0"); /* Original protocol 2 */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005571 PyDict_SetItemString(d, "format_version", format_version);
5572 PyDict_SetItemString(d, "compatible_formats", compatible_formats);
5573 Py_XDECREF(format_version);
5574 Py_XDECREF(compatible_formats);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005575}