blob: 24c98ccb225602ff51ce27c95986a10d079137f2 [file] [log] [blame]
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001#include "Python.h"
2#include "cStringIO.h"
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003#include "structmember.h"
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005PyDoc_STRVAR(cPickle_module_documentation,
Tim Peters64c04d12003-02-01 06:27:59 +00006"C implementation and optimization of the Python pickle module.");
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007
Guido van Rossum142eeb81997-08-13 03:14:41 +00008#ifndef Py_eval_input
9#include <graminit.h>
10#define Py_eval_input eval_input
Guido van Rossumc6ef2041997-08-21 02:30:45 +000011#endif /* Py_eval_input */
Guido van Rossum142eeb81997-08-13 03:14:41 +000012
Guido van Rossum60456fd1997-04-09 17:36:32 +000013#define DEL_LIST_SLICE(list, from, to) (PyList_SetSlice(list, from, to, NULL))
Guido van Rossum2f4caa41997-01-06 22:59:08 +000014
Guido van Rossum60456fd1997-04-09 17:36:32 +000015#define WRITE_BUF_SIZE 256
16
Tim Peters5bd2a792003-02-01 16:45:06 +000017/* Bump this when new opcodes are added to the pickle protocol. */
Tim Peters8587b3c2003-02-13 15:44:41 +000018#define HIGHEST_PROTOCOL 2
Tim Peters5bd2a792003-02-01 16:45:06 +000019
Tim Peters797ec242003-02-01 06:22:36 +000020/*
21 * Pickle opcodes. These must be kept in synch with pickle.py. Extensive
22 * docs are in pickletools.py.
23 */
Guido van Rossum60456fd1997-04-09 17:36:32 +000024#define MARK '('
25#define STOP '.'
26#define POP '0'
27#define POP_MARK '1'
28#define DUP '2'
29#define FLOAT 'F'
Guido van Rossum60456fd1997-04-09 17:36:32 +000030#define BINFLOAT 'G'
Guido van Rossum60456fd1997-04-09 17:36:32 +000031#define INT 'I'
32#define BININT 'J'
33#define BININT1 'K'
34#define LONG 'L'
35#define BININT2 'M'
36#define NONE 'N'
37#define PERSID 'P'
38#define BINPERSID 'Q'
39#define REDUCE 'R'
40#define STRING 'S'
41#define BINSTRING 'T'
Guido van Rossum2f4caa41997-01-06 22:59:08 +000042#define SHORT_BINSTRING 'U'
Guido van Rossum5fccb7c2000-03-10 23:11:40 +000043#define UNICODE 'V'
44#define BINUNICODE 'X'
Guido van Rossum60456fd1997-04-09 17:36:32 +000045#define APPEND 'a'
46#define BUILD 'b'
47#define GLOBAL 'c'
48#define DICT 'd'
49#define EMPTY_DICT '}'
50#define APPENDS 'e'
51#define GET 'g'
52#define BINGET 'h'
53#define INST 'i'
54#define LONG_BINGET 'j'
55#define LIST 'l'
56#define EMPTY_LIST ']'
57#define OBJ 'o'
58#define PUT 'p'
59#define BINPUT 'q'
60#define LONG_BINPUT 'r'
61#define SETITEM 's'
62#define TUPLE 't'
63#define EMPTY_TUPLE ')'
64#define SETITEMS 'u'
Tim Peters797ec242003-02-01 06:22:36 +000065
66/* Protocol 2. */
67#define PROTO '\x80' /* identify pickle protocol */
68#define NEWOBJ '\x81' /* build object by applying cls.__new__ to argtuple */
69#define EXT1 '\x82' /* push object from extension registry; 1-byte index */
70#define EXT2 '\x83' /* ditto, but 2-byte index */
71#define EXT4 '\x84' /* ditto, but 4-byte index */
72#define TUPLE1 '\x85' /* build 1-tuple from stack top */
73#define TUPLE2 '\x86' /* build 2-tuple from two topmost stack items */
74#define TUPLE3 '\x87' /* build 3-tuple from three topmost stack items */
75#define NEWTRUE '\x88' /* push True */
76#define NEWFALSE '\x89' /* push False */
77#define LONG1 '\x8a' /* push long from < 256 bytes */
78#define LONG4 '\x8b' /* push really big long */
79
80/* There aren't opcodes -- they're ways to pickle bools before protocol 2,
81 * so that unpicklers written before bools were introduced unpickle them
82 * as ints, but unpicklers after can recognize that bools were intended.
83 * Note that protocol 2 added direct ways to pickle bools.
84 */
Jack Jansen3a967022002-06-26 20:40:42 +000085#undef TRUE
Guido van Rossume2763392002-04-05 19:30:08 +000086#define TRUE "I01\n"
Jack Jansen3a967022002-06-26 20:40:42 +000087#undef FALSE
Guido van Rossume2763392002-04-05 19:30:08 +000088#define FALSE "I00\n"
Guido van Rossum77f6a652002-04-03 22:41:51 +000089
Tim Peters1092d642003-02-11 21:06:20 +000090/* Keep in synch with pickle.Pickler._BATCHSIZE. This is how many elements
Tim Peters42f08ac2003-02-11 22:43:24 +000091 * batch_list/dict() pumps out before doing APPENDS/SETITEMS. Nothing will
92 * break if this gets out of synch with pickle.py, but it's unclear that
93 * would help anything either.
Tim Peters1092d642003-02-11 21:06:20 +000094 */
95#define BATCHSIZE 1000
96
Guido van Rossum60456fd1997-04-09 17:36:32 +000097static char MARKv = MARK;
Guido van Rossum2f4caa41997-01-06 22:59:08 +000098
Guido van Rossumc03158b1999-06-09 15:23:31 +000099static PyObject *PickleError;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000100static PyObject *PicklingError;
Guido van Rossumc03158b1999-06-09 15:23:31 +0000101static PyObject *UnpickleableError;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000102static PyObject *UnpicklingError;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000103static PyObject *BadPickleGet;
104
Tim Peters5b7da392003-02-04 00:21:07 +0000105/* As the name says, an empty tuple. */
Guido van Rossum60456fd1997-04-09 17:36:32 +0000106static PyObject *empty_tuple;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000107
Tim Peters5b7da392003-02-04 00:21:07 +0000108/* copy_reg.dispatch_table, {type_object: pickling_function} */
109static PyObject *dispatch_table;
110
111/* For EXT[124] opcodes. */
Guido van Rossumd4b920c2003-02-04 01:54:49 +0000112/* copy_reg._extension_registry, {(module_name, function_name): code} */
Tim Peters5b7da392003-02-04 00:21:07 +0000113static PyObject *extension_registry;
Guido van Rossumd4b920c2003-02-04 01:54:49 +0000114/* copy_reg._inverted_registry, {code: (module_name, function_name)} */
Tim Peters5b7da392003-02-04 00:21:07 +0000115static PyObject *inverted_registry;
Guido van Rossumd4b920c2003-02-04 01:54:49 +0000116/* copy_reg._extension_cache, {code: object} */
Tim Peters5b7da392003-02-04 00:21:07 +0000117static PyObject *extension_cache;
118
Tim Peters731098b2003-02-04 20:56:09 +0000119/* For looking up name pairs in copy_reg._extension_registry. */
120static PyObject *two_tuple;
121
Guido van Rossum60456fd1997-04-09 17:36:32 +0000122static PyObject *__class___str, *__getinitargs___str, *__dict___str,
123 *__getstate___str, *__setstate___str, *__name___str, *__reduce___str,
Guido van Rossumb289b872003-02-19 01:45:13 +0000124 *__reduce_ex___str,
Tim Peters1f1b2d22003-02-01 02:16:37 +0000125 *write_str, *append_str,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000126 *read_str, *readline_str, *__main___str,
Tim Peters1f1b2d22003-02-01 02:16:37 +0000127 *copy_reg_str, *dispatch_table_str;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000128
Guido van Rossum053b8df1998-11-25 16:18:00 +0000129/*************************************************************************
130 Internal Data type for pickle data. */
131
132typedef struct {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000133 PyObject_HEAD
Tim Peters1d63c9f2003-02-02 20:29:39 +0000134 int length; /* number of initial slots in data currently used */
135 int size; /* number of slots in data allocated */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000136 PyObject **data;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000137} Pdata;
138
Tim Peters84e87f32001-03-17 04:50:51 +0000139static void
Tim Peterscba30e22003-02-01 06:24:36 +0000140Pdata_dealloc(Pdata *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000141{
142 int i;
143 PyObject **p;
Tim Peterscba30e22003-02-01 06:24:36 +0000144
Tim Peters1d63c9f2003-02-02 20:29:39 +0000145 for (i = self->length, p = self->data; --i >= 0; p++) {
146 Py_DECREF(*p);
147 }
148 if (self->data)
149 free(self->data);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000150 PyObject_Del(self);
Guido van Rossum053b8df1998-11-25 16:18:00 +0000151}
152
153static PyTypeObject PdataType = {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000154 PyObject_HEAD_INIT(NULL) 0, "cPickle.Pdata", sizeof(Pdata), 0,
155 (destructor)Pdata_dealloc,
156 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0L,0L,0L,0L, ""
Guido van Rossum053b8df1998-11-25 16:18:00 +0000157};
158
159#define Pdata_Check(O) ((O)->ob_type == &PdataType)
160
161static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +0000162Pdata_New(void)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000163{
164 Pdata *self;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000165
Tim Peters1d63c9f2003-02-02 20:29:39 +0000166 if (!(self = PyObject_New(Pdata, &PdataType)))
167 return NULL;
168 self->size = 8;
169 self->length = 0;
170 self->data = malloc(self->size * sizeof(PyObject*));
171 if (self->data)
172 return (PyObject*)self;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000173 Py_DECREF(self);
174 return PyErr_NoMemory();
Guido van Rossum053b8df1998-11-25 16:18:00 +0000175}
176
Tim Peters84e87f32001-03-17 04:50:51 +0000177static int
Tim Peterscba30e22003-02-01 06:24:36 +0000178stackUnderflow(void)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000179{
180 PyErr_SetString(UnpicklingError, "unpickling stack underflow");
181 return -1;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000182}
183
Tim Peters1d63c9f2003-02-02 20:29:39 +0000184/* Retain only the initial clearto items. If clearto >= the current
185 * number of items, this is a (non-erroneous) NOP.
186 */
Guido van Rossum053b8df1998-11-25 16:18:00 +0000187static int
Tim Peterscba30e22003-02-01 06:24:36 +0000188Pdata_clear(Pdata *self, int clearto)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000189{
190 int i;
191 PyObject **p;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000192
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000193 if (clearto < 0) return stackUnderflow();
194 if (clearto >= self->length) return 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000195
Tim Peters1d63c9f2003-02-02 20:29:39 +0000196 for (i = self->length, p = self->data + clearto;
197 --i >= clearto;
198 p++) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000199 Py_CLEAR(*p);
Tim Peters1d63c9f2003-02-02 20:29:39 +0000200 }
201 self->length = clearto;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000202
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000203 return 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000204}
205
Tim Peters84e87f32001-03-17 04:50:51 +0000206static int
Tim Peterscba30e22003-02-01 06:24:36 +0000207Pdata_grow(Pdata *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000208{
Tim Peters1d63c9f2003-02-02 20:29:39 +0000209 int bigger;
210 size_t nbytes;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000211 PyObject **tmp;
Tim Peters1d63c9f2003-02-02 20:29:39 +0000212
Tim Peters1d63c9f2003-02-02 20:29:39 +0000213 bigger = self->size << 1;
Tim Peterse0a39072003-02-03 15:45:56 +0000214 if (bigger <= 0) /* was 0, or new value overflows */
Tim Peters1d63c9f2003-02-02 20:29:39 +0000215 goto nomemory;
216 if ((int)(size_t)bigger != bigger)
217 goto nomemory;
218 nbytes = (size_t)bigger * sizeof(PyObject *);
219 if (nbytes / sizeof(PyObject *) != (size_t)bigger)
220 goto nomemory;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000221 tmp = realloc(self->data, nbytes);
222 if (tmp == NULL)
Tim Peters1d63c9f2003-02-02 20:29:39 +0000223 goto nomemory;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000224 self->data = tmp;
Tim Peters1d63c9f2003-02-02 20:29:39 +0000225 self->size = bigger;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000226 return 0;
Tim Peters1d63c9f2003-02-02 20:29:39 +0000227
228 nomemory:
Tim Peters1d63c9f2003-02-02 20:29:39 +0000229 PyErr_NoMemory();
230 return -1;
Tim Peters84e87f32001-03-17 04:50:51 +0000231}
Guido van Rossum053b8df1998-11-25 16:18:00 +0000232
Tim Peterse0a39072003-02-03 15:45:56 +0000233/* D is a Pdata*. Pop the topmost element and store it into V, which
234 * must be an lvalue holding PyObject*. On stack underflow, UnpicklingError
Tim Peters1d63c9f2003-02-02 20:29:39 +0000235 * is raised and V is set to NULL. D and V may be evaluated several times.
236 */
237#define PDATA_POP(D, V) { \
Tim Peterse0a39072003-02-03 15:45:56 +0000238 if ((D)->length) \
239 (V) = (D)->data[--((D)->length)]; \
240 else { \
241 PyErr_SetString(UnpicklingError, "bad pickle data"); \
242 (V) = NULL; \
243 } \
Guido van Rossum053b8df1998-11-25 16:18:00 +0000244}
245
Tim Peterse0a39072003-02-03 15:45:56 +0000246/* PDATA_PUSH and PDATA_APPEND both push rvalue PyObject* O on to Pdata*
247 * D. If the Pdata stack can't be grown to hold the new value, both
248 * raise MemoryError and execute "return ER". The difference is in ownership
249 * of O after: _PUSH transfers ownership of O from the caller to the stack
250 * (no incref of O is done, and in case of error O is decrefed), while
251 * _APPEND pushes a new reference.
252 */
253
254/* Push O on stack D, giving ownership of O to the stack. */
255#define PDATA_PUSH(D, O, ER) { \
256 if (((Pdata*)(D))->length == ((Pdata*)(D))->size && \
257 Pdata_grow((Pdata*)(D)) < 0) { \
258 Py_DECREF(O); \
259 return ER; \
260 } \
261 ((Pdata*)(D))->data[((Pdata*)(D))->length++] = (O); \
262}
263
264/* Push O on stack D, pushing a new reference. */
265#define PDATA_APPEND(D, O, ER) { \
266 if (((Pdata*)(D))->length == ((Pdata*)(D))->size && \
267 Pdata_grow((Pdata*)(D)) < 0) \
268 return ER; \
269 Py_INCREF(O); \
270 ((Pdata*)(D))->data[((Pdata*)(D))->length++] = (O); \
271}
272
273
Guido van Rossum053b8df1998-11-25 16:18:00 +0000274static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +0000275Pdata_popTuple(Pdata *self, int start)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000276{
277 PyObject *r;
278 int i, j, l;
Tim Peterscba30e22003-02-01 06:24:36 +0000279
Tim Peters1d63c9f2003-02-02 20:29:39 +0000280 l = self->length-start;
281 r = PyTuple_New(l);
282 if (r == NULL)
283 return NULL;
284 for (i = start, j = 0 ; j < l; i++, j++)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000285 PyTuple_SET_ITEM(r, j, self->data[i]);
Tim Peterscba30e22003-02-01 06:24:36 +0000286
Tim Peters1d63c9f2003-02-02 20:29:39 +0000287 self->length = start;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000288 return r;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000289}
290
291static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +0000292Pdata_popList(Pdata *self, int start)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000293{
294 PyObject *r;
295 int i, j, l;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000296
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000297 l=self->length-start;
298 if (!( r=PyList_New(l))) return NULL;
299 for (i=start, j=0 ; j < l; i++, j++)
300 PyList_SET_ITEM(r, j, self->data[i]);
Guido van Rossum053b8df1998-11-25 16:18:00 +0000301
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000302 self->length=start;
303 return r;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000304}
305
Guido van Rossum053b8df1998-11-25 16:18:00 +0000306/*************************************************************************/
307
308#define ARG_TUP(self, o) { \
309 if (self->arg || (self->arg=PyTuple_New(1))) { \
310 Py_XDECREF(PyTuple_GET_ITEM(self->arg,0)); \
311 PyTuple_SET_ITEM(self->arg,0,o); \
312 } \
313 else { \
314 Py_DECREF(o); \
315 } \
316}
317
318#define FREE_ARG_TUP(self) { \
319 if (self->arg->ob_refcnt > 1) { \
320 Py_DECREF(self->arg); \
321 self->arg=NULL; \
322 } \
323 }
324
Thomas Wouters3b6448f2000-07-22 23:56:07 +0000325typedef struct Picklerobject {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000326 PyObject_HEAD
327 FILE *fp;
328 PyObject *write;
329 PyObject *file;
330 PyObject *memo;
331 PyObject *arg;
332 PyObject *pers_func;
333 PyObject *inst_pers_func;
Tim Peters797ec242003-02-01 06:22:36 +0000334
335 /* pickle protocol number, >= 0 */
336 int proto;
337
338 /* bool, true if proto > 0 */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000339 int bin;
Tim Peters797ec242003-02-01 06:22:36 +0000340
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000341 int fast; /* Fast mode doesn't save in memo, don't use if circ ref */
Martin v. Löwis5a395302002-08-04 08:20:23 +0000342 int nesting;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000343 int (*write_func)(struct Picklerobject *, const char *, Py_ssize_t);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000344 char *write_buf;
345 int buf_size;
346 PyObject *dispatch_table;
347 int fast_container; /* count nested container dumps */
348 PyObject *fast_memo;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000349} Picklerobject;
350
Barry Warsaw52acb492001-12-21 20:04:22 +0000351#ifndef PY_CPICKLE_FAST_LIMIT
352#define PY_CPICKLE_FAST_LIMIT 50
353#endif
Jeremy Hyltona0fb1772001-10-12 04:11:06 +0000354
Jeremy Hylton938ace62002-07-17 16:30:39 +0000355static PyTypeObject Picklertype;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000356
Thomas Wouters3b6448f2000-07-22 23:56:07 +0000357typedef struct Unpicklerobject {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000358 PyObject_HEAD
359 FILE *fp;
360 PyObject *file;
361 PyObject *readline;
362 PyObject *read;
363 PyObject *memo;
364 PyObject *arg;
365 Pdata *stack;
366 PyObject *mark;
367 PyObject *pers_func;
368 PyObject *last_string;
369 int *marks;
370 int num_marks;
371 int marks_size;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000372 Py_ssize_t (*read_func)(struct Unpicklerobject *, char **, Py_ssize_t);
373 Py_ssize_t (*readline_func)(struct Unpicklerobject *, char **);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000374 int buf_size;
375 char *buf;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000376 PyObject *find_class;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000377} Unpicklerobject;
Tim Peters84e87f32001-03-17 04:50:51 +0000378
Jeremy Hylton938ace62002-07-17 16:30:39 +0000379static PyTypeObject Unpicklertype;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000380
Thomas Wouters4789b3a2000-07-24 11:36:47 +0000381/* Forward decls that need the above structs */
382static int save(Picklerobject *, PyObject *, int);
383static int put2(Picklerobject *, PyObject *);
384
Guido van Rossumd385d591997-04-09 17:47:47 +0000385static
Guido van Rossum60456fd1997-04-09 17:36:32 +0000386PyObject *
Thomas Wouters3b6448f2000-07-22 23:56:07 +0000387cPickle_ErrFormat(PyObject *ErrType, char *stringformat, char *format, ...)
388{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000389 va_list va;
390 PyObject *args=0, *retval=0;
391 va_start(va, format);
Tim Peterscba30e22003-02-01 06:24:36 +0000392
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000393 if (format) args = Py_VaBuildValue(format, va);
394 va_end(va);
395 if (format && ! args) return NULL;
Tim Peterscba30e22003-02-01 06:24:36 +0000396 if (stringformat && !(retval=PyString_FromString(stringformat)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000397 return NULL;
Tim Peterscba30e22003-02-01 06:24:36 +0000398
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000399 if (retval) {
400 if (args) {
401 PyObject *v;
402 v=PyString_Format(retval, args);
403 Py_DECREF(retval);
404 Py_DECREF(args);
405 if (! v) return NULL;
406 retval=v;
407 }
408 }
409 else
410 if (args) retval=args;
411 else {
412 PyErr_SetObject(ErrType,Py_None);
413 return NULL;
414 }
415 PyErr_SetObject(ErrType,retval);
416 Py_DECREF(retval);
417 return NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000418}
419
Tim Peters84e87f32001-03-17 04:50:51 +0000420static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000421write_file(Picklerobject *self, const char *s, Py_ssize_t n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000422{
423 size_t nbyteswritten;
Tim Peters84e87f32001-03-17 04:50:51 +0000424
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000425 if (s == NULL) {
426 return 0;
427 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000428
Martin v. Löwis18e16552006-02-15 17:27:45 +0000429 if (n > INT_MAX) {
430 /* String too large */
431 return -1;
432 }
433
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000434 Py_BEGIN_ALLOW_THREADS
Jeremy Hylton7fa4bfa2002-07-18 20:58:57 +0000435 nbyteswritten = fwrite(s, sizeof(char), n, self->fp);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000436 Py_END_ALLOW_THREADS
Jeremy Hylton7fa4bfa2002-07-18 20:58:57 +0000437 if (nbyteswritten != (size_t)n) {
438 PyErr_SetFromErrno(PyExc_IOError);
439 return -1;
440 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000441
Martin v. Löwis18e16552006-02-15 17:27:45 +0000442 return (int)n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000443}
444
Tim Peters84e87f32001-03-17 04:50:51 +0000445static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000446write_cStringIO(Picklerobject *self, const char *s, Py_ssize_t n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000447{
448 if (s == NULL) {
449 return 0;
450 }
Tim Peterscba30e22003-02-01 06:24:36 +0000451
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000452 if (PycStringIO->cwrite((PyObject *)self->file, s, n) != n) {
453 return -1;
454 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000455
Martin v. Löwis18e16552006-02-15 17:27:45 +0000456 return (int)n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000457}
458
Tim Peters84e87f32001-03-17 04:50:51 +0000459static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000460write_none(Picklerobject *self, const char *s, Py_ssize_t n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000461{
462 if (s == NULL) return 0;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000463 if (n > INT_MAX) return -1;
464 return (int)n;
Guido van Rossum142eeb81997-08-13 03:14:41 +0000465}
466
Tim Peters84e87f32001-03-17 04:50:51 +0000467static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000468write_other(Picklerobject *self, const char *s, Py_ssize_t _n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000469{
470 PyObject *py_str = 0, *junk = 0;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000471 int n;
Tim Peterscba30e22003-02-01 06:24:36 +0000472
Martin v. Löwis18e16552006-02-15 17:27:45 +0000473 if (_n > INT_MAX)
474 return -1;
475 n = (int)_n;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000476 if (s == NULL) {
477 if (!( self->buf_size )) return 0;
Tim Peterscba30e22003-02-01 06:24:36 +0000478 py_str = PyString_FromStringAndSize(self->write_buf,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000479 self->buf_size);
Tim Peterscba30e22003-02-01 06:24:36 +0000480 if (!py_str)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000481 return -1;
482 }
483 else {
484 if (self->buf_size && (n + self->buf_size) > WRITE_BUF_SIZE) {
485 if (write_other(self, NULL, 0) < 0)
486 return -1;
487 }
Tim Peterscba30e22003-02-01 06:24:36 +0000488
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000489 if (n > WRITE_BUF_SIZE) {
490 if (!( py_str =
Tim Peterscba30e22003-02-01 06:24:36 +0000491 PyString_FromStringAndSize(s, n)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000492 return -1;
493 }
494 else {
495 memcpy(self->write_buf + self->buf_size, s, n);
496 self->buf_size += n;
497 return n;
498 }
499 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000500
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000501 if (self->write) {
502 /* object with write method */
503 ARG_TUP(self, py_str);
504 if (self->arg) {
505 junk = PyObject_Call(self->write, self->arg, NULL);
506 FREE_ARG_TUP(self);
507 }
508 if (junk) Py_DECREF(junk);
509 else return -1;
510 }
511 else
512 PDATA_PUSH(self->file, py_str, -1);
Guido van Rossum60456fd1997-04-09 17:36:32 +0000513
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000514 self->buf_size = 0;
515 return n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000516}
517
518
Martin v. Löwis18e16552006-02-15 17:27:45 +0000519static Py_ssize_t
520read_file(Unpicklerobject *self, char **s, Py_ssize_t n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000521{
522 size_t nbytesread;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000523
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000524 if (self->buf_size == 0) {
525 int size;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000526
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000527 size = ((n < 32) ? 32 : n);
Tim Petersee1a53c2003-02-02 02:57:53 +0000528 if (!( self->buf = (char *)malloc(size))) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000529 PyErr_NoMemory();
530 return -1;
531 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000532
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000533 self->buf_size = size;
534 }
535 else if (n > self->buf_size) {
Tim Petersee1a53c2003-02-02 02:57:53 +0000536 self->buf = (char *)realloc(self->buf, n);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000537 if (!self->buf) {
538 PyErr_NoMemory();
539 return -1;
540 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000541 self->buf_size = n;
542 }
Tim Peters84e87f32001-03-17 04:50:51 +0000543
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000544 Py_BEGIN_ALLOW_THREADS
545 nbytesread = fread(self->buf, sizeof(char), n, self->fp);
546 Py_END_ALLOW_THREADS
547 if (nbytesread != (size_t)n) {
548 if (feof(self->fp)) {
549 PyErr_SetNone(PyExc_EOFError);
550 return -1;
551 }
Tim Peterscba30e22003-02-01 06:24:36 +0000552
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000553 PyErr_SetFromErrno(PyExc_IOError);
554 return -1;
555 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000556
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000557 *s = self->buf;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000558
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000559 return n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000560}
561
562
Martin v. Löwis18e16552006-02-15 17:27:45 +0000563static Py_ssize_t
Tim Peterscba30e22003-02-01 06:24:36 +0000564readline_file(Unpicklerobject *self, char **s)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000565{
566 int i;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000567
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000568 if (self->buf_size == 0) {
Tim Petersee1a53c2003-02-02 02:57:53 +0000569 if (!( self->buf = (char *)malloc(40))) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000570 PyErr_NoMemory();
571 return -1;
572 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000573 self->buf_size = 40;
574 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000575
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000576 i = 0;
577 while (1) {
Tim Petersee1a53c2003-02-02 02:57:53 +0000578 int bigger;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000579 for (; i < (self->buf_size - 1); i++) {
Tim Peterscba30e22003-02-01 06:24:36 +0000580 if (feof(self->fp) ||
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000581 (self->buf[i] = getc(self->fp)) == '\n') {
582 self->buf[i + 1] = '\0';
583 *s = self->buf;
584 return i + 1;
585 }
586 }
Tim Petersee1a53c2003-02-02 02:57:53 +0000587 bigger = self->buf_size << 1;
588 if (bigger <= 0) { /* overflow */
589 PyErr_NoMemory();
590 return -1;
591 }
592 self->buf = (char *)realloc(self->buf, bigger);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000593 if (!self->buf) {
594 PyErr_NoMemory();
595 return -1;
596 }
Tim Petersee1a53c2003-02-02 02:57:53 +0000597 self->buf_size = bigger;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000598 }
Tim Peters84e87f32001-03-17 04:50:51 +0000599}
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000600
601
Martin v. Löwis18e16552006-02-15 17:27:45 +0000602static Py_ssize_t
603read_cStringIO(Unpicklerobject *self, char **s, Py_ssize_t n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000604{
605 char *ptr;
Tim Peterscba30e22003-02-01 06:24:36 +0000606
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000607 if (PycStringIO->cread((PyObject *)self->file, &ptr, n) != n) {
608 PyErr_SetNone(PyExc_EOFError);
609 return -1;
610 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000611
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000612 *s = ptr;
Tim Peterscba30e22003-02-01 06:24:36 +0000613
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000614 return n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000615}
616
617
Martin v. Löwis18e16552006-02-15 17:27:45 +0000618static Py_ssize_t
Tim Peterscba30e22003-02-01 06:24:36 +0000619readline_cStringIO(Unpicklerobject *self, char **s)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000620{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000621 Py_ssize_t n;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000622 char *ptr;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000623
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000624 if ((n = PycStringIO->creadline((PyObject *)self->file, &ptr)) < 0) {
625 return -1;
626 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000627
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000628 *s = ptr;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000629
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000630 return n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000631}
632
633
Martin v. Löwis18e16552006-02-15 17:27:45 +0000634static Py_ssize_t
635read_other(Unpicklerobject *self, char **s, Py_ssize_t n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000636{
637 PyObject *bytes, *str=0;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000638
Martin v. Löwis18e16552006-02-15 17:27:45 +0000639 if (!( bytes = PyInt_FromSsize_t(n))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000640
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000641 ARG_TUP(self, bytes);
642 if (self->arg) {
643 str = PyObject_Call(self->read, self->arg, NULL);
644 FREE_ARG_TUP(self);
645 }
646 if (! str) return -1;
Tim Peterscba30e22003-02-01 06:24:36 +0000647
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000648 Py_XDECREF(self->last_string);
649 self->last_string = str;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000650
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000651 if (! (*s = PyString_AsString(str))) return -1;
652 return n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000653}
654
655
Martin v. Löwis18e16552006-02-15 17:27:45 +0000656static Py_ssize_t
Tim Peterscba30e22003-02-01 06:24:36 +0000657readline_other(Unpicklerobject *self, char **s)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000658{
659 PyObject *str;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000660 Py_ssize_t str_size;
Tim Peterscba30e22003-02-01 06:24:36 +0000661
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000662 if (!( str = PyObject_CallObject(self->readline, empty_tuple))) {
663 return -1;
664 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000665
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000666 if ((str_size = PyString_Size(str)) < 0)
667 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000668
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000669 Py_XDECREF(self->last_string);
670 self->last_string = str;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000671
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000672 if (! (*s = PyString_AsString(str)))
673 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000674
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000675 return str_size;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000676}
677
Tim Petersee1a53c2003-02-02 02:57:53 +0000678/* Copy the first n bytes from s into newly malloc'ed memory, plus a
679 * trailing 0 byte. Return a pointer to that, or NULL if out of memory.
680 * The caller is responsible for free()'ing the return value.
681 */
Guido van Rossum60456fd1997-04-09 17:36:32 +0000682static char *
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000683pystrndup(const char *s, int n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000684{
Tim Petersee1a53c2003-02-02 02:57:53 +0000685 char *r = (char *)malloc(n+1);
686 if (r == NULL)
687 return (char*)PyErr_NoMemory();
688 memcpy(r, s, n);
689 r[n] = 0;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000690 return r;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000691}
692
693
694static int
Tim Peterscba30e22003-02-01 06:24:36 +0000695get(Picklerobject *self, PyObject *id)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000696{
697 PyObject *value, *mv;
698 long c_value;
699 char s[30];
700 size_t len;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000701
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000702 if (!( mv = PyDict_GetItem(self->memo, id))) {
703 PyErr_SetObject(PyExc_KeyError, id);
704 return -1;
705 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000706
Tim Peterscba30e22003-02-01 06:24:36 +0000707 if (!( value = PyTuple_GetItem(mv, 0)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000708 return -1;
Tim Peters84e87f32001-03-17 04:50:51 +0000709
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000710 if (!( PyInt_Check(value))) {
711 PyErr_SetString(PicklingError, "no int where int expected in memo");
712 return -1;
713 }
714 c_value = PyInt_AS_LONG((PyIntObject*)value);
Guido van Rossum60456fd1997-04-09 17:36:32 +0000715
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000716 if (!self->bin) {
717 s[0] = GET;
718 PyOS_snprintf(s + 1, sizeof(s) - 1, "%ld\n", c_value);
719 len = strlen(s);
720 }
721 else if (Pdata_Check(self->file)) {
722 if (write_other(self, NULL, 0) < 0) return -1;
723 PDATA_APPEND(self->file, mv, -1);
724 return 0;
725 }
726 else {
727 if (c_value < 256) {
728 s[0] = BINGET;
729 s[1] = (int)(c_value & 0xff);
730 len = 2;
731 }
732 else {
733 s[0] = LONG_BINGET;
734 s[1] = (int)(c_value & 0xff);
735 s[2] = (int)((c_value >> 8) & 0xff);
736 s[3] = (int)((c_value >> 16) & 0xff);
737 s[4] = (int)((c_value >> 24) & 0xff);
738 len = 5;
739 }
740 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000741
Tim Peters0bc93f52003-02-02 18:29:33 +0000742 if (self->write_func(self, s, len) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000743 return -1;
Tim Peterscba30e22003-02-01 06:24:36 +0000744
Jeremy Hyltona0fb1772001-10-12 04:11:06 +0000745 return 0;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000746}
Jeremy Hyltonce616e41998-08-13 23:13:52 +0000747
Guido van Rossum60456fd1997-04-09 17:36:32 +0000748
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000749static int
Tim Peterscba30e22003-02-01 06:24:36 +0000750put(Picklerobject *self, PyObject *ob)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000751{
Tim Peterscba30e22003-02-01 06:24:36 +0000752 if (ob->ob_refcnt < 2 || self->fast)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000753 return 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000754
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000755 return put2(self, ob);
756}
Guido van Rossum053b8df1998-11-25 16:18:00 +0000757
Guido van Rossum053b8df1998-11-25 16:18:00 +0000758
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000759static int
Tim Peterscba30e22003-02-01 06:24:36 +0000760put2(Picklerobject *self, PyObject *ob)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000761{
762 char c_str[30];
763 int p;
764 size_t len;
765 int res = -1;
766 PyObject *py_ob_id = 0, *memo_len = 0, *t = 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000767
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000768 if (self->fast)
769 return 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000770
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000771 if ((p = PyDict_Size(self->memo)) < 0)
772 goto finally;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000773
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000774 /* Make sure memo keys are positive! */
Tim Peters70b02d72003-02-02 17:26:40 +0000775 /* XXX Why?
776 * XXX And does "positive" really mean non-negative?
777 * XXX pickle.py starts with PUT index 0, not 1. This makes for
778 * XXX gratuitous differences between the pickling modules.
779 */
Tim Peterscba30e22003-02-01 06:24:36 +0000780 p++;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000781
Tim Peterscba30e22003-02-01 06:24:36 +0000782 if (!( py_ob_id = PyLong_FromVoidPtr(ob)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000783 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000784
Tim Peterscba30e22003-02-01 06:24:36 +0000785 if (!( memo_len = PyInt_FromLong(p)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000786 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000787
Tim Peterscba30e22003-02-01 06:24:36 +0000788 if (!( t = PyTuple_New(2)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000789 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000790
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000791 PyTuple_SET_ITEM(t, 0, memo_len);
792 Py_INCREF(memo_len);
793 PyTuple_SET_ITEM(t, 1, ob);
794 Py_INCREF(ob);
795
796 if (PyDict_SetItem(self->memo, py_ob_id, t) < 0)
797 goto finally;
798
799 if (!self->bin) {
800 c_str[0] = PUT;
801 PyOS_snprintf(c_str + 1, sizeof(c_str) - 1, "%d\n", p);
802 len = strlen(c_str);
803 }
804 else if (Pdata_Check(self->file)) {
805 if (write_other(self, NULL, 0) < 0) return -1;
806 PDATA_APPEND(self->file, memo_len, -1);
807 res=0; /* Job well done ;) */
808 goto finally;
809 }
810 else {
811 if (p >= 256) {
812 c_str[0] = LONG_BINPUT;
813 c_str[1] = (int)(p & 0xff);
814 c_str[2] = (int)((p >> 8) & 0xff);
815 c_str[3] = (int)((p >> 16) & 0xff);
816 c_str[4] = (int)((p >> 24) & 0xff);
817 len = 5;
818 }
819 else {
820 c_str[0] = BINPUT;
821 c_str[1] = p;
822 len = 2;
823 }
824 }
825
Tim Peters0bc93f52003-02-02 18:29:33 +0000826 if (self->write_func(self, c_str, len) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000827 goto finally;
828
829 res = 0;
830
831 finally:
832 Py_XDECREF(py_ob_id);
833 Py_XDECREF(memo_len);
834 Py_XDECREF(t);
835
836 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000837}
838
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000839static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +0000840whichmodule(PyObject *global, PyObject *global_name)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000841{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000842 Py_ssize_t i, j;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000843 PyObject *module = 0, *modules_dict = 0,
844 *global_name_attr = 0, *name = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000845
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000846 module = PyObject_GetAttrString(global, "__module__");
Jeremy Hyltonf00368f2003-06-16 20:19:49 +0000847 if (module)
848 return module;
849 if (PyErr_ExceptionMatches(PyExc_AttributeError))
850 PyErr_Clear();
851 else
852 return NULL;
Guido van Rossum45188231997-09-28 05:38:51 +0000853
Tim Peterscba30e22003-02-01 06:24:36 +0000854 if (!( modules_dict = PySys_GetObject("modules")))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000855 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000856
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000857 i = 0;
858 while ((j = PyDict_Next(modules_dict, &i, &name, &module))) {
Guido van Rossum142eeb81997-08-13 03:14:41 +0000859
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000860 if (PyObject_Compare(name, __main___str)==0) continue;
Tim Peters84e87f32001-03-17 04:50:51 +0000861
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000862 global_name_attr = PyObject_GetAttr(module, global_name);
863 if (!global_name_attr) {
Jeremy Hyltonf00368f2003-06-16 20:19:49 +0000864 if (PyErr_ExceptionMatches(PyExc_AttributeError))
865 PyErr_Clear();
866 else
867 return NULL;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000868 continue;
869 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000870
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000871 if (global_name_attr != global) {
872 Py_DECREF(global_name_attr);
873 continue;
874 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000875
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000876 Py_DECREF(global_name_attr);
Guido van Rossum60456fd1997-04-09 17:36:32 +0000877
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000878 break;
879 }
Guido van Rossum142eeb81997-08-13 03:14:41 +0000880
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000881 /* The following implements the rule in pickle.py added in 1.5
882 that used __main__ if no module is found. I don't actually
883 like this rule. jlf
884 */
885 if (!j) {
886 j=1;
887 name=__main___str;
888 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000889
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000890 Py_INCREF(name);
891 return name;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000892}
893
894
Guido van Rossum60456fd1997-04-09 17:36:32 +0000895static int
Jeremy Hylton499ab6a2001-10-15 21:37:58 +0000896fast_save_enter(Picklerobject *self, PyObject *obj)
897{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000898 /* if fast_container < 0, we're doing an error exit. */
899 if (++self->fast_container >= PY_CPICKLE_FAST_LIMIT) {
900 PyObject *key = NULL;
901 if (self->fast_memo == NULL) {
902 self->fast_memo = PyDict_New();
903 if (self->fast_memo == NULL) {
904 self->fast_container = -1;
905 return 0;
906 }
907 }
908 key = PyLong_FromVoidPtr(obj);
909 if (key == NULL)
910 return 0;
911 if (PyDict_GetItem(self->fast_memo, key)) {
Tim Peters4e52ca82002-12-07 02:43:28 +0000912 Py_DECREF(key);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000913 PyErr_Format(PyExc_ValueError,
Tim Peters92c8bb32003-02-13 23:00:26 +0000914 "fast mode: can't pickle cyclic objects "
915 "including object type %s at %p",
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000916 obj->ob_type->tp_name, obj);
917 self->fast_container = -1;
918 return 0;
919 }
920 if (PyDict_SetItem(self->fast_memo, key, Py_None) < 0) {
Tim Peters4e52ca82002-12-07 02:43:28 +0000921 Py_DECREF(key);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000922 self->fast_container = -1;
923 return 0;
924 }
Tim Peters4e52ca82002-12-07 02:43:28 +0000925 Py_DECREF(key);
Jeremy Hylton499ab6a2001-10-15 21:37:58 +0000926 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000927 return 1;
Jeremy Hylton499ab6a2001-10-15 21:37:58 +0000928}
929
Tim Peterscba30e22003-02-01 06:24:36 +0000930int
Jeremy Hylton499ab6a2001-10-15 21:37:58 +0000931fast_save_leave(Picklerobject *self, PyObject *obj)
932{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000933 if (self->fast_container-- >= PY_CPICKLE_FAST_LIMIT) {
934 PyObject *key = PyLong_FromVoidPtr(obj);
935 if (key == NULL)
936 return 0;
937 if (PyDict_DelItem(self->fast_memo, key) < 0) {
Tim Peters4e52ca82002-12-07 02:43:28 +0000938 Py_DECREF(key);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000939 return 0;
940 }
Tim Peters4e52ca82002-12-07 02:43:28 +0000941 Py_DECREF(key);
Jeremy Hylton499ab6a2001-10-15 21:37:58 +0000942 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000943 return 1;
Jeremy Hylton499ab6a2001-10-15 21:37:58 +0000944}
945
946static int
Tim Peterscba30e22003-02-01 06:24:36 +0000947save_none(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000948{
949 static char none = NONE;
Tim Peters0bc93f52003-02-02 18:29:33 +0000950 if (self->write_func(self, &none, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000951 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000952
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000953 return 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000954}
955
Guido van Rossum77f6a652002-04-03 22:41:51 +0000956static int
Tim Peterscba30e22003-02-01 06:24:36 +0000957save_bool(Picklerobject *self, PyObject *args)
Guido van Rossum77f6a652002-04-03 22:41:51 +0000958{
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000959 static const char *buf[2] = {FALSE, TRUE};
Guido van Rossume2763392002-04-05 19:30:08 +0000960 static char len[2] = {sizeof(FALSE)-1, sizeof(TRUE)-1};
Guido van Rossum77f6a652002-04-03 22:41:51 +0000961 long l = PyInt_AS_LONG((PyIntObject *)args);
962
Tim Peters3c67d792003-02-02 17:59:11 +0000963 if (self->proto >= 2) {
964 char opcode = l ? NEWTRUE : NEWFALSE;
965 if (self->write_func(self, &opcode, 1) < 0)
966 return -1;
967 }
968 else if (self->write_func(self, buf[l], len[l]) < 0)
Guido van Rossum77f6a652002-04-03 22:41:51 +0000969 return -1;
Guido van Rossum77f6a652002-04-03 22:41:51 +0000970 return 0;
971}
Tim Peters84e87f32001-03-17 04:50:51 +0000972
Guido van Rossum60456fd1997-04-09 17:36:32 +0000973static int
Tim Peterscba30e22003-02-01 06:24:36 +0000974save_int(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000975{
976 char c_str[32];
977 long l = PyInt_AS_LONG((PyIntObject *)args);
978 int len = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000979
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000980 if (!self->bin
Guido van Rossumde8d6d71997-05-13 18:00:44 +0000981#if SIZEOF_LONG > 4
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000982 || l > 0x7fffffffL
983 || l < -0x80000000L
Guido van Rossumde8d6d71997-05-13 18:00:44 +0000984#endif
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000985 ) {
986 /* Text-mode pickle, or long too big to fit in the 4-byte
987 * signed BININT format: store as a string.
988 */
989 c_str[0] = INT;
990 PyOS_snprintf(c_str + 1, sizeof(c_str) - 1, "%ld\n", l);
Tim Peters0bc93f52003-02-02 18:29:33 +0000991 if (self->write_func(self, c_str, strlen(c_str)) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000992 return -1;
993 }
994 else {
995 /* Binary pickle and l fits in a signed 4-byte int. */
996 c_str[1] = (int)( l & 0xff);
997 c_str[2] = (int)((l >> 8) & 0xff);
998 c_str[3] = (int)((l >> 16) & 0xff);
999 c_str[4] = (int)((l >> 24) & 0xff);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001000
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001001 if ((c_str[4] == 0) && (c_str[3] == 0)) {
1002 if (c_str[2] == 0) {
1003 c_str[0] = BININT1;
1004 len = 2;
1005 }
1006 else {
1007 c_str[0] = BININT2;
1008 len = 3;
1009 }
1010 }
1011 else {
1012 c_str[0] = BININT;
1013 len = 5;
1014 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001015
Tim Peters0bc93f52003-02-02 18:29:33 +00001016 if (self->write_func(self, c_str, len) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001017 return -1;
1018 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001019
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001020 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001021}
1022
1023
1024static int
Tim Peterscba30e22003-02-01 06:24:36 +00001025save_long(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001026{
Tim Petersee1a53c2003-02-02 02:57:53 +00001027 int size;
1028 int res = -1;
1029 PyObject *repr = NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001030
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001031 static char l = LONG;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001032
Tim Petersee1a53c2003-02-02 02:57:53 +00001033 if (self->proto >= 2) {
1034 /* Linear-time pickling. */
1035 size_t nbits;
1036 size_t nbytes;
1037 unsigned char *pdata;
1038 char c_str[5];
1039 int i;
1040 int sign = _PyLong_Sign(args);
1041
1042 if (sign == 0) {
1043 /* It's 0 -- an empty bytestring. */
1044 c_str[0] = LONG1;
1045 c_str[1] = 0;
1046 i = self->write_func(self, c_str, 2);
1047 if (i < 0) goto finally;
1048 res = 0;
1049 goto finally;
1050 }
1051 nbits = _PyLong_NumBits(args);
1052 if (nbits == (size_t)-1 && PyErr_Occurred())
1053 goto finally;
1054 /* How many bytes do we need? There are nbits >> 3 full
1055 * bytes of data, and nbits & 7 leftover bits. If there
1056 * are any leftover bits, then we clearly need another
1057 * byte. Wnat's not so obvious is that we *probably*
1058 * need another byte even if there aren't any leftovers:
1059 * the most-significant bit of the most-significant byte
1060 * acts like a sign bit, and it's usually got a sense
1061 * opposite of the one we need. The exception is longs
1062 * of the form -(2**(8*j-1)) for j > 0. Such a long is
1063 * its own 256's-complement, so has the right sign bit
1064 * even without the extra byte. That's a pain to check
1065 * for in advance, though, so we always grab an extra
1066 * byte at the start, and cut it back later if possible.
1067 */
1068 nbytes = (nbits >> 3) + 1;
1069 if ((int)nbytes < 0 || (size_t)(int)nbytes != nbytes) {
1070 PyErr_SetString(PyExc_OverflowError, "long too large "
1071 "to pickle");
1072 goto finally;
1073 }
1074 repr = PyString_FromStringAndSize(NULL, (int)nbytes);
1075 if (repr == NULL) goto finally;
1076 pdata = (unsigned char *)PyString_AS_STRING(repr);
1077 i = _PyLong_AsByteArray((PyLongObject *)args,
1078 pdata, nbytes,
1079 1 /* little endian */, 1 /* signed */);
1080 if (i < 0) goto finally;
1081 /* If the long is negative, this may be a byte more than
1082 * needed. This is so iff the MSB is all redundant sign
1083 * bits.
1084 */
1085 if (sign < 0 && nbytes > 1 && pdata[nbytes - 1] == 0xff &&
1086 (pdata[nbytes - 2] & 0x80) != 0)
1087 --nbytes;
1088
1089 if (nbytes < 256) {
1090 c_str[0] = LONG1;
1091 c_str[1] = (char)nbytes;
1092 size = 2;
1093 }
1094 else {
1095 c_str[0] = LONG4;
1096 size = (int)nbytes;
1097 for (i = 1; i < 5; i++) {
1098 c_str[i] = (char)(size & 0xff);
1099 size >>= 8;
1100 }
1101 size = 5;
1102 }
1103 i = self->write_func(self, c_str, size);
1104 if (i < 0) goto finally;
1105 i = self->write_func(self, (char *)pdata, (int)nbytes);
1106 if (i < 0) goto finally;
1107 res = 0;
1108 goto finally;
1109 }
1110
1111 /* proto < 2: write the repr and newline. This is quadratic-time
1112 * (in the number of digits), in both directions.
1113 */
Tim Peterscba30e22003-02-01 06:24:36 +00001114 if (!( repr = PyObject_Repr(args)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001115 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001116
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001117 if ((size = PyString_Size(repr)) < 0)
1118 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001119
Tim Peters0bc93f52003-02-02 18:29:33 +00001120 if (self->write_func(self, &l, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001121 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001122
Tim Peters0bc93f52003-02-02 18:29:33 +00001123 if (self->write_func(self,
1124 PyString_AS_STRING((PyStringObject *)repr),
1125 size) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001126 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001127
Tim Peters0bc93f52003-02-02 18:29:33 +00001128 if (self->write_func(self, "\n", 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001129 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001130
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001131 res = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001132
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001133 finally:
1134 Py_XDECREF(repr);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001135 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001136}
1137
1138
1139static int
Tim Peterscba30e22003-02-01 06:24:36 +00001140save_float(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001141{
1142 double x = PyFloat_AS_DOUBLE((PyFloatObject *)args);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001143
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001144 if (self->bin) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001145 char str[9];
Tim Peters9905b942003-03-20 20:53:32 +00001146 str[0] = BINFLOAT;
1147 if (_PyFloat_Pack8(x, (unsigned char *)&str[1], 0) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001148 return -1;
Tim Peters0bc93f52003-02-02 18:29:33 +00001149 if (self->write_func(self, str, 9) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001150 return -1;
1151 }
1152 else {
1153 char c_str[250];
1154 c_str[0] = FLOAT;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001155 PyOS_ascii_formatd(c_str + 1, sizeof(c_str) - 2, "%.17g", x);
1156 /* Extend the formatted string with a newline character */
1157 strcat(c_str, "\n");
Guido van Rossum60456fd1997-04-09 17:36:32 +00001158
Tim Peters0bc93f52003-02-02 18:29:33 +00001159 if (self->write_func(self, c_str, strlen(c_str)) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001160 return -1;
1161 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001162
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001163 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001164}
1165
1166
1167static int
Tim Peterscba30e22003-02-01 06:24:36 +00001168save_string(Picklerobject *self, PyObject *args, int doput)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001169{
1170 int size, len;
1171 PyObject *repr=0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001172
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001173 if ((size = PyString_Size(args)) < 0)
1174 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001175
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001176 if (!self->bin) {
1177 char *repr_str;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001178
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001179 static char string = STRING;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001180
Tim Peterscba30e22003-02-01 06:24:36 +00001181 if (!( repr = PyObject_Repr(args)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001182 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001183
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001184 if ((len = PyString_Size(repr)) < 0)
1185 goto err;
1186 repr_str = PyString_AS_STRING((PyStringObject *)repr);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001187
Tim Peters0bc93f52003-02-02 18:29:33 +00001188 if (self->write_func(self, &string, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001189 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001190
Tim Peters0bc93f52003-02-02 18:29:33 +00001191 if (self->write_func(self, repr_str, len) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001192 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001193
Tim Peters0bc93f52003-02-02 18:29:33 +00001194 if (self->write_func(self, "\n", 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001195 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001196
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001197 Py_XDECREF(repr);
1198 }
1199 else {
1200 int i;
1201 char c_str[5];
Guido van Rossum60456fd1997-04-09 17:36:32 +00001202
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001203 if ((size = PyString_Size(args)) < 0)
1204 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001205
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001206 if (size < 256) {
1207 c_str[0] = SHORT_BINSTRING;
1208 c_str[1] = size;
1209 len = 2;
1210 }
1211 else {
1212 c_str[0] = BINSTRING;
1213 for (i = 1; i < 5; i++)
1214 c_str[i] = (int)(size >> ((i - 1) * 8));
1215 len = 5;
1216 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001217
Tim Peters0bc93f52003-02-02 18:29:33 +00001218 if (self->write_func(self, c_str, len) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001219 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001220
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001221 if (size > 128 && Pdata_Check(self->file)) {
1222 if (write_other(self, NULL, 0) < 0) return -1;
1223 PDATA_APPEND(self->file, args, -1);
1224 }
1225 else {
Tim Peters0bc93f52003-02-02 18:29:33 +00001226 if (self->write_func(self,
1227 PyString_AS_STRING(
1228 (PyStringObject *)args),
1229 size) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001230 return -1;
1231 }
1232 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001233
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001234 if (doput)
1235 if (put(self, args) < 0)
1236 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001237
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001238 return 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +00001239
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001240 err:
1241 Py_XDECREF(repr);
1242 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001243}
1244
1245
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001246#ifdef Py_USING_UNICODE
Guido van Rossumfb10c3f2000-12-19 02:08:38 +00001247/* A copy of PyUnicode_EncodeRawUnicodeEscape() that also translates
1248 backslash and newline characters to \uXXXX escapes. */
1249static PyObject *
1250modified_EncodeRawUnicodeEscape(const Py_UNICODE *s, int size)
1251{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001252 PyObject *repr;
1253 char *p;
1254 char *q;
Guido van Rossumfb10c3f2000-12-19 02:08:38 +00001255
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001256 static const char *hexdigit = "0123456789ABCDEF";
Guido van Rossumfb10c3f2000-12-19 02:08:38 +00001257
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001258 repr = PyString_FromStringAndSize(NULL, 6 * size);
1259 if (repr == NULL)
1260 return NULL;
1261 if (size == 0)
1262 return repr;
1263
1264 p = q = PyString_AS_STRING(repr);
1265 while (size-- > 0) {
1266 Py_UNICODE ch = *s++;
1267 /* Map 16-bit characters to '\uxxxx' */
1268 if (ch >= 256 || ch == '\\' || ch == '\n') {
1269 *p++ = '\\';
1270 *p++ = 'u';
1271 *p++ = hexdigit[(ch >> 12) & 0xf];
1272 *p++ = hexdigit[(ch >> 8) & 0xf];
1273 *p++ = hexdigit[(ch >> 4) & 0xf];
1274 *p++ = hexdigit[ch & 15];
1275 }
1276 /* Copy everything else as-is */
1277 else
1278 *p++ = (char) ch;
1279 }
1280 *p = '\0';
Tim Peters5de98422002-04-27 18:44:32 +00001281 _PyString_Resize(&repr, p - q);
Guido van Rossumfb10c3f2000-12-19 02:08:38 +00001282 return repr;
Guido van Rossumfb10c3f2000-12-19 02:08:38 +00001283}
1284
1285
Guido van Rossum60456fd1997-04-09 17:36:32 +00001286static int
Tim Peterscba30e22003-02-01 06:24:36 +00001287save_unicode(Picklerobject *self, PyObject *args, int doput)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001288{
1289 int size, len;
1290 PyObject *repr=0;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001291
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001292 if (!PyUnicode_Check(args))
1293 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001294
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001295 if (!self->bin) {
1296 char *repr_str;
1297 static char string = UNICODE;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001298
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001299 repr = modified_EncodeRawUnicodeEscape(
1300 PyUnicode_AS_UNICODE(args), PyUnicode_GET_SIZE(args));
Tim Peterscba30e22003-02-01 06:24:36 +00001301 if (!repr)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001302 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001303
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001304 if ((len = PyString_Size(repr)) < 0)
1305 goto err;
1306 repr_str = PyString_AS_STRING((PyStringObject *)repr);
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001307
Tim Peters0bc93f52003-02-02 18:29:33 +00001308 if (self->write_func(self, &string, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001309 goto err;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001310
Tim Peters0bc93f52003-02-02 18:29:33 +00001311 if (self->write_func(self, repr_str, len) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001312 goto err;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001313
Tim Peters0bc93f52003-02-02 18:29:33 +00001314 if (self->write_func(self, "\n", 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001315 goto err;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001316
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001317 Py_XDECREF(repr);
1318 }
1319 else {
1320 int i;
1321 char c_str[5];
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001322
Tim Peterscba30e22003-02-01 06:24:36 +00001323 if (!( repr = PyUnicode_AsUTF8String(args)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001324 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001325
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001326 if ((size = PyString_Size(repr)) < 0)
1327 goto err;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001328
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001329 c_str[0] = BINUNICODE;
1330 for (i = 1; i < 5; i++)
1331 c_str[i] = (int)(size >> ((i - 1) * 8));
1332 len = 5;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001333
Tim Peters0bc93f52003-02-02 18:29:33 +00001334 if (self->write_func(self, c_str, len) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001335 goto err;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001336
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001337 if (size > 128 && Pdata_Check(self->file)) {
1338 if (write_other(self, NULL, 0) < 0)
1339 goto err;
1340 PDATA_APPEND(self->file, repr, -1);
1341 }
1342 else {
Tim Peters0bc93f52003-02-02 18:29:33 +00001343 if (self->write_func(self, PyString_AS_STRING(repr),
1344 size) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001345 goto err;
1346 }
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001347
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001348 Py_DECREF(repr);
1349 }
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001350
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001351 if (doput)
1352 if (put(self, args) < 0)
1353 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001354
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001355 return 0;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001356
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001357 err:
1358 Py_XDECREF(repr);
1359 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001360}
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001361#endif
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001362
Tim Peters1d63c9f2003-02-02 20:29:39 +00001363/* A helper for save_tuple. Push the len elements in tuple t on the stack. */
1364static int
Tim Peters67920142003-02-05 03:46:17 +00001365store_tuple_elements(Picklerobject *self, PyObject *t, int len)
Tim Peters1d63c9f2003-02-02 20:29:39 +00001366{
1367 int i;
1368 int res = -1; /* guilty until proved innocent */
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001369
Tim Peters1d63c9f2003-02-02 20:29:39 +00001370 assert(PyTuple_Size(t) == len);
1371
1372 for (i = 0; i < len; i++) {
1373 PyObject *element = PyTuple_GET_ITEM(t, i);
1374
1375 if (element == NULL)
1376 goto finally;
1377 if (save(self, element, 0) < 0)
1378 goto finally;
1379 }
1380 res = 0;
1381
1382 finally:
1383 return res;
1384}
1385
1386/* Tuples are ubiquitous in the pickle protocols, so many techniques are
1387 * used across protocols to minimize the space needed to pickle them.
Tim Peters67920142003-02-05 03:46:17 +00001388 * Tuples are also the only builtin immutable type that can be recursive
Tim Peters1d63c9f2003-02-02 20:29:39 +00001389 * (a tuple can be reached from itself), and that requires some subtle
1390 * magic so that it works in all cases. IOW, this is a long routine.
1391 */
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001392static int
Tim Peterscba30e22003-02-01 06:24:36 +00001393save_tuple(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001394{
Tim Peters1d63c9f2003-02-02 20:29:39 +00001395 PyObject *py_tuple_id = NULL;
1396 int len, i;
1397 int res = -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001398
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001399 static char tuple = TUPLE;
Tim Peters1d63c9f2003-02-02 20:29:39 +00001400 static char pop = POP;
1401 static char pop_mark = POP_MARK;
1402 static char len2opcode[] = {EMPTY_TUPLE, TUPLE1, TUPLE2, TUPLE3};
Guido van Rossum60456fd1997-04-09 17:36:32 +00001403
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001404 if ((len = PyTuple_Size(args)) < 0)
1405 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001406
Tim Peters1d63c9f2003-02-02 20:29:39 +00001407 if (len == 0) {
1408 char c_str[2];
Tim Peters84e87f32001-03-17 04:50:51 +00001409
Tim Peters1d63c9f2003-02-02 20:29:39 +00001410 if (self->proto) {
1411 c_str[0] = EMPTY_TUPLE;
1412 len = 1;
1413 }
1414 else {
1415 c_str[0] = MARK;
1416 c_str[1] = TUPLE;
1417 len = 2;
1418 }
1419 if (self->write_func(self, c_str, len) >= 0)
1420 res = 0;
1421 /* Don't memoize an empty tuple. */
1422 goto finally;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001423 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001424
Tim Peters1d63c9f2003-02-02 20:29:39 +00001425 /* A non-empty tuple. */
1426
1427 /* id(tuple) isn't in the memo now. If it shows up there after
1428 * saving the tuple elements, the tuple must be recursive, in
1429 * which case we'll pop everything we put on the stack, and fetch
1430 * its value from the memo.
1431 */
1432 py_tuple_id = PyLong_FromVoidPtr(args);
1433 if (py_tuple_id == NULL)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001434 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001435
Tim Peters1d63c9f2003-02-02 20:29:39 +00001436 if (len <= 3 && self->proto >= 2) {
1437 /* Use TUPLE{1,2,3} opcodes. */
Tim Peters67920142003-02-05 03:46:17 +00001438 if (store_tuple_elements(self, args, len) < 0)
Tim Peters1d63c9f2003-02-02 20:29:39 +00001439 goto finally;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001440 if (PyDict_GetItem(self->memo, py_tuple_id)) {
Tim Peters1d63c9f2003-02-02 20:29:39 +00001441 /* pop the len elements */
1442 for (i = 0; i < len; ++i)
1443 if (self->write_func(self, &pop, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001444 goto finally;
Tim Peters1d63c9f2003-02-02 20:29:39 +00001445 /* fetch from memo */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001446 if (get(self, py_tuple_id) < 0)
1447 goto finally;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001448 res = 0;
1449 goto finally;
1450 }
Tim Peters1d63c9f2003-02-02 20:29:39 +00001451 /* Not recursive. */
1452 if (self->write_func(self, len2opcode + len, 1) < 0)
1453 goto finally;
1454 goto memoize;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001455 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001456
Tim Peters1d63c9f2003-02-02 20:29:39 +00001457 /* proto < 2 and len > 0, or proto >= 2 and len > 3.
1458 * Generate MARK elt1 elt2 ... TUPLE
1459 */
1460 if (self->write_func(self, &MARKv, 1) < 0)
1461 goto finally;
1462
Tim Peters67920142003-02-05 03:46:17 +00001463 if (store_tuple_elements(self, args, len) < 0)
Tim Peters1d63c9f2003-02-02 20:29:39 +00001464 goto finally;
1465
1466 if (PyDict_GetItem(self->memo, py_tuple_id)) {
1467 /* pop the stack stuff we pushed */
1468 if (self->bin) {
1469 if (self->write_func(self, &pop_mark, 1) < 0)
1470 goto finally;
1471 }
1472 else {
1473 /* Note that we pop one more than len, to remove
1474 * the MARK too.
1475 */
1476 for (i = 0; i <= len; i++)
1477 if (self->write_func(self, &pop, 1) < 0)
1478 goto finally;
1479 }
1480 /* fetch from memo */
1481 if (get(self, py_tuple_id) >= 0)
1482 res = 0;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001483 goto finally;
1484 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001485
Tim Peters1d63c9f2003-02-02 20:29:39 +00001486 /* Not recursive. */
1487 if (self->write_func(self, &tuple, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001488 goto finally;
Tim Peters84e87f32001-03-17 04:50:51 +00001489
Tim Peters1d63c9f2003-02-02 20:29:39 +00001490 memoize:
1491 if (put(self, args) >= 0)
1492 res = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001493
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001494 finally:
1495 Py_XDECREF(py_tuple_id);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001496 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001497}
1498
Tim Peters1092d642003-02-11 21:06:20 +00001499/* iter is an iterator giving items, and we batch up chunks of
1500 * MARK item item ... item APPENDS
1501 * opcode sequences. Calling code should have arranged to first create an
1502 * empty list, or list-like object, for the APPENDS to operate on.
1503 * Returns 0 on success, <0 on error.
1504 */
1505static int
1506batch_list(Picklerobject *self, PyObject *iter)
1507{
1508 PyObject *obj;
1509 PyObject *slice[BATCHSIZE];
1510 int i, n;
1511
1512 static char append = APPEND;
1513 static char appends = APPENDS;
1514
1515 assert(iter != NULL);
1516
1517 if (self->proto == 0) {
1518 /* APPENDS isn't available; do one at a time. */
1519 for (;;) {
1520 obj = PyIter_Next(iter);
1521 if (obj == NULL) {
1522 if (PyErr_Occurred())
1523 return -1;
1524 break;
1525 }
1526 i = save(self, obj, 0);
1527 Py_DECREF(obj);
1528 if (i < 0)
1529 return -1;
1530 if (self->write_func(self, &append, 1) < 0)
1531 return -1;
Tim Peters1092d642003-02-11 21:06:20 +00001532 }
1533 return 0;
1534 }
1535
1536 /* proto > 0: write in batches of BATCHSIZE. */
1537 do {
1538 /* Get next group of (no more than) BATCHSIZE elements. */
1539 for (n = 0; n < BATCHSIZE; ++n) {
1540 obj = PyIter_Next(iter);
1541 if (obj == NULL) {
1542 if (PyErr_Occurred())
1543 goto BatchFailed;
1544 break;
1545 }
1546 slice[n] = obj;
1547 }
1548
1549 if (n > 1) {
1550 /* Pump out MARK, slice[0:n], APPENDS. */
1551 if (self->write_func(self, &MARKv, 1) < 0)
1552 goto BatchFailed;
1553 for (i = 0; i < n; ++i) {
1554 if (save(self, slice[i], 0) < 0)
1555 goto BatchFailed;
1556 }
1557 if (self->write_func(self, &appends, 1) < 0)
1558 goto BatchFailed;
1559 }
1560 else if (n == 1) {
1561 if (save(self, slice[0], 0) < 0)
1562 goto BatchFailed;
1563 if (self->write_func(self, &append, 1) < 0)
1564 goto BatchFailed;
1565 }
1566
1567 for (i = 0; i < n; ++i) {
1568 Py_DECREF(slice[i]);
1569 }
Tim Peters90975f12003-02-12 05:28:58 +00001570 } while (n == BATCHSIZE);
Tim Peters1092d642003-02-11 21:06:20 +00001571 return 0;
1572
1573BatchFailed:
1574 while (--n >= 0) {
1575 Py_DECREF(slice[n]);
1576 }
1577 return -1;
1578}
1579
Guido van Rossum60456fd1997-04-09 17:36:32 +00001580static int
Tim Peterscba30e22003-02-01 06:24:36 +00001581save_list(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001582{
Tim Peters1092d642003-02-11 21:06:20 +00001583 int res = -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001584 char s[3];
Tim Peters1092d642003-02-11 21:06:20 +00001585 int len;
1586 PyObject *iter;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001587
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001588 if (self->fast && !fast_save_enter(self, args))
1589 goto finally;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00001590
Tim Peters1092d642003-02-11 21:06:20 +00001591 /* Create an empty list. */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001592 if (self->bin) {
1593 s[0] = EMPTY_LIST;
Tim Peters1092d642003-02-11 21:06:20 +00001594 len = 1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001595 }
1596 else {
1597 s[0] = MARK;
1598 s[1] = LIST;
Tim Peters1092d642003-02-11 21:06:20 +00001599 len = 2;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001600 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001601
Tim Peters1092d642003-02-11 21:06:20 +00001602 if (self->write_func(self, s, len) < 0)
1603 goto finally;
1604
1605 /* Get list length, and bow out early if empty. */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001606 if ((len = PyList_Size(args)) < 0)
1607 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001608
Tim Peters1092d642003-02-11 21:06:20 +00001609 /* Memoize. */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001610 if (len == 0) {
Tim Peters90975f12003-02-12 05:28:58 +00001611 if (put(self, args) >= 0)
1612 res = 0;
1613 goto finally;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001614 }
Tim Peters90975f12003-02-12 05:28:58 +00001615 if (put2(self, args) < 0)
1616 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001617
Tim Peters1092d642003-02-11 21:06:20 +00001618 /* Materialize the list elements. */
1619 iter = PyObject_GetIter(args);
1620 if (iter == NULL)
1621 goto finally;
1622 res = batch_list(self, iter);
1623 Py_DECREF(iter);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001624
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001625 finally:
1626 if (self->fast && !fast_save_leave(self, args))
1627 res = -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001628
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001629 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001630}
1631
1632
Tim Peters42f08ac2003-02-11 22:43:24 +00001633/* iter is an iterator giving (key, value) pairs, and we batch up chunks of
1634 * MARK key value ... key value SETITEMS
1635 * opcode sequences. Calling code should have arranged to first create an
1636 * empty dict, or dict-like object, for the SETITEMS to operate on.
1637 * Returns 0 on success, <0 on error.
1638 *
1639 * This is very much like batch_list(). The difference between saving
1640 * elements directly, and picking apart two-tuples, is so long-winded at
1641 * the C level, though, that attempts to combine these routines were too
1642 * ugly to bear.
1643 */
1644static int
1645batch_dict(Picklerobject *self, PyObject *iter)
1646{
1647 PyObject *p;
1648 PyObject *slice[BATCHSIZE];
1649 int i, n;
1650
1651 static char setitem = SETITEM;
1652 static char setitems = SETITEMS;
1653
1654 assert(iter != NULL);
1655
1656 if (self->proto == 0) {
1657 /* SETITEMS isn't available; do one at a time. */
1658 for (;;) {
1659 p = PyIter_Next(iter);
1660 if (p == NULL) {
1661 if (PyErr_Occurred())
1662 return -1;
1663 break;
1664 }
1665 if (!PyTuple_Check(p) || PyTuple_Size(p) != 2) {
1666 PyErr_SetString(PyExc_TypeError, "dict items "
1667 "iterator must return 2-tuples");
1668 return -1;
1669 }
1670 i = save(self, PyTuple_GET_ITEM(p, 0), 0);
1671 if (i >= 0)
1672 i = save(self, PyTuple_GET_ITEM(p, 1), 0);
1673 Py_DECREF(p);
1674 if (i < 0)
1675 return -1;
1676 if (self->write_func(self, &setitem, 1) < 0)
1677 return -1;
Tim Peters42f08ac2003-02-11 22:43:24 +00001678 }
1679 return 0;
1680 }
1681
1682 /* proto > 0: write in batches of BATCHSIZE. */
1683 do {
1684 /* Get next group of (no more than) BATCHSIZE elements. */
1685 for (n = 0; n < BATCHSIZE; ++n) {
1686 p = PyIter_Next(iter);
1687 if (p == NULL) {
1688 if (PyErr_Occurred())
1689 goto BatchFailed;
1690 break;
1691 }
1692 if (!PyTuple_Check(p) || PyTuple_Size(p) != 2) {
1693 PyErr_SetString(PyExc_TypeError, "dict items "
1694 "iterator must return 2-tuples");
1695 goto BatchFailed;
1696 }
1697 slice[n] = p;
1698 }
1699
1700 if (n > 1) {
1701 /* Pump out MARK, slice[0:n], SETITEMS. */
1702 if (self->write_func(self, &MARKv, 1) < 0)
1703 goto BatchFailed;
1704 for (i = 0; i < n; ++i) {
1705 p = slice[i];
1706 if (save(self, PyTuple_GET_ITEM(p, 0), 0) < 0)
1707 goto BatchFailed;
1708 if (save(self, PyTuple_GET_ITEM(p, 1), 0) < 0)
1709 goto BatchFailed;
1710 }
1711 if (self->write_func(self, &setitems, 1) < 0)
1712 goto BatchFailed;
1713 }
1714 else if (n == 1) {
1715 p = slice[0];
1716 if (save(self, PyTuple_GET_ITEM(p, 0), 0) < 0)
1717 goto BatchFailed;
1718 if (save(self, PyTuple_GET_ITEM(p, 1), 0) < 0)
1719 goto BatchFailed;
1720 if (self->write_func(self, &setitem, 1) < 0)
1721 goto BatchFailed;
1722 }
1723
1724 for (i = 0; i < n; ++i) {
1725 Py_DECREF(slice[i]);
1726 }
Tim Peters90975f12003-02-12 05:28:58 +00001727 } while (n == BATCHSIZE);
Tim Peters42f08ac2003-02-11 22:43:24 +00001728 return 0;
1729
1730BatchFailed:
1731 while (--n >= 0) {
1732 Py_DECREF(slice[n]);
1733 }
1734 return -1;
1735}
1736
Guido van Rossum60456fd1997-04-09 17:36:32 +00001737static int
Tim Peterscba30e22003-02-01 06:24:36 +00001738save_dict(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001739{
Tim Peters42f08ac2003-02-11 22:43:24 +00001740 int res = -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001741 char s[3];
Tim Peters42f08ac2003-02-11 22:43:24 +00001742 int len;
1743 PyObject *iter;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001744
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001745 if (self->fast && !fast_save_enter(self, args))
1746 goto finally;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00001747
Tim Peters42f08ac2003-02-11 22:43:24 +00001748 /* Create an empty dict. */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001749 if (self->bin) {
1750 s[0] = EMPTY_DICT;
1751 len = 1;
1752 }
1753 else {
1754 s[0] = MARK;
1755 s[1] = DICT;
1756 len = 2;
1757 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001758
Tim Peters0bc93f52003-02-02 18:29:33 +00001759 if (self->write_func(self, s, len) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001760 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001761
Tim Peters42f08ac2003-02-11 22:43:24 +00001762 /* Get dict size, and bow out early if empty. */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001763 if ((len = PyDict_Size(args)) < 0)
1764 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001765
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001766 if (len == 0) {
Tim Peters90975f12003-02-12 05:28:58 +00001767 if (put(self, args) >= 0)
1768 res = 0;
1769 goto finally;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001770 }
Tim Peters90975f12003-02-12 05:28:58 +00001771 if (put2(self, args) < 0)
1772 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001773
Tim Peters42f08ac2003-02-11 22:43:24 +00001774 /* Materialize the dict items. */
1775 iter = PyObject_CallMethod(args, "iteritems", "()");
1776 if (iter == NULL)
1777 goto finally;
1778 res = batch_dict(self, iter);
1779 Py_DECREF(iter);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001780
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001781 finally:
1782 if (self->fast && !fast_save_leave(self, args))
1783 res = -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001784
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001785 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001786}
1787
1788
Tim Peters84e87f32001-03-17 04:50:51 +00001789static int
Tim Peterscba30e22003-02-01 06:24:36 +00001790save_inst(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001791{
1792 PyObject *class = 0, *module = 0, *name = 0, *state = 0,
1793 *getinitargs_func = 0, *getstate_func = 0, *class_args = 0;
1794 char *module_str, *name_str;
1795 int module_size, name_size, res = -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001796
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001797 static char inst = INST, obj = OBJ, build = BUILD;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001798
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001799 if (self->fast && !fast_save_enter(self, args))
1800 goto finally;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00001801
Tim Peters0bc93f52003-02-02 18:29:33 +00001802 if (self->write_func(self, &MARKv, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001803 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001804
Tim Peterscba30e22003-02-01 06:24:36 +00001805 if (!( class = PyObject_GetAttr(args, __class___str)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001806 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001807
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001808 if (self->bin) {
1809 if (save(self, class, 0) < 0)
1810 goto finally;
1811 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001812
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001813 if ((getinitargs_func = PyObject_GetAttr(args, __getinitargs___str))) {
1814 PyObject *element = 0;
1815 int i, len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001816
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001817 if (!( class_args =
Tim Peterscba30e22003-02-01 06:24:36 +00001818 PyObject_Call(getinitargs_func, empty_tuple, NULL)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001819 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001820
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001821 if ((len = PyObject_Size(class_args)) < 0)
1822 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001823
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001824 for (i = 0; i < len; i++) {
Tim Peterscba30e22003-02-01 06:24:36 +00001825 if (!( element = PySequence_GetItem(class_args, i)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001826 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001827
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001828 if (save(self, element, 0) < 0) {
1829 Py_DECREF(element);
1830 goto finally;
1831 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001832
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001833 Py_DECREF(element);
1834 }
1835 }
1836 else {
Jeremy Hyltonf00368f2003-06-16 20:19:49 +00001837 if (PyErr_ExceptionMatches(PyExc_AttributeError))
1838 PyErr_Clear();
1839 else
1840 goto finally;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001841 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001842
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001843 if (!self->bin) {
1844 if (!( name = ((PyClassObject *)class)->cl_name )) {
1845 PyErr_SetString(PicklingError, "class has no name");
1846 goto finally;
1847 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001848
Tim Peterscba30e22003-02-01 06:24:36 +00001849 if (!( module = whichmodule(class, name)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001850 goto finally;
Guido van Rossum053b8df1998-11-25 16:18:00 +00001851
Tim Peters84e87f32001-03-17 04:50:51 +00001852
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001853 if ((module_size = PyString_Size(module)) < 0 ||
1854 (name_size = PyString_Size(name)) < 0)
1855 goto finally;
Guido van Rossum053b8df1998-11-25 16:18:00 +00001856
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001857 module_str = PyString_AS_STRING((PyStringObject *)module);
1858 name_str = PyString_AS_STRING((PyStringObject *)name);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001859
Tim Peters0bc93f52003-02-02 18:29:33 +00001860 if (self->write_func(self, &inst, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001861 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001862
Tim Peters0bc93f52003-02-02 18:29:33 +00001863 if (self->write_func(self, module_str, module_size) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001864 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001865
Tim Peters0bc93f52003-02-02 18:29:33 +00001866 if (self->write_func(self, "\n", 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001867 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001868
Tim Peters0bc93f52003-02-02 18:29:33 +00001869 if (self->write_func(self, name_str, name_size) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001870 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001871
Tim Peters0bc93f52003-02-02 18:29:33 +00001872 if (self->write_func(self, "\n", 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001873 goto finally;
1874 }
Tim Peters0bc93f52003-02-02 18:29:33 +00001875 else if (self->write_func(self, &obj, 1) < 0) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001876 goto finally;
1877 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001878
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001879 if ((getstate_func = PyObject_GetAttr(args, __getstate___str))) {
1880 state = PyObject_Call(getstate_func, empty_tuple, NULL);
Tim Peterscba30e22003-02-01 06:24:36 +00001881 if (!state)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001882 goto finally;
1883 }
1884 else {
Jeremy Hyltonf00368f2003-06-16 20:19:49 +00001885 if (PyErr_ExceptionMatches(PyExc_AttributeError))
1886 PyErr_Clear();
1887 else
1888 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001889
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001890 if (!( state = PyObject_GetAttr(args, __dict___str))) {
Jeremy Hyltonf00368f2003-06-16 20:19:49 +00001891 if (PyErr_ExceptionMatches(PyExc_AttributeError))
1892 PyErr_Clear();
1893 else
1894 goto finally;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001895 res = 0;
1896 goto finally;
1897 }
1898 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001899
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001900 if (!PyDict_Check(state)) {
1901 if (put2(self, args) < 0)
1902 goto finally;
1903 }
1904 else {
1905 if (put(self, args) < 0)
1906 goto finally;
1907 }
Tim Peters84e87f32001-03-17 04:50:51 +00001908
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001909 if (save(self, state, 0) < 0)
1910 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001911
Tim Peters0bc93f52003-02-02 18:29:33 +00001912 if (self->write_func(self, &build, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001913 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001914
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001915 res = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001916
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001917 finally:
1918 if (self->fast && !fast_save_leave(self, args))
1919 res = -1;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00001920
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001921 Py_XDECREF(module);
1922 Py_XDECREF(class);
1923 Py_XDECREF(state);
1924 Py_XDECREF(getinitargs_func);
1925 Py_XDECREF(getstate_func);
1926 Py_XDECREF(class_args);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001927
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001928 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001929}
1930
1931
Guido van Rossum60456fd1997-04-09 17:36:32 +00001932static int
Tim Peterscba30e22003-02-01 06:24:36 +00001933save_global(Picklerobject *self, PyObject *args, PyObject *name)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001934{
Jeremy Hylton9ee91f12002-07-11 22:02:33 +00001935 PyObject *global_name = 0, *module = 0, *mod = 0, *klass = 0;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001936 char *name_str, *module_str;
1937 int module_size, name_size, res = -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001938
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001939 static char global = GLOBAL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001940
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001941 if (name) {
1942 global_name = name;
1943 Py_INCREF(global_name);
1944 }
1945 else {
Tim Peterscba30e22003-02-01 06:24:36 +00001946 if (!( global_name = PyObject_GetAttr(args, __name___str)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001947 goto finally;
1948 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001949
Tim Peterscba30e22003-02-01 06:24:36 +00001950 if (!( module = whichmodule(args, global_name)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001951 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001952
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001953 if ((module_size = PyString_Size(module)) < 0 ||
1954 (name_size = PyString_Size(global_name)) < 0)
1955 goto finally;
Guido van Rossum053b8df1998-11-25 16:18:00 +00001956
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001957 module_str = PyString_AS_STRING((PyStringObject *)module);
1958 name_str = PyString_AS_STRING((PyStringObject *)global_name);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001959
Guido van Rossum75bfd052002-12-24 18:10:07 +00001960 /* XXX This can be doing a relative import. Clearly it shouldn't,
1961 but I don't know how to stop it. :-( */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001962 mod = PyImport_ImportModule(module_str);
1963 if (mod == NULL) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001964 cPickle_ErrFormat(PicklingError,
Tim Petersb9ce7cd2003-02-18 20:50:45 +00001965 "Can't pickle %s: import of module %s "
1966 "failed",
1967 "OS", args, module);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001968 goto finally;
1969 }
Jeremy Hylton0e1f7a82002-07-11 22:01:40 +00001970 klass = PyObject_GetAttrString(mod, name_str);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001971 if (klass == NULL) {
1972 cPickle_ErrFormat(PicklingError,
Tim Petersb9ce7cd2003-02-18 20:50:45 +00001973 "Can't pickle %s: attribute lookup %s.%s "
1974 "failed",
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001975 "OSS", args, module, global_name);
1976 goto finally;
1977 }
1978 if (klass != args) {
Jeremy Hylton0e1f7a82002-07-11 22:01:40 +00001979 Py_DECREF(klass);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001980 cPickle_ErrFormat(PicklingError,
Tim Peters731098b2003-02-04 20:56:09 +00001981 "Can't pickle %s: it's not the same object "
1982 "as %s.%s",
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001983 "OSS", args, module, global_name);
1984 goto finally;
1985 }
Jeremy Hylton0e1f7a82002-07-11 22:01:40 +00001986 Py_DECREF(klass);
Guido van Rossuma92d16a2001-08-18 21:22:07 +00001987
Tim Peters731098b2003-02-04 20:56:09 +00001988 if (self->proto >= 2) {
1989 /* See whether this is in the extension registry, and if
1990 * so generate an EXT opcode.
1991 */
1992 PyObject *py_code; /* extension code as Python object */
Tim Peters3e667d52003-02-04 21:47:44 +00001993 long code; /* extension code as C value */
Tim Peters731098b2003-02-04 20:56:09 +00001994 char c_str[5];
1995 int n;
1996
1997 PyTuple_SET_ITEM(two_tuple, 0, module);
1998 PyTuple_SET_ITEM(two_tuple, 1, global_name);
1999 py_code = PyDict_GetItem(extension_registry, two_tuple);
2000 if (py_code == NULL)
2001 goto gen_global; /* not registered */
2002
2003 /* Verify py_code has the right type and value. */
2004 if (!PyInt_Check(py_code)) {
2005 cPickle_ErrFormat(PicklingError, "Can't pickle %s: "
Tim Peters6288e232003-02-05 03:53:10 +00002006 "extension code %s isn't an integer",
Tim Peters731098b2003-02-04 20:56:09 +00002007 "OO", args, py_code);
2008 goto finally;
2009 }
2010 code = PyInt_AS_LONG(py_code);
2011 if (code <= 0 || code > 0x7fffffffL) {
2012 cPickle_ErrFormat(PicklingError, "Can't pickle %s: "
2013 "extension code %ld is out of range",
2014 "Ol", args, code);
2015 goto finally;
2016 }
2017
2018 /* Generate an EXT opcode. */
2019 if (code <= 0xff) {
2020 c_str[0] = EXT1;
2021 c_str[1] = (char)code;
2022 n = 2;
2023 }
2024 else if (code <= 0xffff) {
2025 c_str[0] = EXT2;
2026 c_str[1] = (char)(code & 0xff);
2027 c_str[2] = (char)((code >> 8) & 0xff);
2028 n = 3;
2029 }
2030 else {
2031 c_str[0] = EXT4;
2032 c_str[1] = (char)(code & 0xff);
2033 c_str[2] = (char)((code >> 8) & 0xff);
2034 c_str[3] = (char)((code >> 16) & 0xff);
2035 c_str[4] = (char)((code >> 24) & 0xff);
2036 n = 5;
2037 }
2038
2039 if (self->write_func(self, c_str, n) >= 0)
2040 res = 0;
2041 goto finally; /* and don't memoize */
2042 }
2043
2044 gen_global:
Tim Peters0bc93f52003-02-02 18:29:33 +00002045 if (self->write_func(self, &global, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002046 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002047
Tim Peters0bc93f52003-02-02 18:29:33 +00002048 if (self->write_func(self, module_str, module_size) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002049 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002050
Tim Peters0bc93f52003-02-02 18:29:33 +00002051 if (self->write_func(self, "\n", 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002052 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002053
Tim Peters0bc93f52003-02-02 18:29:33 +00002054 if (self->write_func(self, name_str, name_size) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002055 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002056
Tim Peters0bc93f52003-02-02 18:29:33 +00002057 if (self->write_func(self, "\n", 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002058 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002059
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002060 if (put(self, args) < 0)
2061 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002062
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002063 res = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002064
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002065 finally:
2066 Py_XDECREF(module);
2067 Py_XDECREF(global_name);
2068 Py_XDECREF(mod);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002069
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002070 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002071}
2072
Guido van Rossum60456fd1997-04-09 17:36:32 +00002073static int
Tim Peterscba30e22003-02-01 06:24:36 +00002074save_pers(Picklerobject *self, PyObject *args, PyObject *f)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002075{
2076 PyObject *pid = 0;
2077 int size, res = -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002078
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002079 static char persid = PERSID, binpersid = BINPERSID;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002080
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002081 Py_INCREF(args);
2082 ARG_TUP(self, args);
2083 if (self->arg) {
2084 pid = PyObject_Call(f, self->arg, NULL);
2085 FREE_ARG_TUP(self);
2086 }
2087 if (! pid) return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002088
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002089 if (pid != Py_None) {
2090 if (!self->bin) {
2091 if (!PyString_Check(pid)) {
2092 PyErr_SetString(PicklingError,
2093 "persistent id must be string");
2094 goto finally;
2095 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002096
Tim Peters0bc93f52003-02-02 18:29:33 +00002097 if (self->write_func(self, &persid, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002098 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002099
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002100 if ((size = PyString_Size(pid)) < 0)
2101 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002102
Tim Peters0bc93f52003-02-02 18:29:33 +00002103 if (self->write_func(self,
2104 PyString_AS_STRING(
2105 (PyStringObject *)pid),
2106 size) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002107 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002108
Tim Peters0bc93f52003-02-02 18:29:33 +00002109 if (self->write_func(self, "\n", 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002110 goto finally;
Tim Peters84e87f32001-03-17 04:50:51 +00002111
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002112 res = 1;
2113 goto finally;
2114 }
2115 else if (save(self, pid, 1) >= 0) {
Tim Peters0bc93f52003-02-02 18:29:33 +00002116 if (self->write_func(self, &binpersid, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002117 res = -1;
2118 else
2119 res = 1;
2120 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002121
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002122 goto finally;
2123 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002124
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002125 res = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002126
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002127 finally:
2128 Py_XDECREF(pid);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002129
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002130 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002131}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002132
Tim Peters71fcda52003-02-14 23:05:28 +00002133/* We're saving ob, and args is the 2-thru-5 tuple returned by the
2134 * appropriate __reduce__ method for ob.
2135 */
Tim Peters84e87f32001-03-17 04:50:51 +00002136static int
Tim Peters71fcda52003-02-14 23:05:28 +00002137save_reduce(Picklerobject *self, PyObject *args, PyObject *ob)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002138{
Tim Peters71fcda52003-02-14 23:05:28 +00002139 PyObject *callable;
2140 PyObject *argtup;
2141 PyObject *state = NULL;
2142 PyObject *listitems = NULL;
2143 PyObject *dictitems = NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002144
Tim Peters71fcda52003-02-14 23:05:28 +00002145 int use_newobj = self->proto >= 2;
2146
2147 static char reduce = REDUCE;
2148 static char build = BUILD;
2149 static char newobj = NEWOBJ;
2150
2151 if (! PyArg_UnpackTuple(args, "save_reduce", 2, 5,
2152 &callable,
2153 &argtup,
2154 &state,
2155 &listitems,
2156 &dictitems))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002157 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002158
Raymond Hettingera6b45cc2004-12-07 07:05:57 +00002159 if (!PyTuple_Check(argtup)) {
2160 PyErr_SetString(PicklingError,
2161 "args from reduce() should be a tuple");
2162 return -1;
2163 }
2164
Tim Peters71fcda52003-02-14 23:05:28 +00002165 if (state == Py_None)
2166 state = NULL;
2167 if (listitems == Py_None)
2168 listitems = NULL;
2169 if (dictitems == Py_None)
2170 dictitems = NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002171
Tim Peters71fcda52003-02-14 23:05:28 +00002172 /* Protocol 2 special case: if callable's name is __newobj__, use
2173 * NEWOBJ. This consumes a lot of code.
2174 */
2175 if (use_newobj) {
2176 PyObject *temp = PyObject_GetAttr(callable, __name___str);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002177
Tim Peters71fcda52003-02-14 23:05:28 +00002178 if (temp == NULL) {
Jeremy Hyltonf00368f2003-06-16 20:19:49 +00002179 if (PyErr_ExceptionMatches(PyExc_AttributeError))
2180 PyErr_Clear();
2181 else
2182 return -1;
Tim Peters71fcda52003-02-14 23:05:28 +00002183 use_newobj = 0;
2184 }
2185 else {
2186 use_newobj = PyString_Check(temp) &&
2187 strcmp(PyString_AS_STRING(temp),
2188 "__newobj__") == 0;
2189 Py_DECREF(temp);
2190 }
2191 }
2192 if (use_newobj) {
2193 PyObject *cls;
2194 PyObject *newargtup;
2195 int n, i;
2196
2197 /* Sanity checks. */
2198 n = PyTuple_Size(argtup);
2199 if (n < 1) {
2200 PyErr_SetString(PicklingError, "__newobj__ arglist "
2201 "is empty");
2202 return -1;
2203 }
2204
2205 cls = PyTuple_GET_ITEM(argtup, 0);
2206 if (! PyObject_HasAttrString(cls, "__new__")) {
2207 PyErr_SetString(PicklingError, "args[0] from "
2208 "__newobj__ args has no __new__");
2209 return -1;
2210 }
2211
2212 /* XXX How could ob be NULL? */
2213 if (ob != NULL) {
2214 PyObject *ob_dot_class;
2215
2216 ob_dot_class = PyObject_GetAttr(ob, __class___str);
Jeremy Hyltonf00368f2003-06-16 20:19:49 +00002217 if (ob_dot_class == NULL) {
2218 if (PyErr_ExceptionMatches(
2219 PyExc_AttributeError))
2220 PyErr_Clear();
2221 else
2222 return -1;
2223 }
Tim Peters71fcda52003-02-14 23:05:28 +00002224 i = ob_dot_class != cls; /* true iff a problem */
2225 Py_XDECREF(ob_dot_class);
2226 if (i) {
2227 PyErr_SetString(PicklingError, "args[0] from "
2228 "__newobj__ args has the wrong class");
2229 return -1;
2230 }
2231 }
2232
2233 /* Save the class and its __new__ arguments. */
2234 if (save(self, cls, 0) < 0)
2235 return -1;
2236
2237 newargtup = PyTuple_New(n-1); /* argtup[1:] */
2238 if (newargtup == NULL)
2239 return -1;
2240 for (i = 1; i < n; ++i) {
2241 PyObject *temp = PyTuple_GET_ITEM(argtup, i);
2242 Py_INCREF(temp);
2243 PyTuple_SET_ITEM(newargtup, i-1, temp);
2244 }
2245 i = save(self, newargtup, 0) < 0;
2246 Py_DECREF(newargtup);
2247 if (i < 0)
2248 return -1;
2249
2250 /* Add NEWOBJ opcode. */
2251 if (self->write_func(self, &newobj, 1) < 0)
2252 return -1;
2253 }
2254 else {
2255 /* Not using NEWOBJ. */
2256 if (save(self, callable, 0) < 0 ||
2257 save(self, argtup, 0) < 0 ||
2258 self->write_func(self, &reduce, 1) < 0)
2259 return -1;
2260 }
2261
2262 /* Memoize. */
2263 /* XXX How can ob be NULL? */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002264 if (ob != NULL) {
2265 if (state && !PyDict_Check(state)) {
2266 if (put2(self, ob) < 0)
2267 return -1;
2268 }
Tim Peters71fcda52003-02-14 23:05:28 +00002269 else if (put(self, ob) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002270 return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002271 }
Tim Peters84e87f32001-03-17 04:50:51 +00002272
Guido van Rossum60456fd1997-04-09 17:36:32 +00002273
Tim Peters71fcda52003-02-14 23:05:28 +00002274 if (listitems && batch_list(self, listitems) < 0)
2275 return -1;
2276
2277 if (dictitems && batch_dict(self, dictitems) < 0)
2278 return -1;
2279
2280 if (state) {
2281 if (save(self, state, 0) < 0 ||
2282 self->write_func(self, &build, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002283 return -1;
2284 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002285
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002286 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002287}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002288
Guido van Rossum60456fd1997-04-09 17:36:32 +00002289static int
Tim Peters1d63c9f2003-02-02 20:29:39 +00002290save(Picklerobject *self, PyObject *args, int pers_save)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002291{
2292 PyTypeObject *type;
Tim Peters71fcda52003-02-14 23:05:28 +00002293 PyObject *py_ob_id = 0, *__reduce__ = 0, *t = 0;
2294 PyObject *arg_tup;
2295 int res = -1;
2296 int tmp, size;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002297
Martin v. Löwis5a395302002-08-04 08:20:23 +00002298 if (self->nesting++ > Py_GetRecursionLimit()){
2299 PyErr_SetString(PyExc_RuntimeError,
2300 "maximum recursion depth exceeded");
2301 goto finally;
2302 }
2303
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002304 if (!pers_save && self->pers_func) {
2305 if ((tmp = save_pers(self, args, self->pers_func)) != 0) {
2306 res = tmp;
2307 goto finally;
2308 }
2309 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002310
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002311 if (args == Py_None) {
2312 res = save_none(self, args);
2313 goto finally;
2314 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002315
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002316 type = args->ob_type;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002317
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002318 switch (type->tp_name[0]) {
Guido van Rossum77f6a652002-04-03 22:41:51 +00002319 case 'b':
2320 if (args == Py_False || args == Py_True) {
2321 res = save_bool(self, args);
2322 goto finally;
2323 }
2324 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002325 case 'i':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002326 if (type == &PyInt_Type) {
2327 res = save_int(self, args);
2328 goto finally;
2329 }
2330 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002331
Guido van Rossum60456fd1997-04-09 17:36:32 +00002332 case 'l':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002333 if (type == &PyLong_Type) {
2334 res = save_long(self, args);
2335 goto finally;
2336 }
2337 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002338
Guido van Rossum60456fd1997-04-09 17:36:32 +00002339 case 'f':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002340 if (type == &PyFloat_Type) {
2341 res = save_float(self, args);
2342 goto finally;
2343 }
2344 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002345
Guido van Rossum60456fd1997-04-09 17:36:32 +00002346 case 't':
Tim Peters1d63c9f2003-02-02 20:29:39 +00002347 if (type == &PyTuple_Type && PyTuple_Size(args) == 0) {
2348 res = save_tuple(self, args);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002349 goto finally;
2350 }
2351 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002352
Guido van Rossum60456fd1997-04-09 17:36:32 +00002353 case 's':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002354 if ((type == &PyString_Type) && (PyString_GET_SIZE(args) < 2)) {
2355 res = save_string(self, args, 0);
2356 goto finally;
2357 }
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00002358
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002359#ifdef Py_USING_UNICODE
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00002360 case 'u':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002361 if ((type == &PyUnicode_Type) && (PyString_GET_SIZE(args) < 2)) {
2362 res = save_unicode(self, args, 0);
2363 goto finally;
2364 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002365#endif
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002366 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002367
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002368 if (args->ob_refcnt > 1) {
Tim Peterscba30e22003-02-01 06:24:36 +00002369 if (!( py_ob_id = PyLong_FromVoidPtr(args)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002370 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002371
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002372 if (PyDict_GetItem(self->memo, py_ob_id)) {
2373 if (get(self, py_ob_id) < 0)
2374 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002375
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002376 res = 0;
2377 goto finally;
2378 }
2379 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002380
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002381 switch (type->tp_name[0]) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00002382 case 's':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002383 if (type == &PyString_Type) {
2384 res = save_string(self, args, 1);
2385 goto finally;
2386 }
2387 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002388
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002389#ifdef Py_USING_UNICODE
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00002390 case 'u':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002391 if (type == &PyUnicode_Type) {
2392 res = save_unicode(self, args, 1);
2393 goto finally;
2394 }
2395 break;
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002396#endif
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00002397
Guido van Rossum60456fd1997-04-09 17:36:32 +00002398 case 't':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002399 if (type == &PyTuple_Type) {
2400 res = save_tuple(self, args);
2401 goto finally;
2402 }
2403 if (type == &PyType_Type) {
2404 res = save_global(self, args, NULL);
2405 goto finally;
2406 }
2407 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002408
Guido van Rossum60456fd1997-04-09 17:36:32 +00002409 case 'l':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002410 if (type == &PyList_Type) {
2411 res = save_list(self, args);
2412 goto finally;
2413 }
2414 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002415
2416 case 'd':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002417 if (type == &PyDict_Type) {
2418 res = save_dict(self, args);
2419 goto finally;
2420 }
2421 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002422
2423 case 'i':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002424 if (type == &PyInstance_Type) {
2425 res = save_inst(self, args);
2426 goto finally;
2427 }
2428 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002429
2430 case 'c':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002431 if (type == &PyClass_Type) {
2432 res = save_global(self, args, NULL);
2433 goto finally;
2434 }
2435 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002436
2437 case 'f':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002438 if (type == &PyFunction_Type) {
2439 res = save_global(self, args, NULL);
Christian Tismer2460c622004-02-26 16:21:45 +00002440 if (res && PyErr_ExceptionMatches(PickleError)) {
2441 /* fall back to reduce */
2442 PyErr_Clear();
2443 break;
2444 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002445 goto finally;
2446 }
2447 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002448
2449 case 'b':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002450 if (type == &PyCFunction_Type) {
2451 res = save_global(self, args, NULL);
2452 goto finally;
2453 }
2454 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002455
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002456 if (!pers_save && self->inst_pers_func) {
2457 if ((tmp = save_pers(self, args, self->inst_pers_func)) != 0) {
2458 res = tmp;
2459 goto finally;
2460 }
2461 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002462
Jeremy Hylton39c61162002-07-16 19:47:43 +00002463 if (PyType_IsSubtype(type, &PyType_Type)) {
2464 res = save_global(self, args, NULL);
2465 goto finally;
2466 }
2467
Guido van Rossumb289b872003-02-19 01:45:13 +00002468 /* Get a reduction callable, and call it. This may come from
2469 * copy_reg.dispatch_table, the object's __reduce_ex__ method,
2470 * or the object's __reduce__ method.
Tim Peters71fcda52003-02-14 23:05:28 +00002471 */
Tim Peters5aa3da62003-02-13 21:03:57 +00002472 __reduce__ = PyDict_GetItem(dispatch_table, (PyObject *)type);
2473 if (__reduce__ != NULL) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002474 Py_INCREF(__reduce__);
Guido van Rossumb289b872003-02-19 01:45:13 +00002475 Py_INCREF(args);
2476 ARG_TUP(self, args);
2477 if (self->arg) {
2478 t = PyObject_Call(__reduce__, self->arg, NULL);
2479 FREE_ARG_TUP(self);
2480 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002481 }
2482 else {
Guido van Rossumb289b872003-02-19 01:45:13 +00002483 /* Check for a __reduce_ex__ method. */
2484 __reduce__ = PyObject_GetAttr(args, __reduce_ex___str);
2485 if (__reduce__ != NULL) {
2486 t = PyInt_FromLong(self->proto);
2487 if (t != NULL) {
2488 ARG_TUP(self, t);
2489 t = NULL;
2490 if (self->arg) {
2491 t = PyObject_Call(__reduce__,
2492 self->arg, NULL);
2493 FREE_ARG_TUP(self);
2494 }
2495 }
2496 }
2497 else {
Jeremy Hyltonf00368f2003-06-16 20:19:49 +00002498 if (PyErr_ExceptionMatches(PyExc_AttributeError))
2499 PyErr_Clear();
2500 else
2501 goto finally;
Guido van Rossumb289b872003-02-19 01:45:13 +00002502 /* Check for a __reduce__ method. */
2503 __reduce__ = PyObject_GetAttr(args, __reduce___str);
2504 if (__reduce__ != NULL) {
2505 t = PyObject_Call(__reduce__,
2506 empty_tuple, NULL);
2507 }
2508 else {
2509 PyErr_SetObject(UnpickleableError, args);
2510 goto finally;
2511 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002512 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002513 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002514
Tim Peters71fcda52003-02-14 23:05:28 +00002515 if (t == NULL)
2516 goto finally;
Tim Peters84e87f32001-03-17 04:50:51 +00002517
Tim Peters71fcda52003-02-14 23:05:28 +00002518 if (PyString_Check(t)) {
2519 res = save_global(self, args, t);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002520 goto finally;
2521 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002522
Tim Peters71fcda52003-02-14 23:05:28 +00002523 if (! PyTuple_Check(t)) {
2524 cPickle_ErrFormat(PicklingError, "Value returned by "
2525 "%s must be string or tuple",
2526 "O", __reduce__);
2527 goto finally;
2528 }
2529
2530 size = PyTuple_Size(t);
2531 if (size < 2 || size > 5) {
2532 cPickle_ErrFormat(PicklingError, "tuple returned by "
2533 "%s must contain 2 through 5 elements",
2534 "O", __reduce__);
2535 goto finally;
2536 }
2537
2538 arg_tup = PyTuple_GET_ITEM(t, 1);
2539 if (!(PyTuple_Check(arg_tup) || arg_tup == Py_None)) {
2540 cPickle_ErrFormat(PicklingError, "Second element of "
2541 "tuple returned by %s must be a tuple",
2542 "O", __reduce__);
2543 goto finally;
2544 }
2545
2546 res = save_reduce(self, t, args);
Guido van Rossum60456fd1997-04-09 17:36:32 +00002547
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002548 finally:
Martin v. Löwis5a395302002-08-04 08:20:23 +00002549 self->nesting--;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002550 Py_XDECREF(py_ob_id);
2551 Py_XDECREF(__reduce__);
2552 Py_XDECREF(t);
Tim Peters84e87f32001-03-17 04:50:51 +00002553
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002554 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002555}
2556
2557
2558static int
Tim Peterscba30e22003-02-01 06:24:36 +00002559dump(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002560{
2561 static char stop = STOP;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002562
Tim Peters4190fb82003-02-02 16:09:05 +00002563 if (self->proto >= 2) {
2564 char bytes[2];
2565
2566 bytes[0] = PROTO;
Tim Peters87482ea2003-02-02 16:16:30 +00002567 assert(self->proto >= 0 && self->proto < 256);
2568 bytes[1] = (char)self->proto;
Tim Peters4190fb82003-02-02 16:09:05 +00002569 if (self->write_func(self, bytes, 2) < 0)
2570 return -1;
2571 }
2572
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002573 if (save(self, args, 0) < 0)
2574 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002575
Tim Peters4190fb82003-02-02 16:09:05 +00002576 if (self->write_func(self, &stop, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002577 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002578
Tim Peters4190fb82003-02-02 16:09:05 +00002579 if (self->write_func(self, NULL, 0) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002580 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002581
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002582 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002583}
2584
2585static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00002586Pickle_clear_memo(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002587{
Tim Peterscba30e22003-02-01 06:24:36 +00002588 if (self->memo)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002589 PyDict_Clear(self->memo);
2590 Py_INCREF(Py_None);
2591 return Py_None;
2592}
2593
2594static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00002595Pickle_getvalue(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002596{
2597 int l, i, rsize, ssize, clear=1, lm;
2598 long ik;
2599 PyObject *k, *r;
2600 char *s, *p, *have_get;
2601 Pdata *data;
2602
2603 /* Can be called by Python code or C code */
Tim Peterscba30e22003-02-01 06:24:36 +00002604 if (args && !PyArg_ParseTuple(args, "|i:getvalue", &clear))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002605 return NULL;
2606
2607 /* Check to make sure we are based on a list */
2608 if (! Pdata_Check(self->file)) {
2609 PyErr_SetString(PicklingError,
2610 "Attempt to getvalue() a non-list-based pickler");
2611 return NULL;
2612 }
2613
2614 /* flush write buffer */
2615 if (write_other(self, NULL, 0) < 0) return NULL;
2616
2617 data=(Pdata*)self->file;
2618 l=data->length;
2619
2620 /* set up an array to hold get/put status */
Tim Petersac5687a2003-02-02 18:08:34 +00002621 lm = PyDict_Size(self->memo);
2622 if (lm < 0) return NULL;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002623 lm++;
Tim Petersac5687a2003-02-02 18:08:34 +00002624 have_get = malloc(lm);
2625 if (have_get == NULL) return PyErr_NoMemory();
2626 memset(have_get, 0, lm);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002627
2628 /* Scan for gets. */
Tim Petersac5687a2003-02-02 18:08:34 +00002629 for (rsize = 0, i = l; --i >= 0; ) {
2630 k = data->data[i];
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002631
Tim Petersac5687a2003-02-02 18:08:34 +00002632 if (PyString_Check(k))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002633 rsize += PyString_GET_SIZE(k);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002634
2635 else if (PyInt_Check(k)) { /* put */
Tim Petersac5687a2003-02-02 18:08:34 +00002636 ik = PyInt_AS_LONG((PyIntObject*)k);
2637 if (ik >= lm || ik == 0) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002638 PyErr_SetString(PicklingError,
2639 "Invalid get data");
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002640 goto err;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002641 }
Tim Petersac5687a2003-02-02 18:08:34 +00002642 if (have_get[ik]) /* with matching get */
2643 rsize += ik < 256 ? 2 : 5;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002644 }
2645
2646 else if (! (PyTuple_Check(k) &&
2647 PyTuple_GET_SIZE(k) == 2 &&
Tim Petersac5687a2003-02-02 18:08:34 +00002648 PyInt_Check((k = PyTuple_GET_ITEM(k, 0))))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002649 ) {
2650 PyErr_SetString(PicklingError,
2651 "Unexpected data in internal list");
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002652 goto err;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002653 }
2654
2655 else { /* put */
Tim Petersac5687a2003-02-02 18:08:34 +00002656 ik = PyInt_AS_LONG((PyIntObject *)k);
2657 if (ik >= lm || ik == 0) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002658 PyErr_SetString(PicklingError,
2659 "Invalid get data");
2660 return NULL;
2661 }
Tim Petersac5687a2003-02-02 18:08:34 +00002662 have_get[ik] = 1;
2663 rsize += ik < 256 ? 2 : 5;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002664 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002665 }
2666
2667 /* Now generate the result */
Tim Petersac5687a2003-02-02 18:08:34 +00002668 r = PyString_FromStringAndSize(NULL, rsize);
2669 if (r == NULL) goto err;
2670 s = PyString_AS_STRING((PyStringObject *)r);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002671
Tim Petersac5687a2003-02-02 18:08:34 +00002672 for (i = 0; i < l; i++) {
2673 k = data->data[i];
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002674
2675 if (PyString_Check(k)) {
Tim Petersac5687a2003-02-02 18:08:34 +00002676 ssize = PyString_GET_SIZE(k);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002677 if (ssize) {
Tim Petersac5687a2003-02-02 18:08:34 +00002678 p=PyString_AS_STRING((PyStringObject *)k);
2679 while (--ssize >= 0)
2680 *s++ = *p++;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002681 }
2682 }
2683
2684 else if (PyTuple_Check(k)) { /* get */
Tim Petersac5687a2003-02-02 18:08:34 +00002685 ik = PyInt_AS_LONG((PyIntObject *)
2686 PyTuple_GET_ITEM(k, 0));
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002687 if (ik < 256) {
2688 *s++ = BINGET;
2689 *s++ = (int)(ik & 0xff);
2690 }
2691 else {
2692 *s++ = LONG_BINGET;
2693 *s++ = (int)(ik & 0xff);
2694 *s++ = (int)((ik >> 8) & 0xff);
2695 *s++ = (int)((ik >> 16) & 0xff);
2696 *s++ = (int)((ik >> 24) & 0xff);
2697 }
2698 }
2699
2700 else { /* put */
Tim Petersac5687a2003-02-02 18:08:34 +00002701 ik = PyInt_AS_LONG((PyIntObject*)k);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002702
2703 if (have_get[ik]) { /* with matching get */
2704 if (ik < 256) {
2705 *s++ = BINPUT;
2706 *s++ = (int)(ik & 0xff);
2707 }
2708 else {
2709 *s++ = LONG_BINPUT;
2710 *s++ = (int)(ik & 0xff);
2711 *s++ = (int)((ik >> 8) & 0xff);
2712 *s++ = (int)((ik >> 16) & 0xff);
2713 *s++ = (int)((ik >> 24) & 0xff);
2714 }
2715 }
2716 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002717 }
2718
2719 if (clear) {
2720 PyDict_Clear(self->memo);
Tim Petersac5687a2003-02-02 18:08:34 +00002721 Pdata_clear(data, 0);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002722 }
2723
2724 free(have_get);
2725 return r;
2726 err:
2727 free(have_get);
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00002728 return NULL;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002729}
2730
2731static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00002732Pickler_dump(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002733{
2734 PyObject *ob;
2735 int get=0;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002736
Tim Peterscba30e22003-02-01 06:24:36 +00002737 if (!( PyArg_ParseTuple(args, "O|i:dump", &ob, &get)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002738 return NULL;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002739
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002740 if (dump(self, ob) < 0)
2741 return NULL;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002742
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002743 if (get) return Pickle_getvalue(self, NULL);
Guido van Rossum053b8df1998-11-25 16:18:00 +00002744
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002745 /* XXX Why does dump() return self? */
2746 Py_INCREF(self);
2747 return (PyObject*)self;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002748}
2749
2750
Tim Peterscba30e22003-02-01 06:24:36 +00002751static struct PyMethodDef Pickler_methods[] =
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002752{
Neal Norwitzb0493252002-03-31 14:44:22 +00002753 {"dump", (PyCFunction)Pickler_dump, METH_VARARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00002754 PyDoc_STR("dump(object) -- "
2755 "Write an object in pickle format to the object's pickle stream")},
Fred Drake0ebacc82002-05-01 20:36:39 +00002756 {"clear_memo", (PyCFunction)Pickle_clear_memo, METH_NOARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00002757 PyDoc_STR("clear_memo() -- Clear the picklers memo")},
Neal Norwitzb0493252002-03-31 14:44:22 +00002758 {"getvalue", (PyCFunction)Pickle_getvalue, METH_VARARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00002759 PyDoc_STR("getvalue() -- Finish picking a list-based pickle")},
Guido van Rossum60456fd1997-04-09 17:36:32 +00002760 {NULL, NULL} /* sentinel */
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002761};
2762
2763
2764static Picklerobject *
Tim Peters5bd2a792003-02-01 16:45:06 +00002765newPicklerobject(PyObject *file, int proto)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002766{
2767 Picklerobject *self;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002768
Tim Peters5bd2a792003-02-01 16:45:06 +00002769 if (proto < 0)
Tim Peters8587b3c2003-02-13 15:44:41 +00002770 proto = HIGHEST_PROTOCOL;
2771 if (proto > HIGHEST_PROTOCOL) {
Tim Peters5bd2a792003-02-01 16:45:06 +00002772 PyErr_Format(PyExc_ValueError, "pickle protocol %d asked for; "
2773 "the highest available protocol is %d",
Tim Peters8587b3c2003-02-13 15:44:41 +00002774 proto, HIGHEST_PROTOCOL);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002775 return NULL;
Tim Peters5bd2a792003-02-01 16:45:06 +00002776 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002777
Jeremy Hylton4cf63192003-04-09 21:05:12 +00002778 self = PyObject_GC_New(Picklerobject, &Picklertype);
Tim Peters5bd2a792003-02-01 16:45:06 +00002779 if (self == NULL)
2780 return NULL;
2781 self->proto = proto;
2782 self->bin = proto > 0;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002783 self->fp = NULL;
2784 self->write = NULL;
2785 self->memo = NULL;
2786 self->arg = NULL;
2787 self->pers_func = NULL;
2788 self->inst_pers_func = NULL;
2789 self->write_buf = NULL;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002790 self->fast = 0;
Martin v. Löwis5a395302002-08-04 08:20:23 +00002791 self->nesting = 0;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002792 self->fast_container = 0;
2793 self->fast_memo = NULL;
2794 self->buf_size = 0;
2795 self->dispatch_table = NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002796
Tim Peters5bd2a792003-02-01 16:45:06 +00002797 self->file = NULL;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002798 if (file)
2799 Py_INCREF(file);
Tim Peters5bd2a792003-02-01 16:45:06 +00002800 else {
2801 file = Pdata_New();
2802 if (file == NULL)
2803 goto err;
2804 }
2805 self->file = file;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002806
Tim Peterscba30e22003-02-01 06:24:36 +00002807 if (!( self->memo = PyDict_New()))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002808 goto err;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002809
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002810 if (PyFile_Check(file)) {
2811 self->fp = PyFile_AsFile(file);
2812 if (self->fp == NULL) {
Tim Peters5bd2a792003-02-01 16:45:06 +00002813 PyErr_SetString(PyExc_ValueError,
2814 "I/O operation on closed file");
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002815 goto err;
2816 }
2817 self->write_func = write_file;
Guido van Rossumc91fcaa1999-03-29 20:00:14 +00002818 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002819 else if (PycStringIO_OutputCheck(file)) {
2820 self->write_func = write_cStringIO;
2821 }
2822 else if (file == Py_None) {
2823 self->write_func = write_none;
2824 }
2825 else {
2826 self->write_func = write_other;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002827
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002828 if (! Pdata_Check(file)) {
2829 self->write = PyObject_GetAttr(file, write_str);
2830 if (!self->write) {
2831 PyErr_Clear();
Tim Peterscba30e22003-02-01 06:24:36 +00002832 PyErr_SetString(PyExc_TypeError,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002833 "argument must have 'write' "
2834 "attribute");
2835 goto err;
2836 }
2837 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002838
Tim Peters5bd2a792003-02-01 16:45:06 +00002839 self->write_buf = (char *)PyMem_Malloc(WRITE_BUF_SIZE);
2840 if (self->write_buf == NULL) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002841 PyErr_NoMemory();
2842 goto err;
2843 }
2844 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002845
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002846 if (PyEval_GetRestricted()) {
2847 /* Restricted execution, get private tables */
Tim Peters5b7da392003-02-04 00:21:07 +00002848 PyObject *m = PyImport_Import(copy_reg_str);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002849
Tim Peters5b7da392003-02-04 00:21:07 +00002850 if (m == NULL)
2851 goto err;
2852 self->dispatch_table = PyObject_GetAttr(m, dispatch_table_str);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002853 Py_DECREF(m);
Tim Peters5b7da392003-02-04 00:21:07 +00002854 if (self->dispatch_table == NULL)
2855 goto err;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002856 }
2857 else {
Tim Peters5b7da392003-02-04 00:21:07 +00002858 self->dispatch_table = dispatch_table;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002859 Py_INCREF(dispatch_table);
2860 }
Jeremy Hylton4cf63192003-04-09 21:05:12 +00002861 PyObject_GC_Track(self);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002862
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002863 return self;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002864
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002865 err:
Tim Peters5bd2a792003-02-01 16:45:06 +00002866 Py_DECREF(self);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002867 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002868}
2869
2870
2871static PyObject *
Martin v. Löwis544f1192004-07-27 05:22:33 +00002872get_Pickler(PyObject *self, PyObject *args, PyObject *kwds)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002873{
Martin v. Löwis15e62742006-02-27 16:46:16 +00002874 static char *kwlist[] = {"file", "protocol", NULL};
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002875 PyObject *file = NULL;
Tim Peters5bd2a792003-02-01 16:45:06 +00002876 int proto = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002877
Tim Peters92c8bb32003-02-13 23:00:26 +00002878 /* XXX
Martin v. Löwis544f1192004-07-27 05:22:33 +00002879 * The documented signature is Pickler(file, protocol=0), but this
Tim Peters92c8bb32003-02-13 23:00:26 +00002880 * accepts Pickler() and Pickler(integer) too. The meaning then
2881 * is clear as mud, undocumented, and not supported by pickle.py.
2882 * I'm told Zope uses this, but I haven't traced into this code
2883 * far enough to figure out what it means.
Tim Peters5bd2a792003-02-01 16:45:06 +00002884 */
2885 if (!PyArg_ParseTuple(args, "|i:Pickler", &proto)) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002886 PyErr_Clear();
Tim Peters5bd2a792003-02-01 16:45:06 +00002887 proto = 0;
Martin v. Löwis544f1192004-07-27 05:22:33 +00002888 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|i:Pickler",
2889 kwlist, &file, &proto))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002890 return NULL;
2891 }
Tim Peters5bd2a792003-02-01 16:45:06 +00002892 return (PyObject *)newPicklerobject(file, proto);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002893}
2894
2895
2896static void
Tim Peterscba30e22003-02-01 06:24:36 +00002897Pickler_dealloc(Picklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002898{
Jeremy Hylton4cf63192003-04-09 21:05:12 +00002899 PyObject_GC_UnTrack(self);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002900 Py_XDECREF(self->write);
2901 Py_XDECREF(self->memo);
2902 Py_XDECREF(self->fast_memo);
2903 Py_XDECREF(self->arg);
2904 Py_XDECREF(self->file);
2905 Py_XDECREF(self->pers_func);
2906 Py_XDECREF(self->inst_pers_func);
2907 Py_XDECREF(self->dispatch_table);
Tim Peters5bd2a792003-02-01 16:45:06 +00002908 PyMem_Free(self->write_buf);
Tim Peters3cfe7542003-05-21 21:29:48 +00002909 self->ob_type->tp_free((PyObject *)self);
Jeremy Hylton4cf63192003-04-09 21:05:12 +00002910}
2911
2912static int
2913Pickler_traverse(Picklerobject *self, visitproc visit, void *arg)
2914{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002915 Py_VISIT(self->write);
2916 Py_VISIT(self->memo);
2917 Py_VISIT(self->fast_memo);
2918 Py_VISIT(self->arg);
2919 Py_VISIT(self->file);
2920 Py_VISIT(self->pers_func);
2921 Py_VISIT(self->inst_pers_func);
2922 Py_VISIT(self->dispatch_table);
Jeremy Hylton4cf63192003-04-09 21:05:12 +00002923 return 0;
2924}
2925
2926static int
2927Pickler_clear(Picklerobject *self)
2928{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002929 Py_CLEAR(self->write);
2930 Py_CLEAR(self->memo);
2931 Py_CLEAR(self->fast_memo);
2932 Py_CLEAR(self->arg);
2933 Py_CLEAR(self->file);
2934 Py_CLEAR(self->pers_func);
2935 Py_CLEAR(self->inst_pers_func);
2936 Py_CLEAR(self->dispatch_table);
Jeremy Hylton4cf63192003-04-09 21:05:12 +00002937 return 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002938}
2939
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002940static PyObject *
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00002941Pickler_get_pers_func(Picklerobject *p)
2942{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002943 if (p->pers_func == NULL)
2944 PyErr_SetString(PyExc_AttributeError, "persistent_id");
2945 else
2946 Py_INCREF(p->pers_func);
2947 return p->pers_func;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002948}
2949
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00002950static int
2951Pickler_set_pers_func(Picklerobject *p, PyObject *v)
2952{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002953 if (v == NULL) {
2954 PyErr_SetString(PyExc_TypeError,
2955 "attribute deletion is not supported");
2956 return -1;
2957 }
2958 Py_XDECREF(p->pers_func);
2959 Py_INCREF(v);
2960 p->pers_func = v;
2961 return 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002962}
2963
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00002964static int
2965Pickler_set_inst_pers_func(Picklerobject *p, PyObject *v)
2966{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002967 if (v == NULL) {
2968 PyErr_SetString(PyExc_TypeError,
2969 "attribute deletion is not supported");
2970 return -1;
2971 }
2972 Py_XDECREF(p->inst_pers_func);
2973 Py_INCREF(v);
2974 p->inst_pers_func = v;
2975 return 0;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00002976}
2977
2978static PyObject *
2979Pickler_get_memo(Picklerobject *p)
2980{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002981 if (p->memo == NULL)
2982 PyErr_SetString(PyExc_AttributeError, "memo");
2983 else
2984 Py_INCREF(p->memo);
2985 return p->memo;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00002986}
2987
2988static int
2989Pickler_set_memo(Picklerobject *p, PyObject *v)
2990{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002991 if (v == NULL) {
2992 PyErr_SetString(PyExc_TypeError,
2993 "attribute deletion is not supported");
2994 return -1;
2995 }
2996 if (!PyDict_Check(v)) {
2997 PyErr_SetString(PyExc_TypeError, "memo must be a dictionary");
2998 return -1;
2999 }
3000 Py_XDECREF(p->memo);
3001 Py_INCREF(v);
3002 p->memo = v;
3003 return 0;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003004}
3005
3006static PyObject *
3007Pickler_get_error(Picklerobject *p)
3008{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003009 /* why is this an attribute on the Pickler? */
3010 Py_INCREF(PicklingError);
3011 return PicklingError;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003012}
3013
3014static PyMemberDef Pickler_members[] = {
3015 {"binary", T_INT, offsetof(Picklerobject, bin)},
3016 {"fast", T_INT, offsetof(Picklerobject, fast)},
Jeremy Hylton3eb46f32001-10-16 17:10:49 +00003017 {NULL}
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003018};
3019
3020static PyGetSetDef Pickler_getsets[] = {
Tim Peterscba30e22003-02-01 06:24:36 +00003021 {"persistent_id", (getter)Pickler_get_pers_func,
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003022 (setter)Pickler_set_pers_func},
3023 {"inst_persistent_id", NULL, (setter)Pickler_set_inst_pers_func},
3024 {"memo", (getter)Pickler_get_memo, (setter)Pickler_set_memo},
Jeremy Hylton3eb46f32001-10-16 17:10:49 +00003025 {"PicklingError", (getter)Pickler_get_error, NULL},
3026 {NULL}
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003027};
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003028
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003029PyDoc_STRVAR(Picklertype__doc__,
3030"Objects that know how to pickle objects\n");
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003031
Guido van Rossum9716aaa1997-12-08 15:15:16 +00003032static PyTypeObject Picklertype = {
3033 PyObject_HEAD_INIT(NULL)
Guido van Rossum60456fd1997-04-09 17:36:32 +00003034 0, /*ob_size*/
Guido van Rossum14648392001-12-08 18:02:58 +00003035 "cPickle.Pickler", /*tp_name*/
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003036 sizeof(Picklerobject), /*tp_basicsize*/
3037 0,
3038 (destructor)Pickler_dealloc, /* tp_dealloc */
3039 0, /* tp_print */
3040 0, /* tp_getattr */
3041 0, /* tp_setattr */
3042 0, /* tp_compare */
3043 0, /* tp_repr */
3044 0, /* tp_as_number */
3045 0, /* tp_as_sequence */
3046 0, /* tp_as_mapping */
3047 0, /* tp_hash */
3048 0, /* tp_call */
3049 0, /* tp_str */
Jason Tishlerfb8595d2003-01-06 12:41:26 +00003050 PyObject_GenericGetAttr, /* tp_getattro */
3051 PyObject_GenericSetAttr, /* tp_setattro */
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003052 0, /* tp_as_buffer */
Jeremy Hylton4cf63192003-04-09 21:05:12 +00003053 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003054 Picklertype__doc__, /* tp_doc */
Jeremy Hylton4cf63192003-04-09 21:05:12 +00003055 (traverseproc)Pickler_traverse, /* tp_traverse */
3056 (inquiry)Pickler_clear, /* tp_clear */
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003057 0, /* tp_richcompare */
3058 0, /* tp_weaklistoffset */
3059 0, /* tp_iter */
3060 0, /* tp_iternext */
3061 Pickler_methods, /* tp_methods */
3062 Pickler_members, /* tp_members */
3063 Pickler_getsets, /* tp_getset */
Guido van Rossum9716aaa1997-12-08 15:15:16 +00003064};
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003065
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003066static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00003067find_class(PyObject *py_module_name, PyObject *py_global_name, PyObject *fc)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003068{
3069 PyObject *global = 0, *module;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003070
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003071 if (fc) {
3072 if (fc==Py_None) {
Tim Peters92c8bb32003-02-13 23:00:26 +00003073 PyErr_SetString(UnpicklingError, "Global and instance "
3074 "pickles are not supported.");
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003075 return NULL;
3076 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00003077 return PyObject_CallFunctionObjArgs(fc, py_module_name,
3078 py_global_name, NULL);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003079 }
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00003080
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003081 module = PySys_GetObject("modules");
3082 if (module == NULL)
3083 return NULL;
Jeremy Hyltond1055231998-08-11 19:52:51 +00003084
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003085 module = PyDict_GetItem(module, py_module_name);
3086 if (module == NULL) {
3087 module = PyImport_Import(py_module_name);
3088 if (!module)
3089 return NULL;
3090 global = PyObject_GetAttr(module, py_global_name);
3091 Py_DECREF(module);
3092 }
3093 else
3094 global = PyObject_GetAttr(module, py_global_name);
3095 return global;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003096}
3097
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003098static int
Tim Peterscba30e22003-02-01 06:24:36 +00003099marker(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003100{
3101 if (self->num_marks < 1) {
3102 PyErr_SetString(UnpicklingError, "could not find MARK");
3103 return -1;
3104 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003105
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003106 return self->marks[--self->num_marks];
Guido van Rossum60456fd1997-04-09 17:36:32 +00003107}
3108
Tim Peters84e87f32001-03-17 04:50:51 +00003109
Guido van Rossum60456fd1997-04-09 17:36:32 +00003110static int
Tim Peterscba30e22003-02-01 06:24:36 +00003111load_none(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003112{
3113 PDATA_APPEND(self->stack, Py_None, -1);
3114 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003115}
3116
Guido van Rossumf9ffb031999-02-04 14:54:04 +00003117static int
Tim Peterscba30e22003-02-01 06:24:36 +00003118bad_readline(void)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003119{
3120 PyErr_SetString(UnpicklingError, "pickle data was truncated");
3121 return -1;
Guido van Rossumf9ffb031999-02-04 14:54:04 +00003122}
Guido van Rossum60456fd1997-04-09 17:36:32 +00003123
3124static int
Tim Peterscba30e22003-02-01 06:24:36 +00003125load_int(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003126{
3127 PyObject *py_int = 0;
3128 char *endptr, *s;
3129 int len, res = -1;
3130 long l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003131
Tim Peters0bc93f52003-02-02 18:29:33 +00003132 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003133 if (len < 2) return bad_readline();
3134 if (!( s=pystrndup(s,len))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003135
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003136 errno = 0;
3137 l = strtol(s, &endptr, 0);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003138
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003139 if (errno || (*endptr != '\n') || (endptr[1] != '\0')) {
3140 /* Hm, maybe we've got something long. Let's try reading
3141 it as a Python long object. */
3142 errno = 0;
3143 py_int = PyLong_FromString(s, NULL, 0);
3144 if (py_int == NULL) {
3145 PyErr_SetString(PyExc_ValueError,
3146 "could not convert string to int");
3147 goto finally;
3148 }
3149 }
3150 else {
Guido van Rossume2763392002-04-05 19:30:08 +00003151 if (len == 3 && (l == 0 || l == 1)) {
3152 if (!( py_int = PyBool_FromLong(l))) goto finally;
3153 }
3154 else {
3155 if (!( py_int = PyInt_FromLong(l))) goto finally;
3156 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003157 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003158
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003159 free(s);
3160 PDATA_PUSH(self->stack, py_int, -1);
3161 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003162
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003163 finally:
3164 free(s);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003165
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003166 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003167}
3168
Tim Peters3c67d792003-02-02 17:59:11 +00003169static int
3170load_bool(Unpicklerobject *self, PyObject *boolean)
3171{
3172 assert(boolean == Py_True || boolean == Py_False);
Tim Peterse0a39072003-02-03 15:45:56 +00003173 PDATA_APPEND(self->stack, boolean, -1);
Tim Peters3c67d792003-02-02 17:59:11 +00003174 return 0;
3175}
3176
Tim Petersee1a53c2003-02-02 02:57:53 +00003177/* s contains x bytes of a little-endian integer. Return its value as a
3178 * C int. Obscure: when x is 1 or 2, this is an unsigned little-endian
3179 * int, but when x is 4 it's a signed one. This is an historical source
3180 * of x-platform bugs.
3181 */
Tim Peters84e87f32001-03-17 04:50:51 +00003182static long
Tim Petersee1a53c2003-02-02 02:57:53 +00003183calc_binint(char *s, int x)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003184{
3185 unsigned char c;
3186 int i;
3187 long l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003188
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003189 for (i = 0, l = 0L; i < x; i++) {
3190 c = (unsigned char)s[i];
3191 l |= (long)c << (i * 8);
3192 }
Tim Petersbfa18f72001-04-10 01:54:42 +00003193#if SIZEOF_LONG > 4
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003194 /* Unlike BININT1 and BININT2, BININT (more accurately BININT4)
3195 * is signed, so on a box with longs bigger than 4 bytes we need
3196 * to extend a BININT's sign bit to the full width.
3197 */
3198 if (x == 4 && l & (1L << 31))
3199 l |= (~0L) << 32;
Tim Petersbfa18f72001-04-10 01:54:42 +00003200#endif
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003201 return l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003202}
3203
3204
3205static int
Tim Peterscba30e22003-02-01 06:24:36 +00003206load_binintx(Unpicklerobject *self, char *s, int x)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003207{
3208 PyObject *py_int = 0;
3209 long l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003210
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003211 l = calc_binint(s, x);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003212
Tim Peterscba30e22003-02-01 06:24:36 +00003213 if (!( py_int = PyInt_FromLong(l)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003214 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003215
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003216 PDATA_PUSH(self->stack, py_int, -1);
3217 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003218}
3219
3220
3221static int
Tim Peterscba30e22003-02-01 06:24:36 +00003222load_binint(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003223{
3224 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003225
Tim Peters0bc93f52003-02-02 18:29:33 +00003226 if (self->read_func(self, &s, 4) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003227 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003228
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003229 return load_binintx(self, s, 4);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003230}
3231
3232
3233static int
Tim Peterscba30e22003-02-01 06:24:36 +00003234load_binint1(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003235{
3236 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003237
Tim Peters0bc93f52003-02-02 18:29:33 +00003238 if (self->read_func(self, &s, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003239 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003240
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003241 return load_binintx(self, s, 1);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003242}
3243
3244
3245static int
Tim Peterscba30e22003-02-01 06:24:36 +00003246load_binint2(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003247{
3248 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003249
Tim Peters0bc93f52003-02-02 18:29:33 +00003250 if (self->read_func(self, &s, 2) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003251 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003252
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003253 return load_binintx(self, s, 2);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003254}
Tim Peters84e87f32001-03-17 04:50:51 +00003255
Guido van Rossum60456fd1997-04-09 17:36:32 +00003256static int
Tim Peterscba30e22003-02-01 06:24:36 +00003257load_long(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003258{
3259 PyObject *l = 0;
3260 char *end, *s;
3261 int len, res = -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003262
Tim Peters0bc93f52003-02-02 18:29:33 +00003263 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003264 if (len < 2) return bad_readline();
3265 if (!( s=pystrndup(s,len))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003266
Tim Peterscba30e22003-02-01 06:24:36 +00003267 if (!( l = PyLong_FromString(s, &end, 0)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003268 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003269
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003270 free(s);
3271 PDATA_PUSH(self->stack, l, -1);
3272 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003273
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003274 finally:
3275 free(s);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003276
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003277 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003278}
3279
Tim Petersee1a53c2003-02-02 02:57:53 +00003280/* 'size' bytes contain the # of bytes of little-endian 256's-complement
3281 * data following.
3282 */
3283static int
3284load_counted_long(Unpicklerobject *self, int size)
3285{
Martin v. Löwis18e16552006-02-15 17:27:45 +00003286 Py_ssize_t i;
Tim Petersee1a53c2003-02-02 02:57:53 +00003287 char *nbytes;
3288 unsigned char *pdata;
3289 PyObject *along;
3290
3291 assert(size == 1 || size == 4);
3292 i = self->read_func(self, &nbytes, size);
3293 if (i < 0) return -1;
3294
3295 size = calc_binint(nbytes, size);
3296 if (size < 0) {
3297 /* Corrupt or hostile pickle -- we never write one like
3298 * this.
3299 */
Tim Peters0c7c48e2003-02-03 22:07:24 +00003300 PyErr_SetString(UnpicklingError, "LONG pickle has negative "
Tim Petersee1a53c2003-02-02 02:57:53 +00003301 "byte count");
3302 return -1;
3303 }
3304
3305 if (size == 0)
3306 along = PyLong_FromLong(0L);
3307 else {
3308 /* Read the raw little-endian bytes & convert. */
Neal Norwitz529baf22003-02-02 17:08:33 +00003309 i = self->read_func(self, (char **)&pdata, size);
Tim Petersee1a53c2003-02-02 02:57:53 +00003310 if (i < 0) return -1;
3311 along = _PyLong_FromByteArray(pdata, (size_t)size,
3312 1 /* little endian */, 1 /* signed */);
3313 }
3314 if (along == NULL)
3315 return -1;
3316 PDATA_PUSH(self->stack, along, -1);
3317 return 0;
3318}
Tim Peters84e87f32001-03-17 04:50:51 +00003319
Guido van Rossum60456fd1997-04-09 17:36:32 +00003320static int
Tim Peterscba30e22003-02-01 06:24:36 +00003321load_float(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003322{
3323 PyObject *py_float = 0;
3324 char *endptr, *s;
3325 int len, res = -1;
3326 double d;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003327
Tim Peters0bc93f52003-02-02 18:29:33 +00003328 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003329 if (len < 2) return bad_readline();
3330 if (!( s=pystrndup(s,len))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003331
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003332 errno = 0;
Martin v. Löwis737ea822004-06-08 18:52:54 +00003333 d = PyOS_ascii_strtod(s, &endptr);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003334
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003335 if (errno || (endptr[0] != '\n') || (endptr[1] != '\0')) {
3336 PyErr_SetString(PyExc_ValueError,
3337 "could not convert string to float");
3338 goto finally;
3339 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003340
Tim Peterscba30e22003-02-01 06:24:36 +00003341 if (!( py_float = PyFloat_FromDouble(d)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003342 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003343
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003344 free(s);
3345 PDATA_PUSH(self->stack, py_float, -1);
3346 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003347
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003348 finally:
3349 free(s);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003350
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003351 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003352}
3353
Guido van Rossum60456fd1997-04-09 17:36:32 +00003354static int
Tim Peterscba30e22003-02-01 06:24:36 +00003355load_binfloat(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003356{
Tim Peters9905b942003-03-20 20:53:32 +00003357 PyObject *py_float;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003358 double x;
3359 char *p;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003360
Tim Peters0bc93f52003-02-02 18:29:33 +00003361 if (self->read_func(self, &p, 8) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003362 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003363
Tim Peters9905b942003-03-20 20:53:32 +00003364 x = _PyFloat_Unpack8((unsigned char *)p, 0);
3365 if (x == -1.0 && PyErr_Occurred())
3366 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003367
Tim Peters9905b942003-03-20 20:53:32 +00003368 py_float = PyFloat_FromDouble(x);
3369 if (py_float == NULL)
3370 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003371
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003372 PDATA_PUSH(self->stack, py_float, -1);
3373 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003374}
Guido van Rossum60456fd1997-04-09 17:36:32 +00003375
3376static int
Tim Peterscba30e22003-02-01 06:24:36 +00003377load_string(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003378{
3379 PyObject *str = 0;
Martin v. Löwis8a8da792002-08-14 07:46:28 +00003380 int len, res = -1;
3381 char *s, *p;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003382
Tim Peters0bc93f52003-02-02 18:29:33 +00003383 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003384 if (len < 2) return bad_readline();
3385 if (!( s=pystrndup(s,len))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003386
Martin v. Löwis8a8da792002-08-14 07:46:28 +00003387
3388 /* Strip outermost quotes */
3389 while (s[len-1] <= ' ')
3390 len--;
3391 if(s[0]=='"' && s[len-1]=='"'){
3392 s[len-1] = '\0';
3393 p = s + 1 ;
3394 len -= 2;
3395 } else if(s[0]=='\'' && s[len-1]=='\''){
3396 s[len-1] = '\0';
3397 p = s + 1 ;
3398 len -= 2;
3399 } else
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003400 goto insecure;
3401 /********************************************/
Guido van Rossum9716aaa1997-12-08 15:15:16 +00003402
Martin v. Löwis8a8da792002-08-14 07:46:28 +00003403 str = PyString_DecodeEscape(p, len, NULL, 0, NULL);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003404 free(s);
Martin v. Löwis8a8da792002-08-14 07:46:28 +00003405 if (str) {
3406 PDATA_PUSH(self->stack, str, -1);
3407 res = 0;
3408 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003409 return res;
Tim Peters84e87f32001-03-17 04:50:51 +00003410
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003411 insecure:
3412 free(s);
3413 PyErr_SetString(PyExc_ValueError,"insecure string pickle");
3414 return -1;
Tim Peters84e87f32001-03-17 04:50:51 +00003415}
Guido van Rossum60456fd1997-04-09 17:36:32 +00003416
3417
3418static int
Tim Peterscba30e22003-02-01 06:24:36 +00003419load_binstring(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003420{
3421 PyObject *py_string = 0;
3422 long l;
3423 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003424
Tim Peters0bc93f52003-02-02 18:29:33 +00003425 if (self->read_func(self, &s, 4) < 0) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003426
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003427 l = calc_binint(s, 4);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003428
Tim Peters0bc93f52003-02-02 18:29:33 +00003429 if (self->read_func(self, &s, l) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003430 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003431
Tim Peterscba30e22003-02-01 06:24:36 +00003432 if (!( py_string = PyString_FromStringAndSize(s, l)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003433 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003434
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003435 PDATA_PUSH(self->stack, py_string, -1);
3436 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003437}
3438
3439
3440static int
Tim Peterscba30e22003-02-01 06:24:36 +00003441load_short_binstring(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003442{
3443 PyObject *py_string = 0;
3444 unsigned char l;
3445 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003446
Tim Peters0bc93f52003-02-02 18:29:33 +00003447 if (self->read_func(self, &s, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003448 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003449
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003450 l = (unsigned char)s[0];
Guido van Rossum60456fd1997-04-09 17:36:32 +00003451
Tim Peters0bc93f52003-02-02 18:29:33 +00003452 if (self->read_func(self, &s, l) < 0) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003453
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003454 if (!( py_string = PyString_FromStringAndSize(s, l))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003455
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003456 PDATA_PUSH(self->stack, py_string, -1);
3457 return 0;
Tim Peters84e87f32001-03-17 04:50:51 +00003458}
Guido van Rossum60456fd1997-04-09 17:36:32 +00003459
3460
Martin v. Löwis339d0f72001-08-17 18:39:25 +00003461#ifdef Py_USING_UNICODE
Guido van Rossum60456fd1997-04-09 17:36:32 +00003462static int
Tim Peterscba30e22003-02-01 06:24:36 +00003463load_unicode(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003464{
3465 PyObject *str = 0;
3466 int len, res = -1;
3467 char *s;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003468
Tim Peters0bc93f52003-02-02 18:29:33 +00003469 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003470 if (len < 1) return bad_readline();
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003471
Tim Peterscba30e22003-02-01 06:24:36 +00003472 if (!( str = PyUnicode_DecodeRawUnicodeEscape(s, len - 1, NULL)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003473 goto finally;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003474
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003475 PDATA_PUSH(self->stack, str, -1);
3476 return 0;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003477
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003478 finally:
3479 return res;
Tim Peters84e87f32001-03-17 04:50:51 +00003480}
Martin v. Löwis339d0f72001-08-17 18:39:25 +00003481#endif
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003482
3483
Martin v. Löwis339d0f72001-08-17 18:39:25 +00003484#ifdef Py_USING_UNICODE
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003485static int
Tim Peterscba30e22003-02-01 06:24:36 +00003486load_binunicode(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003487{
3488 PyObject *unicode;
3489 long l;
3490 char *s;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003491
Tim Peters0bc93f52003-02-02 18:29:33 +00003492 if (self->read_func(self, &s, 4) < 0) return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003493
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003494 l = calc_binint(s, 4);
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003495
Tim Peters0bc93f52003-02-02 18:29:33 +00003496 if (self->read_func(self, &s, l) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003497 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003498
Tim Peterscba30e22003-02-01 06:24:36 +00003499 if (!( unicode = PyUnicode_DecodeUTF8(s, l, NULL)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003500 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003501
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003502 PDATA_PUSH(self->stack, unicode, -1);
3503 return 0;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003504}
Martin v. Löwis339d0f72001-08-17 18:39:25 +00003505#endif
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003506
3507
3508static int
Tim Peterscba30e22003-02-01 06:24:36 +00003509load_tuple(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003510{
3511 PyObject *tup;
3512 int i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003513
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003514 if ((i = marker(self)) < 0) return -1;
3515 if (!( tup=Pdata_popTuple(self->stack, i))) return -1;
3516 PDATA_PUSH(self->stack, tup, -1);
3517 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003518}
3519
3520static int
Tim Peters1d63c9f2003-02-02 20:29:39 +00003521load_counted_tuple(Unpicklerobject *self, int len)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003522{
Tim Peters1d63c9f2003-02-02 20:29:39 +00003523 PyObject *tup = PyTuple_New(len);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003524
Tim Peters1d63c9f2003-02-02 20:29:39 +00003525 if (tup == NULL)
3526 return -1;
3527
3528 while (--len >= 0) {
3529 PyObject *element;
3530
3531 PDATA_POP(self->stack, element);
3532 if (element == NULL)
3533 return -1;
3534 PyTuple_SET_ITEM(tup, len, element);
3535 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003536 PDATA_PUSH(self->stack, tup, -1);
3537 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003538}
3539
3540static int
Tim Peterscba30e22003-02-01 06:24:36 +00003541load_empty_list(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003542{
3543 PyObject *list;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003544
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003545 if (!( list=PyList_New(0))) return -1;
3546 PDATA_PUSH(self->stack, list, -1);
3547 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003548}
3549
3550static int
Tim Peterscba30e22003-02-01 06:24:36 +00003551load_empty_dict(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003552{
3553 PyObject *dict;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003554
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003555 if (!( dict=PyDict_New())) return -1;
3556 PDATA_PUSH(self->stack, dict, -1);
3557 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003558}
3559
3560
3561static int
Tim Peterscba30e22003-02-01 06:24:36 +00003562load_list(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003563{
3564 PyObject *list = 0;
3565 int i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003566
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003567 if ((i = marker(self)) < 0) return -1;
3568 if (!( list=Pdata_popList(self->stack, i))) return -1;
3569 PDATA_PUSH(self->stack, list, -1);
3570 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003571}
3572
3573static int
Tim Peterscba30e22003-02-01 06:24:36 +00003574load_dict(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003575{
3576 PyObject *dict, *key, *value;
3577 int i, j, k;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003578
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003579 if ((i = marker(self)) < 0) return -1;
3580 j=self->stack->length;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003581
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003582 if (!( dict = PyDict_New())) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003583
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003584 for (k = i+1; k < j; k += 2) {
3585 key =self->stack->data[k-1];
3586 value=self->stack->data[k ];
3587 if (PyDict_SetItem(dict, key, value) < 0) {
3588 Py_DECREF(dict);
3589 return -1;
3590 }
3591 }
3592 Pdata_clear(self->stack, i);
3593 PDATA_PUSH(self->stack, dict, -1);
3594 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003595}
3596
3597static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00003598Instance_New(PyObject *cls, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003599{
Tim Peters1f1b2d22003-02-01 02:16:37 +00003600 PyObject *r = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003601
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003602 if (PyClass_Check(cls)) {
3603 int l;
Tim Peters84e87f32001-03-17 04:50:51 +00003604
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003605 if ((l=PyObject_Size(args)) < 0) goto err;
3606 if (!( l )) {
3607 PyObject *__getinitargs__;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003608
Tim Peterscba30e22003-02-01 06:24:36 +00003609 __getinitargs__ = PyObject_GetAttr(cls,
Jeremy Hyltonf00368f2003-06-16 20:19:49 +00003610 __getinitargs___str);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003611 if (!__getinitargs__) {
Tim Peterscba30e22003-02-01 06:24:36 +00003612 /* We have a class with no __getinitargs__,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003613 so bypass usual construction */
3614 PyObject *inst;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003615
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003616 PyErr_Clear();
Tim Peterscba30e22003-02-01 06:24:36 +00003617 if (!( inst=PyInstance_NewRaw(cls, NULL)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003618 goto err;
3619 return inst;
3620 }
3621 Py_DECREF(__getinitargs__);
3622 }
Tim Peters84e87f32001-03-17 04:50:51 +00003623
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003624 if ((r=PyInstance_New(cls, args, NULL))) return r;
3625 else goto err;
3626 }
Tim Peters84e87f32001-03-17 04:50:51 +00003627
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003628 if ((r=PyObject_CallObject(cls, args))) return r;
Guido van Rossum142eeb81997-08-13 03:14:41 +00003629
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003630 err:
3631 {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003632 PyObject *tp, *v, *tb, *tmp_value;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003633
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003634 PyErr_Fetch(&tp, &v, &tb);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003635 tmp_value = v;
3636 /* NULL occurs when there was a KeyboardInterrupt */
3637 if (tmp_value == NULL)
3638 tmp_value = Py_None;
3639 if ((r = PyTuple_Pack(3, tmp_value, cls, args))) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003640 Py_XDECREF(v);
3641 v=r;
3642 }
3643 PyErr_Restore(tp,v,tb);
3644 }
3645 return NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003646}
Tim Peters84e87f32001-03-17 04:50:51 +00003647
Guido van Rossum60456fd1997-04-09 17:36:32 +00003648
3649static int
Tim Peterscba30e22003-02-01 06:24:36 +00003650load_obj(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003651{
3652 PyObject *class, *tup, *obj=0;
3653 int i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003654
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003655 if ((i = marker(self)) < 0) return -1;
3656 if (!( tup=Pdata_popTuple(self->stack, i+1))) return -1;
3657 PDATA_POP(self->stack, class);
3658 if (class) {
3659 obj = Instance_New(class, tup);
3660 Py_DECREF(class);
3661 }
3662 Py_DECREF(tup);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003663
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003664 if (! obj) return -1;
3665 PDATA_PUSH(self->stack, obj, -1);
3666 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003667}
3668
3669
3670static int
Tim Peterscba30e22003-02-01 06:24:36 +00003671load_inst(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003672{
3673 PyObject *tup, *class=0, *obj=0, *module_name, *class_name;
3674 int i, len;
3675 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003676
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003677 if ((i = marker(self)) < 0) return -1;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003678
Tim Peters0bc93f52003-02-02 18:29:33 +00003679 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003680 if (len < 2) return bad_readline();
3681 module_name = PyString_FromStringAndSize(s, len - 1);
3682 if (!module_name) return -1;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003683
Tim Peters0bc93f52003-02-02 18:29:33 +00003684 if ((len = self->readline_func(self, &s)) >= 0) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003685 if (len < 2) return bad_readline();
3686 if ((class_name = PyString_FromStringAndSize(s, len - 1))) {
Tim Peterscba30e22003-02-01 06:24:36 +00003687 class = find_class(module_name, class_name,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003688 self->find_class);
3689 Py_DECREF(class_name);
3690 }
3691 }
3692 Py_DECREF(module_name);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003693
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003694 if (! class) return -1;
Tim Peters84e87f32001-03-17 04:50:51 +00003695
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003696 if ((tup=Pdata_popTuple(self->stack, i))) {
3697 obj = Instance_New(class, tup);
3698 Py_DECREF(tup);
3699 }
3700 Py_DECREF(class);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003701
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003702 if (! obj) return -1;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003703
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003704 PDATA_PUSH(self->stack, obj, -1);
3705 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003706}
3707
Tim Peterseab7db32003-02-13 18:24:14 +00003708static int
3709load_newobj(Unpicklerobject *self)
3710{
3711 PyObject *args = NULL;
3712 PyObject *clsraw = NULL;
3713 PyTypeObject *cls; /* clsraw cast to its true type */
3714 PyObject *obj;
3715
3716 /* Stack is ... cls argtuple, and we want to call
3717 * cls.__new__(cls, *argtuple).
3718 */
3719 PDATA_POP(self->stack, args);
3720 if (args == NULL) goto Fail;
3721 if (! PyTuple_Check(args)) {
3722 PyErr_SetString(UnpicklingError, "NEWOBJ expected an arg "
3723 "tuple.");
3724 goto Fail;
3725 }
3726
3727 PDATA_POP(self->stack, clsraw);
3728 cls = (PyTypeObject *)clsraw;
3729 if (cls == NULL) goto Fail;
3730 if (! PyType_Check(cls)) {
3731 PyErr_SetString(UnpicklingError, "NEWOBJ class argument "
3732 "isn't a type object");
3733 goto Fail;
3734 }
3735 if (cls->tp_new == NULL) {
3736 PyErr_SetString(UnpicklingError, "NEWOBJ class argument "
3737 "has NULL tp_new");
3738 goto Fail;
3739 }
3740
3741 /* Call __new__. */
3742 obj = cls->tp_new(cls, args, NULL);
3743 if (obj == NULL) goto Fail;
3744
3745 Py_DECREF(args);
3746 Py_DECREF(clsraw);
3747 PDATA_PUSH(self->stack, obj, -1);
3748 return 0;
3749
3750 Fail:
3751 Py_XDECREF(args);
3752 Py_XDECREF(clsraw);
3753 return -1;
3754}
Guido van Rossum60456fd1997-04-09 17:36:32 +00003755
3756static int
Tim Peterscba30e22003-02-01 06:24:36 +00003757load_global(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003758{
3759 PyObject *class = 0, *module_name = 0, *class_name = 0;
3760 int len;
3761 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003762
Tim Peters0bc93f52003-02-02 18:29:33 +00003763 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003764 if (len < 2) return bad_readline();
3765 module_name = PyString_FromStringAndSize(s, len - 1);
3766 if (!module_name) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003767
Tim Peters0bc93f52003-02-02 18:29:33 +00003768 if ((len = self->readline_func(self, &s)) >= 0) {
Tim Peters4e52ca82002-12-07 02:43:28 +00003769 if (len < 2) {
3770 Py_DECREF(module_name);
3771 return bad_readline();
3772 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003773 if ((class_name = PyString_FromStringAndSize(s, len - 1))) {
Tim Peterscba30e22003-02-01 06:24:36 +00003774 class = find_class(module_name, class_name,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003775 self->find_class);
3776 Py_DECREF(class_name);
3777 }
3778 }
3779 Py_DECREF(module_name);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003780
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003781 if (! class) return -1;
3782 PDATA_PUSH(self->stack, class, -1);
3783 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003784}
3785
3786
3787static int
Tim Peterscba30e22003-02-01 06:24:36 +00003788load_persid(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003789{
3790 PyObject *pid = 0;
3791 int len;
3792 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003793
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003794 if (self->pers_func) {
Tim Peters0bc93f52003-02-02 18:29:33 +00003795 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003796 if (len < 2) return bad_readline();
3797
3798 pid = PyString_FromStringAndSize(s, len - 1);
3799 if (!pid) return -1;
3800
3801 if (PyList_Check(self->pers_func)) {
3802 if (PyList_Append(self->pers_func, pid) < 0) {
3803 Py_DECREF(pid);
3804 return -1;
3805 }
3806 }
3807 else {
3808 ARG_TUP(self, pid);
3809 if (self->arg) {
Tim Peterscba30e22003-02-01 06:24:36 +00003810 pid = PyObject_Call(self->pers_func, self->arg,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003811 NULL);
3812 FREE_ARG_TUP(self);
3813 }
3814 }
3815
3816 if (! pid) return -1;
3817
3818 PDATA_PUSH(self->stack, pid, -1);
3819 return 0;
3820 }
3821 else {
3822 PyErr_SetString(UnpicklingError,
3823 "A load persistent id instruction was encountered,\n"
3824 "but no persistent_load function was specified.");
3825 return -1;
3826 }
3827}
3828
3829static int
Tim Peterscba30e22003-02-01 06:24:36 +00003830load_binpersid(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003831{
3832 PyObject *pid = 0;
3833
3834 if (self->pers_func) {
3835 PDATA_POP(self->stack, pid);
3836 if (! pid) return -1;
3837
3838 if (PyList_Check(self->pers_func)) {
3839 if (PyList_Append(self->pers_func, pid) < 0) {
3840 Py_DECREF(pid);
3841 return -1;
3842 }
3843 }
3844 else {
3845 ARG_TUP(self, pid);
3846 if (self->arg) {
3847 pid = PyObject_Call(self->pers_func, self->arg,
3848 NULL);
3849 FREE_ARG_TUP(self);
3850 }
3851 if (! pid) return -1;
3852 }
3853
3854 PDATA_PUSH(self->stack, pid, -1);
3855 return 0;
3856 }
3857 else {
3858 PyErr_SetString(UnpicklingError,
3859 "A load persistent id instruction was encountered,\n"
3860 "but no persistent_load function was specified.");
3861 return -1;
3862 }
3863}
3864
3865
3866static int
Tim Peterscba30e22003-02-01 06:24:36 +00003867load_pop(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003868{
3869 int len;
3870
3871 if (!( (len=self->stack->length) > 0 )) return stackUnderflow();
3872
3873 /* Note that we split the (pickle.py) stack into two stacks,
3874 an object stack and a mark stack. We have to be clever and
3875 pop the right one. We do this by looking at the top of the
3876 mark stack.
3877 */
3878
3879 if ((self->num_marks > 0) &&
3880 (self->marks[self->num_marks - 1] == len))
3881 self->num_marks--;
3882 else {
3883 len--;
3884 Py_DECREF(self->stack->data[len]);
3885 self->stack->length=len;
3886 }
3887
3888 return 0;
3889}
3890
3891
3892static int
Tim Peterscba30e22003-02-01 06:24:36 +00003893load_pop_mark(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003894{
3895 int i;
3896
3897 if ((i = marker(self)) < 0)
3898 return -1;
3899
3900 Pdata_clear(self->stack, i);
3901
3902 return 0;
3903}
3904
3905
3906static int
Tim Peterscba30e22003-02-01 06:24:36 +00003907load_dup(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003908{
3909 PyObject *last;
3910 int len;
3911
3912 if ((len = self->stack->length) <= 0) return stackUnderflow();
3913 last=self->stack->data[len-1];
3914 Py_INCREF(last);
3915 PDATA_PUSH(self->stack, last, -1);
3916 return 0;
3917}
3918
3919
3920static int
Tim Peterscba30e22003-02-01 06:24:36 +00003921load_get(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003922{
3923 PyObject *py_str = 0, *value = 0;
3924 int len;
3925 char *s;
3926 int rc;
3927
Tim Peters0bc93f52003-02-02 18:29:33 +00003928 if ((len = self->readline_func(self, &s)) < 0) return -1;
Guido van Rossumf9ffb031999-02-04 14:54:04 +00003929 if (len < 2) return bad_readline();
Tim Peters84e87f32001-03-17 04:50:51 +00003930
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003931 if (!( py_str = PyString_FromStringAndSize(s, len - 1))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003932
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003933 value = PyDict_GetItem(self->memo, py_str);
3934 if (! value) {
3935 PyErr_SetObject(BadPickleGet, py_str);
3936 rc = -1;
Tim Peters92c8bb32003-02-13 23:00:26 +00003937 }
3938 else {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003939 PDATA_APPEND(self->stack, value, -1);
3940 rc = 0;
3941 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003942
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003943 Py_DECREF(py_str);
3944 return rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003945}
3946
3947
3948static int
Tim Peterscba30e22003-02-01 06:24:36 +00003949load_binget(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003950{
3951 PyObject *py_key = 0, *value = 0;
3952 unsigned char key;
3953 char *s;
3954 int rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003955
Tim Peters0bc93f52003-02-02 18:29:33 +00003956 if (self->read_func(self, &s, 1) < 0) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003957
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003958 key = (unsigned char)s[0];
3959 if (!( py_key = PyInt_FromLong((long)key))) return -1;
Guido van Rossumea2b7152000-05-09 18:14:50 +00003960
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003961 value = PyDict_GetItem(self->memo, py_key);
3962 if (! value) {
3963 PyErr_SetObject(BadPickleGet, py_key);
3964 rc = -1;
Tim Peters92c8bb32003-02-13 23:00:26 +00003965 }
3966 else {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003967 PDATA_APPEND(self->stack, value, -1);
3968 rc = 0;
3969 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003970
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003971 Py_DECREF(py_key);
3972 return rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003973}
3974
3975
3976static int
Tim Peterscba30e22003-02-01 06:24:36 +00003977load_long_binget(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003978{
3979 PyObject *py_key = 0, *value = 0;
3980 unsigned char c;
3981 char *s;
3982 long key;
3983 int rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003984
Tim Peters0bc93f52003-02-02 18:29:33 +00003985 if (self->read_func(self, &s, 4) < 0) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003986
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003987 c = (unsigned char)s[0];
3988 key = (long)c;
3989 c = (unsigned char)s[1];
3990 key |= (long)c << 8;
3991 c = (unsigned char)s[2];
3992 key |= (long)c << 16;
3993 c = (unsigned char)s[3];
3994 key |= (long)c << 24;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003995
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003996 if (!( py_key = PyInt_FromLong((long)key))) return -1;
3997
3998 value = PyDict_GetItem(self->memo, py_key);
3999 if (! value) {
4000 PyErr_SetObject(BadPickleGet, py_key);
4001 rc = -1;
Tim Peters92c8bb32003-02-13 23:00:26 +00004002 }
4003 else {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004004 PDATA_APPEND(self->stack, value, -1);
4005 rc = 0;
4006 }
4007
4008 Py_DECREF(py_key);
4009 return rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004010}
4011
Tim Peters2d629652003-02-04 05:06:17 +00004012/* Push an object from the extension registry (EXT[124]). nbytes is
4013 * the number of bytes following the opcode, holding the index (code) value.
4014 */
4015static int
4016load_extension(Unpicklerobject *self, int nbytes)
4017{
4018 char *codebytes; /* the nbytes bytes after the opcode */
4019 long code; /* calc_binint returns long */
4020 PyObject *py_code; /* code as a Python int */
4021 PyObject *obj; /* the object to push */
4022 PyObject *pair; /* (module_name, class_name) */
4023 PyObject *module_name, *class_name;
4024
4025 assert(nbytes == 1 || nbytes == 2 || nbytes == 4);
4026 if (self->read_func(self, &codebytes, nbytes) < 0) return -1;
4027 code = calc_binint(codebytes, nbytes);
4028 if (code <= 0) { /* note that 0 is forbidden */
4029 /* Corrupt or hostile pickle. */
4030 PyErr_SetString(UnpicklingError, "EXT specifies code <= 0");
4031 return -1;
4032 }
4033
4034 /* Look for the code in the cache. */
4035 py_code = PyInt_FromLong(code);
4036 if (py_code == NULL) return -1;
4037 obj = PyDict_GetItem(extension_cache, py_code);
4038 if (obj != NULL) {
4039 /* Bingo. */
4040 Py_DECREF(py_code);
4041 PDATA_APPEND(self->stack, obj, -1);
4042 return 0;
4043 }
4044
4045 /* Look up the (module_name, class_name) pair. */
4046 pair = PyDict_GetItem(inverted_registry, py_code);
4047 if (pair == NULL) {
4048 Py_DECREF(py_code);
4049 PyErr_Format(PyExc_ValueError, "unregistered extension "
4050 "code %ld", code);
4051 return -1;
4052 }
4053 /* Since the extension registry is manipulable via Python code,
Tim Petersfa05ce32003-02-04 05:20:32 +00004054 * confirm that pair is really a 2-tuple of strings.
Tim Peters2d629652003-02-04 05:06:17 +00004055 */
4056 if (!PyTuple_Check(pair) || PyTuple_Size(pair) != 2 ||
4057 !PyString_Check(module_name = PyTuple_GET_ITEM(pair, 0)) ||
4058 !PyString_Check(class_name = PyTuple_GET_ITEM(pair, 1))) {
4059 Py_DECREF(py_code);
4060 PyErr_Format(PyExc_ValueError, "_inverted_registry[%ld] "
4061 "isn't a 2-tuple of strings", code);
4062 return -1;
4063 }
4064 /* Load the object. */
4065 obj = find_class(module_name, class_name, self->find_class);
4066 if (obj == NULL) {
4067 Py_DECREF(py_code);
4068 return -1;
4069 }
4070 /* Cache code -> obj. */
4071 code = PyDict_SetItem(extension_cache, py_code, obj);
4072 Py_DECREF(py_code);
4073 if (code < 0) {
4074 Py_DECREF(obj);
4075 return -1;
4076 }
4077 PDATA_PUSH(self->stack, obj, -1);
4078 return 0;
4079}
Guido van Rossum60456fd1997-04-09 17:36:32 +00004080
4081static int
Tim Peterscba30e22003-02-01 06:24:36 +00004082load_put(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004083{
4084 PyObject *py_str = 0, *value = 0;
4085 int len, l;
4086 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004087
Tim Peters0bc93f52003-02-02 18:29:33 +00004088 if ((l = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004089 if (l < 2) return bad_readline();
4090 if (!( len=self->stack->length )) return stackUnderflow();
4091 if (!( py_str = PyString_FromStringAndSize(s, l - 1))) return -1;
4092 value=self->stack->data[len-1];
4093 l=PyDict_SetItem(self->memo, py_str, value);
4094 Py_DECREF(py_str);
4095 return l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004096}
4097
4098
4099static int
Tim Peterscba30e22003-02-01 06:24:36 +00004100load_binput(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004101{
4102 PyObject *py_key = 0, *value = 0;
4103 unsigned char key;
4104 char *s;
4105 int len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004106
Tim Peters0bc93f52003-02-02 18:29:33 +00004107 if (self->read_func(self, &s, 1) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004108 if (!( (len=self->stack->length) > 0 )) return stackUnderflow();
Guido van Rossum60456fd1997-04-09 17:36:32 +00004109
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004110 key = (unsigned char)s[0];
Guido van Rossum053b8df1998-11-25 16:18:00 +00004111
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004112 if (!( py_key = PyInt_FromLong((long)key))) return -1;
4113 value=self->stack->data[len-1];
4114 len=PyDict_SetItem(self->memo, py_key, value);
4115 Py_DECREF(py_key);
4116 return len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004117}
4118
4119
4120static int
Tim Peterscba30e22003-02-01 06:24:36 +00004121load_long_binput(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004122{
4123 PyObject *py_key = 0, *value = 0;
4124 long key;
4125 unsigned char c;
4126 char *s;
4127 int len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004128
Tim Peters0bc93f52003-02-02 18:29:33 +00004129 if (self->read_func(self, &s, 4) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004130 if (!( len=self->stack->length )) return stackUnderflow();
Guido van Rossum60456fd1997-04-09 17:36:32 +00004131
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004132 c = (unsigned char)s[0];
4133 key = (long)c;
4134 c = (unsigned char)s[1];
4135 key |= (long)c << 8;
4136 c = (unsigned char)s[2];
4137 key |= (long)c << 16;
4138 c = (unsigned char)s[3];
4139 key |= (long)c << 24;
Tim Peters84e87f32001-03-17 04:50:51 +00004140
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004141 if (!( py_key = PyInt_FromLong(key))) return -1;
4142 value=self->stack->data[len-1];
4143 len=PyDict_SetItem(self->memo, py_key, value);
4144 Py_DECREF(py_key);
4145 return len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004146}
4147
4148
4149static int
Tim Peterscba30e22003-02-01 06:24:36 +00004150do_append(Unpicklerobject *self, int x)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004151{
4152 PyObject *value = 0, *list = 0, *append_method = 0;
4153 int len, i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004154
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004155 len=self->stack->length;
4156 if (!( len >= x && x > 0 )) return stackUnderflow();
4157 /* nothing to do */
Tim Peterscba30e22003-02-01 06:24:36 +00004158 if (len==x) return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004159
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004160 list=self->stack->data[x-1];
Guido van Rossum60456fd1997-04-09 17:36:32 +00004161
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004162 if (PyList_Check(list)) {
4163 PyObject *slice;
4164 int list_len;
Tim Peters84e87f32001-03-17 04:50:51 +00004165
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004166 slice=Pdata_popList(self->stack, x);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004167 if (! slice) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004168 list_len = PyList_GET_SIZE(list);
4169 i=PyList_SetSlice(list, list_len, list_len, slice);
4170 Py_DECREF(slice);
4171 return i;
4172 }
4173 else {
Guido van Rossum60456fd1997-04-09 17:36:32 +00004174
Tim Peterscba30e22003-02-01 06:24:36 +00004175 if (!( append_method = PyObject_GetAttr(list, append_str)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004176 return -1;
4177
4178 for (i = x; i < len; i++) {
4179 PyObject *junk;
4180
4181 value=self->stack->data[i];
4182 junk=0;
4183 ARG_TUP(self, value);
4184 if (self->arg) {
Tim Peterscba30e22003-02-01 06:24:36 +00004185 junk = PyObject_Call(append_method, self->arg,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004186 NULL);
4187 FREE_ARG_TUP(self);
4188 }
4189 if (! junk) {
4190 Pdata_clear(self->stack, i+1);
4191 self->stack->length=x;
4192 Py_DECREF(append_method);
4193 return -1;
4194 }
4195 Py_DECREF(junk);
4196 }
4197 self->stack->length=x;
4198 Py_DECREF(append_method);
4199 }
4200
4201 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004202}
4203
4204
4205static int
Tim Peterscba30e22003-02-01 06:24:36 +00004206load_append(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004207{
4208 return do_append(self, self->stack->length - 1);
Guido van Rossum60456fd1997-04-09 17:36:32 +00004209}
4210
4211
4212static int
Tim Peterscba30e22003-02-01 06:24:36 +00004213load_appends(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004214{
4215 return do_append(self, marker(self));
Guido van Rossum60456fd1997-04-09 17:36:32 +00004216}
4217
4218
4219static int
Tim Peterscba30e22003-02-01 06:24:36 +00004220do_setitems(Unpicklerobject *self, int x)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004221{
4222 PyObject *value = 0, *key = 0, *dict = 0;
4223 int len, i, r=0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004224
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004225 if (!( (len=self->stack->length) >= x
4226 && x > 0 )) return stackUnderflow();
Guido van Rossum60456fd1997-04-09 17:36:32 +00004227
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004228 dict=self->stack->data[x-1];
Guido van Rossum60456fd1997-04-09 17:36:32 +00004229
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004230 for (i = x+1; i < len; i += 2) {
4231 key =self->stack->data[i-1];
4232 value=self->stack->data[i ];
4233 if (PyObject_SetItem(dict, key, value) < 0) {
4234 r=-1;
4235 break;
4236 }
4237 }
4238
4239 Pdata_clear(self->stack, x);
4240
4241 return r;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004242}
4243
4244
Tim Peters84e87f32001-03-17 04:50:51 +00004245static int
Tim Peterscba30e22003-02-01 06:24:36 +00004246load_setitem(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004247{
4248 return do_setitems(self, self->stack->length - 2);
4249}
Guido van Rossum60456fd1997-04-09 17:36:32 +00004250
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004251static int
Tim Peterscba30e22003-02-01 06:24:36 +00004252load_setitems(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004253{
4254 return do_setitems(self, marker(self));
Guido van Rossum60456fd1997-04-09 17:36:32 +00004255}
4256
Tim Peters84e87f32001-03-17 04:50:51 +00004257
Guido van Rossum60456fd1997-04-09 17:36:32 +00004258static int
Tim Peterscba30e22003-02-01 06:24:36 +00004259load_build(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004260{
Tim Peters080c88b2003-02-15 03:01:11 +00004261 PyObject *state, *inst, *slotstate;
4262 PyObject *__setstate__;
4263 PyObject *d_key, *d_value;
Martin v. Löwis18e16552006-02-15 17:27:45 +00004264 Py_ssize_t i;
Tim Peters080c88b2003-02-15 03:01:11 +00004265 int res = -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004266
Tim Peters080c88b2003-02-15 03:01:11 +00004267 /* Stack is ... instance, state. We want to leave instance at
4268 * the stack top, possibly mutated via instance.__setstate__(state).
4269 */
4270 if (self->stack->length < 2)
4271 return stackUnderflow();
4272 PDATA_POP(self->stack, state);
4273 if (state == NULL)
4274 return -1;
4275 inst = self->stack->data[self->stack->length - 1];
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004276
Tim Peters080c88b2003-02-15 03:01:11 +00004277 __setstate__ = PyObject_GetAttr(inst, __setstate___str);
4278 if (__setstate__ != NULL) {
4279 PyObject *junk = NULL;
4280
4281 /* The explicit __setstate__ is responsible for everything. */
4282 ARG_TUP(self, state);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004283 if (self->arg) {
4284 junk = PyObject_Call(__setstate__, self->arg, NULL);
4285 FREE_ARG_TUP(self);
4286 }
4287 Py_DECREF(__setstate__);
Tim Peters080c88b2003-02-15 03:01:11 +00004288 if (junk == NULL)
4289 return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004290 Py_DECREF(junk);
4291 return 0;
4292 }
Jeremy Hyltonf00368f2003-06-16 20:19:49 +00004293 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4294 return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004295 PyErr_Clear();
Tim Peters080c88b2003-02-15 03:01:11 +00004296
4297 /* A default __setstate__. First see whether state embeds a
4298 * slot state dict too (a proto 2 addition).
4299 */
4300 if (PyTuple_Check(state) && PyTuple_Size(state) == 2) {
4301 PyObject *temp = state;
4302 state = PyTuple_GET_ITEM(temp, 0);
4303 slotstate = PyTuple_GET_ITEM(temp, 1);
4304 Py_INCREF(state);
4305 Py_INCREF(slotstate);
4306 Py_DECREF(temp);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004307 }
Tim Peters080c88b2003-02-15 03:01:11 +00004308 else
4309 slotstate = NULL;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004310
Tim Peters080c88b2003-02-15 03:01:11 +00004311 /* Set inst.__dict__ from the state dict (if any). */
4312 if (state != Py_None) {
4313 PyObject *dict;
4314 if (! PyDict_Check(state)) {
4315 PyErr_SetString(UnpicklingError, "state is not a "
4316 "dictionary");
4317 goto finally;
4318 }
4319 dict = PyObject_GetAttr(inst, __dict___str);
4320 if (dict == NULL)
4321 goto finally;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004322
Tim Peters080c88b2003-02-15 03:01:11 +00004323 i = 0;
4324 while (PyDict_Next(state, &i, &d_key, &d_value)) {
4325 if (PyObject_SetItem(dict, d_key, d_value) < 0)
4326 goto finally;
4327 }
4328 Py_DECREF(dict);
4329 }
4330
4331 /* Also set instance attributes from the slotstate dict (if any). */
4332 if (slotstate != NULL) {
4333 if (! PyDict_Check(slotstate)) {
4334 PyErr_SetString(UnpicklingError, "slot state is not "
4335 "a dictionary");
4336 goto finally;
4337 }
4338 i = 0;
4339 while (PyDict_Next(slotstate, &i, &d_key, &d_value)) {
4340 if (PyObject_SetAttr(inst, d_key, d_value) < 0)
4341 goto finally;
4342 }
4343 }
4344 res = 0;
4345
4346 finally:
4347 Py_DECREF(state);
4348 Py_XDECREF(slotstate);
4349 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004350}
4351
4352
4353static int
Tim Peterscba30e22003-02-01 06:24:36 +00004354load_mark(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004355{
4356 int s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004357
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004358 /* Note that we split the (pickle.py) stack into two stacks, an
4359 object stack and a mark stack. Here we push a mark onto the
4360 mark stack.
4361 */
Guido van Rossum60456fd1997-04-09 17:36:32 +00004362
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004363 if ((self->num_marks + 1) >= self->marks_size) {
4364 s=self->marks_size+20;
4365 if (s <= self->num_marks) s=self->num_marks + 1;
4366 if (self->marks == NULL)
4367 self->marks=(int *)malloc(s * sizeof(int));
4368 else
Tim Peterscba30e22003-02-01 06:24:36 +00004369 self->marks=(int *)realloc(self->marks,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004370 s * sizeof(int));
4371 if (! self->marks) {
4372 PyErr_NoMemory();
4373 return -1;
4374 }
4375 self->marks_size = s;
4376 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00004377
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004378 self->marks[self->num_marks++] = self->stack->length;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004379
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004380 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004381}
4382
Guido van Rossum60456fd1997-04-09 17:36:32 +00004383static int
Tim Peterscba30e22003-02-01 06:24:36 +00004384load_reduce(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004385{
4386 PyObject *callable = 0, *arg_tup = 0, *ob = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004387
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004388 PDATA_POP(self->stack, arg_tup);
4389 if (! arg_tup) return -1;
4390 PDATA_POP(self->stack, callable);
4391 if (callable) {
4392 ob = Instance_New(callable, arg_tup);
4393 Py_DECREF(callable);
4394 }
4395 Py_DECREF(arg_tup);
Guido van Rossum60456fd1997-04-09 17:36:32 +00004396
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004397 if (! ob) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004398
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004399 PDATA_PUSH(self->stack, ob, -1);
4400 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004401}
Tim Peters84e87f32001-03-17 04:50:51 +00004402
Tim Peters4190fb82003-02-02 16:09:05 +00004403/* Just raises an error if we don't know the protocol specified. PROTO
4404 * is the first opcode for protocols >= 2.
4405 */
4406static int
4407load_proto(Unpicklerobject *self)
4408{
4409 int i;
4410 char *protobyte;
4411
4412 i = self->read_func(self, &protobyte, 1);
4413 if (i < 0)
4414 return -1;
4415
4416 i = calc_binint(protobyte, 1);
4417 /* No point checking for < 0, since calc_binint returns an unsigned
4418 * int when chewing on 1 byte.
4419 */
4420 assert(i >= 0);
Tim Peters8587b3c2003-02-13 15:44:41 +00004421 if (i <= HIGHEST_PROTOCOL)
Tim Peters4190fb82003-02-02 16:09:05 +00004422 return 0;
4423
4424 PyErr_Format(PyExc_ValueError, "unsupported pickle protocol: %d", i);
4425 return -1;
4426}
4427
Guido van Rossum60456fd1997-04-09 17:36:32 +00004428static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00004429load(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004430{
4431 PyObject *err = 0, *val = 0;
4432 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004433
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004434 self->num_marks = 0;
4435 if (self->stack->length) Pdata_clear(self->stack, 0);
Guido van Rossum60456fd1997-04-09 17:36:32 +00004436
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004437 while (1) {
Tim Peters0bc93f52003-02-02 18:29:33 +00004438 if (self->read_func(self, &s, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004439 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004440
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004441 switch (s[0]) {
4442 case NONE:
4443 if (load_none(self) < 0)
4444 break;
4445 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004446
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004447 case BININT:
4448 if (load_binint(self) < 0)
4449 break;
4450 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004451
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004452 case BININT1:
4453 if (load_binint1(self) < 0)
4454 break;
4455 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004456
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004457 case BININT2:
4458 if (load_binint2(self) < 0)
4459 break;
4460 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004461
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004462 case INT:
4463 if (load_int(self) < 0)
4464 break;
4465 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004466
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004467 case LONG:
4468 if (load_long(self) < 0)
4469 break;
4470 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004471
Tim Petersee1a53c2003-02-02 02:57:53 +00004472 case LONG1:
4473 if (load_counted_long(self, 1) < 0)
4474 break;
4475 continue;
4476
4477 case LONG4:
4478 if (load_counted_long(self, 4) < 0)
4479 break;
4480 continue;
4481
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004482 case FLOAT:
4483 if (load_float(self) < 0)
4484 break;
4485 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004486
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004487 case BINFLOAT:
4488 if (load_binfloat(self) < 0)
4489 break;
4490 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004491
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004492 case BINSTRING:
4493 if (load_binstring(self) < 0)
4494 break;
4495 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004496
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004497 case SHORT_BINSTRING:
4498 if (load_short_binstring(self) < 0)
4499 break;
4500 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004501
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004502 case STRING:
4503 if (load_string(self) < 0)
4504 break;
4505 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004506
Martin v. Löwis339d0f72001-08-17 18:39:25 +00004507#ifdef Py_USING_UNICODE
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004508 case UNICODE:
4509 if (load_unicode(self) < 0)
4510 break;
4511 continue;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00004512
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004513 case BINUNICODE:
4514 if (load_binunicode(self) < 0)
4515 break;
4516 continue;
Martin v. Löwis339d0f72001-08-17 18:39:25 +00004517#endif
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00004518
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004519 case EMPTY_TUPLE:
Tim Peters1d63c9f2003-02-02 20:29:39 +00004520 if (load_counted_tuple(self, 0) < 0)
4521 break;
4522 continue;
4523
4524 case TUPLE1:
4525 if (load_counted_tuple(self, 1) < 0)
4526 break;
4527 continue;
4528
4529 case TUPLE2:
4530 if (load_counted_tuple(self, 2) < 0)
4531 break;
4532 continue;
4533
4534 case TUPLE3:
4535 if (load_counted_tuple(self, 3) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004536 break;
4537 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004538
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004539 case TUPLE:
4540 if (load_tuple(self) < 0)
4541 break;
4542 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004543
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004544 case EMPTY_LIST:
4545 if (load_empty_list(self) < 0)
4546 break;
4547 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004548
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004549 case LIST:
4550 if (load_list(self) < 0)
4551 break;
4552 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004553
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004554 case EMPTY_DICT:
4555 if (load_empty_dict(self) < 0)
4556 break;
4557 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004558
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004559 case DICT:
4560 if (load_dict(self) < 0)
4561 break;
4562 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004563
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004564 case OBJ:
4565 if (load_obj(self) < 0)
4566 break;
4567 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004568
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004569 case INST:
4570 if (load_inst(self) < 0)
4571 break;
4572 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004573
Tim Peterseab7db32003-02-13 18:24:14 +00004574 case NEWOBJ:
4575 if (load_newobj(self) < 0)
4576 break;
4577 continue;
4578
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004579 case GLOBAL:
4580 if (load_global(self) < 0)
4581 break;
4582 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004583
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004584 case APPEND:
4585 if (load_append(self) < 0)
4586 break;
4587 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004588
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004589 case APPENDS:
4590 if (load_appends(self) < 0)
4591 break;
4592 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00004593
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004594 case BUILD:
4595 if (load_build(self) < 0)
4596 break;
4597 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00004598
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004599 case DUP:
4600 if (load_dup(self) < 0)
4601 break;
4602 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004603
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004604 case BINGET:
4605 if (load_binget(self) < 0)
4606 break;
4607 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004608
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004609 case LONG_BINGET:
4610 if (load_long_binget(self) < 0)
4611 break;
4612 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00004613
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004614 case GET:
4615 if (load_get(self) < 0)
4616 break;
4617 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004618
Tim Peters2d629652003-02-04 05:06:17 +00004619 case EXT1:
4620 if (load_extension(self, 1) < 0)
4621 break;
4622 continue;
4623
4624 case EXT2:
4625 if (load_extension(self, 2) < 0)
4626 break;
4627 continue;
4628
4629 case EXT4:
4630 if (load_extension(self, 4) < 0)
4631 break;
4632 continue;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004633 case MARK:
4634 if (load_mark(self) < 0)
4635 break;
4636 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004637
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004638 case BINPUT:
4639 if (load_binput(self) < 0)
4640 break;
4641 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004642
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004643 case LONG_BINPUT:
4644 if (load_long_binput(self) < 0)
4645 break;
4646 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00004647
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004648 case PUT:
4649 if (load_put(self) < 0)
4650 break;
4651 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004652
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004653 case POP:
4654 if (load_pop(self) < 0)
4655 break;
4656 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004657
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004658 case POP_MARK:
4659 if (load_pop_mark(self) < 0)
4660 break;
4661 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004662
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004663 case SETITEM:
4664 if (load_setitem(self) < 0)
4665 break;
4666 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004667
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004668 case SETITEMS:
4669 if (load_setitems(self) < 0)
4670 break;
4671 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004672
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004673 case STOP:
4674 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004675
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004676 case PERSID:
4677 if (load_persid(self) < 0)
4678 break;
4679 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004680
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004681 case BINPERSID:
4682 if (load_binpersid(self) < 0)
4683 break;
4684 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004685
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004686 case REDUCE:
4687 if (load_reduce(self) < 0)
4688 break;
4689 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004690
Tim Peters4190fb82003-02-02 16:09:05 +00004691 case PROTO:
4692 if (load_proto(self) < 0)
4693 break;
4694 continue;
4695
Tim Peters3c67d792003-02-02 17:59:11 +00004696 case NEWTRUE:
4697 if (load_bool(self, Py_True) < 0)
4698 break;
4699 continue;
4700
4701 case NEWFALSE:
4702 if (load_bool(self, Py_False) < 0)
4703 break;
4704 continue;
4705
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004706 case '\0':
4707 /* end of file */
4708 PyErr_SetNone(PyExc_EOFError);
4709 break;
Tim Peterscba30e22003-02-01 06:24:36 +00004710
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004711 default:
Tim Peterscba30e22003-02-01 06:24:36 +00004712 cPickle_ErrFormat(UnpicklingError,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004713 "invalid load key, '%s'.",
4714 "c", s[0]);
4715 return NULL;
4716 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00004717
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004718 break;
4719 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00004720
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004721 if ((err = PyErr_Occurred())) {
4722 if (err == PyExc_EOFError) {
4723 PyErr_SetNone(PyExc_EOFError);
4724 }
4725 return NULL;
4726 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00004727
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004728 PDATA_POP(self->stack, val);
4729 return val;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004730}
Tim Peters84e87f32001-03-17 04:50:51 +00004731
Guido van Rossum60456fd1997-04-09 17:36:32 +00004732
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004733/* No-load functions to support noload, which is used to
4734 find persistent references. */
4735
4736static int
Tim Peterscba30e22003-02-01 06:24:36 +00004737noload_obj(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004738{
4739 int i;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004740
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004741 if ((i = marker(self)) < 0) return -1;
4742 return Pdata_clear(self->stack, i+1);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004743}
4744
4745
4746static int
Tim Peterscba30e22003-02-01 06:24:36 +00004747noload_inst(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004748{
4749 int i;
4750 char *s;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004751
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004752 if ((i = marker(self)) < 0) return -1;
4753 Pdata_clear(self->stack, i);
Tim Peters0bc93f52003-02-02 18:29:33 +00004754 if (self->readline_func(self, &s) < 0) return -1;
4755 if (self->readline_func(self, &s) < 0) return -1;
Tim Peterseab7db32003-02-13 18:24:14 +00004756 PDATA_APPEND(self->stack, Py_None, -1);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004757 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004758}
4759
4760static int
Tim Peterseab7db32003-02-13 18:24:14 +00004761noload_newobj(Unpicklerobject *self)
4762{
4763 PyObject *obj;
4764
4765 PDATA_POP(self->stack, obj); /* pop argtuple */
4766 if (obj == NULL) return -1;
4767 Py_DECREF(obj);
4768
4769 PDATA_POP(self->stack, obj); /* pop cls */
4770 if (obj == NULL) return -1;
4771 Py_DECREF(obj);
4772
4773 PDATA_APPEND(self->stack, Py_None, -1);
4774 return 0;
4775}
4776
4777static int
Tim Peterscba30e22003-02-01 06:24:36 +00004778noload_global(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004779{
4780 char *s;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004781
Tim Peters0bc93f52003-02-02 18:29:33 +00004782 if (self->readline_func(self, &s) < 0) return -1;
4783 if (self->readline_func(self, &s) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004784 PDATA_APPEND(self->stack, Py_None,-1);
4785 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004786}
4787
4788static int
Tim Peterscba30e22003-02-01 06:24:36 +00004789noload_reduce(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004790{
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004791
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004792 if (self->stack->length < 2) return stackUnderflow();
4793 Pdata_clear(self->stack, self->stack->length-2);
4794 PDATA_APPEND(self->stack, Py_None,-1);
4795 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004796}
4797
4798static int
4799noload_build(Unpicklerobject *self) {
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004800
Guido van Rossum053b8df1998-11-25 16:18:00 +00004801 if (self->stack->length < 1) return stackUnderflow();
4802 Pdata_clear(self->stack, self->stack->length-1);
Guido van Rossum50f385c1998-12-04 18:48:44 +00004803 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004804}
4805
Tim Peters2d629652003-02-04 05:06:17 +00004806static int
4807noload_extension(Unpicklerobject *self, int nbytes)
4808{
4809 char *codebytes;
4810
4811 assert(nbytes == 1 || nbytes == 2 || nbytes == 4);
4812 if (self->read_func(self, &codebytes, nbytes) < 0) return -1;
4813 PDATA_APPEND(self->stack, Py_None, -1);
4814 return 0;
4815}
4816
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004817
4818static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00004819noload(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004820{
4821 PyObject *err = 0, *val = 0;
4822 char *s;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004823
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004824 self->num_marks = 0;
4825 Pdata_clear(self->stack, 0);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004826
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004827 while (1) {
Tim Peters0bc93f52003-02-02 18:29:33 +00004828 if (self->read_func(self, &s, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004829 break;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004830
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004831 switch (s[0]) {
4832 case NONE:
4833 if (load_none(self) < 0)
4834 break;
4835 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004836
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004837 case BININT:
4838 if (load_binint(self) < 0)
4839 break;
4840 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004841
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004842 case BININT1:
4843 if (load_binint1(self) < 0)
4844 break;
4845 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004846
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004847 case BININT2:
4848 if (load_binint2(self) < 0)
4849 break;
4850 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004851
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004852 case INT:
4853 if (load_int(self) < 0)
4854 break;
4855 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004856
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004857 case LONG:
4858 if (load_long(self) < 0)
4859 break;
4860 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004861
Tim Peters4190fb82003-02-02 16:09:05 +00004862 case LONG1:
4863 if (load_counted_long(self, 1) < 0)
4864 break;
4865 continue;
4866
4867 case LONG4:
4868 if (load_counted_long(self, 4) < 0)
4869 break;
4870 continue;
4871
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004872 case FLOAT:
4873 if (load_float(self) < 0)
4874 break;
4875 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004876
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004877 case BINFLOAT:
4878 if (load_binfloat(self) < 0)
4879 break;
4880 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004881
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004882 case BINSTRING:
4883 if (load_binstring(self) < 0)
4884 break;
4885 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004886
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004887 case SHORT_BINSTRING:
4888 if (load_short_binstring(self) < 0)
4889 break;
4890 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004891
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004892 case STRING:
4893 if (load_string(self) < 0)
4894 break;
4895 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004896
Martin v. Löwis339d0f72001-08-17 18:39:25 +00004897#ifdef Py_USING_UNICODE
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004898 case UNICODE:
4899 if (load_unicode(self) < 0)
4900 break;
4901 continue;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00004902
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004903 case BINUNICODE:
4904 if (load_binunicode(self) < 0)
4905 break;
4906 continue;
Martin v. Löwis339d0f72001-08-17 18:39:25 +00004907#endif
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00004908
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004909 case EMPTY_TUPLE:
Tim Peters1d63c9f2003-02-02 20:29:39 +00004910 if (load_counted_tuple(self, 0) < 0)
4911 break;
4912 continue;
4913
4914 case TUPLE1:
4915 if (load_counted_tuple(self, 1) < 0)
4916 break;
4917 continue;
4918
4919 case TUPLE2:
4920 if (load_counted_tuple(self, 2) < 0)
4921 break;
4922 continue;
4923
4924 case TUPLE3:
4925 if (load_counted_tuple(self, 3) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004926 break;
4927 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004928
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004929 case TUPLE:
4930 if (load_tuple(self) < 0)
4931 break;
4932 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004933
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004934 case EMPTY_LIST:
4935 if (load_empty_list(self) < 0)
4936 break;
4937 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004938
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004939 case LIST:
4940 if (load_list(self) < 0)
4941 break;
4942 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004943
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004944 case EMPTY_DICT:
4945 if (load_empty_dict(self) < 0)
4946 break;
4947 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004948
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004949 case DICT:
4950 if (load_dict(self) < 0)
4951 break;
4952 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004953
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004954 case OBJ:
4955 if (noload_obj(self) < 0)
4956 break;
4957 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004958
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004959 case INST:
4960 if (noload_inst(self) < 0)
4961 break;
4962 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004963
Tim Peterseab7db32003-02-13 18:24:14 +00004964 case NEWOBJ:
4965 if (noload_newobj(self) < 0)
4966 break;
4967 continue;
4968
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004969 case GLOBAL:
4970 if (noload_global(self) < 0)
4971 break;
4972 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004973
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004974 case APPEND:
4975 if (load_append(self) < 0)
4976 break;
4977 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004978
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004979 case APPENDS:
4980 if (load_appends(self) < 0)
4981 break;
4982 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00004983
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004984 case BUILD:
4985 if (noload_build(self) < 0)
4986 break;
4987 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00004988
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004989 case DUP:
4990 if (load_dup(self) < 0)
4991 break;
4992 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004993
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004994 case BINGET:
4995 if (load_binget(self) < 0)
4996 break;
4997 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004998
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004999 case LONG_BINGET:
5000 if (load_long_binget(self) < 0)
5001 break;
5002 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00005003
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005004 case GET:
5005 if (load_get(self) < 0)
5006 break;
5007 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005008
Tim Peters2d629652003-02-04 05:06:17 +00005009 case EXT1:
5010 if (noload_extension(self, 1) < 0)
5011 break;
5012 continue;
5013
5014 case EXT2:
5015 if (noload_extension(self, 2) < 0)
5016 break;
5017 continue;
5018
5019 case EXT4:
5020 if (noload_extension(self, 4) < 0)
5021 break;
5022 continue;
5023
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005024 case MARK:
5025 if (load_mark(self) < 0)
5026 break;
5027 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005028
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005029 case BINPUT:
5030 if (load_binput(self) < 0)
5031 break;
5032 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005033
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005034 case LONG_BINPUT:
5035 if (load_long_binput(self) < 0)
5036 break;
5037 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00005038
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005039 case PUT:
5040 if (load_put(self) < 0)
5041 break;
5042 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005043
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005044 case POP:
5045 if (load_pop(self) < 0)
5046 break;
5047 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005048
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005049 case POP_MARK:
5050 if (load_pop_mark(self) < 0)
5051 break;
5052 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005053
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005054 case SETITEM:
5055 if (load_setitem(self) < 0)
5056 break;
5057 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005058
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005059 case SETITEMS:
5060 if (load_setitems(self) < 0)
5061 break;
5062 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005063
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005064 case STOP:
5065 break;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005066
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005067 case PERSID:
5068 if (load_persid(self) < 0)
5069 break;
5070 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005071
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005072 case BINPERSID:
5073 if (load_binpersid(self) < 0)
5074 break;
5075 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005076
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005077 case REDUCE:
5078 if (noload_reduce(self) < 0)
5079 break;
5080 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005081
Tim Peters4190fb82003-02-02 16:09:05 +00005082 case PROTO:
5083 if (load_proto(self) < 0)
5084 break;
5085 continue;
5086
Tim Peters3c67d792003-02-02 17:59:11 +00005087 case NEWTRUE:
5088 if (load_bool(self, Py_True) < 0)
5089 break;
5090 continue;
5091
5092 case NEWFALSE:
5093 if (load_bool(self, Py_False) < 0)
5094 break;
5095 continue;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005096 default:
Tim Peterscba30e22003-02-01 06:24:36 +00005097 cPickle_ErrFormat(UnpicklingError,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005098 "invalid load key, '%s'.",
5099 "c", s[0]);
5100 return NULL;
5101 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005102
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005103 break;
5104 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005105
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005106 if ((err = PyErr_Occurred())) {
5107 if (err == PyExc_EOFError) {
5108 PyErr_SetNone(PyExc_EOFError);
5109 }
5110 return NULL;
5111 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005112
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005113 PDATA_POP(self->stack, val);
5114 return val;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005115}
Tim Peters84e87f32001-03-17 04:50:51 +00005116
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005117
Guido van Rossum60456fd1997-04-09 17:36:32 +00005118static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00005119Unpickler_load(Unpicklerobject *self, PyObject *unused)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005120{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005121 return load(self);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005122}
5123
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005124static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00005125Unpickler_noload(Unpicklerobject *self, PyObject *unused)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005126{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005127 return noload(self);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005128}
5129
Guido van Rossum60456fd1997-04-09 17:36:32 +00005130
5131static struct PyMethodDef Unpickler_methods[] = {
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00005132 {"load", (PyCFunction)Unpickler_load, METH_NOARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00005133 PyDoc_STR("load() -- Load a pickle")
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005134 },
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00005135 {"noload", (PyCFunction)Unpickler_noload, METH_NOARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00005136 PyDoc_STR(
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005137 "noload() -- not load a pickle, but go through most of the motions\n"
5138 "\n"
5139 "This function can be used to read past a pickle without instantiating\n"
5140 "any objects or importing any modules. It can also be used to find all\n"
5141 "persistent references without instantiating any objects or importing\n"
Neal Norwitz200788c2002-08-13 22:20:41 +00005142 "any modules.\n")
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005143 },
Guido van Rossum60456fd1997-04-09 17:36:32 +00005144 {NULL, NULL} /* sentinel */
5145};
5146
5147
5148static Unpicklerobject *
Tim Peterscba30e22003-02-01 06:24:36 +00005149newUnpicklerobject(PyObject *f)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005150{
5151 Unpicklerobject *self;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005152
Jeremy Hylton7b5ce7f2003-04-09 21:25:30 +00005153 if (!( self = PyObject_GC_New(Unpicklerobject, &Unpicklertype)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005154 return NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005155
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005156 self->file = NULL;
5157 self->arg = NULL;
5158 self->stack = (Pdata*)Pdata_New();
5159 self->pers_func = NULL;
5160 self->last_string = NULL;
5161 self->marks = NULL;
5162 self->num_marks = 0;
5163 self->marks_size = 0;
5164 self->buf_size = 0;
5165 self->read = NULL;
5166 self->readline = NULL;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005167 self->find_class = NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005168
Tim Peterscba30e22003-02-01 06:24:36 +00005169 if (!( self->memo = PyDict_New()))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005170 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005171
Thomas Wouters0e3f5912006-08-11 14:57:12 +00005172 if (!self->stack)
5173 goto err;
5174
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005175 Py_INCREF(f);
5176 self->file = f;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005177
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005178 /* Set read, readline based on type of f */
5179 if (PyFile_Check(f)) {
5180 self->fp = PyFile_AsFile(f);
5181 if (self->fp == NULL) {
Tim Peterscba30e22003-02-01 06:24:36 +00005182 PyErr_SetString(PyExc_ValueError,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005183 "I/O operation on closed file");
5184 goto err;
5185 }
5186 self->read_func = read_file;
5187 self->readline_func = readline_file;
Guido van Rossumc91fcaa1999-03-29 20:00:14 +00005188 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005189 else if (PycStringIO_InputCheck(f)) {
5190 self->fp = NULL;
5191 self->read_func = read_cStringIO;
5192 self->readline_func = readline_cStringIO;
5193 }
5194 else {
Guido van Rossum60456fd1997-04-09 17:36:32 +00005195
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005196 self->fp = NULL;
5197 self->read_func = read_other;
5198 self->readline_func = readline_other;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005199
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005200 if (!( (self->readline = PyObject_GetAttr(f, readline_str)) &&
5201 (self->read = PyObject_GetAttr(f, read_str)))) {
5202 PyErr_Clear();
Tim Peterscba30e22003-02-01 06:24:36 +00005203 PyErr_SetString( PyExc_TypeError,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005204 "argument must have 'read' and "
5205 "'readline' attributes" );
5206 goto err;
5207 }
5208 }
Jeremy Hylton7b5ce7f2003-04-09 21:25:30 +00005209 PyObject_GC_Track(self);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005210
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005211 return self;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005212
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005213 err:
5214 Py_DECREF((PyObject *)self);
5215 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005216}
5217
5218
5219static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00005220get_Unpickler(PyObject *self, PyObject *file)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005221{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005222 return (PyObject *)newUnpicklerobject(file);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005223}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005224
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005225
Guido van Rossum60456fd1997-04-09 17:36:32 +00005226static void
Tim Peterscba30e22003-02-01 06:24:36 +00005227Unpickler_dealloc(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005228{
Jeremy Hylton7b5ce7f2003-04-09 21:25:30 +00005229 PyObject_GC_UnTrack((PyObject *)self);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005230 Py_XDECREF(self->readline);
5231 Py_XDECREF(self->read);
5232 Py_XDECREF(self->file);
5233 Py_XDECREF(self->memo);
5234 Py_XDECREF(self->stack);
5235 Py_XDECREF(self->pers_func);
5236 Py_XDECREF(self->arg);
5237 Py_XDECREF(self->last_string);
Jeremy Hyltonfff093f2003-07-11 19:42:49 +00005238 Py_XDECREF(self->find_class);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005239
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005240 if (self->marks) {
5241 free(self->marks);
5242 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005243
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005244 if (self->buf_size) {
5245 free(self->buf);
5246 }
Tim Peters84e87f32001-03-17 04:50:51 +00005247
Tim Peters3cfe7542003-05-21 21:29:48 +00005248 self->ob_type->tp_free((PyObject *)self);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005249}
5250
Jeremy Hylton7b5ce7f2003-04-09 21:25:30 +00005251static int
5252Unpickler_traverse(Unpicklerobject *self, visitproc visit, void *arg)
5253{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005254 Py_VISIT(self->readline);
5255 Py_VISIT(self->read);
5256 Py_VISIT(self->file);
5257 Py_VISIT(self->memo);
5258 Py_VISIT(self->stack);
5259 Py_VISIT(self->pers_func);
5260 Py_VISIT(self->arg);
5261 Py_VISIT(self->last_string);
5262 Py_VISIT(self->find_class);
Jeremy Hylton7b5ce7f2003-04-09 21:25:30 +00005263 return 0;
5264}
5265
5266static int
5267Unpickler_clear(Unpicklerobject *self)
5268{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005269 Py_CLEAR(self->readline);
5270 Py_CLEAR(self->read);
5271 Py_CLEAR(self->file);
5272 Py_CLEAR(self->memo);
5273 Py_CLEAR(self->stack);
5274 Py_CLEAR(self->pers_func);
5275 Py_CLEAR(self->arg);
5276 Py_CLEAR(self->last_string);
5277 Py_CLEAR(self->find_class);
Jeremy Hylton7b5ce7f2003-04-09 21:25:30 +00005278 return 0;
5279}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005280
5281static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00005282Unpickler_getattr(Unpicklerobject *self, char *name)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005283{
5284 if (!strcmp(name, "persistent_load")) {
5285 if (!self->pers_func) {
5286 PyErr_SetString(PyExc_AttributeError, name);
5287 return NULL;
5288 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005289
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005290 Py_INCREF(self->pers_func);
5291 return self->pers_func;
5292 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005293
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005294 if (!strcmp(name, "find_global")) {
5295 if (!self->find_class) {
5296 PyErr_SetString(PyExc_AttributeError, name);
5297 return NULL;
5298 }
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00005299
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005300 Py_INCREF(self->find_class);
5301 return self->find_class;
5302 }
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00005303
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005304 if (!strcmp(name, "memo")) {
5305 if (!self->memo) {
5306 PyErr_SetString(PyExc_AttributeError, name);
5307 return NULL;
5308 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005309
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005310 Py_INCREF(self->memo);
5311 return self->memo;
5312 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005313
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005314 if (!strcmp(name, "UnpicklingError")) {
5315 Py_INCREF(UnpicklingError);
5316 return UnpicklingError;
5317 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005318
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005319 return Py_FindMethod(Unpickler_methods, (PyObject *)self, name);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005320}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005321
Guido van Rossum60456fd1997-04-09 17:36:32 +00005322
5323static int
Tim Peterscba30e22003-02-01 06:24:36 +00005324Unpickler_setattr(Unpicklerobject *self, char *name, PyObject *value)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005325{
Jeremy Hyltonce616e41998-08-13 23:13:52 +00005326
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005327 if (!strcmp(name, "persistent_load")) {
5328 Py_XDECREF(self->pers_func);
5329 self->pers_func = value;
5330 Py_XINCREF(value);
5331 return 0;
5332 }
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00005333
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005334 if (!strcmp(name, "find_global")) {
5335 Py_XDECREF(self->find_class);
5336 self->find_class = value;
5337 Py_XINCREF(value);
5338 return 0;
5339 }
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00005340
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005341 if (! value) {
5342 PyErr_SetString(PyExc_TypeError,
5343 "attribute deletion is not supported");
5344 return -1;
5345 }
Jeremy Hyltonce616e41998-08-13 23:13:52 +00005346
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005347 if (strcmp(name, "memo") == 0) {
5348 if (!PyDict_Check(value)) {
Tim Peterscba30e22003-02-01 06:24:36 +00005349 PyErr_SetString(PyExc_TypeError,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005350 "memo must be a dictionary");
5351 return -1;
5352 }
5353 Py_XDECREF(self->memo);
5354 self->memo = value;
5355 Py_INCREF(value);
5356 return 0;
5357 }
Jeremy Hyltonce616e41998-08-13 23:13:52 +00005358
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005359 PyErr_SetString(PyExc_AttributeError, name);
5360 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005361}
5362
Tim Peters5bd2a792003-02-01 16:45:06 +00005363/* ---------------------------------------------------------------------------
5364 * Module-level functions.
5365 */
Guido van Rossum60456fd1997-04-09 17:36:32 +00005366
Martin v. Löwis544f1192004-07-27 05:22:33 +00005367/* dump(obj, file, protocol=0). */
Guido van Rossum60456fd1997-04-09 17:36:32 +00005368static PyObject *
Martin v. Löwis544f1192004-07-27 05:22:33 +00005369cpm_dump(PyObject *self, PyObject *args, PyObject *kwds)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005370{
Martin v. Löwis15e62742006-02-27 16:46:16 +00005371 static char *kwlist[] = {"obj", "file", "protocol", NULL};
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005372 PyObject *ob, *file, *res = NULL;
5373 Picklerobject *pickler = 0;
Tim Peters5bd2a792003-02-01 16:45:06 +00005374 int proto = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005375
Martin v. Löwis544f1192004-07-27 05:22:33 +00005376 if (!( PyArg_ParseTupleAndKeywords(args, kwds, "OO|i", kwlist,
5377 &ob, &file, &proto)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005378 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005379
Tim Peters5bd2a792003-02-01 16:45:06 +00005380 if (!( pickler = newPicklerobject(file, proto)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005381 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005382
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005383 if (dump(pickler, ob) < 0)
5384 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005385
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005386 Py_INCREF(Py_None);
5387 res = Py_None;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005388
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005389 finally:
5390 Py_XDECREF(pickler);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005391
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005392 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005393}
5394
5395
Martin v. Löwis544f1192004-07-27 05:22:33 +00005396/* dumps(obj, protocol=0). */
Guido van Rossum60456fd1997-04-09 17:36:32 +00005397static PyObject *
Martin v. Löwis544f1192004-07-27 05:22:33 +00005398cpm_dumps(PyObject *self, PyObject *args, PyObject *kwds)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005399{
Martin v. Löwis15e62742006-02-27 16:46:16 +00005400 static char *kwlist[] = {"obj", "protocol", NULL};
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005401 PyObject *ob, *file = 0, *res = NULL;
5402 Picklerobject *pickler = 0;
Tim Peters5bd2a792003-02-01 16:45:06 +00005403 int proto = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005404
Martin v. Löwis544f1192004-07-27 05:22:33 +00005405 if (!( PyArg_ParseTupleAndKeywords(args, kwds, "O|i:dumps", kwlist,
5406 &ob, &proto)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005407 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005408
Tim Peterscba30e22003-02-01 06:24:36 +00005409 if (!( file = PycStringIO->NewOutput(128)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005410 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005411
Tim Peters5bd2a792003-02-01 16:45:06 +00005412 if (!( pickler = newPicklerobject(file, proto)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005413 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005414
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005415 if (dump(pickler, ob) < 0)
5416 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005417
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005418 res = PycStringIO->cgetvalue(file);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005419
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005420 finally:
5421 Py_XDECREF(pickler);
5422 Py_XDECREF(file);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005423
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005424 return res;
Tim Peters84e87f32001-03-17 04:50:51 +00005425}
5426
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005427
Tim Peters5bd2a792003-02-01 16:45:06 +00005428/* load(fileobj). */
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005429static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00005430cpm_load(PyObject *self, PyObject *ob)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005431{
5432 Unpicklerobject *unpickler = 0;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00005433 PyObject *res = NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005434
Tim Peterscba30e22003-02-01 06:24:36 +00005435 if (!( unpickler = newUnpicklerobject(ob)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005436 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005437
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005438 res = load(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005439
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005440 finally:
5441 Py_XDECREF(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005442
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005443 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005444}
5445
5446
Tim Peters5bd2a792003-02-01 16:45:06 +00005447/* loads(string) */
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005448static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00005449cpm_loads(PyObject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005450{
5451 PyObject *ob, *file = 0, *res = NULL;
5452 Unpicklerobject *unpickler = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005453
Tim Peterscba30e22003-02-01 06:24:36 +00005454 if (!( PyArg_ParseTuple(args, "S:loads", &ob)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005455 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005456
Tim Peterscba30e22003-02-01 06:24:36 +00005457 if (!( file = PycStringIO->NewInput(ob)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005458 goto finally;
Tim Peters84e87f32001-03-17 04:50:51 +00005459
Tim Peterscba30e22003-02-01 06:24:36 +00005460 if (!( unpickler = newUnpicklerobject(file)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005461 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005462
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005463 res = load(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005464
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005465 finally:
5466 Py_XDECREF(file);
5467 Py_XDECREF(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005468
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005469 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005470}
5471
5472
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005473PyDoc_STRVAR(Unpicklertype__doc__,
5474"Objects that know how to unpickle");
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005475
Guido van Rossum9716aaa1997-12-08 15:15:16 +00005476static PyTypeObject Unpicklertype = {
5477 PyObject_HEAD_INIT(NULL)
Jeremy Hylton7b5ce7f2003-04-09 21:25:30 +00005478 0, /*ob_size*/
5479 "cPickle.Unpickler", /*tp_name*/
5480 sizeof(Unpicklerobject), /*tp_basicsize*/
5481 0,
5482 (destructor)Unpickler_dealloc, /* tp_dealloc */
5483 0, /* tp_print */
5484 (getattrfunc)Unpickler_getattr, /* tp_getattr */
5485 (setattrfunc)Unpickler_setattr, /* tp_setattr */
5486 0, /* tp_compare */
5487 0, /* tp_repr */
5488 0, /* tp_as_number */
5489 0, /* tp_as_sequence */
5490 0, /* tp_as_mapping */
5491 0, /* tp_hash */
5492 0, /* tp_call */
5493 0, /* tp_str */
5494 0, /* tp_getattro */
5495 0, /* tp_setattro */
5496 0, /* tp_as_buffer */
5497 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
5498 Unpicklertype__doc__, /* tp_doc */
5499 (traverseproc)Unpickler_traverse, /* tp_traverse */
5500 (inquiry)Unpickler_clear, /* tp_clear */
Guido van Rossum9716aaa1997-12-08 15:15:16 +00005501};
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005502
Guido van Rossum60456fd1997-04-09 17:36:32 +00005503static struct PyMethodDef cPickle_methods[] = {
Martin v. Löwis544f1192004-07-27 05:22:33 +00005504 {"dump", (PyCFunction)cpm_dump, METH_VARARGS | METH_KEYWORDS,
5505 PyDoc_STR("dump(obj, file, protocol=0) -- "
Tim Peters5bd2a792003-02-01 16:45:06 +00005506 "Write an object in pickle format to the given file.\n"
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005507 "\n"
Tim Peters5bd2a792003-02-01 16:45:06 +00005508 "See the Pickler docstring for the meaning of optional argument proto.")
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005509 },
Tim Peters5bd2a792003-02-01 16:45:06 +00005510
Martin v. Löwis544f1192004-07-27 05:22:33 +00005511 {"dumps", (PyCFunction)cpm_dumps, METH_VARARGS | METH_KEYWORDS,
5512 PyDoc_STR("dumps(obj, protocol=0) -- "
Tim Peters5bd2a792003-02-01 16:45:06 +00005513 "Return a string containing an object in pickle format.\n"
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005514 "\n"
Tim Peters5bd2a792003-02-01 16:45:06 +00005515 "See the Pickler docstring for the meaning of optional argument proto.")
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005516 },
Tim Peters5bd2a792003-02-01 16:45:06 +00005517
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00005518 {"load", (PyCFunction)cpm_load, METH_O,
Neal Norwitz200788c2002-08-13 22:20:41 +00005519 PyDoc_STR("load(file) -- Load a pickle from the given file")},
Tim Peters5bd2a792003-02-01 16:45:06 +00005520
Neal Norwitzb0493252002-03-31 14:44:22 +00005521 {"loads", (PyCFunction)cpm_loads, METH_VARARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00005522 PyDoc_STR("loads(string) -- Load a pickle from the given string")},
Tim Peters5bd2a792003-02-01 16:45:06 +00005523
Martin v. Löwis544f1192004-07-27 05:22:33 +00005524 {"Pickler", (PyCFunction)get_Pickler, METH_VARARGS | METH_KEYWORDS,
5525 PyDoc_STR("Pickler(file, protocol=0) -- Create a pickler.\n"
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005526 "\n"
Tim Peters5bd2a792003-02-01 16:45:06 +00005527 "This takes a file-like object for writing a pickle data stream.\n"
5528 "The optional proto argument tells the pickler to use the given\n"
5529 "protocol; supported protocols are 0, 1, 2. The default\n"
5530 "protocol is 0, to be backwards compatible. (Protocol 0 is the\n"
5531 "only protocol that can be written to a file opened in text\n"
5532 "mode and read back successfully. When using a protocol higher\n"
5533 "than 0, make sure the file is opened in binary mode, both when\n"
5534 "pickling and unpickling.)\n"
5535 "\n"
5536 "Protocol 1 is more efficient than protocol 0; protocol 2 is\n"
5537 "more efficient than protocol 1.\n"
5538 "\n"
5539 "Specifying a negative protocol version selects the highest\n"
5540 "protocol version supported. The higher the protocol used, the\n"
5541 "more recent the version of Python needed to read the pickle\n"
5542 "produced.\n"
5543 "\n"
5544 "The file parameter must have a write() method that accepts a single\n"
5545 "string argument. It can thus be an open file object, a StringIO\n"
5546 "object, or any other custom object that meets this interface.\n")
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005547 },
Tim Peters5bd2a792003-02-01 16:45:06 +00005548
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00005549 {"Unpickler", (PyCFunction)get_Unpickler, METH_O,
Tim Peters5bd2a792003-02-01 16:45:06 +00005550 PyDoc_STR("Unpickler(file) -- Create an unpickler.")},
5551
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005552 { NULL, NULL }
5553};
5554
Guido van Rossum60456fd1997-04-09 17:36:32 +00005555static int
Tim Peterscba30e22003-02-01 06:24:36 +00005556init_stuff(PyObject *module_dict)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005557{
5558 PyObject *copy_reg, *t, *r;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005559
Martin v. Löwis43c9d8a2002-04-01 12:34:33 +00005560#define INIT_STR(S) if (!( S ## _str=PyString_InternFromString(#S))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005561
Tim Peters3cfe7542003-05-21 21:29:48 +00005562 if (PyType_Ready(&Unpicklertype) < 0)
5563 return -1;
5564 if (PyType_Ready(&Picklertype) < 0)
5565 return -1;
5566
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005567 INIT_STR(__class__);
5568 INIT_STR(__getinitargs__);
5569 INIT_STR(__dict__);
5570 INIT_STR(__getstate__);
5571 INIT_STR(__setstate__);
5572 INIT_STR(__name__);
5573 INIT_STR(__main__);
5574 INIT_STR(__reduce__);
Guido van Rossumb289b872003-02-19 01:45:13 +00005575 INIT_STR(__reduce_ex__);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005576 INIT_STR(write);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005577 INIT_STR(append);
5578 INIT_STR(read);
5579 INIT_STR(readline);
5580 INIT_STR(copy_reg);
5581 INIT_STR(dispatch_table);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005582
Tim Peterscba30e22003-02-01 06:24:36 +00005583 if (!( copy_reg = PyImport_ImportModule("copy_reg")))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005584 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005585
Tim Peters1f1b2d22003-02-01 02:16:37 +00005586 /* This is special because we want to use a different
5587 one in restricted mode. */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005588 dispatch_table = PyObject_GetAttr(copy_reg, dispatch_table_str);
Tim Peters5b7da392003-02-04 00:21:07 +00005589 if (!dispatch_table) return -1;
5590
5591 extension_registry = PyObject_GetAttrString(copy_reg,
Guido van Rossumd4b920c2003-02-04 01:54:49 +00005592 "_extension_registry");
Tim Peters5b7da392003-02-04 00:21:07 +00005593 if (!extension_registry) return -1;
5594
5595 inverted_registry = PyObject_GetAttrString(copy_reg,
Guido van Rossumd4b920c2003-02-04 01:54:49 +00005596 "_inverted_registry");
Tim Peters5b7da392003-02-04 00:21:07 +00005597 if (!inverted_registry) return -1;
5598
5599 extension_cache = PyObject_GetAttrString(copy_reg,
Guido van Rossumd4b920c2003-02-04 01:54:49 +00005600 "_extension_cache");
Tim Peters5b7da392003-02-04 00:21:07 +00005601 if (!extension_cache) return -1;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005602
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005603 Py_DECREF(copy_reg);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005604
Tim Peters731098b2003-02-04 20:56:09 +00005605 if (!(empty_tuple = PyTuple_New(0)))
5606 return -1;
5607
5608 two_tuple = PyTuple_New(2);
5609 if (two_tuple == NULL)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005610 return -1;
Tim Peters3e667d52003-02-04 21:47:44 +00005611 /* We use this temp container with no regard to refcounts, or to
5612 * keeping containees alive. Exempt from GC, because we don't
5613 * want anything looking at two_tuple() by magic.
5614 */
5615 PyObject_GC_UnTrack(two_tuple);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005616
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005617 /* Ugh */
5618 if (!( t=PyImport_ImportModule("__builtin__"))) return -1;
5619 if (PyDict_SetItemString(module_dict, "__builtins__", t) < 0)
5620 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005621
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005622 if (!( t=PyDict_New())) return -1;
5623 if (!( r=PyRun_String(
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005624 "def __str__(self):\n"
5625 " return self.args and ('%s' % self.args[0]) or '(what)'\n",
5626 Py_file_input,
5627 module_dict, t) )) return -1;
5628 Py_DECREF(r);
Guido van Rossumc03158b1999-06-09 15:23:31 +00005629
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005630 PickleError = PyErr_NewException("cPickle.PickleError", NULL, t);
Tim Peterscba30e22003-02-01 06:24:36 +00005631 if (!PickleError)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005632 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00005633
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005634 Py_DECREF(t);
Guido van Rossumc03158b1999-06-09 15:23:31 +00005635
Tim Peterscba30e22003-02-01 06:24:36 +00005636 PicklingError = PyErr_NewException("cPickle.PicklingError",
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005637 PickleError, NULL);
Tim Peterscba30e22003-02-01 06:24:36 +00005638 if (!PicklingError)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005639 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00005640
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005641 if (!( t=PyDict_New())) return -1;
5642 if (!( r=PyRun_String(
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005643 "def __str__(self):\n"
5644 " a=self.args\n"
5645 " a=a and type(a[0]) or '(what)'\n"
5646 " return 'Cannot pickle %s objects' % a\n"
5647 , Py_file_input,
5648 module_dict, t) )) return -1;
5649 Py_DECREF(r);
Tim Peters84e87f32001-03-17 04:50:51 +00005650
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005651 if (!( UnpickleableError = PyErr_NewException(
Tim Peterscba30e22003-02-01 06:24:36 +00005652 "cPickle.UnpickleableError", PicklingError, t)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005653 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00005654
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005655 Py_DECREF(t);
Guido van Rossumc03158b1999-06-09 15:23:31 +00005656
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005657 if (!( UnpicklingError = PyErr_NewException("cPickle.UnpicklingError",
Tim Peterscba30e22003-02-01 06:24:36 +00005658 PickleError, NULL)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005659 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00005660
Martin v. Löwis658009a2002-09-16 17:26:24 +00005661 if (!( BadPickleGet = PyErr_NewException("cPickle.BadPickleGet",
5662 UnpicklingError, NULL)))
5663 return -1;
Tim Peterscba30e22003-02-01 06:24:36 +00005664
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005665 if (PyDict_SetItemString(module_dict, "PickleError",
5666 PickleError) < 0)
5667 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00005668
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005669 if (PyDict_SetItemString(module_dict, "PicklingError",
5670 PicklingError) < 0)
5671 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00005672
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005673 if (PyDict_SetItemString(module_dict, "UnpicklingError",
5674 UnpicklingError) < 0)
5675 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005676
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005677 if (PyDict_SetItemString(module_dict, "UnpickleableError",
5678 UnpickleableError) < 0)
5679 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005680
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005681 if (PyDict_SetItemString(module_dict, "BadPickleGet",
5682 BadPickleGet) < 0)
5683 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00005684
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005685 PycString_IMPORT;
Guido van Rossum053b8df1998-11-25 16:18:00 +00005686
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005687 return 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005688}
5689
Mark Hammondfe51c6d2002-08-02 02:27:13 +00005690#ifndef PyMODINIT_FUNC /* declarations for DLL import/export */
5691#define PyMODINIT_FUNC void
Guido van Rossumf9ffb031999-02-04 14:54:04 +00005692#endif
Mark Hammondfe51c6d2002-08-02 02:27:13 +00005693PyMODINIT_FUNC
Tim Peterscba30e22003-02-01 06:24:36 +00005694initcPickle(void)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005695{
5696 PyObject *m, *d, *di, *v, *k;
Martin v. Löwis18e16552006-02-15 17:27:45 +00005697 Py_ssize_t i;
Tim Peters5b7da392003-02-04 00:21:07 +00005698 char *rev = "1.71"; /* XXX when does this change? */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005699 PyObject *format_version;
5700 PyObject *compatible_formats;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005701
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005702 Picklertype.ob_type = &PyType_Type;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005703 Unpicklertype.ob_type = &PyType_Type;
5704 PdataType.ob_type = &PyType_Type;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005705
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005706 /* Initialize some pieces. We need to do this before module creation,
Tim Peters5b7da392003-02-04 00:21:07 +00005707 * so we're forced to use a temporary dictionary. :(
5708 */
5709 di = PyDict_New();
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005710 if (!di) return;
5711 if (init_stuff(di) < 0) return;
Guido van Rossumebba4202000-09-07 14:35:37 +00005712
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005713 /* Create the module and add the functions */
5714 m = Py_InitModule4("cPickle", cPickle_methods,
5715 cPickle_module_documentation,
5716 (PyObject*)NULL,PYTHON_API_VERSION);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00005717 if (m == NULL)
5718 return;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005719
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005720 /* Add some symbolic constants to the module */
5721 d = PyModule_GetDict(m);
Tim Peters5b7da392003-02-04 00:21:07 +00005722 v = PyString_FromString(rev);
5723 PyDict_SetItemString(d, "__version__", v);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005724 Py_XDECREF(v);
Barry Warsaw93d29b61997-01-14 17:45:08 +00005725
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005726 /* Copy data from di. Waaa. */
5727 for (i=0; PyDict_Next(di, &i, &k, &v); ) {
5728 if (PyObject_SetItem(d, k, v) < 0) {
5729 Py_DECREF(di);
5730 return;
5731 }
5732 }
5733 Py_DECREF(di);
Guido van Rossumebba4202000-09-07 14:35:37 +00005734
Tim Peters8587b3c2003-02-13 15:44:41 +00005735 i = PyModule_AddIntConstant(m, "HIGHEST_PROTOCOL", HIGHEST_PROTOCOL);
5736 if (i < 0)
5737 return;
5738
Tim Peters5b7da392003-02-04 00:21:07 +00005739 /* These are purely informational; no code uses them. */
5740 /* File format version we write. */
5741 format_version = PyString_FromString("2.0");
5742 /* Format versions we can read. */
5743 compatible_formats = Py_BuildValue("[sssss]",
5744 "1.0", /* Original protocol 0 */
5745 "1.1", /* Protocol 0 + INST */
5746 "1.2", /* Original protocol 1 */
5747 "1.3", /* Protocol 1 + BINFLOAT */
Tim Peters0dd23aa2003-02-04 00:30:46 +00005748 "2.0"); /* Original protocol 2 */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005749 PyDict_SetItemString(d, "format_version", format_version);
5750 PyDict_SetItemString(d, "compatible_formats", compatible_formats);
5751 Py_XDECREF(format_version);
5752 Py_XDECREF(compatible_formats);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005753}