blob: a35905d78856f237be1e4b22eb2d8fd6ca074dd2 [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. */
18#define CURRENT_PROTOCOL_NUMBER 2
19
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
91 * batch_{list, dict} pump out before doing APPENDS/SETITEMS.
92 */
93#define BATCHSIZE 1000
94
Guido van Rossum60456fd1997-04-09 17:36:32 +000095static char MARKv = MARK;
Guido van Rossum2f4caa41997-01-06 22:59:08 +000096
Guido van Rossumc03158b1999-06-09 15:23:31 +000097static PyObject *PickleError;
Guido van Rossum2f4caa41997-01-06 22:59:08 +000098static PyObject *PicklingError;
Guido van Rossumc03158b1999-06-09 15:23:31 +000099static PyObject *UnpickleableError;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000100static PyObject *UnpicklingError;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000101static PyObject *BadPickleGet;
102
Tim Peters5b7da392003-02-04 00:21:07 +0000103/* As the name says, an empty tuple. */
Guido van Rossum60456fd1997-04-09 17:36:32 +0000104static PyObject *empty_tuple;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000105
Tim Peters5b7da392003-02-04 00:21:07 +0000106/* copy_reg.dispatch_table, {type_object: pickling_function} */
107static PyObject *dispatch_table;
108
109/* For EXT[124] opcodes. */
Guido van Rossumd4b920c2003-02-04 01:54:49 +0000110/* copy_reg._extension_registry, {(module_name, function_name): code} */
Tim Peters5b7da392003-02-04 00:21:07 +0000111static PyObject *extension_registry;
Guido van Rossumd4b920c2003-02-04 01:54:49 +0000112/* copy_reg._inverted_registry, {code: (module_name, function_name)} */
Tim Peters5b7da392003-02-04 00:21:07 +0000113static PyObject *inverted_registry;
Guido van Rossumd4b920c2003-02-04 01:54:49 +0000114/* copy_reg._extension_cache, {code: object} */
Tim Peters5b7da392003-02-04 00:21:07 +0000115static PyObject *extension_cache;
116
Tim Peters731098b2003-02-04 20:56:09 +0000117/* For looking up name pairs in copy_reg._extension_registry. */
118static PyObject *two_tuple;
119
Guido van Rossum60456fd1997-04-09 17:36:32 +0000120static PyObject *__class___str, *__getinitargs___str, *__dict___str,
121 *__getstate___str, *__setstate___str, *__name___str, *__reduce___str,
Tim Peters1f1b2d22003-02-01 02:16:37 +0000122 *write_str, *append_str,
Guido van Rossum9716aaa1997-12-08 15:15:16 +0000123 *read_str, *readline_str, *__main___str, *__basicnew___str,
Tim Peters1f1b2d22003-02-01 02:16:37 +0000124 *copy_reg_str, *dispatch_table_str;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000125
Guido van Rossum053b8df1998-11-25 16:18:00 +0000126/*************************************************************************
127 Internal Data type for pickle data. */
128
129typedef struct {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000130 PyObject_HEAD
Tim Peters1d63c9f2003-02-02 20:29:39 +0000131 int length; /* number of initial slots in data currently used */
132 int size; /* number of slots in data allocated */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000133 PyObject **data;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000134} Pdata;
135
Tim Peters84e87f32001-03-17 04:50:51 +0000136static void
Tim Peterscba30e22003-02-01 06:24:36 +0000137Pdata_dealloc(Pdata *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000138{
139 int i;
140 PyObject **p;
Tim Peterscba30e22003-02-01 06:24:36 +0000141
Tim Peters1d63c9f2003-02-02 20:29:39 +0000142 for (i = self->length, p = self->data; --i >= 0; p++) {
143 Py_DECREF(*p);
144 }
145 if (self->data)
146 free(self->data);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000147 PyObject_Del(self);
Guido van Rossum053b8df1998-11-25 16:18:00 +0000148}
149
150static PyTypeObject PdataType = {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000151 PyObject_HEAD_INIT(NULL) 0, "cPickle.Pdata", sizeof(Pdata), 0,
152 (destructor)Pdata_dealloc,
153 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0L,0L,0L,0L, ""
Guido van Rossum053b8df1998-11-25 16:18:00 +0000154};
155
156#define Pdata_Check(O) ((O)->ob_type == &PdataType)
157
158static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +0000159Pdata_New(void)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000160{
161 Pdata *self;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000162
Tim Peters1d63c9f2003-02-02 20:29:39 +0000163 if (!(self = PyObject_New(Pdata, &PdataType)))
164 return NULL;
165 self->size = 8;
166 self->length = 0;
167 self->data = malloc(self->size * sizeof(PyObject*));
168 if (self->data)
169 return (PyObject*)self;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000170 Py_DECREF(self);
171 return PyErr_NoMemory();
Guido van Rossum053b8df1998-11-25 16:18:00 +0000172}
173
Tim Peters84e87f32001-03-17 04:50:51 +0000174static int
Tim Peterscba30e22003-02-01 06:24:36 +0000175stackUnderflow(void)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000176{
177 PyErr_SetString(UnpicklingError, "unpickling stack underflow");
178 return -1;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000179}
180
Tim Peters1d63c9f2003-02-02 20:29:39 +0000181/* Retain only the initial clearto items. If clearto >= the current
182 * number of items, this is a (non-erroneous) NOP.
183 */
Guido van Rossum053b8df1998-11-25 16:18:00 +0000184static int
Tim Peterscba30e22003-02-01 06:24:36 +0000185Pdata_clear(Pdata *self, int clearto)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000186{
187 int i;
188 PyObject **p;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000189
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000190 if (clearto < 0) return stackUnderflow();
191 if (clearto >= self->length) return 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000192
Tim Peters1d63c9f2003-02-02 20:29:39 +0000193 for (i = self->length, p = self->data + clearto;
194 --i >= clearto;
195 p++) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000196 Py_DECREF(*p);
Tim Peters1d63c9f2003-02-02 20:29:39 +0000197 }
198 self->length = clearto;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000199
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000200 return 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000201}
202
Tim Peters84e87f32001-03-17 04:50:51 +0000203static int
Tim Peterscba30e22003-02-01 06:24:36 +0000204Pdata_grow(Pdata *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000205{
Tim Peters1d63c9f2003-02-02 20:29:39 +0000206 int bigger;
207 size_t nbytes;
208
Tim Peters1d63c9f2003-02-02 20:29:39 +0000209 bigger = self->size << 1;
Tim Peterse0a39072003-02-03 15:45:56 +0000210 if (bigger <= 0) /* was 0, or new value overflows */
Tim Peters1d63c9f2003-02-02 20:29:39 +0000211 goto nomemory;
212 if ((int)(size_t)bigger != bigger)
213 goto nomemory;
214 nbytes = (size_t)bigger * sizeof(PyObject *);
215 if (nbytes / sizeof(PyObject *) != (size_t)bigger)
216 goto nomemory;
217 self->data = realloc(self->data, nbytes);
218 if (self->data == NULL)
219 goto nomemory;
220 self->size = bigger;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000221 return 0;
Tim Peters1d63c9f2003-02-02 20:29:39 +0000222
223 nomemory:
224 self->size = 0;
225 PyErr_NoMemory();
226 return -1;
Tim Peters84e87f32001-03-17 04:50:51 +0000227}
Guido van Rossum053b8df1998-11-25 16:18:00 +0000228
Tim Peterse0a39072003-02-03 15:45:56 +0000229/* D is a Pdata*. Pop the topmost element and store it into V, which
230 * must be an lvalue holding PyObject*. On stack underflow, UnpicklingError
Tim Peters1d63c9f2003-02-02 20:29:39 +0000231 * is raised and V is set to NULL. D and V may be evaluated several times.
232 */
233#define PDATA_POP(D, V) { \
Tim Peterse0a39072003-02-03 15:45:56 +0000234 if ((D)->length) \
235 (V) = (D)->data[--((D)->length)]; \
236 else { \
237 PyErr_SetString(UnpicklingError, "bad pickle data"); \
238 (V) = NULL; \
239 } \
Guido van Rossum053b8df1998-11-25 16:18:00 +0000240}
241
Tim Peterse0a39072003-02-03 15:45:56 +0000242/* PDATA_PUSH and PDATA_APPEND both push rvalue PyObject* O on to Pdata*
243 * D. If the Pdata stack can't be grown to hold the new value, both
244 * raise MemoryError and execute "return ER". The difference is in ownership
245 * of O after: _PUSH transfers ownership of O from the caller to the stack
246 * (no incref of O is done, and in case of error O is decrefed), while
247 * _APPEND pushes a new reference.
248 */
249
250/* Push O on stack D, giving ownership of O to the stack. */
251#define PDATA_PUSH(D, O, ER) { \
252 if (((Pdata*)(D))->length == ((Pdata*)(D))->size && \
253 Pdata_grow((Pdata*)(D)) < 0) { \
254 Py_DECREF(O); \
255 return ER; \
256 } \
257 ((Pdata*)(D))->data[((Pdata*)(D))->length++] = (O); \
258}
259
260/* Push O on stack D, pushing a new reference. */
261#define PDATA_APPEND(D, O, ER) { \
262 if (((Pdata*)(D))->length == ((Pdata*)(D))->size && \
263 Pdata_grow((Pdata*)(D)) < 0) \
264 return ER; \
265 Py_INCREF(O); \
266 ((Pdata*)(D))->data[((Pdata*)(D))->length++] = (O); \
267}
268
269
Guido van Rossum053b8df1998-11-25 16:18:00 +0000270static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +0000271Pdata_popTuple(Pdata *self, int start)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000272{
273 PyObject *r;
274 int i, j, l;
Tim Peterscba30e22003-02-01 06:24:36 +0000275
Tim Peters1d63c9f2003-02-02 20:29:39 +0000276 l = self->length-start;
277 r = PyTuple_New(l);
278 if (r == NULL)
279 return NULL;
280 for (i = start, j = 0 ; j < l; i++, j++)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000281 PyTuple_SET_ITEM(r, j, self->data[i]);
Tim Peterscba30e22003-02-01 06:24:36 +0000282
Tim Peters1d63c9f2003-02-02 20:29:39 +0000283 self->length = start;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000284 return r;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000285}
286
287static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +0000288Pdata_popList(Pdata *self, int start)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000289{
290 PyObject *r;
291 int i, j, l;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000292
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000293 l=self->length-start;
294 if (!( r=PyList_New(l))) return NULL;
295 for (i=start, j=0 ; j < l; i++, j++)
296 PyList_SET_ITEM(r, j, self->data[i]);
Guido van Rossum053b8df1998-11-25 16:18:00 +0000297
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000298 self->length=start;
299 return r;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000300}
301
Guido van Rossum053b8df1998-11-25 16:18:00 +0000302/*************************************************************************/
303
304#define ARG_TUP(self, o) { \
305 if (self->arg || (self->arg=PyTuple_New(1))) { \
306 Py_XDECREF(PyTuple_GET_ITEM(self->arg,0)); \
307 PyTuple_SET_ITEM(self->arg,0,o); \
308 } \
309 else { \
310 Py_DECREF(o); \
311 } \
312}
313
314#define FREE_ARG_TUP(self) { \
315 if (self->arg->ob_refcnt > 1) { \
316 Py_DECREF(self->arg); \
317 self->arg=NULL; \
318 } \
319 }
320
Thomas Wouters3b6448f2000-07-22 23:56:07 +0000321typedef struct Picklerobject {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000322 PyObject_HEAD
323 FILE *fp;
324 PyObject *write;
325 PyObject *file;
326 PyObject *memo;
327 PyObject *arg;
328 PyObject *pers_func;
329 PyObject *inst_pers_func;
Tim Peters797ec242003-02-01 06:22:36 +0000330
331 /* pickle protocol number, >= 0 */
332 int proto;
333
334 /* bool, true if proto > 0 */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000335 int bin;
Tim Peters797ec242003-02-01 06:22:36 +0000336
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000337 int fast; /* Fast mode doesn't save in memo, don't use if circ ref */
Martin v. Löwis5a395302002-08-04 08:20:23 +0000338 int nesting;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000339 int (*write_func)(struct Picklerobject *, char *, int);
340 char *write_buf;
341 int buf_size;
342 PyObject *dispatch_table;
343 int fast_container; /* count nested container dumps */
344 PyObject *fast_memo;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000345} Picklerobject;
346
Barry Warsaw52acb492001-12-21 20:04:22 +0000347#ifndef PY_CPICKLE_FAST_LIMIT
348#define PY_CPICKLE_FAST_LIMIT 50
349#endif
Jeremy Hyltona0fb1772001-10-12 04:11:06 +0000350
Jeremy Hylton938ace62002-07-17 16:30:39 +0000351static PyTypeObject Picklertype;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000352
Thomas Wouters3b6448f2000-07-22 23:56:07 +0000353typedef struct Unpicklerobject {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000354 PyObject_HEAD
355 FILE *fp;
356 PyObject *file;
357 PyObject *readline;
358 PyObject *read;
359 PyObject *memo;
360 PyObject *arg;
361 Pdata *stack;
362 PyObject *mark;
363 PyObject *pers_func;
364 PyObject *last_string;
365 int *marks;
366 int num_marks;
367 int marks_size;
368 int (*read_func)(struct Unpicklerobject *, char **, int);
369 int (*readline_func)(struct Unpicklerobject *, char **);
370 int buf_size;
371 char *buf;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000372 PyObject *find_class;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000373} Unpicklerobject;
Tim Peters84e87f32001-03-17 04:50:51 +0000374
Jeremy Hylton938ace62002-07-17 16:30:39 +0000375static PyTypeObject Unpicklertype;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000376
Thomas Wouters4789b3a2000-07-24 11:36:47 +0000377/* Forward decls that need the above structs */
378static int save(Picklerobject *, PyObject *, int);
379static int put2(Picklerobject *, PyObject *);
380
Guido van Rossumd385d591997-04-09 17:47:47 +0000381static
Guido van Rossum60456fd1997-04-09 17:36:32 +0000382PyObject *
Thomas Wouters3b6448f2000-07-22 23:56:07 +0000383cPickle_ErrFormat(PyObject *ErrType, char *stringformat, char *format, ...)
384{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000385 va_list va;
386 PyObject *args=0, *retval=0;
387 va_start(va, format);
Tim Peterscba30e22003-02-01 06:24:36 +0000388
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000389 if (format) args = Py_VaBuildValue(format, va);
390 va_end(va);
391 if (format && ! args) return NULL;
Tim Peterscba30e22003-02-01 06:24:36 +0000392 if (stringformat && !(retval=PyString_FromString(stringformat)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000393 return NULL;
Tim Peterscba30e22003-02-01 06:24:36 +0000394
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000395 if (retval) {
396 if (args) {
397 PyObject *v;
398 v=PyString_Format(retval, args);
399 Py_DECREF(retval);
400 Py_DECREF(args);
401 if (! v) return NULL;
402 retval=v;
403 }
404 }
405 else
406 if (args) retval=args;
407 else {
408 PyErr_SetObject(ErrType,Py_None);
409 return NULL;
410 }
411 PyErr_SetObject(ErrType,retval);
412 Py_DECREF(retval);
413 return NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000414}
415
Tim Peters84e87f32001-03-17 04:50:51 +0000416static int
Tim Peterscba30e22003-02-01 06:24:36 +0000417write_file(Picklerobject *self, char *s, int n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000418{
419 size_t nbyteswritten;
Tim Peters84e87f32001-03-17 04:50:51 +0000420
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000421 if (s == NULL) {
422 return 0;
423 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000424
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000425 Py_BEGIN_ALLOW_THREADS
Jeremy Hylton7fa4bfa2002-07-18 20:58:57 +0000426 nbyteswritten = fwrite(s, sizeof(char), n, self->fp);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000427 Py_END_ALLOW_THREADS
Jeremy Hylton7fa4bfa2002-07-18 20:58:57 +0000428 if (nbyteswritten != (size_t)n) {
429 PyErr_SetFromErrno(PyExc_IOError);
430 return -1;
431 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000432
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000433 return n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000434}
435
Tim Peters84e87f32001-03-17 04:50:51 +0000436static int
Tim Peterscba30e22003-02-01 06:24:36 +0000437write_cStringIO(Picklerobject *self, char *s, int n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000438{
439 if (s == NULL) {
440 return 0;
441 }
Tim Peterscba30e22003-02-01 06:24:36 +0000442
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000443 if (PycStringIO->cwrite((PyObject *)self->file, s, n) != n) {
444 return -1;
445 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000446
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000447 return n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000448}
449
Tim Peters84e87f32001-03-17 04:50:51 +0000450static int
Tim Peterscba30e22003-02-01 06:24:36 +0000451write_none(Picklerobject *self, char *s, int n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000452{
453 if (s == NULL) return 0;
454 return n;
Guido van Rossum142eeb81997-08-13 03:14:41 +0000455}
456
Tim Peters84e87f32001-03-17 04:50:51 +0000457static int
Tim Peterscba30e22003-02-01 06:24:36 +0000458write_other(Picklerobject *self, char *s, int n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000459{
460 PyObject *py_str = 0, *junk = 0;
Tim Peterscba30e22003-02-01 06:24:36 +0000461
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000462 if (s == NULL) {
463 if (!( self->buf_size )) return 0;
Tim Peterscba30e22003-02-01 06:24:36 +0000464 py_str = PyString_FromStringAndSize(self->write_buf,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000465 self->buf_size);
Tim Peterscba30e22003-02-01 06:24:36 +0000466 if (!py_str)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000467 return -1;
468 }
469 else {
470 if (self->buf_size && (n + self->buf_size) > WRITE_BUF_SIZE) {
471 if (write_other(self, NULL, 0) < 0)
472 return -1;
473 }
Tim Peterscba30e22003-02-01 06:24:36 +0000474
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000475 if (n > WRITE_BUF_SIZE) {
476 if (!( py_str =
Tim Peterscba30e22003-02-01 06:24:36 +0000477 PyString_FromStringAndSize(s, n)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000478 return -1;
479 }
480 else {
481 memcpy(self->write_buf + self->buf_size, s, n);
482 self->buf_size += n;
483 return n;
484 }
485 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000486
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000487 if (self->write) {
488 /* object with write method */
489 ARG_TUP(self, py_str);
490 if (self->arg) {
491 junk = PyObject_Call(self->write, self->arg, NULL);
492 FREE_ARG_TUP(self);
493 }
494 if (junk) Py_DECREF(junk);
495 else return -1;
496 }
497 else
498 PDATA_PUSH(self->file, py_str, -1);
Guido van Rossum60456fd1997-04-09 17:36:32 +0000499
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000500 self->buf_size = 0;
501 return n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000502}
503
504
Tim Peters84e87f32001-03-17 04:50:51 +0000505static int
Tim Petersee1a53c2003-02-02 02:57:53 +0000506read_file(Unpicklerobject *self, char **s, int n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000507{
508 size_t nbytesread;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000509
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000510 if (self->buf_size == 0) {
511 int size;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000512
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000513 size = ((n < 32) ? 32 : n);
Tim Petersee1a53c2003-02-02 02:57:53 +0000514 if (!( self->buf = (char *)malloc(size))) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000515 PyErr_NoMemory();
516 return -1;
517 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000518
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000519 self->buf_size = size;
520 }
521 else if (n > self->buf_size) {
Tim Petersee1a53c2003-02-02 02:57:53 +0000522 self->buf = (char *)realloc(self->buf, n);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000523 if (!self->buf) {
524 PyErr_NoMemory();
525 return -1;
526 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000527 self->buf_size = n;
528 }
Tim Peters84e87f32001-03-17 04:50:51 +0000529
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000530 Py_BEGIN_ALLOW_THREADS
531 nbytesread = fread(self->buf, sizeof(char), n, self->fp);
532 Py_END_ALLOW_THREADS
533 if (nbytesread != (size_t)n) {
534 if (feof(self->fp)) {
535 PyErr_SetNone(PyExc_EOFError);
536 return -1;
537 }
Tim Peterscba30e22003-02-01 06:24:36 +0000538
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000539 PyErr_SetFromErrno(PyExc_IOError);
540 return -1;
541 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000542
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000543 *s = self->buf;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000544
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000545 return n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000546}
547
548
Tim Peters84e87f32001-03-17 04:50:51 +0000549static int
Tim Peterscba30e22003-02-01 06:24:36 +0000550readline_file(Unpicklerobject *self, char **s)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000551{
552 int i;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000553
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000554 if (self->buf_size == 0) {
Tim Petersee1a53c2003-02-02 02:57:53 +0000555 if (!( self->buf = (char *)malloc(40))) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000556 PyErr_NoMemory();
557 return -1;
558 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000559 self->buf_size = 40;
560 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000561
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000562 i = 0;
563 while (1) {
Tim Petersee1a53c2003-02-02 02:57:53 +0000564 int bigger;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000565 for (; i < (self->buf_size - 1); i++) {
Tim Peterscba30e22003-02-01 06:24:36 +0000566 if (feof(self->fp) ||
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000567 (self->buf[i] = getc(self->fp)) == '\n') {
568 self->buf[i + 1] = '\0';
569 *s = self->buf;
570 return i + 1;
571 }
572 }
Tim Petersee1a53c2003-02-02 02:57:53 +0000573 bigger = self->buf_size << 1;
574 if (bigger <= 0) { /* overflow */
575 PyErr_NoMemory();
576 return -1;
577 }
578 self->buf = (char *)realloc(self->buf, bigger);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000579 if (!self->buf) {
580 PyErr_NoMemory();
581 return -1;
582 }
Tim Petersee1a53c2003-02-02 02:57:53 +0000583 self->buf_size = bigger;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000584 }
Tim Peters84e87f32001-03-17 04:50:51 +0000585}
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000586
587
Tim Peters84e87f32001-03-17 04:50:51 +0000588static int
Tim Peterscba30e22003-02-01 06:24:36 +0000589read_cStringIO(Unpicklerobject *self, char **s, int n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000590{
591 char *ptr;
Tim Peterscba30e22003-02-01 06:24:36 +0000592
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000593 if (PycStringIO->cread((PyObject *)self->file, &ptr, n) != n) {
594 PyErr_SetNone(PyExc_EOFError);
595 return -1;
596 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000597
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000598 *s = ptr;
Tim Peterscba30e22003-02-01 06:24:36 +0000599
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000600 return n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000601}
602
603
Tim Peters84e87f32001-03-17 04:50:51 +0000604static int
Tim Peterscba30e22003-02-01 06:24:36 +0000605readline_cStringIO(Unpicklerobject *self, char **s)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000606{
607 int n;
608 char *ptr;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000609
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000610 if ((n = PycStringIO->creadline((PyObject *)self->file, &ptr)) < 0) {
611 return -1;
612 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000613
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000614 *s = ptr;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000615
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000616 return n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000617}
618
619
Tim Peters84e87f32001-03-17 04:50:51 +0000620static int
Tim Peterscba30e22003-02-01 06:24:36 +0000621read_other(Unpicklerobject *self, char **s, int n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000622{
623 PyObject *bytes, *str=0;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000624
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000625 if (!( bytes = PyInt_FromLong(n))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000626
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000627 ARG_TUP(self, bytes);
628 if (self->arg) {
629 str = PyObject_Call(self->read, self->arg, NULL);
630 FREE_ARG_TUP(self);
631 }
632 if (! str) return -1;
Tim Peterscba30e22003-02-01 06:24:36 +0000633
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000634 Py_XDECREF(self->last_string);
635 self->last_string = str;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000636
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000637 if (! (*s = PyString_AsString(str))) return -1;
638 return n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000639}
640
641
Tim Peters84e87f32001-03-17 04:50:51 +0000642static int
Tim Peterscba30e22003-02-01 06:24:36 +0000643readline_other(Unpicklerobject *self, char **s)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000644{
645 PyObject *str;
646 int str_size;
Tim Peterscba30e22003-02-01 06:24:36 +0000647
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000648 if (!( str = PyObject_CallObject(self->readline, empty_tuple))) {
649 return -1;
650 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000651
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000652 if ((str_size = PyString_Size(str)) < 0)
653 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000654
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000655 Py_XDECREF(self->last_string);
656 self->last_string = str;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000657
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000658 if (! (*s = PyString_AsString(str)))
659 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000660
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000661 return str_size;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000662}
663
Tim Petersee1a53c2003-02-02 02:57:53 +0000664/* Copy the first n bytes from s into newly malloc'ed memory, plus a
665 * trailing 0 byte. Return a pointer to that, or NULL if out of memory.
666 * The caller is responsible for free()'ing the return value.
667 */
Guido van Rossum60456fd1997-04-09 17:36:32 +0000668static char *
Tim Petersee1a53c2003-02-02 02:57:53 +0000669pystrndup(char *s, int n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000670{
Tim Petersee1a53c2003-02-02 02:57:53 +0000671 char *r = (char *)malloc(n+1);
672 if (r == NULL)
673 return (char*)PyErr_NoMemory();
674 memcpy(r, s, n);
675 r[n] = 0;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000676 return r;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000677}
678
679
680static int
Tim Peterscba30e22003-02-01 06:24:36 +0000681get(Picklerobject *self, PyObject *id)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000682{
683 PyObject *value, *mv;
684 long c_value;
685 char s[30];
686 size_t len;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000687
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000688 if (!( mv = PyDict_GetItem(self->memo, id))) {
689 PyErr_SetObject(PyExc_KeyError, id);
690 return -1;
691 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000692
Tim Peterscba30e22003-02-01 06:24:36 +0000693 if (!( value = PyTuple_GetItem(mv, 0)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000694 return -1;
Tim Peters84e87f32001-03-17 04:50:51 +0000695
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000696 if (!( PyInt_Check(value))) {
697 PyErr_SetString(PicklingError, "no int where int expected in memo");
698 return -1;
699 }
700 c_value = PyInt_AS_LONG((PyIntObject*)value);
Guido van Rossum60456fd1997-04-09 17:36:32 +0000701
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000702 if (!self->bin) {
703 s[0] = GET;
704 PyOS_snprintf(s + 1, sizeof(s) - 1, "%ld\n", c_value);
705 len = strlen(s);
706 }
707 else if (Pdata_Check(self->file)) {
708 if (write_other(self, NULL, 0) < 0) return -1;
709 PDATA_APPEND(self->file, mv, -1);
710 return 0;
711 }
712 else {
713 if (c_value < 256) {
714 s[0] = BINGET;
715 s[1] = (int)(c_value & 0xff);
716 len = 2;
717 }
718 else {
719 s[0] = LONG_BINGET;
720 s[1] = (int)(c_value & 0xff);
721 s[2] = (int)((c_value >> 8) & 0xff);
722 s[3] = (int)((c_value >> 16) & 0xff);
723 s[4] = (int)((c_value >> 24) & 0xff);
724 len = 5;
725 }
726 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000727
Tim Peters0bc93f52003-02-02 18:29:33 +0000728 if (self->write_func(self, s, len) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000729 return -1;
Tim Peterscba30e22003-02-01 06:24:36 +0000730
Jeremy Hyltona0fb1772001-10-12 04:11:06 +0000731 return 0;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000732}
Jeremy Hyltonce616e41998-08-13 23:13:52 +0000733
Guido van Rossum60456fd1997-04-09 17:36:32 +0000734
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000735static int
Tim Peterscba30e22003-02-01 06:24:36 +0000736put(Picklerobject *self, PyObject *ob)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000737{
Tim Peterscba30e22003-02-01 06:24:36 +0000738 if (ob->ob_refcnt < 2 || self->fast)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000739 return 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000740
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000741 return put2(self, ob);
742}
Guido van Rossum053b8df1998-11-25 16:18:00 +0000743
Guido van Rossum053b8df1998-11-25 16:18:00 +0000744
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000745static int
Tim Peterscba30e22003-02-01 06:24:36 +0000746put2(Picklerobject *self, PyObject *ob)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000747{
748 char c_str[30];
749 int p;
750 size_t len;
751 int res = -1;
752 PyObject *py_ob_id = 0, *memo_len = 0, *t = 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000753
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000754 if (self->fast)
755 return 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000756
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000757 if ((p = PyDict_Size(self->memo)) < 0)
758 goto finally;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000759
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000760 /* Make sure memo keys are positive! */
Tim Peters70b02d72003-02-02 17:26:40 +0000761 /* XXX Why?
762 * XXX And does "positive" really mean non-negative?
763 * XXX pickle.py starts with PUT index 0, not 1. This makes for
764 * XXX gratuitous differences between the pickling modules.
765 */
Tim Peterscba30e22003-02-01 06:24:36 +0000766 p++;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000767
Tim Peterscba30e22003-02-01 06:24:36 +0000768 if (!( py_ob_id = PyLong_FromVoidPtr(ob)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000769 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000770
Tim Peterscba30e22003-02-01 06:24:36 +0000771 if (!( memo_len = PyInt_FromLong(p)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000772 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000773
Tim Peterscba30e22003-02-01 06:24:36 +0000774 if (!( t = PyTuple_New(2)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000775 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000776
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000777 PyTuple_SET_ITEM(t, 0, memo_len);
778 Py_INCREF(memo_len);
779 PyTuple_SET_ITEM(t, 1, ob);
780 Py_INCREF(ob);
781
782 if (PyDict_SetItem(self->memo, py_ob_id, t) < 0)
783 goto finally;
784
785 if (!self->bin) {
786 c_str[0] = PUT;
787 PyOS_snprintf(c_str + 1, sizeof(c_str) - 1, "%d\n", p);
788 len = strlen(c_str);
789 }
790 else if (Pdata_Check(self->file)) {
791 if (write_other(self, NULL, 0) < 0) return -1;
792 PDATA_APPEND(self->file, memo_len, -1);
793 res=0; /* Job well done ;) */
794 goto finally;
795 }
796 else {
797 if (p >= 256) {
798 c_str[0] = LONG_BINPUT;
799 c_str[1] = (int)(p & 0xff);
800 c_str[2] = (int)((p >> 8) & 0xff);
801 c_str[3] = (int)((p >> 16) & 0xff);
802 c_str[4] = (int)((p >> 24) & 0xff);
803 len = 5;
804 }
805 else {
806 c_str[0] = BINPUT;
807 c_str[1] = p;
808 len = 2;
809 }
810 }
811
Tim Peters0bc93f52003-02-02 18:29:33 +0000812 if (self->write_func(self, c_str, len) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000813 goto finally;
814
815 res = 0;
816
817 finally:
818 Py_XDECREF(py_ob_id);
819 Py_XDECREF(memo_len);
820 Py_XDECREF(t);
821
822 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000823}
824
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000825#define PyImport_Import cPickle_Import
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000826
827static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +0000828PyImport_Import(PyObject *module_name)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000829{
830 static PyObject *silly_list=0, *__builtins___str=0, *__import___str;
831 static PyObject *standard_builtins=0;
832 PyObject *globals=0, *__import__=0, *__builtins__=0, *r=0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000833
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000834 if (!( silly_list )) {
Tim Peterscba30e22003-02-01 06:24:36 +0000835 if (!( __import___str=PyString_FromString("__import__")))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000836 return NULL;
Tim Peterscba30e22003-02-01 06:24:36 +0000837 if (!( __builtins___str=PyString_FromString("__builtins__")))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000838 return NULL;
Tim Peterscba30e22003-02-01 06:24:36 +0000839 if (!( silly_list=Py_BuildValue("[s]","__doc__")))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000840 return NULL;
841 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000842
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000843 if ((globals=PyEval_GetGlobals())) {
844 Py_INCREF(globals);
845 __builtins__=PyObject_GetItem(globals,__builtins___str);
Tim Peterscba30e22003-02-01 06:24:36 +0000846 if (!__builtins__)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000847 goto err;
848 }
849 else {
850 PyErr_Clear();
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000851
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000852 if (!(standard_builtins ||
Tim Peterscba30e22003-02-01 06:24:36 +0000853 (standard_builtins=PyImport_ImportModule("__builtin__"))))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000854 return NULL;
Tim Peters84e87f32001-03-17 04:50:51 +0000855
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000856 __builtins__=standard_builtins;
857 Py_INCREF(__builtins__);
858 globals = Py_BuildValue("{sO}", "__builtins__", __builtins__);
Tim Peterscba30e22003-02-01 06:24:36 +0000859 if (!globals)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000860 goto err;
861 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000862
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000863 if (PyDict_Check(__builtins__)) {
864 __import__=PyObject_GetItem(__builtins__,__import___str);
865 if (!__import__) goto err;
866 }
867 else {
868 __import__=PyObject_GetAttr(__builtins__,__import___str);
869 if (!__import__) goto err;
870 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000871
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000872 r=PyObject_CallFunction(__import__,"OOOO",
873 module_name, globals, globals, silly_list);
Tim Peterscba30e22003-02-01 06:24:36 +0000874 if (!r)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000875 goto err;
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000876
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000877 Py_DECREF(globals);
878 Py_DECREF(__builtins__);
879 Py_DECREF(__import__);
Tim Peters84e87f32001-03-17 04:50:51 +0000880
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000881 return r;
882 err:
883 Py_XDECREF(globals);
884 Py_XDECREF(__builtins__);
885 Py_XDECREF(__import__);
886 return NULL;
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000887}
888
889static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +0000890whichmodule(PyObject *global, PyObject *global_name)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000891{
892 int i, j;
893 PyObject *module = 0, *modules_dict = 0,
894 *global_name_attr = 0, *name = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000895
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000896 module = PyObject_GetAttrString(global, "__module__");
897 if (module) return module;
898 PyErr_Clear();
Guido van Rossum45188231997-09-28 05:38:51 +0000899
Tim Peterscba30e22003-02-01 06:24:36 +0000900 if (!( modules_dict = PySys_GetObject("modules")))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000901 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000902
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000903 i = 0;
904 while ((j = PyDict_Next(modules_dict, &i, &name, &module))) {
Guido van Rossum142eeb81997-08-13 03:14:41 +0000905
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000906 if (PyObject_Compare(name, __main___str)==0) continue;
Tim Peters84e87f32001-03-17 04:50:51 +0000907
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000908 global_name_attr = PyObject_GetAttr(module, global_name);
909 if (!global_name_attr) {
910 PyErr_Clear();
911 continue;
912 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000913
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000914 if (global_name_attr != global) {
915 Py_DECREF(global_name_attr);
916 continue;
917 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000918
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000919 Py_DECREF(global_name_attr);
Guido van Rossum60456fd1997-04-09 17:36:32 +0000920
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000921 break;
922 }
Guido van Rossum142eeb81997-08-13 03:14:41 +0000923
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000924 /* The following implements the rule in pickle.py added in 1.5
925 that used __main__ if no module is found. I don't actually
926 like this rule. jlf
927 */
928 if (!j) {
929 j=1;
930 name=__main___str;
931 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000932
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000933 Py_INCREF(name);
934 return name;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000935}
936
937
Guido van Rossum60456fd1997-04-09 17:36:32 +0000938static int
Jeremy Hylton499ab6a2001-10-15 21:37:58 +0000939fast_save_enter(Picklerobject *self, PyObject *obj)
940{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000941 /* if fast_container < 0, we're doing an error exit. */
942 if (++self->fast_container >= PY_CPICKLE_FAST_LIMIT) {
943 PyObject *key = NULL;
944 if (self->fast_memo == NULL) {
945 self->fast_memo = PyDict_New();
946 if (self->fast_memo == NULL) {
947 self->fast_container = -1;
948 return 0;
949 }
950 }
951 key = PyLong_FromVoidPtr(obj);
952 if (key == NULL)
953 return 0;
954 if (PyDict_GetItem(self->fast_memo, key)) {
Tim Peters4e52ca82002-12-07 02:43:28 +0000955 Py_DECREF(key);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000956 PyErr_Format(PyExc_ValueError,
957 "fast mode: can't pickle cyclic objects including object type %s at %p",
958 obj->ob_type->tp_name, obj);
959 self->fast_container = -1;
960 return 0;
961 }
962 if (PyDict_SetItem(self->fast_memo, key, Py_None) < 0) {
Tim Peters4e52ca82002-12-07 02:43:28 +0000963 Py_DECREF(key);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000964 self->fast_container = -1;
965 return 0;
966 }
Tim Peters4e52ca82002-12-07 02:43:28 +0000967 Py_DECREF(key);
Jeremy Hylton499ab6a2001-10-15 21:37:58 +0000968 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000969 return 1;
Jeremy Hylton499ab6a2001-10-15 21:37:58 +0000970}
971
Tim Peterscba30e22003-02-01 06:24:36 +0000972int
Jeremy Hylton499ab6a2001-10-15 21:37:58 +0000973fast_save_leave(Picklerobject *self, PyObject *obj)
974{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000975 if (self->fast_container-- >= PY_CPICKLE_FAST_LIMIT) {
976 PyObject *key = PyLong_FromVoidPtr(obj);
977 if (key == NULL)
978 return 0;
979 if (PyDict_DelItem(self->fast_memo, key) < 0) {
Tim Peters4e52ca82002-12-07 02:43:28 +0000980 Py_DECREF(key);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000981 return 0;
982 }
Tim Peters4e52ca82002-12-07 02:43:28 +0000983 Py_DECREF(key);
Jeremy Hylton499ab6a2001-10-15 21:37:58 +0000984 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000985 return 1;
Jeremy Hylton499ab6a2001-10-15 21:37:58 +0000986}
987
988static int
Tim Peterscba30e22003-02-01 06:24:36 +0000989save_none(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000990{
991 static char none = NONE;
Tim Peters0bc93f52003-02-02 18:29:33 +0000992 if (self->write_func(self, &none, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000993 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000994
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000995 return 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000996}
997
Guido van Rossum77f6a652002-04-03 22:41:51 +0000998static int
Tim Peterscba30e22003-02-01 06:24:36 +0000999save_bool(Picklerobject *self, PyObject *args)
Guido van Rossum77f6a652002-04-03 22:41:51 +00001000{
Guido van Rossume2763392002-04-05 19:30:08 +00001001 static char *buf[2] = {FALSE, TRUE};
1002 static char len[2] = {sizeof(FALSE)-1, sizeof(TRUE)-1};
Guido van Rossum77f6a652002-04-03 22:41:51 +00001003 long l = PyInt_AS_LONG((PyIntObject *)args);
1004
Tim Peters3c67d792003-02-02 17:59:11 +00001005 if (self->proto >= 2) {
1006 char opcode = l ? NEWTRUE : NEWFALSE;
1007 if (self->write_func(self, &opcode, 1) < 0)
1008 return -1;
1009 }
1010 else if (self->write_func(self, buf[l], len[l]) < 0)
Guido van Rossum77f6a652002-04-03 22:41:51 +00001011 return -1;
Guido van Rossum77f6a652002-04-03 22:41:51 +00001012 return 0;
1013}
Tim Peters84e87f32001-03-17 04:50:51 +00001014
Guido van Rossum60456fd1997-04-09 17:36:32 +00001015static int
Tim Peterscba30e22003-02-01 06:24:36 +00001016save_int(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001017{
1018 char c_str[32];
1019 long l = PyInt_AS_LONG((PyIntObject *)args);
1020 int len = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001021
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001022 if (!self->bin
Guido van Rossumde8d6d71997-05-13 18:00:44 +00001023#if SIZEOF_LONG > 4
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001024 || l > 0x7fffffffL
1025 || l < -0x80000000L
Guido van Rossumde8d6d71997-05-13 18:00:44 +00001026#endif
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001027 ) {
1028 /* Text-mode pickle, or long too big to fit in the 4-byte
1029 * signed BININT format: store as a string.
1030 */
1031 c_str[0] = INT;
1032 PyOS_snprintf(c_str + 1, sizeof(c_str) - 1, "%ld\n", l);
Tim Peters0bc93f52003-02-02 18:29:33 +00001033 if (self->write_func(self, c_str, strlen(c_str)) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001034 return -1;
1035 }
1036 else {
1037 /* Binary pickle and l fits in a signed 4-byte int. */
1038 c_str[1] = (int)( l & 0xff);
1039 c_str[2] = (int)((l >> 8) & 0xff);
1040 c_str[3] = (int)((l >> 16) & 0xff);
1041 c_str[4] = (int)((l >> 24) & 0xff);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001042
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001043 if ((c_str[4] == 0) && (c_str[3] == 0)) {
1044 if (c_str[2] == 0) {
1045 c_str[0] = BININT1;
1046 len = 2;
1047 }
1048 else {
1049 c_str[0] = BININT2;
1050 len = 3;
1051 }
1052 }
1053 else {
1054 c_str[0] = BININT;
1055 len = 5;
1056 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001057
Tim Peters0bc93f52003-02-02 18:29:33 +00001058 if (self->write_func(self, c_str, len) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001059 return -1;
1060 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001061
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001062 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001063}
1064
1065
1066static int
Tim Peterscba30e22003-02-01 06:24:36 +00001067save_long(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001068{
Tim Petersee1a53c2003-02-02 02:57:53 +00001069 int size;
1070 int res = -1;
1071 PyObject *repr = NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001072
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001073 static char l = LONG;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001074
Tim Petersee1a53c2003-02-02 02:57:53 +00001075 if (self->proto >= 2) {
1076 /* Linear-time pickling. */
1077 size_t nbits;
1078 size_t nbytes;
1079 unsigned char *pdata;
1080 char c_str[5];
1081 int i;
1082 int sign = _PyLong_Sign(args);
1083
1084 if (sign == 0) {
1085 /* It's 0 -- an empty bytestring. */
1086 c_str[0] = LONG1;
1087 c_str[1] = 0;
1088 i = self->write_func(self, c_str, 2);
1089 if (i < 0) goto finally;
1090 res = 0;
1091 goto finally;
1092 }
1093 nbits = _PyLong_NumBits(args);
1094 if (nbits == (size_t)-1 && PyErr_Occurred())
1095 goto finally;
1096 /* How many bytes do we need? There are nbits >> 3 full
1097 * bytes of data, and nbits & 7 leftover bits. If there
1098 * are any leftover bits, then we clearly need another
1099 * byte. Wnat's not so obvious is that we *probably*
1100 * need another byte even if there aren't any leftovers:
1101 * the most-significant bit of the most-significant byte
1102 * acts like a sign bit, and it's usually got a sense
1103 * opposite of the one we need. The exception is longs
1104 * of the form -(2**(8*j-1)) for j > 0. Such a long is
1105 * its own 256's-complement, so has the right sign bit
1106 * even without the extra byte. That's a pain to check
1107 * for in advance, though, so we always grab an extra
1108 * byte at the start, and cut it back later if possible.
1109 */
1110 nbytes = (nbits >> 3) + 1;
1111 if ((int)nbytes < 0 || (size_t)(int)nbytes != nbytes) {
1112 PyErr_SetString(PyExc_OverflowError, "long too large "
1113 "to pickle");
1114 goto finally;
1115 }
1116 repr = PyString_FromStringAndSize(NULL, (int)nbytes);
1117 if (repr == NULL) goto finally;
1118 pdata = (unsigned char *)PyString_AS_STRING(repr);
1119 i = _PyLong_AsByteArray((PyLongObject *)args,
1120 pdata, nbytes,
1121 1 /* little endian */, 1 /* signed */);
1122 if (i < 0) goto finally;
1123 /* If the long is negative, this may be a byte more than
1124 * needed. This is so iff the MSB is all redundant sign
1125 * bits.
1126 */
1127 if (sign < 0 && nbytes > 1 && pdata[nbytes - 1] == 0xff &&
1128 (pdata[nbytes - 2] & 0x80) != 0)
1129 --nbytes;
1130
1131 if (nbytes < 256) {
1132 c_str[0] = LONG1;
1133 c_str[1] = (char)nbytes;
1134 size = 2;
1135 }
1136 else {
1137 c_str[0] = LONG4;
1138 size = (int)nbytes;
1139 for (i = 1; i < 5; i++) {
1140 c_str[i] = (char)(size & 0xff);
1141 size >>= 8;
1142 }
1143 size = 5;
1144 }
1145 i = self->write_func(self, c_str, size);
1146 if (i < 0) goto finally;
1147 i = self->write_func(self, (char *)pdata, (int)nbytes);
1148 if (i < 0) goto finally;
1149 res = 0;
1150 goto finally;
1151 }
1152
1153 /* proto < 2: write the repr and newline. This is quadratic-time
1154 * (in the number of digits), in both directions.
1155 */
Tim Peterscba30e22003-02-01 06:24:36 +00001156 if (!( repr = PyObject_Repr(args)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001157 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001158
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001159 if ((size = PyString_Size(repr)) < 0)
1160 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001161
Tim Peters0bc93f52003-02-02 18:29:33 +00001162 if (self->write_func(self, &l, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001163 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001164
Tim Peters0bc93f52003-02-02 18:29:33 +00001165 if (self->write_func(self,
1166 PyString_AS_STRING((PyStringObject *)repr),
1167 size) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001168 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001169
Tim Peters0bc93f52003-02-02 18:29:33 +00001170 if (self->write_func(self, "\n", 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001171 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001172
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001173 res = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001174
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001175 finally:
1176 Py_XDECREF(repr);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001177 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001178}
1179
1180
1181static int
Tim Peterscba30e22003-02-01 06:24:36 +00001182save_float(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001183{
1184 double x = PyFloat_AS_DOUBLE((PyFloatObject *)args);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001185
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001186 if (self->bin) {
1187 int s, e;
1188 double f;
1189 long fhi, flo;
1190 char str[9];
1191 unsigned char *p = (unsigned char *)str;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001192
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001193 *p = BINFLOAT;
1194 p++;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001195
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001196 if (x < 0) {
1197 s = 1;
1198 x = -x;
1199 }
1200 else
1201 s = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001202
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001203 f = frexp(x, &e);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001204
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001205 /* Normalize f to be in the range [1.0, 2.0) */
1206 if (0.5 <= f && f < 1.0) {
1207 f *= 2.0;
1208 e--;
1209 }
1210 else if (f == 0.0) {
1211 e = 0;
1212 }
1213 else {
1214 PyErr_SetString(PyExc_SystemError,
1215 "frexp() result out of range");
1216 return -1;
1217 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001218
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001219 if (e >= 1024) {
1220 /* XXX 1024 itself is reserved for Inf/NaN */
1221 PyErr_SetString(PyExc_OverflowError,
1222 "float too large to pack with d format");
1223 return -1;
1224 }
1225 else if (e < -1022) {
1226 /* Gradual underflow */
1227 f = ldexp(f, 1022 + e);
1228 e = 0;
1229 }
1230 else if (!(e == 0 && f == 0.0)) {
1231 e += 1023;
1232 f -= 1.0; /* Get rid of leading 1 */
1233 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001234
Tim Peterscba30e22003-02-01 06:24:36 +00001235 /* fhi receives the high 28 bits;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001236 flo the low 24 bits (== 52 bits) */
1237 f *= 268435456.0; /* 2**28 */
1238 fhi = (long) floor(f); /* Truncate */
1239 f -= (double)fhi;
1240 f *= 16777216.0; /* 2**24 */
1241 flo = (long) floor(f + 0.5); /* Round */
Guido van Rossum60456fd1997-04-09 17:36:32 +00001242
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001243 /* First byte */
1244 *p = (s<<7) | (e>>4);
1245 p++;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001246
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001247 /* Second byte */
1248 *p = (unsigned char) (((e&0xF)<<4) | (fhi>>24));
1249 p++;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001250
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001251 /* Third byte */
1252 *p = (unsigned char) ((fhi>>16) & 0xFF);
1253 p++;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001254
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001255 /* Fourth byte */
1256 *p = (unsigned char) ((fhi>>8) & 0xFF);
1257 p++;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001258
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001259 /* Fifth byte */
1260 *p = (unsigned char) (fhi & 0xFF);
1261 p++;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001262
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001263 /* Sixth byte */
1264 *p = (unsigned char) ((flo>>16) & 0xFF);
1265 p++;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001266
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001267 /* Seventh byte */
1268 *p = (unsigned char) ((flo>>8) & 0xFF);
1269 p++;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001270
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001271 /* Eighth byte */
1272 *p = (unsigned char) (flo & 0xFF);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001273
Tim Peters0bc93f52003-02-02 18:29:33 +00001274 if (self->write_func(self, str, 9) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001275 return -1;
1276 }
1277 else {
1278 char c_str[250];
1279 c_str[0] = FLOAT;
1280 PyOS_snprintf(c_str + 1, sizeof(c_str) - 1, "%.17g\n", x);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001281
Tim Peters0bc93f52003-02-02 18:29:33 +00001282 if (self->write_func(self, c_str, strlen(c_str)) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001283 return -1;
1284 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001285
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001286 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001287}
1288
1289
1290static int
Tim Peterscba30e22003-02-01 06:24:36 +00001291save_string(Picklerobject *self, PyObject *args, int doput)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001292{
1293 int size, len;
1294 PyObject *repr=0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001295
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001296 if ((size = PyString_Size(args)) < 0)
1297 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001298
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001299 if (!self->bin) {
1300 char *repr_str;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001301
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001302 static char string = STRING;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001303
Tim Peterscba30e22003-02-01 06:24:36 +00001304 if (!( repr = PyObject_Repr(args)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001305 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001306
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001307 if ((len = PyString_Size(repr)) < 0)
1308 goto err;
1309 repr_str = PyString_AS_STRING((PyStringObject *)repr);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001310
Tim Peters0bc93f52003-02-02 18:29:33 +00001311 if (self->write_func(self, &string, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001312 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001313
Tim Peters0bc93f52003-02-02 18:29:33 +00001314 if (self->write_func(self, repr_str, len) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001315 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001316
Tim Peters0bc93f52003-02-02 18:29:33 +00001317 if (self->write_func(self, "\n", 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001318 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001319
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001320 Py_XDECREF(repr);
1321 }
1322 else {
1323 int i;
1324 char c_str[5];
Guido van Rossum60456fd1997-04-09 17:36:32 +00001325
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001326 if ((size = PyString_Size(args)) < 0)
1327 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001328
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001329 if (size < 256) {
1330 c_str[0] = SHORT_BINSTRING;
1331 c_str[1] = size;
1332 len = 2;
1333 }
1334 else {
1335 c_str[0] = BINSTRING;
1336 for (i = 1; i < 5; i++)
1337 c_str[i] = (int)(size >> ((i - 1) * 8));
1338 len = 5;
1339 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001340
Tim Peters0bc93f52003-02-02 18:29:33 +00001341 if (self->write_func(self, c_str, len) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001342 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001343
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001344 if (size > 128 && Pdata_Check(self->file)) {
1345 if (write_other(self, NULL, 0) < 0) return -1;
1346 PDATA_APPEND(self->file, args, -1);
1347 }
1348 else {
Tim Peters0bc93f52003-02-02 18:29:33 +00001349 if (self->write_func(self,
1350 PyString_AS_STRING(
1351 (PyStringObject *)args),
1352 size) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001353 return -1;
1354 }
1355 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001356
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001357 if (doput)
1358 if (put(self, args) < 0)
1359 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001360
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001361 return 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +00001362
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001363 err:
1364 Py_XDECREF(repr);
1365 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001366}
1367
1368
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001369#ifdef Py_USING_UNICODE
Guido van Rossumfb10c3f2000-12-19 02:08:38 +00001370/* A copy of PyUnicode_EncodeRawUnicodeEscape() that also translates
1371 backslash and newline characters to \uXXXX escapes. */
1372static PyObject *
1373modified_EncodeRawUnicodeEscape(const Py_UNICODE *s, int size)
1374{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001375 PyObject *repr;
1376 char *p;
1377 char *q;
Guido van Rossumfb10c3f2000-12-19 02:08:38 +00001378
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001379 static const char *hexdigit = "0123456789ABCDEF";
Guido van Rossumfb10c3f2000-12-19 02:08:38 +00001380
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001381 repr = PyString_FromStringAndSize(NULL, 6 * size);
1382 if (repr == NULL)
1383 return NULL;
1384 if (size == 0)
1385 return repr;
1386
1387 p = q = PyString_AS_STRING(repr);
1388 while (size-- > 0) {
1389 Py_UNICODE ch = *s++;
1390 /* Map 16-bit characters to '\uxxxx' */
1391 if (ch >= 256 || ch == '\\' || ch == '\n') {
1392 *p++ = '\\';
1393 *p++ = 'u';
1394 *p++ = hexdigit[(ch >> 12) & 0xf];
1395 *p++ = hexdigit[(ch >> 8) & 0xf];
1396 *p++ = hexdigit[(ch >> 4) & 0xf];
1397 *p++ = hexdigit[ch & 15];
1398 }
1399 /* Copy everything else as-is */
1400 else
1401 *p++ = (char) ch;
1402 }
1403 *p = '\0';
Tim Peters5de98422002-04-27 18:44:32 +00001404 _PyString_Resize(&repr, p - q);
Guido van Rossumfb10c3f2000-12-19 02:08:38 +00001405 return repr;
Guido van Rossumfb10c3f2000-12-19 02:08:38 +00001406}
1407
1408
Guido van Rossum60456fd1997-04-09 17:36:32 +00001409static int
Tim Peterscba30e22003-02-01 06:24:36 +00001410save_unicode(Picklerobject *self, PyObject *args, int doput)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001411{
1412 int size, len;
1413 PyObject *repr=0;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001414
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001415 if (!PyUnicode_Check(args))
1416 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001417
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001418 if (!self->bin) {
1419 char *repr_str;
1420 static char string = UNICODE;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001421
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001422 repr = modified_EncodeRawUnicodeEscape(
1423 PyUnicode_AS_UNICODE(args), PyUnicode_GET_SIZE(args));
Tim Peterscba30e22003-02-01 06:24:36 +00001424 if (!repr)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001425 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001426
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001427 if ((len = PyString_Size(repr)) < 0)
1428 goto err;
1429 repr_str = PyString_AS_STRING((PyStringObject *)repr);
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001430
Tim Peters0bc93f52003-02-02 18:29:33 +00001431 if (self->write_func(self, &string, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001432 goto err;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001433
Tim Peters0bc93f52003-02-02 18:29:33 +00001434 if (self->write_func(self, repr_str, len) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001435 goto err;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001436
Tim Peters0bc93f52003-02-02 18:29:33 +00001437 if (self->write_func(self, "\n", 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001438 goto err;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001439
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001440 Py_XDECREF(repr);
1441 }
1442 else {
1443 int i;
1444 char c_str[5];
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001445
Tim Peterscba30e22003-02-01 06:24:36 +00001446 if (!( repr = PyUnicode_AsUTF8String(args)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001447 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001448
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001449 if ((size = PyString_Size(repr)) < 0)
1450 goto err;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001451
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001452 c_str[0] = BINUNICODE;
1453 for (i = 1; i < 5; i++)
1454 c_str[i] = (int)(size >> ((i - 1) * 8));
1455 len = 5;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001456
Tim Peters0bc93f52003-02-02 18:29:33 +00001457 if (self->write_func(self, c_str, len) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001458 goto err;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001459
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001460 if (size > 128 && Pdata_Check(self->file)) {
1461 if (write_other(self, NULL, 0) < 0)
1462 goto err;
1463 PDATA_APPEND(self->file, repr, -1);
1464 }
1465 else {
Tim Peters0bc93f52003-02-02 18:29:33 +00001466 if (self->write_func(self, PyString_AS_STRING(repr),
1467 size) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001468 goto err;
1469 }
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001470
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001471 Py_DECREF(repr);
1472 }
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001473
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001474 if (doput)
1475 if (put(self, args) < 0)
1476 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001477
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001478 return 0;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001479
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001480 err:
1481 Py_XDECREF(repr);
1482 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001483}
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001484#endif
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001485
Tim Peters1d63c9f2003-02-02 20:29:39 +00001486/* A helper for save_tuple. Push the len elements in tuple t on the stack. */
1487static int
Tim Peters67920142003-02-05 03:46:17 +00001488store_tuple_elements(Picklerobject *self, PyObject *t, int len)
Tim Peters1d63c9f2003-02-02 20:29:39 +00001489{
1490 int i;
1491 int res = -1; /* guilty until proved innocent */
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001492
Tim Peters1d63c9f2003-02-02 20:29:39 +00001493 assert(PyTuple_Size(t) == len);
1494
1495 for (i = 0; i < len; i++) {
1496 PyObject *element = PyTuple_GET_ITEM(t, i);
1497
1498 if (element == NULL)
1499 goto finally;
1500 if (save(self, element, 0) < 0)
1501 goto finally;
1502 }
1503 res = 0;
1504
1505 finally:
1506 return res;
1507}
1508
1509/* Tuples are ubiquitous in the pickle protocols, so many techniques are
1510 * used across protocols to minimize the space needed to pickle them.
Tim Peters67920142003-02-05 03:46:17 +00001511 * Tuples are also the only builtin immutable type that can be recursive
Tim Peters1d63c9f2003-02-02 20:29:39 +00001512 * (a tuple can be reached from itself), and that requires some subtle
1513 * magic so that it works in all cases. IOW, this is a long routine.
1514 */
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001515static int
Tim Peterscba30e22003-02-01 06:24:36 +00001516save_tuple(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001517{
Tim Peters1d63c9f2003-02-02 20:29:39 +00001518 PyObject *py_tuple_id = NULL;
1519 int len, i;
1520 int res = -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001521
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001522 static char tuple = TUPLE;
Tim Peters1d63c9f2003-02-02 20:29:39 +00001523 static char pop = POP;
1524 static char pop_mark = POP_MARK;
1525 static char len2opcode[] = {EMPTY_TUPLE, TUPLE1, TUPLE2, TUPLE3};
Guido van Rossum60456fd1997-04-09 17:36:32 +00001526
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001527 if ((len = PyTuple_Size(args)) < 0)
1528 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001529
Tim Peters1d63c9f2003-02-02 20:29:39 +00001530 if (len == 0) {
1531 char c_str[2];
Tim Peters84e87f32001-03-17 04:50:51 +00001532
Tim Peters1d63c9f2003-02-02 20:29:39 +00001533 if (self->proto) {
1534 c_str[0] = EMPTY_TUPLE;
1535 len = 1;
1536 }
1537 else {
1538 c_str[0] = MARK;
1539 c_str[1] = TUPLE;
1540 len = 2;
1541 }
1542 if (self->write_func(self, c_str, len) >= 0)
1543 res = 0;
1544 /* Don't memoize an empty tuple. */
1545 goto finally;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001546 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001547
Tim Peters1d63c9f2003-02-02 20:29:39 +00001548 /* A non-empty tuple. */
1549
1550 /* id(tuple) isn't in the memo now. If it shows up there after
1551 * saving the tuple elements, the tuple must be recursive, in
1552 * which case we'll pop everything we put on the stack, and fetch
1553 * its value from the memo.
1554 */
1555 py_tuple_id = PyLong_FromVoidPtr(args);
1556 if (py_tuple_id == NULL)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001557 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001558
Tim Peters1d63c9f2003-02-02 20:29:39 +00001559 if (len <= 3 && self->proto >= 2) {
1560 /* Use TUPLE{1,2,3} opcodes. */
Tim Peters67920142003-02-05 03:46:17 +00001561 if (store_tuple_elements(self, args, len) < 0)
Tim Peters1d63c9f2003-02-02 20:29:39 +00001562 goto finally;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001563 if (PyDict_GetItem(self->memo, py_tuple_id)) {
Tim Peters1d63c9f2003-02-02 20:29:39 +00001564 /* pop the len elements */
1565 for (i = 0; i < len; ++i)
1566 if (self->write_func(self, &pop, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001567 goto finally;
Tim Peters1d63c9f2003-02-02 20:29:39 +00001568 /* fetch from memo */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001569 if (get(self, py_tuple_id) < 0)
1570 goto finally;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001571 res = 0;
1572 goto finally;
1573 }
Tim Peters1d63c9f2003-02-02 20:29:39 +00001574 /* Not recursive. */
1575 if (self->write_func(self, len2opcode + len, 1) < 0)
1576 goto finally;
1577 goto memoize;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001578 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001579
Tim Peters1d63c9f2003-02-02 20:29:39 +00001580 /* proto < 2 and len > 0, or proto >= 2 and len > 3.
1581 * Generate MARK elt1 elt2 ... TUPLE
1582 */
1583 if (self->write_func(self, &MARKv, 1) < 0)
1584 goto finally;
1585
Tim Peters67920142003-02-05 03:46:17 +00001586 if (store_tuple_elements(self, args, len) < 0)
Tim Peters1d63c9f2003-02-02 20:29:39 +00001587 goto finally;
1588
1589 if (PyDict_GetItem(self->memo, py_tuple_id)) {
1590 /* pop the stack stuff we pushed */
1591 if (self->bin) {
1592 if (self->write_func(self, &pop_mark, 1) < 0)
1593 goto finally;
1594 }
1595 else {
1596 /* Note that we pop one more than len, to remove
1597 * the MARK too.
1598 */
1599 for (i = 0; i <= len; i++)
1600 if (self->write_func(self, &pop, 1) < 0)
1601 goto finally;
1602 }
1603 /* fetch from memo */
1604 if (get(self, py_tuple_id) >= 0)
1605 res = 0;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001606 goto finally;
1607 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001608
Tim Peters1d63c9f2003-02-02 20:29:39 +00001609 /* Not recursive. */
1610 if (self->write_func(self, &tuple, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001611 goto finally;
Tim Peters84e87f32001-03-17 04:50:51 +00001612
Tim Peters1d63c9f2003-02-02 20:29:39 +00001613 memoize:
1614 if (put(self, args) >= 0)
1615 res = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001616
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001617 finally:
1618 Py_XDECREF(py_tuple_id);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001619 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001620}
1621
Tim Peters1092d642003-02-11 21:06:20 +00001622/* iter is an iterator giving items, and we batch up chunks of
1623 * MARK item item ... item APPENDS
1624 * opcode sequences. Calling code should have arranged to first create an
1625 * empty list, or list-like object, for the APPENDS to operate on.
1626 * Returns 0 on success, <0 on error.
1627 */
1628static int
1629batch_list(Picklerobject *self, PyObject *iter)
1630{
1631 PyObject *obj;
1632 PyObject *slice[BATCHSIZE];
1633 int i, n;
1634
1635 static char append = APPEND;
1636 static char appends = APPENDS;
1637
1638 assert(iter != NULL);
1639
1640 if (self->proto == 0) {
1641 /* APPENDS isn't available; do one at a time. */
1642 for (;;) {
1643 obj = PyIter_Next(iter);
1644 if (obj == NULL) {
1645 if (PyErr_Occurred())
1646 return -1;
1647 break;
1648 }
1649 i = save(self, obj, 0);
1650 Py_DECREF(obj);
1651 if (i < 0)
1652 return -1;
1653 if (self->write_func(self, &append, 1) < 0)
1654 return -1;
1655
1656 }
1657 return 0;
1658 }
1659
1660 /* proto > 0: write in batches of BATCHSIZE. */
1661 do {
1662 /* Get next group of (no more than) BATCHSIZE elements. */
1663 for (n = 0; n < BATCHSIZE; ++n) {
1664 obj = PyIter_Next(iter);
1665 if (obj == NULL) {
1666 if (PyErr_Occurred())
1667 goto BatchFailed;
1668 break;
1669 }
1670 slice[n] = obj;
1671 }
1672
1673 if (n > 1) {
1674 /* Pump out MARK, slice[0:n], APPENDS. */
1675 if (self->write_func(self, &MARKv, 1) < 0)
1676 goto BatchFailed;
1677 for (i = 0; i < n; ++i) {
1678 if (save(self, slice[i], 0) < 0)
1679 goto BatchFailed;
1680 }
1681 if (self->write_func(self, &appends, 1) < 0)
1682 goto BatchFailed;
1683 }
1684 else if (n == 1) {
1685 if (save(self, slice[0], 0) < 0)
1686 goto BatchFailed;
1687 if (self->write_func(self, &append, 1) < 0)
1688 goto BatchFailed;
1689 }
1690
1691 for (i = 0; i < n; ++i) {
1692 Py_DECREF(slice[i]);
1693 }
1694 }while (n == BATCHSIZE);
1695 return 0;
1696
1697BatchFailed:
1698 while (--n >= 0) {
1699 Py_DECREF(slice[n]);
1700 }
1701 return -1;
1702}
1703
Guido van Rossum60456fd1997-04-09 17:36:32 +00001704static int
Tim Peterscba30e22003-02-01 06:24:36 +00001705save_list(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001706{
Tim Peters1092d642003-02-11 21:06:20 +00001707 int res = -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001708 char s[3];
Tim Peters1092d642003-02-11 21:06:20 +00001709 int len;
1710 PyObject *iter;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001711
Guido van Rossum60456fd1997-04-09 17:36:32 +00001712
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001713 if (self->fast && !fast_save_enter(self, args))
1714 goto finally;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00001715
Tim Peters1092d642003-02-11 21:06:20 +00001716 /* Create an empty list. */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001717 if (self->bin) {
1718 s[0] = EMPTY_LIST;
Tim Peters1092d642003-02-11 21:06:20 +00001719 len = 1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001720 }
1721 else {
1722 s[0] = MARK;
1723 s[1] = LIST;
Tim Peters1092d642003-02-11 21:06:20 +00001724 len = 2;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001725 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001726
Tim Peters1092d642003-02-11 21:06:20 +00001727 if (self->write_func(self, s, len) < 0)
1728 goto finally;
1729
1730 /* Get list length, and bow out early if empty. */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001731 if ((len = PyList_Size(args)) < 0)
1732 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001733
Tim Peters1092d642003-02-11 21:06:20 +00001734 /* Memoize. */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001735 if (len == 0) {
1736 if (put(self, args) < 0)
1737 goto finally;
1738 }
1739 else {
1740 if (put2(self, args) < 0)
1741 goto finally;
1742 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001743
Tim Peters1092d642003-02-11 21:06:20 +00001744 /* Materialize the list elements. */
1745 iter = PyObject_GetIter(args);
1746 if (iter == NULL)
1747 goto finally;
1748 res = batch_list(self, iter);
1749 Py_DECREF(iter);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001750
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001751 finally:
1752 if (self->fast && !fast_save_leave(self, args))
1753 res = -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001754
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001755 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001756}
1757
1758
Guido van Rossum60456fd1997-04-09 17:36:32 +00001759static int
Tim Peterscba30e22003-02-01 06:24:36 +00001760save_dict(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001761{
1762 PyObject *key = 0, *value = 0;
1763 int i, len, res = -1, using_setitems;
1764 char s[3];
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001765
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001766 static char setitem = SETITEM, setitems = SETITEMS;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001767
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001768 if (self->fast && !fast_save_enter(self, args))
1769 goto finally;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00001770
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001771 if (self->bin) {
1772 s[0] = EMPTY_DICT;
1773 len = 1;
1774 }
1775 else {
1776 s[0] = MARK;
1777 s[1] = DICT;
1778 len = 2;
1779 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001780
Tim Peters0bc93f52003-02-02 18:29:33 +00001781 if (self->write_func(self, s, len) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001782 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001783
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001784 if ((len = PyDict_Size(args)) < 0)
1785 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001786
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001787 if (len == 0) {
1788 if (put(self, args) < 0)
1789 goto finally;
1790 }
1791 else {
1792 if (put2(self, args) < 0)
1793 goto finally;
1794 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001795
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001796 if ((using_setitems = (self->bin && (PyDict_Size(args) > 1))))
Tim Peters0bc93f52003-02-02 18:29:33 +00001797 if (self->write_func(self, &MARKv, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001798 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001799
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001800 i = 0;
1801 while (PyDict_Next(args, &i, &key, &value)) {
1802 if (save(self, key, 0) < 0)
1803 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001804
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001805 if (save(self, value, 0) < 0)
1806 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001807
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001808 if (!using_setitems) {
Tim Peters0bc93f52003-02-02 18:29:33 +00001809 if (self->write_func(self, &setitem, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001810 goto finally;
1811 }
1812 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001813
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001814 if (using_setitems) {
Tim Peters0bc93f52003-02-02 18:29:33 +00001815 if (self->write_func(self, &setitems, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001816 goto finally;
1817 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001818
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001819 res = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001820
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001821 finally:
1822 if (self->fast && !fast_save_leave(self, args))
1823 res = -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001824
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001825 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001826}
1827
1828
Tim Peters84e87f32001-03-17 04:50:51 +00001829static int
Tim Peterscba30e22003-02-01 06:24:36 +00001830save_inst(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001831{
1832 PyObject *class = 0, *module = 0, *name = 0, *state = 0,
1833 *getinitargs_func = 0, *getstate_func = 0, *class_args = 0;
1834 char *module_str, *name_str;
1835 int module_size, name_size, res = -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001836
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001837 static char inst = INST, obj = OBJ, build = BUILD;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001838
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001839 if (self->fast && !fast_save_enter(self, args))
1840 goto finally;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00001841
Tim Peters0bc93f52003-02-02 18:29:33 +00001842 if (self->write_func(self, &MARKv, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001843 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001844
Tim Peterscba30e22003-02-01 06:24:36 +00001845 if (!( class = PyObject_GetAttr(args, __class___str)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001846 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001847
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001848 if (self->bin) {
1849 if (save(self, class, 0) < 0)
1850 goto finally;
1851 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001852
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001853 if ((getinitargs_func = PyObject_GetAttr(args, __getinitargs___str))) {
1854 PyObject *element = 0;
1855 int i, len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001856
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001857 if (!( class_args =
Tim Peterscba30e22003-02-01 06:24:36 +00001858 PyObject_Call(getinitargs_func, empty_tuple, NULL)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001859 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001860
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001861 if ((len = PyObject_Size(class_args)) < 0)
1862 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001863
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001864 for (i = 0; i < len; i++) {
Tim Peterscba30e22003-02-01 06:24:36 +00001865 if (!( element = PySequence_GetItem(class_args, i)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001866 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001867
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001868 if (save(self, element, 0) < 0) {
1869 Py_DECREF(element);
1870 goto finally;
1871 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001872
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001873 Py_DECREF(element);
1874 }
1875 }
1876 else {
1877 PyErr_Clear();
1878 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001879
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001880 if (!self->bin) {
1881 if (!( name = ((PyClassObject *)class)->cl_name )) {
1882 PyErr_SetString(PicklingError, "class has no name");
1883 goto finally;
1884 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001885
Tim Peterscba30e22003-02-01 06:24:36 +00001886 if (!( module = whichmodule(class, name)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001887 goto finally;
Guido van Rossum053b8df1998-11-25 16:18:00 +00001888
Tim Peters84e87f32001-03-17 04:50:51 +00001889
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001890 if ((module_size = PyString_Size(module)) < 0 ||
1891 (name_size = PyString_Size(name)) < 0)
1892 goto finally;
Guido van Rossum053b8df1998-11-25 16:18:00 +00001893
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001894 module_str = PyString_AS_STRING((PyStringObject *)module);
1895 name_str = PyString_AS_STRING((PyStringObject *)name);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001896
Tim Peters0bc93f52003-02-02 18:29:33 +00001897 if (self->write_func(self, &inst, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001898 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001899
Tim Peters0bc93f52003-02-02 18:29:33 +00001900 if (self->write_func(self, module_str, module_size) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001901 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001902
Tim Peters0bc93f52003-02-02 18:29:33 +00001903 if (self->write_func(self, "\n", 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001904 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001905
Tim Peters0bc93f52003-02-02 18:29:33 +00001906 if (self->write_func(self, name_str, name_size) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001907 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001908
Tim Peters0bc93f52003-02-02 18:29:33 +00001909 if (self->write_func(self, "\n", 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001910 goto finally;
1911 }
Tim Peters0bc93f52003-02-02 18:29:33 +00001912 else if (self->write_func(self, &obj, 1) < 0) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001913 goto finally;
1914 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001915
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001916 if ((getstate_func = PyObject_GetAttr(args, __getstate___str))) {
1917 state = PyObject_Call(getstate_func, empty_tuple, NULL);
Tim Peterscba30e22003-02-01 06:24:36 +00001918 if (!state)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001919 goto finally;
1920 }
1921 else {
1922 PyErr_Clear();
Guido van Rossum60456fd1997-04-09 17:36:32 +00001923
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001924 if (!( state = PyObject_GetAttr(args, __dict___str))) {
1925 PyErr_Clear();
1926 res = 0;
1927 goto finally;
1928 }
1929 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001930
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001931 if (!PyDict_Check(state)) {
1932 if (put2(self, args) < 0)
1933 goto finally;
1934 }
1935 else {
1936 if (put(self, args) < 0)
1937 goto finally;
1938 }
Tim Peters84e87f32001-03-17 04:50:51 +00001939
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001940 if (save(self, state, 0) < 0)
1941 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001942
Tim Peters0bc93f52003-02-02 18:29:33 +00001943 if (self->write_func(self, &build, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001944 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001945
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001946 res = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001947
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001948 finally:
1949 if (self->fast && !fast_save_leave(self, args))
1950 res = -1;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00001951
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001952 Py_XDECREF(module);
1953 Py_XDECREF(class);
1954 Py_XDECREF(state);
1955 Py_XDECREF(getinitargs_func);
1956 Py_XDECREF(getstate_func);
1957 Py_XDECREF(class_args);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001958
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001959 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001960}
1961
1962
Guido van Rossum60456fd1997-04-09 17:36:32 +00001963static int
Tim Peterscba30e22003-02-01 06:24:36 +00001964save_global(Picklerobject *self, PyObject *args, PyObject *name)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001965{
Jeremy Hylton9ee91f12002-07-11 22:02:33 +00001966 PyObject *global_name = 0, *module = 0, *mod = 0, *klass = 0;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001967 char *name_str, *module_str;
1968 int module_size, name_size, res = -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001969
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001970 static char global = GLOBAL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001971
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001972 if (name) {
1973 global_name = name;
1974 Py_INCREF(global_name);
1975 }
1976 else {
Tim Peterscba30e22003-02-01 06:24:36 +00001977 if (!( global_name = PyObject_GetAttr(args, __name___str)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001978 goto finally;
1979 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001980
Tim Peterscba30e22003-02-01 06:24:36 +00001981 if (!( module = whichmodule(args, global_name)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001982 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001983
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001984 if ((module_size = PyString_Size(module)) < 0 ||
1985 (name_size = PyString_Size(global_name)) < 0)
1986 goto finally;
Guido van Rossum053b8df1998-11-25 16:18:00 +00001987
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001988 module_str = PyString_AS_STRING((PyStringObject *)module);
1989 name_str = PyString_AS_STRING((PyStringObject *)global_name);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001990
Guido van Rossum75bfd052002-12-24 18:10:07 +00001991 /* XXX This can be doing a relative import. Clearly it shouldn't,
1992 but I don't know how to stop it. :-( */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001993 mod = PyImport_ImportModule(module_str);
1994 if (mod == NULL) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001995 cPickle_ErrFormat(PicklingError,
1996 "Can't pickle %s: it's not found as %s.%s",
1997 "OSS", args, module, global_name);
1998 goto finally;
1999 }
Jeremy Hylton0e1f7a82002-07-11 22:01:40 +00002000 klass = PyObject_GetAttrString(mod, name_str);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002001 if (klass == NULL) {
2002 cPickle_ErrFormat(PicklingError,
2003 "Can't pickle %s: it's not found as %s.%s",
2004 "OSS", args, module, global_name);
2005 goto finally;
2006 }
2007 if (klass != args) {
Jeremy Hylton0e1f7a82002-07-11 22:01:40 +00002008 Py_DECREF(klass);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002009 cPickle_ErrFormat(PicklingError,
Tim Peters731098b2003-02-04 20:56:09 +00002010 "Can't pickle %s: it's not the same object "
2011 "as %s.%s",
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002012 "OSS", args, module, global_name);
2013 goto finally;
2014 }
Jeremy Hylton0e1f7a82002-07-11 22:01:40 +00002015 Py_DECREF(klass);
Guido van Rossuma92d16a2001-08-18 21:22:07 +00002016
Tim Peters731098b2003-02-04 20:56:09 +00002017 if (self->proto >= 2) {
2018 /* See whether this is in the extension registry, and if
2019 * so generate an EXT opcode.
2020 */
2021 PyObject *py_code; /* extension code as Python object */
Tim Peters3e667d52003-02-04 21:47:44 +00002022 long code; /* extension code as C value */
Tim Peters731098b2003-02-04 20:56:09 +00002023 char c_str[5];
2024 int n;
2025
2026 PyTuple_SET_ITEM(two_tuple, 0, module);
2027 PyTuple_SET_ITEM(two_tuple, 1, global_name);
2028 py_code = PyDict_GetItem(extension_registry, two_tuple);
2029 if (py_code == NULL)
2030 goto gen_global; /* not registered */
2031
2032 /* Verify py_code has the right type and value. */
2033 if (!PyInt_Check(py_code)) {
2034 cPickle_ErrFormat(PicklingError, "Can't pickle %s: "
Tim Peters6288e232003-02-05 03:53:10 +00002035 "extension code %s isn't an integer",
Tim Peters731098b2003-02-04 20:56:09 +00002036 "OO", args, py_code);
2037 goto finally;
2038 }
2039 code = PyInt_AS_LONG(py_code);
2040 if (code <= 0 || code > 0x7fffffffL) {
2041 cPickle_ErrFormat(PicklingError, "Can't pickle %s: "
2042 "extension code %ld is out of range",
2043 "Ol", args, code);
2044 goto finally;
2045 }
2046
2047 /* Generate an EXT opcode. */
2048 if (code <= 0xff) {
2049 c_str[0] = EXT1;
2050 c_str[1] = (char)code;
2051 n = 2;
2052 }
2053 else if (code <= 0xffff) {
2054 c_str[0] = EXT2;
2055 c_str[1] = (char)(code & 0xff);
2056 c_str[2] = (char)((code >> 8) & 0xff);
2057 n = 3;
2058 }
2059 else {
2060 c_str[0] = EXT4;
2061 c_str[1] = (char)(code & 0xff);
2062 c_str[2] = (char)((code >> 8) & 0xff);
2063 c_str[3] = (char)((code >> 16) & 0xff);
2064 c_str[4] = (char)((code >> 24) & 0xff);
2065 n = 5;
2066 }
2067
2068 if (self->write_func(self, c_str, n) >= 0)
2069 res = 0;
2070 goto finally; /* and don't memoize */
2071 }
2072
2073 gen_global:
Tim Peters0bc93f52003-02-02 18:29:33 +00002074 if (self->write_func(self, &global, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002075 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002076
Tim Peters0bc93f52003-02-02 18:29:33 +00002077 if (self->write_func(self, module_str, module_size) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002078 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002079
Tim Peters0bc93f52003-02-02 18:29:33 +00002080 if (self->write_func(self, "\n", 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002081 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002082
Tim Peters0bc93f52003-02-02 18:29:33 +00002083 if (self->write_func(self, name_str, name_size) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002084 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002085
Tim Peters0bc93f52003-02-02 18:29:33 +00002086 if (self->write_func(self, "\n", 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002087 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002088
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002089 if (put(self, args) < 0)
2090 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002091
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002092 res = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002093
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002094 finally:
2095 Py_XDECREF(module);
2096 Py_XDECREF(global_name);
2097 Py_XDECREF(mod);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002098
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002099 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002100}
2101
Guido van Rossum60456fd1997-04-09 17:36:32 +00002102static int
Tim Peterscba30e22003-02-01 06:24:36 +00002103save_pers(Picklerobject *self, PyObject *args, PyObject *f)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002104{
2105 PyObject *pid = 0;
2106 int size, res = -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002107
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002108 static char persid = PERSID, binpersid = BINPERSID;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002109
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002110 Py_INCREF(args);
2111 ARG_TUP(self, args);
2112 if (self->arg) {
2113 pid = PyObject_Call(f, self->arg, NULL);
2114 FREE_ARG_TUP(self);
2115 }
2116 if (! pid) return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002117
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002118 if (pid != Py_None) {
2119 if (!self->bin) {
2120 if (!PyString_Check(pid)) {
2121 PyErr_SetString(PicklingError,
2122 "persistent id must be string");
2123 goto finally;
2124 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002125
Tim Peters0bc93f52003-02-02 18:29:33 +00002126 if (self->write_func(self, &persid, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002127 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002128
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002129 if ((size = PyString_Size(pid)) < 0)
2130 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002131
Tim Peters0bc93f52003-02-02 18:29:33 +00002132 if (self->write_func(self,
2133 PyString_AS_STRING(
2134 (PyStringObject *)pid),
2135 size) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002136 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002137
Tim Peters0bc93f52003-02-02 18:29:33 +00002138 if (self->write_func(self, "\n", 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002139 goto finally;
Tim Peters84e87f32001-03-17 04:50:51 +00002140
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002141 res = 1;
2142 goto finally;
2143 }
2144 else if (save(self, pid, 1) >= 0) {
Tim Peters0bc93f52003-02-02 18:29:33 +00002145 if (self->write_func(self, &binpersid, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002146 res = -1;
2147 else
2148 res = 1;
2149 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002150
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002151 goto finally;
2152 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002153
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002154 res = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002155
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002156 finally:
2157 Py_XDECREF(pid);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002158
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002159 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002160}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002161
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002162
Tim Peters84e87f32001-03-17 04:50:51 +00002163static int
Guido van Rossum60456fd1997-04-09 17:36:32 +00002164save_reduce(Picklerobject *self, PyObject *callable,
Tim Peterscba30e22003-02-01 06:24:36 +00002165 PyObject *tup, PyObject *state, PyObject *ob)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002166{
2167 static char reduce = REDUCE, build = BUILD;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002168
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002169 if (save(self, callable, 0) < 0)
2170 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002171
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002172 if (save(self, tup, 0) < 0)
2173 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002174
Tim Peters0bc93f52003-02-02 18:29:33 +00002175 if (self->write_func(self, &reduce, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002176 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002177
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002178 if (ob != NULL) {
2179 if (state && !PyDict_Check(state)) {
2180 if (put2(self, ob) < 0)
2181 return -1;
2182 }
2183 else {
2184 if (put(self, ob) < 0)
2185 return -1;
2186 }
2187 }
Tim Peters84e87f32001-03-17 04:50:51 +00002188
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002189 if (state) {
2190 if (save(self, state, 0) < 0)
2191 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002192
Tim Peters0bc93f52003-02-02 18:29:33 +00002193 if (self->write_func(self, &build, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002194 return -1;
2195 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002196
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002197 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002198}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002199
Guido van Rossum60456fd1997-04-09 17:36:32 +00002200static int
Tim Peters1d63c9f2003-02-02 20:29:39 +00002201save(Picklerobject *self, PyObject *args, int pers_save)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002202{
2203 PyTypeObject *type;
2204 PyObject *py_ob_id = 0, *__reduce__ = 0, *t = 0, *arg_tup = 0,
2205 *callable = 0, *state = 0;
2206 int res = -1, tmp, size;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002207
Martin v. Löwis5a395302002-08-04 08:20:23 +00002208 if (self->nesting++ > Py_GetRecursionLimit()){
2209 PyErr_SetString(PyExc_RuntimeError,
2210 "maximum recursion depth exceeded");
2211 goto finally;
2212 }
2213
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002214 if (!pers_save && self->pers_func) {
2215 if ((tmp = save_pers(self, args, self->pers_func)) != 0) {
2216 res = tmp;
2217 goto finally;
2218 }
2219 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002220
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002221 if (args == Py_None) {
2222 res = save_none(self, args);
2223 goto finally;
2224 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002225
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002226 type = args->ob_type;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002227
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002228 switch (type->tp_name[0]) {
Guido van Rossum77f6a652002-04-03 22:41:51 +00002229 case 'b':
2230 if (args == Py_False || args == Py_True) {
2231 res = save_bool(self, args);
2232 goto finally;
2233 }
2234 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002235 case 'i':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002236 if (type == &PyInt_Type) {
2237 res = save_int(self, args);
2238 goto finally;
2239 }
2240 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002241
Guido van Rossum60456fd1997-04-09 17:36:32 +00002242 case 'l':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002243 if (type == &PyLong_Type) {
2244 res = save_long(self, args);
2245 goto finally;
2246 }
2247 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002248
Guido van Rossum60456fd1997-04-09 17:36:32 +00002249 case 'f':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002250 if (type == &PyFloat_Type) {
2251 res = save_float(self, args);
2252 goto finally;
2253 }
2254 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002255
Guido van Rossum60456fd1997-04-09 17:36:32 +00002256 case 't':
Tim Peters1d63c9f2003-02-02 20:29:39 +00002257 if (type == &PyTuple_Type && PyTuple_Size(args) == 0) {
2258 res = save_tuple(self, args);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002259 goto finally;
2260 }
2261 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002262
Guido van Rossum60456fd1997-04-09 17:36:32 +00002263 case 's':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002264 if ((type == &PyString_Type) && (PyString_GET_SIZE(args) < 2)) {
2265 res = save_string(self, args, 0);
2266 goto finally;
2267 }
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00002268
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002269#ifdef Py_USING_UNICODE
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00002270 case 'u':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002271 if ((type == &PyUnicode_Type) && (PyString_GET_SIZE(args) < 2)) {
2272 res = save_unicode(self, args, 0);
2273 goto finally;
2274 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002275#endif
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002276 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002277
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002278 if (args->ob_refcnt > 1) {
Tim Peterscba30e22003-02-01 06:24:36 +00002279 if (!( py_ob_id = PyLong_FromVoidPtr(args)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002280 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002281
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002282 if (PyDict_GetItem(self->memo, py_ob_id)) {
2283 if (get(self, py_ob_id) < 0)
2284 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002285
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002286 res = 0;
2287 goto finally;
2288 }
2289 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002290
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002291 switch (type->tp_name[0]) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00002292 case 's':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002293 if (type == &PyString_Type) {
2294 res = save_string(self, args, 1);
2295 goto finally;
2296 }
2297 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002298
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002299#ifdef Py_USING_UNICODE
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00002300 case 'u':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002301 if (type == &PyUnicode_Type) {
2302 res = save_unicode(self, args, 1);
2303 goto finally;
2304 }
2305 break;
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002306#endif
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00002307
Guido van Rossum60456fd1997-04-09 17:36:32 +00002308 case 't':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002309 if (type == &PyTuple_Type) {
2310 res = save_tuple(self, args);
2311 goto finally;
2312 }
2313 if (type == &PyType_Type) {
2314 res = save_global(self, args, NULL);
2315 goto finally;
2316 }
2317 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002318
Guido van Rossum60456fd1997-04-09 17:36:32 +00002319 case 'l':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002320 if (type == &PyList_Type) {
2321 res = save_list(self, args);
2322 goto finally;
2323 }
2324 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002325
2326 case 'd':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002327 if (type == &PyDict_Type) {
2328 res = save_dict(self, args);
2329 goto finally;
2330 }
2331 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002332
2333 case 'i':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002334 if (type == &PyInstance_Type) {
2335 res = save_inst(self, args);
2336 goto finally;
2337 }
2338 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002339
2340 case 'c':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002341 if (type == &PyClass_Type) {
2342 res = save_global(self, args, NULL);
2343 goto finally;
2344 }
2345 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002346
2347 case 'f':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002348 if (type == &PyFunction_Type) {
2349 res = save_global(self, args, NULL);
2350 goto finally;
2351 }
2352 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002353
2354 case 'b':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002355 if (type == &PyCFunction_Type) {
2356 res = save_global(self, args, NULL);
2357 goto finally;
2358 }
2359 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002360
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002361 if (!pers_save && self->inst_pers_func) {
2362 if ((tmp = save_pers(self, args, self->inst_pers_func)) != 0) {
2363 res = tmp;
2364 goto finally;
2365 }
2366 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002367
Jeremy Hylton39c61162002-07-16 19:47:43 +00002368 if (PyType_IsSubtype(type, &PyType_Type)) {
2369 res = save_global(self, args, NULL);
2370 goto finally;
2371 }
2372
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002373 if ((__reduce__ = PyDict_GetItem(dispatch_table, (PyObject *)type))) {
2374 Py_INCREF(__reduce__);
Guido van Rossum60456fd1997-04-09 17:36:32 +00002375
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002376 Py_INCREF(args);
2377 ARG_TUP(self, args);
2378 if (self->arg) {
2379 t = PyObject_Call(__reduce__, self->arg, NULL);
2380 FREE_ARG_TUP(self);
2381 }
2382 if (! t) goto finally;
2383 }
2384 else {
2385 PyErr_Clear();
Guido van Rossum60456fd1997-04-09 17:36:32 +00002386
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002387 if ((__reduce__ = PyObject_GetAttr(args, __reduce___str))) {
2388 t = PyObject_Call(__reduce__, empty_tuple, NULL);
Tim Peterscba30e22003-02-01 06:24:36 +00002389 if (!t)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002390 goto finally;
2391 }
2392 else {
2393 PyErr_Clear();
2394 }
2395 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002396
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002397 if (t) {
2398 if (PyString_Check(t)) {
2399 res = save_global(self, args, t);
2400 goto finally;
2401 }
Tim Peters84e87f32001-03-17 04:50:51 +00002402
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002403 if (!PyTuple_Check(t)) {
2404 cPickle_ErrFormat(PicklingError, "Value returned by %s must "
2405 "be a tuple", "O", __reduce__);
2406 goto finally;
2407 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002408
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002409 size = PyTuple_Size(t);
Tim Peters84e87f32001-03-17 04:50:51 +00002410
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002411 if ((size != 3) && (size != 2)) {
2412 cPickle_ErrFormat(PicklingError, "tuple returned by %s must "
2413 "contain only two or three elements", "O", __reduce__);
2414 goto finally;
2415 }
Tim Peters84e87f32001-03-17 04:50:51 +00002416
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002417 callable = PyTuple_GET_ITEM(t, 0);
Guido van Rossum142eeb81997-08-13 03:14:41 +00002418
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002419 arg_tup = PyTuple_GET_ITEM(t, 1);
Guido van Rossum60456fd1997-04-09 17:36:32 +00002420
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002421 if (size > 2) {
2422 state = PyTuple_GET_ITEM(t, 2);
Guido van Rossum8e0ad0c2003-01-31 21:10:31 +00002423 if (state == Py_None)
2424 state = NULL;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002425 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002426
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002427 if (!( PyTuple_Check(arg_tup) || arg_tup==Py_None )) {
2428 cPickle_ErrFormat(PicklingError, "Second element of tuple "
2429 "returned by %s must be a tuple", "O", __reduce__);
2430 goto finally;
2431 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002432
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002433 res = save_reduce(self, callable, arg_tup, state, args);
2434 goto finally;
2435 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002436
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002437 PyErr_SetObject(UnpickleableError, args);
Guido van Rossum60456fd1997-04-09 17:36:32 +00002438
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002439 finally:
Martin v. Löwis5a395302002-08-04 08:20:23 +00002440 self->nesting--;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002441 Py_XDECREF(py_ob_id);
2442 Py_XDECREF(__reduce__);
2443 Py_XDECREF(t);
Tim Peters84e87f32001-03-17 04:50:51 +00002444
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002445 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002446}
2447
2448
2449static int
Tim Peterscba30e22003-02-01 06:24:36 +00002450dump(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002451{
2452 static char stop = STOP;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002453
Tim Peters4190fb82003-02-02 16:09:05 +00002454 if (self->proto >= 2) {
2455 char bytes[2];
2456
2457 bytes[0] = PROTO;
Tim Peters87482ea2003-02-02 16:16:30 +00002458 assert(self->proto >= 0 && self->proto < 256);
2459 bytes[1] = (char)self->proto;
Tim Peters4190fb82003-02-02 16:09:05 +00002460 if (self->write_func(self, bytes, 2) < 0)
2461 return -1;
2462 }
2463
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002464 if (save(self, args, 0) < 0)
2465 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002466
Tim Peters4190fb82003-02-02 16:09:05 +00002467 if (self->write_func(self, &stop, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002468 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002469
Tim Peters4190fb82003-02-02 16:09:05 +00002470 if (self->write_func(self, NULL, 0) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002471 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002472
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002473 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002474}
2475
2476static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00002477Pickle_clear_memo(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002478{
Tim Peterscba30e22003-02-01 06:24:36 +00002479 if (self->memo)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002480 PyDict_Clear(self->memo);
2481 Py_INCREF(Py_None);
2482 return Py_None;
2483}
2484
2485static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00002486Pickle_getvalue(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002487{
2488 int l, i, rsize, ssize, clear=1, lm;
2489 long ik;
2490 PyObject *k, *r;
2491 char *s, *p, *have_get;
2492 Pdata *data;
2493
2494 /* Can be called by Python code or C code */
Tim Peterscba30e22003-02-01 06:24:36 +00002495 if (args && !PyArg_ParseTuple(args, "|i:getvalue", &clear))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002496 return NULL;
2497
2498 /* Check to make sure we are based on a list */
2499 if (! Pdata_Check(self->file)) {
2500 PyErr_SetString(PicklingError,
2501 "Attempt to getvalue() a non-list-based pickler");
2502 return NULL;
2503 }
2504
2505 /* flush write buffer */
2506 if (write_other(self, NULL, 0) < 0) return NULL;
2507
2508 data=(Pdata*)self->file;
2509 l=data->length;
2510
2511 /* set up an array to hold get/put status */
Tim Petersac5687a2003-02-02 18:08:34 +00002512 lm = PyDict_Size(self->memo);
2513 if (lm < 0) return NULL;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002514 lm++;
Tim Petersac5687a2003-02-02 18:08:34 +00002515 have_get = malloc(lm);
2516 if (have_get == NULL) return PyErr_NoMemory();
2517 memset(have_get, 0, lm);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002518
2519 /* Scan for gets. */
Tim Petersac5687a2003-02-02 18:08:34 +00002520 for (rsize = 0, i = l; --i >= 0; ) {
2521 k = data->data[i];
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002522
Tim Petersac5687a2003-02-02 18:08:34 +00002523 if (PyString_Check(k))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002524 rsize += PyString_GET_SIZE(k);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002525
2526 else if (PyInt_Check(k)) { /* put */
Tim Petersac5687a2003-02-02 18:08:34 +00002527 ik = PyInt_AS_LONG((PyIntObject*)k);
2528 if (ik >= lm || ik == 0) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002529 PyErr_SetString(PicklingError,
2530 "Invalid get data");
2531 return NULL;
2532 }
Tim Petersac5687a2003-02-02 18:08:34 +00002533 if (have_get[ik]) /* with matching get */
2534 rsize += ik < 256 ? 2 : 5;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002535 }
2536
2537 else if (! (PyTuple_Check(k) &&
2538 PyTuple_GET_SIZE(k) == 2 &&
Tim Petersac5687a2003-02-02 18:08:34 +00002539 PyInt_Check((k = PyTuple_GET_ITEM(k, 0))))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002540 ) {
2541 PyErr_SetString(PicklingError,
2542 "Unexpected data in internal list");
2543 return NULL;
2544 }
2545
2546 else { /* put */
Tim Petersac5687a2003-02-02 18:08:34 +00002547 ik = PyInt_AS_LONG((PyIntObject *)k);
2548 if (ik >= lm || ik == 0) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002549 PyErr_SetString(PicklingError,
2550 "Invalid get data");
2551 return NULL;
2552 }
Tim Petersac5687a2003-02-02 18:08:34 +00002553 have_get[ik] = 1;
2554 rsize += ik < 256 ? 2 : 5;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002555 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002556 }
2557
2558 /* Now generate the result */
Tim Petersac5687a2003-02-02 18:08:34 +00002559 r = PyString_FromStringAndSize(NULL, rsize);
2560 if (r == NULL) goto err;
2561 s = PyString_AS_STRING((PyStringObject *)r);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002562
Tim Petersac5687a2003-02-02 18:08:34 +00002563 for (i = 0; i < l; i++) {
2564 k = data->data[i];
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002565
2566 if (PyString_Check(k)) {
Tim Petersac5687a2003-02-02 18:08:34 +00002567 ssize = PyString_GET_SIZE(k);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002568 if (ssize) {
Tim Petersac5687a2003-02-02 18:08:34 +00002569 p=PyString_AS_STRING((PyStringObject *)k);
2570 while (--ssize >= 0)
2571 *s++ = *p++;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002572 }
2573 }
2574
2575 else if (PyTuple_Check(k)) { /* get */
Tim Petersac5687a2003-02-02 18:08:34 +00002576 ik = PyInt_AS_LONG((PyIntObject *)
2577 PyTuple_GET_ITEM(k, 0));
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002578 if (ik < 256) {
2579 *s++ = BINGET;
2580 *s++ = (int)(ik & 0xff);
2581 }
2582 else {
2583 *s++ = LONG_BINGET;
2584 *s++ = (int)(ik & 0xff);
2585 *s++ = (int)((ik >> 8) & 0xff);
2586 *s++ = (int)((ik >> 16) & 0xff);
2587 *s++ = (int)((ik >> 24) & 0xff);
2588 }
2589 }
2590
2591 else { /* put */
Tim Petersac5687a2003-02-02 18:08:34 +00002592 ik = PyInt_AS_LONG((PyIntObject*)k);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002593
2594 if (have_get[ik]) { /* with matching get */
2595 if (ik < 256) {
2596 *s++ = BINPUT;
2597 *s++ = (int)(ik & 0xff);
2598 }
2599 else {
2600 *s++ = LONG_BINPUT;
2601 *s++ = (int)(ik & 0xff);
2602 *s++ = (int)((ik >> 8) & 0xff);
2603 *s++ = (int)((ik >> 16) & 0xff);
2604 *s++ = (int)((ik >> 24) & 0xff);
2605 }
2606 }
2607 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002608 }
2609
2610 if (clear) {
2611 PyDict_Clear(self->memo);
Tim Petersac5687a2003-02-02 18:08:34 +00002612 Pdata_clear(data, 0);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002613 }
2614
2615 free(have_get);
2616 return r;
2617 err:
2618 free(have_get);
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00002619 return NULL;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002620}
2621
2622static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00002623Pickler_dump(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002624{
2625 PyObject *ob;
2626 int get=0;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002627
Tim Peterscba30e22003-02-01 06:24:36 +00002628 if (!( PyArg_ParseTuple(args, "O|i:dump", &ob, &get)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002629 return NULL;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002630
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002631 if (dump(self, ob) < 0)
2632 return NULL;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002633
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002634 if (get) return Pickle_getvalue(self, NULL);
Guido van Rossum053b8df1998-11-25 16:18:00 +00002635
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002636 /* XXX Why does dump() return self? */
2637 Py_INCREF(self);
2638 return (PyObject*)self;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002639}
2640
2641
Tim Peterscba30e22003-02-01 06:24:36 +00002642static struct PyMethodDef Pickler_methods[] =
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002643{
Neal Norwitzb0493252002-03-31 14:44:22 +00002644 {"dump", (PyCFunction)Pickler_dump, METH_VARARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00002645 PyDoc_STR("dump(object) -- "
2646 "Write an object in pickle format to the object's pickle stream")},
Fred Drake0ebacc82002-05-01 20:36:39 +00002647 {"clear_memo", (PyCFunction)Pickle_clear_memo, METH_NOARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00002648 PyDoc_STR("clear_memo() -- Clear the picklers memo")},
Neal Norwitzb0493252002-03-31 14:44:22 +00002649 {"getvalue", (PyCFunction)Pickle_getvalue, METH_VARARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00002650 PyDoc_STR("getvalue() -- Finish picking a list-based pickle")},
Guido van Rossum60456fd1997-04-09 17:36:32 +00002651 {NULL, NULL} /* sentinel */
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002652};
2653
2654
2655static Picklerobject *
Tim Peters5bd2a792003-02-01 16:45:06 +00002656newPicklerobject(PyObject *file, int proto)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002657{
2658 Picklerobject *self;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002659
Tim Peters5bd2a792003-02-01 16:45:06 +00002660 if (proto < 0)
2661 proto = CURRENT_PROTOCOL_NUMBER;
2662 if (proto > CURRENT_PROTOCOL_NUMBER) {
2663 PyErr_Format(PyExc_ValueError, "pickle protocol %d asked for; "
2664 "the highest available protocol is %d",
2665 proto, CURRENT_PROTOCOL_NUMBER);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002666 return NULL;
Tim Peters5bd2a792003-02-01 16:45:06 +00002667 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002668
Tim Peters5bd2a792003-02-01 16:45:06 +00002669 self = PyObject_New(Picklerobject, &Picklertype);
2670 if (self == NULL)
2671 return NULL;
2672 self->proto = proto;
2673 self->bin = proto > 0;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002674 self->fp = NULL;
2675 self->write = NULL;
2676 self->memo = NULL;
2677 self->arg = NULL;
2678 self->pers_func = NULL;
2679 self->inst_pers_func = NULL;
2680 self->write_buf = NULL;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002681 self->fast = 0;
Martin v. Löwis5a395302002-08-04 08:20:23 +00002682 self->nesting = 0;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002683 self->fast_container = 0;
2684 self->fast_memo = NULL;
2685 self->buf_size = 0;
2686 self->dispatch_table = NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002687
Tim Peters5bd2a792003-02-01 16:45:06 +00002688 self->file = NULL;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002689 if (file)
2690 Py_INCREF(file);
Tim Peters5bd2a792003-02-01 16:45:06 +00002691 else {
2692 file = Pdata_New();
2693 if (file == NULL)
2694 goto err;
2695 }
2696 self->file = file;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002697
Tim Peterscba30e22003-02-01 06:24:36 +00002698 if (!( self->memo = PyDict_New()))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002699 goto err;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002700
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002701 if (PyFile_Check(file)) {
2702 self->fp = PyFile_AsFile(file);
2703 if (self->fp == NULL) {
Tim Peters5bd2a792003-02-01 16:45:06 +00002704 PyErr_SetString(PyExc_ValueError,
2705 "I/O operation on closed file");
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002706 goto err;
2707 }
2708 self->write_func = write_file;
Guido van Rossumc91fcaa1999-03-29 20:00:14 +00002709 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002710 else if (PycStringIO_OutputCheck(file)) {
2711 self->write_func = write_cStringIO;
2712 }
2713 else if (file == Py_None) {
2714 self->write_func = write_none;
2715 }
2716 else {
2717 self->write_func = write_other;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002718
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002719 if (! Pdata_Check(file)) {
2720 self->write = PyObject_GetAttr(file, write_str);
2721 if (!self->write) {
2722 PyErr_Clear();
Tim Peterscba30e22003-02-01 06:24:36 +00002723 PyErr_SetString(PyExc_TypeError,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002724 "argument must have 'write' "
2725 "attribute");
2726 goto err;
2727 }
2728 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002729
Tim Peters5bd2a792003-02-01 16:45:06 +00002730 self->write_buf = (char *)PyMem_Malloc(WRITE_BUF_SIZE);
2731 if (self->write_buf == NULL) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002732 PyErr_NoMemory();
2733 goto err;
2734 }
2735 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002736
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002737 if (PyEval_GetRestricted()) {
2738 /* Restricted execution, get private tables */
Tim Peters5b7da392003-02-04 00:21:07 +00002739 PyObject *m = PyImport_Import(copy_reg_str);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002740
Tim Peters5b7da392003-02-04 00:21:07 +00002741 if (m == NULL)
2742 goto err;
2743 self->dispatch_table = PyObject_GetAttr(m, dispatch_table_str);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002744 Py_DECREF(m);
Tim Peters5b7da392003-02-04 00:21:07 +00002745 if (self->dispatch_table == NULL)
2746 goto err;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002747 }
2748 else {
Tim Peters5b7da392003-02-04 00:21:07 +00002749 self->dispatch_table = dispatch_table;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002750 Py_INCREF(dispatch_table);
2751 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002752
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002753 return self;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002754
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002755 err:
Tim Peters5bd2a792003-02-01 16:45:06 +00002756 Py_DECREF(self);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002757 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002758}
2759
2760
2761static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00002762get_Pickler(PyObject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002763{
2764 PyObject *file = NULL;
Tim Peters5bd2a792003-02-01 16:45:06 +00002765 int proto = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002766
Tim Peters5bd2a792003-02-01 16:45:06 +00002767 /* XXX What is this doing? The documented signature is
2768 * XXX Pickler(file, proto=0), but this accepts Pickler() and
2769 * XXX Pickler(integer) too. The meaning then is clear as mud.
2770 * XXX Bug? Feature?
2771 */
2772 if (!PyArg_ParseTuple(args, "|i:Pickler", &proto)) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002773 PyErr_Clear();
Tim Peters5bd2a792003-02-01 16:45:06 +00002774 proto = 0;
2775 if (!PyArg_ParseTuple(args, "O|i:Pickler", &file, &proto))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002776 return NULL;
2777 }
Tim Peters5bd2a792003-02-01 16:45:06 +00002778 return (PyObject *)newPicklerobject(file, proto);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002779}
2780
2781
2782static void
Tim Peterscba30e22003-02-01 06:24:36 +00002783Pickler_dealloc(Picklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002784{
2785 Py_XDECREF(self->write);
2786 Py_XDECREF(self->memo);
2787 Py_XDECREF(self->fast_memo);
2788 Py_XDECREF(self->arg);
2789 Py_XDECREF(self->file);
2790 Py_XDECREF(self->pers_func);
2791 Py_XDECREF(self->inst_pers_func);
2792 Py_XDECREF(self->dispatch_table);
Tim Peters5bd2a792003-02-01 16:45:06 +00002793 PyMem_Free(self->write_buf);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002794 PyObject_Del(self);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002795}
2796
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002797static PyObject *
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00002798Pickler_get_pers_func(Picklerobject *p)
2799{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002800 if (p->pers_func == NULL)
2801 PyErr_SetString(PyExc_AttributeError, "persistent_id");
2802 else
2803 Py_INCREF(p->pers_func);
2804 return p->pers_func;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002805}
2806
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00002807static int
2808Pickler_set_pers_func(Picklerobject *p, PyObject *v)
2809{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002810 if (v == NULL) {
2811 PyErr_SetString(PyExc_TypeError,
2812 "attribute deletion is not supported");
2813 return -1;
2814 }
2815 Py_XDECREF(p->pers_func);
2816 Py_INCREF(v);
2817 p->pers_func = v;
2818 return 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002819}
2820
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00002821static int
2822Pickler_set_inst_pers_func(Picklerobject *p, PyObject *v)
2823{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002824 if (v == NULL) {
2825 PyErr_SetString(PyExc_TypeError,
2826 "attribute deletion is not supported");
2827 return -1;
2828 }
2829 Py_XDECREF(p->inst_pers_func);
2830 Py_INCREF(v);
2831 p->inst_pers_func = v;
2832 return 0;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00002833}
2834
2835static PyObject *
2836Pickler_get_memo(Picklerobject *p)
2837{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002838 if (p->memo == NULL)
2839 PyErr_SetString(PyExc_AttributeError, "memo");
2840 else
2841 Py_INCREF(p->memo);
2842 return p->memo;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00002843}
2844
2845static int
2846Pickler_set_memo(Picklerobject *p, PyObject *v)
2847{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002848 if (v == NULL) {
2849 PyErr_SetString(PyExc_TypeError,
2850 "attribute deletion is not supported");
2851 return -1;
2852 }
2853 if (!PyDict_Check(v)) {
2854 PyErr_SetString(PyExc_TypeError, "memo must be a dictionary");
2855 return -1;
2856 }
2857 Py_XDECREF(p->memo);
2858 Py_INCREF(v);
2859 p->memo = v;
2860 return 0;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00002861}
2862
2863static PyObject *
2864Pickler_get_error(Picklerobject *p)
2865{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002866 /* why is this an attribute on the Pickler? */
2867 Py_INCREF(PicklingError);
2868 return PicklingError;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00002869}
2870
2871static PyMemberDef Pickler_members[] = {
2872 {"binary", T_INT, offsetof(Picklerobject, bin)},
2873 {"fast", T_INT, offsetof(Picklerobject, fast)},
Jeremy Hylton3eb46f32001-10-16 17:10:49 +00002874 {NULL}
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00002875};
2876
2877static PyGetSetDef Pickler_getsets[] = {
Tim Peterscba30e22003-02-01 06:24:36 +00002878 {"persistent_id", (getter)Pickler_get_pers_func,
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00002879 (setter)Pickler_set_pers_func},
2880 {"inst_persistent_id", NULL, (setter)Pickler_set_inst_pers_func},
2881 {"memo", (getter)Pickler_get_memo, (setter)Pickler_set_memo},
Jeremy Hylton3eb46f32001-10-16 17:10:49 +00002882 {"PicklingError", (getter)Pickler_get_error, NULL},
2883 {NULL}
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00002884};
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002885
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002886PyDoc_STRVAR(Picklertype__doc__,
2887"Objects that know how to pickle objects\n");
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002888
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002889static PyTypeObject Picklertype = {
2890 PyObject_HEAD_INIT(NULL)
Guido van Rossum60456fd1997-04-09 17:36:32 +00002891 0, /*ob_size*/
Guido van Rossum14648392001-12-08 18:02:58 +00002892 "cPickle.Pickler", /*tp_name*/
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00002893 sizeof(Picklerobject), /*tp_basicsize*/
2894 0,
2895 (destructor)Pickler_dealloc, /* tp_dealloc */
2896 0, /* tp_print */
2897 0, /* tp_getattr */
2898 0, /* tp_setattr */
2899 0, /* tp_compare */
2900 0, /* tp_repr */
2901 0, /* tp_as_number */
2902 0, /* tp_as_sequence */
2903 0, /* tp_as_mapping */
2904 0, /* tp_hash */
2905 0, /* tp_call */
2906 0, /* tp_str */
Jason Tishlerfb8595d2003-01-06 12:41:26 +00002907 PyObject_GenericGetAttr, /* tp_getattro */
2908 PyObject_GenericSetAttr, /* tp_setattro */
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00002909 0, /* tp_as_buffer */
2910 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
2911 Picklertype__doc__, /* tp_doc */
2912 0, /* tp_traverse */
2913 0, /* tp_clear */
2914 0, /* tp_richcompare */
2915 0, /* tp_weaklistoffset */
2916 0, /* tp_iter */
2917 0, /* tp_iternext */
2918 Pickler_methods, /* tp_methods */
2919 Pickler_members, /* tp_members */
2920 Pickler_getsets, /* tp_getset */
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002921};
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002922
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002923static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00002924find_class(PyObject *py_module_name, PyObject *py_global_name, PyObject *fc)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002925{
2926 PyObject *global = 0, *module;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002927
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002928 if (fc) {
2929 if (fc==Py_None) {
2930 PyErr_SetString(UnpicklingError,
2931 "Global and instance pickles are not supported.");
2932 return NULL;
2933 }
Tim Peterscba30e22003-02-01 06:24:36 +00002934 return PyObject_CallFunction(fc, "OO", py_module_name,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002935 py_global_name);
2936 }
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00002937
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002938 module = PySys_GetObject("modules");
2939 if (module == NULL)
2940 return NULL;
Jeremy Hyltond1055231998-08-11 19:52:51 +00002941
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002942 module = PyDict_GetItem(module, py_module_name);
2943 if (module == NULL) {
2944 module = PyImport_Import(py_module_name);
2945 if (!module)
2946 return NULL;
2947 global = PyObject_GetAttr(module, py_global_name);
2948 Py_DECREF(module);
2949 }
2950 else
2951 global = PyObject_GetAttr(module, py_global_name);
2952 return global;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002953}
2954
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002955static int
Tim Peterscba30e22003-02-01 06:24:36 +00002956marker(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002957{
2958 if (self->num_marks < 1) {
2959 PyErr_SetString(UnpicklingError, "could not find MARK");
2960 return -1;
2961 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002962
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002963 return self->marks[--self->num_marks];
Guido van Rossum60456fd1997-04-09 17:36:32 +00002964}
2965
Tim Peters84e87f32001-03-17 04:50:51 +00002966
Guido van Rossum60456fd1997-04-09 17:36:32 +00002967static int
Tim Peterscba30e22003-02-01 06:24:36 +00002968load_none(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002969{
2970 PDATA_APPEND(self->stack, Py_None, -1);
2971 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002972}
2973
Guido van Rossumf9ffb031999-02-04 14:54:04 +00002974static int
Tim Peterscba30e22003-02-01 06:24:36 +00002975bad_readline(void)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002976{
2977 PyErr_SetString(UnpicklingError, "pickle data was truncated");
2978 return -1;
Guido van Rossumf9ffb031999-02-04 14:54:04 +00002979}
Guido van Rossum60456fd1997-04-09 17:36:32 +00002980
2981static int
Tim Peterscba30e22003-02-01 06:24:36 +00002982load_int(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002983{
2984 PyObject *py_int = 0;
2985 char *endptr, *s;
2986 int len, res = -1;
2987 long l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002988
Tim Peters0bc93f52003-02-02 18:29:33 +00002989 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002990 if (len < 2) return bad_readline();
2991 if (!( s=pystrndup(s,len))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002992
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002993 errno = 0;
2994 l = strtol(s, &endptr, 0);
Guido van Rossum60456fd1997-04-09 17:36:32 +00002995
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002996 if (errno || (*endptr != '\n') || (endptr[1] != '\0')) {
2997 /* Hm, maybe we've got something long. Let's try reading
2998 it as a Python long object. */
2999 errno = 0;
3000 py_int = PyLong_FromString(s, NULL, 0);
3001 if (py_int == NULL) {
3002 PyErr_SetString(PyExc_ValueError,
3003 "could not convert string to int");
3004 goto finally;
3005 }
3006 }
3007 else {
Guido van Rossume2763392002-04-05 19:30:08 +00003008 if (len == 3 && (l == 0 || l == 1)) {
3009 if (!( py_int = PyBool_FromLong(l))) goto finally;
3010 }
3011 else {
3012 if (!( py_int = PyInt_FromLong(l))) goto finally;
3013 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003014 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003015
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003016 free(s);
3017 PDATA_PUSH(self->stack, py_int, -1);
3018 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003019
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003020 finally:
3021 free(s);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003022
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003023 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003024}
3025
Tim Peters3c67d792003-02-02 17:59:11 +00003026static int
3027load_bool(Unpicklerobject *self, PyObject *boolean)
3028{
3029 assert(boolean == Py_True || boolean == Py_False);
Tim Peterse0a39072003-02-03 15:45:56 +00003030 PDATA_APPEND(self->stack, boolean, -1);
Tim Peters3c67d792003-02-02 17:59:11 +00003031 return 0;
3032}
3033
Tim Petersee1a53c2003-02-02 02:57:53 +00003034/* s contains x bytes of a little-endian integer. Return its value as a
3035 * C int. Obscure: when x is 1 or 2, this is an unsigned little-endian
3036 * int, but when x is 4 it's a signed one. This is an historical source
3037 * of x-platform bugs.
3038 */
Tim Peters84e87f32001-03-17 04:50:51 +00003039static long
Tim Petersee1a53c2003-02-02 02:57:53 +00003040calc_binint(char *s, int x)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003041{
3042 unsigned char c;
3043 int i;
3044 long l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003045
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003046 for (i = 0, l = 0L; i < x; i++) {
3047 c = (unsigned char)s[i];
3048 l |= (long)c << (i * 8);
3049 }
Tim Petersbfa18f72001-04-10 01:54:42 +00003050#if SIZEOF_LONG > 4
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003051 /* Unlike BININT1 and BININT2, BININT (more accurately BININT4)
3052 * is signed, so on a box with longs bigger than 4 bytes we need
3053 * to extend a BININT's sign bit to the full width.
3054 */
3055 if (x == 4 && l & (1L << 31))
3056 l |= (~0L) << 32;
Tim Petersbfa18f72001-04-10 01:54:42 +00003057#endif
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003058 return l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003059}
3060
3061
3062static int
Tim Peterscba30e22003-02-01 06:24:36 +00003063load_binintx(Unpicklerobject *self, char *s, int x)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003064{
3065 PyObject *py_int = 0;
3066 long l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003067
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003068 l = calc_binint(s, x);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003069
Tim Peterscba30e22003-02-01 06:24:36 +00003070 if (!( py_int = PyInt_FromLong(l)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003071 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003072
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003073 PDATA_PUSH(self->stack, py_int, -1);
3074 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003075}
3076
3077
3078static int
Tim Peterscba30e22003-02-01 06:24:36 +00003079load_binint(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003080{
3081 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003082
Tim Peters0bc93f52003-02-02 18:29:33 +00003083 if (self->read_func(self, &s, 4) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003084 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003085
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003086 return load_binintx(self, s, 4);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003087}
3088
3089
3090static int
Tim Peterscba30e22003-02-01 06:24:36 +00003091load_binint1(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003092{
3093 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003094
Tim Peters0bc93f52003-02-02 18:29:33 +00003095 if (self->read_func(self, &s, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003096 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003097
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003098 return load_binintx(self, s, 1);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003099}
3100
3101
3102static int
Tim Peterscba30e22003-02-01 06:24:36 +00003103load_binint2(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003104{
3105 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003106
Tim Peters0bc93f52003-02-02 18:29:33 +00003107 if (self->read_func(self, &s, 2) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003108 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003109
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003110 return load_binintx(self, s, 2);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003111}
Tim Peters84e87f32001-03-17 04:50:51 +00003112
Guido van Rossum60456fd1997-04-09 17:36:32 +00003113static int
Tim Peterscba30e22003-02-01 06:24:36 +00003114load_long(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003115{
3116 PyObject *l = 0;
3117 char *end, *s;
3118 int len, res = -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003119
Tim Peters0bc93f52003-02-02 18:29:33 +00003120 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003121 if (len < 2) return bad_readline();
3122 if (!( s=pystrndup(s,len))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003123
Tim Peterscba30e22003-02-01 06:24:36 +00003124 if (!( l = PyLong_FromString(s, &end, 0)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003125 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003126
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003127 free(s);
3128 PDATA_PUSH(self->stack, l, -1);
3129 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003130
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003131 finally:
3132 free(s);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003133
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003134 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003135}
3136
Tim Petersee1a53c2003-02-02 02:57:53 +00003137/* 'size' bytes contain the # of bytes of little-endian 256's-complement
3138 * data following.
3139 */
3140static int
3141load_counted_long(Unpicklerobject *self, int size)
3142{
3143 int i;
3144 char *nbytes;
3145 unsigned char *pdata;
3146 PyObject *along;
3147
3148 assert(size == 1 || size == 4);
3149 i = self->read_func(self, &nbytes, size);
3150 if (i < 0) return -1;
3151
3152 size = calc_binint(nbytes, size);
3153 if (size < 0) {
3154 /* Corrupt or hostile pickle -- we never write one like
3155 * this.
3156 */
Tim Peters0c7c48e2003-02-03 22:07:24 +00003157 PyErr_SetString(UnpicklingError, "LONG pickle has negative "
Tim Petersee1a53c2003-02-02 02:57:53 +00003158 "byte count");
3159 return -1;
3160 }
3161
3162 if (size == 0)
3163 along = PyLong_FromLong(0L);
3164 else {
3165 /* Read the raw little-endian bytes & convert. */
Neal Norwitz529baf22003-02-02 17:08:33 +00003166 i = self->read_func(self, (char **)&pdata, size);
Tim Petersee1a53c2003-02-02 02:57:53 +00003167 if (i < 0) return -1;
3168 along = _PyLong_FromByteArray(pdata, (size_t)size,
3169 1 /* little endian */, 1 /* signed */);
3170 }
3171 if (along == NULL)
3172 return -1;
3173 PDATA_PUSH(self->stack, along, -1);
3174 return 0;
3175}
Tim Peters84e87f32001-03-17 04:50:51 +00003176
Guido van Rossum60456fd1997-04-09 17:36:32 +00003177static int
Tim Peterscba30e22003-02-01 06:24:36 +00003178load_float(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003179{
3180 PyObject *py_float = 0;
3181 char *endptr, *s;
3182 int len, res = -1;
3183 double d;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003184
Tim Peters0bc93f52003-02-02 18:29:33 +00003185 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003186 if (len < 2) return bad_readline();
3187 if (!( s=pystrndup(s,len))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003188
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003189 errno = 0;
3190 d = strtod(s, &endptr);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003191
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003192 if (errno || (endptr[0] != '\n') || (endptr[1] != '\0')) {
3193 PyErr_SetString(PyExc_ValueError,
3194 "could not convert string to float");
3195 goto finally;
3196 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003197
Tim Peterscba30e22003-02-01 06:24:36 +00003198 if (!( py_float = PyFloat_FromDouble(d)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003199 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003200
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003201 free(s);
3202 PDATA_PUSH(self->stack, py_float, -1);
3203 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003204
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003205 finally:
3206 free(s);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003207
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003208 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003209}
3210
Guido van Rossum60456fd1997-04-09 17:36:32 +00003211static int
Tim Peterscba30e22003-02-01 06:24:36 +00003212load_binfloat(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003213{
3214 PyObject *py_float = 0;
3215 int s, e;
3216 long fhi, flo;
3217 double x;
3218 char *p;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003219
Tim Peters0bc93f52003-02-02 18:29:33 +00003220 if (self->read_func(self, &p, 8) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003221 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003222
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003223 /* First byte */
3224 s = (*p>>7) & 1;
3225 e = (*p & 0x7F) << 4;
3226 p++;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003227
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003228 /* Second byte */
3229 e |= (*p>>4) & 0xF;
3230 fhi = (*p & 0xF) << 24;
3231 p++;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003232
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003233 /* Third byte */
3234 fhi |= (*p & 0xFF) << 16;
3235 p++;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003236
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003237 /* Fourth byte */
3238 fhi |= (*p & 0xFF) << 8;
3239 p++;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003240
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003241 /* Fifth byte */
3242 fhi |= *p & 0xFF;
3243 p++;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003244
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003245 /* Sixth byte */
3246 flo = (*p & 0xFF) << 16;
3247 p++;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003248
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003249 /* Seventh byte */
3250 flo |= (*p & 0xFF) << 8;
3251 p++;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003252
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003253 /* Eighth byte */
3254 flo |= *p & 0xFF;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003255
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003256 x = (double)fhi + (double)flo / 16777216.0; /* 2**24 */
3257 x /= 268435456.0; /* 2**28 */
Guido van Rossum60456fd1997-04-09 17:36:32 +00003258
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003259 /* XXX This sadly ignores Inf/NaN */
3260 if (e == 0)
3261 e = -1022;
3262 else {
3263 x += 1.0;
3264 e -= 1023;
3265 }
3266 x = ldexp(x, e);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003267
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003268 if (s)
3269 x = -x;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003270
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003271 if (!( py_float = PyFloat_FromDouble(x))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003272
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003273 PDATA_PUSH(self->stack, py_float, -1);
3274 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003275}
Guido van Rossum60456fd1997-04-09 17:36:32 +00003276
3277static int
Tim Peterscba30e22003-02-01 06:24:36 +00003278load_string(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003279{
3280 PyObject *str = 0;
Martin v. Löwis8a8da792002-08-14 07:46:28 +00003281 int len, res = -1;
3282 char *s, *p;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003283
Tim Peters0bc93f52003-02-02 18:29:33 +00003284 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003285 if (len < 2) return bad_readline();
3286 if (!( s=pystrndup(s,len))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003287
Martin v. Löwis8a8da792002-08-14 07:46:28 +00003288
3289 /* Strip outermost quotes */
3290 while (s[len-1] <= ' ')
3291 len--;
3292 if(s[0]=='"' && s[len-1]=='"'){
3293 s[len-1] = '\0';
3294 p = s + 1 ;
3295 len -= 2;
3296 } else if(s[0]=='\'' && s[len-1]=='\''){
3297 s[len-1] = '\0';
3298 p = s + 1 ;
3299 len -= 2;
3300 } else
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003301 goto insecure;
3302 /********************************************/
Guido van Rossum9716aaa1997-12-08 15:15:16 +00003303
Martin v. Löwis8a8da792002-08-14 07:46:28 +00003304 str = PyString_DecodeEscape(p, len, NULL, 0, NULL);
3305 if (str) {
3306 PDATA_PUSH(self->stack, str, -1);
3307 res = 0;
3308 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003309 free(s);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003310 return res;
Tim Peters84e87f32001-03-17 04:50:51 +00003311
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003312 insecure:
3313 free(s);
3314 PyErr_SetString(PyExc_ValueError,"insecure string pickle");
3315 return -1;
Tim Peters84e87f32001-03-17 04:50:51 +00003316}
Guido van Rossum60456fd1997-04-09 17:36:32 +00003317
3318
3319static int
Tim Peterscba30e22003-02-01 06:24:36 +00003320load_binstring(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003321{
3322 PyObject *py_string = 0;
3323 long l;
3324 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003325
Tim Peters0bc93f52003-02-02 18:29:33 +00003326 if (self->read_func(self, &s, 4) < 0) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003327
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003328 l = calc_binint(s, 4);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003329
Tim Peters0bc93f52003-02-02 18:29:33 +00003330 if (self->read_func(self, &s, l) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003331 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003332
Tim Peterscba30e22003-02-01 06:24:36 +00003333 if (!( py_string = PyString_FromStringAndSize(s, l)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003334 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003335
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003336 PDATA_PUSH(self->stack, py_string, -1);
3337 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003338}
3339
3340
3341static int
Tim Peterscba30e22003-02-01 06:24:36 +00003342load_short_binstring(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003343{
3344 PyObject *py_string = 0;
3345 unsigned char l;
3346 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003347
Tim Peters0bc93f52003-02-02 18:29:33 +00003348 if (self->read_func(self, &s, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003349 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003350
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003351 l = (unsigned char)s[0];
Guido van Rossum60456fd1997-04-09 17:36:32 +00003352
Tim Peters0bc93f52003-02-02 18:29:33 +00003353 if (self->read_func(self, &s, l) < 0) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003354
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003355 if (!( py_string = PyString_FromStringAndSize(s, l))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003356
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003357 PDATA_PUSH(self->stack, py_string, -1);
3358 return 0;
Tim Peters84e87f32001-03-17 04:50:51 +00003359}
Guido van Rossum60456fd1997-04-09 17:36:32 +00003360
3361
Martin v. Löwis339d0f72001-08-17 18:39:25 +00003362#ifdef Py_USING_UNICODE
Guido van Rossum60456fd1997-04-09 17:36:32 +00003363static int
Tim Peterscba30e22003-02-01 06:24:36 +00003364load_unicode(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003365{
3366 PyObject *str = 0;
3367 int len, res = -1;
3368 char *s;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003369
Tim Peters0bc93f52003-02-02 18:29:33 +00003370 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003371 if (len < 1) return bad_readline();
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003372
Tim Peterscba30e22003-02-01 06:24:36 +00003373 if (!( str = PyUnicode_DecodeRawUnicodeEscape(s, len - 1, NULL)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003374 goto finally;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003375
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003376 PDATA_PUSH(self->stack, str, -1);
3377 return 0;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003378
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003379 finally:
3380 return res;
Tim Peters84e87f32001-03-17 04:50:51 +00003381}
Martin v. Löwis339d0f72001-08-17 18:39:25 +00003382#endif
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003383
3384
Martin v. Löwis339d0f72001-08-17 18:39:25 +00003385#ifdef Py_USING_UNICODE
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003386static int
Tim Peterscba30e22003-02-01 06:24:36 +00003387load_binunicode(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003388{
3389 PyObject *unicode;
3390 long l;
3391 char *s;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003392
Tim Peters0bc93f52003-02-02 18:29:33 +00003393 if (self->read_func(self, &s, 4) < 0) return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003394
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003395 l = calc_binint(s, 4);
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003396
Tim Peters0bc93f52003-02-02 18:29:33 +00003397 if (self->read_func(self, &s, l) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003398 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003399
Tim Peterscba30e22003-02-01 06:24:36 +00003400 if (!( unicode = PyUnicode_DecodeUTF8(s, l, NULL)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003401 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003402
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003403 PDATA_PUSH(self->stack, unicode, -1);
3404 return 0;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003405}
Martin v. Löwis339d0f72001-08-17 18:39:25 +00003406#endif
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003407
3408
3409static int
Tim Peterscba30e22003-02-01 06:24:36 +00003410load_tuple(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003411{
3412 PyObject *tup;
3413 int i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003414
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003415 if ((i = marker(self)) < 0) return -1;
3416 if (!( tup=Pdata_popTuple(self->stack, i))) return -1;
3417 PDATA_PUSH(self->stack, tup, -1);
3418 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003419}
3420
3421static int
Tim Peters1d63c9f2003-02-02 20:29:39 +00003422load_counted_tuple(Unpicklerobject *self, int len)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003423{
Tim Peters1d63c9f2003-02-02 20:29:39 +00003424 PyObject *tup = PyTuple_New(len);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003425
Tim Peters1d63c9f2003-02-02 20:29:39 +00003426 if (tup == NULL)
3427 return -1;
3428
3429 while (--len >= 0) {
3430 PyObject *element;
3431
3432 PDATA_POP(self->stack, element);
3433 if (element == NULL)
3434 return -1;
3435 PyTuple_SET_ITEM(tup, len, element);
3436 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003437 PDATA_PUSH(self->stack, tup, -1);
3438 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003439}
3440
3441static int
Tim Peterscba30e22003-02-01 06:24:36 +00003442load_empty_list(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003443{
3444 PyObject *list;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003445
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003446 if (!( list=PyList_New(0))) return -1;
3447 PDATA_PUSH(self->stack, list, -1);
3448 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003449}
3450
3451static int
Tim Peterscba30e22003-02-01 06:24:36 +00003452load_empty_dict(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003453{
3454 PyObject *dict;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003455
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003456 if (!( dict=PyDict_New())) return -1;
3457 PDATA_PUSH(self->stack, dict, -1);
3458 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003459}
3460
3461
3462static int
Tim Peterscba30e22003-02-01 06:24:36 +00003463load_list(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003464{
3465 PyObject *list = 0;
3466 int i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003467
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003468 if ((i = marker(self)) < 0) return -1;
3469 if (!( list=Pdata_popList(self->stack, i))) return -1;
3470 PDATA_PUSH(self->stack, list, -1);
3471 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003472}
3473
3474static int
Tim Peterscba30e22003-02-01 06:24:36 +00003475load_dict(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003476{
3477 PyObject *dict, *key, *value;
3478 int i, j, k;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003479
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003480 if ((i = marker(self)) < 0) return -1;
3481 j=self->stack->length;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003482
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003483 if (!( dict = PyDict_New())) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003484
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003485 for (k = i+1; k < j; k += 2) {
3486 key =self->stack->data[k-1];
3487 value=self->stack->data[k ];
3488 if (PyDict_SetItem(dict, key, value) < 0) {
3489 Py_DECREF(dict);
3490 return -1;
3491 }
3492 }
3493 Pdata_clear(self->stack, i);
3494 PDATA_PUSH(self->stack, dict, -1);
3495 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003496}
3497
3498static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00003499Instance_New(PyObject *cls, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003500{
Tim Peters1f1b2d22003-02-01 02:16:37 +00003501 PyObject *r = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003502
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003503 if (PyClass_Check(cls)) {
3504 int l;
Tim Peters84e87f32001-03-17 04:50:51 +00003505
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003506 if ((l=PyObject_Size(args)) < 0) goto err;
3507 if (!( l )) {
3508 PyObject *__getinitargs__;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003509
Tim Peterscba30e22003-02-01 06:24:36 +00003510 __getinitargs__ = PyObject_GetAttr(cls,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003511 __getinitargs___str);
3512 if (!__getinitargs__) {
Tim Peterscba30e22003-02-01 06:24:36 +00003513 /* We have a class with no __getinitargs__,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003514 so bypass usual construction */
3515 PyObject *inst;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003516
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003517 PyErr_Clear();
Tim Peterscba30e22003-02-01 06:24:36 +00003518 if (!( inst=PyInstance_NewRaw(cls, NULL)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003519 goto err;
3520 return inst;
3521 }
3522 Py_DECREF(__getinitargs__);
3523 }
Tim Peters84e87f32001-03-17 04:50:51 +00003524
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003525 if ((r=PyInstance_New(cls, args, NULL))) return r;
3526 else goto err;
3527 }
Tim Peters84e87f32001-03-17 04:50:51 +00003528
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003529 if (args==Py_None) {
3530 /* Special case, call cls.__basicnew__() */
3531 PyObject *basicnew;
Tim Peters84e87f32001-03-17 04:50:51 +00003532
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003533 basicnew = PyObject_GetAttr(cls, __basicnew___str);
3534 if (!basicnew) return NULL;
3535 r=PyObject_CallObject(basicnew, NULL);
3536 Py_DECREF(basicnew);
3537 if (r) return r;
3538 }
Guido van Rossum9716aaa1997-12-08 15:15:16 +00003539
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003540 if ((r=PyObject_CallObject(cls, args))) return r;
Guido van Rossum142eeb81997-08-13 03:14:41 +00003541
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003542 err:
3543 {
3544 PyObject *tp, *v, *tb;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003545
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003546 PyErr_Fetch(&tp, &v, &tb);
3547 if ((r=Py_BuildValue("OOO",v,cls,args))) {
3548 Py_XDECREF(v);
3549 v=r;
3550 }
3551 PyErr_Restore(tp,v,tb);
3552 }
3553 return NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003554}
Tim Peters84e87f32001-03-17 04:50:51 +00003555
Guido van Rossum60456fd1997-04-09 17:36:32 +00003556
3557static int
Tim Peterscba30e22003-02-01 06:24:36 +00003558load_obj(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003559{
3560 PyObject *class, *tup, *obj=0;
3561 int i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003562
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003563 if ((i = marker(self)) < 0) return -1;
3564 if (!( tup=Pdata_popTuple(self->stack, i+1))) return -1;
3565 PDATA_POP(self->stack, class);
3566 if (class) {
3567 obj = Instance_New(class, tup);
3568 Py_DECREF(class);
3569 }
3570 Py_DECREF(tup);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003571
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003572 if (! obj) return -1;
3573 PDATA_PUSH(self->stack, obj, -1);
3574 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003575}
3576
3577
3578static int
Tim Peterscba30e22003-02-01 06:24:36 +00003579load_inst(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003580{
3581 PyObject *tup, *class=0, *obj=0, *module_name, *class_name;
3582 int i, len;
3583 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003584
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003585 if ((i = marker(self)) < 0) return -1;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003586
Tim Peters0bc93f52003-02-02 18:29:33 +00003587 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003588 if (len < 2) return bad_readline();
3589 module_name = PyString_FromStringAndSize(s, len - 1);
3590 if (!module_name) return -1;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003591
Tim Peters0bc93f52003-02-02 18:29:33 +00003592 if ((len = self->readline_func(self, &s)) >= 0) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003593 if (len < 2) return bad_readline();
3594 if ((class_name = PyString_FromStringAndSize(s, len - 1))) {
Tim Peterscba30e22003-02-01 06:24:36 +00003595 class = find_class(module_name, class_name,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003596 self->find_class);
3597 Py_DECREF(class_name);
3598 }
3599 }
3600 Py_DECREF(module_name);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003601
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003602 if (! class) return -1;
Tim Peters84e87f32001-03-17 04:50:51 +00003603
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003604 if ((tup=Pdata_popTuple(self->stack, i))) {
3605 obj = Instance_New(class, tup);
3606 Py_DECREF(tup);
3607 }
3608 Py_DECREF(class);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003609
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003610 if (! obj) return -1;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003611
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003612 PDATA_PUSH(self->stack, obj, -1);
3613 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003614}
3615
3616
3617static int
Tim Peterscba30e22003-02-01 06:24:36 +00003618load_global(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003619{
3620 PyObject *class = 0, *module_name = 0, *class_name = 0;
3621 int len;
3622 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003623
Tim Peters0bc93f52003-02-02 18:29:33 +00003624 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003625 if (len < 2) return bad_readline();
3626 module_name = PyString_FromStringAndSize(s, len - 1);
3627 if (!module_name) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003628
Tim Peters0bc93f52003-02-02 18:29:33 +00003629 if ((len = self->readline_func(self, &s)) >= 0) {
Tim Peters4e52ca82002-12-07 02:43:28 +00003630 if (len < 2) {
3631 Py_DECREF(module_name);
3632 return bad_readline();
3633 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003634 if ((class_name = PyString_FromStringAndSize(s, len - 1))) {
Tim Peterscba30e22003-02-01 06:24:36 +00003635 class = find_class(module_name, class_name,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003636 self->find_class);
3637 Py_DECREF(class_name);
3638 }
3639 }
3640 Py_DECREF(module_name);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003641
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003642 if (! class) return -1;
3643 PDATA_PUSH(self->stack, class, -1);
3644 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003645}
3646
3647
3648static int
Tim Peterscba30e22003-02-01 06:24:36 +00003649load_persid(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003650{
3651 PyObject *pid = 0;
3652 int len;
3653 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003654
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003655 if (self->pers_func) {
Tim Peters0bc93f52003-02-02 18:29:33 +00003656 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003657 if (len < 2) return bad_readline();
3658
3659 pid = PyString_FromStringAndSize(s, len - 1);
3660 if (!pid) return -1;
3661
3662 if (PyList_Check(self->pers_func)) {
3663 if (PyList_Append(self->pers_func, pid) < 0) {
3664 Py_DECREF(pid);
3665 return -1;
3666 }
3667 }
3668 else {
3669 ARG_TUP(self, pid);
3670 if (self->arg) {
Tim Peterscba30e22003-02-01 06:24:36 +00003671 pid = PyObject_Call(self->pers_func, self->arg,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003672 NULL);
3673 FREE_ARG_TUP(self);
3674 }
3675 }
3676
3677 if (! pid) return -1;
3678
3679 PDATA_PUSH(self->stack, pid, -1);
3680 return 0;
3681 }
3682 else {
3683 PyErr_SetString(UnpicklingError,
3684 "A load persistent id instruction was encountered,\n"
3685 "but no persistent_load function was specified.");
3686 return -1;
3687 }
3688}
3689
3690static int
Tim Peterscba30e22003-02-01 06:24:36 +00003691load_binpersid(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003692{
3693 PyObject *pid = 0;
3694
3695 if (self->pers_func) {
3696 PDATA_POP(self->stack, pid);
3697 if (! pid) return -1;
3698
3699 if (PyList_Check(self->pers_func)) {
3700 if (PyList_Append(self->pers_func, pid) < 0) {
3701 Py_DECREF(pid);
3702 return -1;
3703 }
3704 }
3705 else {
3706 ARG_TUP(self, pid);
3707 if (self->arg) {
3708 pid = PyObject_Call(self->pers_func, self->arg,
3709 NULL);
3710 FREE_ARG_TUP(self);
3711 }
3712 if (! pid) return -1;
3713 }
3714
3715 PDATA_PUSH(self->stack, pid, -1);
3716 return 0;
3717 }
3718 else {
3719 PyErr_SetString(UnpicklingError,
3720 "A load persistent id instruction was encountered,\n"
3721 "but no persistent_load function was specified.");
3722 return -1;
3723 }
3724}
3725
3726
3727static int
Tim Peterscba30e22003-02-01 06:24:36 +00003728load_pop(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003729{
3730 int len;
3731
3732 if (!( (len=self->stack->length) > 0 )) return stackUnderflow();
3733
3734 /* Note that we split the (pickle.py) stack into two stacks,
3735 an object stack and a mark stack. We have to be clever and
3736 pop the right one. We do this by looking at the top of the
3737 mark stack.
3738 */
3739
3740 if ((self->num_marks > 0) &&
3741 (self->marks[self->num_marks - 1] == len))
3742 self->num_marks--;
3743 else {
3744 len--;
3745 Py_DECREF(self->stack->data[len]);
3746 self->stack->length=len;
3747 }
3748
3749 return 0;
3750}
3751
3752
3753static int
Tim Peterscba30e22003-02-01 06:24:36 +00003754load_pop_mark(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003755{
3756 int i;
3757
3758 if ((i = marker(self)) < 0)
3759 return -1;
3760
3761 Pdata_clear(self->stack, i);
3762
3763 return 0;
3764}
3765
3766
3767static int
Tim Peterscba30e22003-02-01 06:24:36 +00003768load_dup(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003769{
3770 PyObject *last;
3771 int len;
3772
3773 if ((len = self->stack->length) <= 0) return stackUnderflow();
3774 last=self->stack->data[len-1];
3775 Py_INCREF(last);
3776 PDATA_PUSH(self->stack, last, -1);
3777 return 0;
3778}
3779
3780
3781static int
Tim Peterscba30e22003-02-01 06:24:36 +00003782load_get(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003783{
3784 PyObject *py_str = 0, *value = 0;
3785 int len;
3786 char *s;
3787 int rc;
3788
Tim Peters0bc93f52003-02-02 18:29:33 +00003789 if ((len = self->readline_func(self, &s)) < 0) return -1;
Guido van Rossumf9ffb031999-02-04 14:54:04 +00003790 if (len < 2) return bad_readline();
Tim Peters84e87f32001-03-17 04:50:51 +00003791
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003792 if (!( py_str = PyString_FromStringAndSize(s, len - 1))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003793
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003794 value = PyDict_GetItem(self->memo, py_str);
3795 if (! value) {
3796 PyErr_SetObject(BadPickleGet, py_str);
3797 rc = -1;
3798 } else {
3799 PDATA_APPEND(self->stack, value, -1);
3800 rc = 0;
3801 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003802
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003803 Py_DECREF(py_str);
3804 return rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003805}
3806
3807
3808static int
Tim Peterscba30e22003-02-01 06:24:36 +00003809load_binget(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003810{
3811 PyObject *py_key = 0, *value = 0;
3812 unsigned char key;
3813 char *s;
3814 int rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003815
Tim Peters0bc93f52003-02-02 18:29:33 +00003816 if (self->read_func(self, &s, 1) < 0) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003817
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003818 key = (unsigned char)s[0];
3819 if (!( py_key = PyInt_FromLong((long)key))) return -1;
Guido van Rossumea2b7152000-05-09 18:14:50 +00003820
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003821 value = PyDict_GetItem(self->memo, py_key);
3822 if (! value) {
3823 PyErr_SetObject(BadPickleGet, py_key);
3824 rc = -1;
3825 } else {
3826 PDATA_APPEND(self->stack, value, -1);
3827 rc = 0;
3828 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003829
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003830 Py_DECREF(py_key);
3831 return rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003832}
3833
3834
3835static int
Tim Peterscba30e22003-02-01 06:24:36 +00003836load_long_binget(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003837{
3838 PyObject *py_key = 0, *value = 0;
3839 unsigned char c;
3840 char *s;
3841 long key;
3842 int rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003843
Tim Peters0bc93f52003-02-02 18:29:33 +00003844 if (self->read_func(self, &s, 4) < 0) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003845
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003846 c = (unsigned char)s[0];
3847 key = (long)c;
3848 c = (unsigned char)s[1];
3849 key |= (long)c << 8;
3850 c = (unsigned char)s[2];
3851 key |= (long)c << 16;
3852 c = (unsigned char)s[3];
3853 key |= (long)c << 24;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003854
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003855 if (!( py_key = PyInt_FromLong((long)key))) return -1;
3856
3857 value = PyDict_GetItem(self->memo, py_key);
3858 if (! value) {
3859 PyErr_SetObject(BadPickleGet, py_key);
3860 rc = -1;
3861 } else {
3862 PDATA_APPEND(self->stack, value, -1);
3863 rc = 0;
3864 }
3865
3866 Py_DECREF(py_key);
3867 return rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003868}
3869
Tim Peters2d629652003-02-04 05:06:17 +00003870/* Push an object from the extension registry (EXT[124]). nbytes is
3871 * the number of bytes following the opcode, holding the index (code) value.
3872 */
3873static int
3874load_extension(Unpicklerobject *self, int nbytes)
3875{
3876 char *codebytes; /* the nbytes bytes after the opcode */
3877 long code; /* calc_binint returns long */
3878 PyObject *py_code; /* code as a Python int */
3879 PyObject *obj; /* the object to push */
3880 PyObject *pair; /* (module_name, class_name) */
3881 PyObject *module_name, *class_name;
3882
3883 assert(nbytes == 1 || nbytes == 2 || nbytes == 4);
3884 if (self->read_func(self, &codebytes, nbytes) < 0) return -1;
3885 code = calc_binint(codebytes, nbytes);
3886 if (code <= 0) { /* note that 0 is forbidden */
3887 /* Corrupt or hostile pickle. */
3888 PyErr_SetString(UnpicklingError, "EXT specifies code <= 0");
3889 return -1;
3890 }
3891
3892 /* Look for the code in the cache. */
3893 py_code = PyInt_FromLong(code);
3894 if (py_code == NULL) return -1;
3895 obj = PyDict_GetItem(extension_cache, py_code);
3896 if (obj != NULL) {
3897 /* Bingo. */
3898 Py_DECREF(py_code);
3899 PDATA_APPEND(self->stack, obj, -1);
3900 return 0;
3901 }
3902
3903 /* Look up the (module_name, class_name) pair. */
3904 pair = PyDict_GetItem(inverted_registry, py_code);
3905 if (pair == NULL) {
3906 Py_DECREF(py_code);
3907 PyErr_Format(PyExc_ValueError, "unregistered extension "
3908 "code %ld", code);
3909 return -1;
3910 }
3911 /* Since the extension registry is manipulable via Python code,
Tim Petersfa05ce32003-02-04 05:20:32 +00003912 * confirm that pair is really a 2-tuple of strings.
Tim Peters2d629652003-02-04 05:06:17 +00003913 */
3914 if (!PyTuple_Check(pair) || PyTuple_Size(pair) != 2 ||
3915 !PyString_Check(module_name = PyTuple_GET_ITEM(pair, 0)) ||
3916 !PyString_Check(class_name = PyTuple_GET_ITEM(pair, 1))) {
3917 Py_DECREF(py_code);
3918 PyErr_Format(PyExc_ValueError, "_inverted_registry[%ld] "
3919 "isn't a 2-tuple of strings", code);
3920 return -1;
3921 }
3922 /* Load the object. */
3923 obj = find_class(module_name, class_name, self->find_class);
3924 if (obj == NULL) {
3925 Py_DECREF(py_code);
3926 return -1;
3927 }
3928 /* Cache code -> obj. */
3929 code = PyDict_SetItem(extension_cache, py_code, obj);
3930 Py_DECREF(py_code);
3931 if (code < 0) {
3932 Py_DECREF(obj);
3933 return -1;
3934 }
3935 PDATA_PUSH(self->stack, obj, -1);
3936 return 0;
3937}
Guido van Rossum60456fd1997-04-09 17:36:32 +00003938
3939static int
Tim Peterscba30e22003-02-01 06:24:36 +00003940load_put(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003941{
3942 PyObject *py_str = 0, *value = 0;
3943 int len, l;
3944 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003945
Tim Peters0bc93f52003-02-02 18:29:33 +00003946 if ((l = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003947 if (l < 2) return bad_readline();
3948 if (!( len=self->stack->length )) return stackUnderflow();
3949 if (!( py_str = PyString_FromStringAndSize(s, l - 1))) return -1;
3950 value=self->stack->data[len-1];
3951 l=PyDict_SetItem(self->memo, py_str, value);
3952 Py_DECREF(py_str);
3953 return l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003954}
3955
3956
3957static int
Tim Peterscba30e22003-02-01 06:24:36 +00003958load_binput(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003959{
3960 PyObject *py_key = 0, *value = 0;
3961 unsigned char key;
3962 char *s;
3963 int len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003964
Tim Peters0bc93f52003-02-02 18:29:33 +00003965 if (self->read_func(self, &s, 1) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003966 if (!( (len=self->stack->length) > 0 )) return stackUnderflow();
Guido van Rossum60456fd1997-04-09 17:36:32 +00003967
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003968 key = (unsigned char)s[0];
Guido van Rossum053b8df1998-11-25 16:18:00 +00003969
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003970 if (!( py_key = PyInt_FromLong((long)key))) return -1;
3971 value=self->stack->data[len-1];
3972 len=PyDict_SetItem(self->memo, py_key, value);
3973 Py_DECREF(py_key);
3974 return len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003975}
3976
3977
3978static int
Tim Peterscba30e22003-02-01 06:24:36 +00003979load_long_binput(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003980{
3981 PyObject *py_key = 0, *value = 0;
3982 long key;
3983 unsigned char c;
3984 char *s;
3985 int len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003986
Tim Peters0bc93f52003-02-02 18:29:33 +00003987 if (self->read_func(self, &s, 4) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003988 if (!( len=self->stack->length )) return stackUnderflow();
Guido van Rossum60456fd1997-04-09 17:36:32 +00003989
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003990 c = (unsigned char)s[0];
3991 key = (long)c;
3992 c = (unsigned char)s[1];
3993 key |= (long)c << 8;
3994 c = (unsigned char)s[2];
3995 key |= (long)c << 16;
3996 c = (unsigned char)s[3];
3997 key |= (long)c << 24;
Tim Peters84e87f32001-03-17 04:50:51 +00003998
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003999 if (!( py_key = PyInt_FromLong(key))) return -1;
4000 value=self->stack->data[len-1];
4001 len=PyDict_SetItem(self->memo, py_key, value);
4002 Py_DECREF(py_key);
4003 return len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004004}
4005
4006
4007static int
Tim Peterscba30e22003-02-01 06:24:36 +00004008do_append(Unpicklerobject *self, int x)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004009{
4010 PyObject *value = 0, *list = 0, *append_method = 0;
4011 int len, i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004012
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004013 len=self->stack->length;
4014 if (!( len >= x && x > 0 )) return stackUnderflow();
4015 /* nothing to do */
Tim Peterscba30e22003-02-01 06:24:36 +00004016 if (len==x) return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004017
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004018 list=self->stack->data[x-1];
Guido van Rossum60456fd1997-04-09 17:36:32 +00004019
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004020 if (PyList_Check(list)) {
4021 PyObject *slice;
4022 int list_len;
Tim Peters84e87f32001-03-17 04:50:51 +00004023
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004024 slice=Pdata_popList(self->stack, x);
4025 list_len = PyList_GET_SIZE(list);
4026 i=PyList_SetSlice(list, list_len, list_len, slice);
4027 Py_DECREF(slice);
4028 return i;
4029 }
4030 else {
Guido van Rossum60456fd1997-04-09 17:36:32 +00004031
Tim Peterscba30e22003-02-01 06:24:36 +00004032 if (!( append_method = PyObject_GetAttr(list, append_str)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004033 return -1;
4034
4035 for (i = x; i < len; i++) {
4036 PyObject *junk;
4037
4038 value=self->stack->data[i];
4039 junk=0;
4040 ARG_TUP(self, value);
4041 if (self->arg) {
Tim Peterscba30e22003-02-01 06:24:36 +00004042 junk = PyObject_Call(append_method, self->arg,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004043 NULL);
4044 FREE_ARG_TUP(self);
4045 }
4046 if (! junk) {
4047 Pdata_clear(self->stack, i+1);
4048 self->stack->length=x;
4049 Py_DECREF(append_method);
4050 return -1;
4051 }
4052 Py_DECREF(junk);
4053 }
4054 self->stack->length=x;
4055 Py_DECREF(append_method);
4056 }
4057
4058 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004059}
4060
4061
4062static int
Tim Peterscba30e22003-02-01 06:24:36 +00004063load_append(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004064{
4065 return do_append(self, self->stack->length - 1);
Guido van Rossum60456fd1997-04-09 17:36:32 +00004066}
4067
4068
4069static int
Tim Peterscba30e22003-02-01 06:24:36 +00004070load_appends(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004071{
4072 return do_append(self, marker(self));
Guido van Rossum60456fd1997-04-09 17:36:32 +00004073}
4074
4075
4076static int
Tim Peterscba30e22003-02-01 06:24:36 +00004077do_setitems(Unpicklerobject *self, int x)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004078{
4079 PyObject *value = 0, *key = 0, *dict = 0;
4080 int len, i, r=0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004081
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004082 if (!( (len=self->stack->length) >= x
4083 && x > 0 )) return stackUnderflow();
Guido van Rossum60456fd1997-04-09 17:36:32 +00004084
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004085 dict=self->stack->data[x-1];
Guido van Rossum60456fd1997-04-09 17:36:32 +00004086
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004087 for (i = x+1; i < len; i += 2) {
4088 key =self->stack->data[i-1];
4089 value=self->stack->data[i ];
4090 if (PyObject_SetItem(dict, key, value) < 0) {
4091 r=-1;
4092 break;
4093 }
4094 }
4095
4096 Pdata_clear(self->stack, x);
4097
4098 return r;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004099}
4100
4101
Tim Peters84e87f32001-03-17 04:50:51 +00004102static int
Tim Peterscba30e22003-02-01 06:24:36 +00004103load_setitem(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004104{
4105 return do_setitems(self, self->stack->length - 2);
4106}
Guido van Rossum60456fd1997-04-09 17:36:32 +00004107
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004108static int
Tim Peterscba30e22003-02-01 06:24:36 +00004109load_setitems(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004110{
4111 return do_setitems(self, marker(self));
Guido van Rossum60456fd1997-04-09 17:36:32 +00004112}
4113
Tim Peters84e87f32001-03-17 04:50:51 +00004114
Guido van Rossum60456fd1997-04-09 17:36:32 +00004115static int
Tim Peterscba30e22003-02-01 06:24:36 +00004116load_build(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004117{
4118 PyObject *value = 0, *inst = 0, *instdict = 0, *d_key = 0, *d_value = 0,
4119 *junk = 0, *__setstate__ = 0;
4120 int i, r = 0;
4121
4122 if (self->stack->length < 2) return stackUnderflow();
4123 PDATA_POP(self->stack, value);
4124 if (! value) return -1;
4125 inst=self->stack->data[self->stack->length-1];
4126
4127 if ((__setstate__ = PyObject_GetAttr(inst, __setstate___str))) {
4128 ARG_TUP(self, value);
4129 if (self->arg) {
4130 junk = PyObject_Call(__setstate__, self->arg, NULL);
4131 FREE_ARG_TUP(self);
4132 }
4133 Py_DECREF(__setstate__);
4134 if (! junk) return -1;
4135 Py_DECREF(junk);
4136 return 0;
4137 }
4138
4139 PyErr_Clear();
4140 if ((instdict = PyObject_GetAttr(inst, __dict___str))) {
4141 i = 0;
4142 while (PyDict_Next(value, &i, &d_key, &d_value)) {
4143 if (PyObject_SetItem(instdict, d_key, d_value) < 0) {
4144 r=-1;
4145 break;
4146 }
4147 }
4148 Py_DECREF(instdict);
4149 }
4150 else r=-1;
4151
4152 Py_XDECREF(value);
4153
4154 return r;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004155}
4156
4157
4158static int
Tim Peterscba30e22003-02-01 06:24:36 +00004159load_mark(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004160{
4161 int s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004162
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004163 /* Note that we split the (pickle.py) stack into two stacks, an
4164 object stack and a mark stack. Here we push a mark onto the
4165 mark stack.
4166 */
Guido van Rossum60456fd1997-04-09 17:36:32 +00004167
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004168 if ((self->num_marks + 1) >= self->marks_size) {
4169 s=self->marks_size+20;
4170 if (s <= self->num_marks) s=self->num_marks + 1;
4171 if (self->marks == NULL)
4172 self->marks=(int *)malloc(s * sizeof(int));
4173 else
Tim Peterscba30e22003-02-01 06:24:36 +00004174 self->marks=(int *)realloc(self->marks,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004175 s * sizeof(int));
4176 if (! self->marks) {
4177 PyErr_NoMemory();
4178 return -1;
4179 }
4180 self->marks_size = s;
4181 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00004182
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004183 self->marks[self->num_marks++] = self->stack->length;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004184
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004185 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004186}
4187
Guido van Rossum60456fd1997-04-09 17:36:32 +00004188static int
Tim Peterscba30e22003-02-01 06:24:36 +00004189load_reduce(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004190{
4191 PyObject *callable = 0, *arg_tup = 0, *ob = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004192
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004193 PDATA_POP(self->stack, arg_tup);
4194 if (! arg_tup) return -1;
4195 PDATA_POP(self->stack, callable);
4196 if (callable) {
4197 ob = Instance_New(callable, arg_tup);
4198 Py_DECREF(callable);
4199 }
4200 Py_DECREF(arg_tup);
Guido van Rossum60456fd1997-04-09 17:36:32 +00004201
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004202 if (! ob) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004203
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004204 PDATA_PUSH(self->stack, ob, -1);
4205 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004206}
Tim Peters84e87f32001-03-17 04:50:51 +00004207
Tim Peters4190fb82003-02-02 16:09:05 +00004208/* Just raises an error if we don't know the protocol specified. PROTO
4209 * is the first opcode for protocols >= 2.
4210 */
4211static int
4212load_proto(Unpicklerobject *self)
4213{
4214 int i;
4215 char *protobyte;
4216
4217 i = self->read_func(self, &protobyte, 1);
4218 if (i < 0)
4219 return -1;
4220
4221 i = calc_binint(protobyte, 1);
4222 /* No point checking for < 0, since calc_binint returns an unsigned
4223 * int when chewing on 1 byte.
4224 */
4225 assert(i >= 0);
4226 if (i <= CURRENT_PROTOCOL_NUMBER)
4227 return 0;
4228
4229 PyErr_Format(PyExc_ValueError, "unsupported pickle protocol: %d", i);
4230 return -1;
4231}
4232
Guido van Rossum60456fd1997-04-09 17:36:32 +00004233static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00004234load(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004235{
4236 PyObject *err = 0, *val = 0;
4237 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004238
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004239 self->num_marks = 0;
4240 if (self->stack->length) Pdata_clear(self->stack, 0);
Guido van Rossum60456fd1997-04-09 17:36:32 +00004241
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004242 while (1) {
Tim Peters0bc93f52003-02-02 18:29:33 +00004243 if (self->read_func(self, &s, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004244 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004245
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004246 switch (s[0]) {
4247 case NONE:
4248 if (load_none(self) < 0)
4249 break;
4250 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004251
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004252 case BININT:
4253 if (load_binint(self) < 0)
4254 break;
4255 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004256
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004257 case BININT1:
4258 if (load_binint1(self) < 0)
4259 break;
4260 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004261
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004262 case BININT2:
4263 if (load_binint2(self) < 0)
4264 break;
4265 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004266
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004267 case INT:
4268 if (load_int(self) < 0)
4269 break;
4270 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004271
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004272 case LONG:
4273 if (load_long(self) < 0)
4274 break;
4275 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004276
Tim Petersee1a53c2003-02-02 02:57:53 +00004277 case LONG1:
4278 if (load_counted_long(self, 1) < 0)
4279 break;
4280 continue;
4281
4282 case LONG4:
4283 if (load_counted_long(self, 4) < 0)
4284 break;
4285 continue;
4286
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004287 case FLOAT:
4288 if (load_float(self) < 0)
4289 break;
4290 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004291
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004292 case BINFLOAT:
4293 if (load_binfloat(self) < 0)
4294 break;
4295 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004296
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004297 case BINSTRING:
4298 if (load_binstring(self) < 0)
4299 break;
4300 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004301
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004302 case SHORT_BINSTRING:
4303 if (load_short_binstring(self) < 0)
4304 break;
4305 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004306
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004307 case STRING:
4308 if (load_string(self) < 0)
4309 break;
4310 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004311
Martin v. Löwis339d0f72001-08-17 18:39:25 +00004312#ifdef Py_USING_UNICODE
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004313 case UNICODE:
4314 if (load_unicode(self) < 0)
4315 break;
4316 continue;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00004317
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004318 case BINUNICODE:
4319 if (load_binunicode(self) < 0)
4320 break;
4321 continue;
Martin v. Löwis339d0f72001-08-17 18:39:25 +00004322#endif
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00004323
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004324 case EMPTY_TUPLE:
Tim Peters1d63c9f2003-02-02 20:29:39 +00004325 if (load_counted_tuple(self, 0) < 0)
4326 break;
4327 continue;
4328
4329 case TUPLE1:
4330 if (load_counted_tuple(self, 1) < 0)
4331 break;
4332 continue;
4333
4334 case TUPLE2:
4335 if (load_counted_tuple(self, 2) < 0)
4336 break;
4337 continue;
4338
4339 case TUPLE3:
4340 if (load_counted_tuple(self, 3) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004341 break;
4342 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004343
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004344 case TUPLE:
4345 if (load_tuple(self) < 0)
4346 break;
4347 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004348
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004349 case EMPTY_LIST:
4350 if (load_empty_list(self) < 0)
4351 break;
4352 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004353
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004354 case LIST:
4355 if (load_list(self) < 0)
4356 break;
4357 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004358
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004359 case EMPTY_DICT:
4360 if (load_empty_dict(self) < 0)
4361 break;
4362 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004363
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004364 case DICT:
4365 if (load_dict(self) < 0)
4366 break;
4367 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004368
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004369 case OBJ:
4370 if (load_obj(self) < 0)
4371 break;
4372 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004373
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004374 case INST:
4375 if (load_inst(self) < 0)
4376 break;
4377 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004378
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004379 case GLOBAL:
4380 if (load_global(self) < 0)
4381 break;
4382 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004383
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004384 case APPEND:
4385 if (load_append(self) < 0)
4386 break;
4387 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004388
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004389 case APPENDS:
4390 if (load_appends(self) < 0)
4391 break;
4392 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00004393
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004394 case BUILD:
4395 if (load_build(self) < 0)
4396 break;
4397 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00004398
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004399 case DUP:
4400 if (load_dup(self) < 0)
4401 break;
4402 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004403
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004404 case BINGET:
4405 if (load_binget(self) < 0)
4406 break;
4407 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004408
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004409 case LONG_BINGET:
4410 if (load_long_binget(self) < 0)
4411 break;
4412 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00004413
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004414 case GET:
4415 if (load_get(self) < 0)
4416 break;
4417 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004418
Tim Peters2d629652003-02-04 05:06:17 +00004419 case EXT1:
4420 if (load_extension(self, 1) < 0)
4421 break;
4422 continue;
4423
4424 case EXT2:
4425 if (load_extension(self, 2) < 0)
4426 break;
4427 continue;
4428
4429 case EXT4:
4430 if (load_extension(self, 4) < 0)
4431 break;
4432 continue;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004433 case MARK:
4434 if (load_mark(self) < 0)
4435 break;
4436 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004437
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004438 case BINPUT:
4439 if (load_binput(self) < 0)
4440 break;
4441 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004442
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004443 case LONG_BINPUT:
4444 if (load_long_binput(self) < 0)
4445 break;
4446 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00004447
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004448 case PUT:
4449 if (load_put(self) < 0)
4450 break;
4451 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004452
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004453 case POP:
4454 if (load_pop(self) < 0)
4455 break;
4456 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004457
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004458 case POP_MARK:
4459 if (load_pop_mark(self) < 0)
4460 break;
4461 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004462
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004463 case SETITEM:
4464 if (load_setitem(self) < 0)
4465 break;
4466 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004467
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004468 case SETITEMS:
4469 if (load_setitems(self) < 0)
4470 break;
4471 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004472
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004473 case STOP:
4474 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004475
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004476 case PERSID:
4477 if (load_persid(self) < 0)
4478 break;
4479 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004480
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004481 case BINPERSID:
4482 if (load_binpersid(self) < 0)
4483 break;
4484 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004485
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004486 case REDUCE:
4487 if (load_reduce(self) < 0)
4488 break;
4489 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004490
Tim Peters4190fb82003-02-02 16:09:05 +00004491 case PROTO:
4492 if (load_proto(self) < 0)
4493 break;
4494 continue;
4495
Tim Peters3c67d792003-02-02 17:59:11 +00004496 case NEWTRUE:
4497 if (load_bool(self, Py_True) < 0)
4498 break;
4499 continue;
4500
4501 case NEWFALSE:
4502 if (load_bool(self, Py_False) < 0)
4503 break;
4504 continue;
4505
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004506 case '\0':
4507 /* end of file */
4508 PyErr_SetNone(PyExc_EOFError);
4509 break;
Tim Peterscba30e22003-02-01 06:24:36 +00004510
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004511 default:
Tim Peterscba30e22003-02-01 06:24:36 +00004512 cPickle_ErrFormat(UnpicklingError,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004513 "invalid load key, '%s'.",
4514 "c", s[0]);
4515 return NULL;
4516 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00004517
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004518 break;
4519 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00004520
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004521 if ((err = PyErr_Occurred())) {
4522 if (err == PyExc_EOFError) {
4523 PyErr_SetNone(PyExc_EOFError);
4524 }
4525 return NULL;
4526 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00004527
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004528 PDATA_POP(self->stack, val);
4529 return val;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004530}
Tim Peters84e87f32001-03-17 04:50:51 +00004531
Guido van Rossum60456fd1997-04-09 17:36:32 +00004532
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004533/* No-load functions to support noload, which is used to
4534 find persistent references. */
4535
4536static int
Tim Peterscba30e22003-02-01 06:24:36 +00004537noload_obj(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004538{
4539 int i;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004540
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004541 if ((i = marker(self)) < 0) return -1;
4542 return Pdata_clear(self->stack, i+1);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004543}
4544
4545
4546static int
Tim Peterscba30e22003-02-01 06:24:36 +00004547noload_inst(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004548{
4549 int i;
4550 char *s;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004551
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004552 if ((i = marker(self)) < 0) return -1;
4553 Pdata_clear(self->stack, i);
Tim Peters0bc93f52003-02-02 18:29:33 +00004554 if (self->readline_func(self, &s) < 0) return -1;
4555 if (self->readline_func(self, &s) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004556 PDATA_APPEND(self->stack, Py_None,-1);
4557 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004558}
4559
4560static int
Tim Peterscba30e22003-02-01 06:24:36 +00004561noload_global(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004562{
4563 char *s;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004564
Tim Peters0bc93f52003-02-02 18:29:33 +00004565 if (self->readline_func(self, &s) < 0) return -1;
4566 if (self->readline_func(self, &s) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004567 PDATA_APPEND(self->stack, Py_None,-1);
4568 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004569}
4570
4571static int
Tim Peterscba30e22003-02-01 06:24:36 +00004572noload_reduce(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004573{
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004574
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004575 if (self->stack->length < 2) return stackUnderflow();
4576 Pdata_clear(self->stack, self->stack->length-2);
4577 PDATA_APPEND(self->stack, Py_None,-1);
4578 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004579}
4580
4581static int
4582noload_build(Unpicklerobject *self) {
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004583
Guido van Rossum053b8df1998-11-25 16:18:00 +00004584 if (self->stack->length < 1) return stackUnderflow();
4585 Pdata_clear(self->stack, self->stack->length-1);
Guido van Rossum50f385c1998-12-04 18:48:44 +00004586 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004587}
4588
Tim Peters2d629652003-02-04 05:06:17 +00004589static int
4590noload_extension(Unpicklerobject *self, int nbytes)
4591{
4592 char *codebytes;
4593
4594 assert(nbytes == 1 || nbytes == 2 || nbytes == 4);
4595 if (self->read_func(self, &codebytes, nbytes) < 0) return -1;
4596 PDATA_APPEND(self->stack, Py_None, -1);
4597 return 0;
4598}
4599
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004600
4601static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00004602noload(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004603{
4604 PyObject *err = 0, *val = 0;
4605 char *s;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004606
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004607 self->num_marks = 0;
4608 Pdata_clear(self->stack, 0);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004609
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004610 while (1) {
Tim Peters0bc93f52003-02-02 18:29:33 +00004611 if (self->read_func(self, &s, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004612 break;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004613
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004614 switch (s[0]) {
4615 case NONE:
4616 if (load_none(self) < 0)
4617 break;
4618 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004619
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004620 case BININT:
4621 if (load_binint(self) < 0)
4622 break;
4623 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004624
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004625 case BININT1:
4626 if (load_binint1(self) < 0)
4627 break;
4628 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004629
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004630 case BININT2:
4631 if (load_binint2(self) < 0)
4632 break;
4633 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004634
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004635 case INT:
4636 if (load_int(self) < 0)
4637 break;
4638 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004639
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004640 case LONG:
4641 if (load_long(self) < 0)
4642 break;
4643 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004644
Tim Peters4190fb82003-02-02 16:09:05 +00004645 case LONG1:
4646 if (load_counted_long(self, 1) < 0)
4647 break;
4648 continue;
4649
4650 case LONG4:
4651 if (load_counted_long(self, 4) < 0)
4652 break;
4653 continue;
4654
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004655 case FLOAT:
4656 if (load_float(self) < 0)
4657 break;
4658 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004659
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004660 case BINFLOAT:
4661 if (load_binfloat(self) < 0)
4662 break;
4663 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004664
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004665 case BINSTRING:
4666 if (load_binstring(self) < 0)
4667 break;
4668 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004669
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004670 case SHORT_BINSTRING:
4671 if (load_short_binstring(self) < 0)
4672 break;
4673 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004674
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004675 case STRING:
4676 if (load_string(self) < 0)
4677 break;
4678 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004679
Martin v. Löwis339d0f72001-08-17 18:39:25 +00004680#ifdef Py_USING_UNICODE
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004681 case UNICODE:
4682 if (load_unicode(self) < 0)
4683 break;
4684 continue;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00004685
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004686 case BINUNICODE:
4687 if (load_binunicode(self) < 0)
4688 break;
4689 continue;
Martin v. Löwis339d0f72001-08-17 18:39:25 +00004690#endif
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00004691
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004692 case EMPTY_TUPLE:
Tim Peters1d63c9f2003-02-02 20:29:39 +00004693 if (load_counted_tuple(self, 0) < 0)
4694 break;
4695 continue;
4696
4697 case TUPLE1:
4698 if (load_counted_tuple(self, 1) < 0)
4699 break;
4700 continue;
4701
4702 case TUPLE2:
4703 if (load_counted_tuple(self, 2) < 0)
4704 break;
4705 continue;
4706
4707 case TUPLE3:
4708 if (load_counted_tuple(self, 3) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004709 break;
4710 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004711
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004712 case TUPLE:
4713 if (load_tuple(self) < 0)
4714 break;
4715 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004716
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004717 case EMPTY_LIST:
4718 if (load_empty_list(self) < 0)
4719 break;
4720 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004721
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004722 case LIST:
4723 if (load_list(self) < 0)
4724 break;
4725 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004726
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004727 case EMPTY_DICT:
4728 if (load_empty_dict(self) < 0)
4729 break;
4730 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004731
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004732 case DICT:
4733 if (load_dict(self) < 0)
4734 break;
4735 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004736
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004737 case OBJ:
4738 if (noload_obj(self) < 0)
4739 break;
4740 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004741
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004742 case INST:
4743 if (noload_inst(self) < 0)
4744 break;
4745 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004746
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004747 case GLOBAL:
4748 if (noload_global(self) < 0)
4749 break;
4750 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004751
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004752 case APPEND:
4753 if (load_append(self) < 0)
4754 break;
4755 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004756
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004757 case APPENDS:
4758 if (load_appends(self) < 0)
4759 break;
4760 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00004761
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004762 case BUILD:
4763 if (noload_build(self) < 0)
4764 break;
4765 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00004766
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004767 case DUP:
4768 if (load_dup(self) < 0)
4769 break;
4770 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004771
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004772 case BINGET:
4773 if (load_binget(self) < 0)
4774 break;
4775 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004776
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004777 case LONG_BINGET:
4778 if (load_long_binget(self) < 0)
4779 break;
4780 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00004781
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004782 case GET:
4783 if (load_get(self) < 0)
4784 break;
4785 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004786
Tim Peters2d629652003-02-04 05:06:17 +00004787 case EXT1:
4788 if (noload_extension(self, 1) < 0)
4789 break;
4790 continue;
4791
4792 case EXT2:
4793 if (noload_extension(self, 2) < 0)
4794 break;
4795 continue;
4796
4797 case EXT4:
4798 if (noload_extension(self, 4) < 0)
4799 break;
4800 continue;
4801
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004802 case MARK:
4803 if (load_mark(self) < 0)
4804 break;
4805 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004806
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004807 case BINPUT:
4808 if (load_binput(self) < 0)
4809 break;
4810 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004811
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004812 case LONG_BINPUT:
4813 if (load_long_binput(self) < 0)
4814 break;
4815 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00004816
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004817 case PUT:
4818 if (load_put(self) < 0)
4819 break;
4820 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004821
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004822 case POP:
4823 if (load_pop(self) < 0)
4824 break;
4825 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004826
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004827 case POP_MARK:
4828 if (load_pop_mark(self) < 0)
4829 break;
4830 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004831
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004832 case SETITEM:
4833 if (load_setitem(self) < 0)
4834 break;
4835 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004836
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004837 case SETITEMS:
4838 if (load_setitems(self) < 0)
4839 break;
4840 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004841
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004842 case STOP:
4843 break;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004844
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004845 case PERSID:
4846 if (load_persid(self) < 0)
4847 break;
4848 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004849
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004850 case BINPERSID:
4851 if (load_binpersid(self) < 0)
4852 break;
4853 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004854
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004855 case REDUCE:
4856 if (noload_reduce(self) < 0)
4857 break;
4858 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004859
Tim Peters4190fb82003-02-02 16:09:05 +00004860 case PROTO:
4861 if (load_proto(self) < 0)
4862 break;
4863 continue;
4864
Tim Peters3c67d792003-02-02 17:59:11 +00004865 case NEWTRUE:
4866 if (load_bool(self, Py_True) < 0)
4867 break;
4868 continue;
4869
4870 case NEWFALSE:
4871 if (load_bool(self, Py_False) < 0)
4872 break;
4873 continue;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004874 default:
Tim Peterscba30e22003-02-01 06:24:36 +00004875 cPickle_ErrFormat(UnpicklingError,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004876 "invalid load key, '%s'.",
4877 "c", s[0]);
4878 return NULL;
4879 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004880
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004881 break;
4882 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004883
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004884 if ((err = PyErr_Occurred())) {
4885 if (err == PyExc_EOFError) {
4886 PyErr_SetNone(PyExc_EOFError);
4887 }
4888 return NULL;
4889 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004890
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004891 PDATA_POP(self->stack, val);
4892 return val;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004893}
Tim Peters84e87f32001-03-17 04:50:51 +00004894
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004895
Guido van Rossum60456fd1997-04-09 17:36:32 +00004896static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00004897Unpickler_load(Unpicklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004898{
Tim Peterscba30e22003-02-01 06:24:36 +00004899 if (!( PyArg_ParseTuple(args, ":load")))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004900 return NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004901
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004902 return load(self);
Guido van Rossum60456fd1997-04-09 17:36:32 +00004903}
4904
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004905static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00004906Unpickler_noload(Unpicklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004907{
Tim Peterscba30e22003-02-01 06:24:36 +00004908 if (!( PyArg_ParseTuple(args, ":noload")))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004909 return NULL;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004910
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004911 return noload(self);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004912}
4913
Guido van Rossum60456fd1997-04-09 17:36:32 +00004914
4915static struct PyMethodDef Unpickler_methods[] = {
Neal Norwitzb0493252002-03-31 14:44:22 +00004916 {"load", (PyCFunction)Unpickler_load, METH_VARARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00004917 PyDoc_STR("load() -- Load a pickle")
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004918 },
Neal Norwitzb0493252002-03-31 14:44:22 +00004919 {"noload", (PyCFunction)Unpickler_noload, METH_VARARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00004920 PyDoc_STR(
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004921 "noload() -- not load a pickle, but go through most of the motions\n"
4922 "\n"
4923 "This function can be used to read past a pickle without instantiating\n"
4924 "any objects or importing any modules. It can also be used to find all\n"
4925 "persistent references without instantiating any objects or importing\n"
Neal Norwitz200788c2002-08-13 22:20:41 +00004926 "any modules.\n")
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004927 },
Guido van Rossum60456fd1997-04-09 17:36:32 +00004928 {NULL, NULL} /* sentinel */
4929};
4930
4931
4932static Unpicklerobject *
Tim Peterscba30e22003-02-01 06:24:36 +00004933newUnpicklerobject(PyObject *f)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004934{
4935 Unpicklerobject *self;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004936
Tim Peterscba30e22003-02-01 06:24:36 +00004937 if (!( self = PyObject_New(Unpicklerobject, &Unpicklertype)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004938 return NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004939
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004940 self->file = NULL;
4941 self->arg = NULL;
4942 self->stack = (Pdata*)Pdata_New();
4943 self->pers_func = NULL;
4944 self->last_string = NULL;
4945 self->marks = NULL;
4946 self->num_marks = 0;
4947 self->marks_size = 0;
4948 self->buf_size = 0;
4949 self->read = NULL;
4950 self->readline = NULL;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004951 self->find_class = NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004952
Tim Peterscba30e22003-02-01 06:24:36 +00004953 if (!( self->memo = PyDict_New()))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004954 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004955
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004956 Py_INCREF(f);
4957 self->file = f;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004958
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004959 /* Set read, readline based on type of f */
4960 if (PyFile_Check(f)) {
4961 self->fp = PyFile_AsFile(f);
4962 if (self->fp == NULL) {
Tim Peterscba30e22003-02-01 06:24:36 +00004963 PyErr_SetString(PyExc_ValueError,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004964 "I/O operation on closed file");
4965 goto err;
4966 }
4967 self->read_func = read_file;
4968 self->readline_func = readline_file;
Guido van Rossumc91fcaa1999-03-29 20:00:14 +00004969 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004970 else if (PycStringIO_InputCheck(f)) {
4971 self->fp = NULL;
4972 self->read_func = read_cStringIO;
4973 self->readline_func = readline_cStringIO;
4974 }
4975 else {
Guido van Rossum60456fd1997-04-09 17:36:32 +00004976
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004977 self->fp = NULL;
4978 self->read_func = read_other;
4979 self->readline_func = readline_other;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004980
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004981 if (!( (self->readline = PyObject_GetAttr(f, readline_str)) &&
4982 (self->read = PyObject_GetAttr(f, read_str)))) {
4983 PyErr_Clear();
Tim Peterscba30e22003-02-01 06:24:36 +00004984 PyErr_SetString( PyExc_TypeError,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004985 "argument must have 'read' and "
4986 "'readline' attributes" );
4987 goto err;
4988 }
4989 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00004990
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004991 return self;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004992
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004993 err:
4994 Py_DECREF((PyObject *)self);
4995 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004996}
4997
4998
4999static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00005000get_Unpickler(PyObject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005001{
5002 PyObject *file;
Tim Peters84e87f32001-03-17 04:50:51 +00005003
Tim Peterscba30e22003-02-01 06:24:36 +00005004 if (!( PyArg_ParseTuple(args, "O:Unpickler", &file)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005005 return NULL;
5006 return (PyObject *)newUnpicklerobject(file);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005007}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005008
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005009
Guido van Rossum60456fd1997-04-09 17:36:32 +00005010static void
Tim Peterscba30e22003-02-01 06:24:36 +00005011Unpickler_dealloc(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005012{
5013 Py_XDECREF(self->readline);
5014 Py_XDECREF(self->read);
5015 Py_XDECREF(self->file);
5016 Py_XDECREF(self->memo);
5017 Py_XDECREF(self->stack);
5018 Py_XDECREF(self->pers_func);
5019 Py_XDECREF(self->arg);
5020 Py_XDECREF(self->last_string);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005021
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005022 if (self->marks) {
5023 free(self->marks);
5024 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005025
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005026 if (self->buf_size) {
5027 free(self->buf);
5028 }
Tim Peters84e87f32001-03-17 04:50:51 +00005029
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005030 PyObject_Del(self);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005031}
5032
5033
5034static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00005035Unpickler_getattr(Unpicklerobject *self, char *name)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005036{
5037 if (!strcmp(name, "persistent_load")) {
5038 if (!self->pers_func) {
5039 PyErr_SetString(PyExc_AttributeError, name);
5040 return NULL;
5041 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005042
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005043 Py_INCREF(self->pers_func);
5044 return self->pers_func;
5045 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005046
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005047 if (!strcmp(name, "find_global")) {
5048 if (!self->find_class) {
5049 PyErr_SetString(PyExc_AttributeError, name);
5050 return NULL;
5051 }
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00005052
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005053 Py_INCREF(self->find_class);
5054 return self->find_class;
5055 }
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00005056
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005057 if (!strcmp(name, "memo")) {
5058 if (!self->memo) {
5059 PyErr_SetString(PyExc_AttributeError, name);
5060 return NULL;
5061 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005062
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005063 Py_INCREF(self->memo);
5064 return self->memo;
5065 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005066
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005067 if (!strcmp(name, "UnpicklingError")) {
5068 Py_INCREF(UnpicklingError);
5069 return UnpicklingError;
5070 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005071
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005072 return Py_FindMethod(Unpickler_methods, (PyObject *)self, name);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005073}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005074
Guido van Rossum60456fd1997-04-09 17:36:32 +00005075
5076static int
Tim Peterscba30e22003-02-01 06:24:36 +00005077Unpickler_setattr(Unpicklerobject *self, char *name, PyObject *value)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005078{
Jeremy Hyltonce616e41998-08-13 23:13:52 +00005079
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005080 if (!strcmp(name, "persistent_load")) {
5081 Py_XDECREF(self->pers_func);
5082 self->pers_func = value;
5083 Py_XINCREF(value);
5084 return 0;
5085 }
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00005086
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005087 if (!strcmp(name, "find_global")) {
5088 Py_XDECREF(self->find_class);
5089 self->find_class = value;
5090 Py_XINCREF(value);
5091 return 0;
5092 }
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00005093
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005094 if (! value) {
5095 PyErr_SetString(PyExc_TypeError,
5096 "attribute deletion is not supported");
5097 return -1;
5098 }
Jeremy Hyltonce616e41998-08-13 23:13:52 +00005099
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005100 if (strcmp(name, "memo") == 0) {
5101 if (!PyDict_Check(value)) {
Tim Peterscba30e22003-02-01 06:24:36 +00005102 PyErr_SetString(PyExc_TypeError,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005103 "memo must be a dictionary");
5104 return -1;
5105 }
5106 Py_XDECREF(self->memo);
5107 self->memo = value;
5108 Py_INCREF(value);
5109 return 0;
5110 }
Jeremy Hyltonce616e41998-08-13 23:13:52 +00005111
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005112 PyErr_SetString(PyExc_AttributeError, name);
5113 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005114}
5115
Tim Peters5bd2a792003-02-01 16:45:06 +00005116/* ---------------------------------------------------------------------------
5117 * Module-level functions.
5118 */
Guido van Rossum60456fd1997-04-09 17:36:32 +00005119
Tim Peters5bd2a792003-02-01 16:45:06 +00005120/* dump(obj, file, proto=0). */
Guido van Rossum60456fd1997-04-09 17:36:32 +00005121static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00005122cpm_dump(PyObject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005123{
5124 PyObject *ob, *file, *res = NULL;
5125 Picklerobject *pickler = 0;
Tim Peters5bd2a792003-02-01 16:45:06 +00005126 int proto = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005127
Tim Peters5bd2a792003-02-01 16:45:06 +00005128 if (!( PyArg_ParseTuple(args, "OO|i", &ob, &file, &proto)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005129 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005130
Tim Peters5bd2a792003-02-01 16:45:06 +00005131 if (!( pickler = newPicklerobject(file, proto)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005132 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005133
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005134 if (dump(pickler, ob) < 0)
5135 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005136
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005137 Py_INCREF(Py_None);
5138 res = Py_None;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005139
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005140 finally:
5141 Py_XDECREF(pickler);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005142
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005143 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005144}
5145
5146
Tim Peters5bd2a792003-02-01 16:45:06 +00005147/* dumps(obj, proto=0). */
Guido van Rossum60456fd1997-04-09 17:36:32 +00005148static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00005149cpm_dumps(PyObject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005150{
5151 PyObject *ob, *file = 0, *res = NULL;
5152 Picklerobject *pickler = 0;
Tim Peters5bd2a792003-02-01 16:45:06 +00005153 int proto = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005154
Tim Peters5bd2a792003-02-01 16:45:06 +00005155 if (!( PyArg_ParseTuple(args, "O|i:dumps", &ob, &proto)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005156 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005157
Tim Peterscba30e22003-02-01 06:24:36 +00005158 if (!( file = PycStringIO->NewOutput(128)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005159 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005160
Tim Peters5bd2a792003-02-01 16:45:06 +00005161 if (!( pickler = newPicklerobject(file, proto)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005162 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005163
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005164 if (dump(pickler, ob) < 0)
5165 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005166
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005167 res = PycStringIO->cgetvalue(file);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005168
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005169 finally:
5170 Py_XDECREF(pickler);
5171 Py_XDECREF(file);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005172
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005173 return res;
Tim Peters84e87f32001-03-17 04:50:51 +00005174}
5175
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005176
Tim Peters5bd2a792003-02-01 16:45:06 +00005177/* load(fileobj). */
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005178static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00005179cpm_load(PyObject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005180{
5181 Unpicklerobject *unpickler = 0;
5182 PyObject *ob, *res = NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005183
Tim Peterscba30e22003-02-01 06:24:36 +00005184 if (!( PyArg_ParseTuple(args, "O:load", &ob)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005185 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005186
Tim Peterscba30e22003-02-01 06:24:36 +00005187 if (!( unpickler = newUnpicklerobject(ob)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005188 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005189
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005190 res = load(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005191
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005192 finally:
5193 Py_XDECREF(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005194
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005195 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005196}
5197
5198
Tim Peters5bd2a792003-02-01 16:45:06 +00005199/* loads(string) */
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005200static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00005201cpm_loads(PyObject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005202{
5203 PyObject *ob, *file = 0, *res = NULL;
5204 Unpicklerobject *unpickler = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005205
Tim Peterscba30e22003-02-01 06:24:36 +00005206 if (!( PyArg_ParseTuple(args, "S:loads", &ob)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005207 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005208
Tim Peterscba30e22003-02-01 06:24:36 +00005209 if (!( file = PycStringIO->NewInput(ob)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005210 goto finally;
Tim Peters84e87f32001-03-17 04:50:51 +00005211
Tim Peterscba30e22003-02-01 06:24:36 +00005212 if (!( unpickler = newUnpicklerobject(file)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005213 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005214
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005215 res = load(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005216
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005217 finally:
5218 Py_XDECREF(file);
5219 Py_XDECREF(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005220
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005221 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005222}
5223
5224
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005225PyDoc_STRVAR(Unpicklertype__doc__,
5226"Objects that know how to unpickle");
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005227
Guido van Rossum9716aaa1997-12-08 15:15:16 +00005228static PyTypeObject Unpicklertype = {
5229 PyObject_HEAD_INIT(NULL)
Guido van Rossum60456fd1997-04-09 17:36:32 +00005230 0, /*ob_size*/
Guido van Rossum14648392001-12-08 18:02:58 +00005231 "cPickle.Unpickler", /*tp_name*/
Guido van Rossum60456fd1997-04-09 17:36:32 +00005232 sizeof(Unpicklerobject), /*tp_basicsize*/
5233 0, /*tp_itemsize*/
5234 /* methods */
5235 (destructor)Unpickler_dealloc, /*tp_dealloc*/
5236 (printfunc)0, /*tp_print*/
5237 (getattrfunc)Unpickler_getattr, /*tp_getattr*/
5238 (setattrfunc)Unpickler_setattr, /*tp_setattr*/
5239 (cmpfunc)0, /*tp_compare*/
5240 (reprfunc)0, /*tp_repr*/
5241 0, /*tp_as_number*/
5242 0, /*tp_as_sequence*/
5243 0, /*tp_as_mapping*/
5244 (hashfunc)0, /*tp_hash*/
5245 (ternaryfunc)0, /*tp_call*/
5246 (reprfunc)0, /*tp_str*/
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005247
Guido van Rossum60456fd1997-04-09 17:36:32 +00005248 /* Space for future expansion */
5249 0L,0L,0L,0L,
5250 Unpicklertype__doc__ /* Documentation string */
Guido van Rossum9716aaa1997-12-08 15:15:16 +00005251};
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005252
Guido van Rossum60456fd1997-04-09 17:36:32 +00005253static struct PyMethodDef cPickle_methods[] = {
Neal Norwitzb0493252002-03-31 14:44:22 +00005254 {"dump", (PyCFunction)cpm_dump, METH_VARARGS,
Tim Peters5bd2a792003-02-01 16:45:06 +00005255 PyDoc_STR("dump(object, file, proto=0) -- "
5256 "Write an object in pickle format to the given file.\n"
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005257 "\n"
Tim Peters5bd2a792003-02-01 16:45:06 +00005258 "See the Pickler docstring for the meaning of optional argument proto.")
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005259 },
Tim Peters5bd2a792003-02-01 16:45:06 +00005260
Neal Norwitzb0493252002-03-31 14:44:22 +00005261 {"dumps", (PyCFunction)cpm_dumps, METH_VARARGS,
Tim Peters5bd2a792003-02-01 16:45:06 +00005262 PyDoc_STR("dumps(object, proto=0) -- "
5263 "Return a string containing an object in pickle format.\n"
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005264 "\n"
Tim Peters5bd2a792003-02-01 16:45:06 +00005265 "See the Pickler docstring for the meaning of optional argument proto.")
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005266 },
Tim Peters5bd2a792003-02-01 16:45:06 +00005267
Neal Norwitzb0493252002-03-31 14:44:22 +00005268 {"load", (PyCFunction)cpm_load, METH_VARARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00005269 PyDoc_STR("load(file) -- Load a pickle from the given file")},
Tim Peters5bd2a792003-02-01 16:45:06 +00005270
Neal Norwitzb0493252002-03-31 14:44:22 +00005271 {"loads", (PyCFunction)cpm_loads, METH_VARARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00005272 PyDoc_STR("loads(string) -- Load a pickle from the given string")},
Tim Peters5bd2a792003-02-01 16:45:06 +00005273
Neal Norwitzb0493252002-03-31 14:44:22 +00005274 {"Pickler", (PyCFunction)get_Pickler, METH_VARARGS,
Tim Peters5bd2a792003-02-01 16:45:06 +00005275 PyDoc_STR("Pickler(file, proto=0) -- Create a pickler.\n"
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005276 "\n"
Tim Peters5bd2a792003-02-01 16:45:06 +00005277 "This takes a file-like object for writing a pickle data stream.\n"
5278 "The optional proto argument tells the pickler to use the given\n"
5279 "protocol; supported protocols are 0, 1, 2. The default\n"
5280 "protocol is 0, to be backwards compatible. (Protocol 0 is the\n"
5281 "only protocol that can be written to a file opened in text\n"
5282 "mode and read back successfully. When using a protocol higher\n"
5283 "than 0, make sure the file is opened in binary mode, both when\n"
5284 "pickling and unpickling.)\n"
5285 "\n"
5286 "Protocol 1 is more efficient than protocol 0; protocol 2 is\n"
5287 "more efficient than protocol 1.\n"
5288 "\n"
5289 "Specifying a negative protocol version selects the highest\n"
5290 "protocol version supported. The higher the protocol used, the\n"
5291 "more recent the version of Python needed to read the pickle\n"
5292 "produced.\n"
5293 "\n"
5294 "The file parameter must have a write() method that accepts a single\n"
5295 "string argument. It can thus be an open file object, a StringIO\n"
5296 "object, or any other custom object that meets this interface.\n")
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005297 },
Tim Peters5bd2a792003-02-01 16:45:06 +00005298
Neal Norwitzb0493252002-03-31 14:44:22 +00005299 {"Unpickler", (PyCFunction)get_Unpickler, METH_VARARGS,
Tim Peters5bd2a792003-02-01 16:45:06 +00005300 PyDoc_STR("Unpickler(file) -- Create an unpickler.")},
5301
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005302 { NULL, NULL }
5303};
5304
Guido van Rossum60456fd1997-04-09 17:36:32 +00005305static int
Tim Peterscba30e22003-02-01 06:24:36 +00005306init_stuff(PyObject *module_dict)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005307{
5308 PyObject *copy_reg, *t, *r;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005309
Martin v. Löwis43c9d8a2002-04-01 12:34:33 +00005310#define INIT_STR(S) if (!( S ## _str=PyString_InternFromString(#S))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005311
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005312 INIT_STR(__class__);
5313 INIT_STR(__getinitargs__);
5314 INIT_STR(__dict__);
5315 INIT_STR(__getstate__);
5316 INIT_STR(__setstate__);
5317 INIT_STR(__name__);
5318 INIT_STR(__main__);
5319 INIT_STR(__reduce__);
5320 INIT_STR(write);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005321 INIT_STR(append);
5322 INIT_STR(read);
5323 INIT_STR(readline);
5324 INIT_STR(copy_reg);
5325 INIT_STR(dispatch_table);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005326 INIT_STR(__basicnew__);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005327
Tim Peterscba30e22003-02-01 06:24:36 +00005328 if (!( copy_reg = PyImport_ImportModule("copy_reg")))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005329 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005330
Tim Peters1f1b2d22003-02-01 02:16:37 +00005331 /* This is special because we want to use a different
5332 one in restricted mode. */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005333 dispatch_table = PyObject_GetAttr(copy_reg, dispatch_table_str);
Tim Peters5b7da392003-02-04 00:21:07 +00005334 if (!dispatch_table) return -1;
5335
5336 extension_registry = PyObject_GetAttrString(copy_reg,
Guido van Rossumd4b920c2003-02-04 01:54:49 +00005337 "_extension_registry");
Tim Peters5b7da392003-02-04 00:21:07 +00005338 if (!extension_registry) return -1;
5339
5340 inverted_registry = PyObject_GetAttrString(copy_reg,
Guido van Rossumd4b920c2003-02-04 01:54:49 +00005341 "_inverted_registry");
Tim Peters5b7da392003-02-04 00:21:07 +00005342 if (!inverted_registry) return -1;
5343
5344 extension_cache = PyObject_GetAttrString(copy_reg,
Guido van Rossumd4b920c2003-02-04 01:54:49 +00005345 "_extension_cache");
Tim Peters5b7da392003-02-04 00:21:07 +00005346 if (!extension_cache) return -1;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005347
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005348 Py_DECREF(copy_reg);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005349
Tim Peters731098b2003-02-04 20:56:09 +00005350 if (!(empty_tuple = PyTuple_New(0)))
5351 return -1;
5352
5353 two_tuple = PyTuple_New(2);
5354 if (two_tuple == NULL)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005355 return -1;
Tim Peters3e667d52003-02-04 21:47:44 +00005356 /* We use this temp container with no regard to refcounts, or to
5357 * keeping containees alive. Exempt from GC, because we don't
5358 * want anything looking at two_tuple() by magic.
5359 */
5360 PyObject_GC_UnTrack(two_tuple);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005361
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005362 /* Ugh */
5363 if (!( t=PyImport_ImportModule("__builtin__"))) return -1;
5364 if (PyDict_SetItemString(module_dict, "__builtins__", t) < 0)
5365 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005366
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005367 if (!( t=PyDict_New())) return -1;
5368 if (!( r=PyRun_String(
5369 "def __init__(self, *args): self.args=args\n\n"
5370 "def __str__(self):\n"
5371 " return self.args and ('%s' % self.args[0]) or '(what)'\n",
5372 Py_file_input,
5373 module_dict, t) )) return -1;
5374 Py_DECREF(r);
Guido van Rossumc03158b1999-06-09 15:23:31 +00005375
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005376 PickleError = PyErr_NewException("cPickle.PickleError", NULL, t);
Tim Peterscba30e22003-02-01 06:24:36 +00005377 if (!PickleError)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005378 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00005379
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005380 Py_DECREF(t);
Guido van Rossumc03158b1999-06-09 15:23:31 +00005381
Tim Peterscba30e22003-02-01 06:24:36 +00005382 PicklingError = PyErr_NewException("cPickle.PicklingError",
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005383 PickleError, NULL);
Tim Peterscba30e22003-02-01 06:24:36 +00005384 if (!PicklingError)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005385 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00005386
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005387 if (!( t=PyDict_New())) return -1;
5388 if (!( r=PyRun_String(
5389 "def __init__(self, *args): self.args=args\n\n"
5390 "def __str__(self):\n"
5391 " a=self.args\n"
5392 " a=a and type(a[0]) or '(what)'\n"
5393 " return 'Cannot pickle %s objects' % a\n"
5394 , Py_file_input,
5395 module_dict, t) )) return -1;
5396 Py_DECREF(r);
Tim Peters84e87f32001-03-17 04:50:51 +00005397
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005398 if (!( UnpickleableError = PyErr_NewException(
Tim Peterscba30e22003-02-01 06:24:36 +00005399 "cPickle.UnpickleableError", PicklingError, t)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005400 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00005401
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005402 Py_DECREF(t);
Guido van Rossumc03158b1999-06-09 15:23:31 +00005403
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005404 if (!( UnpicklingError = PyErr_NewException("cPickle.UnpicklingError",
Tim Peterscba30e22003-02-01 06:24:36 +00005405 PickleError, NULL)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005406 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00005407
Martin v. Löwis658009a2002-09-16 17:26:24 +00005408 if (!( BadPickleGet = PyErr_NewException("cPickle.BadPickleGet",
5409 UnpicklingError, NULL)))
5410 return -1;
Tim Peterscba30e22003-02-01 06:24:36 +00005411
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005412 if (PyDict_SetItemString(module_dict, "PickleError",
5413 PickleError) < 0)
5414 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00005415
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005416 if (PyDict_SetItemString(module_dict, "PicklingError",
5417 PicklingError) < 0)
5418 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00005419
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005420 if (PyDict_SetItemString(module_dict, "UnpicklingError",
5421 UnpicklingError) < 0)
5422 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005423
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005424 if (PyDict_SetItemString(module_dict, "UnpickleableError",
5425 UnpickleableError) < 0)
5426 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005427
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005428 if (PyDict_SetItemString(module_dict, "BadPickleGet",
5429 BadPickleGet) < 0)
5430 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00005431
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005432 PycString_IMPORT;
Guido van Rossum053b8df1998-11-25 16:18:00 +00005433
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005434 return 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005435}
5436
Mark Hammondfe51c6d2002-08-02 02:27:13 +00005437#ifndef PyMODINIT_FUNC /* declarations for DLL import/export */
5438#define PyMODINIT_FUNC void
Guido van Rossumf9ffb031999-02-04 14:54:04 +00005439#endif
Mark Hammondfe51c6d2002-08-02 02:27:13 +00005440PyMODINIT_FUNC
Tim Peterscba30e22003-02-01 06:24:36 +00005441initcPickle(void)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005442{
5443 PyObject *m, *d, *di, *v, *k;
5444 int i;
Tim Peters5b7da392003-02-04 00:21:07 +00005445 char *rev = "1.71"; /* XXX when does this change? */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005446 PyObject *format_version;
5447 PyObject *compatible_formats;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005448
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005449 Picklertype.ob_type = &PyType_Type;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005450 Unpicklertype.ob_type = &PyType_Type;
5451 PdataType.ob_type = &PyType_Type;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005452
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005453 /* Initialize some pieces. We need to do this before module creation,
Tim Peters5b7da392003-02-04 00:21:07 +00005454 * so we're forced to use a temporary dictionary. :(
5455 */
5456 di = PyDict_New();
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005457 if (!di) return;
5458 if (init_stuff(di) < 0) return;
Guido van Rossumebba4202000-09-07 14:35:37 +00005459
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005460 /* Create the module and add the functions */
5461 m = Py_InitModule4("cPickle", cPickle_methods,
5462 cPickle_module_documentation,
5463 (PyObject*)NULL,PYTHON_API_VERSION);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005464
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005465 /* Add some symbolic constants to the module */
5466 d = PyModule_GetDict(m);
Tim Peters5b7da392003-02-04 00:21:07 +00005467 v = PyString_FromString(rev);
5468 PyDict_SetItemString(d, "__version__", v);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005469 Py_XDECREF(v);
Barry Warsaw93d29b61997-01-14 17:45:08 +00005470
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005471 /* Copy data from di. Waaa. */
5472 for (i=0; PyDict_Next(di, &i, &k, &v); ) {
5473 if (PyObject_SetItem(d, k, v) < 0) {
5474 Py_DECREF(di);
5475 return;
5476 }
5477 }
5478 Py_DECREF(di);
Guido van Rossumebba4202000-09-07 14:35:37 +00005479
Tim Peters5b7da392003-02-04 00:21:07 +00005480 /* These are purely informational; no code uses them. */
5481 /* File format version we write. */
5482 format_version = PyString_FromString("2.0");
5483 /* Format versions we can read. */
5484 compatible_formats = Py_BuildValue("[sssss]",
5485 "1.0", /* Original protocol 0 */
5486 "1.1", /* Protocol 0 + INST */
5487 "1.2", /* Original protocol 1 */
5488 "1.3", /* Protocol 1 + BINFLOAT */
Tim Peters0dd23aa2003-02-04 00:30:46 +00005489 "2.0"); /* Original protocol 2 */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005490 PyDict_SetItemString(d, "format_version", format_version);
5491 PyDict_SetItemString(d, "compatible_formats", compatible_formats);
5492 Py_XDECREF(format_version);
5493 Py_XDECREF(compatible_formats);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005494}