blob: f3178abe131f61c7108d35d70db1d4c8c60abc95 [file] [log] [blame]
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001#include "Python.h"
2#include "cStringIO.h"
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003#include "structmember.h"
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005PyDoc_STRVAR(cPickle_module_documentation,
Tim Peters64c04d12003-02-01 06:27:59 +00006"C implementation and optimization of the Python pickle module.");
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007
Guido van Rossum142eeb81997-08-13 03:14:41 +00008#ifndef Py_eval_input
9#include <graminit.h>
10#define Py_eval_input eval_input
Guido van Rossumc6ef2041997-08-21 02:30:45 +000011#endif /* Py_eval_input */
Guido van Rossum142eeb81997-08-13 03:14:41 +000012
Guido van Rossum60456fd1997-04-09 17:36:32 +000013#define DEL_LIST_SLICE(list, from, to) (PyList_SetSlice(list, from, to, NULL))
Guido van Rossum2f4caa41997-01-06 22:59:08 +000014
Guido van Rossum60456fd1997-04-09 17:36:32 +000015#define WRITE_BUF_SIZE 256
16
Tim Peters5bd2a792003-02-01 16:45:06 +000017/* Bump this when new opcodes are added to the pickle protocol. */
Tim Peters8587b3c2003-02-13 15:44:41 +000018#define HIGHEST_PROTOCOL 2
Tim Peters5bd2a792003-02-01 16:45:06 +000019
Tim Peters797ec242003-02-01 06:22:36 +000020/*
21 * Pickle opcodes. These must be kept in synch with pickle.py. Extensive
22 * docs are in pickletools.py.
23 */
Guido van Rossum60456fd1997-04-09 17:36:32 +000024#define MARK '('
25#define STOP '.'
26#define POP '0'
27#define POP_MARK '1'
28#define DUP '2'
29#define FLOAT 'F'
Guido van Rossum60456fd1997-04-09 17:36:32 +000030#define BINFLOAT 'G'
Guido van Rossum60456fd1997-04-09 17:36:32 +000031#define INT 'I'
32#define BININT 'J'
33#define BININT1 'K'
34#define LONG 'L'
35#define BININT2 'M'
36#define NONE 'N'
37#define PERSID 'P'
38#define BINPERSID 'Q'
39#define REDUCE 'R'
40#define STRING 'S'
41#define BINSTRING 'T'
Guido van Rossum2f4caa41997-01-06 22:59:08 +000042#define SHORT_BINSTRING 'U'
Guido van Rossum5fccb7c2000-03-10 23:11:40 +000043#define UNICODE 'V'
44#define BINUNICODE 'X'
Guido van Rossum60456fd1997-04-09 17:36:32 +000045#define APPEND 'a'
46#define BUILD 'b'
47#define GLOBAL 'c'
48#define DICT 'd'
49#define EMPTY_DICT '}'
50#define APPENDS 'e'
51#define GET 'g'
52#define BINGET 'h'
53#define INST 'i'
54#define LONG_BINGET 'j'
55#define LIST 'l'
56#define EMPTY_LIST ']'
57#define OBJ 'o'
58#define PUT 'p'
59#define BINPUT 'q'
60#define LONG_BINPUT 'r'
61#define SETITEM 's'
62#define TUPLE 't'
63#define EMPTY_TUPLE ')'
64#define SETITEMS 'u'
Tim Peters797ec242003-02-01 06:22:36 +000065
66/* Protocol 2. */
67#define PROTO '\x80' /* identify pickle protocol */
68#define NEWOBJ '\x81' /* build object by applying cls.__new__ to argtuple */
69#define EXT1 '\x82' /* push object from extension registry; 1-byte index */
70#define EXT2 '\x83' /* ditto, but 2-byte index */
71#define EXT4 '\x84' /* ditto, but 4-byte index */
72#define TUPLE1 '\x85' /* build 1-tuple from stack top */
73#define TUPLE2 '\x86' /* build 2-tuple from two topmost stack items */
74#define TUPLE3 '\x87' /* build 3-tuple from three topmost stack items */
75#define NEWTRUE '\x88' /* push True */
76#define NEWFALSE '\x89' /* push False */
77#define LONG1 '\x8a' /* push long from < 256 bytes */
78#define LONG4 '\x8b' /* push really big long */
79
80/* There aren't opcodes -- they're ways to pickle bools before protocol 2,
81 * so that unpicklers written before bools were introduced unpickle them
82 * as ints, but unpicklers after can recognize that bools were intended.
83 * Note that protocol 2 added direct ways to pickle bools.
84 */
Jack Jansen3a967022002-06-26 20:40:42 +000085#undef TRUE
Guido van Rossume2763392002-04-05 19:30:08 +000086#define TRUE "I01\n"
Jack Jansen3a967022002-06-26 20:40:42 +000087#undef FALSE
Guido van Rossume2763392002-04-05 19:30:08 +000088#define FALSE "I00\n"
Guido van Rossum77f6a652002-04-03 22:41:51 +000089
Tim Peters1092d642003-02-11 21:06:20 +000090/* Keep in synch with pickle.Pickler._BATCHSIZE. This is how many elements
Tim Peters42f08ac2003-02-11 22:43:24 +000091 * batch_list/dict() pumps out before doing APPENDS/SETITEMS. Nothing will
92 * break if this gets out of synch with pickle.py, but it's unclear that
93 * would help anything either.
Tim Peters1092d642003-02-11 21:06:20 +000094 */
95#define BATCHSIZE 1000
96
Guido van Rossum60456fd1997-04-09 17:36:32 +000097static char MARKv = MARK;
Guido van Rossum2f4caa41997-01-06 22:59:08 +000098
Guido van Rossumc03158b1999-06-09 15:23:31 +000099static PyObject *PickleError;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000100static PyObject *PicklingError;
Guido van Rossumc03158b1999-06-09 15:23:31 +0000101static PyObject *UnpickleableError;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000102static PyObject *UnpicklingError;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000103static PyObject *BadPickleGet;
104
Tim Peters5b7da392003-02-04 00:21:07 +0000105/* As the name says, an empty tuple. */
Guido van Rossum60456fd1997-04-09 17:36:32 +0000106static PyObject *empty_tuple;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000107
Tim Peters5b7da392003-02-04 00:21:07 +0000108/* copy_reg.dispatch_table, {type_object: pickling_function} */
109static PyObject *dispatch_table;
110
111/* For EXT[124] opcodes. */
Guido van Rossumd4b920c2003-02-04 01:54:49 +0000112/* copy_reg._extension_registry, {(module_name, function_name): code} */
Tim Peters5b7da392003-02-04 00:21:07 +0000113static PyObject *extension_registry;
Guido van Rossumd4b920c2003-02-04 01:54:49 +0000114/* copy_reg._inverted_registry, {code: (module_name, function_name)} */
Tim Peters5b7da392003-02-04 00:21:07 +0000115static PyObject *inverted_registry;
Guido van Rossumd4b920c2003-02-04 01:54:49 +0000116/* copy_reg._extension_cache, {code: object} */
Tim Peters5b7da392003-02-04 00:21:07 +0000117static PyObject *extension_cache;
118
Tim Peters731098b2003-02-04 20:56:09 +0000119/* For looking up name pairs in copy_reg._extension_registry. */
120static PyObject *two_tuple;
121
Guido van Rossum60456fd1997-04-09 17:36:32 +0000122static PyObject *__class___str, *__getinitargs___str, *__dict___str,
123 *__getstate___str, *__setstate___str, *__name___str, *__reduce___str,
Guido van Rossumb289b872003-02-19 01:45:13 +0000124 *__reduce_ex___str,
Tim Peters1f1b2d22003-02-01 02:16:37 +0000125 *write_str, *append_str,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000126 *read_str, *readline_str, *__main___str,
Tim Peters1f1b2d22003-02-01 02:16:37 +0000127 *copy_reg_str, *dispatch_table_str;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000128
Guido van Rossum053b8df1998-11-25 16:18:00 +0000129/*************************************************************************
130 Internal Data type for pickle data. */
131
132typedef struct {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000133 PyObject_HEAD
Tim Peters1d63c9f2003-02-02 20:29:39 +0000134 int length; /* number of initial slots in data currently used */
135 int size; /* number of slots in data allocated */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000136 PyObject **data;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000137} Pdata;
138
Tim Peters84e87f32001-03-17 04:50:51 +0000139static void
Tim Peterscba30e22003-02-01 06:24:36 +0000140Pdata_dealloc(Pdata *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000141{
142 int i;
143 PyObject **p;
Tim Peterscba30e22003-02-01 06:24:36 +0000144
Tim Peters1d63c9f2003-02-02 20:29:39 +0000145 for (i = self->length, p = self->data; --i >= 0; p++) {
146 Py_DECREF(*p);
147 }
148 if (self->data)
149 free(self->data);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000150 PyObject_Del(self);
Guido van Rossum053b8df1998-11-25 16:18:00 +0000151}
152
153static PyTypeObject PdataType = {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000154 PyObject_HEAD_INIT(NULL) 0, "cPickle.Pdata", sizeof(Pdata), 0,
155 (destructor)Pdata_dealloc,
156 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0L,0L,0L,0L, ""
Guido van Rossum053b8df1998-11-25 16:18:00 +0000157};
158
159#define Pdata_Check(O) ((O)->ob_type == &PdataType)
160
161static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +0000162Pdata_New(void)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000163{
164 Pdata *self;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000165
Tim Peters1d63c9f2003-02-02 20:29:39 +0000166 if (!(self = PyObject_New(Pdata, &PdataType)))
167 return NULL;
168 self->size = 8;
169 self->length = 0;
170 self->data = malloc(self->size * sizeof(PyObject*));
171 if (self->data)
172 return (PyObject*)self;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000173 Py_DECREF(self);
174 return PyErr_NoMemory();
Guido van Rossum053b8df1998-11-25 16:18:00 +0000175}
176
Tim Peters84e87f32001-03-17 04:50:51 +0000177static int
Tim Peterscba30e22003-02-01 06:24:36 +0000178stackUnderflow(void)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000179{
180 PyErr_SetString(UnpicklingError, "unpickling stack underflow");
181 return -1;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000182}
183
Tim Peters1d63c9f2003-02-02 20:29:39 +0000184/* Retain only the initial clearto items. If clearto >= the current
185 * number of items, this is a (non-erroneous) NOP.
186 */
Guido van Rossum053b8df1998-11-25 16:18:00 +0000187static int
Tim Peterscba30e22003-02-01 06:24:36 +0000188Pdata_clear(Pdata *self, int clearto)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000189{
190 int i;
191 PyObject **p;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000192
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000193 if (clearto < 0) return stackUnderflow();
194 if (clearto >= self->length) return 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000195
Tim Peters1d63c9f2003-02-02 20:29:39 +0000196 for (i = self->length, p = self->data + clearto;
197 --i >= clearto;
198 p++) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000199 Py_CLEAR(*p);
Tim Peters1d63c9f2003-02-02 20:29:39 +0000200 }
201 self->length = clearto;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000202
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000203 return 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000204}
205
Tim Peters84e87f32001-03-17 04:50:51 +0000206static int
Tim Peterscba30e22003-02-01 06:24:36 +0000207Pdata_grow(Pdata *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000208{
Tim Peters1d63c9f2003-02-02 20:29:39 +0000209 int bigger;
210 size_t nbytes;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000211 PyObject **tmp;
Tim Peters1d63c9f2003-02-02 20:29:39 +0000212
Tim Peters1d63c9f2003-02-02 20:29:39 +0000213 bigger = self->size << 1;
Tim Peterse0a39072003-02-03 15:45:56 +0000214 if (bigger <= 0) /* was 0, or new value overflows */
Tim Peters1d63c9f2003-02-02 20:29:39 +0000215 goto nomemory;
216 if ((int)(size_t)bigger != bigger)
217 goto nomemory;
218 nbytes = (size_t)bigger * sizeof(PyObject *);
219 if (nbytes / sizeof(PyObject *) != (size_t)bigger)
220 goto nomemory;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000221 tmp = realloc(self->data, nbytes);
222 if (tmp == NULL)
Tim Peters1d63c9f2003-02-02 20:29:39 +0000223 goto nomemory;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000224 self->data = tmp;
Tim Peters1d63c9f2003-02-02 20:29:39 +0000225 self->size = bigger;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000226 return 0;
Tim Peters1d63c9f2003-02-02 20:29:39 +0000227
228 nomemory:
Tim Peters1d63c9f2003-02-02 20:29:39 +0000229 PyErr_NoMemory();
230 return -1;
Tim Peters84e87f32001-03-17 04:50:51 +0000231}
Guido van Rossum053b8df1998-11-25 16:18:00 +0000232
Tim Peterse0a39072003-02-03 15:45:56 +0000233/* D is a Pdata*. Pop the topmost element and store it into V, which
234 * must be an lvalue holding PyObject*. On stack underflow, UnpicklingError
Tim Peters1d63c9f2003-02-02 20:29:39 +0000235 * is raised and V is set to NULL. D and V may be evaluated several times.
236 */
237#define PDATA_POP(D, V) { \
Tim Peterse0a39072003-02-03 15:45:56 +0000238 if ((D)->length) \
239 (V) = (D)->data[--((D)->length)]; \
240 else { \
241 PyErr_SetString(UnpicklingError, "bad pickle data"); \
242 (V) = NULL; \
243 } \
Guido van Rossum053b8df1998-11-25 16:18:00 +0000244}
245
Tim Peterse0a39072003-02-03 15:45:56 +0000246/* PDATA_PUSH and PDATA_APPEND both push rvalue PyObject* O on to Pdata*
247 * D. If the Pdata stack can't be grown to hold the new value, both
248 * raise MemoryError and execute "return ER". The difference is in ownership
249 * of O after: _PUSH transfers ownership of O from the caller to the stack
250 * (no incref of O is done, and in case of error O is decrefed), while
251 * _APPEND pushes a new reference.
252 */
253
254/* Push O on stack D, giving ownership of O to the stack. */
255#define PDATA_PUSH(D, O, ER) { \
256 if (((Pdata*)(D))->length == ((Pdata*)(D))->size && \
257 Pdata_grow((Pdata*)(D)) < 0) { \
258 Py_DECREF(O); \
259 return ER; \
260 } \
261 ((Pdata*)(D))->data[((Pdata*)(D))->length++] = (O); \
262}
263
264/* Push O on stack D, pushing a new reference. */
265#define PDATA_APPEND(D, O, ER) { \
266 if (((Pdata*)(D))->length == ((Pdata*)(D))->size && \
267 Pdata_grow((Pdata*)(D)) < 0) \
268 return ER; \
269 Py_INCREF(O); \
270 ((Pdata*)(D))->data[((Pdata*)(D))->length++] = (O); \
271}
272
273
Guido van Rossum053b8df1998-11-25 16:18:00 +0000274static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +0000275Pdata_popTuple(Pdata *self, int start)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000276{
277 PyObject *r;
278 int i, j, l;
Tim Peterscba30e22003-02-01 06:24:36 +0000279
Tim Peters1d63c9f2003-02-02 20:29:39 +0000280 l = self->length-start;
281 r = PyTuple_New(l);
282 if (r == NULL)
283 return NULL;
284 for (i = start, j = 0 ; j < l; i++, j++)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000285 PyTuple_SET_ITEM(r, j, self->data[i]);
Tim Peterscba30e22003-02-01 06:24:36 +0000286
Tim Peters1d63c9f2003-02-02 20:29:39 +0000287 self->length = start;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000288 return r;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000289}
290
291static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +0000292Pdata_popList(Pdata *self, int start)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000293{
294 PyObject *r;
295 int i, j, l;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000296
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000297 l=self->length-start;
298 if (!( r=PyList_New(l))) return NULL;
299 for (i=start, j=0 ; j < l; i++, j++)
300 PyList_SET_ITEM(r, j, self->data[i]);
Guido van Rossum053b8df1998-11-25 16:18:00 +0000301
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000302 self->length=start;
303 return r;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000304}
305
Guido van Rossum053b8df1998-11-25 16:18:00 +0000306/*************************************************************************/
307
308#define ARG_TUP(self, o) { \
309 if (self->arg || (self->arg=PyTuple_New(1))) { \
310 Py_XDECREF(PyTuple_GET_ITEM(self->arg,0)); \
311 PyTuple_SET_ITEM(self->arg,0,o); \
312 } \
313 else { \
314 Py_DECREF(o); \
315 } \
316}
317
318#define FREE_ARG_TUP(self) { \
319 if (self->arg->ob_refcnt > 1) { \
320 Py_DECREF(self->arg); \
321 self->arg=NULL; \
322 } \
323 }
324
Thomas Wouters3b6448f2000-07-22 23:56:07 +0000325typedef struct Picklerobject {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000326 PyObject_HEAD
327 FILE *fp;
328 PyObject *write;
329 PyObject *file;
330 PyObject *memo;
331 PyObject *arg;
332 PyObject *pers_func;
333 PyObject *inst_pers_func;
Tim Peters797ec242003-02-01 06:22:36 +0000334
335 /* pickle protocol number, >= 0 */
336 int proto;
337
338 /* bool, true if proto > 0 */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000339 int bin;
Tim Peters797ec242003-02-01 06:22:36 +0000340
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000341 int fast; /* Fast mode doesn't save in memo, don't use if circ ref */
Martin v. Löwis5a395302002-08-04 08:20:23 +0000342 int nesting;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000343 int (*write_func)(struct Picklerobject *, const char *, Py_ssize_t);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000344 char *write_buf;
345 int buf_size;
346 PyObject *dispatch_table;
347 int fast_container; /* count nested container dumps */
348 PyObject *fast_memo;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000349} Picklerobject;
350
Barry Warsaw52acb492001-12-21 20:04:22 +0000351#ifndef PY_CPICKLE_FAST_LIMIT
352#define PY_CPICKLE_FAST_LIMIT 50
353#endif
Jeremy Hyltona0fb1772001-10-12 04:11:06 +0000354
Jeremy Hylton938ace62002-07-17 16:30:39 +0000355static PyTypeObject Picklertype;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000356
Thomas Wouters3b6448f2000-07-22 23:56:07 +0000357typedef struct Unpicklerobject {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000358 PyObject_HEAD
359 FILE *fp;
360 PyObject *file;
361 PyObject *readline;
362 PyObject *read;
363 PyObject *memo;
364 PyObject *arg;
365 Pdata *stack;
366 PyObject *mark;
367 PyObject *pers_func;
368 PyObject *last_string;
369 int *marks;
370 int num_marks;
371 int marks_size;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000372 Py_ssize_t (*read_func)(struct Unpicklerobject *, char **, Py_ssize_t);
373 Py_ssize_t (*readline_func)(struct Unpicklerobject *, char **);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000374 int buf_size;
375 char *buf;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000376 PyObject *find_class;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000377} Unpicklerobject;
Tim Peters84e87f32001-03-17 04:50:51 +0000378
Jeremy Hylton938ace62002-07-17 16:30:39 +0000379static PyTypeObject Unpicklertype;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000380
Thomas Wouters4789b3a2000-07-24 11:36:47 +0000381/* Forward decls that need the above structs */
382static int save(Picklerobject *, PyObject *, int);
383static int put2(Picklerobject *, PyObject *);
384
Guido van Rossumd385d591997-04-09 17:47:47 +0000385static
Guido van Rossum60456fd1997-04-09 17:36:32 +0000386PyObject *
Thomas Wouters3b6448f2000-07-22 23:56:07 +0000387cPickle_ErrFormat(PyObject *ErrType, char *stringformat, char *format, ...)
388{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000389 va_list va;
390 PyObject *args=0, *retval=0;
391 va_start(va, format);
Tim Peterscba30e22003-02-01 06:24:36 +0000392
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000393 if (format) args = Py_VaBuildValue(format, va);
394 va_end(va);
395 if (format && ! args) return NULL;
Tim Peterscba30e22003-02-01 06:24:36 +0000396 if (stringformat && !(retval=PyString_FromString(stringformat)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000397 return NULL;
Tim Peterscba30e22003-02-01 06:24:36 +0000398
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000399 if (retval) {
400 if (args) {
401 PyObject *v;
402 v=PyString_Format(retval, args);
403 Py_DECREF(retval);
404 Py_DECREF(args);
405 if (! v) return NULL;
406 retval=v;
407 }
408 }
409 else
410 if (args) retval=args;
411 else {
412 PyErr_SetObject(ErrType,Py_None);
413 return NULL;
414 }
415 PyErr_SetObject(ErrType,retval);
416 Py_DECREF(retval);
417 return NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000418}
419
Tim Peters84e87f32001-03-17 04:50:51 +0000420static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000421write_file(Picklerobject *self, const char *s, Py_ssize_t n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000422{
423 size_t nbyteswritten;
Tim Peters84e87f32001-03-17 04:50:51 +0000424
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000425 if (s == NULL) {
426 return 0;
427 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000428
Martin v. Löwis18e16552006-02-15 17:27:45 +0000429 if (n > INT_MAX) {
430 /* String too large */
431 return -1;
432 }
433
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000434 Py_BEGIN_ALLOW_THREADS
Jeremy Hylton7fa4bfa2002-07-18 20:58:57 +0000435 nbyteswritten = fwrite(s, sizeof(char), n, self->fp);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000436 Py_END_ALLOW_THREADS
Jeremy Hylton7fa4bfa2002-07-18 20:58:57 +0000437 if (nbyteswritten != (size_t)n) {
438 PyErr_SetFromErrno(PyExc_IOError);
439 return -1;
440 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000441
Martin v. Löwis18e16552006-02-15 17:27:45 +0000442 return (int)n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000443}
444
Tim Peters84e87f32001-03-17 04:50:51 +0000445static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000446write_cStringIO(Picklerobject *self, const char *s, Py_ssize_t n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000447{
448 if (s == NULL) {
449 return 0;
450 }
Tim Peterscba30e22003-02-01 06:24:36 +0000451
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000452 if (PycStringIO->cwrite((PyObject *)self->file, s, n) != n) {
453 return -1;
454 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000455
Martin v. Löwis18e16552006-02-15 17:27:45 +0000456 return (int)n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000457}
458
Tim Peters84e87f32001-03-17 04:50:51 +0000459static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000460write_none(Picklerobject *self, const char *s, Py_ssize_t n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000461{
462 if (s == NULL) return 0;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000463 if (n > INT_MAX) return -1;
464 return (int)n;
Guido van Rossum142eeb81997-08-13 03:14:41 +0000465}
466
Tim Peters84e87f32001-03-17 04:50:51 +0000467static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000468write_other(Picklerobject *self, const char *s, Py_ssize_t _n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000469{
470 PyObject *py_str = 0, *junk = 0;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000471 int n;
Tim Peterscba30e22003-02-01 06:24:36 +0000472
Martin v. Löwis18e16552006-02-15 17:27:45 +0000473 if (_n > INT_MAX)
474 return -1;
475 n = (int)_n;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000476 if (s == NULL) {
477 if (!( self->buf_size )) return 0;
Tim Peterscba30e22003-02-01 06:24:36 +0000478 py_str = PyString_FromStringAndSize(self->write_buf,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000479 self->buf_size);
Tim Peterscba30e22003-02-01 06:24:36 +0000480 if (!py_str)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000481 return -1;
482 }
483 else {
484 if (self->buf_size && (n + self->buf_size) > WRITE_BUF_SIZE) {
485 if (write_other(self, NULL, 0) < 0)
486 return -1;
487 }
Tim Peterscba30e22003-02-01 06:24:36 +0000488
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000489 if (n > WRITE_BUF_SIZE) {
490 if (!( py_str =
Tim Peterscba30e22003-02-01 06:24:36 +0000491 PyString_FromStringAndSize(s, n)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000492 return -1;
493 }
494 else {
495 memcpy(self->write_buf + self->buf_size, s, n);
496 self->buf_size += n;
497 return n;
498 }
499 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000500
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000501 if (self->write) {
502 /* object with write method */
503 ARG_TUP(self, py_str);
504 if (self->arg) {
505 junk = PyObject_Call(self->write, self->arg, NULL);
506 FREE_ARG_TUP(self);
507 }
508 if (junk) Py_DECREF(junk);
509 else return -1;
510 }
511 else
512 PDATA_PUSH(self->file, py_str, -1);
Guido van Rossum60456fd1997-04-09 17:36:32 +0000513
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000514 self->buf_size = 0;
515 return n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000516}
517
518
Martin v. Löwis18e16552006-02-15 17:27:45 +0000519static Py_ssize_t
520read_file(Unpicklerobject *self, char **s, Py_ssize_t n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000521{
522 size_t nbytesread;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000523
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000524 if (self->buf_size == 0) {
525 int size;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000526
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000527 size = ((n < 32) ? 32 : n);
Tim Petersee1a53c2003-02-02 02:57:53 +0000528 if (!( self->buf = (char *)malloc(size))) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000529 PyErr_NoMemory();
530 return -1;
531 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000532
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000533 self->buf_size = size;
534 }
535 else if (n > self->buf_size) {
Tim Petersee1a53c2003-02-02 02:57:53 +0000536 self->buf = (char *)realloc(self->buf, n);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000537 if (!self->buf) {
538 PyErr_NoMemory();
539 return -1;
540 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000541 self->buf_size = n;
542 }
Tim Peters84e87f32001-03-17 04:50:51 +0000543
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000544 Py_BEGIN_ALLOW_THREADS
545 nbytesread = fread(self->buf, sizeof(char), n, self->fp);
546 Py_END_ALLOW_THREADS
547 if (nbytesread != (size_t)n) {
548 if (feof(self->fp)) {
549 PyErr_SetNone(PyExc_EOFError);
550 return -1;
551 }
Tim Peterscba30e22003-02-01 06:24:36 +0000552
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000553 PyErr_SetFromErrno(PyExc_IOError);
554 return -1;
555 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000556
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000557 *s = self->buf;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000558
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000559 return n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000560}
561
562
Martin v. Löwis18e16552006-02-15 17:27:45 +0000563static Py_ssize_t
Tim Peterscba30e22003-02-01 06:24:36 +0000564readline_file(Unpicklerobject *self, char **s)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000565{
566 int i;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000567
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000568 if (self->buf_size == 0) {
Tim Petersee1a53c2003-02-02 02:57:53 +0000569 if (!( self->buf = (char *)malloc(40))) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000570 PyErr_NoMemory();
571 return -1;
572 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000573 self->buf_size = 40;
574 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000575
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000576 i = 0;
577 while (1) {
Tim Petersee1a53c2003-02-02 02:57:53 +0000578 int bigger;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000579 for (; i < (self->buf_size - 1); i++) {
Tim Peterscba30e22003-02-01 06:24:36 +0000580 if (feof(self->fp) ||
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000581 (self->buf[i] = getc(self->fp)) == '\n') {
582 self->buf[i + 1] = '\0';
583 *s = self->buf;
584 return i + 1;
585 }
586 }
Tim Petersee1a53c2003-02-02 02:57:53 +0000587 bigger = self->buf_size << 1;
588 if (bigger <= 0) { /* overflow */
589 PyErr_NoMemory();
590 return -1;
591 }
592 self->buf = (char *)realloc(self->buf, bigger);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000593 if (!self->buf) {
594 PyErr_NoMemory();
595 return -1;
596 }
Tim Petersee1a53c2003-02-02 02:57:53 +0000597 self->buf_size = bigger;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000598 }
Tim Peters84e87f32001-03-17 04:50:51 +0000599}
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000600
601
Martin v. Löwis18e16552006-02-15 17:27:45 +0000602static Py_ssize_t
603read_cStringIO(Unpicklerobject *self, char **s, Py_ssize_t n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000604{
605 char *ptr;
Tim Peterscba30e22003-02-01 06:24:36 +0000606
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000607 if (PycStringIO->cread((PyObject *)self->file, &ptr, n) != n) {
608 PyErr_SetNone(PyExc_EOFError);
609 return -1;
610 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000611
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000612 *s = ptr;
Tim Peterscba30e22003-02-01 06:24:36 +0000613
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000614 return n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000615}
616
617
Martin v. Löwis18e16552006-02-15 17:27:45 +0000618static Py_ssize_t
Tim Peterscba30e22003-02-01 06:24:36 +0000619readline_cStringIO(Unpicklerobject *self, char **s)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000620{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000621 Py_ssize_t n;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000622 char *ptr;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000623
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000624 if ((n = PycStringIO->creadline((PyObject *)self->file, &ptr)) < 0) {
625 return -1;
626 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000627
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000628 *s = ptr;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000629
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000630 return n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000631}
632
633
Martin v. Löwis18e16552006-02-15 17:27:45 +0000634static Py_ssize_t
635read_other(Unpicklerobject *self, char **s, Py_ssize_t n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000636{
637 PyObject *bytes, *str=0;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000638
Martin v. Löwis18e16552006-02-15 17:27:45 +0000639 if (!( bytes = PyInt_FromSsize_t(n))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000640
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000641 ARG_TUP(self, bytes);
642 if (self->arg) {
643 str = PyObject_Call(self->read, self->arg, NULL);
644 FREE_ARG_TUP(self);
645 }
646 if (! str) return -1;
Tim Peterscba30e22003-02-01 06:24:36 +0000647
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000648 Py_XDECREF(self->last_string);
649 self->last_string = str;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000650
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000651 if (! (*s = PyString_AsString(str))) return -1;
652 return n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000653}
654
655
Martin v. Löwis18e16552006-02-15 17:27:45 +0000656static Py_ssize_t
Tim Peterscba30e22003-02-01 06:24:36 +0000657readline_other(Unpicklerobject *self, char **s)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000658{
659 PyObject *str;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000660 Py_ssize_t str_size;
Tim Peterscba30e22003-02-01 06:24:36 +0000661
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000662 if (!( str = PyObject_CallObject(self->readline, empty_tuple))) {
663 return -1;
664 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000665
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000666 if ((str_size = PyString_Size(str)) < 0)
667 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000668
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000669 Py_XDECREF(self->last_string);
670 self->last_string = str;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000671
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000672 if (! (*s = PyString_AsString(str)))
673 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000674
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000675 return str_size;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000676}
677
Tim Petersee1a53c2003-02-02 02:57:53 +0000678/* Copy the first n bytes from s into newly malloc'ed memory, plus a
679 * trailing 0 byte. Return a pointer to that, or NULL if out of memory.
680 * The caller is responsible for free()'ing the return value.
681 */
Guido van Rossum60456fd1997-04-09 17:36:32 +0000682static char *
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000683pystrndup(const char *s, int n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000684{
Tim Petersee1a53c2003-02-02 02:57:53 +0000685 char *r = (char *)malloc(n+1);
686 if (r == NULL)
687 return (char*)PyErr_NoMemory();
688 memcpy(r, s, n);
689 r[n] = 0;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000690 return r;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000691}
692
693
694static int
Tim Peterscba30e22003-02-01 06:24:36 +0000695get(Picklerobject *self, PyObject *id)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000696{
697 PyObject *value, *mv;
698 long c_value;
699 char s[30];
700 size_t len;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000701
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000702 if (!( mv = PyDict_GetItem(self->memo, id))) {
703 PyErr_SetObject(PyExc_KeyError, id);
704 return -1;
705 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000706
Tim Peterscba30e22003-02-01 06:24:36 +0000707 if (!( value = PyTuple_GetItem(mv, 0)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000708 return -1;
Tim Peters84e87f32001-03-17 04:50:51 +0000709
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000710 if (!( PyInt_Check(value))) {
711 PyErr_SetString(PicklingError, "no int where int expected in memo");
712 return -1;
713 }
714 c_value = PyInt_AS_LONG((PyIntObject*)value);
Guido van Rossum60456fd1997-04-09 17:36:32 +0000715
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000716 if (!self->bin) {
717 s[0] = GET;
718 PyOS_snprintf(s + 1, sizeof(s) - 1, "%ld\n", c_value);
719 len = strlen(s);
720 }
721 else if (Pdata_Check(self->file)) {
722 if (write_other(self, NULL, 0) < 0) return -1;
723 PDATA_APPEND(self->file, mv, -1);
724 return 0;
725 }
726 else {
727 if (c_value < 256) {
728 s[0] = BINGET;
729 s[1] = (int)(c_value & 0xff);
730 len = 2;
731 }
732 else {
733 s[0] = LONG_BINGET;
734 s[1] = (int)(c_value & 0xff);
735 s[2] = (int)((c_value >> 8) & 0xff);
736 s[3] = (int)((c_value >> 16) & 0xff);
737 s[4] = (int)((c_value >> 24) & 0xff);
738 len = 5;
739 }
740 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000741
Tim Peters0bc93f52003-02-02 18:29:33 +0000742 if (self->write_func(self, s, len) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000743 return -1;
Tim Peterscba30e22003-02-01 06:24:36 +0000744
Jeremy Hyltona0fb1772001-10-12 04:11:06 +0000745 return 0;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000746}
Jeremy Hyltonce616e41998-08-13 23:13:52 +0000747
Guido van Rossum60456fd1997-04-09 17:36:32 +0000748
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000749static int
Tim Peterscba30e22003-02-01 06:24:36 +0000750put(Picklerobject *self, PyObject *ob)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000751{
Tim Peterscba30e22003-02-01 06:24:36 +0000752 if (ob->ob_refcnt < 2 || self->fast)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000753 return 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000754
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000755 return put2(self, ob);
756}
Guido van Rossum053b8df1998-11-25 16:18:00 +0000757
Guido van Rossum053b8df1998-11-25 16:18:00 +0000758
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000759static int
Tim Peterscba30e22003-02-01 06:24:36 +0000760put2(Picklerobject *self, PyObject *ob)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000761{
762 char c_str[30];
763 int p;
764 size_t len;
765 int res = -1;
766 PyObject *py_ob_id = 0, *memo_len = 0, *t = 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000767
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000768 if (self->fast)
769 return 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000770
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000771 if ((p = PyDict_Size(self->memo)) < 0)
772 goto finally;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000773
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000774 /* Make sure memo keys are positive! */
Tim Peters70b02d72003-02-02 17:26:40 +0000775 /* XXX Why?
776 * XXX And does "positive" really mean non-negative?
777 * XXX pickle.py starts with PUT index 0, not 1. This makes for
778 * XXX gratuitous differences between the pickling modules.
779 */
Tim Peterscba30e22003-02-01 06:24:36 +0000780 p++;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000781
Tim Peterscba30e22003-02-01 06:24:36 +0000782 if (!( py_ob_id = PyLong_FromVoidPtr(ob)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000783 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000784
Tim Peterscba30e22003-02-01 06:24:36 +0000785 if (!( memo_len = PyInt_FromLong(p)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000786 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000787
Tim Peterscba30e22003-02-01 06:24:36 +0000788 if (!( t = PyTuple_New(2)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000789 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000790
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000791 PyTuple_SET_ITEM(t, 0, memo_len);
792 Py_INCREF(memo_len);
793 PyTuple_SET_ITEM(t, 1, ob);
794 Py_INCREF(ob);
795
796 if (PyDict_SetItem(self->memo, py_ob_id, t) < 0)
797 goto finally;
798
799 if (!self->bin) {
800 c_str[0] = PUT;
801 PyOS_snprintf(c_str + 1, sizeof(c_str) - 1, "%d\n", p);
802 len = strlen(c_str);
803 }
804 else if (Pdata_Check(self->file)) {
805 if (write_other(self, NULL, 0) < 0) return -1;
806 PDATA_APPEND(self->file, memo_len, -1);
807 res=0; /* Job well done ;) */
808 goto finally;
809 }
810 else {
811 if (p >= 256) {
812 c_str[0] = LONG_BINPUT;
813 c_str[1] = (int)(p & 0xff);
814 c_str[2] = (int)((p >> 8) & 0xff);
815 c_str[3] = (int)((p >> 16) & 0xff);
816 c_str[4] = (int)((p >> 24) & 0xff);
817 len = 5;
818 }
819 else {
820 c_str[0] = BINPUT;
821 c_str[1] = p;
822 len = 2;
823 }
824 }
825
Tim Peters0bc93f52003-02-02 18:29:33 +0000826 if (self->write_func(self, c_str, len) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000827 goto finally;
828
829 res = 0;
830
831 finally:
832 Py_XDECREF(py_ob_id);
833 Py_XDECREF(memo_len);
834 Py_XDECREF(t);
835
836 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000837}
838
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000839static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +0000840whichmodule(PyObject *global, PyObject *global_name)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000841{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000842 Py_ssize_t i, j;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000843 PyObject *module = 0, *modules_dict = 0,
844 *global_name_attr = 0, *name = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000845
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000846 module = PyObject_GetAttrString(global, "__module__");
Jeremy Hyltonf00368f2003-06-16 20:19:49 +0000847 if (module)
848 return module;
849 if (PyErr_ExceptionMatches(PyExc_AttributeError))
850 PyErr_Clear();
851 else
852 return NULL;
Guido van Rossum45188231997-09-28 05:38:51 +0000853
Tim Peterscba30e22003-02-01 06:24:36 +0000854 if (!( modules_dict = PySys_GetObject("modules")))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000855 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000856
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000857 i = 0;
858 while ((j = PyDict_Next(modules_dict, &i, &name, &module))) {
Guido van Rossum142eeb81997-08-13 03:14:41 +0000859
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000860 if (PyObject_Compare(name, __main___str)==0) continue;
Tim Peters84e87f32001-03-17 04:50:51 +0000861
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000862 global_name_attr = PyObject_GetAttr(module, global_name);
863 if (!global_name_attr) {
Jeremy Hyltonf00368f2003-06-16 20:19:49 +0000864 if (PyErr_ExceptionMatches(PyExc_AttributeError))
865 PyErr_Clear();
866 else
867 return NULL;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000868 continue;
869 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000870
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000871 if (global_name_attr != global) {
872 Py_DECREF(global_name_attr);
873 continue;
874 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000875
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000876 Py_DECREF(global_name_attr);
Guido van Rossum60456fd1997-04-09 17:36:32 +0000877
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000878 break;
879 }
Guido van Rossum142eeb81997-08-13 03:14:41 +0000880
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000881 /* The following implements the rule in pickle.py added in 1.5
882 that used __main__ if no module is found. I don't actually
883 like this rule. jlf
884 */
885 if (!j) {
886 j=1;
887 name=__main___str;
888 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000889
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000890 Py_INCREF(name);
891 return name;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000892}
893
894
Guido van Rossum60456fd1997-04-09 17:36:32 +0000895static int
Jeremy Hylton499ab6a2001-10-15 21:37:58 +0000896fast_save_enter(Picklerobject *self, PyObject *obj)
897{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000898 /* if fast_container < 0, we're doing an error exit. */
899 if (++self->fast_container >= PY_CPICKLE_FAST_LIMIT) {
900 PyObject *key = NULL;
901 if (self->fast_memo == NULL) {
902 self->fast_memo = PyDict_New();
903 if (self->fast_memo == NULL) {
904 self->fast_container = -1;
905 return 0;
906 }
907 }
908 key = PyLong_FromVoidPtr(obj);
909 if (key == NULL)
910 return 0;
911 if (PyDict_GetItem(self->fast_memo, key)) {
Tim Peters4e52ca82002-12-07 02:43:28 +0000912 Py_DECREF(key);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000913 PyErr_Format(PyExc_ValueError,
Tim Peters92c8bb32003-02-13 23:00:26 +0000914 "fast mode: can't pickle cyclic objects "
915 "including object type %s at %p",
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000916 obj->ob_type->tp_name, obj);
917 self->fast_container = -1;
918 return 0;
919 }
920 if (PyDict_SetItem(self->fast_memo, key, Py_None) < 0) {
Tim Peters4e52ca82002-12-07 02:43:28 +0000921 Py_DECREF(key);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000922 self->fast_container = -1;
923 return 0;
924 }
Tim Peters4e52ca82002-12-07 02:43:28 +0000925 Py_DECREF(key);
Jeremy Hylton499ab6a2001-10-15 21:37:58 +0000926 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000927 return 1;
Jeremy Hylton499ab6a2001-10-15 21:37:58 +0000928}
929
Tim Peterscba30e22003-02-01 06:24:36 +0000930int
Jeremy Hylton499ab6a2001-10-15 21:37:58 +0000931fast_save_leave(Picklerobject *self, PyObject *obj)
932{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000933 if (self->fast_container-- >= PY_CPICKLE_FAST_LIMIT) {
934 PyObject *key = PyLong_FromVoidPtr(obj);
935 if (key == NULL)
936 return 0;
937 if (PyDict_DelItem(self->fast_memo, key) < 0) {
Tim Peters4e52ca82002-12-07 02:43:28 +0000938 Py_DECREF(key);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000939 return 0;
940 }
Tim Peters4e52ca82002-12-07 02:43:28 +0000941 Py_DECREF(key);
Jeremy Hylton499ab6a2001-10-15 21:37:58 +0000942 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000943 return 1;
Jeremy Hylton499ab6a2001-10-15 21:37:58 +0000944}
945
946static int
Tim Peterscba30e22003-02-01 06:24:36 +0000947save_none(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000948{
949 static char none = NONE;
Tim Peters0bc93f52003-02-02 18:29:33 +0000950 if (self->write_func(self, &none, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000951 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000952
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000953 return 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000954}
955
Guido van Rossum77f6a652002-04-03 22:41:51 +0000956static int
Tim Peterscba30e22003-02-01 06:24:36 +0000957save_bool(Picklerobject *self, PyObject *args)
Guido van Rossum77f6a652002-04-03 22:41:51 +0000958{
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000959 static const char *buf[2] = {FALSE, TRUE};
Guido van Rossume2763392002-04-05 19:30:08 +0000960 static char len[2] = {sizeof(FALSE)-1, sizeof(TRUE)-1};
Guido van Rossum77f6a652002-04-03 22:41:51 +0000961 long l = PyInt_AS_LONG((PyIntObject *)args);
962
Tim Peters3c67d792003-02-02 17:59:11 +0000963 if (self->proto >= 2) {
964 char opcode = l ? NEWTRUE : NEWFALSE;
965 if (self->write_func(self, &opcode, 1) < 0)
966 return -1;
967 }
968 else if (self->write_func(self, buf[l], len[l]) < 0)
Guido van Rossum77f6a652002-04-03 22:41:51 +0000969 return -1;
Guido van Rossum77f6a652002-04-03 22:41:51 +0000970 return 0;
971}
Tim Peters84e87f32001-03-17 04:50:51 +0000972
Guido van Rossum60456fd1997-04-09 17:36:32 +0000973static int
Tim Peterscba30e22003-02-01 06:24:36 +0000974save_int(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000975{
976 char c_str[32];
977 long l = PyInt_AS_LONG((PyIntObject *)args);
978 int len = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000979
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000980 if (!self->bin
Guido van Rossumde8d6d71997-05-13 18:00:44 +0000981#if SIZEOF_LONG > 4
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000982 || l > 0x7fffffffL
983 || l < -0x80000000L
Guido van Rossumde8d6d71997-05-13 18:00:44 +0000984#endif
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000985 ) {
986 /* Text-mode pickle, or long too big to fit in the 4-byte
987 * signed BININT format: store as a string.
988 */
989 c_str[0] = INT;
990 PyOS_snprintf(c_str + 1, sizeof(c_str) - 1, "%ld\n", l);
Tim Peters0bc93f52003-02-02 18:29:33 +0000991 if (self->write_func(self, c_str, strlen(c_str)) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000992 return -1;
993 }
994 else {
995 /* Binary pickle and l fits in a signed 4-byte int. */
996 c_str[1] = (int)( l & 0xff);
997 c_str[2] = (int)((l >> 8) & 0xff);
998 c_str[3] = (int)((l >> 16) & 0xff);
999 c_str[4] = (int)((l >> 24) & 0xff);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001000
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001001 if ((c_str[4] == 0) && (c_str[3] == 0)) {
1002 if (c_str[2] == 0) {
1003 c_str[0] = BININT1;
1004 len = 2;
1005 }
1006 else {
1007 c_str[0] = BININT2;
1008 len = 3;
1009 }
1010 }
1011 else {
1012 c_str[0] = BININT;
1013 len = 5;
1014 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001015
Tim Peters0bc93f52003-02-02 18:29:33 +00001016 if (self->write_func(self, c_str, len) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001017 return -1;
1018 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001019
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001020 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001021}
1022
1023
1024static int
Tim Peterscba30e22003-02-01 06:24:36 +00001025save_long(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001026{
Thomas Wouters89f507f2006-12-13 04:49:30 +00001027 Py_ssize_t size;
Tim Petersee1a53c2003-02-02 02:57:53 +00001028 int res = -1;
1029 PyObject *repr = NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001030
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001031 static char l = LONG;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001032
Tim Petersee1a53c2003-02-02 02:57:53 +00001033 if (self->proto >= 2) {
1034 /* Linear-time pickling. */
1035 size_t nbits;
1036 size_t nbytes;
1037 unsigned char *pdata;
1038 char c_str[5];
1039 int i;
1040 int sign = _PyLong_Sign(args);
1041
1042 if (sign == 0) {
1043 /* It's 0 -- an empty bytestring. */
1044 c_str[0] = LONG1;
1045 c_str[1] = 0;
1046 i = self->write_func(self, c_str, 2);
1047 if (i < 0) goto finally;
1048 res = 0;
1049 goto finally;
1050 }
1051 nbits = _PyLong_NumBits(args);
1052 if (nbits == (size_t)-1 && PyErr_Occurred())
1053 goto finally;
1054 /* How many bytes do we need? There are nbits >> 3 full
1055 * bytes of data, and nbits & 7 leftover bits. If there
1056 * are any leftover bits, then we clearly need another
1057 * byte. Wnat's not so obvious is that we *probably*
1058 * need another byte even if there aren't any leftovers:
1059 * the most-significant bit of the most-significant byte
1060 * acts like a sign bit, and it's usually got a sense
1061 * opposite of the one we need. The exception is longs
1062 * of the form -(2**(8*j-1)) for j > 0. Such a long is
1063 * its own 256's-complement, so has the right sign bit
1064 * even without the extra byte. That's a pain to check
1065 * for in advance, though, so we always grab an extra
1066 * byte at the start, and cut it back later if possible.
1067 */
1068 nbytes = (nbits >> 3) + 1;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001069 if (nbytes > INT_MAX) {
Tim Petersee1a53c2003-02-02 02:57:53 +00001070 PyErr_SetString(PyExc_OverflowError, "long too large "
1071 "to pickle");
1072 goto finally;
1073 }
1074 repr = PyString_FromStringAndSize(NULL, (int)nbytes);
1075 if (repr == NULL) goto finally;
1076 pdata = (unsigned char *)PyString_AS_STRING(repr);
1077 i = _PyLong_AsByteArray((PyLongObject *)args,
1078 pdata, nbytes,
1079 1 /* little endian */, 1 /* signed */);
1080 if (i < 0) goto finally;
1081 /* If the long is negative, this may be a byte more than
1082 * needed. This is so iff the MSB is all redundant sign
1083 * bits.
1084 */
1085 if (sign < 0 && nbytes > 1 && pdata[nbytes - 1] == 0xff &&
1086 (pdata[nbytes - 2] & 0x80) != 0)
1087 --nbytes;
1088
1089 if (nbytes < 256) {
1090 c_str[0] = LONG1;
1091 c_str[1] = (char)nbytes;
1092 size = 2;
1093 }
1094 else {
1095 c_str[0] = LONG4;
1096 size = (int)nbytes;
1097 for (i = 1; i < 5; i++) {
1098 c_str[i] = (char)(size & 0xff);
1099 size >>= 8;
1100 }
1101 size = 5;
1102 }
1103 i = self->write_func(self, c_str, size);
1104 if (i < 0) goto finally;
1105 i = self->write_func(self, (char *)pdata, (int)nbytes);
1106 if (i < 0) goto finally;
1107 res = 0;
1108 goto finally;
1109 }
1110
1111 /* proto < 2: write the repr and newline. This is quadratic-time
1112 * (in the number of digits), in both directions.
1113 */
Tim Peterscba30e22003-02-01 06:24:36 +00001114 if (!( repr = PyObject_Repr(args)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001115 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001116
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001117 if ((size = PyString_Size(repr)) < 0)
1118 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001119
Tim Peters0bc93f52003-02-02 18:29:33 +00001120 if (self->write_func(self, &l, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001121 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001122
Tim Peters0bc93f52003-02-02 18:29:33 +00001123 if (self->write_func(self,
1124 PyString_AS_STRING((PyStringObject *)repr),
1125 size) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001126 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001127
Tim Peters0bc93f52003-02-02 18:29:33 +00001128 if (self->write_func(self, "\n", 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001129 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001130
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001131 res = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001132
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001133 finally:
1134 Py_XDECREF(repr);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001135 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001136}
1137
1138
1139static int
Tim Peterscba30e22003-02-01 06:24:36 +00001140save_float(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001141{
1142 double x = PyFloat_AS_DOUBLE((PyFloatObject *)args);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001143
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001144 if (self->bin) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001145 char str[9];
Tim Peters9905b942003-03-20 20:53:32 +00001146 str[0] = BINFLOAT;
1147 if (_PyFloat_Pack8(x, (unsigned char *)&str[1], 0) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001148 return -1;
Tim Peters0bc93f52003-02-02 18:29:33 +00001149 if (self->write_func(self, str, 9) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001150 return -1;
1151 }
1152 else {
1153 char c_str[250];
1154 c_str[0] = FLOAT;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001155 PyOS_ascii_formatd(c_str + 1, sizeof(c_str) - 2, "%.17g", x);
1156 /* Extend the formatted string with a newline character */
1157 strcat(c_str, "\n");
Guido van Rossum60456fd1997-04-09 17:36:32 +00001158
Tim Peters0bc93f52003-02-02 18:29:33 +00001159 if (self->write_func(self, c_str, strlen(c_str)) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001160 return -1;
1161 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001162
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001163 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001164}
1165
1166
1167static int
Tim Peterscba30e22003-02-01 06:24:36 +00001168save_string(Picklerobject *self, PyObject *args, int doput)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001169{
1170 int size, len;
1171 PyObject *repr=0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001172
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001173 if ((size = PyString_Size(args)) < 0)
1174 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001175
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001176 if (!self->bin) {
1177 char *repr_str;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001178
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001179 static char string = STRING;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001180
Tim Peterscba30e22003-02-01 06:24:36 +00001181 if (!( repr = PyObject_Repr(args)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001182 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001183
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001184 if ((len = PyString_Size(repr)) < 0)
1185 goto err;
1186 repr_str = PyString_AS_STRING((PyStringObject *)repr);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001187
Tim Peters0bc93f52003-02-02 18:29:33 +00001188 if (self->write_func(self, &string, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001189 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001190
Tim Peters0bc93f52003-02-02 18:29:33 +00001191 if (self->write_func(self, repr_str, len) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001192 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001193
Tim Peters0bc93f52003-02-02 18:29:33 +00001194 if (self->write_func(self, "\n", 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001195 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001196
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001197 Py_XDECREF(repr);
1198 }
1199 else {
1200 int i;
1201 char c_str[5];
Guido van Rossum60456fd1997-04-09 17:36:32 +00001202
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001203 if ((size = PyString_Size(args)) < 0)
1204 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001205
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001206 if (size < 256) {
1207 c_str[0] = SHORT_BINSTRING;
1208 c_str[1] = size;
1209 len = 2;
1210 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00001211 else if (size <= INT_MAX) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001212 c_str[0] = BINSTRING;
1213 for (i = 1; i < 5; i++)
1214 c_str[i] = (int)(size >> ((i - 1) * 8));
1215 len = 5;
1216 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00001217 else
1218 return -1; /* string too large */
Guido van Rossum60456fd1997-04-09 17:36:32 +00001219
Tim Peters0bc93f52003-02-02 18:29:33 +00001220 if (self->write_func(self, c_str, len) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001221 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001222
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001223 if (size > 128 && Pdata_Check(self->file)) {
1224 if (write_other(self, NULL, 0) < 0) return -1;
1225 PDATA_APPEND(self->file, args, -1);
1226 }
1227 else {
Tim Peters0bc93f52003-02-02 18:29:33 +00001228 if (self->write_func(self,
1229 PyString_AS_STRING(
1230 (PyStringObject *)args),
1231 size) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001232 return -1;
1233 }
1234 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001235
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001236 if (doput)
1237 if (put(self, args) < 0)
1238 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001239
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001240 return 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +00001241
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001242 err:
1243 Py_XDECREF(repr);
1244 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001245}
1246
1247
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001248#ifdef Py_USING_UNICODE
Guido van Rossumfb10c3f2000-12-19 02:08:38 +00001249/* A copy of PyUnicode_EncodeRawUnicodeEscape() that also translates
1250 backslash and newline characters to \uXXXX escapes. */
1251static PyObject *
1252modified_EncodeRawUnicodeEscape(const Py_UNICODE *s, int size)
1253{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001254 PyObject *repr;
1255 char *p;
1256 char *q;
Guido van Rossumfb10c3f2000-12-19 02:08:38 +00001257
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001258 static const char *hexdigit = "0123456789ABCDEF";
Guido van Rossumfb10c3f2000-12-19 02:08:38 +00001259
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001260 repr = PyString_FromStringAndSize(NULL, 6 * size);
1261 if (repr == NULL)
1262 return NULL;
1263 if (size == 0)
1264 return repr;
1265
1266 p = q = PyString_AS_STRING(repr);
1267 while (size-- > 0) {
1268 Py_UNICODE ch = *s++;
1269 /* Map 16-bit characters to '\uxxxx' */
1270 if (ch >= 256 || ch == '\\' || ch == '\n') {
1271 *p++ = '\\';
1272 *p++ = 'u';
1273 *p++ = hexdigit[(ch >> 12) & 0xf];
1274 *p++ = hexdigit[(ch >> 8) & 0xf];
1275 *p++ = hexdigit[(ch >> 4) & 0xf];
1276 *p++ = hexdigit[ch & 15];
1277 }
1278 /* Copy everything else as-is */
1279 else
1280 *p++ = (char) ch;
1281 }
1282 *p = '\0';
Tim Peters5de98422002-04-27 18:44:32 +00001283 _PyString_Resize(&repr, p - q);
Guido van Rossumfb10c3f2000-12-19 02:08:38 +00001284 return repr;
Guido van Rossumfb10c3f2000-12-19 02:08:38 +00001285}
1286
1287
Guido van Rossum60456fd1997-04-09 17:36:32 +00001288static int
Tim Peterscba30e22003-02-01 06:24:36 +00001289save_unicode(Picklerobject *self, PyObject *args, int doput)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001290{
Thomas Wouters89f507f2006-12-13 04:49:30 +00001291 Py_ssize_t size, len;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001292 PyObject *repr=0;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001293
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001294 if (!PyUnicode_Check(args))
1295 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001296
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001297 if (!self->bin) {
1298 char *repr_str;
1299 static char string = UNICODE;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001300
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001301 repr = modified_EncodeRawUnicodeEscape(
1302 PyUnicode_AS_UNICODE(args), PyUnicode_GET_SIZE(args));
Tim Peterscba30e22003-02-01 06:24:36 +00001303 if (!repr)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001304 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001305
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001306 if ((len = PyString_Size(repr)) < 0)
1307 goto err;
1308 repr_str = PyString_AS_STRING((PyStringObject *)repr);
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001309
Tim Peters0bc93f52003-02-02 18:29:33 +00001310 if (self->write_func(self, &string, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001311 goto err;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001312
Tim Peters0bc93f52003-02-02 18:29:33 +00001313 if (self->write_func(self, repr_str, len) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001314 goto err;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001315
Tim Peters0bc93f52003-02-02 18:29:33 +00001316 if (self->write_func(self, "\n", 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001317 goto err;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001318
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001319 Py_XDECREF(repr);
1320 }
1321 else {
1322 int i;
1323 char c_str[5];
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001324
Tim Peterscba30e22003-02-01 06:24:36 +00001325 if (!( repr = PyUnicode_AsUTF8String(args)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001326 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001327
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001328 if ((size = PyString_Size(repr)) < 0)
1329 goto err;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001330 if (size > INT_MAX)
1331 return -1; /* string too large */
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001332
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001333 c_str[0] = BINUNICODE;
1334 for (i = 1; i < 5; i++)
1335 c_str[i] = (int)(size >> ((i - 1) * 8));
1336 len = 5;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001337
Tim Peters0bc93f52003-02-02 18:29:33 +00001338 if (self->write_func(self, c_str, len) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001339 goto err;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001340
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001341 if (size > 128 && Pdata_Check(self->file)) {
1342 if (write_other(self, NULL, 0) < 0)
1343 goto err;
1344 PDATA_APPEND(self->file, repr, -1);
1345 }
1346 else {
Tim Peters0bc93f52003-02-02 18:29:33 +00001347 if (self->write_func(self, PyString_AS_STRING(repr),
1348 size) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001349 goto err;
1350 }
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001351
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001352 Py_DECREF(repr);
1353 }
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001354
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001355 if (doput)
1356 if (put(self, args) < 0)
1357 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001358
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001359 return 0;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001360
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001361 err:
1362 Py_XDECREF(repr);
1363 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001364}
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001365#endif
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001366
Tim Peters1d63c9f2003-02-02 20:29:39 +00001367/* A helper for save_tuple. Push the len elements in tuple t on the stack. */
1368static int
Tim Peters67920142003-02-05 03:46:17 +00001369store_tuple_elements(Picklerobject *self, PyObject *t, int len)
Tim Peters1d63c9f2003-02-02 20:29:39 +00001370{
1371 int i;
1372 int res = -1; /* guilty until proved innocent */
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001373
Tim Peters1d63c9f2003-02-02 20:29:39 +00001374 assert(PyTuple_Size(t) == len);
1375
1376 for (i = 0; i < len; i++) {
1377 PyObject *element = PyTuple_GET_ITEM(t, i);
1378
1379 if (element == NULL)
1380 goto finally;
1381 if (save(self, element, 0) < 0)
1382 goto finally;
1383 }
1384 res = 0;
1385
1386 finally:
1387 return res;
1388}
1389
1390/* Tuples are ubiquitous in the pickle protocols, so many techniques are
1391 * used across protocols to minimize the space needed to pickle them.
Tim Peters67920142003-02-05 03:46:17 +00001392 * Tuples are also the only builtin immutable type that can be recursive
Tim Peters1d63c9f2003-02-02 20:29:39 +00001393 * (a tuple can be reached from itself), and that requires some subtle
1394 * magic so that it works in all cases. IOW, this is a long routine.
1395 */
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001396static int
Tim Peterscba30e22003-02-01 06:24:36 +00001397save_tuple(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001398{
Tim Peters1d63c9f2003-02-02 20:29:39 +00001399 PyObject *py_tuple_id = NULL;
1400 int len, i;
1401 int res = -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001402
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001403 static char tuple = TUPLE;
Tim Peters1d63c9f2003-02-02 20:29:39 +00001404 static char pop = POP;
1405 static char pop_mark = POP_MARK;
1406 static char len2opcode[] = {EMPTY_TUPLE, TUPLE1, TUPLE2, TUPLE3};
Guido van Rossum60456fd1997-04-09 17:36:32 +00001407
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001408 if ((len = PyTuple_Size(args)) < 0)
1409 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001410
Tim Peters1d63c9f2003-02-02 20:29:39 +00001411 if (len == 0) {
1412 char c_str[2];
Tim Peters84e87f32001-03-17 04:50:51 +00001413
Tim Peters1d63c9f2003-02-02 20:29:39 +00001414 if (self->proto) {
1415 c_str[0] = EMPTY_TUPLE;
1416 len = 1;
1417 }
1418 else {
1419 c_str[0] = MARK;
1420 c_str[1] = TUPLE;
1421 len = 2;
1422 }
1423 if (self->write_func(self, c_str, len) >= 0)
1424 res = 0;
1425 /* Don't memoize an empty tuple. */
1426 goto finally;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001427 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001428
Tim Peters1d63c9f2003-02-02 20:29:39 +00001429 /* A non-empty tuple. */
1430
1431 /* id(tuple) isn't in the memo now. If it shows up there after
1432 * saving the tuple elements, the tuple must be recursive, in
1433 * which case we'll pop everything we put on the stack, and fetch
1434 * its value from the memo.
1435 */
1436 py_tuple_id = PyLong_FromVoidPtr(args);
1437 if (py_tuple_id == NULL)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001438 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001439
Tim Peters1d63c9f2003-02-02 20:29:39 +00001440 if (len <= 3 && self->proto >= 2) {
1441 /* Use TUPLE{1,2,3} opcodes. */
Tim Peters67920142003-02-05 03:46:17 +00001442 if (store_tuple_elements(self, args, len) < 0)
Tim Peters1d63c9f2003-02-02 20:29:39 +00001443 goto finally;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001444 if (PyDict_GetItem(self->memo, py_tuple_id)) {
Tim Peters1d63c9f2003-02-02 20:29:39 +00001445 /* pop the len elements */
1446 for (i = 0; i < len; ++i)
1447 if (self->write_func(self, &pop, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001448 goto finally;
Tim Peters1d63c9f2003-02-02 20:29:39 +00001449 /* fetch from memo */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001450 if (get(self, py_tuple_id) < 0)
1451 goto finally;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001452 res = 0;
1453 goto finally;
1454 }
Tim Peters1d63c9f2003-02-02 20:29:39 +00001455 /* Not recursive. */
1456 if (self->write_func(self, len2opcode + len, 1) < 0)
1457 goto finally;
1458 goto memoize;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001459 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001460
Tim Peters1d63c9f2003-02-02 20:29:39 +00001461 /* proto < 2 and len > 0, or proto >= 2 and len > 3.
1462 * Generate MARK elt1 elt2 ... TUPLE
1463 */
1464 if (self->write_func(self, &MARKv, 1) < 0)
1465 goto finally;
1466
Tim Peters67920142003-02-05 03:46:17 +00001467 if (store_tuple_elements(self, args, len) < 0)
Tim Peters1d63c9f2003-02-02 20:29:39 +00001468 goto finally;
1469
1470 if (PyDict_GetItem(self->memo, py_tuple_id)) {
1471 /* pop the stack stuff we pushed */
1472 if (self->bin) {
1473 if (self->write_func(self, &pop_mark, 1) < 0)
1474 goto finally;
1475 }
1476 else {
1477 /* Note that we pop one more than len, to remove
1478 * the MARK too.
1479 */
1480 for (i = 0; i <= len; i++)
1481 if (self->write_func(self, &pop, 1) < 0)
1482 goto finally;
1483 }
1484 /* fetch from memo */
1485 if (get(self, py_tuple_id) >= 0)
1486 res = 0;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001487 goto finally;
1488 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001489
Tim Peters1d63c9f2003-02-02 20:29:39 +00001490 /* Not recursive. */
1491 if (self->write_func(self, &tuple, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001492 goto finally;
Tim Peters84e87f32001-03-17 04:50:51 +00001493
Tim Peters1d63c9f2003-02-02 20:29:39 +00001494 memoize:
1495 if (put(self, args) >= 0)
1496 res = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001497
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001498 finally:
1499 Py_XDECREF(py_tuple_id);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001500 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001501}
1502
Tim Peters1092d642003-02-11 21:06:20 +00001503/* iter is an iterator giving items, and we batch up chunks of
1504 * MARK item item ... item APPENDS
1505 * opcode sequences. Calling code should have arranged to first create an
1506 * empty list, or list-like object, for the APPENDS to operate on.
1507 * Returns 0 on success, <0 on error.
1508 */
1509static int
1510batch_list(Picklerobject *self, PyObject *iter)
1511{
1512 PyObject *obj;
1513 PyObject *slice[BATCHSIZE];
1514 int i, n;
1515
1516 static char append = APPEND;
1517 static char appends = APPENDS;
1518
1519 assert(iter != NULL);
1520
1521 if (self->proto == 0) {
1522 /* APPENDS isn't available; do one at a time. */
1523 for (;;) {
1524 obj = PyIter_Next(iter);
1525 if (obj == NULL) {
1526 if (PyErr_Occurred())
1527 return -1;
1528 break;
1529 }
1530 i = save(self, obj, 0);
1531 Py_DECREF(obj);
1532 if (i < 0)
1533 return -1;
1534 if (self->write_func(self, &append, 1) < 0)
1535 return -1;
Tim Peters1092d642003-02-11 21:06:20 +00001536 }
1537 return 0;
1538 }
1539
1540 /* proto > 0: write in batches of BATCHSIZE. */
1541 do {
1542 /* Get next group of (no more than) BATCHSIZE elements. */
1543 for (n = 0; n < BATCHSIZE; ++n) {
1544 obj = PyIter_Next(iter);
1545 if (obj == NULL) {
1546 if (PyErr_Occurred())
1547 goto BatchFailed;
1548 break;
1549 }
1550 slice[n] = obj;
1551 }
1552
1553 if (n > 1) {
1554 /* Pump out MARK, slice[0:n], APPENDS. */
1555 if (self->write_func(self, &MARKv, 1) < 0)
1556 goto BatchFailed;
1557 for (i = 0; i < n; ++i) {
1558 if (save(self, slice[i], 0) < 0)
1559 goto BatchFailed;
1560 }
1561 if (self->write_func(self, &appends, 1) < 0)
1562 goto BatchFailed;
1563 }
1564 else if (n == 1) {
1565 if (save(self, slice[0], 0) < 0)
1566 goto BatchFailed;
1567 if (self->write_func(self, &append, 1) < 0)
1568 goto BatchFailed;
1569 }
1570
1571 for (i = 0; i < n; ++i) {
1572 Py_DECREF(slice[i]);
1573 }
Tim Peters90975f12003-02-12 05:28:58 +00001574 } while (n == BATCHSIZE);
Tim Peters1092d642003-02-11 21:06:20 +00001575 return 0;
1576
1577BatchFailed:
1578 while (--n >= 0) {
1579 Py_DECREF(slice[n]);
1580 }
1581 return -1;
1582}
1583
Guido van Rossum60456fd1997-04-09 17:36:32 +00001584static int
Tim Peterscba30e22003-02-01 06:24:36 +00001585save_list(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001586{
Tim Peters1092d642003-02-11 21:06:20 +00001587 int res = -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001588 char s[3];
Tim Peters1092d642003-02-11 21:06:20 +00001589 int len;
1590 PyObject *iter;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001591
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001592 if (self->fast && !fast_save_enter(self, args))
1593 goto finally;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00001594
Tim Peters1092d642003-02-11 21:06:20 +00001595 /* Create an empty list. */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001596 if (self->bin) {
1597 s[0] = EMPTY_LIST;
Tim Peters1092d642003-02-11 21:06:20 +00001598 len = 1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001599 }
1600 else {
1601 s[0] = MARK;
1602 s[1] = LIST;
Tim Peters1092d642003-02-11 21:06:20 +00001603 len = 2;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001604 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001605
Tim Peters1092d642003-02-11 21:06:20 +00001606 if (self->write_func(self, s, len) < 0)
1607 goto finally;
1608
1609 /* Get list length, and bow out early if empty. */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001610 if ((len = PyList_Size(args)) < 0)
1611 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001612
Tim Peters1092d642003-02-11 21:06:20 +00001613 /* Memoize. */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001614 if (len == 0) {
Tim Peters90975f12003-02-12 05:28:58 +00001615 if (put(self, args) >= 0)
1616 res = 0;
1617 goto finally;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001618 }
Tim Peters90975f12003-02-12 05:28:58 +00001619 if (put2(self, args) < 0)
1620 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001621
Tim Peters1092d642003-02-11 21:06:20 +00001622 /* Materialize the list elements. */
1623 iter = PyObject_GetIter(args);
1624 if (iter == NULL)
1625 goto finally;
1626 res = batch_list(self, iter);
1627 Py_DECREF(iter);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001628
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001629 finally:
1630 if (self->fast && !fast_save_leave(self, args))
1631 res = -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001632
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001633 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001634}
1635
1636
Tim Peters42f08ac2003-02-11 22:43:24 +00001637/* iter is an iterator giving (key, value) pairs, and we batch up chunks of
1638 * MARK key value ... key value SETITEMS
1639 * opcode sequences. Calling code should have arranged to first create an
1640 * empty dict, or dict-like object, for the SETITEMS to operate on.
1641 * Returns 0 on success, <0 on error.
1642 *
1643 * This is very much like batch_list(). The difference between saving
1644 * elements directly, and picking apart two-tuples, is so long-winded at
1645 * the C level, though, that attempts to combine these routines were too
1646 * ugly to bear.
1647 */
1648static int
1649batch_dict(Picklerobject *self, PyObject *iter)
1650{
1651 PyObject *p;
1652 PyObject *slice[BATCHSIZE];
1653 int i, n;
1654
1655 static char setitem = SETITEM;
1656 static char setitems = SETITEMS;
1657
1658 assert(iter != NULL);
1659
1660 if (self->proto == 0) {
1661 /* SETITEMS isn't available; do one at a time. */
1662 for (;;) {
1663 p = PyIter_Next(iter);
1664 if (p == NULL) {
1665 if (PyErr_Occurred())
1666 return -1;
1667 break;
1668 }
1669 if (!PyTuple_Check(p) || PyTuple_Size(p) != 2) {
1670 PyErr_SetString(PyExc_TypeError, "dict items "
1671 "iterator must return 2-tuples");
1672 return -1;
1673 }
1674 i = save(self, PyTuple_GET_ITEM(p, 0), 0);
1675 if (i >= 0)
1676 i = save(self, PyTuple_GET_ITEM(p, 1), 0);
1677 Py_DECREF(p);
1678 if (i < 0)
1679 return -1;
1680 if (self->write_func(self, &setitem, 1) < 0)
1681 return -1;
Tim Peters42f08ac2003-02-11 22:43:24 +00001682 }
1683 return 0;
1684 }
1685
1686 /* proto > 0: write in batches of BATCHSIZE. */
1687 do {
1688 /* Get next group of (no more than) BATCHSIZE elements. */
1689 for (n = 0; n < BATCHSIZE; ++n) {
1690 p = PyIter_Next(iter);
1691 if (p == NULL) {
1692 if (PyErr_Occurred())
1693 goto BatchFailed;
1694 break;
1695 }
1696 if (!PyTuple_Check(p) || PyTuple_Size(p) != 2) {
1697 PyErr_SetString(PyExc_TypeError, "dict items "
1698 "iterator must return 2-tuples");
1699 goto BatchFailed;
1700 }
1701 slice[n] = p;
1702 }
1703
1704 if (n > 1) {
1705 /* Pump out MARK, slice[0:n], SETITEMS. */
1706 if (self->write_func(self, &MARKv, 1) < 0)
1707 goto BatchFailed;
1708 for (i = 0; i < n; ++i) {
1709 p = slice[i];
1710 if (save(self, PyTuple_GET_ITEM(p, 0), 0) < 0)
1711 goto BatchFailed;
1712 if (save(self, PyTuple_GET_ITEM(p, 1), 0) < 0)
1713 goto BatchFailed;
1714 }
1715 if (self->write_func(self, &setitems, 1) < 0)
1716 goto BatchFailed;
1717 }
1718 else if (n == 1) {
1719 p = slice[0];
1720 if (save(self, PyTuple_GET_ITEM(p, 0), 0) < 0)
1721 goto BatchFailed;
1722 if (save(self, PyTuple_GET_ITEM(p, 1), 0) < 0)
1723 goto BatchFailed;
1724 if (self->write_func(self, &setitem, 1) < 0)
1725 goto BatchFailed;
1726 }
1727
1728 for (i = 0; i < n; ++i) {
1729 Py_DECREF(slice[i]);
1730 }
Tim Peters90975f12003-02-12 05:28:58 +00001731 } while (n == BATCHSIZE);
Tim Peters42f08ac2003-02-11 22:43:24 +00001732 return 0;
1733
1734BatchFailed:
1735 while (--n >= 0) {
1736 Py_DECREF(slice[n]);
1737 }
1738 return -1;
1739}
1740
Guido van Rossum60456fd1997-04-09 17:36:32 +00001741static int
Tim Peterscba30e22003-02-01 06:24:36 +00001742save_dict(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001743{
Tim Peters42f08ac2003-02-11 22:43:24 +00001744 int res = -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001745 char s[3];
Tim Peters42f08ac2003-02-11 22:43:24 +00001746 int len;
1747 PyObject *iter;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001748
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001749 if (self->fast && !fast_save_enter(self, args))
1750 goto finally;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00001751
Tim Peters42f08ac2003-02-11 22:43:24 +00001752 /* Create an empty dict. */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001753 if (self->bin) {
1754 s[0] = EMPTY_DICT;
1755 len = 1;
1756 }
1757 else {
1758 s[0] = MARK;
1759 s[1] = DICT;
1760 len = 2;
1761 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001762
Tim Peters0bc93f52003-02-02 18:29:33 +00001763 if (self->write_func(self, s, len) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001764 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001765
Tim Peters42f08ac2003-02-11 22:43:24 +00001766 /* Get dict size, and bow out early if empty. */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001767 if ((len = PyDict_Size(args)) < 0)
1768 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001769
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001770 if (len == 0) {
Tim Peters90975f12003-02-12 05:28:58 +00001771 if (put(self, args) >= 0)
1772 res = 0;
1773 goto finally;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001774 }
Tim Peters90975f12003-02-12 05:28:58 +00001775 if (put2(self, args) < 0)
1776 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001777
Tim Peters42f08ac2003-02-11 22:43:24 +00001778 /* Materialize the dict items. */
1779 iter = PyObject_CallMethod(args, "iteritems", "()");
1780 if (iter == NULL)
1781 goto finally;
1782 res = batch_dict(self, iter);
1783 Py_DECREF(iter);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001784
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001785 finally:
1786 if (self->fast && !fast_save_leave(self, args))
1787 res = -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001788
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001789 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001790}
1791
1792
Guido van Rossum60456fd1997-04-09 17:36:32 +00001793static int
Tim Peterscba30e22003-02-01 06:24:36 +00001794save_global(Picklerobject *self, PyObject *args, PyObject *name)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001795{
Jeremy Hylton9ee91f12002-07-11 22:02:33 +00001796 PyObject *global_name = 0, *module = 0, *mod = 0, *klass = 0;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001797 char *name_str, *module_str;
1798 int module_size, name_size, res = -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001799
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001800 static char global = GLOBAL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001801
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001802 if (name) {
1803 global_name = name;
1804 Py_INCREF(global_name);
1805 }
1806 else {
Tim Peterscba30e22003-02-01 06:24:36 +00001807 if (!( global_name = PyObject_GetAttr(args, __name___str)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001808 goto finally;
1809 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001810
Tim Peterscba30e22003-02-01 06:24:36 +00001811 if (!( module = whichmodule(args, global_name)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001812 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001813
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001814 if ((module_size = PyString_Size(module)) < 0 ||
1815 (name_size = PyString_Size(global_name)) < 0)
1816 goto finally;
Guido van Rossum053b8df1998-11-25 16:18:00 +00001817
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001818 module_str = PyString_AS_STRING((PyStringObject *)module);
1819 name_str = PyString_AS_STRING((PyStringObject *)global_name);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001820
Guido van Rossum75bfd052002-12-24 18:10:07 +00001821 /* XXX This can be doing a relative import. Clearly it shouldn't,
1822 but I don't know how to stop it. :-( */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001823 mod = PyImport_ImportModule(module_str);
1824 if (mod == NULL) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001825 cPickle_ErrFormat(PicklingError,
Tim Petersb9ce7cd2003-02-18 20:50:45 +00001826 "Can't pickle %s: import of module %s "
1827 "failed",
1828 "OS", args, module);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001829 goto finally;
1830 }
Jeremy Hylton0e1f7a82002-07-11 22:01:40 +00001831 klass = PyObject_GetAttrString(mod, name_str);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001832 if (klass == NULL) {
1833 cPickle_ErrFormat(PicklingError,
Tim Petersb9ce7cd2003-02-18 20:50:45 +00001834 "Can't pickle %s: attribute lookup %s.%s "
1835 "failed",
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001836 "OSS", args, module, global_name);
1837 goto finally;
1838 }
1839 if (klass != args) {
Jeremy Hylton0e1f7a82002-07-11 22:01:40 +00001840 Py_DECREF(klass);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001841 cPickle_ErrFormat(PicklingError,
Tim Peters731098b2003-02-04 20:56:09 +00001842 "Can't pickle %s: it's not the same object "
1843 "as %s.%s",
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001844 "OSS", args, module, global_name);
1845 goto finally;
1846 }
Jeremy Hylton0e1f7a82002-07-11 22:01:40 +00001847 Py_DECREF(klass);
Guido van Rossuma92d16a2001-08-18 21:22:07 +00001848
Tim Peters731098b2003-02-04 20:56:09 +00001849 if (self->proto >= 2) {
1850 /* See whether this is in the extension registry, and if
1851 * so generate an EXT opcode.
1852 */
1853 PyObject *py_code; /* extension code as Python object */
Tim Peters3e667d52003-02-04 21:47:44 +00001854 long code; /* extension code as C value */
Tim Peters731098b2003-02-04 20:56:09 +00001855 char c_str[5];
1856 int n;
1857
1858 PyTuple_SET_ITEM(two_tuple, 0, module);
1859 PyTuple_SET_ITEM(two_tuple, 1, global_name);
1860 py_code = PyDict_GetItem(extension_registry, two_tuple);
1861 if (py_code == NULL)
1862 goto gen_global; /* not registered */
1863
1864 /* Verify py_code has the right type and value. */
1865 if (!PyInt_Check(py_code)) {
1866 cPickle_ErrFormat(PicklingError, "Can't pickle %s: "
Tim Peters6288e232003-02-05 03:53:10 +00001867 "extension code %s isn't an integer",
Tim Peters731098b2003-02-04 20:56:09 +00001868 "OO", args, py_code);
1869 goto finally;
1870 }
1871 code = PyInt_AS_LONG(py_code);
1872 if (code <= 0 || code > 0x7fffffffL) {
1873 cPickle_ErrFormat(PicklingError, "Can't pickle %s: "
1874 "extension code %ld is out of range",
1875 "Ol", args, code);
1876 goto finally;
1877 }
1878
1879 /* Generate an EXT opcode. */
1880 if (code <= 0xff) {
1881 c_str[0] = EXT1;
1882 c_str[1] = (char)code;
1883 n = 2;
1884 }
1885 else if (code <= 0xffff) {
1886 c_str[0] = EXT2;
1887 c_str[1] = (char)(code & 0xff);
1888 c_str[2] = (char)((code >> 8) & 0xff);
1889 n = 3;
1890 }
1891 else {
1892 c_str[0] = EXT4;
1893 c_str[1] = (char)(code & 0xff);
1894 c_str[2] = (char)((code >> 8) & 0xff);
1895 c_str[3] = (char)((code >> 16) & 0xff);
1896 c_str[4] = (char)((code >> 24) & 0xff);
1897 n = 5;
1898 }
1899
1900 if (self->write_func(self, c_str, n) >= 0)
1901 res = 0;
1902 goto finally; /* and don't memoize */
1903 }
1904
1905 gen_global:
Tim Peters0bc93f52003-02-02 18:29:33 +00001906 if (self->write_func(self, &global, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001907 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001908
Tim Peters0bc93f52003-02-02 18:29:33 +00001909 if (self->write_func(self, module_str, module_size) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001910 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001911
Tim Peters0bc93f52003-02-02 18:29:33 +00001912 if (self->write_func(self, "\n", 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001913 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001914
Tim Peters0bc93f52003-02-02 18:29:33 +00001915 if (self->write_func(self, name_str, name_size) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001916 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001917
Tim Peters0bc93f52003-02-02 18:29:33 +00001918 if (self->write_func(self, "\n", 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001919 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001920
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001921 if (put(self, args) < 0)
1922 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001923
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001924 res = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001925
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001926 finally:
1927 Py_XDECREF(module);
1928 Py_XDECREF(global_name);
1929 Py_XDECREF(mod);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001930
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001931 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001932}
1933
Guido van Rossum60456fd1997-04-09 17:36:32 +00001934static int
Tim Peterscba30e22003-02-01 06:24:36 +00001935save_pers(Picklerobject *self, PyObject *args, PyObject *f)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001936{
1937 PyObject *pid = 0;
1938 int size, res = -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001939
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001940 static char persid = PERSID, binpersid = BINPERSID;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001941
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001942 Py_INCREF(args);
1943 ARG_TUP(self, args);
1944 if (self->arg) {
1945 pid = PyObject_Call(f, self->arg, NULL);
1946 FREE_ARG_TUP(self);
1947 }
1948 if (! pid) return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001949
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001950 if (pid != Py_None) {
1951 if (!self->bin) {
1952 if (!PyString_Check(pid)) {
1953 PyErr_SetString(PicklingError,
1954 "persistent id must be string");
1955 goto finally;
1956 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001957
Tim Peters0bc93f52003-02-02 18:29:33 +00001958 if (self->write_func(self, &persid, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001959 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001960
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001961 if ((size = PyString_Size(pid)) < 0)
1962 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001963
Tim Peters0bc93f52003-02-02 18:29:33 +00001964 if (self->write_func(self,
1965 PyString_AS_STRING(
1966 (PyStringObject *)pid),
1967 size) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001968 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001969
Tim Peters0bc93f52003-02-02 18:29:33 +00001970 if (self->write_func(self, "\n", 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001971 goto finally;
Tim Peters84e87f32001-03-17 04:50:51 +00001972
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001973 res = 1;
1974 goto finally;
1975 }
1976 else if (save(self, pid, 1) >= 0) {
Tim Peters0bc93f52003-02-02 18:29:33 +00001977 if (self->write_func(self, &binpersid, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001978 res = -1;
1979 else
1980 res = 1;
1981 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001982
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001983 goto finally;
1984 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001985
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001986 res = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001987
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001988 finally:
1989 Py_XDECREF(pid);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001990
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001991 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001992}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001993
Tim Peters71fcda52003-02-14 23:05:28 +00001994/* We're saving ob, and args is the 2-thru-5 tuple returned by the
1995 * appropriate __reduce__ method for ob.
1996 */
Tim Peters84e87f32001-03-17 04:50:51 +00001997static int
Tim Peters71fcda52003-02-14 23:05:28 +00001998save_reduce(Picklerobject *self, PyObject *args, PyObject *ob)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001999{
Tim Peters71fcda52003-02-14 23:05:28 +00002000 PyObject *callable;
2001 PyObject *argtup;
2002 PyObject *state = NULL;
2003 PyObject *listitems = NULL;
2004 PyObject *dictitems = NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002005
Tim Peters71fcda52003-02-14 23:05:28 +00002006 int use_newobj = self->proto >= 2;
2007
2008 static char reduce = REDUCE;
2009 static char build = BUILD;
2010 static char newobj = NEWOBJ;
2011
2012 if (! PyArg_UnpackTuple(args, "save_reduce", 2, 5,
2013 &callable,
2014 &argtup,
2015 &state,
2016 &listitems,
2017 &dictitems))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002018 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002019
Raymond Hettingera6b45cc2004-12-07 07:05:57 +00002020 if (!PyTuple_Check(argtup)) {
2021 PyErr_SetString(PicklingError,
2022 "args from reduce() should be a tuple");
2023 return -1;
2024 }
2025
Tim Peters71fcda52003-02-14 23:05:28 +00002026 if (state == Py_None)
2027 state = NULL;
2028 if (listitems == Py_None)
2029 listitems = NULL;
2030 if (dictitems == Py_None)
2031 dictitems = NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002032
Tim Peters71fcda52003-02-14 23:05:28 +00002033 /* Protocol 2 special case: if callable's name is __newobj__, use
2034 * NEWOBJ. This consumes a lot of code.
2035 */
2036 if (use_newobj) {
2037 PyObject *temp = PyObject_GetAttr(callable, __name___str);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002038
Tim Peters71fcda52003-02-14 23:05:28 +00002039 if (temp == NULL) {
Jeremy Hyltonf00368f2003-06-16 20:19:49 +00002040 if (PyErr_ExceptionMatches(PyExc_AttributeError))
2041 PyErr_Clear();
2042 else
2043 return -1;
Tim Peters71fcda52003-02-14 23:05:28 +00002044 use_newobj = 0;
2045 }
2046 else {
2047 use_newobj = PyString_Check(temp) &&
2048 strcmp(PyString_AS_STRING(temp),
2049 "__newobj__") == 0;
2050 Py_DECREF(temp);
2051 }
2052 }
2053 if (use_newobj) {
2054 PyObject *cls;
2055 PyObject *newargtup;
2056 int n, i;
2057
2058 /* Sanity checks. */
2059 n = PyTuple_Size(argtup);
2060 if (n < 1) {
2061 PyErr_SetString(PicklingError, "__newobj__ arglist "
2062 "is empty");
2063 return -1;
2064 }
2065
2066 cls = PyTuple_GET_ITEM(argtup, 0);
2067 if (! PyObject_HasAttrString(cls, "__new__")) {
2068 PyErr_SetString(PicklingError, "args[0] from "
2069 "__newobj__ args has no __new__");
2070 return -1;
2071 }
2072
2073 /* XXX How could ob be NULL? */
2074 if (ob != NULL) {
2075 PyObject *ob_dot_class;
2076
2077 ob_dot_class = PyObject_GetAttr(ob, __class___str);
Jeremy Hyltonf00368f2003-06-16 20:19:49 +00002078 if (ob_dot_class == NULL) {
2079 if (PyErr_ExceptionMatches(
2080 PyExc_AttributeError))
2081 PyErr_Clear();
2082 else
2083 return -1;
2084 }
Tim Peters71fcda52003-02-14 23:05:28 +00002085 i = ob_dot_class != cls; /* true iff a problem */
2086 Py_XDECREF(ob_dot_class);
2087 if (i) {
2088 PyErr_SetString(PicklingError, "args[0] from "
2089 "__newobj__ args has the wrong class");
2090 return -1;
2091 }
2092 }
2093
2094 /* Save the class and its __new__ arguments. */
2095 if (save(self, cls, 0) < 0)
2096 return -1;
2097
2098 newargtup = PyTuple_New(n-1); /* argtup[1:] */
2099 if (newargtup == NULL)
2100 return -1;
2101 for (i = 1; i < n; ++i) {
2102 PyObject *temp = PyTuple_GET_ITEM(argtup, i);
2103 Py_INCREF(temp);
2104 PyTuple_SET_ITEM(newargtup, i-1, temp);
2105 }
2106 i = save(self, newargtup, 0) < 0;
2107 Py_DECREF(newargtup);
2108 if (i < 0)
2109 return -1;
2110
2111 /* Add NEWOBJ opcode. */
2112 if (self->write_func(self, &newobj, 1) < 0)
2113 return -1;
2114 }
2115 else {
2116 /* Not using NEWOBJ. */
2117 if (save(self, callable, 0) < 0 ||
2118 save(self, argtup, 0) < 0 ||
2119 self->write_func(self, &reduce, 1) < 0)
2120 return -1;
2121 }
2122
2123 /* Memoize. */
2124 /* XXX How can ob be NULL? */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002125 if (ob != NULL) {
2126 if (state && !PyDict_Check(state)) {
2127 if (put2(self, ob) < 0)
2128 return -1;
2129 }
Tim Peters71fcda52003-02-14 23:05:28 +00002130 else if (put(self, ob) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002131 return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002132 }
Tim Peters84e87f32001-03-17 04:50:51 +00002133
Guido van Rossum60456fd1997-04-09 17:36:32 +00002134
Tim Peters71fcda52003-02-14 23:05:28 +00002135 if (listitems && batch_list(self, listitems) < 0)
2136 return -1;
2137
2138 if (dictitems && batch_dict(self, dictitems) < 0)
2139 return -1;
2140
2141 if (state) {
2142 if (save(self, state, 0) < 0 ||
2143 self->write_func(self, &build, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002144 return -1;
2145 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002146
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002147 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002148}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002149
Guido van Rossum60456fd1997-04-09 17:36:32 +00002150static int
Tim Peters1d63c9f2003-02-02 20:29:39 +00002151save(Picklerobject *self, PyObject *args, int pers_save)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002152{
2153 PyTypeObject *type;
Tim Peters71fcda52003-02-14 23:05:28 +00002154 PyObject *py_ob_id = 0, *__reduce__ = 0, *t = 0;
2155 PyObject *arg_tup;
2156 int res = -1;
2157 int tmp, size;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002158
Martin v. Löwis5a395302002-08-04 08:20:23 +00002159 if (self->nesting++ > Py_GetRecursionLimit()){
2160 PyErr_SetString(PyExc_RuntimeError,
2161 "maximum recursion depth exceeded");
2162 goto finally;
2163 }
2164
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002165 if (!pers_save && self->pers_func) {
2166 if ((tmp = save_pers(self, args, self->pers_func)) != 0) {
2167 res = tmp;
2168 goto finally;
2169 }
2170 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002171
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002172 if (args == Py_None) {
2173 res = save_none(self, args);
2174 goto finally;
2175 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002176
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002177 type = args->ob_type;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002178
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002179 switch (type->tp_name[0]) {
Guido van Rossum77f6a652002-04-03 22:41:51 +00002180 case 'b':
2181 if (args == Py_False || args == Py_True) {
2182 res = save_bool(self, args);
2183 goto finally;
2184 }
2185 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002186 case 'i':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002187 if (type == &PyInt_Type) {
2188 res = save_int(self, args);
2189 goto finally;
2190 }
2191 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002192
Guido van Rossum60456fd1997-04-09 17:36:32 +00002193 case 'l':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002194 if (type == &PyLong_Type) {
2195 res = save_long(self, args);
2196 goto finally;
2197 }
2198 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002199
Guido van Rossum60456fd1997-04-09 17:36:32 +00002200 case 'f':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002201 if (type == &PyFloat_Type) {
2202 res = save_float(self, args);
2203 goto finally;
2204 }
2205 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002206
Guido van Rossum60456fd1997-04-09 17:36:32 +00002207 case 't':
Tim Peters1d63c9f2003-02-02 20:29:39 +00002208 if (type == &PyTuple_Type && PyTuple_Size(args) == 0) {
2209 res = save_tuple(self, args);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002210 goto finally;
2211 }
2212 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002213
Guido van Rossum60456fd1997-04-09 17:36:32 +00002214 case 's':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002215 if ((type == &PyString_Type) && (PyString_GET_SIZE(args) < 2)) {
2216 res = save_string(self, args, 0);
2217 goto finally;
2218 }
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00002219
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002220#ifdef Py_USING_UNICODE
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00002221 case 'u':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002222 if ((type == &PyUnicode_Type) && (PyString_GET_SIZE(args) < 2)) {
2223 res = save_unicode(self, args, 0);
2224 goto finally;
2225 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002226#endif
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002227 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002228
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002229 if (args->ob_refcnt > 1) {
Tim Peterscba30e22003-02-01 06:24:36 +00002230 if (!( py_ob_id = PyLong_FromVoidPtr(args)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002231 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002232
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002233 if (PyDict_GetItem(self->memo, py_ob_id)) {
2234 if (get(self, py_ob_id) < 0)
2235 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002236
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002237 res = 0;
2238 goto finally;
2239 }
2240 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002241
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002242 switch (type->tp_name[0]) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00002243 case 's':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002244 if (type == &PyString_Type) {
2245 res = save_string(self, args, 1);
2246 goto finally;
2247 }
2248 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002249
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002250#ifdef Py_USING_UNICODE
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00002251 case 'u':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002252 if (type == &PyUnicode_Type) {
2253 res = save_unicode(self, args, 1);
2254 goto finally;
2255 }
2256 break;
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002257#endif
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00002258
Guido van Rossum60456fd1997-04-09 17:36:32 +00002259 case 't':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002260 if (type == &PyTuple_Type) {
2261 res = save_tuple(self, args);
2262 goto finally;
2263 }
2264 if (type == &PyType_Type) {
2265 res = save_global(self, args, NULL);
2266 goto finally;
2267 }
2268 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002269
Guido van Rossum60456fd1997-04-09 17:36:32 +00002270 case 'l':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002271 if (type == &PyList_Type) {
2272 res = save_list(self, args);
2273 goto finally;
2274 }
2275 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002276
2277 case 'd':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002278 if (type == &PyDict_Type) {
2279 res = save_dict(self, args);
2280 goto finally;
2281 }
2282 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002283
Guido van Rossum512ab9f2006-08-17 22:28:49 +00002284 case 'i':
2285 break;
2286
2287 case 'c':
2288 break;
2289
Guido van Rossum60456fd1997-04-09 17:36:32 +00002290 case 'f':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002291 if (type == &PyFunction_Type) {
2292 res = save_global(self, args, NULL);
Christian Tismer2460c622004-02-26 16:21:45 +00002293 if (res && PyErr_ExceptionMatches(PickleError)) {
2294 /* fall back to reduce */
2295 PyErr_Clear();
2296 break;
2297 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002298 goto finally;
2299 }
2300 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002301
2302 case 'b':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002303 if (type == &PyCFunction_Type) {
2304 res = save_global(self, args, NULL);
2305 goto finally;
2306 }
2307 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002308
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002309 if (!pers_save && self->inst_pers_func) {
2310 if ((tmp = save_pers(self, args, self->inst_pers_func)) != 0) {
2311 res = tmp;
2312 goto finally;
2313 }
2314 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002315
Jeremy Hylton39c61162002-07-16 19:47:43 +00002316 if (PyType_IsSubtype(type, &PyType_Type)) {
2317 res = save_global(self, args, NULL);
2318 goto finally;
2319 }
2320
Guido van Rossumb289b872003-02-19 01:45:13 +00002321 /* Get a reduction callable, and call it. This may come from
2322 * copy_reg.dispatch_table, the object's __reduce_ex__ method,
2323 * or the object's __reduce__ method.
Tim Peters71fcda52003-02-14 23:05:28 +00002324 */
Tim Peters5aa3da62003-02-13 21:03:57 +00002325 __reduce__ = PyDict_GetItem(dispatch_table, (PyObject *)type);
2326 if (__reduce__ != NULL) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002327 Py_INCREF(__reduce__);
Guido van Rossumb289b872003-02-19 01:45:13 +00002328 Py_INCREF(args);
2329 ARG_TUP(self, args);
2330 if (self->arg) {
2331 t = PyObject_Call(__reduce__, self->arg, NULL);
2332 FREE_ARG_TUP(self);
2333 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002334 }
2335 else {
Guido van Rossumb289b872003-02-19 01:45:13 +00002336 /* Check for a __reduce_ex__ method. */
2337 __reduce__ = PyObject_GetAttr(args, __reduce_ex___str);
2338 if (__reduce__ != NULL) {
2339 t = PyInt_FromLong(self->proto);
2340 if (t != NULL) {
2341 ARG_TUP(self, t);
2342 t = NULL;
2343 if (self->arg) {
2344 t = PyObject_Call(__reduce__,
2345 self->arg, NULL);
2346 FREE_ARG_TUP(self);
2347 }
2348 }
2349 }
2350 else {
Jeremy Hyltonf00368f2003-06-16 20:19:49 +00002351 if (PyErr_ExceptionMatches(PyExc_AttributeError))
2352 PyErr_Clear();
2353 else
2354 goto finally;
Guido van Rossumb289b872003-02-19 01:45:13 +00002355 /* Check for a __reduce__ method. */
2356 __reduce__ = PyObject_GetAttr(args, __reduce___str);
2357 if (__reduce__ != NULL) {
2358 t = PyObject_Call(__reduce__,
2359 empty_tuple, NULL);
2360 }
2361 else {
2362 PyErr_SetObject(UnpickleableError, args);
2363 goto finally;
2364 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002365 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002366 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002367
Tim Peters71fcda52003-02-14 23:05:28 +00002368 if (t == NULL)
2369 goto finally;
Tim Peters84e87f32001-03-17 04:50:51 +00002370
Tim Peters71fcda52003-02-14 23:05:28 +00002371 if (PyString_Check(t)) {
2372 res = save_global(self, args, t);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002373 goto finally;
2374 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002375
Tim Peters71fcda52003-02-14 23:05:28 +00002376 if (! PyTuple_Check(t)) {
2377 cPickle_ErrFormat(PicklingError, "Value returned by "
2378 "%s must be string or tuple",
2379 "O", __reduce__);
2380 goto finally;
2381 }
2382
2383 size = PyTuple_Size(t);
2384 if (size < 2 || size > 5) {
2385 cPickle_ErrFormat(PicklingError, "tuple returned by "
2386 "%s must contain 2 through 5 elements",
2387 "O", __reduce__);
2388 goto finally;
2389 }
2390
2391 arg_tup = PyTuple_GET_ITEM(t, 1);
2392 if (!(PyTuple_Check(arg_tup) || arg_tup == Py_None)) {
2393 cPickle_ErrFormat(PicklingError, "Second element of "
2394 "tuple returned by %s must be a tuple",
2395 "O", __reduce__);
2396 goto finally;
2397 }
2398
2399 res = save_reduce(self, t, args);
Guido van Rossum60456fd1997-04-09 17:36:32 +00002400
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002401 finally:
Martin v. Löwis5a395302002-08-04 08:20:23 +00002402 self->nesting--;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002403 Py_XDECREF(py_ob_id);
2404 Py_XDECREF(__reduce__);
2405 Py_XDECREF(t);
Tim Peters84e87f32001-03-17 04:50:51 +00002406
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002407 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002408}
2409
2410
2411static int
Tim Peterscba30e22003-02-01 06:24:36 +00002412dump(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002413{
2414 static char stop = STOP;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002415
Tim Peters4190fb82003-02-02 16:09:05 +00002416 if (self->proto >= 2) {
2417 char bytes[2];
2418
2419 bytes[0] = PROTO;
Tim Peters87482ea2003-02-02 16:16:30 +00002420 assert(self->proto >= 0 && self->proto < 256);
2421 bytes[1] = (char)self->proto;
Tim Peters4190fb82003-02-02 16:09:05 +00002422 if (self->write_func(self, bytes, 2) < 0)
2423 return -1;
2424 }
2425
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002426 if (save(self, args, 0) < 0)
2427 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002428
Tim Peters4190fb82003-02-02 16:09:05 +00002429 if (self->write_func(self, &stop, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002430 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002431
Tim Peters4190fb82003-02-02 16:09:05 +00002432 if (self->write_func(self, NULL, 0) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002433 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002434
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002435 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002436}
2437
2438static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00002439Pickle_clear_memo(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002440{
Tim Peterscba30e22003-02-01 06:24:36 +00002441 if (self->memo)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002442 PyDict_Clear(self->memo);
2443 Py_INCREF(Py_None);
2444 return Py_None;
2445}
2446
2447static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00002448Pickle_getvalue(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002449{
2450 int l, i, rsize, ssize, clear=1, lm;
2451 long ik;
2452 PyObject *k, *r;
2453 char *s, *p, *have_get;
2454 Pdata *data;
2455
2456 /* Can be called by Python code or C code */
Tim Peterscba30e22003-02-01 06:24:36 +00002457 if (args && !PyArg_ParseTuple(args, "|i:getvalue", &clear))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002458 return NULL;
2459
2460 /* Check to make sure we are based on a list */
2461 if (! Pdata_Check(self->file)) {
2462 PyErr_SetString(PicklingError,
2463 "Attempt to getvalue() a non-list-based pickler");
2464 return NULL;
2465 }
2466
2467 /* flush write buffer */
2468 if (write_other(self, NULL, 0) < 0) return NULL;
2469
2470 data=(Pdata*)self->file;
2471 l=data->length;
2472
2473 /* set up an array to hold get/put status */
Tim Petersac5687a2003-02-02 18:08:34 +00002474 lm = PyDict_Size(self->memo);
2475 if (lm < 0) return NULL;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002476 lm++;
Tim Petersac5687a2003-02-02 18:08:34 +00002477 have_get = malloc(lm);
2478 if (have_get == NULL) return PyErr_NoMemory();
2479 memset(have_get, 0, lm);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002480
2481 /* Scan for gets. */
Tim Petersac5687a2003-02-02 18:08:34 +00002482 for (rsize = 0, i = l; --i >= 0; ) {
2483 k = data->data[i];
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002484
Tim Petersac5687a2003-02-02 18:08:34 +00002485 if (PyString_Check(k))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002486 rsize += PyString_GET_SIZE(k);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002487
2488 else if (PyInt_Check(k)) { /* put */
Tim Petersac5687a2003-02-02 18:08:34 +00002489 ik = PyInt_AS_LONG((PyIntObject*)k);
2490 if (ik >= lm || ik == 0) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002491 PyErr_SetString(PicklingError,
2492 "Invalid get data");
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002493 goto err;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002494 }
Tim Petersac5687a2003-02-02 18:08:34 +00002495 if (have_get[ik]) /* with matching get */
2496 rsize += ik < 256 ? 2 : 5;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002497 }
2498
2499 else if (! (PyTuple_Check(k) &&
2500 PyTuple_GET_SIZE(k) == 2 &&
Tim Petersac5687a2003-02-02 18:08:34 +00002501 PyInt_Check((k = PyTuple_GET_ITEM(k, 0))))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002502 ) {
2503 PyErr_SetString(PicklingError,
2504 "Unexpected data in internal list");
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002505 goto err;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002506 }
2507
2508 else { /* put */
Tim Petersac5687a2003-02-02 18:08:34 +00002509 ik = PyInt_AS_LONG((PyIntObject *)k);
2510 if (ik >= lm || ik == 0) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002511 PyErr_SetString(PicklingError,
2512 "Invalid get data");
2513 return NULL;
2514 }
Tim Petersac5687a2003-02-02 18:08:34 +00002515 have_get[ik] = 1;
2516 rsize += ik < 256 ? 2 : 5;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002517 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002518 }
2519
2520 /* Now generate the result */
Tim Petersac5687a2003-02-02 18:08:34 +00002521 r = PyString_FromStringAndSize(NULL, rsize);
2522 if (r == NULL) goto err;
2523 s = PyString_AS_STRING((PyStringObject *)r);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002524
Tim Petersac5687a2003-02-02 18:08:34 +00002525 for (i = 0; i < l; i++) {
2526 k = data->data[i];
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002527
2528 if (PyString_Check(k)) {
Tim Petersac5687a2003-02-02 18:08:34 +00002529 ssize = PyString_GET_SIZE(k);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002530 if (ssize) {
Tim Petersac5687a2003-02-02 18:08:34 +00002531 p=PyString_AS_STRING((PyStringObject *)k);
2532 while (--ssize >= 0)
2533 *s++ = *p++;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002534 }
2535 }
2536
2537 else if (PyTuple_Check(k)) { /* get */
Tim Petersac5687a2003-02-02 18:08:34 +00002538 ik = PyInt_AS_LONG((PyIntObject *)
2539 PyTuple_GET_ITEM(k, 0));
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002540 if (ik < 256) {
2541 *s++ = BINGET;
2542 *s++ = (int)(ik & 0xff);
2543 }
2544 else {
2545 *s++ = LONG_BINGET;
2546 *s++ = (int)(ik & 0xff);
2547 *s++ = (int)((ik >> 8) & 0xff);
2548 *s++ = (int)((ik >> 16) & 0xff);
2549 *s++ = (int)((ik >> 24) & 0xff);
2550 }
2551 }
2552
2553 else { /* put */
Tim Petersac5687a2003-02-02 18:08:34 +00002554 ik = PyInt_AS_LONG((PyIntObject*)k);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002555
2556 if (have_get[ik]) { /* with matching get */
2557 if (ik < 256) {
2558 *s++ = BINPUT;
2559 *s++ = (int)(ik & 0xff);
2560 }
2561 else {
2562 *s++ = LONG_BINPUT;
2563 *s++ = (int)(ik & 0xff);
2564 *s++ = (int)((ik >> 8) & 0xff);
2565 *s++ = (int)((ik >> 16) & 0xff);
2566 *s++ = (int)((ik >> 24) & 0xff);
2567 }
2568 }
2569 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002570 }
2571
2572 if (clear) {
2573 PyDict_Clear(self->memo);
Tim Petersac5687a2003-02-02 18:08:34 +00002574 Pdata_clear(data, 0);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002575 }
2576
2577 free(have_get);
2578 return r;
2579 err:
2580 free(have_get);
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00002581 return NULL;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002582}
2583
2584static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00002585Pickler_dump(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002586{
2587 PyObject *ob;
2588 int get=0;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002589
Tim Peterscba30e22003-02-01 06:24:36 +00002590 if (!( PyArg_ParseTuple(args, "O|i:dump", &ob, &get)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002591 return NULL;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002592
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002593 if (dump(self, ob) < 0)
2594 return NULL;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002595
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002596 if (get) return Pickle_getvalue(self, NULL);
Guido van Rossum053b8df1998-11-25 16:18:00 +00002597
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002598 /* XXX Why does dump() return self? */
2599 Py_INCREF(self);
2600 return (PyObject*)self;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002601}
2602
2603
Tim Peterscba30e22003-02-01 06:24:36 +00002604static struct PyMethodDef Pickler_methods[] =
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002605{
Neal Norwitzb0493252002-03-31 14:44:22 +00002606 {"dump", (PyCFunction)Pickler_dump, METH_VARARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00002607 PyDoc_STR("dump(object) -- "
2608 "Write an object in pickle format to the object's pickle stream")},
Fred Drake0ebacc82002-05-01 20:36:39 +00002609 {"clear_memo", (PyCFunction)Pickle_clear_memo, METH_NOARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00002610 PyDoc_STR("clear_memo() -- Clear the picklers memo")},
Neal Norwitzb0493252002-03-31 14:44:22 +00002611 {"getvalue", (PyCFunction)Pickle_getvalue, METH_VARARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00002612 PyDoc_STR("getvalue() -- Finish picking a list-based pickle")},
Guido van Rossum60456fd1997-04-09 17:36:32 +00002613 {NULL, NULL} /* sentinel */
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002614};
2615
2616
2617static Picklerobject *
Tim Peters5bd2a792003-02-01 16:45:06 +00002618newPicklerobject(PyObject *file, int proto)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002619{
2620 Picklerobject *self;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002621
Tim Peters5bd2a792003-02-01 16:45:06 +00002622 if (proto < 0)
Tim Peters8587b3c2003-02-13 15:44:41 +00002623 proto = HIGHEST_PROTOCOL;
2624 if (proto > HIGHEST_PROTOCOL) {
Tim Peters5bd2a792003-02-01 16:45:06 +00002625 PyErr_Format(PyExc_ValueError, "pickle protocol %d asked for; "
2626 "the highest available protocol is %d",
Tim Peters8587b3c2003-02-13 15:44:41 +00002627 proto, HIGHEST_PROTOCOL);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002628 return NULL;
Tim Peters5bd2a792003-02-01 16:45:06 +00002629 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002630
Jeremy Hylton4cf63192003-04-09 21:05:12 +00002631 self = PyObject_GC_New(Picklerobject, &Picklertype);
Tim Peters5bd2a792003-02-01 16:45:06 +00002632 if (self == NULL)
2633 return NULL;
2634 self->proto = proto;
2635 self->bin = proto > 0;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002636 self->fp = NULL;
2637 self->write = NULL;
2638 self->memo = NULL;
2639 self->arg = NULL;
2640 self->pers_func = NULL;
2641 self->inst_pers_func = NULL;
2642 self->write_buf = NULL;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002643 self->fast = 0;
Martin v. Löwis5a395302002-08-04 08:20:23 +00002644 self->nesting = 0;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002645 self->fast_container = 0;
2646 self->fast_memo = NULL;
2647 self->buf_size = 0;
2648 self->dispatch_table = NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002649
Tim Peters5bd2a792003-02-01 16:45:06 +00002650 self->file = NULL;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002651 if (file)
2652 Py_INCREF(file);
Tim Peters5bd2a792003-02-01 16:45:06 +00002653 else {
2654 file = Pdata_New();
2655 if (file == NULL)
2656 goto err;
2657 }
2658 self->file = file;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002659
Tim Peterscba30e22003-02-01 06:24:36 +00002660 if (!( self->memo = PyDict_New()))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002661 goto err;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002662
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002663 if (PyFile_Check(file)) {
2664 self->fp = PyFile_AsFile(file);
2665 if (self->fp == NULL) {
Tim Peters5bd2a792003-02-01 16:45:06 +00002666 PyErr_SetString(PyExc_ValueError,
2667 "I/O operation on closed file");
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002668 goto err;
2669 }
2670 self->write_func = write_file;
Guido van Rossumc91fcaa1999-03-29 20:00:14 +00002671 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002672 else if (PycStringIO_OutputCheck(file)) {
2673 self->write_func = write_cStringIO;
2674 }
2675 else if (file == Py_None) {
2676 self->write_func = write_none;
2677 }
2678 else {
2679 self->write_func = write_other;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002680
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002681 if (! Pdata_Check(file)) {
2682 self->write = PyObject_GetAttr(file, write_str);
2683 if (!self->write) {
2684 PyErr_Clear();
Tim Peterscba30e22003-02-01 06:24:36 +00002685 PyErr_SetString(PyExc_TypeError,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002686 "argument must have 'write' "
2687 "attribute");
2688 goto err;
2689 }
2690 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002691
Tim Peters5bd2a792003-02-01 16:45:06 +00002692 self->write_buf = (char *)PyMem_Malloc(WRITE_BUF_SIZE);
2693 if (self->write_buf == NULL) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002694 PyErr_NoMemory();
2695 goto err;
2696 }
2697 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002698
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002699 if (PyEval_GetRestricted()) {
2700 /* Restricted execution, get private tables */
Tim Peters5b7da392003-02-04 00:21:07 +00002701 PyObject *m = PyImport_Import(copy_reg_str);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002702
Tim Peters5b7da392003-02-04 00:21:07 +00002703 if (m == NULL)
2704 goto err;
2705 self->dispatch_table = PyObject_GetAttr(m, dispatch_table_str);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002706 Py_DECREF(m);
Tim Peters5b7da392003-02-04 00:21:07 +00002707 if (self->dispatch_table == NULL)
2708 goto err;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002709 }
2710 else {
Tim Peters5b7da392003-02-04 00:21:07 +00002711 self->dispatch_table = dispatch_table;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002712 Py_INCREF(dispatch_table);
2713 }
Jeremy Hylton4cf63192003-04-09 21:05:12 +00002714 PyObject_GC_Track(self);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002715
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002716 return self;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002717
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002718 err:
Tim Peters5bd2a792003-02-01 16:45:06 +00002719 Py_DECREF(self);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002720 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002721}
2722
2723
2724static PyObject *
Martin v. Löwis544f1192004-07-27 05:22:33 +00002725get_Pickler(PyObject *self, PyObject *args, PyObject *kwds)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002726{
Martin v. Löwis15e62742006-02-27 16:46:16 +00002727 static char *kwlist[] = {"file", "protocol", NULL};
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002728 PyObject *file = NULL;
Tim Peters5bd2a792003-02-01 16:45:06 +00002729 int proto = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002730
Tim Peters92c8bb32003-02-13 23:00:26 +00002731 /* XXX
Martin v. Löwis544f1192004-07-27 05:22:33 +00002732 * The documented signature is Pickler(file, protocol=0), but this
Tim Peters92c8bb32003-02-13 23:00:26 +00002733 * accepts Pickler() and Pickler(integer) too. The meaning then
2734 * is clear as mud, undocumented, and not supported by pickle.py.
2735 * I'm told Zope uses this, but I haven't traced into this code
2736 * far enough to figure out what it means.
Tim Peters5bd2a792003-02-01 16:45:06 +00002737 */
2738 if (!PyArg_ParseTuple(args, "|i:Pickler", &proto)) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002739 PyErr_Clear();
Tim Peters5bd2a792003-02-01 16:45:06 +00002740 proto = 0;
Martin v. Löwis544f1192004-07-27 05:22:33 +00002741 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|i:Pickler",
2742 kwlist, &file, &proto))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002743 return NULL;
2744 }
Tim Peters5bd2a792003-02-01 16:45:06 +00002745 return (PyObject *)newPicklerobject(file, proto);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002746}
2747
2748
2749static void
Tim Peterscba30e22003-02-01 06:24:36 +00002750Pickler_dealloc(Picklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002751{
Jeremy Hylton4cf63192003-04-09 21:05:12 +00002752 PyObject_GC_UnTrack(self);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002753 Py_XDECREF(self->write);
2754 Py_XDECREF(self->memo);
2755 Py_XDECREF(self->fast_memo);
2756 Py_XDECREF(self->arg);
2757 Py_XDECREF(self->file);
2758 Py_XDECREF(self->pers_func);
2759 Py_XDECREF(self->inst_pers_func);
2760 Py_XDECREF(self->dispatch_table);
Tim Peters5bd2a792003-02-01 16:45:06 +00002761 PyMem_Free(self->write_buf);
Tim Peters3cfe7542003-05-21 21:29:48 +00002762 self->ob_type->tp_free((PyObject *)self);
Jeremy Hylton4cf63192003-04-09 21:05:12 +00002763}
2764
2765static int
2766Pickler_traverse(Picklerobject *self, visitproc visit, void *arg)
2767{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002768 Py_VISIT(self->write);
2769 Py_VISIT(self->memo);
2770 Py_VISIT(self->fast_memo);
2771 Py_VISIT(self->arg);
2772 Py_VISIT(self->file);
2773 Py_VISIT(self->pers_func);
2774 Py_VISIT(self->inst_pers_func);
2775 Py_VISIT(self->dispatch_table);
Jeremy Hylton4cf63192003-04-09 21:05:12 +00002776 return 0;
2777}
2778
2779static int
2780Pickler_clear(Picklerobject *self)
2781{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002782 Py_CLEAR(self->write);
2783 Py_CLEAR(self->memo);
2784 Py_CLEAR(self->fast_memo);
2785 Py_CLEAR(self->arg);
2786 Py_CLEAR(self->file);
2787 Py_CLEAR(self->pers_func);
2788 Py_CLEAR(self->inst_pers_func);
2789 Py_CLEAR(self->dispatch_table);
Jeremy Hylton4cf63192003-04-09 21:05:12 +00002790 return 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002791}
2792
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002793static PyObject *
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00002794Pickler_get_pers_func(Picklerobject *p)
2795{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002796 if (p->pers_func == NULL)
2797 PyErr_SetString(PyExc_AttributeError, "persistent_id");
2798 else
2799 Py_INCREF(p->pers_func);
2800 return p->pers_func;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002801}
2802
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00002803static int
2804Pickler_set_pers_func(Picklerobject *p, PyObject *v)
2805{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002806 if (v == NULL) {
2807 PyErr_SetString(PyExc_TypeError,
2808 "attribute deletion is not supported");
2809 return -1;
2810 }
2811 Py_XDECREF(p->pers_func);
2812 Py_INCREF(v);
2813 p->pers_func = v;
2814 return 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002815}
2816
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00002817static int
2818Pickler_set_inst_pers_func(Picklerobject *p, PyObject *v)
2819{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002820 if (v == NULL) {
2821 PyErr_SetString(PyExc_TypeError,
2822 "attribute deletion is not supported");
2823 return -1;
2824 }
2825 Py_XDECREF(p->inst_pers_func);
2826 Py_INCREF(v);
2827 p->inst_pers_func = v;
2828 return 0;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00002829}
2830
2831static PyObject *
2832Pickler_get_memo(Picklerobject *p)
2833{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002834 if (p->memo == NULL)
2835 PyErr_SetString(PyExc_AttributeError, "memo");
2836 else
2837 Py_INCREF(p->memo);
2838 return p->memo;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00002839}
2840
2841static int
2842Pickler_set_memo(Picklerobject *p, PyObject *v)
2843{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002844 if (v == NULL) {
2845 PyErr_SetString(PyExc_TypeError,
2846 "attribute deletion is not supported");
2847 return -1;
2848 }
2849 if (!PyDict_Check(v)) {
2850 PyErr_SetString(PyExc_TypeError, "memo must be a dictionary");
2851 return -1;
2852 }
2853 Py_XDECREF(p->memo);
2854 Py_INCREF(v);
2855 p->memo = v;
2856 return 0;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00002857}
2858
2859static PyObject *
2860Pickler_get_error(Picklerobject *p)
2861{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002862 /* why is this an attribute on the Pickler? */
2863 Py_INCREF(PicklingError);
2864 return PicklingError;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00002865}
2866
2867static PyMemberDef Pickler_members[] = {
2868 {"binary", T_INT, offsetof(Picklerobject, bin)},
2869 {"fast", T_INT, offsetof(Picklerobject, fast)},
Jeremy Hylton3eb46f32001-10-16 17:10:49 +00002870 {NULL}
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00002871};
2872
2873static PyGetSetDef Pickler_getsets[] = {
Tim Peterscba30e22003-02-01 06:24:36 +00002874 {"persistent_id", (getter)Pickler_get_pers_func,
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00002875 (setter)Pickler_set_pers_func},
2876 {"inst_persistent_id", NULL, (setter)Pickler_set_inst_pers_func},
2877 {"memo", (getter)Pickler_get_memo, (setter)Pickler_set_memo},
Jeremy Hylton3eb46f32001-10-16 17:10:49 +00002878 {"PicklingError", (getter)Pickler_get_error, NULL},
2879 {NULL}
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00002880};
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002881
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002882PyDoc_STRVAR(Picklertype__doc__,
2883"Objects that know how to pickle objects\n");
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002884
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002885static PyTypeObject Picklertype = {
2886 PyObject_HEAD_INIT(NULL)
Guido van Rossum60456fd1997-04-09 17:36:32 +00002887 0, /*ob_size*/
Guido van Rossum14648392001-12-08 18:02:58 +00002888 "cPickle.Pickler", /*tp_name*/
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00002889 sizeof(Picklerobject), /*tp_basicsize*/
2890 0,
2891 (destructor)Pickler_dealloc, /* tp_dealloc */
2892 0, /* tp_print */
2893 0, /* tp_getattr */
2894 0, /* tp_setattr */
2895 0, /* tp_compare */
2896 0, /* tp_repr */
2897 0, /* tp_as_number */
2898 0, /* tp_as_sequence */
2899 0, /* tp_as_mapping */
2900 0, /* tp_hash */
2901 0, /* tp_call */
2902 0, /* tp_str */
Jason Tishlerfb8595d2003-01-06 12:41:26 +00002903 PyObject_GenericGetAttr, /* tp_getattro */
2904 PyObject_GenericSetAttr, /* tp_setattro */
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00002905 0, /* tp_as_buffer */
Jeremy Hylton4cf63192003-04-09 21:05:12 +00002906 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00002907 Picklertype__doc__, /* tp_doc */
Jeremy Hylton4cf63192003-04-09 21:05:12 +00002908 (traverseproc)Pickler_traverse, /* tp_traverse */
2909 (inquiry)Pickler_clear, /* tp_clear */
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00002910 0, /* tp_richcompare */
2911 0, /* tp_weaklistoffset */
2912 0, /* tp_iter */
2913 0, /* tp_iternext */
2914 Pickler_methods, /* tp_methods */
2915 Pickler_members, /* tp_members */
2916 Pickler_getsets, /* tp_getset */
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002917};
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002918
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002919static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00002920find_class(PyObject *py_module_name, PyObject *py_global_name, PyObject *fc)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002921{
2922 PyObject *global = 0, *module;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002923
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002924 if (fc) {
2925 if (fc==Py_None) {
Tim Peters92c8bb32003-02-13 23:00:26 +00002926 PyErr_SetString(UnpicklingError, "Global and instance "
2927 "pickles are not supported.");
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002928 return NULL;
2929 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00002930 return PyObject_CallFunctionObjArgs(fc, py_module_name,
2931 py_global_name, NULL);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002932 }
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00002933
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002934 module = PySys_GetObject("modules");
2935 if (module == NULL)
2936 return NULL;
Jeremy Hyltond1055231998-08-11 19:52:51 +00002937
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002938 module = PyDict_GetItem(module, py_module_name);
2939 if (module == NULL) {
2940 module = PyImport_Import(py_module_name);
2941 if (!module)
2942 return NULL;
2943 global = PyObject_GetAttr(module, py_global_name);
2944 Py_DECREF(module);
2945 }
2946 else
2947 global = PyObject_GetAttr(module, py_global_name);
2948 return global;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002949}
2950
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002951static int
Tim Peterscba30e22003-02-01 06:24:36 +00002952marker(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002953{
2954 if (self->num_marks < 1) {
2955 PyErr_SetString(UnpicklingError, "could not find MARK");
2956 return -1;
2957 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002958
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002959 return self->marks[--self->num_marks];
Guido van Rossum60456fd1997-04-09 17:36:32 +00002960}
2961
Tim Peters84e87f32001-03-17 04:50:51 +00002962
Guido van Rossum60456fd1997-04-09 17:36:32 +00002963static int
Tim Peterscba30e22003-02-01 06:24:36 +00002964load_none(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002965{
2966 PDATA_APPEND(self->stack, Py_None, -1);
2967 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002968}
2969
Guido van Rossumf9ffb031999-02-04 14:54:04 +00002970static int
Tim Peterscba30e22003-02-01 06:24:36 +00002971bad_readline(void)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002972{
2973 PyErr_SetString(UnpicklingError, "pickle data was truncated");
2974 return -1;
Guido van Rossumf9ffb031999-02-04 14:54:04 +00002975}
Guido van Rossum60456fd1997-04-09 17:36:32 +00002976
2977static int
Tim Peterscba30e22003-02-01 06:24:36 +00002978load_int(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002979{
2980 PyObject *py_int = 0;
2981 char *endptr, *s;
2982 int len, res = -1;
2983 long l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002984
Tim Peters0bc93f52003-02-02 18:29:33 +00002985 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002986 if (len < 2) return bad_readline();
2987 if (!( s=pystrndup(s,len))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002988
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002989 errno = 0;
2990 l = strtol(s, &endptr, 0);
Guido van Rossum60456fd1997-04-09 17:36:32 +00002991
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002992 if (errno || (*endptr != '\n') || (endptr[1] != '\0')) {
2993 /* Hm, maybe we've got something long. Let's try reading
2994 it as a Python long object. */
2995 errno = 0;
2996 py_int = PyLong_FromString(s, NULL, 0);
2997 if (py_int == NULL) {
2998 PyErr_SetString(PyExc_ValueError,
2999 "could not convert string to int");
3000 goto finally;
3001 }
3002 }
3003 else {
Guido van Rossume2763392002-04-05 19:30:08 +00003004 if (len == 3 && (l == 0 || l == 1)) {
3005 if (!( py_int = PyBool_FromLong(l))) goto finally;
3006 }
3007 else {
3008 if (!( py_int = PyInt_FromLong(l))) goto finally;
3009 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003010 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003011
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003012 free(s);
3013 PDATA_PUSH(self->stack, py_int, -1);
3014 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003015
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003016 finally:
3017 free(s);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003018
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003019 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003020}
3021
Tim Peters3c67d792003-02-02 17:59:11 +00003022static int
3023load_bool(Unpicklerobject *self, PyObject *boolean)
3024{
3025 assert(boolean == Py_True || boolean == Py_False);
Tim Peterse0a39072003-02-03 15:45:56 +00003026 PDATA_APPEND(self->stack, boolean, -1);
Tim Peters3c67d792003-02-02 17:59:11 +00003027 return 0;
3028}
3029
Tim Petersee1a53c2003-02-02 02:57:53 +00003030/* s contains x bytes of a little-endian integer. Return its value as a
3031 * C int. Obscure: when x is 1 or 2, this is an unsigned little-endian
3032 * int, but when x is 4 it's a signed one. This is an historical source
3033 * of x-platform bugs.
3034 */
Tim Peters84e87f32001-03-17 04:50:51 +00003035static long
Tim Petersee1a53c2003-02-02 02:57:53 +00003036calc_binint(char *s, int x)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003037{
3038 unsigned char c;
3039 int i;
3040 long l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003041
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003042 for (i = 0, l = 0L; i < x; i++) {
3043 c = (unsigned char)s[i];
3044 l |= (long)c << (i * 8);
3045 }
Tim Petersbfa18f72001-04-10 01:54:42 +00003046#if SIZEOF_LONG > 4
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003047 /* Unlike BININT1 and BININT2, BININT (more accurately BININT4)
3048 * is signed, so on a box with longs bigger than 4 bytes we need
3049 * to extend a BININT's sign bit to the full width.
3050 */
3051 if (x == 4 && l & (1L << 31))
3052 l |= (~0L) << 32;
Tim Petersbfa18f72001-04-10 01:54:42 +00003053#endif
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003054 return l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003055}
3056
3057
3058static int
Tim Peterscba30e22003-02-01 06:24:36 +00003059load_binintx(Unpicklerobject *self, char *s, int x)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003060{
3061 PyObject *py_int = 0;
3062 long l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003063
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003064 l = calc_binint(s, x);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003065
Tim Peterscba30e22003-02-01 06:24:36 +00003066 if (!( py_int = PyInt_FromLong(l)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003067 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003068
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003069 PDATA_PUSH(self->stack, py_int, -1);
3070 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003071}
3072
3073
3074static int
Tim Peterscba30e22003-02-01 06:24:36 +00003075load_binint(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003076{
3077 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003078
Tim Peters0bc93f52003-02-02 18:29:33 +00003079 if (self->read_func(self, &s, 4) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003080 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003081
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003082 return load_binintx(self, s, 4);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003083}
3084
3085
3086static int
Tim Peterscba30e22003-02-01 06:24:36 +00003087load_binint1(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003088{
3089 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003090
Tim Peters0bc93f52003-02-02 18:29:33 +00003091 if (self->read_func(self, &s, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003092 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003093
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003094 return load_binintx(self, s, 1);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003095}
3096
3097
3098static int
Tim Peterscba30e22003-02-01 06:24:36 +00003099load_binint2(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003100{
3101 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003102
Tim Peters0bc93f52003-02-02 18:29:33 +00003103 if (self->read_func(self, &s, 2) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003104 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003105
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003106 return load_binintx(self, s, 2);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003107}
Tim Peters84e87f32001-03-17 04:50:51 +00003108
Guido van Rossum60456fd1997-04-09 17:36:32 +00003109static int
Tim Peterscba30e22003-02-01 06:24:36 +00003110load_long(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003111{
3112 PyObject *l = 0;
3113 char *end, *s;
3114 int len, res = -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003115
Tim Peters0bc93f52003-02-02 18:29:33 +00003116 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003117 if (len < 2) return bad_readline();
3118 if (!( s=pystrndup(s,len))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003119
Tim Peterscba30e22003-02-01 06:24:36 +00003120 if (!( l = PyLong_FromString(s, &end, 0)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003121 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003122
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003123 free(s);
3124 PDATA_PUSH(self->stack, l, -1);
3125 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003126
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003127 finally:
3128 free(s);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003129
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003130 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003131}
3132
Tim Petersee1a53c2003-02-02 02:57:53 +00003133/* 'size' bytes contain the # of bytes of little-endian 256's-complement
3134 * data following.
3135 */
3136static int
3137load_counted_long(Unpicklerobject *self, int size)
3138{
Martin v. Löwis18e16552006-02-15 17:27:45 +00003139 Py_ssize_t i;
Tim Petersee1a53c2003-02-02 02:57:53 +00003140 char *nbytes;
3141 unsigned char *pdata;
3142 PyObject *along;
3143
3144 assert(size == 1 || size == 4);
3145 i = self->read_func(self, &nbytes, size);
3146 if (i < 0) return -1;
3147
3148 size = calc_binint(nbytes, size);
3149 if (size < 0) {
3150 /* Corrupt or hostile pickle -- we never write one like
3151 * this.
3152 */
Tim Peters0c7c48e2003-02-03 22:07:24 +00003153 PyErr_SetString(UnpicklingError, "LONG pickle has negative "
Tim Petersee1a53c2003-02-02 02:57:53 +00003154 "byte count");
3155 return -1;
3156 }
3157
3158 if (size == 0)
3159 along = PyLong_FromLong(0L);
3160 else {
3161 /* Read the raw little-endian bytes & convert. */
Neal Norwitz529baf22003-02-02 17:08:33 +00003162 i = self->read_func(self, (char **)&pdata, size);
Tim Petersee1a53c2003-02-02 02:57:53 +00003163 if (i < 0) return -1;
3164 along = _PyLong_FromByteArray(pdata, (size_t)size,
3165 1 /* little endian */, 1 /* signed */);
3166 }
3167 if (along == NULL)
3168 return -1;
3169 PDATA_PUSH(self->stack, along, -1);
3170 return 0;
3171}
Tim Peters84e87f32001-03-17 04:50:51 +00003172
Guido van Rossum60456fd1997-04-09 17:36:32 +00003173static int
Tim Peterscba30e22003-02-01 06:24:36 +00003174load_float(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003175{
3176 PyObject *py_float = 0;
3177 char *endptr, *s;
3178 int len, res = -1;
3179 double d;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003180
Tim Peters0bc93f52003-02-02 18:29:33 +00003181 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003182 if (len < 2) return bad_readline();
3183 if (!( s=pystrndup(s,len))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003184
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003185 errno = 0;
Martin v. Löwis737ea822004-06-08 18:52:54 +00003186 d = PyOS_ascii_strtod(s, &endptr);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003187
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003188 if (errno || (endptr[0] != '\n') || (endptr[1] != '\0')) {
3189 PyErr_SetString(PyExc_ValueError,
3190 "could not convert string to float");
3191 goto finally;
3192 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003193
Tim Peterscba30e22003-02-01 06:24:36 +00003194 if (!( py_float = PyFloat_FromDouble(d)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003195 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003196
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003197 free(s);
3198 PDATA_PUSH(self->stack, py_float, -1);
3199 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003200
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003201 finally:
3202 free(s);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003203
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003204 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003205}
3206
Guido van Rossum60456fd1997-04-09 17:36:32 +00003207static int
Tim Peterscba30e22003-02-01 06:24:36 +00003208load_binfloat(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003209{
Tim Peters9905b942003-03-20 20:53:32 +00003210 PyObject *py_float;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003211 double x;
3212 char *p;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003213
Tim Peters0bc93f52003-02-02 18:29:33 +00003214 if (self->read_func(self, &p, 8) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003215 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003216
Tim Peters9905b942003-03-20 20:53:32 +00003217 x = _PyFloat_Unpack8((unsigned char *)p, 0);
3218 if (x == -1.0 && PyErr_Occurred())
3219 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003220
Tim Peters9905b942003-03-20 20:53:32 +00003221 py_float = PyFloat_FromDouble(x);
3222 if (py_float == NULL)
3223 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003224
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003225 PDATA_PUSH(self->stack, py_float, -1);
3226 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003227}
Guido van Rossum60456fd1997-04-09 17:36:32 +00003228
3229static int
Tim Peterscba30e22003-02-01 06:24:36 +00003230load_string(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003231{
3232 PyObject *str = 0;
Martin v. Löwis8a8da792002-08-14 07:46:28 +00003233 int len, res = -1;
3234 char *s, *p;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003235
Tim Peters0bc93f52003-02-02 18:29:33 +00003236 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003237 if (len < 2) return bad_readline();
3238 if (!( s=pystrndup(s,len))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003239
Martin v. Löwis8a8da792002-08-14 07:46:28 +00003240
3241 /* Strip outermost quotes */
3242 while (s[len-1] <= ' ')
3243 len--;
3244 if(s[0]=='"' && s[len-1]=='"'){
3245 s[len-1] = '\0';
3246 p = s + 1 ;
3247 len -= 2;
3248 } else if(s[0]=='\'' && s[len-1]=='\''){
3249 s[len-1] = '\0';
3250 p = s + 1 ;
3251 len -= 2;
3252 } else
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003253 goto insecure;
3254 /********************************************/
Guido van Rossum9716aaa1997-12-08 15:15:16 +00003255
Martin v. Löwis8a8da792002-08-14 07:46:28 +00003256 str = PyString_DecodeEscape(p, len, NULL, 0, NULL);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003257 free(s);
Martin v. Löwis8a8da792002-08-14 07:46:28 +00003258 if (str) {
3259 PDATA_PUSH(self->stack, str, -1);
3260 res = 0;
3261 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003262 return res;
Tim Peters84e87f32001-03-17 04:50:51 +00003263
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003264 insecure:
3265 free(s);
3266 PyErr_SetString(PyExc_ValueError,"insecure string pickle");
3267 return -1;
Tim Peters84e87f32001-03-17 04:50:51 +00003268}
Guido van Rossum60456fd1997-04-09 17:36:32 +00003269
3270
3271static int
Tim Peterscba30e22003-02-01 06:24:36 +00003272load_binstring(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003273{
3274 PyObject *py_string = 0;
3275 long l;
3276 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003277
Tim Peters0bc93f52003-02-02 18:29:33 +00003278 if (self->read_func(self, &s, 4) < 0) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003279
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003280 l = calc_binint(s, 4);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003281
Tim Peters0bc93f52003-02-02 18:29:33 +00003282 if (self->read_func(self, &s, l) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003283 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003284
Tim Peterscba30e22003-02-01 06:24:36 +00003285 if (!( py_string = PyString_FromStringAndSize(s, l)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003286 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003287
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003288 PDATA_PUSH(self->stack, py_string, -1);
3289 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003290}
3291
3292
3293static int
Tim Peterscba30e22003-02-01 06:24:36 +00003294load_short_binstring(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003295{
3296 PyObject *py_string = 0;
3297 unsigned char l;
3298 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003299
Tim Peters0bc93f52003-02-02 18:29:33 +00003300 if (self->read_func(self, &s, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003301 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003302
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003303 l = (unsigned char)s[0];
Guido van Rossum60456fd1997-04-09 17:36:32 +00003304
Tim Peters0bc93f52003-02-02 18:29:33 +00003305 if (self->read_func(self, &s, l) < 0) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003306
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003307 if (!( py_string = PyString_FromStringAndSize(s, l))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003308
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003309 PDATA_PUSH(self->stack, py_string, -1);
3310 return 0;
Tim Peters84e87f32001-03-17 04:50:51 +00003311}
Guido van Rossum60456fd1997-04-09 17:36:32 +00003312
3313
Martin v. Löwis339d0f72001-08-17 18:39:25 +00003314#ifdef Py_USING_UNICODE
Guido van Rossum60456fd1997-04-09 17:36:32 +00003315static int
Tim Peterscba30e22003-02-01 06:24:36 +00003316load_unicode(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003317{
3318 PyObject *str = 0;
3319 int len, res = -1;
3320 char *s;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003321
Tim Peters0bc93f52003-02-02 18:29:33 +00003322 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003323 if (len < 1) return bad_readline();
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003324
Tim Peterscba30e22003-02-01 06:24:36 +00003325 if (!( str = PyUnicode_DecodeRawUnicodeEscape(s, len - 1, NULL)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003326 goto finally;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003327
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003328 PDATA_PUSH(self->stack, str, -1);
3329 return 0;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003330
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003331 finally:
3332 return res;
Tim Peters84e87f32001-03-17 04:50:51 +00003333}
Martin v. Löwis339d0f72001-08-17 18:39:25 +00003334#endif
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003335
3336
Martin v. Löwis339d0f72001-08-17 18:39:25 +00003337#ifdef Py_USING_UNICODE
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003338static int
Tim Peterscba30e22003-02-01 06:24:36 +00003339load_binunicode(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003340{
3341 PyObject *unicode;
3342 long l;
3343 char *s;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003344
Tim Peters0bc93f52003-02-02 18:29:33 +00003345 if (self->read_func(self, &s, 4) < 0) return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003346
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003347 l = calc_binint(s, 4);
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003348
Tim Peters0bc93f52003-02-02 18:29:33 +00003349 if (self->read_func(self, &s, l) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003350 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003351
Tim Peterscba30e22003-02-01 06:24:36 +00003352 if (!( unicode = PyUnicode_DecodeUTF8(s, l, NULL)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003353 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003354
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003355 PDATA_PUSH(self->stack, unicode, -1);
3356 return 0;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003357}
Martin v. Löwis339d0f72001-08-17 18:39:25 +00003358#endif
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003359
3360
3361static int
Tim Peterscba30e22003-02-01 06:24:36 +00003362load_tuple(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003363{
3364 PyObject *tup;
3365 int i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003366
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003367 if ((i = marker(self)) < 0) return -1;
3368 if (!( tup=Pdata_popTuple(self->stack, i))) return -1;
3369 PDATA_PUSH(self->stack, tup, -1);
3370 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003371}
3372
3373static int
Tim Peters1d63c9f2003-02-02 20:29:39 +00003374load_counted_tuple(Unpicklerobject *self, int len)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003375{
Tim Peters1d63c9f2003-02-02 20:29:39 +00003376 PyObject *tup = PyTuple_New(len);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003377
Tim Peters1d63c9f2003-02-02 20:29:39 +00003378 if (tup == NULL)
3379 return -1;
3380
3381 while (--len >= 0) {
3382 PyObject *element;
3383
3384 PDATA_POP(self->stack, element);
3385 if (element == NULL)
3386 return -1;
3387 PyTuple_SET_ITEM(tup, len, element);
3388 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003389 PDATA_PUSH(self->stack, tup, -1);
3390 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003391}
3392
3393static int
Tim Peterscba30e22003-02-01 06:24:36 +00003394load_empty_list(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003395{
3396 PyObject *list;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003397
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003398 if (!( list=PyList_New(0))) return -1;
3399 PDATA_PUSH(self->stack, list, -1);
3400 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003401}
3402
3403static int
Tim Peterscba30e22003-02-01 06:24:36 +00003404load_empty_dict(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003405{
3406 PyObject *dict;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003407
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003408 if (!( dict=PyDict_New())) return -1;
3409 PDATA_PUSH(self->stack, dict, -1);
3410 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003411}
3412
3413
3414static int
Tim Peterscba30e22003-02-01 06:24:36 +00003415load_list(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003416{
3417 PyObject *list = 0;
3418 int i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003419
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003420 if ((i = marker(self)) < 0) return -1;
3421 if (!( list=Pdata_popList(self->stack, i))) return -1;
3422 PDATA_PUSH(self->stack, list, -1);
3423 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003424}
3425
3426static int
Tim Peterscba30e22003-02-01 06:24:36 +00003427load_dict(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003428{
3429 PyObject *dict, *key, *value;
3430 int i, j, k;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003431
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003432 if ((i = marker(self)) < 0) return -1;
3433 j=self->stack->length;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003434
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003435 if (!( dict = PyDict_New())) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003436
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003437 for (k = i+1; k < j; k += 2) {
3438 key =self->stack->data[k-1];
3439 value=self->stack->data[k ];
3440 if (PyDict_SetItem(dict, key, value) < 0) {
3441 Py_DECREF(dict);
3442 return -1;
3443 }
3444 }
3445 Pdata_clear(self->stack, i);
3446 PDATA_PUSH(self->stack, dict, -1);
3447 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003448}
3449
Guido van Rossum512ab9f2006-08-17 22:28:49 +00003450static PyObject *
3451Instance_New(PyObject *cls, PyObject *args)
3452{
3453 PyObject *r = 0;
3454
3455 if ((r=PyObject_CallObject(cls, args))) return r;
3456
3457 {
3458 PyObject *tp, *v, *tb, *tmp_value;
3459
3460 PyErr_Fetch(&tp, &v, &tb);
3461 tmp_value = v;
3462 /* NULL occurs when there was a KeyboardInterrupt */
3463 if (tmp_value == NULL)
3464 tmp_value = Py_None;
3465 if ((r = PyTuple_Pack(3, tmp_value, cls, args))) {
3466 Py_XDECREF(v);
3467 v=r;
3468 }
3469 PyErr_Restore(tp,v,tb);
3470 }
3471 return NULL;
3472}
3473
Guido van Rossum60456fd1997-04-09 17:36:32 +00003474
3475static int
Tim Peterscba30e22003-02-01 06:24:36 +00003476load_obj(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003477{
3478 PyObject *class, *tup, *obj=0;
3479 int i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003480
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003481 if ((i = marker(self)) < 0) return -1;
3482 if (!( tup=Pdata_popTuple(self->stack, i+1))) return -1;
3483 PDATA_POP(self->stack, class);
Guido van Rossum512ab9f2006-08-17 22:28:49 +00003484 if (class) {
3485 obj = Instance_New(class, tup);
3486 Py_DECREF(class);
3487 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003488 Py_DECREF(tup);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003489
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003490 if (! obj) return -1;
3491 PDATA_PUSH(self->stack, obj, -1);
3492 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003493}
3494
3495
3496static int
Tim Peterscba30e22003-02-01 06:24:36 +00003497load_inst(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003498{
3499 PyObject *tup, *class=0, *obj=0, *module_name, *class_name;
3500 int i, len;
3501 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003502
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003503 if ((i = marker(self)) < 0) return -1;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003504
Tim Peters0bc93f52003-02-02 18:29:33 +00003505 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003506 if (len < 2) return bad_readline();
3507 module_name = PyString_FromStringAndSize(s, len - 1);
3508 if (!module_name) return -1;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003509
Tim Peters0bc93f52003-02-02 18:29:33 +00003510 if ((len = self->readline_func(self, &s)) >= 0) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003511 if (len < 2) return bad_readline();
3512 if ((class_name = PyString_FromStringAndSize(s, len - 1))) {
Tim Peterscba30e22003-02-01 06:24:36 +00003513 class = find_class(module_name, class_name,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003514 self->find_class);
3515 Py_DECREF(class_name);
3516 }
3517 }
3518 Py_DECREF(module_name);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003519
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003520 if (! class) return -1;
Tim Peters84e87f32001-03-17 04:50:51 +00003521
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003522 if ((tup=Pdata_popTuple(self->stack, i))) {
Guido van Rossum512ab9f2006-08-17 22:28:49 +00003523 obj = Instance_New(class, tup);
3524 Py_DECREF(tup);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003525 }
3526 Py_DECREF(class);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003527
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003528 if (! obj) return -1;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003529
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003530 PDATA_PUSH(self->stack, obj, -1);
3531 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003532}
3533
Tim Peterseab7db32003-02-13 18:24:14 +00003534static int
3535load_newobj(Unpicklerobject *self)
3536{
3537 PyObject *args = NULL;
3538 PyObject *clsraw = NULL;
3539 PyTypeObject *cls; /* clsraw cast to its true type */
3540 PyObject *obj;
3541
3542 /* Stack is ... cls argtuple, and we want to call
3543 * cls.__new__(cls, *argtuple).
3544 */
3545 PDATA_POP(self->stack, args);
3546 if (args == NULL) goto Fail;
3547 if (! PyTuple_Check(args)) {
3548 PyErr_SetString(UnpicklingError, "NEWOBJ expected an arg "
3549 "tuple.");
3550 goto Fail;
3551 }
3552
3553 PDATA_POP(self->stack, clsraw);
3554 cls = (PyTypeObject *)clsraw;
3555 if (cls == NULL) goto Fail;
3556 if (! PyType_Check(cls)) {
3557 PyErr_SetString(UnpicklingError, "NEWOBJ class argument "
3558 "isn't a type object");
3559 goto Fail;
3560 }
3561 if (cls->tp_new == NULL) {
3562 PyErr_SetString(UnpicklingError, "NEWOBJ class argument "
3563 "has NULL tp_new");
3564 goto Fail;
3565 }
3566
3567 /* Call __new__. */
3568 obj = cls->tp_new(cls, args, NULL);
3569 if (obj == NULL) goto Fail;
3570
3571 Py_DECREF(args);
3572 Py_DECREF(clsraw);
3573 PDATA_PUSH(self->stack, obj, -1);
3574 return 0;
3575
3576 Fail:
3577 Py_XDECREF(args);
3578 Py_XDECREF(clsraw);
3579 return -1;
3580}
Guido van Rossum60456fd1997-04-09 17:36:32 +00003581
3582static int
Tim Peterscba30e22003-02-01 06:24:36 +00003583load_global(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003584{
3585 PyObject *class = 0, *module_name = 0, *class_name = 0;
3586 int len;
3587 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003588
Tim Peters0bc93f52003-02-02 18:29:33 +00003589 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003590 if (len < 2) return bad_readline();
3591 module_name = PyString_FromStringAndSize(s, len - 1);
3592 if (!module_name) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003593
Tim Peters0bc93f52003-02-02 18:29:33 +00003594 if ((len = self->readline_func(self, &s)) >= 0) {
Tim Peters4e52ca82002-12-07 02:43:28 +00003595 if (len < 2) {
3596 Py_DECREF(module_name);
3597 return bad_readline();
3598 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003599 if ((class_name = PyString_FromStringAndSize(s, len - 1))) {
Tim Peterscba30e22003-02-01 06:24:36 +00003600 class = find_class(module_name, class_name,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003601 self->find_class);
3602 Py_DECREF(class_name);
3603 }
3604 }
3605 Py_DECREF(module_name);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003606
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003607 if (! class) return -1;
3608 PDATA_PUSH(self->stack, class, -1);
3609 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003610}
3611
3612
3613static int
Tim Peterscba30e22003-02-01 06:24:36 +00003614load_persid(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003615{
3616 PyObject *pid = 0;
3617 int len;
3618 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003619
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003620 if (self->pers_func) {
Tim Peters0bc93f52003-02-02 18:29:33 +00003621 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003622 if (len < 2) return bad_readline();
3623
3624 pid = PyString_FromStringAndSize(s, len - 1);
3625 if (!pid) return -1;
3626
3627 if (PyList_Check(self->pers_func)) {
3628 if (PyList_Append(self->pers_func, pid) < 0) {
3629 Py_DECREF(pid);
3630 return -1;
3631 }
3632 }
3633 else {
3634 ARG_TUP(self, pid);
3635 if (self->arg) {
Tim Peterscba30e22003-02-01 06:24:36 +00003636 pid = PyObject_Call(self->pers_func, self->arg,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003637 NULL);
3638 FREE_ARG_TUP(self);
3639 }
3640 }
3641
3642 if (! pid) return -1;
3643
3644 PDATA_PUSH(self->stack, pid, -1);
3645 return 0;
3646 }
3647 else {
3648 PyErr_SetString(UnpicklingError,
3649 "A load persistent id instruction was encountered,\n"
3650 "but no persistent_load function was specified.");
3651 return -1;
3652 }
3653}
3654
3655static int
Tim Peterscba30e22003-02-01 06:24:36 +00003656load_binpersid(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003657{
3658 PyObject *pid = 0;
3659
3660 if (self->pers_func) {
3661 PDATA_POP(self->stack, pid);
3662 if (! pid) return -1;
3663
3664 if (PyList_Check(self->pers_func)) {
3665 if (PyList_Append(self->pers_func, pid) < 0) {
3666 Py_DECREF(pid);
3667 return -1;
3668 }
3669 }
3670 else {
3671 ARG_TUP(self, pid);
3672 if (self->arg) {
3673 pid = PyObject_Call(self->pers_func, self->arg,
3674 NULL);
3675 FREE_ARG_TUP(self);
3676 }
3677 if (! pid) return -1;
3678 }
3679
3680 PDATA_PUSH(self->stack, pid, -1);
3681 return 0;
3682 }
3683 else {
3684 PyErr_SetString(UnpicklingError,
3685 "A load persistent id instruction was encountered,\n"
3686 "but no persistent_load function was specified.");
3687 return -1;
3688 }
3689}
3690
3691
3692static int
Tim Peterscba30e22003-02-01 06:24:36 +00003693load_pop(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003694{
3695 int len;
3696
3697 if (!( (len=self->stack->length) > 0 )) return stackUnderflow();
3698
3699 /* Note that we split the (pickle.py) stack into two stacks,
3700 an object stack and a mark stack. We have to be clever and
3701 pop the right one. We do this by looking at the top of the
3702 mark stack.
3703 */
3704
3705 if ((self->num_marks > 0) &&
3706 (self->marks[self->num_marks - 1] == len))
3707 self->num_marks--;
3708 else {
3709 len--;
3710 Py_DECREF(self->stack->data[len]);
3711 self->stack->length=len;
3712 }
3713
3714 return 0;
3715}
3716
3717
3718static int
Tim Peterscba30e22003-02-01 06:24:36 +00003719load_pop_mark(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003720{
3721 int i;
3722
3723 if ((i = marker(self)) < 0)
3724 return -1;
3725
3726 Pdata_clear(self->stack, i);
3727
3728 return 0;
3729}
3730
3731
3732static int
Tim Peterscba30e22003-02-01 06:24:36 +00003733load_dup(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003734{
3735 PyObject *last;
3736 int len;
3737
3738 if ((len = self->stack->length) <= 0) return stackUnderflow();
3739 last=self->stack->data[len-1];
3740 Py_INCREF(last);
3741 PDATA_PUSH(self->stack, last, -1);
3742 return 0;
3743}
3744
3745
3746static int
Tim Peterscba30e22003-02-01 06:24:36 +00003747load_get(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003748{
3749 PyObject *py_str = 0, *value = 0;
3750 int len;
3751 char *s;
3752 int rc;
3753
Tim Peters0bc93f52003-02-02 18:29:33 +00003754 if ((len = self->readline_func(self, &s)) < 0) return -1;
Guido van Rossumf9ffb031999-02-04 14:54:04 +00003755 if (len < 2) return bad_readline();
Tim Peters84e87f32001-03-17 04:50:51 +00003756
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003757 if (!( py_str = PyString_FromStringAndSize(s, len - 1))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003758
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003759 value = PyDict_GetItem(self->memo, py_str);
3760 if (! value) {
3761 PyErr_SetObject(BadPickleGet, py_str);
3762 rc = -1;
Tim Peters92c8bb32003-02-13 23:00:26 +00003763 }
3764 else {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003765 PDATA_APPEND(self->stack, value, -1);
3766 rc = 0;
3767 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003768
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003769 Py_DECREF(py_str);
3770 return rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003771}
3772
3773
3774static int
Tim Peterscba30e22003-02-01 06:24:36 +00003775load_binget(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003776{
3777 PyObject *py_key = 0, *value = 0;
3778 unsigned char key;
3779 char *s;
3780 int rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003781
Tim Peters0bc93f52003-02-02 18:29:33 +00003782 if (self->read_func(self, &s, 1) < 0) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003783
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003784 key = (unsigned char)s[0];
3785 if (!( py_key = PyInt_FromLong((long)key))) return -1;
Guido van Rossumea2b7152000-05-09 18:14:50 +00003786
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003787 value = PyDict_GetItem(self->memo, py_key);
3788 if (! value) {
3789 PyErr_SetObject(BadPickleGet, py_key);
3790 rc = -1;
Tim Peters92c8bb32003-02-13 23:00:26 +00003791 }
3792 else {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003793 PDATA_APPEND(self->stack, value, -1);
3794 rc = 0;
3795 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003796
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003797 Py_DECREF(py_key);
3798 return rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003799}
3800
3801
3802static int
Tim Peterscba30e22003-02-01 06:24:36 +00003803load_long_binget(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003804{
3805 PyObject *py_key = 0, *value = 0;
3806 unsigned char c;
3807 char *s;
3808 long key;
3809 int rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003810
Tim Peters0bc93f52003-02-02 18:29:33 +00003811 if (self->read_func(self, &s, 4) < 0) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003812
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003813 c = (unsigned char)s[0];
3814 key = (long)c;
3815 c = (unsigned char)s[1];
3816 key |= (long)c << 8;
3817 c = (unsigned char)s[2];
3818 key |= (long)c << 16;
3819 c = (unsigned char)s[3];
3820 key |= (long)c << 24;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003821
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003822 if (!( py_key = PyInt_FromLong((long)key))) return -1;
3823
3824 value = PyDict_GetItem(self->memo, py_key);
3825 if (! value) {
3826 PyErr_SetObject(BadPickleGet, py_key);
3827 rc = -1;
Tim Peters92c8bb32003-02-13 23:00:26 +00003828 }
3829 else {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003830 PDATA_APPEND(self->stack, value, -1);
3831 rc = 0;
3832 }
3833
3834 Py_DECREF(py_key);
3835 return rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003836}
3837
Tim Peters2d629652003-02-04 05:06:17 +00003838/* Push an object from the extension registry (EXT[124]). nbytes is
3839 * the number of bytes following the opcode, holding the index (code) value.
3840 */
3841static int
3842load_extension(Unpicklerobject *self, int nbytes)
3843{
3844 char *codebytes; /* the nbytes bytes after the opcode */
3845 long code; /* calc_binint returns long */
3846 PyObject *py_code; /* code as a Python int */
3847 PyObject *obj; /* the object to push */
3848 PyObject *pair; /* (module_name, class_name) */
3849 PyObject *module_name, *class_name;
3850
3851 assert(nbytes == 1 || nbytes == 2 || nbytes == 4);
3852 if (self->read_func(self, &codebytes, nbytes) < 0) return -1;
3853 code = calc_binint(codebytes, nbytes);
3854 if (code <= 0) { /* note that 0 is forbidden */
3855 /* Corrupt or hostile pickle. */
3856 PyErr_SetString(UnpicklingError, "EXT specifies code <= 0");
3857 return -1;
3858 }
3859
3860 /* Look for the code in the cache. */
3861 py_code = PyInt_FromLong(code);
3862 if (py_code == NULL) return -1;
3863 obj = PyDict_GetItem(extension_cache, py_code);
3864 if (obj != NULL) {
3865 /* Bingo. */
3866 Py_DECREF(py_code);
3867 PDATA_APPEND(self->stack, obj, -1);
3868 return 0;
3869 }
3870
3871 /* Look up the (module_name, class_name) pair. */
3872 pair = PyDict_GetItem(inverted_registry, py_code);
3873 if (pair == NULL) {
3874 Py_DECREF(py_code);
3875 PyErr_Format(PyExc_ValueError, "unregistered extension "
3876 "code %ld", code);
3877 return -1;
3878 }
3879 /* Since the extension registry is manipulable via Python code,
Tim Petersfa05ce32003-02-04 05:20:32 +00003880 * confirm that pair is really a 2-tuple of strings.
Tim Peters2d629652003-02-04 05:06:17 +00003881 */
3882 if (!PyTuple_Check(pair) || PyTuple_Size(pair) != 2 ||
3883 !PyString_Check(module_name = PyTuple_GET_ITEM(pair, 0)) ||
3884 !PyString_Check(class_name = PyTuple_GET_ITEM(pair, 1))) {
3885 Py_DECREF(py_code);
3886 PyErr_Format(PyExc_ValueError, "_inverted_registry[%ld] "
3887 "isn't a 2-tuple of strings", code);
3888 return -1;
3889 }
3890 /* Load the object. */
3891 obj = find_class(module_name, class_name, self->find_class);
3892 if (obj == NULL) {
3893 Py_DECREF(py_code);
3894 return -1;
3895 }
3896 /* Cache code -> obj. */
3897 code = PyDict_SetItem(extension_cache, py_code, obj);
3898 Py_DECREF(py_code);
3899 if (code < 0) {
3900 Py_DECREF(obj);
3901 return -1;
3902 }
3903 PDATA_PUSH(self->stack, obj, -1);
3904 return 0;
3905}
Guido van Rossum60456fd1997-04-09 17:36:32 +00003906
3907static int
Tim Peterscba30e22003-02-01 06:24:36 +00003908load_put(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003909{
3910 PyObject *py_str = 0, *value = 0;
3911 int len, l;
3912 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003913
Tim Peters0bc93f52003-02-02 18:29:33 +00003914 if ((l = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003915 if (l < 2) return bad_readline();
3916 if (!( len=self->stack->length )) return stackUnderflow();
3917 if (!( py_str = PyString_FromStringAndSize(s, l - 1))) return -1;
3918 value=self->stack->data[len-1];
3919 l=PyDict_SetItem(self->memo, py_str, value);
3920 Py_DECREF(py_str);
3921 return l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003922}
3923
3924
3925static int
Tim Peterscba30e22003-02-01 06:24:36 +00003926load_binput(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003927{
3928 PyObject *py_key = 0, *value = 0;
3929 unsigned char key;
3930 char *s;
3931 int len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003932
Tim Peters0bc93f52003-02-02 18:29:33 +00003933 if (self->read_func(self, &s, 1) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003934 if (!( (len=self->stack->length) > 0 )) return stackUnderflow();
Guido van Rossum60456fd1997-04-09 17:36:32 +00003935
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003936 key = (unsigned char)s[0];
Guido van Rossum053b8df1998-11-25 16:18:00 +00003937
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003938 if (!( py_key = PyInt_FromLong((long)key))) return -1;
3939 value=self->stack->data[len-1];
3940 len=PyDict_SetItem(self->memo, py_key, value);
3941 Py_DECREF(py_key);
3942 return len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003943}
3944
3945
3946static int
Tim Peterscba30e22003-02-01 06:24:36 +00003947load_long_binput(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003948{
3949 PyObject *py_key = 0, *value = 0;
3950 long key;
3951 unsigned char c;
3952 char *s;
3953 int len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003954
Tim Peters0bc93f52003-02-02 18:29:33 +00003955 if (self->read_func(self, &s, 4) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003956 if (!( len=self->stack->length )) return stackUnderflow();
Guido van Rossum60456fd1997-04-09 17:36:32 +00003957
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003958 c = (unsigned char)s[0];
3959 key = (long)c;
3960 c = (unsigned char)s[1];
3961 key |= (long)c << 8;
3962 c = (unsigned char)s[2];
3963 key |= (long)c << 16;
3964 c = (unsigned char)s[3];
3965 key |= (long)c << 24;
Tim Peters84e87f32001-03-17 04:50:51 +00003966
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003967 if (!( py_key = PyInt_FromLong(key))) return -1;
3968 value=self->stack->data[len-1];
3969 len=PyDict_SetItem(self->memo, py_key, value);
3970 Py_DECREF(py_key);
3971 return len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003972}
3973
3974
3975static int
Tim Peterscba30e22003-02-01 06:24:36 +00003976do_append(Unpicklerobject *self, int x)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003977{
3978 PyObject *value = 0, *list = 0, *append_method = 0;
3979 int len, i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003980
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003981 len=self->stack->length;
3982 if (!( len >= x && x > 0 )) return stackUnderflow();
3983 /* nothing to do */
Tim Peterscba30e22003-02-01 06:24:36 +00003984 if (len==x) return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003985
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003986 list=self->stack->data[x-1];
Guido van Rossum60456fd1997-04-09 17:36:32 +00003987
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003988 if (PyList_Check(list)) {
3989 PyObject *slice;
3990 int list_len;
Tim Peters84e87f32001-03-17 04:50:51 +00003991
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003992 slice=Pdata_popList(self->stack, x);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003993 if (! slice) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003994 list_len = PyList_GET_SIZE(list);
3995 i=PyList_SetSlice(list, list_len, list_len, slice);
3996 Py_DECREF(slice);
3997 return i;
3998 }
3999 else {
Guido van Rossum60456fd1997-04-09 17:36:32 +00004000
Tim Peterscba30e22003-02-01 06:24:36 +00004001 if (!( append_method = PyObject_GetAttr(list, append_str)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004002 return -1;
4003
4004 for (i = x; i < len; i++) {
4005 PyObject *junk;
4006
4007 value=self->stack->data[i];
4008 junk=0;
4009 ARG_TUP(self, value);
4010 if (self->arg) {
Tim Peterscba30e22003-02-01 06:24:36 +00004011 junk = PyObject_Call(append_method, self->arg,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004012 NULL);
4013 FREE_ARG_TUP(self);
4014 }
4015 if (! junk) {
4016 Pdata_clear(self->stack, i+1);
4017 self->stack->length=x;
4018 Py_DECREF(append_method);
4019 return -1;
4020 }
4021 Py_DECREF(junk);
4022 }
4023 self->stack->length=x;
4024 Py_DECREF(append_method);
4025 }
4026
4027 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004028}
4029
4030
4031static int
Tim Peterscba30e22003-02-01 06:24:36 +00004032load_append(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004033{
4034 return do_append(self, self->stack->length - 1);
Guido van Rossum60456fd1997-04-09 17:36:32 +00004035}
4036
4037
4038static int
Tim Peterscba30e22003-02-01 06:24:36 +00004039load_appends(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004040{
4041 return do_append(self, marker(self));
Guido van Rossum60456fd1997-04-09 17:36:32 +00004042}
4043
4044
4045static int
Tim Peterscba30e22003-02-01 06:24:36 +00004046do_setitems(Unpicklerobject *self, int x)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004047{
4048 PyObject *value = 0, *key = 0, *dict = 0;
4049 int len, i, r=0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004050
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004051 if (!( (len=self->stack->length) >= x
4052 && x > 0 )) return stackUnderflow();
Guido van Rossum60456fd1997-04-09 17:36:32 +00004053
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004054 dict=self->stack->data[x-1];
Guido van Rossum60456fd1997-04-09 17:36:32 +00004055
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004056 for (i = x+1; i < len; i += 2) {
4057 key =self->stack->data[i-1];
4058 value=self->stack->data[i ];
4059 if (PyObject_SetItem(dict, key, value) < 0) {
4060 r=-1;
4061 break;
4062 }
4063 }
4064
4065 Pdata_clear(self->stack, x);
4066
4067 return r;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004068}
4069
4070
Tim Peters84e87f32001-03-17 04:50:51 +00004071static int
Tim Peterscba30e22003-02-01 06:24:36 +00004072load_setitem(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004073{
4074 return do_setitems(self, self->stack->length - 2);
4075}
Guido van Rossum60456fd1997-04-09 17:36:32 +00004076
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004077static int
Tim Peterscba30e22003-02-01 06:24:36 +00004078load_setitems(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004079{
4080 return do_setitems(self, marker(self));
Guido van Rossum60456fd1997-04-09 17:36:32 +00004081}
4082
Tim Peters84e87f32001-03-17 04:50:51 +00004083
Guido van Rossum60456fd1997-04-09 17:36:32 +00004084static int
Tim Peterscba30e22003-02-01 06:24:36 +00004085load_build(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004086{
Tim Peters080c88b2003-02-15 03:01:11 +00004087 PyObject *state, *inst, *slotstate;
4088 PyObject *__setstate__;
4089 PyObject *d_key, *d_value;
Martin v. Löwis18e16552006-02-15 17:27:45 +00004090 Py_ssize_t i;
Tim Peters080c88b2003-02-15 03:01:11 +00004091 int res = -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004092
Tim Peters080c88b2003-02-15 03:01:11 +00004093 /* Stack is ... instance, state. We want to leave instance at
4094 * the stack top, possibly mutated via instance.__setstate__(state).
4095 */
4096 if (self->stack->length < 2)
4097 return stackUnderflow();
4098 PDATA_POP(self->stack, state);
4099 if (state == NULL)
4100 return -1;
4101 inst = self->stack->data[self->stack->length - 1];
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004102
Tim Peters080c88b2003-02-15 03:01:11 +00004103 __setstate__ = PyObject_GetAttr(inst, __setstate___str);
4104 if (__setstate__ != NULL) {
4105 PyObject *junk = NULL;
4106
4107 /* The explicit __setstate__ is responsible for everything. */
4108 ARG_TUP(self, state);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004109 if (self->arg) {
4110 junk = PyObject_Call(__setstate__, self->arg, NULL);
4111 FREE_ARG_TUP(self);
4112 }
4113 Py_DECREF(__setstate__);
Tim Peters080c88b2003-02-15 03:01:11 +00004114 if (junk == NULL)
4115 return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004116 Py_DECREF(junk);
4117 return 0;
4118 }
Jeremy Hyltonf00368f2003-06-16 20:19:49 +00004119 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4120 return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004121 PyErr_Clear();
Tim Peters080c88b2003-02-15 03:01:11 +00004122
4123 /* A default __setstate__. First see whether state embeds a
4124 * slot state dict too (a proto 2 addition).
4125 */
4126 if (PyTuple_Check(state) && PyTuple_Size(state) == 2) {
4127 PyObject *temp = state;
4128 state = PyTuple_GET_ITEM(temp, 0);
4129 slotstate = PyTuple_GET_ITEM(temp, 1);
4130 Py_INCREF(state);
4131 Py_INCREF(slotstate);
4132 Py_DECREF(temp);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004133 }
Tim Peters080c88b2003-02-15 03:01:11 +00004134 else
4135 slotstate = NULL;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004136
Tim Peters080c88b2003-02-15 03:01:11 +00004137 /* Set inst.__dict__ from the state dict (if any). */
4138 if (state != Py_None) {
4139 PyObject *dict;
4140 if (! PyDict_Check(state)) {
4141 PyErr_SetString(UnpicklingError, "state is not a "
4142 "dictionary");
4143 goto finally;
4144 }
4145 dict = PyObject_GetAttr(inst, __dict___str);
4146 if (dict == NULL)
4147 goto finally;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004148
Tim Peters080c88b2003-02-15 03:01:11 +00004149 i = 0;
4150 while (PyDict_Next(state, &i, &d_key, &d_value)) {
4151 if (PyObject_SetItem(dict, d_key, d_value) < 0)
4152 goto finally;
4153 }
4154 Py_DECREF(dict);
4155 }
4156
4157 /* Also set instance attributes from the slotstate dict (if any). */
4158 if (slotstate != NULL) {
4159 if (! PyDict_Check(slotstate)) {
4160 PyErr_SetString(UnpicklingError, "slot state is not "
4161 "a dictionary");
4162 goto finally;
4163 }
4164 i = 0;
4165 while (PyDict_Next(slotstate, &i, &d_key, &d_value)) {
4166 if (PyObject_SetAttr(inst, d_key, d_value) < 0)
4167 goto finally;
4168 }
4169 }
4170 res = 0;
4171
4172 finally:
4173 Py_DECREF(state);
4174 Py_XDECREF(slotstate);
4175 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004176}
4177
4178
4179static int
Tim Peterscba30e22003-02-01 06:24:36 +00004180load_mark(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004181{
4182 int s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004183
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004184 /* Note that we split the (pickle.py) stack into two stacks, an
4185 object stack and a mark stack. Here we push a mark onto the
4186 mark stack.
4187 */
Guido van Rossum60456fd1997-04-09 17:36:32 +00004188
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004189 if ((self->num_marks + 1) >= self->marks_size) {
4190 s=self->marks_size+20;
4191 if (s <= self->num_marks) s=self->num_marks + 1;
4192 if (self->marks == NULL)
4193 self->marks=(int *)malloc(s * sizeof(int));
4194 else
Tim Peterscba30e22003-02-01 06:24:36 +00004195 self->marks=(int *)realloc(self->marks,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004196 s * sizeof(int));
4197 if (! self->marks) {
4198 PyErr_NoMemory();
4199 return -1;
4200 }
4201 self->marks_size = s;
4202 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00004203
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004204 self->marks[self->num_marks++] = self->stack->length;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004205
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004206 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004207}
4208
Guido van Rossum60456fd1997-04-09 17:36:32 +00004209static int
Tim Peterscba30e22003-02-01 06:24:36 +00004210load_reduce(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004211{
4212 PyObject *callable = 0, *arg_tup = 0, *ob = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004213
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004214 PDATA_POP(self->stack, arg_tup);
4215 if (! arg_tup) return -1;
4216 PDATA_POP(self->stack, callable);
Guido van Rossum512ab9f2006-08-17 22:28:49 +00004217 if (callable) {
4218 ob = Instance_New(callable, arg_tup);
4219 Py_DECREF(callable);
4220 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004221 Py_DECREF(arg_tup);
Guido van Rossum60456fd1997-04-09 17:36:32 +00004222
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004223 if (! ob) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004224
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004225 PDATA_PUSH(self->stack, ob, -1);
4226 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004227}
Tim Peters84e87f32001-03-17 04:50:51 +00004228
Tim Peters4190fb82003-02-02 16:09:05 +00004229/* Just raises an error if we don't know the protocol specified. PROTO
4230 * is the first opcode for protocols >= 2.
4231 */
4232static int
4233load_proto(Unpicklerobject *self)
4234{
4235 int i;
4236 char *protobyte;
4237
4238 i = self->read_func(self, &protobyte, 1);
4239 if (i < 0)
4240 return -1;
4241
4242 i = calc_binint(protobyte, 1);
4243 /* No point checking for < 0, since calc_binint returns an unsigned
4244 * int when chewing on 1 byte.
4245 */
4246 assert(i >= 0);
Tim Peters8587b3c2003-02-13 15:44:41 +00004247 if (i <= HIGHEST_PROTOCOL)
Tim Peters4190fb82003-02-02 16:09:05 +00004248 return 0;
4249
4250 PyErr_Format(PyExc_ValueError, "unsupported pickle protocol: %d", i);
4251 return -1;
4252}
4253
Guido van Rossum60456fd1997-04-09 17:36:32 +00004254static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00004255load(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004256{
4257 PyObject *err = 0, *val = 0;
4258 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004259
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004260 self->num_marks = 0;
4261 if (self->stack->length) Pdata_clear(self->stack, 0);
Guido van Rossum60456fd1997-04-09 17:36:32 +00004262
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004263 while (1) {
Tim Peters0bc93f52003-02-02 18:29:33 +00004264 if (self->read_func(self, &s, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004265 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004266
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004267 switch (s[0]) {
4268 case NONE:
4269 if (load_none(self) < 0)
4270 break;
4271 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004272
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004273 case BININT:
4274 if (load_binint(self) < 0)
4275 break;
4276 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004277
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004278 case BININT1:
4279 if (load_binint1(self) < 0)
4280 break;
4281 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004282
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004283 case BININT2:
4284 if (load_binint2(self) < 0)
4285 break;
4286 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004287
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004288 case INT:
4289 if (load_int(self) < 0)
4290 break;
4291 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004292
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004293 case LONG:
4294 if (load_long(self) < 0)
4295 break;
4296 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004297
Tim Petersee1a53c2003-02-02 02:57:53 +00004298 case LONG1:
4299 if (load_counted_long(self, 1) < 0)
4300 break;
4301 continue;
4302
4303 case LONG4:
4304 if (load_counted_long(self, 4) < 0)
4305 break;
4306 continue;
4307
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004308 case FLOAT:
4309 if (load_float(self) < 0)
4310 break;
4311 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004312
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004313 case BINFLOAT:
4314 if (load_binfloat(self) < 0)
4315 break;
4316 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004317
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004318 case BINSTRING:
4319 if (load_binstring(self) < 0)
4320 break;
4321 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004322
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004323 case SHORT_BINSTRING:
4324 if (load_short_binstring(self) < 0)
4325 break;
4326 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004327
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004328 case STRING:
4329 if (load_string(self) < 0)
4330 break;
4331 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004332
Martin v. Löwis339d0f72001-08-17 18:39:25 +00004333#ifdef Py_USING_UNICODE
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004334 case UNICODE:
4335 if (load_unicode(self) < 0)
4336 break;
4337 continue;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00004338
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004339 case BINUNICODE:
4340 if (load_binunicode(self) < 0)
4341 break;
4342 continue;
Martin v. Löwis339d0f72001-08-17 18:39:25 +00004343#endif
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00004344
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004345 case EMPTY_TUPLE:
Tim Peters1d63c9f2003-02-02 20:29:39 +00004346 if (load_counted_tuple(self, 0) < 0)
4347 break;
4348 continue;
4349
4350 case TUPLE1:
4351 if (load_counted_tuple(self, 1) < 0)
4352 break;
4353 continue;
4354
4355 case TUPLE2:
4356 if (load_counted_tuple(self, 2) < 0)
4357 break;
4358 continue;
4359
4360 case TUPLE3:
4361 if (load_counted_tuple(self, 3) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004362 break;
4363 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004364
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004365 case TUPLE:
4366 if (load_tuple(self) < 0)
4367 break;
4368 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004369
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004370 case EMPTY_LIST:
4371 if (load_empty_list(self) < 0)
4372 break;
4373 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004374
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004375 case LIST:
4376 if (load_list(self) < 0)
4377 break;
4378 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004379
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004380 case EMPTY_DICT:
4381 if (load_empty_dict(self) < 0)
4382 break;
4383 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004384
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004385 case DICT:
4386 if (load_dict(self) < 0)
4387 break;
4388 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004389
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004390 case OBJ:
4391 if (load_obj(self) < 0)
4392 break;
4393 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004394
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004395 case INST:
4396 if (load_inst(self) < 0)
4397 break;
4398 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004399
Tim Peterseab7db32003-02-13 18:24:14 +00004400 case NEWOBJ:
4401 if (load_newobj(self) < 0)
4402 break;
4403 continue;
4404
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004405 case GLOBAL:
4406 if (load_global(self) < 0)
4407 break;
4408 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004409
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004410 case APPEND:
4411 if (load_append(self) < 0)
4412 break;
4413 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004414
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004415 case APPENDS:
4416 if (load_appends(self) < 0)
4417 break;
4418 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00004419
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004420 case BUILD:
4421 if (load_build(self) < 0)
4422 break;
4423 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00004424
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004425 case DUP:
4426 if (load_dup(self) < 0)
4427 break;
4428 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004429
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004430 case BINGET:
4431 if (load_binget(self) < 0)
4432 break;
4433 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004434
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004435 case LONG_BINGET:
4436 if (load_long_binget(self) < 0)
4437 break;
4438 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00004439
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004440 case GET:
4441 if (load_get(self) < 0)
4442 break;
4443 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004444
Tim Peters2d629652003-02-04 05:06:17 +00004445 case EXT1:
4446 if (load_extension(self, 1) < 0)
4447 break;
4448 continue;
4449
4450 case EXT2:
4451 if (load_extension(self, 2) < 0)
4452 break;
4453 continue;
4454
4455 case EXT4:
4456 if (load_extension(self, 4) < 0)
4457 break;
4458 continue;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004459 case MARK:
4460 if (load_mark(self) < 0)
4461 break;
4462 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004463
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004464 case BINPUT:
4465 if (load_binput(self) < 0)
4466 break;
4467 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004468
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004469 case LONG_BINPUT:
4470 if (load_long_binput(self) < 0)
4471 break;
4472 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00004473
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004474 case PUT:
4475 if (load_put(self) < 0)
4476 break;
4477 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004478
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004479 case POP:
4480 if (load_pop(self) < 0)
4481 break;
4482 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004483
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004484 case POP_MARK:
4485 if (load_pop_mark(self) < 0)
4486 break;
4487 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004488
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004489 case SETITEM:
4490 if (load_setitem(self) < 0)
4491 break;
4492 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004493
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004494 case SETITEMS:
4495 if (load_setitems(self) < 0)
4496 break;
4497 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004498
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004499 case STOP:
4500 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004501
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004502 case PERSID:
4503 if (load_persid(self) < 0)
4504 break;
4505 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004506
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004507 case BINPERSID:
4508 if (load_binpersid(self) < 0)
4509 break;
4510 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004511
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004512 case REDUCE:
4513 if (load_reduce(self) < 0)
4514 break;
4515 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004516
Tim Peters4190fb82003-02-02 16:09:05 +00004517 case PROTO:
4518 if (load_proto(self) < 0)
4519 break;
4520 continue;
4521
Tim Peters3c67d792003-02-02 17:59:11 +00004522 case NEWTRUE:
4523 if (load_bool(self, Py_True) < 0)
4524 break;
4525 continue;
4526
4527 case NEWFALSE:
4528 if (load_bool(self, Py_False) < 0)
4529 break;
4530 continue;
4531
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004532 case '\0':
4533 /* end of file */
4534 PyErr_SetNone(PyExc_EOFError);
4535 break;
Tim Peterscba30e22003-02-01 06:24:36 +00004536
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004537 default:
Tim Peterscba30e22003-02-01 06:24:36 +00004538 cPickle_ErrFormat(UnpicklingError,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004539 "invalid load key, '%s'.",
4540 "c", s[0]);
4541 return NULL;
4542 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00004543
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004544 break;
4545 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00004546
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004547 if ((err = PyErr_Occurred())) {
4548 if (err == PyExc_EOFError) {
4549 PyErr_SetNone(PyExc_EOFError);
4550 }
4551 return NULL;
4552 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00004553
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004554 PDATA_POP(self->stack, val);
4555 return val;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004556}
Tim Peters84e87f32001-03-17 04:50:51 +00004557
Guido van Rossum60456fd1997-04-09 17:36:32 +00004558
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004559/* No-load functions to support noload, which is used to
4560 find persistent references. */
4561
4562static int
Tim Peterscba30e22003-02-01 06:24:36 +00004563noload_obj(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004564{
4565 int i;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004566
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004567 if ((i = marker(self)) < 0) return -1;
4568 return Pdata_clear(self->stack, i+1);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004569}
4570
4571
4572static int
Tim Peterscba30e22003-02-01 06:24:36 +00004573noload_inst(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004574{
4575 int i;
4576 char *s;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004577
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004578 if ((i = marker(self)) < 0) return -1;
4579 Pdata_clear(self->stack, i);
Tim Peters0bc93f52003-02-02 18:29:33 +00004580 if (self->readline_func(self, &s) < 0) return -1;
4581 if (self->readline_func(self, &s) < 0) return -1;
Tim Peterseab7db32003-02-13 18:24:14 +00004582 PDATA_APPEND(self->stack, Py_None, -1);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004583 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004584}
4585
4586static int
Tim Peterseab7db32003-02-13 18:24:14 +00004587noload_newobj(Unpicklerobject *self)
4588{
4589 PyObject *obj;
4590
4591 PDATA_POP(self->stack, obj); /* pop argtuple */
4592 if (obj == NULL) return -1;
4593 Py_DECREF(obj);
4594
4595 PDATA_POP(self->stack, obj); /* pop cls */
4596 if (obj == NULL) return -1;
4597 Py_DECREF(obj);
4598
4599 PDATA_APPEND(self->stack, Py_None, -1);
4600 return 0;
4601}
4602
4603static int
Tim Peterscba30e22003-02-01 06:24:36 +00004604noload_global(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004605{
4606 char *s;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004607
Tim Peters0bc93f52003-02-02 18:29:33 +00004608 if (self->readline_func(self, &s) < 0) return -1;
4609 if (self->readline_func(self, &s) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004610 PDATA_APPEND(self->stack, Py_None,-1);
4611 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004612}
4613
4614static int
Tim Peterscba30e22003-02-01 06:24:36 +00004615noload_reduce(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004616{
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004617
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004618 if (self->stack->length < 2) return stackUnderflow();
4619 Pdata_clear(self->stack, self->stack->length-2);
4620 PDATA_APPEND(self->stack, Py_None,-1);
4621 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004622}
4623
4624static int
4625noload_build(Unpicklerobject *self) {
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004626
Guido van Rossum053b8df1998-11-25 16:18:00 +00004627 if (self->stack->length < 1) return stackUnderflow();
4628 Pdata_clear(self->stack, self->stack->length-1);
Guido van Rossum50f385c1998-12-04 18:48:44 +00004629 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004630}
4631
Tim Peters2d629652003-02-04 05:06:17 +00004632static int
4633noload_extension(Unpicklerobject *self, int nbytes)
4634{
4635 char *codebytes;
4636
4637 assert(nbytes == 1 || nbytes == 2 || nbytes == 4);
4638 if (self->read_func(self, &codebytes, nbytes) < 0) return -1;
4639 PDATA_APPEND(self->stack, Py_None, -1);
4640 return 0;
4641}
4642
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004643
4644static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00004645noload(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004646{
4647 PyObject *err = 0, *val = 0;
4648 char *s;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004649
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004650 self->num_marks = 0;
4651 Pdata_clear(self->stack, 0);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004652
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004653 while (1) {
Tim Peters0bc93f52003-02-02 18:29:33 +00004654 if (self->read_func(self, &s, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004655 break;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004656
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004657 switch (s[0]) {
4658 case NONE:
4659 if (load_none(self) < 0)
4660 break;
4661 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004662
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004663 case BININT:
4664 if (load_binint(self) < 0)
4665 break;
4666 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004667
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004668 case BININT1:
4669 if (load_binint1(self) < 0)
4670 break;
4671 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004672
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004673 case BININT2:
4674 if (load_binint2(self) < 0)
4675 break;
4676 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004677
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004678 case INT:
4679 if (load_int(self) < 0)
4680 break;
4681 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004682
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004683 case LONG:
4684 if (load_long(self) < 0)
4685 break;
4686 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004687
Tim Peters4190fb82003-02-02 16:09:05 +00004688 case LONG1:
4689 if (load_counted_long(self, 1) < 0)
4690 break;
4691 continue;
4692
4693 case LONG4:
4694 if (load_counted_long(self, 4) < 0)
4695 break;
4696 continue;
4697
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004698 case FLOAT:
4699 if (load_float(self) < 0)
4700 break;
4701 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004702
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004703 case BINFLOAT:
4704 if (load_binfloat(self) < 0)
4705 break;
4706 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004707
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004708 case BINSTRING:
4709 if (load_binstring(self) < 0)
4710 break;
4711 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004712
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004713 case SHORT_BINSTRING:
4714 if (load_short_binstring(self) < 0)
4715 break;
4716 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004717
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004718 case STRING:
4719 if (load_string(self) < 0)
4720 break;
4721 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004722
Martin v. Löwis339d0f72001-08-17 18:39:25 +00004723#ifdef Py_USING_UNICODE
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004724 case UNICODE:
4725 if (load_unicode(self) < 0)
4726 break;
4727 continue;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00004728
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004729 case BINUNICODE:
4730 if (load_binunicode(self) < 0)
4731 break;
4732 continue;
Martin v. Löwis339d0f72001-08-17 18:39:25 +00004733#endif
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00004734
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004735 case EMPTY_TUPLE:
Tim Peters1d63c9f2003-02-02 20:29:39 +00004736 if (load_counted_tuple(self, 0) < 0)
4737 break;
4738 continue;
4739
4740 case TUPLE1:
4741 if (load_counted_tuple(self, 1) < 0)
4742 break;
4743 continue;
4744
4745 case TUPLE2:
4746 if (load_counted_tuple(self, 2) < 0)
4747 break;
4748 continue;
4749
4750 case TUPLE3:
4751 if (load_counted_tuple(self, 3) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004752 break;
4753 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004754
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004755 case TUPLE:
4756 if (load_tuple(self) < 0)
4757 break;
4758 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004759
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004760 case EMPTY_LIST:
4761 if (load_empty_list(self) < 0)
4762 break;
4763 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004764
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004765 case LIST:
4766 if (load_list(self) < 0)
4767 break;
4768 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004769
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004770 case EMPTY_DICT:
4771 if (load_empty_dict(self) < 0)
4772 break;
4773 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004774
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004775 case DICT:
4776 if (load_dict(self) < 0)
4777 break;
4778 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004779
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004780 case OBJ:
4781 if (noload_obj(self) < 0)
4782 break;
4783 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004784
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004785 case INST:
4786 if (noload_inst(self) < 0)
4787 break;
4788 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004789
Tim Peterseab7db32003-02-13 18:24:14 +00004790 case NEWOBJ:
4791 if (noload_newobj(self) < 0)
4792 break;
4793 continue;
4794
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004795 case GLOBAL:
4796 if (noload_global(self) < 0)
4797 break;
4798 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004799
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004800 case APPEND:
4801 if (load_append(self) < 0)
4802 break;
4803 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004804
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004805 case APPENDS:
4806 if (load_appends(self) < 0)
4807 break;
4808 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00004809
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004810 case BUILD:
4811 if (noload_build(self) < 0)
4812 break;
4813 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00004814
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004815 case DUP:
4816 if (load_dup(self) < 0)
4817 break;
4818 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004819
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004820 case BINGET:
4821 if (load_binget(self) < 0)
4822 break;
4823 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004824
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004825 case LONG_BINGET:
4826 if (load_long_binget(self) < 0)
4827 break;
4828 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00004829
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004830 case GET:
4831 if (load_get(self) < 0)
4832 break;
4833 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004834
Tim Peters2d629652003-02-04 05:06:17 +00004835 case EXT1:
4836 if (noload_extension(self, 1) < 0)
4837 break;
4838 continue;
4839
4840 case EXT2:
4841 if (noload_extension(self, 2) < 0)
4842 break;
4843 continue;
4844
4845 case EXT4:
4846 if (noload_extension(self, 4) < 0)
4847 break;
4848 continue;
4849
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004850 case MARK:
4851 if (load_mark(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 BINPUT:
4856 if (load_binput(self) < 0)
4857 break;
4858 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004859
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004860 case LONG_BINPUT:
4861 if (load_long_binput(self) < 0)
4862 break;
4863 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00004864
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004865 case PUT:
4866 if (load_put(self) < 0)
4867 break;
4868 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004869
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004870 case POP:
4871 if (load_pop(self) < 0)
4872 break;
4873 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004874
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004875 case POP_MARK:
4876 if (load_pop_mark(self) < 0)
4877 break;
4878 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004879
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004880 case SETITEM:
4881 if (load_setitem(self) < 0)
4882 break;
4883 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004884
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004885 case SETITEMS:
4886 if (load_setitems(self) < 0)
4887 break;
4888 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004889
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004890 case STOP:
4891 break;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004892
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004893 case PERSID:
4894 if (load_persid(self) < 0)
4895 break;
4896 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004897
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004898 case BINPERSID:
4899 if (load_binpersid(self) < 0)
4900 break;
4901 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004902
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004903 case REDUCE:
4904 if (noload_reduce(self) < 0)
4905 break;
4906 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004907
Tim Peters4190fb82003-02-02 16:09:05 +00004908 case PROTO:
4909 if (load_proto(self) < 0)
4910 break;
4911 continue;
4912
Tim Peters3c67d792003-02-02 17:59:11 +00004913 case NEWTRUE:
4914 if (load_bool(self, Py_True) < 0)
4915 break;
4916 continue;
4917
4918 case NEWFALSE:
4919 if (load_bool(self, Py_False) < 0)
4920 break;
4921 continue;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004922 default:
Tim Peterscba30e22003-02-01 06:24:36 +00004923 cPickle_ErrFormat(UnpicklingError,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004924 "invalid load key, '%s'.",
4925 "c", s[0]);
4926 return NULL;
4927 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004928
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004929 break;
4930 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004931
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004932 if ((err = PyErr_Occurred())) {
4933 if (err == PyExc_EOFError) {
4934 PyErr_SetNone(PyExc_EOFError);
4935 }
4936 return NULL;
4937 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004938
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004939 PDATA_POP(self->stack, val);
4940 return val;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004941}
Tim Peters84e87f32001-03-17 04:50:51 +00004942
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004943
Guido van Rossum60456fd1997-04-09 17:36:32 +00004944static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00004945Unpickler_load(Unpicklerobject *self, PyObject *unused)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004946{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004947 return load(self);
Guido van Rossum60456fd1997-04-09 17:36:32 +00004948}
4949
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004950static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00004951Unpickler_noload(Unpicklerobject *self, PyObject *unused)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004952{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004953 return noload(self);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004954}
4955
Guido van Rossum60456fd1997-04-09 17:36:32 +00004956
4957static struct PyMethodDef Unpickler_methods[] = {
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00004958 {"load", (PyCFunction)Unpickler_load, METH_NOARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00004959 PyDoc_STR("load() -- Load a pickle")
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004960 },
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00004961 {"noload", (PyCFunction)Unpickler_noload, METH_NOARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00004962 PyDoc_STR(
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004963 "noload() -- not load a pickle, but go through most of the motions\n"
4964 "\n"
4965 "This function can be used to read past a pickle without instantiating\n"
4966 "any objects or importing any modules. It can also be used to find all\n"
4967 "persistent references without instantiating any objects or importing\n"
Neal Norwitz200788c2002-08-13 22:20:41 +00004968 "any modules.\n")
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004969 },
Guido van Rossum60456fd1997-04-09 17:36:32 +00004970 {NULL, NULL} /* sentinel */
4971};
4972
4973
4974static Unpicklerobject *
Tim Peterscba30e22003-02-01 06:24:36 +00004975newUnpicklerobject(PyObject *f)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004976{
4977 Unpicklerobject *self;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004978
Jeremy Hylton7b5ce7f2003-04-09 21:25:30 +00004979 if (!( self = PyObject_GC_New(Unpicklerobject, &Unpicklertype)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004980 return NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004981
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004982 self->file = NULL;
4983 self->arg = NULL;
4984 self->stack = (Pdata*)Pdata_New();
4985 self->pers_func = NULL;
4986 self->last_string = NULL;
4987 self->marks = NULL;
4988 self->num_marks = 0;
4989 self->marks_size = 0;
4990 self->buf_size = 0;
4991 self->read = NULL;
4992 self->readline = NULL;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004993 self->find_class = NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004994
Tim Peterscba30e22003-02-01 06:24:36 +00004995 if (!( self->memo = PyDict_New()))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004996 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004997
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004998 if (!self->stack)
4999 goto err;
5000
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005001 Py_INCREF(f);
5002 self->file = f;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005003
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005004 /* Set read, readline based on type of f */
5005 if (PyFile_Check(f)) {
5006 self->fp = PyFile_AsFile(f);
5007 if (self->fp == NULL) {
Tim Peterscba30e22003-02-01 06:24:36 +00005008 PyErr_SetString(PyExc_ValueError,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005009 "I/O operation on closed file");
5010 goto err;
5011 }
5012 self->read_func = read_file;
5013 self->readline_func = readline_file;
Guido van Rossumc91fcaa1999-03-29 20:00:14 +00005014 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005015 else if (PycStringIO_InputCheck(f)) {
5016 self->fp = NULL;
5017 self->read_func = read_cStringIO;
5018 self->readline_func = readline_cStringIO;
5019 }
5020 else {
Guido van Rossum60456fd1997-04-09 17:36:32 +00005021
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005022 self->fp = NULL;
5023 self->read_func = read_other;
5024 self->readline_func = readline_other;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005025
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005026 if (!( (self->readline = PyObject_GetAttr(f, readline_str)) &&
5027 (self->read = PyObject_GetAttr(f, read_str)))) {
5028 PyErr_Clear();
Tim Peterscba30e22003-02-01 06:24:36 +00005029 PyErr_SetString( PyExc_TypeError,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005030 "argument must have 'read' and "
5031 "'readline' attributes" );
5032 goto err;
5033 }
5034 }
Jeremy Hylton7b5ce7f2003-04-09 21:25:30 +00005035 PyObject_GC_Track(self);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005036
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005037 return self;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005038
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005039 err:
5040 Py_DECREF((PyObject *)self);
5041 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005042}
5043
5044
5045static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00005046get_Unpickler(PyObject *self, PyObject *file)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005047{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005048 return (PyObject *)newUnpicklerobject(file);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005049}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005050
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005051
Guido van Rossum60456fd1997-04-09 17:36:32 +00005052static void
Tim Peterscba30e22003-02-01 06:24:36 +00005053Unpickler_dealloc(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005054{
Jeremy Hylton7b5ce7f2003-04-09 21:25:30 +00005055 PyObject_GC_UnTrack((PyObject *)self);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005056 Py_XDECREF(self->readline);
5057 Py_XDECREF(self->read);
5058 Py_XDECREF(self->file);
5059 Py_XDECREF(self->memo);
5060 Py_XDECREF(self->stack);
5061 Py_XDECREF(self->pers_func);
5062 Py_XDECREF(self->arg);
5063 Py_XDECREF(self->last_string);
Jeremy Hyltonfff093f2003-07-11 19:42:49 +00005064 Py_XDECREF(self->find_class);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005065
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005066 if (self->marks) {
5067 free(self->marks);
5068 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005069
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005070 if (self->buf_size) {
5071 free(self->buf);
5072 }
Tim Peters84e87f32001-03-17 04:50:51 +00005073
Tim Peters3cfe7542003-05-21 21:29:48 +00005074 self->ob_type->tp_free((PyObject *)self);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005075}
5076
Jeremy Hylton7b5ce7f2003-04-09 21:25:30 +00005077static int
5078Unpickler_traverse(Unpicklerobject *self, visitproc visit, void *arg)
5079{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005080 Py_VISIT(self->readline);
5081 Py_VISIT(self->read);
5082 Py_VISIT(self->file);
5083 Py_VISIT(self->memo);
5084 Py_VISIT(self->stack);
5085 Py_VISIT(self->pers_func);
5086 Py_VISIT(self->arg);
5087 Py_VISIT(self->last_string);
5088 Py_VISIT(self->find_class);
Jeremy Hylton7b5ce7f2003-04-09 21:25:30 +00005089 return 0;
5090}
5091
5092static int
5093Unpickler_clear(Unpicklerobject *self)
5094{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005095 Py_CLEAR(self->readline);
5096 Py_CLEAR(self->read);
5097 Py_CLEAR(self->file);
5098 Py_CLEAR(self->memo);
5099 Py_CLEAR(self->stack);
5100 Py_CLEAR(self->pers_func);
5101 Py_CLEAR(self->arg);
5102 Py_CLEAR(self->last_string);
5103 Py_CLEAR(self->find_class);
Jeremy Hylton7b5ce7f2003-04-09 21:25:30 +00005104 return 0;
5105}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005106
5107static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00005108Unpickler_getattr(Unpicklerobject *self, char *name)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005109{
5110 if (!strcmp(name, "persistent_load")) {
5111 if (!self->pers_func) {
5112 PyErr_SetString(PyExc_AttributeError, name);
5113 return NULL;
5114 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005115
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005116 Py_INCREF(self->pers_func);
5117 return self->pers_func;
5118 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005119
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005120 if (!strcmp(name, "find_global")) {
5121 if (!self->find_class) {
5122 PyErr_SetString(PyExc_AttributeError, name);
5123 return NULL;
5124 }
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00005125
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005126 Py_INCREF(self->find_class);
5127 return self->find_class;
5128 }
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00005129
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005130 if (!strcmp(name, "memo")) {
5131 if (!self->memo) {
5132 PyErr_SetString(PyExc_AttributeError, name);
5133 return NULL;
5134 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005135
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005136 Py_INCREF(self->memo);
5137 return self->memo;
5138 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005139
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005140 if (!strcmp(name, "UnpicklingError")) {
5141 Py_INCREF(UnpicklingError);
5142 return UnpicklingError;
5143 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005144
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005145 return Py_FindMethod(Unpickler_methods, (PyObject *)self, name);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005146}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005147
Guido van Rossum60456fd1997-04-09 17:36:32 +00005148
5149static int
Tim Peterscba30e22003-02-01 06:24:36 +00005150Unpickler_setattr(Unpicklerobject *self, char *name, PyObject *value)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005151{
Jeremy Hyltonce616e41998-08-13 23:13:52 +00005152
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005153 if (!strcmp(name, "persistent_load")) {
5154 Py_XDECREF(self->pers_func);
5155 self->pers_func = value;
5156 Py_XINCREF(value);
5157 return 0;
5158 }
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00005159
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005160 if (!strcmp(name, "find_global")) {
5161 Py_XDECREF(self->find_class);
5162 self->find_class = value;
5163 Py_XINCREF(value);
5164 return 0;
5165 }
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00005166
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005167 if (! value) {
5168 PyErr_SetString(PyExc_TypeError,
5169 "attribute deletion is not supported");
5170 return -1;
5171 }
Jeremy Hyltonce616e41998-08-13 23:13:52 +00005172
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005173 if (strcmp(name, "memo") == 0) {
5174 if (!PyDict_Check(value)) {
Tim Peterscba30e22003-02-01 06:24:36 +00005175 PyErr_SetString(PyExc_TypeError,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005176 "memo must be a dictionary");
5177 return -1;
5178 }
5179 Py_XDECREF(self->memo);
5180 self->memo = value;
5181 Py_INCREF(value);
5182 return 0;
5183 }
Jeremy Hyltonce616e41998-08-13 23:13:52 +00005184
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005185 PyErr_SetString(PyExc_AttributeError, name);
5186 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005187}
5188
Tim Peters5bd2a792003-02-01 16:45:06 +00005189/* ---------------------------------------------------------------------------
5190 * Module-level functions.
5191 */
Guido van Rossum60456fd1997-04-09 17:36:32 +00005192
Martin v. Löwis544f1192004-07-27 05:22:33 +00005193/* dump(obj, file, protocol=0). */
Guido van Rossum60456fd1997-04-09 17:36:32 +00005194static PyObject *
Martin v. Löwis544f1192004-07-27 05:22:33 +00005195cpm_dump(PyObject *self, PyObject *args, PyObject *kwds)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005196{
Martin v. Löwis15e62742006-02-27 16:46:16 +00005197 static char *kwlist[] = {"obj", "file", "protocol", NULL};
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005198 PyObject *ob, *file, *res = NULL;
5199 Picklerobject *pickler = 0;
Tim Peters5bd2a792003-02-01 16:45:06 +00005200 int proto = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005201
Martin v. Löwis544f1192004-07-27 05:22:33 +00005202 if (!( PyArg_ParseTupleAndKeywords(args, kwds, "OO|i", kwlist,
5203 &ob, &file, &proto)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005204 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005205
Tim Peters5bd2a792003-02-01 16:45:06 +00005206 if (!( pickler = newPicklerobject(file, proto)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005207 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005208
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005209 if (dump(pickler, ob) < 0)
5210 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005211
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005212 Py_INCREF(Py_None);
5213 res = Py_None;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005214
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005215 finally:
5216 Py_XDECREF(pickler);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005217
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005218 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005219}
5220
5221
Martin v. Löwis544f1192004-07-27 05:22:33 +00005222/* dumps(obj, protocol=0). */
Guido van Rossum60456fd1997-04-09 17:36:32 +00005223static PyObject *
Martin v. Löwis544f1192004-07-27 05:22:33 +00005224cpm_dumps(PyObject *self, PyObject *args, PyObject *kwds)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005225{
Martin v. Löwis15e62742006-02-27 16:46:16 +00005226 static char *kwlist[] = {"obj", "protocol", NULL};
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005227 PyObject *ob, *file = 0, *res = NULL;
5228 Picklerobject *pickler = 0;
Tim Peters5bd2a792003-02-01 16:45:06 +00005229 int proto = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005230
Martin v. Löwis544f1192004-07-27 05:22:33 +00005231 if (!( PyArg_ParseTupleAndKeywords(args, kwds, "O|i:dumps", kwlist,
5232 &ob, &proto)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005233 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005234
Tim Peterscba30e22003-02-01 06:24:36 +00005235 if (!( file = PycStringIO->NewOutput(128)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005236 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005237
Tim Peters5bd2a792003-02-01 16:45:06 +00005238 if (!( pickler = newPicklerobject(file, proto)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005239 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005240
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005241 if (dump(pickler, ob) < 0)
5242 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005243
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005244 res = PycStringIO->cgetvalue(file);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005245
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005246 finally:
5247 Py_XDECREF(pickler);
5248 Py_XDECREF(file);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005249
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005250 return res;
Tim Peters84e87f32001-03-17 04:50:51 +00005251}
5252
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005253
Tim Peters5bd2a792003-02-01 16:45:06 +00005254/* load(fileobj). */
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005255static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00005256cpm_load(PyObject *self, PyObject *ob)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005257{
5258 Unpicklerobject *unpickler = 0;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00005259 PyObject *res = NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005260
Tim Peterscba30e22003-02-01 06:24:36 +00005261 if (!( unpickler = newUnpicklerobject(ob)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005262 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005263
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005264 res = load(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005265
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005266 finally:
5267 Py_XDECREF(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005268
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005269 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005270}
5271
5272
Tim Peters5bd2a792003-02-01 16:45:06 +00005273/* loads(string) */
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005274static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00005275cpm_loads(PyObject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005276{
5277 PyObject *ob, *file = 0, *res = NULL;
5278 Unpicklerobject *unpickler = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005279
Tim Peterscba30e22003-02-01 06:24:36 +00005280 if (!( PyArg_ParseTuple(args, "S:loads", &ob)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005281 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005282
Tim Peterscba30e22003-02-01 06:24:36 +00005283 if (!( file = PycStringIO->NewInput(ob)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005284 goto finally;
Tim Peters84e87f32001-03-17 04:50:51 +00005285
Tim Peterscba30e22003-02-01 06:24:36 +00005286 if (!( unpickler = newUnpicklerobject(file)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005287 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005288
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005289 res = load(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005290
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005291 finally:
5292 Py_XDECREF(file);
5293 Py_XDECREF(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005294
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005295 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005296}
5297
5298
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005299PyDoc_STRVAR(Unpicklertype__doc__,
5300"Objects that know how to unpickle");
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005301
Guido van Rossum9716aaa1997-12-08 15:15:16 +00005302static PyTypeObject Unpicklertype = {
5303 PyObject_HEAD_INIT(NULL)
Jeremy Hylton7b5ce7f2003-04-09 21:25:30 +00005304 0, /*ob_size*/
5305 "cPickle.Unpickler", /*tp_name*/
5306 sizeof(Unpicklerobject), /*tp_basicsize*/
5307 0,
5308 (destructor)Unpickler_dealloc, /* tp_dealloc */
5309 0, /* tp_print */
5310 (getattrfunc)Unpickler_getattr, /* tp_getattr */
5311 (setattrfunc)Unpickler_setattr, /* tp_setattr */
5312 0, /* tp_compare */
5313 0, /* tp_repr */
5314 0, /* tp_as_number */
5315 0, /* tp_as_sequence */
5316 0, /* tp_as_mapping */
5317 0, /* tp_hash */
5318 0, /* tp_call */
5319 0, /* tp_str */
5320 0, /* tp_getattro */
5321 0, /* tp_setattro */
5322 0, /* tp_as_buffer */
5323 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
5324 Unpicklertype__doc__, /* tp_doc */
5325 (traverseproc)Unpickler_traverse, /* tp_traverse */
5326 (inquiry)Unpickler_clear, /* tp_clear */
Guido van Rossum9716aaa1997-12-08 15:15:16 +00005327};
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005328
Guido van Rossum60456fd1997-04-09 17:36:32 +00005329static struct PyMethodDef cPickle_methods[] = {
Martin v. Löwis544f1192004-07-27 05:22:33 +00005330 {"dump", (PyCFunction)cpm_dump, METH_VARARGS | METH_KEYWORDS,
5331 PyDoc_STR("dump(obj, file, protocol=0) -- "
Tim Peters5bd2a792003-02-01 16:45:06 +00005332 "Write an object in pickle format to the given file.\n"
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005333 "\n"
Tim Peters5bd2a792003-02-01 16:45:06 +00005334 "See the Pickler docstring for the meaning of optional argument proto.")
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005335 },
Tim Peters5bd2a792003-02-01 16:45:06 +00005336
Martin v. Löwis544f1192004-07-27 05:22:33 +00005337 {"dumps", (PyCFunction)cpm_dumps, METH_VARARGS | METH_KEYWORDS,
5338 PyDoc_STR("dumps(obj, protocol=0) -- "
Tim Peters5bd2a792003-02-01 16:45:06 +00005339 "Return a string containing an object in pickle format.\n"
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005340 "\n"
Tim Peters5bd2a792003-02-01 16:45:06 +00005341 "See the Pickler docstring for the meaning of optional argument proto.")
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005342 },
Tim Peters5bd2a792003-02-01 16:45:06 +00005343
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00005344 {"load", (PyCFunction)cpm_load, METH_O,
Neal Norwitz200788c2002-08-13 22:20:41 +00005345 PyDoc_STR("load(file) -- Load a pickle from the given file")},
Tim Peters5bd2a792003-02-01 16:45:06 +00005346
Neal Norwitzb0493252002-03-31 14:44:22 +00005347 {"loads", (PyCFunction)cpm_loads, METH_VARARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00005348 PyDoc_STR("loads(string) -- Load a pickle from the given string")},
Tim Peters5bd2a792003-02-01 16:45:06 +00005349
Martin v. Löwis544f1192004-07-27 05:22:33 +00005350 {"Pickler", (PyCFunction)get_Pickler, METH_VARARGS | METH_KEYWORDS,
5351 PyDoc_STR("Pickler(file, protocol=0) -- Create a pickler.\n"
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005352 "\n"
Tim Peters5bd2a792003-02-01 16:45:06 +00005353 "This takes a file-like object for writing a pickle data stream.\n"
5354 "The optional proto argument tells the pickler to use the given\n"
5355 "protocol; supported protocols are 0, 1, 2. The default\n"
5356 "protocol is 0, to be backwards compatible. (Protocol 0 is the\n"
5357 "only protocol that can be written to a file opened in text\n"
5358 "mode and read back successfully. When using a protocol higher\n"
5359 "than 0, make sure the file is opened in binary mode, both when\n"
5360 "pickling and unpickling.)\n"
5361 "\n"
5362 "Protocol 1 is more efficient than protocol 0; protocol 2 is\n"
5363 "more efficient than protocol 1.\n"
5364 "\n"
5365 "Specifying a negative protocol version selects the highest\n"
5366 "protocol version supported. The higher the protocol used, the\n"
5367 "more recent the version of Python needed to read the pickle\n"
5368 "produced.\n"
5369 "\n"
5370 "The file parameter must have a write() method that accepts a single\n"
5371 "string argument. It can thus be an open file object, a StringIO\n"
5372 "object, or any other custom object that meets this interface.\n")
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005373 },
Tim Peters5bd2a792003-02-01 16:45:06 +00005374
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00005375 {"Unpickler", (PyCFunction)get_Unpickler, METH_O,
Tim Peters5bd2a792003-02-01 16:45:06 +00005376 PyDoc_STR("Unpickler(file) -- Create an unpickler.")},
5377
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005378 { NULL, NULL }
5379};
5380
Guido van Rossum60456fd1997-04-09 17:36:32 +00005381static int
Tim Peterscba30e22003-02-01 06:24:36 +00005382init_stuff(PyObject *module_dict)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005383{
5384 PyObject *copy_reg, *t, *r;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005385
Martin v. Löwis43c9d8a2002-04-01 12:34:33 +00005386#define INIT_STR(S) if (!( S ## _str=PyString_InternFromString(#S))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005387
Tim Peters3cfe7542003-05-21 21:29:48 +00005388 if (PyType_Ready(&Unpicklertype) < 0)
5389 return -1;
5390 if (PyType_Ready(&Picklertype) < 0)
5391 return -1;
5392
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005393 INIT_STR(__class__);
5394 INIT_STR(__getinitargs__);
5395 INIT_STR(__dict__);
5396 INIT_STR(__getstate__);
5397 INIT_STR(__setstate__);
5398 INIT_STR(__name__);
5399 INIT_STR(__main__);
5400 INIT_STR(__reduce__);
Guido van Rossumb289b872003-02-19 01:45:13 +00005401 INIT_STR(__reduce_ex__);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005402 INIT_STR(write);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005403 INIT_STR(append);
5404 INIT_STR(read);
5405 INIT_STR(readline);
5406 INIT_STR(copy_reg);
5407 INIT_STR(dispatch_table);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005408
Tim Peterscba30e22003-02-01 06:24:36 +00005409 if (!( copy_reg = PyImport_ImportModule("copy_reg")))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005410 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005411
Tim Peters1f1b2d22003-02-01 02:16:37 +00005412 /* This is special because we want to use a different
5413 one in restricted mode. */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005414 dispatch_table = PyObject_GetAttr(copy_reg, dispatch_table_str);
Tim Peters5b7da392003-02-04 00:21:07 +00005415 if (!dispatch_table) return -1;
5416
5417 extension_registry = PyObject_GetAttrString(copy_reg,
Guido van Rossumd4b920c2003-02-04 01:54:49 +00005418 "_extension_registry");
Tim Peters5b7da392003-02-04 00:21:07 +00005419 if (!extension_registry) return -1;
5420
5421 inverted_registry = PyObject_GetAttrString(copy_reg,
Guido van Rossumd4b920c2003-02-04 01:54:49 +00005422 "_inverted_registry");
Tim Peters5b7da392003-02-04 00:21:07 +00005423 if (!inverted_registry) return -1;
5424
5425 extension_cache = PyObject_GetAttrString(copy_reg,
Guido van Rossumd4b920c2003-02-04 01:54:49 +00005426 "_extension_cache");
Tim Peters5b7da392003-02-04 00:21:07 +00005427 if (!extension_cache) return -1;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005428
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005429 Py_DECREF(copy_reg);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005430
Tim Peters731098b2003-02-04 20:56:09 +00005431 if (!(empty_tuple = PyTuple_New(0)))
5432 return -1;
5433
5434 two_tuple = PyTuple_New(2);
5435 if (two_tuple == NULL)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005436 return -1;
Tim Peters3e667d52003-02-04 21:47:44 +00005437 /* We use this temp container with no regard to refcounts, or to
5438 * keeping containees alive. Exempt from GC, because we don't
5439 * want anything looking at two_tuple() by magic.
5440 */
5441 PyObject_GC_UnTrack(two_tuple);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005442
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005443 /* Ugh */
5444 if (!( t=PyImport_ImportModule("__builtin__"))) return -1;
5445 if (PyDict_SetItemString(module_dict, "__builtins__", t) < 0)
5446 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005447
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005448 if (!( t=PyDict_New())) return -1;
5449 if (!( r=PyRun_String(
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005450 "def __str__(self):\n"
5451 " return self.args and ('%s' % self.args[0]) or '(what)'\n",
5452 Py_file_input,
5453 module_dict, t) )) return -1;
5454 Py_DECREF(r);
Guido van Rossumc03158b1999-06-09 15:23:31 +00005455
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005456 PickleError = PyErr_NewException("cPickle.PickleError", NULL, t);
Tim Peterscba30e22003-02-01 06:24:36 +00005457 if (!PickleError)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005458 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00005459
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005460 Py_DECREF(t);
Guido van Rossumc03158b1999-06-09 15:23:31 +00005461
Tim Peterscba30e22003-02-01 06:24:36 +00005462 PicklingError = PyErr_NewException("cPickle.PicklingError",
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005463 PickleError, NULL);
Tim Peterscba30e22003-02-01 06:24:36 +00005464 if (!PicklingError)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005465 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00005466
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005467 if (!( t=PyDict_New())) return -1;
5468 if (!( r=PyRun_String(
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005469 "def __str__(self):\n"
5470 " a=self.args\n"
5471 " a=a and type(a[0]) or '(what)'\n"
5472 " return 'Cannot pickle %s objects' % a\n"
5473 , Py_file_input,
5474 module_dict, t) )) return -1;
5475 Py_DECREF(r);
Tim Peters84e87f32001-03-17 04:50:51 +00005476
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005477 if (!( UnpickleableError = PyErr_NewException(
Tim Peterscba30e22003-02-01 06:24:36 +00005478 "cPickle.UnpickleableError", PicklingError, t)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005479 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00005480
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005481 Py_DECREF(t);
Guido van Rossumc03158b1999-06-09 15:23:31 +00005482
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005483 if (!( UnpicklingError = PyErr_NewException("cPickle.UnpicklingError",
Tim Peterscba30e22003-02-01 06:24:36 +00005484 PickleError, NULL)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005485 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00005486
Martin v. Löwis658009a2002-09-16 17:26:24 +00005487 if (!( BadPickleGet = PyErr_NewException("cPickle.BadPickleGet",
5488 UnpicklingError, NULL)))
5489 return -1;
Tim Peterscba30e22003-02-01 06:24:36 +00005490
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005491 if (PyDict_SetItemString(module_dict, "PickleError",
5492 PickleError) < 0)
5493 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00005494
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005495 if (PyDict_SetItemString(module_dict, "PicklingError",
5496 PicklingError) < 0)
5497 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00005498
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005499 if (PyDict_SetItemString(module_dict, "UnpicklingError",
5500 UnpicklingError) < 0)
5501 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005502
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005503 if (PyDict_SetItemString(module_dict, "UnpickleableError",
5504 UnpickleableError) < 0)
5505 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005506
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005507 if (PyDict_SetItemString(module_dict, "BadPickleGet",
5508 BadPickleGet) < 0)
5509 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00005510
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005511 PycString_IMPORT;
Guido van Rossum053b8df1998-11-25 16:18:00 +00005512
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005513 return 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005514}
5515
Mark Hammondfe51c6d2002-08-02 02:27:13 +00005516#ifndef PyMODINIT_FUNC /* declarations for DLL import/export */
5517#define PyMODINIT_FUNC void
Guido van Rossumf9ffb031999-02-04 14:54:04 +00005518#endif
Mark Hammondfe51c6d2002-08-02 02:27:13 +00005519PyMODINIT_FUNC
Tim Peterscba30e22003-02-01 06:24:36 +00005520initcPickle(void)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005521{
5522 PyObject *m, *d, *di, *v, *k;
Martin v. Löwis18e16552006-02-15 17:27:45 +00005523 Py_ssize_t i;
Tim Peters5b7da392003-02-04 00:21:07 +00005524 char *rev = "1.71"; /* XXX when does this change? */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005525 PyObject *format_version;
5526 PyObject *compatible_formats;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005527
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005528 Picklertype.ob_type = &PyType_Type;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005529 Unpicklertype.ob_type = &PyType_Type;
5530 PdataType.ob_type = &PyType_Type;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005531
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005532 /* Initialize some pieces. We need to do this before module creation,
Tim Peters5b7da392003-02-04 00:21:07 +00005533 * so we're forced to use a temporary dictionary. :(
5534 */
5535 di = PyDict_New();
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005536 if (!di) return;
5537 if (init_stuff(di) < 0) return;
Guido van Rossumebba4202000-09-07 14:35:37 +00005538
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005539 /* Create the module and add the functions */
5540 m = Py_InitModule4("cPickle", cPickle_methods,
5541 cPickle_module_documentation,
5542 (PyObject*)NULL,PYTHON_API_VERSION);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00005543 if (m == NULL)
5544 return;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005545
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005546 /* Add some symbolic constants to the module */
5547 d = PyModule_GetDict(m);
Tim Peters5b7da392003-02-04 00:21:07 +00005548 v = PyString_FromString(rev);
5549 PyDict_SetItemString(d, "__version__", v);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005550 Py_XDECREF(v);
Barry Warsaw93d29b61997-01-14 17:45:08 +00005551
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005552 /* Copy data from di. Waaa. */
5553 for (i=0; PyDict_Next(di, &i, &k, &v); ) {
5554 if (PyObject_SetItem(d, k, v) < 0) {
5555 Py_DECREF(di);
5556 return;
5557 }
5558 }
5559 Py_DECREF(di);
Guido van Rossumebba4202000-09-07 14:35:37 +00005560
Tim Peters8587b3c2003-02-13 15:44:41 +00005561 i = PyModule_AddIntConstant(m, "HIGHEST_PROTOCOL", HIGHEST_PROTOCOL);
5562 if (i < 0)
5563 return;
5564
Tim Peters5b7da392003-02-04 00:21:07 +00005565 /* These are purely informational; no code uses them. */
5566 /* File format version we write. */
5567 format_version = PyString_FromString("2.0");
5568 /* Format versions we can read. */
5569 compatible_formats = Py_BuildValue("[sssss]",
5570 "1.0", /* Original protocol 0 */
5571 "1.1", /* Protocol 0 + INST */
5572 "1.2", /* Original protocol 1 */
5573 "1.3", /* Protocol 1 + BINFLOAT */
Tim Peters0dd23aa2003-02-04 00:30:46 +00005574 "2.0"); /* Original protocol 2 */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005575 PyDict_SetItemString(d, "format_version", format_version);
5576 PyDict_SetItemString(d, "compatible_formats", compatible_formats);
5577 Py_XDECREF(format_version);
5578 Py_XDECREF(compatible_formats);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005579}