blob: 85fd459318e1c245c1c7975ba0f835a0544a33bf [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++) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000199 Py_DECREF(*p);
Tim Peters1d63c9f2003-02-02 20:29:39 +0000200 }
201 self->length = clearto;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000202
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000203 return 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000204}
205
Tim Peters84e87f32001-03-17 04:50:51 +0000206static int
Tim Peterscba30e22003-02-01 06:24:36 +0000207Pdata_grow(Pdata *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000208{
Tim Peters1d63c9f2003-02-02 20:29:39 +0000209 int bigger;
210 size_t nbytes;
211
Tim Peters1d63c9f2003-02-02 20:29:39 +0000212 bigger = self->size << 1;
Tim Peterse0a39072003-02-03 15:45:56 +0000213 if (bigger <= 0) /* was 0, or new value overflows */
Tim Peters1d63c9f2003-02-02 20:29:39 +0000214 goto nomemory;
215 if ((int)(size_t)bigger != bigger)
216 goto nomemory;
217 nbytes = (size_t)bigger * sizeof(PyObject *);
218 if (nbytes / sizeof(PyObject *) != (size_t)bigger)
219 goto nomemory;
220 self->data = realloc(self->data, nbytes);
221 if (self->data == NULL)
222 goto nomemory;
223 self->size = bigger;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000224 return 0;
Tim Peters1d63c9f2003-02-02 20:29:39 +0000225
226 nomemory:
227 self->size = 0;
228 PyErr_NoMemory();
229 return -1;
Tim Peters84e87f32001-03-17 04:50:51 +0000230}
Guido van Rossum053b8df1998-11-25 16:18:00 +0000231
Tim Peterse0a39072003-02-03 15:45:56 +0000232/* D is a Pdata*. Pop the topmost element and store it into V, which
233 * must be an lvalue holding PyObject*. On stack underflow, UnpicklingError
Tim Peters1d63c9f2003-02-02 20:29:39 +0000234 * is raised and V is set to NULL. D and V may be evaluated several times.
235 */
236#define PDATA_POP(D, V) { \
Tim Peterse0a39072003-02-03 15:45:56 +0000237 if ((D)->length) \
238 (V) = (D)->data[--((D)->length)]; \
239 else { \
240 PyErr_SetString(UnpicklingError, "bad pickle data"); \
241 (V) = NULL; \
242 } \
Guido van Rossum053b8df1998-11-25 16:18:00 +0000243}
244
Tim Peterse0a39072003-02-03 15:45:56 +0000245/* PDATA_PUSH and PDATA_APPEND both push rvalue PyObject* O on to Pdata*
246 * D. If the Pdata stack can't be grown to hold the new value, both
247 * raise MemoryError and execute "return ER". The difference is in ownership
248 * of O after: _PUSH transfers ownership of O from the caller to the stack
249 * (no incref of O is done, and in case of error O is decrefed), while
250 * _APPEND pushes a new reference.
251 */
252
253/* Push O on stack D, giving ownership of O to the stack. */
254#define PDATA_PUSH(D, O, ER) { \
255 if (((Pdata*)(D))->length == ((Pdata*)(D))->size && \
256 Pdata_grow((Pdata*)(D)) < 0) { \
257 Py_DECREF(O); \
258 return ER; \
259 } \
260 ((Pdata*)(D))->data[((Pdata*)(D))->length++] = (O); \
261}
262
263/* Push O on stack D, pushing a new reference. */
264#define PDATA_APPEND(D, O, ER) { \
265 if (((Pdata*)(D))->length == ((Pdata*)(D))->size && \
266 Pdata_grow((Pdata*)(D)) < 0) \
267 return ER; \
268 Py_INCREF(O); \
269 ((Pdata*)(D))->data[((Pdata*)(D))->length++] = (O); \
270}
271
272
Guido van Rossum053b8df1998-11-25 16:18:00 +0000273static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +0000274Pdata_popTuple(Pdata *self, int start)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000275{
276 PyObject *r;
277 int i, j, l;
Tim Peterscba30e22003-02-01 06:24:36 +0000278
Tim Peters1d63c9f2003-02-02 20:29:39 +0000279 l = self->length-start;
280 r = PyTuple_New(l);
281 if (r == NULL)
282 return NULL;
283 for (i = start, j = 0 ; j < l; i++, j++)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000284 PyTuple_SET_ITEM(r, j, self->data[i]);
Tim Peterscba30e22003-02-01 06:24:36 +0000285
Tim Peters1d63c9f2003-02-02 20:29:39 +0000286 self->length = start;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000287 return r;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000288}
289
290static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +0000291Pdata_popList(Pdata *self, int start)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000292{
293 PyObject *r;
294 int i, j, l;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000295
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000296 l=self->length-start;
297 if (!( r=PyList_New(l))) return NULL;
298 for (i=start, j=0 ; j < l; i++, j++)
299 PyList_SET_ITEM(r, j, self->data[i]);
Guido van Rossum053b8df1998-11-25 16:18:00 +0000300
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000301 self->length=start;
302 return r;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000303}
304
Guido van Rossum053b8df1998-11-25 16:18:00 +0000305/*************************************************************************/
306
307#define ARG_TUP(self, o) { \
308 if (self->arg || (self->arg=PyTuple_New(1))) { \
309 Py_XDECREF(PyTuple_GET_ITEM(self->arg,0)); \
310 PyTuple_SET_ITEM(self->arg,0,o); \
311 } \
312 else { \
313 Py_DECREF(o); \
314 } \
315}
316
317#define FREE_ARG_TUP(self) { \
318 if (self->arg->ob_refcnt > 1) { \
319 Py_DECREF(self->arg); \
320 self->arg=NULL; \
321 } \
322 }
323
Thomas Wouters3b6448f2000-07-22 23:56:07 +0000324typedef struct Picklerobject {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000325 PyObject_HEAD
326 FILE *fp;
327 PyObject *write;
328 PyObject *file;
329 PyObject *memo;
330 PyObject *arg;
331 PyObject *pers_func;
332 PyObject *inst_pers_func;
Tim Peters797ec242003-02-01 06:22:36 +0000333
334 /* pickle protocol number, >= 0 */
335 int proto;
336
337 /* bool, true if proto > 0 */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000338 int bin;
Tim Peters797ec242003-02-01 06:22:36 +0000339
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000340 int fast; /* Fast mode doesn't save in memo, don't use if circ ref */
Martin v. Löwis5a395302002-08-04 08:20:23 +0000341 int nesting;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000342 int (*write_func)(struct Picklerobject *, const char *, Py_ssize_t);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000343 char *write_buf;
344 int buf_size;
345 PyObject *dispatch_table;
346 int fast_container; /* count nested container dumps */
347 PyObject *fast_memo;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000348} Picklerobject;
349
Barry Warsaw52acb492001-12-21 20:04:22 +0000350#ifndef PY_CPICKLE_FAST_LIMIT
351#define PY_CPICKLE_FAST_LIMIT 50
352#endif
Jeremy Hyltona0fb1772001-10-12 04:11:06 +0000353
Jeremy Hylton938ace62002-07-17 16:30:39 +0000354static PyTypeObject Picklertype;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000355
Thomas Wouters3b6448f2000-07-22 23:56:07 +0000356typedef struct Unpicklerobject {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000357 PyObject_HEAD
358 FILE *fp;
359 PyObject *file;
360 PyObject *readline;
361 PyObject *read;
362 PyObject *memo;
363 PyObject *arg;
364 Pdata *stack;
365 PyObject *mark;
366 PyObject *pers_func;
367 PyObject *last_string;
368 int *marks;
369 int num_marks;
370 int marks_size;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000371 Py_ssize_t (*read_func)(struct Unpicklerobject *, char **, Py_ssize_t);
372 Py_ssize_t (*readline_func)(struct Unpicklerobject *, char **);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000373 int buf_size;
374 char *buf;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000375 PyObject *find_class;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000376} Unpicklerobject;
Tim Peters84e87f32001-03-17 04:50:51 +0000377
Jeremy Hylton938ace62002-07-17 16:30:39 +0000378static PyTypeObject Unpicklertype;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000379
Thomas Wouters4789b3a2000-07-24 11:36:47 +0000380/* Forward decls that need the above structs */
381static int save(Picklerobject *, PyObject *, int);
382static int put2(Picklerobject *, PyObject *);
383
Guido van Rossumd385d591997-04-09 17:47:47 +0000384static
Guido van Rossum60456fd1997-04-09 17:36:32 +0000385PyObject *
Thomas Wouters3b6448f2000-07-22 23:56:07 +0000386cPickle_ErrFormat(PyObject *ErrType, char *stringformat, char *format, ...)
387{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000388 va_list va;
389 PyObject *args=0, *retval=0;
390 va_start(va, format);
Tim Peterscba30e22003-02-01 06:24:36 +0000391
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000392 if (format) args = Py_VaBuildValue(format, va);
393 va_end(va);
394 if (format && ! args) return NULL;
Tim Peterscba30e22003-02-01 06:24:36 +0000395 if (stringformat && !(retval=PyString_FromString(stringformat)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000396 return NULL;
Tim Peterscba30e22003-02-01 06:24:36 +0000397
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000398 if (retval) {
399 if (args) {
400 PyObject *v;
401 v=PyString_Format(retval, args);
402 Py_DECREF(retval);
403 Py_DECREF(args);
404 if (! v) return NULL;
405 retval=v;
406 }
407 }
408 else
409 if (args) retval=args;
410 else {
411 PyErr_SetObject(ErrType,Py_None);
412 return NULL;
413 }
414 PyErr_SetObject(ErrType,retval);
415 Py_DECREF(retval);
416 return NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000417}
418
Tim Peters84e87f32001-03-17 04:50:51 +0000419static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000420write_file(Picklerobject *self, const char *s, Py_ssize_t n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000421{
422 size_t nbyteswritten;
Tim Peters84e87f32001-03-17 04:50:51 +0000423
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000424 if (s == NULL) {
425 return 0;
426 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000427
Martin v. Löwis18e16552006-02-15 17:27:45 +0000428 if (n > INT_MAX) {
429 /* String too large */
430 return -1;
431 }
432
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000433 Py_BEGIN_ALLOW_THREADS
Jeremy Hylton7fa4bfa2002-07-18 20:58:57 +0000434 nbyteswritten = fwrite(s, sizeof(char), n, self->fp);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000435 Py_END_ALLOW_THREADS
Jeremy Hylton7fa4bfa2002-07-18 20:58:57 +0000436 if (nbyteswritten != (size_t)n) {
437 PyErr_SetFromErrno(PyExc_IOError);
438 return -1;
439 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000440
Martin v. Löwis18e16552006-02-15 17:27:45 +0000441 return (int)n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000442}
443
Tim Peters84e87f32001-03-17 04:50:51 +0000444static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000445write_cStringIO(Picklerobject *self, const char *s, Py_ssize_t n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000446{
447 if (s == NULL) {
448 return 0;
449 }
Tim Peterscba30e22003-02-01 06:24:36 +0000450
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000451 if (PycStringIO->cwrite((PyObject *)self->file, s, n) != n) {
452 return -1;
453 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000454
Martin v. Löwis18e16552006-02-15 17:27:45 +0000455 return (int)n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000456}
457
Tim Peters84e87f32001-03-17 04:50:51 +0000458static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000459write_none(Picklerobject *self, const char *s, Py_ssize_t n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000460{
461 if (s == NULL) return 0;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000462 if (n > INT_MAX) return -1;
463 return (int)n;
Guido van Rossum142eeb81997-08-13 03:14:41 +0000464}
465
Tim Peters84e87f32001-03-17 04:50:51 +0000466static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000467write_other(Picklerobject *self, const char *s, Py_ssize_t _n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000468{
469 PyObject *py_str = 0, *junk = 0;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000470 int n;
Tim Peterscba30e22003-02-01 06:24:36 +0000471
Martin v. Löwis18e16552006-02-15 17:27:45 +0000472 if (_n > INT_MAX)
473 return -1;
474 n = (int)_n;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000475 if (s == NULL) {
476 if (!( self->buf_size )) return 0;
Tim Peterscba30e22003-02-01 06:24:36 +0000477 py_str = PyString_FromStringAndSize(self->write_buf,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000478 self->buf_size);
Tim Peterscba30e22003-02-01 06:24:36 +0000479 if (!py_str)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000480 return -1;
481 }
482 else {
483 if (self->buf_size && (n + self->buf_size) > WRITE_BUF_SIZE) {
484 if (write_other(self, NULL, 0) < 0)
485 return -1;
486 }
Tim Peterscba30e22003-02-01 06:24:36 +0000487
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000488 if (n > WRITE_BUF_SIZE) {
489 if (!( py_str =
Tim Peterscba30e22003-02-01 06:24:36 +0000490 PyString_FromStringAndSize(s, n)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000491 return -1;
492 }
493 else {
494 memcpy(self->write_buf + self->buf_size, s, n);
495 self->buf_size += n;
496 return n;
497 }
498 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000499
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000500 if (self->write) {
501 /* object with write method */
502 ARG_TUP(self, py_str);
503 if (self->arg) {
504 junk = PyObject_Call(self->write, self->arg, NULL);
505 FREE_ARG_TUP(self);
506 }
507 if (junk) Py_DECREF(junk);
508 else return -1;
509 }
510 else
511 PDATA_PUSH(self->file, py_str, -1);
Guido van Rossum60456fd1997-04-09 17:36:32 +0000512
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000513 self->buf_size = 0;
514 return n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000515}
516
517
Martin v. Löwis18e16552006-02-15 17:27:45 +0000518static Py_ssize_t
519read_file(Unpicklerobject *self, char **s, Py_ssize_t n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000520{
521 size_t nbytesread;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000522
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000523 if (self->buf_size == 0) {
524 int size;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000525
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000526 size = ((n < 32) ? 32 : n);
Tim Petersee1a53c2003-02-02 02:57:53 +0000527 if (!( self->buf = (char *)malloc(size))) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000528 PyErr_NoMemory();
529 return -1;
530 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000531
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000532 self->buf_size = size;
533 }
534 else if (n > self->buf_size) {
Tim Petersee1a53c2003-02-02 02:57:53 +0000535 self->buf = (char *)realloc(self->buf, n);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000536 if (!self->buf) {
537 PyErr_NoMemory();
538 return -1;
539 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000540 self->buf_size = n;
541 }
Tim Peters84e87f32001-03-17 04:50:51 +0000542
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000543 Py_BEGIN_ALLOW_THREADS
544 nbytesread = fread(self->buf, sizeof(char), n, self->fp);
545 Py_END_ALLOW_THREADS
546 if (nbytesread != (size_t)n) {
547 if (feof(self->fp)) {
548 PyErr_SetNone(PyExc_EOFError);
549 return -1;
550 }
Tim Peterscba30e22003-02-01 06:24:36 +0000551
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000552 PyErr_SetFromErrno(PyExc_IOError);
553 return -1;
554 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000555
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000556 *s = self->buf;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000557
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000558 return n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000559}
560
561
Martin v. Löwis18e16552006-02-15 17:27:45 +0000562static Py_ssize_t
Tim Peterscba30e22003-02-01 06:24:36 +0000563readline_file(Unpicklerobject *self, char **s)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000564{
565 int i;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000566
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000567 if (self->buf_size == 0) {
Tim Petersee1a53c2003-02-02 02:57:53 +0000568 if (!( self->buf = (char *)malloc(40))) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000569 PyErr_NoMemory();
570 return -1;
571 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000572 self->buf_size = 40;
573 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000574
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000575 i = 0;
576 while (1) {
Tim Petersee1a53c2003-02-02 02:57:53 +0000577 int bigger;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000578 for (; i < (self->buf_size - 1); i++) {
Tim Peterscba30e22003-02-01 06:24:36 +0000579 if (feof(self->fp) ||
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000580 (self->buf[i] = getc(self->fp)) == '\n') {
581 self->buf[i + 1] = '\0';
582 *s = self->buf;
583 return i + 1;
584 }
585 }
Tim Petersee1a53c2003-02-02 02:57:53 +0000586 bigger = self->buf_size << 1;
587 if (bigger <= 0) { /* overflow */
588 PyErr_NoMemory();
589 return -1;
590 }
591 self->buf = (char *)realloc(self->buf, bigger);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000592 if (!self->buf) {
593 PyErr_NoMemory();
594 return -1;
595 }
Tim Petersee1a53c2003-02-02 02:57:53 +0000596 self->buf_size = bigger;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000597 }
Tim Peters84e87f32001-03-17 04:50:51 +0000598}
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000599
600
Martin v. Löwis18e16552006-02-15 17:27:45 +0000601static Py_ssize_t
602read_cStringIO(Unpicklerobject *self, char **s, Py_ssize_t n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000603{
604 char *ptr;
Tim Peterscba30e22003-02-01 06:24:36 +0000605
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000606 if (PycStringIO->cread((PyObject *)self->file, &ptr, n) != n) {
607 PyErr_SetNone(PyExc_EOFError);
608 return -1;
609 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000610
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000611 *s = ptr;
Tim Peterscba30e22003-02-01 06:24:36 +0000612
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000613 return n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000614}
615
616
Martin v. Löwis18e16552006-02-15 17:27:45 +0000617static Py_ssize_t
Tim Peterscba30e22003-02-01 06:24:36 +0000618readline_cStringIO(Unpicklerobject *self, char **s)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000619{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000620 Py_ssize_t n;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000621 char *ptr;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000622
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000623 if ((n = PycStringIO->creadline((PyObject *)self->file, &ptr)) < 0) {
624 return -1;
625 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000626
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000627 *s = ptr;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000628
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000629 return n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000630}
631
632
Martin v. Löwis18e16552006-02-15 17:27:45 +0000633static Py_ssize_t
634read_other(Unpicklerobject *self, char **s, Py_ssize_t n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000635{
636 PyObject *bytes, *str=0;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000637
Martin v. Löwis18e16552006-02-15 17:27:45 +0000638 if (!( bytes = PyInt_FromSsize_t(n))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000639
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000640 ARG_TUP(self, bytes);
641 if (self->arg) {
642 str = PyObject_Call(self->read, self->arg, NULL);
643 FREE_ARG_TUP(self);
644 }
645 if (! str) return -1;
Tim Peterscba30e22003-02-01 06:24:36 +0000646
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000647 Py_XDECREF(self->last_string);
648 self->last_string = str;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000649
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000650 if (! (*s = PyString_AsString(str))) return -1;
651 return n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000652}
653
654
Martin v. Löwis18e16552006-02-15 17:27:45 +0000655static Py_ssize_t
Tim Peterscba30e22003-02-01 06:24:36 +0000656readline_other(Unpicklerobject *self, char **s)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000657{
658 PyObject *str;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000659 Py_ssize_t str_size;
Tim Peterscba30e22003-02-01 06:24:36 +0000660
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000661 if (!( str = PyObject_CallObject(self->readline, empty_tuple))) {
662 return -1;
663 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000664
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000665 if ((str_size = PyString_Size(str)) < 0)
666 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000667
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000668 Py_XDECREF(self->last_string);
669 self->last_string = str;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000670
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000671 if (! (*s = PyString_AsString(str)))
672 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000673
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000674 return str_size;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000675}
676
Tim Petersee1a53c2003-02-02 02:57:53 +0000677/* Copy the first n bytes from s into newly malloc'ed memory, plus a
678 * trailing 0 byte. Return a pointer to that, or NULL if out of memory.
679 * The caller is responsible for free()'ing the return value.
680 */
Guido van Rossum60456fd1997-04-09 17:36:32 +0000681static char *
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000682pystrndup(const char *s, int n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000683{
Tim Petersee1a53c2003-02-02 02:57:53 +0000684 char *r = (char *)malloc(n+1);
685 if (r == NULL)
686 return (char*)PyErr_NoMemory();
687 memcpy(r, s, n);
688 r[n] = 0;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000689 return r;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000690}
691
692
693static int
Tim Peterscba30e22003-02-01 06:24:36 +0000694get(Picklerobject *self, PyObject *id)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000695{
696 PyObject *value, *mv;
697 long c_value;
698 char s[30];
699 size_t len;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000700
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000701 if (!( mv = PyDict_GetItem(self->memo, id))) {
702 PyErr_SetObject(PyExc_KeyError, id);
703 return -1;
704 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000705
Tim Peterscba30e22003-02-01 06:24:36 +0000706 if (!( value = PyTuple_GetItem(mv, 0)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000707 return -1;
Tim Peters84e87f32001-03-17 04:50:51 +0000708
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000709 if (!( PyInt_Check(value))) {
710 PyErr_SetString(PicklingError, "no int where int expected in memo");
711 return -1;
712 }
713 c_value = PyInt_AS_LONG((PyIntObject*)value);
Guido van Rossum60456fd1997-04-09 17:36:32 +0000714
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000715 if (!self->bin) {
716 s[0] = GET;
717 PyOS_snprintf(s + 1, sizeof(s) - 1, "%ld\n", c_value);
718 len = strlen(s);
719 }
720 else if (Pdata_Check(self->file)) {
721 if (write_other(self, NULL, 0) < 0) return -1;
722 PDATA_APPEND(self->file, mv, -1);
723 return 0;
724 }
725 else {
726 if (c_value < 256) {
727 s[0] = BINGET;
728 s[1] = (int)(c_value & 0xff);
729 len = 2;
730 }
731 else {
732 s[0] = LONG_BINGET;
733 s[1] = (int)(c_value & 0xff);
734 s[2] = (int)((c_value >> 8) & 0xff);
735 s[3] = (int)((c_value >> 16) & 0xff);
736 s[4] = (int)((c_value >> 24) & 0xff);
737 len = 5;
738 }
739 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000740
Tim Peters0bc93f52003-02-02 18:29:33 +0000741 if (self->write_func(self, s, len) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000742 return -1;
Tim Peterscba30e22003-02-01 06:24:36 +0000743
Jeremy Hyltona0fb1772001-10-12 04:11:06 +0000744 return 0;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000745}
Jeremy Hyltonce616e41998-08-13 23:13:52 +0000746
Guido van Rossum60456fd1997-04-09 17:36:32 +0000747
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000748static int
Tim Peterscba30e22003-02-01 06:24:36 +0000749put(Picklerobject *self, PyObject *ob)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000750{
Tim Peterscba30e22003-02-01 06:24:36 +0000751 if (ob->ob_refcnt < 2 || self->fast)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000752 return 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000753
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000754 return put2(self, ob);
755}
Guido van Rossum053b8df1998-11-25 16:18:00 +0000756
Guido van Rossum053b8df1998-11-25 16:18:00 +0000757
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000758static int
Tim Peterscba30e22003-02-01 06:24:36 +0000759put2(Picklerobject *self, PyObject *ob)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000760{
761 char c_str[30];
762 int p;
763 size_t len;
764 int res = -1;
765 PyObject *py_ob_id = 0, *memo_len = 0, *t = 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000766
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000767 if (self->fast)
768 return 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000769
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000770 if ((p = PyDict_Size(self->memo)) < 0)
771 goto finally;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000772
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000773 /* Make sure memo keys are positive! */
Tim Peters70b02d72003-02-02 17:26:40 +0000774 /* XXX Why?
775 * XXX And does "positive" really mean non-negative?
776 * XXX pickle.py starts with PUT index 0, not 1. This makes for
777 * XXX gratuitous differences between the pickling modules.
778 */
Tim Peterscba30e22003-02-01 06:24:36 +0000779 p++;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000780
Tim Peterscba30e22003-02-01 06:24:36 +0000781 if (!( py_ob_id = PyLong_FromVoidPtr(ob)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000782 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000783
Tim Peterscba30e22003-02-01 06:24:36 +0000784 if (!( memo_len = PyInt_FromLong(p)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000785 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000786
Tim Peterscba30e22003-02-01 06:24:36 +0000787 if (!( t = PyTuple_New(2)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000788 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000789
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000790 PyTuple_SET_ITEM(t, 0, memo_len);
791 Py_INCREF(memo_len);
792 PyTuple_SET_ITEM(t, 1, ob);
793 Py_INCREF(ob);
794
795 if (PyDict_SetItem(self->memo, py_ob_id, t) < 0)
796 goto finally;
797
798 if (!self->bin) {
799 c_str[0] = PUT;
800 PyOS_snprintf(c_str + 1, sizeof(c_str) - 1, "%d\n", p);
801 len = strlen(c_str);
802 }
803 else if (Pdata_Check(self->file)) {
804 if (write_other(self, NULL, 0) < 0) return -1;
805 PDATA_APPEND(self->file, memo_len, -1);
806 res=0; /* Job well done ;) */
807 goto finally;
808 }
809 else {
810 if (p >= 256) {
811 c_str[0] = LONG_BINPUT;
812 c_str[1] = (int)(p & 0xff);
813 c_str[2] = (int)((p >> 8) & 0xff);
814 c_str[3] = (int)((p >> 16) & 0xff);
815 c_str[4] = (int)((p >> 24) & 0xff);
816 len = 5;
817 }
818 else {
819 c_str[0] = BINPUT;
820 c_str[1] = p;
821 len = 2;
822 }
823 }
824
Tim Peters0bc93f52003-02-02 18:29:33 +0000825 if (self->write_func(self, c_str, len) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000826 goto finally;
827
828 res = 0;
829
830 finally:
831 Py_XDECREF(py_ob_id);
832 Py_XDECREF(memo_len);
833 Py_XDECREF(t);
834
835 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000836}
837
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000838static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +0000839whichmodule(PyObject *global, PyObject *global_name)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000840{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000841 Py_ssize_t i, j;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000842 PyObject *module = 0, *modules_dict = 0,
843 *global_name_attr = 0, *name = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000844
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000845 module = PyObject_GetAttrString(global, "__module__");
Jeremy Hyltonf00368f2003-06-16 20:19:49 +0000846 if (module)
847 return module;
848 if (PyErr_ExceptionMatches(PyExc_AttributeError))
849 PyErr_Clear();
850 else
851 return NULL;
Guido van Rossum45188231997-09-28 05:38:51 +0000852
Tim Peterscba30e22003-02-01 06:24:36 +0000853 if (!( modules_dict = PySys_GetObject("modules")))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000854 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000855
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000856 i = 0;
857 while ((j = PyDict_Next(modules_dict, &i, &name, &module))) {
Guido van Rossum142eeb81997-08-13 03:14:41 +0000858
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000859 if (PyObject_Compare(name, __main___str)==0) continue;
Tim Peters84e87f32001-03-17 04:50:51 +0000860
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000861 global_name_attr = PyObject_GetAttr(module, global_name);
862 if (!global_name_attr) {
Jeremy Hyltonf00368f2003-06-16 20:19:49 +0000863 if (PyErr_ExceptionMatches(PyExc_AttributeError))
864 PyErr_Clear();
865 else
866 return NULL;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000867 continue;
868 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000869
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000870 if (global_name_attr != global) {
871 Py_DECREF(global_name_attr);
872 continue;
873 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000874
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000875 Py_DECREF(global_name_attr);
Guido van Rossum60456fd1997-04-09 17:36:32 +0000876
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000877 break;
878 }
Guido van Rossum142eeb81997-08-13 03:14:41 +0000879
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000880 /* The following implements the rule in pickle.py added in 1.5
881 that used __main__ if no module is found. I don't actually
882 like this rule. jlf
883 */
884 if (!j) {
885 j=1;
886 name=__main___str;
887 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000888
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000889 Py_INCREF(name);
890 return name;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000891}
892
893
Guido van Rossum60456fd1997-04-09 17:36:32 +0000894static int
Jeremy Hylton499ab6a2001-10-15 21:37:58 +0000895fast_save_enter(Picklerobject *self, PyObject *obj)
896{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000897 /* if fast_container < 0, we're doing an error exit. */
898 if (++self->fast_container >= PY_CPICKLE_FAST_LIMIT) {
899 PyObject *key = NULL;
900 if (self->fast_memo == NULL) {
901 self->fast_memo = PyDict_New();
902 if (self->fast_memo == NULL) {
903 self->fast_container = -1;
904 return 0;
905 }
906 }
907 key = PyLong_FromVoidPtr(obj);
908 if (key == NULL)
909 return 0;
910 if (PyDict_GetItem(self->fast_memo, key)) {
Tim Peters4e52ca82002-12-07 02:43:28 +0000911 Py_DECREF(key);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000912 PyErr_Format(PyExc_ValueError,
Tim Peters92c8bb32003-02-13 23:00:26 +0000913 "fast mode: can't pickle cyclic objects "
914 "including object type %s at %p",
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000915 obj->ob_type->tp_name, obj);
916 self->fast_container = -1;
917 return 0;
918 }
919 if (PyDict_SetItem(self->fast_memo, key, Py_None) < 0) {
Tim Peters4e52ca82002-12-07 02:43:28 +0000920 Py_DECREF(key);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000921 self->fast_container = -1;
922 return 0;
923 }
Tim Peters4e52ca82002-12-07 02:43:28 +0000924 Py_DECREF(key);
Jeremy Hylton499ab6a2001-10-15 21:37:58 +0000925 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000926 return 1;
Jeremy Hylton499ab6a2001-10-15 21:37:58 +0000927}
928
Tim Peterscba30e22003-02-01 06:24:36 +0000929int
Jeremy Hylton499ab6a2001-10-15 21:37:58 +0000930fast_save_leave(Picklerobject *self, PyObject *obj)
931{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000932 if (self->fast_container-- >= PY_CPICKLE_FAST_LIMIT) {
933 PyObject *key = PyLong_FromVoidPtr(obj);
934 if (key == NULL)
935 return 0;
936 if (PyDict_DelItem(self->fast_memo, key) < 0) {
Tim Peters4e52ca82002-12-07 02:43:28 +0000937 Py_DECREF(key);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000938 return 0;
939 }
Tim Peters4e52ca82002-12-07 02:43:28 +0000940 Py_DECREF(key);
Jeremy Hylton499ab6a2001-10-15 21:37:58 +0000941 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000942 return 1;
Jeremy Hylton499ab6a2001-10-15 21:37:58 +0000943}
944
945static int
Tim Peterscba30e22003-02-01 06:24:36 +0000946save_none(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000947{
948 static char none = NONE;
Tim Peters0bc93f52003-02-02 18:29:33 +0000949 if (self->write_func(self, &none, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000950 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000951
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000952 return 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000953}
954
Guido van Rossum77f6a652002-04-03 22:41:51 +0000955static int
Tim Peterscba30e22003-02-01 06:24:36 +0000956save_bool(Picklerobject *self, PyObject *args)
Guido van Rossum77f6a652002-04-03 22:41:51 +0000957{
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000958 static const char *buf[2] = {FALSE, TRUE};
Guido van Rossume2763392002-04-05 19:30:08 +0000959 static char len[2] = {sizeof(FALSE)-1, sizeof(TRUE)-1};
Guido van Rossum77f6a652002-04-03 22:41:51 +0000960 long l = PyInt_AS_LONG((PyIntObject *)args);
961
Tim Peters3c67d792003-02-02 17:59:11 +0000962 if (self->proto >= 2) {
963 char opcode = l ? NEWTRUE : NEWFALSE;
964 if (self->write_func(self, &opcode, 1) < 0)
965 return -1;
966 }
967 else if (self->write_func(self, buf[l], len[l]) < 0)
Guido van Rossum77f6a652002-04-03 22:41:51 +0000968 return -1;
Guido van Rossum77f6a652002-04-03 22:41:51 +0000969 return 0;
970}
Tim Peters84e87f32001-03-17 04:50:51 +0000971
Guido van Rossum60456fd1997-04-09 17:36:32 +0000972static int
Tim Peterscba30e22003-02-01 06:24:36 +0000973save_int(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000974{
975 char c_str[32];
976 long l = PyInt_AS_LONG((PyIntObject *)args);
977 int len = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000978
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000979 if (!self->bin
Guido van Rossumde8d6d71997-05-13 18:00:44 +0000980#if SIZEOF_LONG > 4
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000981 || l > 0x7fffffffL
982 || l < -0x80000000L
Guido van Rossumde8d6d71997-05-13 18:00:44 +0000983#endif
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000984 ) {
985 /* Text-mode pickle, or long too big to fit in the 4-byte
986 * signed BININT format: store as a string.
987 */
988 c_str[0] = INT;
989 PyOS_snprintf(c_str + 1, sizeof(c_str) - 1, "%ld\n", l);
Tim Peters0bc93f52003-02-02 18:29:33 +0000990 if (self->write_func(self, c_str, strlen(c_str)) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000991 return -1;
992 }
993 else {
994 /* Binary pickle and l fits in a signed 4-byte int. */
995 c_str[1] = (int)( l & 0xff);
996 c_str[2] = (int)((l >> 8) & 0xff);
997 c_str[3] = (int)((l >> 16) & 0xff);
998 c_str[4] = (int)((l >> 24) & 0xff);
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000999
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001000 if ((c_str[4] == 0) && (c_str[3] == 0)) {
1001 if (c_str[2] == 0) {
1002 c_str[0] = BININT1;
1003 len = 2;
1004 }
1005 else {
1006 c_str[0] = BININT2;
1007 len = 3;
1008 }
1009 }
1010 else {
1011 c_str[0] = BININT;
1012 len = 5;
1013 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001014
Tim Peters0bc93f52003-02-02 18:29:33 +00001015 if (self->write_func(self, c_str, len) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001016 return -1;
1017 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001018
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001019 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001020}
1021
1022
1023static int
Tim Peterscba30e22003-02-01 06:24:36 +00001024save_long(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001025{
Tim Petersee1a53c2003-02-02 02:57:53 +00001026 int size;
1027 int res = -1;
1028 PyObject *repr = NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001029
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001030 static char l = LONG;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001031
Tim Petersee1a53c2003-02-02 02:57:53 +00001032 if (self->proto >= 2) {
1033 /* Linear-time pickling. */
1034 size_t nbits;
1035 size_t nbytes;
1036 unsigned char *pdata;
1037 char c_str[5];
1038 int i;
1039 int sign = _PyLong_Sign(args);
1040
1041 if (sign == 0) {
1042 /* It's 0 -- an empty bytestring. */
1043 c_str[0] = LONG1;
1044 c_str[1] = 0;
1045 i = self->write_func(self, c_str, 2);
1046 if (i < 0) goto finally;
1047 res = 0;
1048 goto finally;
1049 }
1050 nbits = _PyLong_NumBits(args);
1051 if (nbits == (size_t)-1 && PyErr_Occurred())
1052 goto finally;
1053 /* How many bytes do we need? There are nbits >> 3 full
1054 * bytes of data, and nbits & 7 leftover bits. If there
1055 * are any leftover bits, then we clearly need another
1056 * byte. Wnat's not so obvious is that we *probably*
1057 * need another byte even if there aren't any leftovers:
1058 * the most-significant bit of the most-significant byte
1059 * acts like a sign bit, and it's usually got a sense
1060 * opposite of the one we need. The exception is longs
1061 * of the form -(2**(8*j-1)) for j > 0. Such a long is
1062 * its own 256's-complement, so has the right sign bit
1063 * even without the extra byte. That's a pain to check
1064 * for in advance, though, so we always grab an extra
1065 * byte at the start, and cut it back later if possible.
1066 */
1067 nbytes = (nbits >> 3) + 1;
1068 if ((int)nbytes < 0 || (size_t)(int)nbytes != nbytes) {
1069 PyErr_SetString(PyExc_OverflowError, "long too large "
1070 "to pickle");
1071 goto finally;
1072 }
1073 repr = PyString_FromStringAndSize(NULL, (int)nbytes);
1074 if (repr == NULL) goto finally;
1075 pdata = (unsigned char *)PyString_AS_STRING(repr);
1076 i = _PyLong_AsByteArray((PyLongObject *)args,
1077 pdata, nbytes,
1078 1 /* little endian */, 1 /* signed */);
1079 if (i < 0) goto finally;
1080 /* If the long is negative, this may be a byte more than
1081 * needed. This is so iff the MSB is all redundant sign
1082 * bits.
1083 */
1084 if (sign < 0 && nbytes > 1 && pdata[nbytes - 1] == 0xff &&
1085 (pdata[nbytes - 2] & 0x80) != 0)
1086 --nbytes;
1087
1088 if (nbytes < 256) {
1089 c_str[0] = LONG1;
1090 c_str[1] = (char)nbytes;
1091 size = 2;
1092 }
1093 else {
1094 c_str[0] = LONG4;
1095 size = (int)nbytes;
1096 for (i = 1; i < 5; i++) {
1097 c_str[i] = (char)(size & 0xff);
1098 size >>= 8;
1099 }
1100 size = 5;
1101 }
1102 i = self->write_func(self, c_str, size);
1103 if (i < 0) goto finally;
1104 i = self->write_func(self, (char *)pdata, (int)nbytes);
1105 if (i < 0) goto finally;
1106 res = 0;
1107 goto finally;
1108 }
1109
1110 /* proto < 2: write the repr and newline. This is quadratic-time
1111 * (in the number of digits), in both directions.
1112 */
Tim Peterscba30e22003-02-01 06:24:36 +00001113 if (!( repr = PyObject_Repr(args)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001114 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001115
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001116 if ((size = PyString_Size(repr)) < 0)
1117 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001118
Tim Peters0bc93f52003-02-02 18:29:33 +00001119 if (self->write_func(self, &l, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001120 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001121
Tim Peters0bc93f52003-02-02 18:29:33 +00001122 if (self->write_func(self,
1123 PyString_AS_STRING((PyStringObject *)repr),
1124 size) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001125 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001126
Tim Peters0bc93f52003-02-02 18:29:33 +00001127 if (self->write_func(self, "\n", 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001128 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001129
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001130 res = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001131
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001132 finally:
1133 Py_XDECREF(repr);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001134 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001135}
1136
1137
1138static int
Tim Peterscba30e22003-02-01 06:24:36 +00001139save_float(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001140{
1141 double x = PyFloat_AS_DOUBLE((PyFloatObject *)args);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001142
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001143 if (self->bin) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001144 char str[9];
Tim Peters9905b942003-03-20 20:53:32 +00001145 str[0] = BINFLOAT;
1146 if (_PyFloat_Pack8(x, (unsigned char *)&str[1], 0) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001147 return -1;
Tim Peters0bc93f52003-02-02 18:29:33 +00001148 if (self->write_func(self, str, 9) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001149 return -1;
1150 }
1151 else {
1152 char c_str[250];
1153 c_str[0] = FLOAT;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001154 PyOS_ascii_formatd(c_str + 1, sizeof(c_str) - 2, "%.17g", x);
1155 /* Extend the formatted string with a newline character */
1156 strcat(c_str, "\n");
Guido van Rossum60456fd1997-04-09 17:36:32 +00001157
Tim Peters0bc93f52003-02-02 18:29:33 +00001158 if (self->write_func(self, c_str, strlen(c_str)) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001159 return -1;
1160 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001161
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001162 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001163}
1164
1165
1166static int
Tim Peterscba30e22003-02-01 06:24:36 +00001167save_string(Picklerobject *self, PyObject *args, int doput)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001168{
1169 int size, len;
1170 PyObject *repr=0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001171
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001172 if ((size = PyString_Size(args)) < 0)
1173 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001174
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001175 if (!self->bin) {
1176 char *repr_str;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001177
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001178 static char string = STRING;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001179
Tim Peterscba30e22003-02-01 06:24:36 +00001180 if (!( repr = PyObject_Repr(args)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001181 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001182
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001183 if ((len = PyString_Size(repr)) < 0)
1184 goto err;
1185 repr_str = PyString_AS_STRING((PyStringObject *)repr);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001186
Tim Peters0bc93f52003-02-02 18:29:33 +00001187 if (self->write_func(self, &string, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001188 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001189
Tim Peters0bc93f52003-02-02 18:29:33 +00001190 if (self->write_func(self, repr_str, len) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001191 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001192
Tim Peters0bc93f52003-02-02 18:29:33 +00001193 if (self->write_func(self, "\n", 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001194 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001195
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001196 Py_XDECREF(repr);
1197 }
1198 else {
1199 int i;
1200 char c_str[5];
Guido van Rossum60456fd1997-04-09 17:36:32 +00001201
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001202 if ((size = PyString_Size(args)) < 0)
1203 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001204
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001205 if (size < 256) {
1206 c_str[0] = SHORT_BINSTRING;
1207 c_str[1] = size;
1208 len = 2;
1209 }
1210 else {
1211 c_str[0] = BINSTRING;
1212 for (i = 1; i < 5; i++)
1213 c_str[i] = (int)(size >> ((i - 1) * 8));
1214 len = 5;
1215 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001216
Tim Peters0bc93f52003-02-02 18:29:33 +00001217 if (self->write_func(self, c_str, len) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001218 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001219
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001220 if (size > 128 && Pdata_Check(self->file)) {
1221 if (write_other(self, NULL, 0) < 0) return -1;
1222 PDATA_APPEND(self->file, args, -1);
1223 }
1224 else {
Tim Peters0bc93f52003-02-02 18:29:33 +00001225 if (self->write_func(self,
1226 PyString_AS_STRING(
1227 (PyStringObject *)args),
1228 size) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001229 return -1;
1230 }
1231 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001232
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001233 if (doput)
1234 if (put(self, args) < 0)
1235 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001236
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001237 return 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +00001238
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001239 err:
1240 Py_XDECREF(repr);
1241 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001242}
1243
1244
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001245#ifdef Py_USING_UNICODE
Guido van Rossumfb10c3f2000-12-19 02:08:38 +00001246/* A copy of PyUnicode_EncodeRawUnicodeEscape() that also translates
1247 backslash and newline characters to \uXXXX escapes. */
1248static PyObject *
1249modified_EncodeRawUnicodeEscape(const Py_UNICODE *s, int size)
1250{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001251 PyObject *repr;
1252 char *p;
1253 char *q;
Guido van Rossumfb10c3f2000-12-19 02:08:38 +00001254
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001255 static const char *hexdigit = "0123456789ABCDEF";
Guido van Rossumfb10c3f2000-12-19 02:08:38 +00001256
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001257 repr = PyString_FromStringAndSize(NULL, 6 * size);
1258 if (repr == NULL)
1259 return NULL;
1260 if (size == 0)
1261 return repr;
1262
1263 p = q = PyString_AS_STRING(repr);
1264 while (size-- > 0) {
1265 Py_UNICODE ch = *s++;
1266 /* Map 16-bit characters to '\uxxxx' */
1267 if (ch >= 256 || ch == '\\' || ch == '\n') {
1268 *p++ = '\\';
1269 *p++ = 'u';
1270 *p++ = hexdigit[(ch >> 12) & 0xf];
1271 *p++ = hexdigit[(ch >> 8) & 0xf];
1272 *p++ = hexdigit[(ch >> 4) & 0xf];
1273 *p++ = hexdigit[ch & 15];
1274 }
1275 /* Copy everything else as-is */
1276 else
1277 *p++ = (char) ch;
1278 }
1279 *p = '\0';
Tim Peters5de98422002-04-27 18:44:32 +00001280 _PyString_Resize(&repr, p - q);
Guido van Rossumfb10c3f2000-12-19 02:08:38 +00001281 return repr;
Guido van Rossumfb10c3f2000-12-19 02:08:38 +00001282}
1283
1284
Guido van Rossum60456fd1997-04-09 17:36:32 +00001285static int
Tim Peterscba30e22003-02-01 06:24:36 +00001286save_unicode(Picklerobject *self, PyObject *args, int doput)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001287{
1288 int size, len;
1289 PyObject *repr=0;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001290
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001291 if (!PyUnicode_Check(args))
1292 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001293
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001294 if (!self->bin) {
1295 char *repr_str;
1296 static char string = UNICODE;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001297
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001298 repr = modified_EncodeRawUnicodeEscape(
1299 PyUnicode_AS_UNICODE(args), PyUnicode_GET_SIZE(args));
Tim Peterscba30e22003-02-01 06:24:36 +00001300 if (!repr)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001301 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001302
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001303 if ((len = PyString_Size(repr)) < 0)
1304 goto err;
1305 repr_str = PyString_AS_STRING((PyStringObject *)repr);
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001306
Tim Peters0bc93f52003-02-02 18:29:33 +00001307 if (self->write_func(self, &string, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001308 goto err;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001309
Tim Peters0bc93f52003-02-02 18:29:33 +00001310 if (self->write_func(self, repr_str, len) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001311 goto err;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001312
Tim Peters0bc93f52003-02-02 18:29:33 +00001313 if (self->write_func(self, "\n", 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001314 goto err;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001315
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001316 Py_XDECREF(repr);
1317 }
1318 else {
1319 int i;
1320 char c_str[5];
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001321
Tim Peterscba30e22003-02-01 06:24:36 +00001322 if (!( repr = PyUnicode_AsUTF8String(args)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001323 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001324
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001325 if ((size = PyString_Size(repr)) < 0)
1326 goto err;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001327
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001328 c_str[0] = BINUNICODE;
1329 for (i = 1; i < 5; i++)
1330 c_str[i] = (int)(size >> ((i - 1) * 8));
1331 len = 5;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001332
Tim Peters0bc93f52003-02-02 18:29:33 +00001333 if (self->write_func(self, c_str, len) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001334 goto err;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001335
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001336 if (size > 128 && Pdata_Check(self->file)) {
1337 if (write_other(self, NULL, 0) < 0)
1338 goto err;
1339 PDATA_APPEND(self->file, repr, -1);
1340 }
1341 else {
Tim Peters0bc93f52003-02-02 18:29:33 +00001342 if (self->write_func(self, PyString_AS_STRING(repr),
1343 size) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001344 goto err;
1345 }
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001346
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001347 Py_DECREF(repr);
1348 }
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001349
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001350 if (doput)
1351 if (put(self, args) < 0)
1352 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001353
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001354 return 0;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001355
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001356 err:
1357 Py_XDECREF(repr);
1358 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001359}
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001360#endif
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001361
Tim Peters1d63c9f2003-02-02 20:29:39 +00001362/* A helper for save_tuple. Push the len elements in tuple t on the stack. */
1363static int
Tim Peters67920142003-02-05 03:46:17 +00001364store_tuple_elements(Picklerobject *self, PyObject *t, int len)
Tim Peters1d63c9f2003-02-02 20:29:39 +00001365{
1366 int i;
1367 int res = -1; /* guilty until proved innocent */
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001368
Tim Peters1d63c9f2003-02-02 20:29:39 +00001369 assert(PyTuple_Size(t) == len);
1370
1371 for (i = 0; i < len; i++) {
1372 PyObject *element = PyTuple_GET_ITEM(t, i);
1373
1374 if (element == NULL)
1375 goto finally;
1376 if (save(self, element, 0) < 0)
1377 goto finally;
1378 }
1379 res = 0;
1380
1381 finally:
1382 return res;
1383}
1384
1385/* Tuples are ubiquitous in the pickle protocols, so many techniques are
1386 * used across protocols to minimize the space needed to pickle them.
Tim Peters67920142003-02-05 03:46:17 +00001387 * Tuples are also the only builtin immutable type that can be recursive
Tim Peters1d63c9f2003-02-02 20:29:39 +00001388 * (a tuple can be reached from itself), and that requires some subtle
1389 * magic so that it works in all cases. IOW, this is a long routine.
1390 */
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001391static int
Tim Peterscba30e22003-02-01 06:24:36 +00001392save_tuple(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001393{
Tim Peters1d63c9f2003-02-02 20:29:39 +00001394 PyObject *py_tuple_id = NULL;
1395 int len, i;
1396 int res = -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001397
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001398 static char tuple = TUPLE;
Tim Peters1d63c9f2003-02-02 20:29:39 +00001399 static char pop = POP;
1400 static char pop_mark = POP_MARK;
1401 static char len2opcode[] = {EMPTY_TUPLE, TUPLE1, TUPLE2, TUPLE3};
Guido van Rossum60456fd1997-04-09 17:36:32 +00001402
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001403 if ((len = PyTuple_Size(args)) < 0)
1404 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001405
Tim Peters1d63c9f2003-02-02 20:29:39 +00001406 if (len == 0) {
1407 char c_str[2];
Tim Peters84e87f32001-03-17 04:50:51 +00001408
Tim Peters1d63c9f2003-02-02 20:29:39 +00001409 if (self->proto) {
1410 c_str[0] = EMPTY_TUPLE;
1411 len = 1;
1412 }
1413 else {
1414 c_str[0] = MARK;
1415 c_str[1] = TUPLE;
1416 len = 2;
1417 }
1418 if (self->write_func(self, c_str, len) >= 0)
1419 res = 0;
1420 /* Don't memoize an empty tuple. */
1421 goto finally;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001422 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001423
Tim Peters1d63c9f2003-02-02 20:29:39 +00001424 /* A non-empty tuple. */
1425
1426 /* id(tuple) isn't in the memo now. If it shows up there after
1427 * saving the tuple elements, the tuple must be recursive, in
1428 * which case we'll pop everything we put on the stack, and fetch
1429 * its value from the memo.
1430 */
1431 py_tuple_id = PyLong_FromVoidPtr(args);
1432 if (py_tuple_id == NULL)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001433 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001434
Tim Peters1d63c9f2003-02-02 20:29:39 +00001435 if (len <= 3 && self->proto >= 2) {
1436 /* Use TUPLE{1,2,3} opcodes. */
Tim Peters67920142003-02-05 03:46:17 +00001437 if (store_tuple_elements(self, args, len) < 0)
Tim Peters1d63c9f2003-02-02 20:29:39 +00001438 goto finally;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001439 if (PyDict_GetItem(self->memo, py_tuple_id)) {
Tim Peters1d63c9f2003-02-02 20:29:39 +00001440 /* pop the len elements */
1441 for (i = 0; i < len; ++i)
1442 if (self->write_func(self, &pop, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001443 goto finally;
Tim Peters1d63c9f2003-02-02 20:29:39 +00001444 /* fetch from memo */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001445 if (get(self, py_tuple_id) < 0)
1446 goto finally;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001447 res = 0;
1448 goto finally;
1449 }
Tim Peters1d63c9f2003-02-02 20:29:39 +00001450 /* Not recursive. */
1451 if (self->write_func(self, len2opcode + len, 1) < 0)
1452 goto finally;
1453 goto memoize;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001454 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001455
Tim Peters1d63c9f2003-02-02 20:29:39 +00001456 /* proto < 2 and len > 0, or proto >= 2 and len > 3.
1457 * Generate MARK elt1 elt2 ... TUPLE
1458 */
1459 if (self->write_func(self, &MARKv, 1) < 0)
1460 goto finally;
1461
Tim Peters67920142003-02-05 03:46:17 +00001462 if (store_tuple_elements(self, args, len) < 0)
Tim Peters1d63c9f2003-02-02 20:29:39 +00001463 goto finally;
1464
1465 if (PyDict_GetItem(self->memo, py_tuple_id)) {
1466 /* pop the stack stuff we pushed */
1467 if (self->bin) {
1468 if (self->write_func(self, &pop_mark, 1) < 0)
1469 goto finally;
1470 }
1471 else {
1472 /* Note that we pop one more than len, to remove
1473 * the MARK too.
1474 */
1475 for (i = 0; i <= len; i++)
1476 if (self->write_func(self, &pop, 1) < 0)
1477 goto finally;
1478 }
1479 /* fetch from memo */
1480 if (get(self, py_tuple_id) >= 0)
1481 res = 0;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001482 goto finally;
1483 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001484
Tim Peters1d63c9f2003-02-02 20:29:39 +00001485 /* Not recursive. */
1486 if (self->write_func(self, &tuple, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001487 goto finally;
Tim Peters84e87f32001-03-17 04:50:51 +00001488
Tim Peters1d63c9f2003-02-02 20:29:39 +00001489 memoize:
1490 if (put(self, args) >= 0)
1491 res = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001492
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001493 finally:
1494 Py_XDECREF(py_tuple_id);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001495 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001496}
1497
Tim Peters1092d642003-02-11 21:06:20 +00001498/* iter is an iterator giving items, and we batch up chunks of
1499 * MARK item item ... item APPENDS
1500 * opcode sequences. Calling code should have arranged to first create an
1501 * empty list, or list-like object, for the APPENDS to operate on.
1502 * Returns 0 on success, <0 on error.
1503 */
1504static int
1505batch_list(Picklerobject *self, PyObject *iter)
1506{
1507 PyObject *obj;
1508 PyObject *slice[BATCHSIZE];
1509 int i, n;
1510
1511 static char append = APPEND;
1512 static char appends = APPENDS;
1513
1514 assert(iter != NULL);
1515
1516 if (self->proto == 0) {
1517 /* APPENDS isn't available; do one at a time. */
1518 for (;;) {
1519 obj = PyIter_Next(iter);
1520 if (obj == NULL) {
1521 if (PyErr_Occurred())
1522 return -1;
1523 break;
1524 }
1525 i = save(self, obj, 0);
1526 Py_DECREF(obj);
1527 if (i < 0)
1528 return -1;
1529 if (self->write_func(self, &append, 1) < 0)
1530 return -1;
Tim Peters1092d642003-02-11 21:06:20 +00001531 }
1532 return 0;
1533 }
1534
1535 /* proto > 0: write in batches of BATCHSIZE. */
1536 do {
1537 /* Get next group of (no more than) BATCHSIZE elements. */
1538 for (n = 0; n < BATCHSIZE; ++n) {
1539 obj = PyIter_Next(iter);
1540 if (obj == NULL) {
1541 if (PyErr_Occurred())
1542 goto BatchFailed;
1543 break;
1544 }
1545 slice[n] = obj;
1546 }
1547
1548 if (n > 1) {
1549 /* Pump out MARK, slice[0:n], APPENDS. */
1550 if (self->write_func(self, &MARKv, 1) < 0)
1551 goto BatchFailed;
1552 for (i = 0; i < n; ++i) {
1553 if (save(self, slice[i], 0) < 0)
1554 goto BatchFailed;
1555 }
1556 if (self->write_func(self, &appends, 1) < 0)
1557 goto BatchFailed;
1558 }
1559 else if (n == 1) {
1560 if (save(self, slice[0], 0) < 0)
1561 goto BatchFailed;
1562 if (self->write_func(self, &append, 1) < 0)
1563 goto BatchFailed;
1564 }
1565
1566 for (i = 0; i < n; ++i) {
1567 Py_DECREF(slice[i]);
1568 }
Tim Peters90975f12003-02-12 05:28:58 +00001569 } while (n == BATCHSIZE);
Tim Peters1092d642003-02-11 21:06:20 +00001570 return 0;
1571
1572BatchFailed:
1573 while (--n >= 0) {
1574 Py_DECREF(slice[n]);
1575 }
1576 return -1;
1577}
1578
Guido van Rossum60456fd1997-04-09 17:36:32 +00001579static int
Tim Peterscba30e22003-02-01 06:24:36 +00001580save_list(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001581{
Tim Peters1092d642003-02-11 21:06:20 +00001582 int res = -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001583 char s[3];
Tim Peters1092d642003-02-11 21:06:20 +00001584 int len;
1585 PyObject *iter;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001586
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001587 if (self->fast && !fast_save_enter(self, args))
1588 goto finally;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00001589
Tim Peters1092d642003-02-11 21:06:20 +00001590 /* Create an empty list. */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001591 if (self->bin) {
1592 s[0] = EMPTY_LIST;
Tim Peters1092d642003-02-11 21:06:20 +00001593 len = 1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001594 }
1595 else {
1596 s[0] = MARK;
1597 s[1] = LIST;
Tim Peters1092d642003-02-11 21:06:20 +00001598 len = 2;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001599 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001600
Tim Peters1092d642003-02-11 21:06:20 +00001601 if (self->write_func(self, s, len) < 0)
1602 goto finally;
1603
1604 /* Get list length, and bow out early if empty. */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001605 if ((len = PyList_Size(args)) < 0)
1606 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001607
Tim Peters1092d642003-02-11 21:06:20 +00001608 /* Memoize. */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001609 if (len == 0) {
Tim Peters90975f12003-02-12 05:28:58 +00001610 if (put(self, args) >= 0)
1611 res = 0;
1612 goto finally;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001613 }
Tim Peters90975f12003-02-12 05:28:58 +00001614 if (put2(self, args) < 0)
1615 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001616
Tim Peters1092d642003-02-11 21:06:20 +00001617 /* Materialize the list elements. */
1618 iter = PyObject_GetIter(args);
1619 if (iter == NULL)
1620 goto finally;
1621 res = batch_list(self, iter);
1622 Py_DECREF(iter);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001623
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001624 finally:
1625 if (self->fast && !fast_save_leave(self, args))
1626 res = -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001627
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001628 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001629}
1630
1631
Tim Peters42f08ac2003-02-11 22:43:24 +00001632/* iter is an iterator giving (key, value) pairs, and we batch up chunks of
1633 * MARK key value ... key value SETITEMS
1634 * opcode sequences. Calling code should have arranged to first create an
1635 * empty dict, or dict-like object, for the SETITEMS to operate on.
1636 * Returns 0 on success, <0 on error.
1637 *
1638 * This is very much like batch_list(). The difference between saving
1639 * elements directly, and picking apart two-tuples, is so long-winded at
1640 * the C level, though, that attempts to combine these routines were too
1641 * ugly to bear.
1642 */
1643static int
1644batch_dict(Picklerobject *self, PyObject *iter)
1645{
1646 PyObject *p;
1647 PyObject *slice[BATCHSIZE];
1648 int i, n;
1649
1650 static char setitem = SETITEM;
1651 static char setitems = SETITEMS;
1652
1653 assert(iter != NULL);
1654
1655 if (self->proto == 0) {
1656 /* SETITEMS isn't available; do one at a time. */
1657 for (;;) {
1658 p = PyIter_Next(iter);
1659 if (p == NULL) {
1660 if (PyErr_Occurred())
1661 return -1;
1662 break;
1663 }
1664 if (!PyTuple_Check(p) || PyTuple_Size(p) != 2) {
1665 PyErr_SetString(PyExc_TypeError, "dict items "
1666 "iterator must return 2-tuples");
1667 return -1;
1668 }
1669 i = save(self, PyTuple_GET_ITEM(p, 0), 0);
1670 if (i >= 0)
1671 i = save(self, PyTuple_GET_ITEM(p, 1), 0);
1672 Py_DECREF(p);
1673 if (i < 0)
1674 return -1;
1675 if (self->write_func(self, &setitem, 1) < 0)
1676 return -1;
Tim Peters42f08ac2003-02-11 22:43:24 +00001677 }
1678 return 0;
1679 }
1680
1681 /* proto > 0: write in batches of BATCHSIZE. */
1682 do {
1683 /* Get next group of (no more than) BATCHSIZE elements. */
1684 for (n = 0; n < BATCHSIZE; ++n) {
1685 p = PyIter_Next(iter);
1686 if (p == NULL) {
1687 if (PyErr_Occurred())
1688 goto BatchFailed;
1689 break;
1690 }
1691 if (!PyTuple_Check(p) || PyTuple_Size(p) != 2) {
1692 PyErr_SetString(PyExc_TypeError, "dict items "
1693 "iterator must return 2-tuples");
1694 goto BatchFailed;
1695 }
1696 slice[n] = p;
1697 }
1698
1699 if (n > 1) {
1700 /* Pump out MARK, slice[0:n], SETITEMS. */
1701 if (self->write_func(self, &MARKv, 1) < 0)
1702 goto BatchFailed;
1703 for (i = 0; i < n; ++i) {
1704 p = slice[i];
1705 if (save(self, PyTuple_GET_ITEM(p, 0), 0) < 0)
1706 goto BatchFailed;
1707 if (save(self, PyTuple_GET_ITEM(p, 1), 0) < 0)
1708 goto BatchFailed;
1709 }
1710 if (self->write_func(self, &setitems, 1) < 0)
1711 goto BatchFailed;
1712 }
1713 else if (n == 1) {
1714 p = slice[0];
1715 if (save(self, PyTuple_GET_ITEM(p, 0), 0) < 0)
1716 goto BatchFailed;
1717 if (save(self, PyTuple_GET_ITEM(p, 1), 0) < 0)
1718 goto BatchFailed;
1719 if (self->write_func(self, &setitem, 1) < 0)
1720 goto BatchFailed;
1721 }
1722
1723 for (i = 0; i < n; ++i) {
1724 Py_DECREF(slice[i]);
1725 }
Tim Peters90975f12003-02-12 05:28:58 +00001726 } while (n == BATCHSIZE);
Tim Peters42f08ac2003-02-11 22:43:24 +00001727 return 0;
1728
1729BatchFailed:
1730 while (--n >= 0) {
1731 Py_DECREF(slice[n]);
1732 }
1733 return -1;
1734}
1735
Guido van Rossum60456fd1997-04-09 17:36:32 +00001736static int
Tim Peterscba30e22003-02-01 06:24:36 +00001737save_dict(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001738{
Tim Peters42f08ac2003-02-11 22:43:24 +00001739 int res = -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001740 char s[3];
Tim Peters42f08ac2003-02-11 22:43:24 +00001741 int len;
1742 PyObject *iter;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001743
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001744 if (self->fast && !fast_save_enter(self, args))
1745 goto finally;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00001746
Tim Peters42f08ac2003-02-11 22:43:24 +00001747 /* Create an empty dict. */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001748 if (self->bin) {
1749 s[0] = EMPTY_DICT;
1750 len = 1;
1751 }
1752 else {
1753 s[0] = MARK;
1754 s[1] = DICT;
1755 len = 2;
1756 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001757
Tim Peters0bc93f52003-02-02 18:29:33 +00001758 if (self->write_func(self, s, len) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001759 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001760
Tim Peters42f08ac2003-02-11 22:43:24 +00001761 /* Get dict size, and bow out early if empty. */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001762 if ((len = PyDict_Size(args)) < 0)
1763 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001764
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001765 if (len == 0) {
Tim Peters90975f12003-02-12 05:28:58 +00001766 if (put(self, args) >= 0)
1767 res = 0;
1768 goto finally;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001769 }
Tim Peters90975f12003-02-12 05:28:58 +00001770 if (put2(self, args) < 0)
1771 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001772
Tim Peters42f08ac2003-02-11 22:43:24 +00001773 /* Materialize the dict items. */
1774 iter = PyObject_CallMethod(args, "iteritems", "()");
1775 if (iter == NULL)
1776 goto finally;
1777 res = batch_dict(self, iter);
1778 Py_DECREF(iter);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001779
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001780 finally:
1781 if (self->fast && !fast_save_leave(self, args))
1782 res = -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001783
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001784 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001785}
1786
1787
Tim Peters84e87f32001-03-17 04:50:51 +00001788static int
Tim Peterscba30e22003-02-01 06:24:36 +00001789save_inst(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001790{
1791 PyObject *class = 0, *module = 0, *name = 0, *state = 0,
1792 *getinitargs_func = 0, *getstate_func = 0, *class_args = 0;
1793 char *module_str, *name_str;
1794 int module_size, name_size, res = -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001795
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001796 static char inst = INST, obj = OBJ, build = BUILD;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001797
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001798 if (self->fast && !fast_save_enter(self, args))
1799 goto finally;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00001800
Tim Peters0bc93f52003-02-02 18:29:33 +00001801 if (self->write_func(self, &MARKv, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001802 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001803
Tim Peterscba30e22003-02-01 06:24:36 +00001804 if (!( class = PyObject_GetAttr(args, __class___str)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001805 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001806
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001807 if (self->bin) {
1808 if (save(self, class, 0) < 0)
1809 goto finally;
1810 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001811
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001812 if ((getinitargs_func = PyObject_GetAttr(args, __getinitargs___str))) {
1813 PyObject *element = 0;
1814 int i, len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001815
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001816 if (!( class_args =
Tim Peterscba30e22003-02-01 06:24:36 +00001817 PyObject_Call(getinitargs_func, empty_tuple, NULL)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001818 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001819
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001820 if ((len = PyObject_Size(class_args)) < 0)
1821 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001822
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001823 for (i = 0; i < len; i++) {
Tim Peterscba30e22003-02-01 06:24:36 +00001824 if (!( element = PySequence_GetItem(class_args, i)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001825 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001826
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001827 if (save(self, element, 0) < 0) {
1828 Py_DECREF(element);
1829 goto finally;
1830 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001831
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001832 Py_DECREF(element);
1833 }
1834 }
1835 else {
Jeremy Hyltonf00368f2003-06-16 20:19:49 +00001836 if (PyErr_ExceptionMatches(PyExc_AttributeError))
1837 PyErr_Clear();
1838 else
1839 goto finally;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001840 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001841
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001842 if (!self->bin) {
1843 if (!( name = ((PyClassObject *)class)->cl_name )) {
1844 PyErr_SetString(PicklingError, "class has no name");
1845 goto finally;
1846 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001847
Tim Peterscba30e22003-02-01 06:24:36 +00001848 if (!( module = whichmodule(class, name)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001849 goto finally;
Guido van Rossum053b8df1998-11-25 16:18:00 +00001850
Tim Peters84e87f32001-03-17 04:50:51 +00001851
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001852 if ((module_size = PyString_Size(module)) < 0 ||
1853 (name_size = PyString_Size(name)) < 0)
1854 goto finally;
Guido van Rossum053b8df1998-11-25 16:18:00 +00001855
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001856 module_str = PyString_AS_STRING((PyStringObject *)module);
1857 name_str = PyString_AS_STRING((PyStringObject *)name);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001858
Tim Peters0bc93f52003-02-02 18:29:33 +00001859 if (self->write_func(self, &inst, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001860 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001861
Tim Peters0bc93f52003-02-02 18:29:33 +00001862 if (self->write_func(self, module_str, module_size) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001863 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001864
Tim Peters0bc93f52003-02-02 18:29:33 +00001865 if (self->write_func(self, "\n", 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001866 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001867
Tim Peters0bc93f52003-02-02 18:29:33 +00001868 if (self->write_func(self, name_str, name_size) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001869 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001870
Tim Peters0bc93f52003-02-02 18:29:33 +00001871 if (self->write_func(self, "\n", 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001872 goto finally;
1873 }
Tim Peters0bc93f52003-02-02 18:29:33 +00001874 else if (self->write_func(self, &obj, 1) < 0) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001875 goto finally;
1876 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001877
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001878 if ((getstate_func = PyObject_GetAttr(args, __getstate___str))) {
1879 state = PyObject_Call(getstate_func, empty_tuple, NULL);
Tim Peterscba30e22003-02-01 06:24:36 +00001880 if (!state)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001881 goto finally;
1882 }
1883 else {
Jeremy Hyltonf00368f2003-06-16 20:19:49 +00001884 if (PyErr_ExceptionMatches(PyExc_AttributeError))
1885 PyErr_Clear();
1886 else
1887 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001888
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001889 if (!( state = PyObject_GetAttr(args, __dict___str))) {
Jeremy Hyltonf00368f2003-06-16 20:19:49 +00001890 if (PyErr_ExceptionMatches(PyExc_AttributeError))
1891 PyErr_Clear();
1892 else
1893 goto finally;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001894 res = 0;
1895 goto finally;
1896 }
1897 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001898
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001899 if (!PyDict_Check(state)) {
1900 if (put2(self, args) < 0)
1901 goto finally;
1902 }
1903 else {
1904 if (put(self, args) < 0)
1905 goto finally;
1906 }
Tim Peters84e87f32001-03-17 04:50:51 +00001907
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001908 if (save(self, state, 0) < 0)
1909 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001910
Tim Peters0bc93f52003-02-02 18:29:33 +00001911 if (self->write_func(self, &build, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001912 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001913
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001914 res = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001915
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001916 finally:
1917 if (self->fast && !fast_save_leave(self, args))
1918 res = -1;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00001919
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001920 Py_XDECREF(module);
1921 Py_XDECREF(class);
1922 Py_XDECREF(state);
1923 Py_XDECREF(getinitargs_func);
1924 Py_XDECREF(getstate_func);
1925 Py_XDECREF(class_args);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001926
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001927 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001928}
1929
1930
Guido van Rossum60456fd1997-04-09 17:36:32 +00001931static int
Tim Peterscba30e22003-02-01 06:24:36 +00001932save_global(Picklerobject *self, PyObject *args, PyObject *name)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001933{
Jeremy Hylton9ee91f12002-07-11 22:02:33 +00001934 PyObject *global_name = 0, *module = 0, *mod = 0, *klass = 0;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001935 char *name_str, *module_str;
1936 int module_size, name_size, res = -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001937
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001938 static char global = GLOBAL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001939
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001940 if (name) {
1941 global_name = name;
1942 Py_INCREF(global_name);
1943 }
1944 else {
Tim Peterscba30e22003-02-01 06:24:36 +00001945 if (!( global_name = PyObject_GetAttr(args, __name___str)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001946 goto finally;
1947 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001948
Tim Peterscba30e22003-02-01 06:24:36 +00001949 if (!( module = whichmodule(args, global_name)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001950 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001951
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001952 if ((module_size = PyString_Size(module)) < 0 ||
1953 (name_size = PyString_Size(global_name)) < 0)
1954 goto finally;
Guido van Rossum053b8df1998-11-25 16:18:00 +00001955
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001956 module_str = PyString_AS_STRING((PyStringObject *)module);
1957 name_str = PyString_AS_STRING((PyStringObject *)global_name);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001958
Guido van Rossum75bfd052002-12-24 18:10:07 +00001959 /* XXX This can be doing a relative import. Clearly it shouldn't,
1960 but I don't know how to stop it. :-( */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001961 mod = PyImport_ImportModule(module_str);
1962 if (mod == NULL) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001963 cPickle_ErrFormat(PicklingError,
Tim Petersb9ce7cd2003-02-18 20:50:45 +00001964 "Can't pickle %s: import of module %s "
1965 "failed",
1966 "OS", args, module);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001967 goto finally;
1968 }
Jeremy Hylton0e1f7a82002-07-11 22:01:40 +00001969 klass = PyObject_GetAttrString(mod, name_str);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001970 if (klass == NULL) {
1971 cPickle_ErrFormat(PicklingError,
Tim Petersb9ce7cd2003-02-18 20:50:45 +00001972 "Can't pickle %s: attribute lookup %s.%s "
1973 "failed",
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001974 "OSS", args, module, global_name);
1975 goto finally;
1976 }
1977 if (klass != args) {
Jeremy Hylton0e1f7a82002-07-11 22:01:40 +00001978 Py_DECREF(klass);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001979 cPickle_ErrFormat(PicklingError,
Tim Peters731098b2003-02-04 20:56:09 +00001980 "Can't pickle %s: it's not the same object "
1981 "as %s.%s",
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001982 "OSS", args, module, global_name);
1983 goto finally;
1984 }
Jeremy Hylton0e1f7a82002-07-11 22:01:40 +00001985 Py_DECREF(klass);
Guido van Rossuma92d16a2001-08-18 21:22:07 +00001986
Tim Peters731098b2003-02-04 20:56:09 +00001987 if (self->proto >= 2) {
1988 /* See whether this is in the extension registry, and if
1989 * so generate an EXT opcode.
1990 */
1991 PyObject *py_code; /* extension code as Python object */
Tim Peters3e667d52003-02-04 21:47:44 +00001992 long code; /* extension code as C value */
Tim Peters731098b2003-02-04 20:56:09 +00001993 char c_str[5];
1994 int n;
1995
1996 PyTuple_SET_ITEM(two_tuple, 0, module);
1997 PyTuple_SET_ITEM(two_tuple, 1, global_name);
1998 py_code = PyDict_GetItem(extension_registry, two_tuple);
1999 if (py_code == NULL)
2000 goto gen_global; /* not registered */
2001
2002 /* Verify py_code has the right type and value. */
2003 if (!PyInt_Check(py_code)) {
2004 cPickle_ErrFormat(PicklingError, "Can't pickle %s: "
Tim Peters6288e232003-02-05 03:53:10 +00002005 "extension code %s isn't an integer",
Tim Peters731098b2003-02-04 20:56:09 +00002006 "OO", args, py_code);
2007 goto finally;
2008 }
2009 code = PyInt_AS_LONG(py_code);
2010 if (code <= 0 || code > 0x7fffffffL) {
2011 cPickle_ErrFormat(PicklingError, "Can't pickle %s: "
2012 "extension code %ld is out of range",
2013 "Ol", args, code);
2014 goto finally;
2015 }
2016
2017 /* Generate an EXT opcode. */
2018 if (code <= 0xff) {
2019 c_str[0] = EXT1;
2020 c_str[1] = (char)code;
2021 n = 2;
2022 }
2023 else if (code <= 0xffff) {
2024 c_str[0] = EXT2;
2025 c_str[1] = (char)(code & 0xff);
2026 c_str[2] = (char)((code >> 8) & 0xff);
2027 n = 3;
2028 }
2029 else {
2030 c_str[0] = EXT4;
2031 c_str[1] = (char)(code & 0xff);
2032 c_str[2] = (char)((code >> 8) & 0xff);
2033 c_str[3] = (char)((code >> 16) & 0xff);
2034 c_str[4] = (char)((code >> 24) & 0xff);
2035 n = 5;
2036 }
2037
2038 if (self->write_func(self, c_str, n) >= 0)
2039 res = 0;
2040 goto finally; /* and don't memoize */
2041 }
2042
2043 gen_global:
Tim Peters0bc93f52003-02-02 18:29:33 +00002044 if (self->write_func(self, &global, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002045 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002046
Tim Peters0bc93f52003-02-02 18:29:33 +00002047 if (self->write_func(self, module_str, module_size) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002048 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002049
Tim Peters0bc93f52003-02-02 18:29:33 +00002050 if (self->write_func(self, "\n", 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002051 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002052
Tim Peters0bc93f52003-02-02 18:29:33 +00002053 if (self->write_func(self, name_str, name_size) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002054 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002055
Tim Peters0bc93f52003-02-02 18:29:33 +00002056 if (self->write_func(self, "\n", 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002057 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002058
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002059 if (put(self, args) < 0)
2060 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002061
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002062 res = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002063
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002064 finally:
2065 Py_XDECREF(module);
2066 Py_XDECREF(global_name);
2067 Py_XDECREF(mod);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002068
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002069 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002070}
2071
Guido van Rossum60456fd1997-04-09 17:36:32 +00002072static int
Tim Peterscba30e22003-02-01 06:24:36 +00002073save_pers(Picklerobject *self, PyObject *args, PyObject *f)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002074{
2075 PyObject *pid = 0;
2076 int size, res = -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002077
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002078 static char persid = PERSID, binpersid = BINPERSID;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002079
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002080 Py_INCREF(args);
2081 ARG_TUP(self, args);
2082 if (self->arg) {
2083 pid = PyObject_Call(f, self->arg, NULL);
2084 FREE_ARG_TUP(self);
2085 }
2086 if (! pid) return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002087
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002088 if (pid != Py_None) {
2089 if (!self->bin) {
2090 if (!PyString_Check(pid)) {
2091 PyErr_SetString(PicklingError,
2092 "persistent id must be string");
2093 goto finally;
2094 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002095
Tim Peters0bc93f52003-02-02 18:29:33 +00002096 if (self->write_func(self, &persid, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002097 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002098
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002099 if ((size = PyString_Size(pid)) < 0)
2100 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002101
Tim Peters0bc93f52003-02-02 18:29:33 +00002102 if (self->write_func(self,
2103 PyString_AS_STRING(
2104 (PyStringObject *)pid),
2105 size) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002106 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002107
Tim Peters0bc93f52003-02-02 18:29:33 +00002108 if (self->write_func(self, "\n", 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002109 goto finally;
Tim Peters84e87f32001-03-17 04:50:51 +00002110
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002111 res = 1;
2112 goto finally;
2113 }
2114 else if (save(self, pid, 1) >= 0) {
Tim Peters0bc93f52003-02-02 18:29:33 +00002115 if (self->write_func(self, &binpersid, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002116 res = -1;
2117 else
2118 res = 1;
2119 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002120
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002121 goto finally;
2122 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002123
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002124 res = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002125
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002126 finally:
2127 Py_XDECREF(pid);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002128
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002129 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002130}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002131
Tim Peters71fcda52003-02-14 23:05:28 +00002132/* We're saving ob, and args is the 2-thru-5 tuple returned by the
2133 * appropriate __reduce__ method for ob.
2134 */
Tim Peters84e87f32001-03-17 04:50:51 +00002135static int
Tim Peters71fcda52003-02-14 23:05:28 +00002136save_reduce(Picklerobject *self, PyObject *args, PyObject *ob)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002137{
Tim Peters71fcda52003-02-14 23:05:28 +00002138 PyObject *callable;
2139 PyObject *argtup;
2140 PyObject *state = NULL;
2141 PyObject *listitems = NULL;
2142 PyObject *dictitems = NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002143
Tim Peters71fcda52003-02-14 23:05:28 +00002144 int use_newobj = self->proto >= 2;
2145
2146 static char reduce = REDUCE;
2147 static char build = BUILD;
2148 static char newobj = NEWOBJ;
2149
2150 if (! PyArg_UnpackTuple(args, "save_reduce", 2, 5,
2151 &callable,
2152 &argtup,
2153 &state,
2154 &listitems,
2155 &dictitems))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002156 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002157
Raymond Hettingera6b45cc2004-12-07 07:05:57 +00002158 if (!PyTuple_Check(argtup)) {
2159 PyErr_SetString(PicklingError,
2160 "args from reduce() should be a tuple");
2161 return -1;
2162 }
2163
Tim Peters71fcda52003-02-14 23:05:28 +00002164 if (state == Py_None)
2165 state = NULL;
2166 if (listitems == Py_None)
2167 listitems = NULL;
2168 if (dictitems == Py_None)
2169 dictitems = NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002170
Tim Peters71fcda52003-02-14 23:05:28 +00002171 /* Protocol 2 special case: if callable's name is __newobj__, use
2172 * NEWOBJ. This consumes a lot of code.
2173 */
2174 if (use_newobj) {
2175 PyObject *temp = PyObject_GetAttr(callable, __name___str);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002176
Tim Peters71fcda52003-02-14 23:05:28 +00002177 if (temp == NULL) {
Jeremy Hyltonf00368f2003-06-16 20:19:49 +00002178 if (PyErr_ExceptionMatches(PyExc_AttributeError))
2179 PyErr_Clear();
2180 else
2181 return -1;
Tim Peters71fcda52003-02-14 23:05:28 +00002182 use_newobj = 0;
2183 }
2184 else {
2185 use_newobj = PyString_Check(temp) &&
2186 strcmp(PyString_AS_STRING(temp),
2187 "__newobj__") == 0;
2188 Py_DECREF(temp);
2189 }
2190 }
2191 if (use_newobj) {
2192 PyObject *cls;
2193 PyObject *newargtup;
2194 int n, i;
2195
2196 /* Sanity checks. */
2197 n = PyTuple_Size(argtup);
2198 if (n < 1) {
2199 PyErr_SetString(PicklingError, "__newobj__ arglist "
2200 "is empty");
2201 return -1;
2202 }
2203
2204 cls = PyTuple_GET_ITEM(argtup, 0);
2205 if (! PyObject_HasAttrString(cls, "__new__")) {
2206 PyErr_SetString(PicklingError, "args[0] from "
2207 "__newobj__ args has no __new__");
2208 return -1;
2209 }
2210
2211 /* XXX How could ob be NULL? */
2212 if (ob != NULL) {
2213 PyObject *ob_dot_class;
2214
2215 ob_dot_class = PyObject_GetAttr(ob, __class___str);
Jeremy Hyltonf00368f2003-06-16 20:19:49 +00002216 if (ob_dot_class == NULL) {
2217 if (PyErr_ExceptionMatches(
2218 PyExc_AttributeError))
2219 PyErr_Clear();
2220 else
2221 return -1;
2222 }
Tim Peters71fcda52003-02-14 23:05:28 +00002223 i = ob_dot_class != cls; /* true iff a problem */
2224 Py_XDECREF(ob_dot_class);
2225 if (i) {
2226 PyErr_SetString(PicklingError, "args[0] from "
2227 "__newobj__ args has the wrong class");
2228 return -1;
2229 }
2230 }
2231
2232 /* Save the class and its __new__ arguments. */
2233 if (save(self, cls, 0) < 0)
2234 return -1;
2235
2236 newargtup = PyTuple_New(n-1); /* argtup[1:] */
2237 if (newargtup == NULL)
2238 return -1;
2239 for (i = 1; i < n; ++i) {
2240 PyObject *temp = PyTuple_GET_ITEM(argtup, i);
2241 Py_INCREF(temp);
2242 PyTuple_SET_ITEM(newargtup, i-1, temp);
2243 }
2244 i = save(self, newargtup, 0) < 0;
2245 Py_DECREF(newargtup);
2246 if (i < 0)
2247 return -1;
2248
2249 /* Add NEWOBJ opcode. */
2250 if (self->write_func(self, &newobj, 1) < 0)
2251 return -1;
2252 }
2253 else {
2254 /* Not using NEWOBJ. */
2255 if (save(self, callable, 0) < 0 ||
2256 save(self, argtup, 0) < 0 ||
2257 self->write_func(self, &reduce, 1) < 0)
2258 return -1;
2259 }
2260
2261 /* Memoize. */
2262 /* XXX How can ob be NULL? */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002263 if (ob != NULL) {
2264 if (state && !PyDict_Check(state)) {
2265 if (put2(self, ob) < 0)
2266 return -1;
2267 }
Tim Peters71fcda52003-02-14 23:05:28 +00002268 else if (put(self, ob) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002269 return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002270 }
Tim Peters84e87f32001-03-17 04:50:51 +00002271
Guido van Rossum60456fd1997-04-09 17:36:32 +00002272
Tim Peters71fcda52003-02-14 23:05:28 +00002273 if (listitems && batch_list(self, listitems) < 0)
2274 return -1;
2275
2276 if (dictitems && batch_dict(self, dictitems) < 0)
2277 return -1;
2278
2279 if (state) {
2280 if (save(self, state, 0) < 0 ||
2281 self->write_func(self, &build, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002282 return -1;
2283 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002284
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002285 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002286}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002287
Guido van Rossum60456fd1997-04-09 17:36:32 +00002288static int
Tim Peters1d63c9f2003-02-02 20:29:39 +00002289save(Picklerobject *self, PyObject *args, int pers_save)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002290{
2291 PyTypeObject *type;
Tim Peters71fcda52003-02-14 23:05:28 +00002292 PyObject *py_ob_id = 0, *__reduce__ = 0, *t = 0;
2293 PyObject *arg_tup;
2294 int res = -1;
2295 int tmp, size;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002296
Martin v. Löwis5a395302002-08-04 08:20:23 +00002297 if (self->nesting++ > Py_GetRecursionLimit()){
2298 PyErr_SetString(PyExc_RuntimeError,
2299 "maximum recursion depth exceeded");
2300 goto finally;
2301 }
2302
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002303 if (!pers_save && self->pers_func) {
2304 if ((tmp = save_pers(self, args, self->pers_func)) != 0) {
2305 res = tmp;
2306 goto finally;
2307 }
2308 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002309
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002310 if (args == Py_None) {
2311 res = save_none(self, args);
2312 goto finally;
2313 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002314
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002315 type = args->ob_type;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002316
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002317 switch (type->tp_name[0]) {
Guido van Rossum77f6a652002-04-03 22:41:51 +00002318 case 'b':
2319 if (args == Py_False || args == Py_True) {
2320 res = save_bool(self, args);
2321 goto finally;
2322 }
2323 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002324 case 'i':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002325 if (type == &PyInt_Type) {
2326 res = save_int(self, args);
2327 goto finally;
2328 }
2329 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002330
Guido van Rossum60456fd1997-04-09 17:36:32 +00002331 case 'l':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002332 if (type == &PyLong_Type) {
2333 res = save_long(self, args);
2334 goto finally;
2335 }
2336 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002337
Guido van Rossum60456fd1997-04-09 17:36:32 +00002338 case 'f':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002339 if (type == &PyFloat_Type) {
2340 res = save_float(self, args);
2341 goto finally;
2342 }
2343 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002344
Guido van Rossum60456fd1997-04-09 17:36:32 +00002345 case 't':
Tim Peters1d63c9f2003-02-02 20:29:39 +00002346 if (type == &PyTuple_Type && PyTuple_Size(args) == 0) {
2347 res = save_tuple(self, args);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002348 goto finally;
2349 }
2350 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002351
Guido van Rossum60456fd1997-04-09 17:36:32 +00002352 case 's':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002353 if ((type == &PyString_Type) && (PyString_GET_SIZE(args) < 2)) {
2354 res = save_string(self, args, 0);
2355 goto finally;
2356 }
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00002357
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002358#ifdef Py_USING_UNICODE
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00002359 case 'u':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002360 if ((type == &PyUnicode_Type) && (PyString_GET_SIZE(args) < 2)) {
2361 res = save_unicode(self, args, 0);
2362 goto finally;
2363 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002364#endif
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002365 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002366
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002367 if (args->ob_refcnt > 1) {
Tim Peterscba30e22003-02-01 06:24:36 +00002368 if (!( py_ob_id = PyLong_FromVoidPtr(args)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002369 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002370
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002371 if (PyDict_GetItem(self->memo, py_ob_id)) {
2372 if (get(self, py_ob_id) < 0)
2373 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002374
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002375 res = 0;
2376 goto finally;
2377 }
2378 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002379
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002380 switch (type->tp_name[0]) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00002381 case 's':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002382 if (type == &PyString_Type) {
2383 res = save_string(self, args, 1);
2384 goto finally;
2385 }
2386 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002387
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002388#ifdef Py_USING_UNICODE
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00002389 case 'u':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002390 if (type == &PyUnicode_Type) {
2391 res = save_unicode(self, args, 1);
2392 goto finally;
2393 }
2394 break;
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002395#endif
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00002396
Guido van Rossum60456fd1997-04-09 17:36:32 +00002397 case 't':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002398 if (type == &PyTuple_Type) {
2399 res = save_tuple(self, args);
2400 goto finally;
2401 }
2402 if (type == &PyType_Type) {
2403 res = save_global(self, args, NULL);
2404 goto finally;
2405 }
2406 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002407
Guido van Rossum60456fd1997-04-09 17:36:32 +00002408 case 'l':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002409 if (type == &PyList_Type) {
2410 res = save_list(self, args);
2411 goto finally;
2412 }
2413 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002414
2415 case 'd':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002416 if (type == &PyDict_Type) {
2417 res = save_dict(self, args);
2418 goto finally;
2419 }
2420 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002421
2422 case 'i':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002423 if (type == &PyInstance_Type) {
2424 res = save_inst(self, args);
2425 goto finally;
2426 }
2427 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002428
2429 case 'c':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002430 if (type == &PyClass_Type) {
2431 res = save_global(self, args, NULL);
2432 goto finally;
2433 }
2434 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002435
2436 case 'f':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002437 if (type == &PyFunction_Type) {
2438 res = save_global(self, args, NULL);
Christian Tismer2460c622004-02-26 16:21:45 +00002439 if (res && PyErr_ExceptionMatches(PickleError)) {
2440 /* fall back to reduce */
2441 PyErr_Clear();
2442 break;
2443 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002444 goto finally;
2445 }
2446 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002447
2448 case 'b':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002449 if (type == &PyCFunction_Type) {
2450 res = save_global(self, args, NULL);
2451 goto finally;
2452 }
2453 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002454
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002455 if (!pers_save && self->inst_pers_func) {
2456 if ((tmp = save_pers(self, args, self->inst_pers_func)) != 0) {
2457 res = tmp;
2458 goto finally;
2459 }
2460 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002461
Jeremy Hylton39c61162002-07-16 19:47:43 +00002462 if (PyType_IsSubtype(type, &PyType_Type)) {
2463 res = save_global(self, args, NULL);
2464 goto finally;
2465 }
2466
Guido van Rossumb289b872003-02-19 01:45:13 +00002467 /* Get a reduction callable, and call it. This may come from
2468 * copy_reg.dispatch_table, the object's __reduce_ex__ method,
2469 * or the object's __reduce__ method.
Tim Peters71fcda52003-02-14 23:05:28 +00002470 */
Tim Peters5aa3da62003-02-13 21:03:57 +00002471 __reduce__ = PyDict_GetItem(dispatch_table, (PyObject *)type);
2472 if (__reduce__ != NULL) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002473 Py_INCREF(__reduce__);
Guido van Rossumb289b872003-02-19 01:45:13 +00002474 Py_INCREF(args);
2475 ARG_TUP(self, args);
2476 if (self->arg) {
2477 t = PyObject_Call(__reduce__, self->arg, NULL);
2478 FREE_ARG_TUP(self);
2479 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002480 }
2481 else {
Guido van Rossumb289b872003-02-19 01:45:13 +00002482 /* Check for a __reduce_ex__ method. */
2483 __reduce__ = PyObject_GetAttr(args, __reduce_ex___str);
2484 if (__reduce__ != NULL) {
2485 t = PyInt_FromLong(self->proto);
2486 if (t != NULL) {
2487 ARG_TUP(self, t);
2488 t = NULL;
2489 if (self->arg) {
2490 t = PyObject_Call(__reduce__,
2491 self->arg, NULL);
2492 FREE_ARG_TUP(self);
2493 }
2494 }
2495 }
2496 else {
Jeremy Hyltonf00368f2003-06-16 20:19:49 +00002497 if (PyErr_ExceptionMatches(PyExc_AttributeError))
2498 PyErr_Clear();
2499 else
2500 goto finally;
Guido van Rossumb289b872003-02-19 01:45:13 +00002501 /* Check for a __reduce__ method. */
2502 __reduce__ = PyObject_GetAttr(args, __reduce___str);
2503 if (__reduce__ != NULL) {
2504 t = PyObject_Call(__reduce__,
2505 empty_tuple, NULL);
2506 }
2507 else {
2508 PyErr_SetObject(UnpickleableError, args);
2509 goto finally;
2510 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002511 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002512 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002513
Tim Peters71fcda52003-02-14 23:05:28 +00002514 if (t == NULL)
2515 goto finally;
Tim Peters84e87f32001-03-17 04:50:51 +00002516
Tim Peters71fcda52003-02-14 23:05:28 +00002517 if (PyString_Check(t)) {
2518 res = save_global(self, args, t);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002519 goto finally;
2520 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002521
Tim Peters71fcda52003-02-14 23:05:28 +00002522 if (! PyTuple_Check(t)) {
2523 cPickle_ErrFormat(PicklingError, "Value returned by "
2524 "%s must be string or tuple",
2525 "O", __reduce__);
2526 goto finally;
2527 }
2528
2529 size = PyTuple_Size(t);
2530 if (size < 2 || size > 5) {
2531 cPickle_ErrFormat(PicklingError, "tuple returned by "
2532 "%s must contain 2 through 5 elements",
2533 "O", __reduce__);
2534 goto finally;
2535 }
2536
2537 arg_tup = PyTuple_GET_ITEM(t, 1);
2538 if (!(PyTuple_Check(arg_tup) || arg_tup == Py_None)) {
2539 cPickle_ErrFormat(PicklingError, "Second element of "
2540 "tuple returned by %s must be a tuple",
2541 "O", __reduce__);
2542 goto finally;
2543 }
2544
2545 res = save_reduce(self, t, args);
Guido van Rossum60456fd1997-04-09 17:36:32 +00002546
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002547 finally:
Martin v. Löwis5a395302002-08-04 08:20:23 +00002548 self->nesting--;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002549 Py_XDECREF(py_ob_id);
2550 Py_XDECREF(__reduce__);
2551 Py_XDECREF(t);
Tim Peters84e87f32001-03-17 04:50:51 +00002552
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002553 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002554}
2555
2556
2557static int
Tim Peterscba30e22003-02-01 06:24:36 +00002558dump(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002559{
2560 static char stop = STOP;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002561
Tim Peters4190fb82003-02-02 16:09:05 +00002562 if (self->proto >= 2) {
2563 char bytes[2];
2564
2565 bytes[0] = PROTO;
Tim Peters87482ea2003-02-02 16:16:30 +00002566 assert(self->proto >= 0 && self->proto < 256);
2567 bytes[1] = (char)self->proto;
Tim Peters4190fb82003-02-02 16:09:05 +00002568 if (self->write_func(self, bytes, 2) < 0)
2569 return -1;
2570 }
2571
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002572 if (save(self, args, 0) < 0)
2573 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002574
Tim Peters4190fb82003-02-02 16:09:05 +00002575 if (self->write_func(self, &stop, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002576 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002577
Tim Peters4190fb82003-02-02 16:09:05 +00002578 if (self->write_func(self, NULL, 0) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002579 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002580
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002581 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002582}
2583
2584static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00002585Pickle_clear_memo(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002586{
Tim Peterscba30e22003-02-01 06:24:36 +00002587 if (self->memo)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002588 PyDict_Clear(self->memo);
2589 Py_INCREF(Py_None);
2590 return Py_None;
2591}
2592
2593static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00002594Pickle_getvalue(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002595{
2596 int l, i, rsize, ssize, clear=1, lm;
2597 long ik;
2598 PyObject *k, *r;
2599 char *s, *p, *have_get;
2600 Pdata *data;
2601
2602 /* Can be called by Python code or C code */
Tim Peterscba30e22003-02-01 06:24:36 +00002603 if (args && !PyArg_ParseTuple(args, "|i:getvalue", &clear))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002604 return NULL;
2605
2606 /* Check to make sure we are based on a list */
2607 if (! Pdata_Check(self->file)) {
2608 PyErr_SetString(PicklingError,
2609 "Attempt to getvalue() a non-list-based pickler");
2610 return NULL;
2611 }
2612
2613 /* flush write buffer */
2614 if (write_other(self, NULL, 0) < 0) return NULL;
2615
2616 data=(Pdata*)self->file;
2617 l=data->length;
2618
2619 /* set up an array to hold get/put status */
Tim Petersac5687a2003-02-02 18:08:34 +00002620 lm = PyDict_Size(self->memo);
2621 if (lm < 0) return NULL;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002622 lm++;
Tim Petersac5687a2003-02-02 18:08:34 +00002623 have_get = malloc(lm);
2624 if (have_get == NULL) return PyErr_NoMemory();
2625 memset(have_get, 0, lm);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002626
2627 /* Scan for gets. */
Tim Petersac5687a2003-02-02 18:08:34 +00002628 for (rsize = 0, i = l; --i >= 0; ) {
2629 k = data->data[i];
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002630
Tim Petersac5687a2003-02-02 18:08:34 +00002631 if (PyString_Check(k))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002632 rsize += PyString_GET_SIZE(k);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002633
2634 else if (PyInt_Check(k)) { /* put */
Tim Petersac5687a2003-02-02 18:08:34 +00002635 ik = PyInt_AS_LONG((PyIntObject*)k);
2636 if (ik >= lm || ik == 0) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002637 PyErr_SetString(PicklingError,
2638 "Invalid get data");
2639 return NULL;
2640 }
Tim Petersac5687a2003-02-02 18:08:34 +00002641 if (have_get[ik]) /* with matching get */
2642 rsize += ik < 256 ? 2 : 5;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002643 }
2644
2645 else if (! (PyTuple_Check(k) &&
2646 PyTuple_GET_SIZE(k) == 2 &&
Tim Petersac5687a2003-02-02 18:08:34 +00002647 PyInt_Check((k = PyTuple_GET_ITEM(k, 0))))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002648 ) {
2649 PyErr_SetString(PicklingError,
2650 "Unexpected data in internal list");
2651 return NULL;
2652 }
2653
2654 else { /* put */
Tim Petersac5687a2003-02-02 18:08:34 +00002655 ik = PyInt_AS_LONG((PyIntObject *)k);
2656 if (ik >= lm || ik == 0) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002657 PyErr_SetString(PicklingError,
2658 "Invalid get data");
2659 return NULL;
2660 }
Tim Petersac5687a2003-02-02 18:08:34 +00002661 have_get[ik] = 1;
2662 rsize += ik < 256 ? 2 : 5;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002663 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002664 }
2665
2666 /* Now generate the result */
Tim Petersac5687a2003-02-02 18:08:34 +00002667 r = PyString_FromStringAndSize(NULL, rsize);
2668 if (r == NULL) goto err;
2669 s = PyString_AS_STRING((PyStringObject *)r);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002670
Tim Petersac5687a2003-02-02 18:08:34 +00002671 for (i = 0; i < l; i++) {
2672 k = data->data[i];
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002673
2674 if (PyString_Check(k)) {
Tim Petersac5687a2003-02-02 18:08:34 +00002675 ssize = PyString_GET_SIZE(k);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002676 if (ssize) {
Tim Petersac5687a2003-02-02 18:08:34 +00002677 p=PyString_AS_STRING((PyStringObject *)k);
2678 while (--ssize >= 0)
2679 *s++ = *p++;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002680 }
2681 }
2682
2683 else if (PyTuple_Check(k)) { /* get */
Tim Petersac5687a2003-02-02 18:08:34 +00002684 ik = PyInt_AS_LONG((PyIntObject *)
2685 PyTuple_GET_ITEM(k, 0));
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002686 if (ik < 256) {
2687 *s++ = BINGET;
2688 *s++ = (int)(ik & 0xff);
2689 }
2690 else {
2691 *s++ = LONG_BINGET;
2692 *s++ = (int)(ik & 0xff);
2693 *s++ = (int)((ik >> 8) & 0xff);
2694 *s++ = (int)((ik >> 16) & 0xff);
2695 *s++ = (int)((ik >> 24) & 0xff);
2696 }
2697 }
2698
2699 else { /* put */
Tim Petersac5687a2003-02-02 18:08:34 +00002700 ik = PyInt_AS_LONG((PyIntObject*)k);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002701
2702 if (have_get[ik]) { /* with matching get */
2703 if (ik < 256) {
2704 *s++ = BINPUT;
2705 *s++ = (int)(ik & 0xff);
2706 }
2707 else {
2708 *s++ = LONG_BINPUT;
2709 *s++ = (int)(ik & 0xff);
2710 *s++ = (int)((ik >> 8) & 0xff);
2711 *s++ = (int)((ik >> 16) & 0xff);
2712 *s++ = (int)((ik >> 24) & 0xff);
2713 }
2714 }
2715 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002716 }
2717
2718 if (clear) {
2719 PyDict_Clear(self->memo);
Tim Petersac5687a2003-02-02 18:08:34 +00002720 Pdata_clear(data, 0);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002721 }
2722
2723 free(have_get);
2724 return r;
2725 err:
2726 free(have_get);
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00002727 return NULL;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002728}
2729
2730static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00002731Pickler_dump(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002732{
2733 PyObject *ob;
2734 int get=0;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002735
Tim Peterscba30e22003-02-01 06:24:36 +00002736 if (!( PyArg_ParseTuple(args, "O|i:dump", &ob, &get)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002737 return NULL;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002738
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002739 if (dump(self, ob) < 0)
2740 return NULL;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002741
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002742 if (get) return Pickle_getvalue(self, NULL);
Guido van Rossum053b8df1998-11-25 16:18:00 +00002743
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002744 /* XXX Why does dump() return self? */
2745 Py_INCREF(self);
2746 return (PyObject*)self;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002747}
2748
2749
Tim Peterscba30e22003-02-01 06:24:36 +00002750static struct PyMethodDef Pickler_methods[] =
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002751{
Neal Norwitzb0493252002-03-31 14:44:22 +00002752 {"dump", (PyCFunction)Pickler_dump, METH_VARARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00002753 PyDoc_STR("dump(object) -- "
2754 "Write an object in pickle format to the object's pickle stream")},
Fred Drake0ebacc82002-05-01 20:36:39 +00002755 {"clear_memo", (PyCFunction)Pickle_clear_memo, METH_NOARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00002756 PyDoc_STR("clear_memo() -- Clear the picklers memo")},
Neal Norwitzb0493252002-03-31 14:44:22 +00002757 {"getvalue", (PyCFunction)Pickle_getvalue, METH_VARARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00002758 PyDoc_STR("getvalue() -- Finish picking a list-based pickle")},
Guido van Rossum60456fd1997-04-09 17:36:32 +00002759 {NULL, NULL} /* sentinel */
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002760};
2761
2762
2763static Picklerobject *
Tim Peters5bd2a792003-02-01 16:45:06 +00002764newPicklerobject(PyObject *file, int proto)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002765{
2766 Picklerobject *self;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002767
Tim Peters5bd2a792003-02-01 16:45:06 +00002768 if (proto < 0)
Tim Peters8587b3c2003-02-13 15:44:41 +00002769 proto = HIGHEST_PROTOCOL;
2770 if (proto > HIGHEST_PROTOCOL) {
Tim Peters5bd2a792003-02-01 16:45:06 +00002771 PyErr_Format(PyExc_ValueError, "pickle protocol %d asked for; "
2772 "the highest available protocol is %d",
Tim Peters8587b3c2003-02-13 15:44:41 +00002773 proto, HIGHEST_PROTOCOL);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002774 return NULL;
Tim Peters5bd2a792003-02-01 16:45:06 +00002775 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002776
Jeremy Hylton4cf63192003-04-09 21:05:12 +00002777 self = PyObject_GC_New(Picklerobject, &Picklertype);
Tim Peters5bd2a792003-02-01 16:45:06 +00002778 if (self == NULL)
2779 return NULL;
2780 self->proto = proto;
2781 self->bin = proto > 0;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002782 self->fp = NULL;
2783 self->write = NULL;
2784 self->memo = NULL;
2785 self->arg = NULL;
2786 self->pers_func = NULL;
2787 self->inst_pers_func = NULL;
2788 self->write_buf = NULL;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002789 self->fast = 0;
Martin v. Löwis5a395302002-08-04 08:20:23 +00002790 self->nesting = 0;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002791 self->fast_container = 0;
2792 self->fast_memo = NULL;
2793 self->buf_size = 0;
2794 self->dispatch_table = NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002795
Tim Peters5bd2a792003-02-01 16:45:06 +00002796 self->file = NULL;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002797 if (file)
2798 Py_INCREF(file);
Tim Peters5bd2a792003-02-01 16:45:06 +00002799 else {
2800 file = Pdata_New();
2801 if (file == NULL)
2802 goto err;
2803 }
2804 self->file = file;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002805
Tim Peterscba30e22003-02-01 06:24:36 +00002806 if (!( self->memo = PyDict_New()))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002807 goto err;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002808
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002809 if (PyFile_Check(file)) {
2810 self->fp = PyFile_AsFile(file);
2811 if (self->fp == NULL) {
Tim Peters5bd2a792003-02-01 16:45:06 +00002812 PyErr_SetString(PyExc_ValueError,
2813 "I/O operation on closed file");
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002814 goto err;
2815 }
2816 self->write_func = write_file;
Guido van Rossumc91fcaa1999-03-29 20:00:14 +00002817 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002818 else if (PycStringIO_OutputCheck(file)) {
2819 self->write_func = write_cStringIO;
2820 }
2821 else if (file == Py_None) {
2822 self->write_func = write_none;
2823 }
2824 else {
2825 self->write_func = write_other;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002826
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002827 if (! Pdata_Check(file)) {
2828 self->write = PyObject_GetAttr(file, write_str);
2829 if (!self->write) {
2830 PyErr_Clear();
Tim Peterscba30e22003-02-01 06:24:36 +00002831 PyErr_SetString(PyExc_TypeError,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002832 "argument must have 'write' "
2833 "attribute");
2834 goto err;
2835 }
2836 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002837
Tim Peters5bd2a792003-02-01 16:45:06 +00002838 self->write_buf = (char *)PyMem_Malloc(WRITE_BUF_SIZE);
2839 if (self->write_buf == NULL) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002840 PyErr_NoMemory();
2841 goto err;
2842 }
2843 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002844
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002845 if (PyEval_GetRestricted()) {
2846 /* Restricted execution, get private tables */
Tim Peters5b7da392003-02-04 00:21:07 +00002847 PyObject *m = PyImport_Import(copy_reg_str);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002848
Tim Peters5b7da392003-02-04 00:21:07 +00002849 if (m == NULL)
2850 goto err;
2851 self->dispatch_table = PyObject_GetAttr(m, dispatch_table_str);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002852 Py_DECREF(m);
Tim Peters5b7da392003-02-04 00:21:07 +00002853 if (self->dispatch_table == NULL)
2854 goto err;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002855 }
2856 else {
Tim Peters5b7da392003-02-04 00:21:07 +00002857 self->dispatch_table = dispatch_table;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002858 Py_INCREF(dispatch_table);
2859 }
Jeremy Hylton4cf63192003-04-09 21:05:12 +00002860 PyObject_GC_Track(self);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002861
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002862 return self;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002863
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002864 err:
Tim Peters5bd2a792003-02-01 16:45:06 +00002865 Py_DECREF(self);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002866 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002867}
2868
2869
2870static PyObject *
Martin v. Löwis544f1192004-07-27 05:22:33 +00002871get_Pickler(PyObject *self, PyObject *args, PyObject *kwds)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002872{
Martin v. Löwis15e62742006-02-27 16:46:16 +00002873 static char *kwlist[] = {"file", "protocol", NULL};
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002874 PyObject *file = NULL;
Tim Peters5bd2a792003-02-01 16:45:06 +00002875 int proto = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002876
Tim Peters92c8bb32003-02-13 23:00:26 +00002877 /* XXX
Martin v. Löwis544f1192004-07-27 05:22:33 +00002878 * The documented signature is Pickler(file, protocol=0), but this
Tim Peters92c8bb32003-02-13 23:00:26 +00002879 * accepts Pickler() and Pickler(integer) too. The meaning then
2880 * is clear as mud, undocumented, and not supported by pickle.py.
2881 * I'm told Zope uses this, but I haven't traced into this code
2882 * far enough to figure out what it means.
Tim Peters5bd2a792003-02-01 16:45:06 +00002883 */
2884 if (!PyArg_ParseTuple(args, "|i:Pickler", &proto)) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002885 PyErr_Clear();
Tim Peters5bd2a792003-02-01 16:45:06 +00002886 proto = 0;
Martin v. Löwis544f1192004-07-27 05:22:33 +00002887 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|i:Pickler",
2888 kwlist, &file, &proto))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002889 return NULL;
2890 }
Tim Peters5bd2a792003-02-01 16:45:06 +00002891 return (PyObject *)newPicklerobject(file, proto);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002892}
2893
2894
2895static void
Tim Peterscba30e22003-02-01 06:24:36 +00002896Pickler_dealloc(Picklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002897{
Jeremy Hylton4cf63192003-04-09 21:05:12 +00002898 PyObject_GC_UnTrack(self);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002899 Py_XDECREF(self->write);
2900 Py_XDECREF(self->memo);
2901 Py_XDECREF(self->fast_memo);
2902 Py_XDECREF(self->arg);
2903 Py_XDECREF(self->file);
2904 Py_XDECREF(self->pers_func);
2905 Py_XDECREF(self->inst_pers_func);
2906 Py_XDECREF(self->dispatch_table);
Tim Peters5bd2a792003-02-01 16:45:06 +00002907 PyMem_Free(self->write_buf);
Tim Peters3cfe7542003-05-21 21:29:48 +00002908 self->ob_type->tp_free((PyObject *)self);
Jeremy Hylton4cf63192003-04-09 21:05:12 +00002909}
2910
2911static int
2912Pickler_traverse(Picklerobject *self, visitproc visit, void *arg)
2913{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002914 Py_VISIT(self->write);
2915 Py_VISIT(self->memo);
2916 Py_VISIT(self->fast_memo);
2917 Py_VISIT(self->arg);
2918 Py_VISIT(self->file);
2919 Py_VISIT(self->pers_func);
2920 Py_VISIT(self->inst_pers_func);
2921 Py_VISIT(self->dispatch_table);
Jeremy Hylton4cf63192003-04-09 21:05:12 +00002922 return 0;
2923}
2924
2925static int
2926Pickler_clear(Picklerobject *self)
2927{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002928 Py_CLEAR(self->write);
2929 Py_CLEAR(self->memo);
2930 Py_CLEAR(self->fast_memo);
2931 Py_CLEAR(self->arg);
2932 Py_CLEAR(self->file);
2933 Py_CLEAR(self->pers_func);
2934 Py_CLEAR(self->inst_pers_func);
2935 Py_CLEAR(self->dispatch_table);
Jeremy Hylton4cf63192003-04-09 21:05:12 +00002936 return 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002937}
2938
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002939static PyObject *
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00002940Pickler_get_pers_func(Picklerobject *p)
2941{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002942 if (p->pers_func == NULL)
2943 PyErr_SetString(PyExc_AttributeError, "persistent_id");
2944 else
2945 Py_INCREF(p->pers_func);
2946 return p->pers_func;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002947}
2948
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00002949static int
2950Pickler_set_pers_func(Picklerobject *p, PyObject *v)
2951{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002952 if (v == NULL) {
2953 PyErr_SetString(PyExc_TypeError,
2954 "attribute deletion is not supported");
2955 return -1;
2956 }
2957 Py_XDECREF(p->pers_func);
2958 Py_INCREF(v);
2959 p->pers_func = v;
2960 return 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002961}
2962
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00002963static int
2964Pickler_set_inst_pers_func(Picklerobject *p, PyObject *v)
2965{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002966 if (v == NULL) {
2967 PyErr_SetString(PyExc_TypeError,
2968 "attribute deletion is not supported");
2969 return -1;
2970 }
2971 Py_XDECREF(p->inst_pers_func);
2972 Py_INCREF(v);
2973 p->inst_pers_func = v;
2974 return 0;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00002975}
2976
2977static PyObject *
2978Pickler_get_memo(Picklerobject *p)
2979{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002980 if (p->memo == NULL)
2981 PyErr_SetString(PyExc_AttributeError, "memo");
2982 else
2983 Py_INCREF(p->memo);
2984 return p->memo;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00002985}
2986
2987static int
2988Pickler_set_memo(Picklerobject *p, PyObject *v)
2989{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002990 if (v == NULL) {
2991 PyErr_SetString(PyExc_TypeError,
2992 "attribute deletion is not supported");
2993 return -1;
2994 }
2995 if (!PyDict_Check(v)) {
2996 PyErr_SetString(PyExc_TypeError, "memo must be a dictionary");
2997 return -1;
2998 }
2999 Py_XDECREF(p->memo);
3000 Py_INCREF(v);
3001 p->memo = v;
3002 return 0;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003003}
3004
3005static PyObject *
3006Pickler_get_error(Picklerobject *p)
3007{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003008 /* why is this an attribute on the Pickler? */
3009 Py_INCREF(PicklingError);
3010 return PicklingError;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003011}
3012
3013static PyMemberDef Pickler_members[] = {
3014 {"binary", T_INT, offsetof(Picklerobject, bin)},
3015 {"fast", T_INT, offsetof(Picklerobject, fast)},
Jeremy Hylton3eb46f32001-10-16 17:10:49 +00003016 {NULL}
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003017};
3018
3019static PyGetSetDef Pickler_getsets[] = {
Tim Peterscba30e22003-02-01 06:24:36 +00003020 {"persistent_id", (getter)Pickler_get_pers_func,
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003021 (setter)Pickler_set_pers_func},
3022 {"inst_persistent_id", NULL, (setter)Pickler_set_inst_pers_func},
3023 {"memo", (getter)Pickler_get_memo, (setter)Pickler_set_memo},
Jeremy Hylton3eb46f32001-10-16 17:10:49 +00003024 {"PicklingError", (getter)Pickler_get_error, NULL},
3025 {NULL}
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003026};
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003027
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003028PyDoc_STRVAR(Picklertype__doc__,
3029"Objects that know how to pickle objects\n");
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003030
Guido van Rossum9716aaa1997-12-08 15:15:16 +00003031static PyTypeObject Picklertype = {
3032 PyObject_HEAD_INIT(NULL)
Guido van Rossum60456fd1997-04-09 17:36:32 +00003033 0, /*ob_size*/
Guido van Rossum14648392001-12-08 18:02:58 +00003034 "cPickle.Pickler", /*tp_name*/
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003035 sizeof(Picklerobject), /*tp_basicsize*/
3036 0,
3037 (destructor)Pickler_dealloc, /* tp_dealloc */
3038 0, /* tp_print */
3039 0, /* tp_getattr */
3040 0, /* tp_setattr */
3041 0, /* tp_compare */
3042 0, /* tp_repr */
3043 0, /* tp_as_number */
3044 0, /* tp_as_sequence */
3045 0, /* tp_as_mapping */
3046 0, /* tp_hash */
3047 0, /* tp_call */
3048 0, /* tp_str */
Jason Tishlerfb8595d2003-01-06 12:41:26 +00003049 PyObject_GenericGetAttr, /* tp_getattro */
3050 PyObject_GenericSetAttr, /* tp_setattro */
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003051 0, /* tp_as_buffer */
Jeremy Hylton4cf63192003-04-09 21:05:12 +00003052 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003053 Picklertype__doc__, /* tp_doc */
Jeremy Hylton4cf63192003-04-09 21:05:12 +00003054 (traverseproc)Pickler_traverse, /* tp_traverse */
3055 (inquiry)Pickler_clear, /* tp_clear */
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003056 0, /* tp_richcompare */
3057 0, /* tp_weaklistoffset */
3058 0, /* tp_iter */
3059 0, /* tp_iternext */
3060 Pickler_methods, /* tp_methods */
3061 Pickler_members, /* tp_members */
3062 Pickler_getsets, /* tp_getset */
Guido van Rossum9716aaa1997-12-08 15:15:16 +00003063};
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003064
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003065static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00003066find_class(PyObject *py_module_name, PyObject *py_global_name, PyObject *fc)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003067{
3068 PyObject *global = 0, *module;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003069
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003070 if (fc) {
3071 if (fc==Py_None) {
Tim Peters92c8bb32003-02-13 23:00:26 +00003072 PyErr_SetString(UnpicklingError, "Global and instance "
3073 "pickles are not supported.");
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003074 return NULL;
3075 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00003076 return PyObject_CallFunctionObjArgs(fc, py_module_name,
3077 py_global_name, NULL);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003078 }
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00003079
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003080 module = PySys_GetObject("modules");
3081 if (module == NULL)
3082 return NULL;
Jeremy Hyltond1055231998-08-11 19:52:51 +00003083
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003084 module = PyDict_GetItem(module, py_module_name);
3085 if (module == NULL) {
3086 module = PyImport_Import(py_module_name);
3087 if (!module)
3088 return NULL;
3089 global = PyObject_GetAttr(module, py_global_name);
3090 Py_DECREF(module);
3091 }
3092 else
3093 global = PyObject_GetAttr(module, py_global_name);
3094 return global;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003095}
3096
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003097static int
Tim Peterscba30e22003-02-01 06:24:36 +00003098marker(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003099{
3100 if (self->num_marks < 1) {
3101 PyErr_SetString(UnpicklingError, "could not find MARK");
3102 return -1;
3103 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003104
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003105 return self->marks[--self->num_marks];
Guido van Rossum60456fd1997-04-09 17:36:32 +00003106}
3107
Tim Peters84e87f32001-03-17 04:50:51 +00003108
Guido van Rossum60456fd1997-04-09 17:36:32 +00003109static int
Tim Peterscba30e22003-02-01 06:24:36 +00003110load_none(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003111{
3112 PDATA_APPEND(self->stack, Py_None, -1);
3113 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003114}
3115
Guido van Rossumf9ffb031999-02-04 14:54:04 +00003116static int
Tim Peterscba30e22003-02-01 06:24:36 +00003117bad_readline(void)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003118{
3119 PyErr_SetString(UnpicklingError, "pickle data was truncated");
3120 return -1;
Guido van Rossumf9ffb031999-02-04 14:54:04 +00003121}
Guido van Rossum60456fd1997-04-09 17:36:32 +00003122
3123static int
Tim Peterscba30e22003-02-01 06:24:36 +00003124load_int(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003125{
3126 PyObject *py_int = 0;
3127 char *endptr, *s;
3128 int len, res = -1;
3129 long l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003130
Tim Peters0bc93f52003-02-02 18:29:33 +00003131 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003132 if (len < 2) return bad_readline();
3133 if (!( s=pystrndup(s,len))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003134
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003135 errno = 0;
3136 l = strtol(s, &endptr, 0);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003137
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003138 if (errno || (*endptr != '\n') || (endptr[1] != '\0')) {
3139 /* Hm, maybe we've got something long. Let's try reading
3140 it as a Python long object. */
3141 errno = 0;
3142 py_int = PyLong_FromString(s, NULL, 0);
3143 if (py_int == NULL) {
3144 PyErr_SetString(PyExc_ValueError,
3145 "could not convert string to int");
3146 goto finally;
3147 }
3148 }
3149 else {
Guido van Rossume2763392002-04-05 19:30:08 +00003150 if (len == 3 && (l == 0 || l == 1)) {
3151 if (!( py_int = PyBool_FromLong(l))) goto finally;
3152 }
3153 else {
3154 if (!( py_int = PyInt_FromLong(l))) goto finally;
3155 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003156 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003157
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003158 free(s);
3159 PDATA_PUSH(self->stack, py_int, -1);
3160 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003161
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003162 finally:
3163 free(s);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003164
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003165 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003166}
3167
Tim Peters3c67d792003-02-02 17:59:11 +00003168static int
3169load_bool(Unpicklerobject *self, PyObject *boolean)
3170{
3171 assert(boolean == Py_True || boolean == Py_False);
Tim Peterse0a39072003-02-03 15:45:56 +00003172 PDATA_APPEND(self->stack, boolean, -1);
Tim Peters3c67d792003-02-02 17:59:11 +00003173 return 0;
3174}
3175
Tim Petersee1a53c2003-02-02 02:57:53 +00003176/* s contains x bytes of a little-endian integer. Return its value as a
3177 * C int. Obscure: when x is 1 or 2, this is an unsigned little-endian
3178 * int, but when x is 4 it's a signed one. This is an historical source
3179 * of x-platform bugs.
3180 */
Tim Peters84e87f32001-03-17 04:50:51 +00003181static long
Tim Petersee1a53c2003-02-02 02:57:53 +00003182calc_binint(char *s, int x)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003183{
3184 unsigned char c;
3185 int i;
3186 long l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003187
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003188 for (i = 0, l = 0L; i < x; i++) {
3189 c = (unsigned char)s[i];
3190 l |= (long)c << (i * 8);
3191 }
Tim Petersbfa18f72001-04-10 01:54:42 +00003192#if SIZEOF_LONG > 4
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003193 /* Unlike BININT1 and BININT2, BININT (more accurately BININT4)
3194 * is signed, so on a box with longs bigger than 4 bytes we need
3195 * to extend a BININT's sign bit to the full width.
3196 */
3197 if (x == 4 && l & (1L << 31))
3198 l |= (~0L) << 32;
Tim Petersbfa18f72001-04-10 01:54:42 +00003199#endif
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003200 return l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003201}
3202
3203
3204static int
Tim Peterscba30e22003-02-01 06:24:36 +00003205load_binintx(Unpicklerobject *self, char *s, int x)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003206{
3207 PyObject *py_int = 0;
3208 long l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003209
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003210 l = calc_binint(s, x);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003211
Tim Peterscba30e22003-02-01 06:24:36 +00003212 if (!( py_int = PyInt_FromLong(l)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003213 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003214
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003215 PDATA_PUSH(self->stack, py_int, -1);
3216 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003217}
3218
3219
3220static int
Tim Peterscba30e22003-02-01 06:24:36 +00003221load_binint(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003222{
3223 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003224
Tim Peters0bc93f52003-02-02 18:29:33 +00003225 if (self->read_func(self, &s, 4) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003226 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003227
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003228 return load_binintx(self, s, 4);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003229}
3230
3231
3232static int
Tim Peterscba30e22003-02-01 06:24:36 +00003233load_binint1(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003234{
3235 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003236
Tim Peters0bc93f52003-02-02 18:29:33 +00003237 if (self->read_func(self, &s, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003238 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003239
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003240 return load_binintx(self, s, 1);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003241}
3242
3243
3244static int
Tim Peterscba30e22003-02-01 06:24:36 +00003245load_binint2(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003246{
3247 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003248
Tim Peters0bc93f52003-02-02 18:29:33 +00003249 if (self->read_func(self, &s, 2) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003250 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003251
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003252 return load_binintx(self, s, 2);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003253}
Tim Peters84e87f32001-03-17 04:50:51 +00003254
Guido van Rossum60456fd1997-04-09 17:36:32 +00003255static int
Tim Peterscba30e22003-02-01 06:24:36 +00003256load_long(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003257{
3258 PyObject *l = 0;
3259 char *end, *s;
3260 int len, res = -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003261
Tim Peters0bc93f52003-02-02 18:29:33 +00003262 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003263 if (len < 2) return bad_readline();
3264 if (!( s=pystrndup(s,len))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003265
Tim Peterscba30e22003-02-01 06:24:36 +00003266 if (!( l = PyLong_FromString(s, &end, 0)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003267 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003268
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003269 free(s);
3270 PDATA_PUSH(self->stack, l, -1);
3271 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003272
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003273 finally:
3274 free(s);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003275
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003276 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003277}
3278
Tim Petersee1a53c2003-02-02 02:57:53 +00003279/* 'size' bytes contain the # of bytes of little-endian 256's-complement
3280 * data following.
3281 */
3282static int
3283load_counted_long(Unpicklerobject *self, int size)
3284{
Martin v. Löwis18e16552006-02-15 17:27:45 +00003285 Py_ssize_t i;
Tim Petersee1a53c2003-02-02 02:57:53 +00003286 char *nbytes;
3287 unsigned char *pdata;
3288 PyObject *along;
3289
3290 assert(size == 1 || size == 4);
3291 i = self->read_func(self, &nbytes, size);
3292 if (i < 0) return -1;
3293
3294 size = calc_binint(nbytes, size);
3295 if (size < 0) {
3296 /* Corrupt or hostile pickle -- we never write one like
3297 * this.
3298 */
Tim Peters0c7c48e2003-02-03 22:07:24 +00003299 PyErr_SetString(UnpicklingError, "LONG pickle has negative "
Tim Petersee1a53c2003-02-02 02:57:53 +00003300 "byte count");
3301 return -1;
3302 }
3303
3304 if (size == 0)
3305 along = PyLong_FromLong(0L);
3306 else {
3307 /* Read the raw little-endian bytes & convert. */
Neal Norwitz529baf22003-02-02 17:08:33 +00003308 i = self->read_func(self, (char **)&pdata, size);
Tim Petersee1a53c2003-02-02 02:57:53 +00003309 if (i < 0) return -1;
3310 along = _PyLong_FromByteArray(pdata, (size_t)size,
3311 1 /* little endian */, 1 /* signed */);
3312 }
3313 if (along == NULL)
3314 return -1;
3315 PDATA_PUSH(self->stack, along, -1);
3316 return 0;
3317}
Tim Peters84e87f32001-03-17 04:50:51 +00003318
Guido van Rossum60456fd1997-04-09 17:36:32 +00003319static int
Tim Peterscba30e22003-02-01 06:24:36 +00003320load_float(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003321{
3322 PyObject *py_float = 0;
3323 char *endptr, *s;
3324 int len, res = -1;
3325 double d;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003326
Tim Peters0bc93f52003-02-02 18:29:33 +00003327 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003328 if (len < 2) return bad_readline();
3329 if (!( s=pystrndup(s,len))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003330
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003331 errno = 0;
Martin v. Löwis737ea822004-06-08 18:52:54 +00003332 d = PyOS_ascii_strtod(s, &endptr);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003333
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003334 if (errno || (endptr[0] != '\n') || (endptr[1] != '\0')) {
3335 PyErr_SetString(PyExc_ValueError,
3336 "could not convert string to float");
3337 goto finally;
3338 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003339
Tim Peterscba30e22003-02-01 06:24:36 +00003340 if (!( py_float = PyFloat_FromDouble(d)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003341 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003342
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003343 free(s);
3344 PDATA_PUSH(self->stack, py_float, -1);
3345 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003346
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003347 finally:
3348 free(s);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003349
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003350 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003351}
3352
Guido van Rossum60456fd1997-04-09 17:36:32 +00003353static int
Tim Peterscba30e22003-02-01 06:24:36 +00003354load_binfloat(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003355{
Tim Peters9905b942003-03-20 20:53:32 +00003356 PyObject *py_float;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003357 double x;
3358 char *p;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003359
Tim Peters0bc93f52003-02-02 18:29:33 +00003360 if (self->read_func(self, &p, 8) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003361 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003362
Tim Peters9905b942003-03-20 20:53:32 +00003363 x = _PyFloat_Unpack8((unsigned char *)p, 0);
3364 if (x == -1.0 && PyErr_Occurred())
3365 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003366
Tim Peters9905b942003-03-20 20:53:32 +00003367 py_float = PyFloat_FromDouble(x);
3368 if (py_float == NULL)
3369 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003370
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003371 PDATA_PUSH(self->stack, py_float, -1);
3372 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003373}
Guido van Rossum60456fd1997-04-09 17:36:32 +00003374
3375static int
Tim Peterscba30e22003-02-01 06:24:36 +00003376load_string(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003377{
3378 PyObject *str = 0;
Martin v. Löwis8a8da792002-08-14 07:46:28 +00003379 int len, res = -1;
3380 char *s, *p;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003381
Tim Peters0bc93f52003-02-02 18:29:33 +00003382 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003383 if (len < 2) return bad_readline();
3384 if (!( s=pystrndup(s,len))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003385
Martin v. Löwis8a8da792002-08-14 07:46:28 +00003386
3387 /* Strip outermost quotes */
3388 while (s[len-1] <= ' ')
3389 len--;
3390 if(s[0]=='"' && s[len-1]=='"'){
3391 s[len-1] = '\0';
3392 p = s + 1 ;
3393 len -= 2;
3394 } else if(s[0]=='\'' && s[len-1]=='\''){
3395 s[len-1] = '\0';
3396 p = s + 1 ;
3397 len -= 2;
3398 } else
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003399 goto insecure;
3400 /********************************************/
Guido van Rossum9716aaa1997-12-08 15:15:16 +00003401
Martin v. Löwis8a8da792002-08-14 07:46:28 +00003402 str = PyString_DecodeEscape(p, len, NULL, 0, NULL);
3403 if (str) {
3404 PDATA_PUSH(self->stack, str, -1);
3405 res = 0;
3406 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003407 free(s);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003408 return res;
Tim Peters84e87f32001-03-17 04:50:51 +00003409
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003410 insecure:
3411 free(s);
3412 PyErr_SetString(PyExc_ValueError,"insecure string pickle");
3413 return -1;
Tim Peters84e87f32001-03-17 04:50:51 +00003414}
Guido van Rossum60456fd1997-04-09 17:36:32 +00003415
3416
3417static int
Tim Peterscba30e22003-02-01 06:24:36 +00003418load_binstring(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003419{
3420 PyObject *py_string = 0;
3421 long l;
3422 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003423
Tim Peters0bc93f52003-02-02 18:29:33 +00003424 if (self->read_func(self, &s, 4) < 0) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003425
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003426 l = calc_binint(s, 4);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003427
Tim Peters0bc93f52003-02-02 18:29:33 +00003428 if (self->read_func(self, &s, l) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003429 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003430
Tim Peterscba30e22003-02-01 06:24:36 +00003431 if (!( py_string = PyString_FromStringAndSize(s, l)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003432 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003433
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003434 PDATA_PUSH(self->stack, py_string, -1);
3435 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003436}
3437
3438
3439static int
Tim Peterscba30e22003-02-01 06:24:36 +00003440load_short_binstring(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003441{
3442 PyObject *py_string = 0;
3443 unsigned char l;
3444 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003445
Tim Peters0bc93f52003-02-02 18:29:33 +00003446 if (self->read_func(self, &s, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003447 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003448
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003449 l = (unsigned char)s[0];
Guido van Rossum60456fd1997-04-09 17:36:32 +00003450
Tim Peters0bc93f52003-02-02 18:29:33 +00003451 if (self->read_func(self, &s, l) < 0) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003452
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003453 if (!( py_string = PyString_FromStringAndSize(s, l))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003454
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003455 PDATA_PUSH(self->stack, py_string, -1);
3456 return 0;
Tim Peters84e87f32001-03-17 04:50:51 +00003457}
Guido van Rossum60456fd1997-04-09 17:36:32 +00003458
3459
Martin v. Löwis339d0f72001-08-17 18:39:25 +00003460#ifdef Py_USING_UNICODE
Guido van Rossum60456fd1997-04-09 17:36:32 +00003461static int
Tim Peterscba30e22003-02-01 06:24:36 +00003462load_unicode(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003463{
3464 PyObject *str = 0;
3465 int len, res = -1;
3466 char *s;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003467
Tim Peters0bc93f52003-02-02 18:29:33 +00003468 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003469 if (len < 1) return bad_readline();
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003470
Tim Peterscba30e22003-02-01 06:24:36 +00003471 if (!( str = PyUnicode_DecodeRawUnicodeEscape(s, len - 1, NULL)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003472 goto finally;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003473
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003474 PDATA_PUSH(self->stack, str, -1);
3475 return 0;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003476
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003477 finally:
3478 return res;
Tim Peters84e87f32001-03-17 04:50:51 +00003479}
Martin v. Löwis339d0f72001-08-17 18:39:25 +00003480#endif
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003481
3482
Martin v. Löwis339d0f72001-08-17 18:39:25 +00003483#ifdef Py_USING_UNICODE
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003484static int
Tim Peterscba30e22003-02-01 06:24:36 +00003485load_binunicode(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003486{
3487 PyObject *unicode;
3488 long l;
3489 char *s;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003490
Tim Peters0bc93f52003-02-02 18:29:33 +00003491 if (self->read_func(self, &s, 4) < 0) return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003492
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003493 l = calc_binint(s, 4);
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003494
Tim Peters0bc93f52003-02-02 18:29:33 +00003495 if (self->read_func(self, &s, l) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003496 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003497
Tim Peterscba30e22003-02-01 06:24:36 +00003498 if (!( unicode = PyUnicode_DecodeUTF8(s, l, NULL)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003499 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003500
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003501 PDATA_PUSH(self->stack, unicode, -1);
3502 return 0;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003503}
Martin v. Löwis339d0f72001-08-17 18:39:25 +00003504#endif
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003505
3506
3507static int
Tim Peterscba30e22003-02-01 06:24:36 +00003508load_tuple(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003509{
3510 PyObject *tup;
3511 int i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003512
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003513 if ((i = marker(self)) < 0) return -1;
3514 if (!( tup=Pdata_popTuple(self->stack, i))) return -1;
3515 PDATA_PUSH(self->stack, tup, -1);
3516 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003517}
3518
3519static int
Tim Peters1d63c9f2003-02-02 20:29:39 +00003520load_counted_tuple(Unpicklerobject *self, int len)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003521{
Tim Peters1d63c9f2003-02-02 20:29:39 +00003522 PyObject *tup = PyTuple_New(len);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003523
Tim Peters1d63c9f2003-02-02 20:29:39 +00003524 if (tup == NULL)
3525 return -1;
3526
3527 while (--len >= 0) {
3528 PyObject *element;
3529
3530 PDATA_POP(self->stack, element);
3531 if (element == NULL)
3532 return -1;
3533 PyTuple_SET_ITEM(tup, len, element);
3534 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003535 PDATA_PUSH(self->stack, tup, -1);
3536 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003537}
3538
3539static int
Tim Peterscba30e22003-02-01 06:24:36 +00003540load_empty_list(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003541{
3542 PyObject *list;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003543
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003544 if (!( list=PyList_New(0))) return -1;
3545 PDATA_PUSH(self->stack, list, -1);
3546 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003547}
3548
3549static int
Tim Peterscba30e22003-02-01 06:24:36 +00003550load_empty_dict(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003551{
3552 PyObject *dict;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003553
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003554 if (!( dict=PyDict_New())) return -1;
3555 PDATA_PUSH(self->stack, dict, -1);
3556 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003557}
3558
3559
3560static int
Tim Peterscba30e22003-02-01 06:24:36 +00003561load_list(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003562{
3563 PyObject *list = 0;
3564 int i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003565
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003566 if ((i = marker(self)) < 0) return -1;
3567 if (!( list=Pdata_popList(self->stack, i))) return -1;
3568 PDATA_PUSH(self->stack, list, -1);
3569 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003570}
3571
3572static int
Tim Peterscba30e22003-02-01 06:24:36 +00003573load_dict(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003574{
3575 PyObject *dict, *key, *value;
3576 int i, j, k;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003577
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003578 if ((i = marker(self)) < 0) return -1;
3579 j=self->stack->length;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003580
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003581 if (!( dict = PyDict_New())) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003582
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003583 for (k = i+1; k < j; k += 2) {
3584 key =self->stack->data[k-1];
3585 value=self->stack->data[k ];
3586 if (PyDict_SetItem(dict, key, value) < 0) {
3587 Py_DECREF(dict);
3588 return -1;
3589 }
3590 }
3591 Pdata_clear(self->stack, i);
3592 PDATA_PUSH(self->stack, dict, -1);
3593 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003594}
3595
3596static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00003597Instance_New(PyObject *cls, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003598{
Tim Peters1f1b2d22003-02-01 02:16:37 +00003599 PyObject *r = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003600
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003601 if (PyClass_Check(cls)) {
3602 int l;
Tim Peters84e87f32001-03-17 04:50:51 +00003603
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003604 if ((l=PyObject_Size(args)) < 0) goto err;
3605 if (!( l )) {
3606 PyObject *__getinitargs__;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003607
Tim Peterscba30e22003-02-01 06:24:36 +00003608 __getinitargs__ = PyObject_GetAttr(cls,
Jeremy Hyltonf00368f2003-06-16 20:19:49 +00003609 __getinitargs___str);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003610 if (!__getinitargs__) {
Tim Peterscba30e22003-02-01 06:24:36 +00003611 /* We have a class with no __getinitargs__,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003612 so bypass usual construction */
3613 PyObject *inst;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003614
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003615 PyErr_Clear();
Tim Peterscba30e22003-02-01 06:24:36 +00003616 if (!( inst=PyInstance_NewRaw(cls, NULL)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003617 goto err;
3618 return inst;
3619 }
3620 Py_DECREF(__getinitargs__);
3621 }
Tim Peters84e87f32001-03-17 04:50:51 +00003622
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003623 if ((r=PyInstance_New(cls, args, NULL))) return r;
3624 else goto err;
3625 }
Tim Peters84e87f32001-03-17 04:50:51 +00003626
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003627 if ((r=PyObject_CallObject(cls, args))) return r;
Guido van Rossum142eeb81997-08-13 03:14:41 +00003628
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003629 err:
3630 {
3631 PyObject *tp, *v, *tb;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003632
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003633 PyErr_Fetch(&tp, &v, &tb);
Raymond Hettinger8ae46892003-10-12 19:09:37 +00003634 if ((r=PyTuple_Pack(3,v,cls,args))) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003635 Py_XDECREF(v);
3636 v=r;
3637 }
3638 PyErr_Restore(tp,v,tb);
3639 }
3640 return NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003641}
Tim Peters84e87f32001-03-17 04:50:51 +00003642
Guido van Rossum60456fd1997-04-09 17:36:32 +00003643
3644static int
Tim Peterscba30e22003-02-01 06:24:36 +00003645load_obj(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003646{
3647 PyObject *class, *tup, *obj=0;
3648 int i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003649
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003650 if ((i = marker(self)) < 0) return -1;
3651 if (!( tup=Pdata_popTuple(self->stack, i+1))) return -1;
3652 PDATA_POP(self->stack, class);
3653 if (class) {
3654 obj = Instance_New(class, tup);
3655 Py_DECREF(class);
3656 }
3657 Py_DECREF(tup);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003658
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003659 if (! obj) return -1;
3660 PDATA_PUSH(self->stack, obj, -1);
3661 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003662}
3663
3664
3665static int
Tim Peterscba30e22003-02-01 06:24:36 +00003666load_inst(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003667{
3668 PyObject *tup, *class=0, *obj=0, *module_name, *class_name;
3669 int i, len;
3670 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003671
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003672 if ((i = marker(self)) < 0) return -1;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003673
Tim Peters0bc93f52003-02-02 18:29:33 +00003674 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003675 if (len < 2) return bad_readline();
3676 module_name = PyString_FromStringAndSize(s, len - 1);
3677 if (!module_name) 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) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003680 if (len < 2) return bad_readline();
3681 if ((class_name = PyString_FromStringAndSize(s, len - 1))) {
Tim Peterscba30e22003-02-01 06:24:36 +00003682 class = find_class(module_name, class_name,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003683 self->find_class);
3684 Py_DECREF(class_name);
3685 }
3686 }
3687 Py_DECREF(module_name);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003688
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003689 if (! class) return -1;
Tim Peters84e87f32001-03-17 04:50:51 +00003690
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003691 if ((tup=Pdata_popTuple(self->stack, i))) {
3692 obj = Instance_New(class, tup);
3693 Py_DECREF(tup);
3694 }
3695 Py_DECREF(class);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003696
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003697 if (! obj) return -1;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003698
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003699 PDATA_PUSH(self->stack, obj, -1);
3700 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003701}
3702
Tim Peterseab7db32003-02-13 18:24:14 +00003703static int
3704load_newobj(Unpicklerobject *self)
3705{
3706 PyObject *args = NULL;
3707 PyObject *clsraw = NULL;
3708 PyTypeObject *cls; /* clsraw cast to its true type */
3709 PyObject *obj;
3710
3711 /* Stack is ... cls argtuple, and we want to call
3712 * cls.__new__(cls, *argtuple).
3713 */
3714 PDATA_POP(self->stack, args);
3715 if (args == NULL) goto Fail;
3716 if (! PyTuple_Check(args)) {
3717 PyErr_SetString(UnpicklingError, "NEWOBJ expected an arg "
3718 "tuple.");
3719 goto Fail;
3720 }
3721
3722 PDATA_POP(self->stack, clsraw);
3723 cls = (PyTypeObject *)clsraw;
3724 if (cls == NULL) goto Fail;
3725 if (! PyType_Check(cls)) {
3726 PyErr_SetString(UnpicklingError, "NEWOBJ class argument "
3727 "isn't a type object");
3728 goto Fail;
3729 }
3730 if (cls->tp_new == NULL) {
3731 PyErr_SetString(UnpicklingError, "NEWOBJ class argument "
3732 "has NULL tp_new");
3733 goto Fail;
3734 }
3735
3736 /* Call __new__. */
3737 obj = cls->tp_new(cls, args, NULL);
3738 if (obj == NULL) goto Fail;
3739
3740 Py_DECREF(args);
3741 Py_DECREF(clsraw);
3742 PDATA_PUSH(self->stack, obj, -1);
3743 return 0;
3744
3745 Fail:
3746 Py_XDECREF(args);
3747 Py_XDECREF(clsraw);
3748 return -1;
3749}
Guido van Rossum60456fd1997-04-09 17:36:32 +00003750
3751static int
Tim Peterscba30e22003-02-01 06:24:36 +00003752load_global(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003753{
3754 PyObject *class = 0, *module_name = 0, *class_name = 0;
3755 int len;
3756 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003757
Tim Peters0bc93f52003-02-02 18:29:33 +00003758 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003759 if (len < 2) return bad_readline();
3760 module_name = PyString_FromStringAndSize(s, len - 1);
3761 if (!module_name) return -1;
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) {
Tim Peters4e52ca82002-12-07 02:43:28 +00003764 if (len < 2) {
3765 Py_DECREF(module_name);
3766 return bad_readline();
3767 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003768 if ((class_name = PyString_FromStringAndSize(s, len - 1))) {
Tim Peterscba30e22003-02-01 06:24:36 +00003769 class = find_class(module_name, class_name,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003770 self->find_class);
3771 Py_DECREF(class_name);
3772 }
3773 }
3774 Py_DECREF(module_name);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003775
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003776 if (! class) return -1;
3777 PDATA_PUSH(self->stack, class, -1);
3778 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003779}
3780
3781
3782static int
Tim Peterscba30e22003-02-01 06:24:36 +00003783load_persid(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003784{
3785 PyObject *pid = 0;
3786 int len;
3787 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003788
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003789 if (self->pers_func) {
Tim Peters0bc93f52003-02-02 18:29:33 +00003790 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003791 if (len < 2) return bad_readline();
3792
3793 pid = PyString_FromStringAndSize(s, len - 1);
3794 if (!pid) return -1;
3795
3796 if (PyList_Check(self->pers_func)) {
3797 if (PyList_Append(self->pers_func, pid) < 0) {
3798 Py_DECREF(pid);
3799 return -1;
3800 }
3801 }
3802 else {
3803 ARG_TUP(self, pid);
3804 if (self->arg) {
Tim Peterscba30e22003-02-01 06:24:36 +00003805 pid = PyObject_Call(self->pers_func, self->arg,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003806 NULL);
3807 FREE_ARG_TUP(self);
3808 }
3809 }
3810
3811 if (! pid) return -1;
3812
3813 PDATA_PUSH(self->stack, pid, -1);
3814 return 0;
3815 }
3816 else {
3817 PyErr_SetString(UnpicklingError,
3818 "A load persistent id instruction was encountered,\n"
3819 "but no persistent_load function was specified.");
3820 return -1;
3821 }
3822}
3823
3824static int
Tim Peterscba30e22003-02-01 06:24:36 +00003825load_binpersid(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003826{
3827 PyObject *pid = 0;
3828
3829 if (self->pers_func) {
3830 PDATA_POP(self->stack, pid);
3831 if (! pid) return -1;
3832
3833 if (PyList_Check(self->pers_func)) {
3834 if (PyList_Append(self->pers_func, pid) < 0) {
3835 Py_DECREF(pid);
3836 return -1;
3837 }
3838 }
3839 else {
3840 ARG_TUP(self, pid);
3841 if (self->arg) {
3842 pid = PyObject_Call(self->pers_func, self->arg,
3843 NULL);
3844 FREE_ARG_TUP(self);
3845 }
3846 if (! pid) return -1;
3847 }
3848
3849 PDATA_PUSH(self->stack, pid, -1);
3850 return 0;
3851 }
3852 else {
3853 PyErr_SetString(UnpicklingError,
3854 "A load persistent id instruction was encountered,\n"
3855 "but no persistent_load function was specified.");
3856 return -1;
3857 }
3858}
3859
3860
3861static int
Tim Peterscba30e22003-02-01 06:24:36 +00003862load_pop(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003863{
3864 int len;
3865
3866 if (!( (len=self->stack->length) > 0 )) return stackUnderflow();
3867
3868 /* Note that we split the (pickle.py) stack into two stacks,
3869 an object stack and a mark stack. We have to be clever and
3870 pop the right one. We do this by looking at the top of the
3871 mark stack.
3872 */
3873
3874 if ((self->num_marks > 0) &&
3875 (self->marks[self->num_marks - 1] == len))
3876 self->num_marks--;
3877 else {
3878 len--;
3879 Py_DECREF(self->stack->data[len]);
3880 self->stack->length=len;
3881 }
3882
3883 return 0;
3884}
3885
3886
3887static int
Tim Peterscba30e22003-02-01 06:24:36 +00003888load_pop_mark(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003889{
3890 int i;
3891
3892 if ((i = marker(self)) < 0)
3893 return -1;
3894
3895 Pdata_clear(self->stack, i);
3896
3897 return 0;
3898}
3899
3900
3901static int
Tim Peterscba30e22003-02-01 06:24:36 +00003902load_dup(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003903{
3904 PyObject *last;
3905 int len;
3906
3907 if ((len = self->stack->length) <= 0) return stackUnderflow();
3908 last=self->stack->data[len-1];
3909 Py_INCREF(last);
3910 PDATA_PUSH(self->stack, last, -1);
3911 return 0;
3912}
3913
3914
3915static int
Tim Peterscba30e22003-02-01 06:24:36 +00003916load_get(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003917{
3918 PyObject *py_str = 0, *value = 0;
3919 int len;
3920 char *s;
3921 int rc;
3922
Tim Peters0bc93f52003-02-02 18:29:33 +00003923 if ((len = self->readline_func(self, &s)) < 0) return -1;
Guido van Rossumf9ffb031999-02-04 14:54:04 +00003924 if (len < 2) return bad_readline();
Tim Peters84e87f32001-03-17 04:50:51 +00003925
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003926 if (!( py_str = PyString_FromStringAndSize(s, len - 1))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003927
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003928 value = PyDict_GetItem(self->memo, py_str);
3929 if (! value) {
3930 PyErr_SetObject(BadPickleGet, py_str);
3931 rc = -1;
Tim Peters92c8bb32003-02-13 23:00:26 +00003932 }
3933 else {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003934 PDATA_APPEND(self->stack, value, -1);
3935 rc = 0;
3936 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003937
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003938 Py_DECREF(py_str);
3939 return rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003940}
3941
3942
3943static int
Tim Peterscba30e22003-02-01 06:24:36 +00003944load_binget(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003945{
3946 PyObject *py_key = 0, *value = 0;
3947 unsigned char key;
3948 char *s;
3949 int rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003950
Tim Peters0bc93f52003-02-02 18:29:33 +00003951 if (self->read_func(self, &s, 1) < 0) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003952
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003953 key = (unsigned char)s[0];
3954 if (!( py_key = PyInt_FromLong((long)key))) return -1;
Guido van Rossumea2b7152000-05-09 18:14:50 +00003955
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003956 value = PyDict_GetItem(self->memo, py_key);
3957 if (! value) {
3958 PyErr_SetObject(BadPickleGet, py_key);
3959 rc = -1;
Tim Peters92c8bb32003-02-13 23:00:26 +00003960 }
3961 else {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003962 PDATA_APPEND(self->stack, value, -1);
3963 rc = 0;
3964 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003965
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003966 Py_DECREF(py_key);
3967 return rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003968}
3969
3970
3971static int
Tim Peterscba30e22003-02-01 06:24:36 +00003972load_long_binget(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003973{
3974 PyObject *py_key = 0, *value = 0;
3975 unsigned char c;
3976 char *s;
3977 long key;
3978 int rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003979
Tim Peters0bc93f52003-02-02 18:29:33 +00003980 if (self->read_func(self, &s, 4) < 0) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003981
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003982 c = (unsigned char)s[0];
3983 key = (long)c;
3984 c = (unsigned char)s[1];
3985 key |= (long)c << 8;
3986 c = (unsigned char)s[2];
3987 key |= (long)c << 16;
3988 c = (unsigned char)s[3];
3989 key |= (long)c << 24;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003990
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003991 if (!( py_key = PyInt_FromLong((long)key))) return -1;
3992
3993 value = PyDict_GetItem(self->memo, py_key);
3994 if (! value) {
3995 PyErr_SetObject(BadPickleGet, py_key);
3996 rc = -1;
Tim Peters92c8bb32003-02-13 23:00:26 +00003997 }
3998 else {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003999 PDATA_APPEND(self->stack, value, -1);
4000 rc = 0;
4001 }
4002
4003 Py_DECREF(py_key);
4004 return rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004005}
4006
Tim Peters2d629652003-02-04 05:06:17 +00004007/* Push an object from the extension registry (EXT[124]). nbytes is
4008 * the number of bytes following the opcode, holding the index (code) value.
4009 */
4010static int
4011load_extension(Unpicklerobject *self, int nbytes)
4012{
4013 char *codebytes; /* the nbytes bytes after the opcode */
4014 long code; /* calc_binint returns long */
4015 PyObject *py_code; /* code as a Python int */
4016 PyObject *obj; /* the object to push */
4017 PyObject *pair; /* (module_name, class_name) */
4018 PyObject *module_name, *class_name;
4019
4020 assert(nbytes == 1 || nbytes == 2 || nbytes == 4);
4021 if (self->read_func(self, &codebytes, nbytes) < 0) return -1;
4022 code = calc_binint(codebytes, nbytes);
4023 if (code <= 0) { /* note that 0 is forbidden */
4024 /* Corrupt or hostile pickle. */
4025 PyErr_SetString(UnpicklingError, "EXT specifies code <= 0");
4026 return -1;
4027 }
4028
4029 /* Look for the code in the cache. */
4030 py_code = PyInt_FromLong(code);
4031 if (py_code == NULL) return -1;
4032 obj = PyDict_GetItem(extension_cache, py_code);
4033 if (obj != NULL) {
4034 /* Bingo. */
4035 Py_DECREF(py_code);
4036 PDATA_APPEND(self->stack, obj, -1);
4037 return 0;
4038 }
4039
4040 /* Look up the (module_name, class_name) pair. */
4041 pair = PyDict_GetItem(inverted_registry, py_code);
4042 if (pair == NULL) {
4043 Py_DECREF(py_code);
4044 PyErr_Format(PyExc_ValueError, "unregistered extension "
4045 "code %ld", code);
4046 return -1;
4047 }
4048 /* Since the extension registry is manipulable via Python code,
Tim Petersfa05ce32003-02-04 05:20:32 +00004049 * confirm that pair is really a 2-tuple of strings.
Tim Peters2d629652003-02-04 05:06:17 +00004050 */
4051 if (!PyTuple_Check(pair) || PyTuple_Size(pair) != 2 ||
4052 !PyString_Check(module_name = PyTuple_GET_ITEM(pair, 0)) ||
4053 !PyString_Check(class_name = PyTuple_GET_ITEM(pair, 1))) {
4054 Py_DECREF(py_code);
4055 PyErr_Format(PyExc_ValueError, "_inverted_registry[%ld] "
4056 "isn't a 2-tuple of strings", code);
4057 return -1;
4058 }
4059 /* Load the object. */
4060 obj = find_class(module_name, class_name, self->find_class);
4061 if (obj == NULL) {
4062 Py_DECREF(py_code);
4063 return -1;
4064 }
4065 /* Cache code -> obj. */
4066 code = PyDict_SetItem(extension_cache, py_code, obj);
4067 Py_DECREF(py_code);
4068 if (code < 0) {
4069 Py_DECREF(obj);
4070 return -1;
4071 }
4072 PDATA_PUSH(self->stack, obj, -1);
4073 return 0;
4074}
Guido van Rossum60456fd1997-04-09 17:36:32 +00004075
4076static int
Tim Peterscba30e22003-02-01 06:24:36 +00004077load_put(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004078{
4079 PyObject *py_str = 0, *value = 0;
4080 int len, l;
4081 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004082
Tim Peters0bc93f52003-02-02 18:29:33 +00004083 if ((l = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004084 if (l < 2) return bad_readline();
4085 if (!( len=self->stack->length )) return stackUnderflow();
4086 if (!( py_str = PyString_FromStringAndSize(s, l - 1))) return -1;
4087 value=self->stack->data[len-1];
4088 l=PyDict_SetItem(self->memo, py_str, value);
4089 Py_DECREF(py_str);
4090 return l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004091}
4092
4093
4094static int
Tim Peterscba30e22003-02-01 06:24:36 +00004095load_binput(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004096{
4097 PyObject *py_key = 0, *value = 0;
4098 unsigned char key;
4099 char *s;
4100 int len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004101
Tim Peters0bc93f52003-02-02 18:29:33 +00004102 if (self->read_func(self, &s, 1) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004103 if (!( (len=self->stack->length) > 0 )) return stackUnderflow();
Guido van Rossum60456fd1997-04-09 17:36:32 +00004104
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004105 key = (unsigned char)s[0];
Guido van Rossum053b8df1998-11-25 16:18:00 +00004106
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004107 if (!( py_key = PyInt_FromLong((long)key))) return -1;
4108 value=self->stack->data[len-1];
4109 len=PyDict_SetItem(self->memo, py_key, value);
4110 Py_DECREF(py_key);
4111 return len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004112}
4113
4114
4115static int
Tim Peterscba30e22003-02-01 06:24:36 +00004116load_long_binput(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004117{
4118 PyObject *py_key = 0, *value = 0;
4119 long key;
4120 unsigned char c;
4121 char *s;
4122 int len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004123
Tim Peters0bc93f52003-02-02 18:29:33 +00004124 if (self->read_func(self, &s, 4) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004125 if (!( len=self->stack->length )) return stackUnderflow();
Guido van Rossum60456fd1997-04-09 17:36:32 +00004126
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004127 c = (unsigned char)s[0];
4128 key = (long)c;
4129 c = (unsigned char)s[1];
4130 key |= (long)c << 8;
4131 c = (unsigned char)s[2];
4132 key |= (long)c << 16;
4133 c = (unsigned char)s[3];
4134 key |= (long)c << 24;
Tim Peters84e87f32001-03-17 04:50:51 +00004135
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004136 if (!( py_key = PyInt_FromLong(key))) return -1;
4137 value=self->stack->data[len-1];
4138 len=PyDict_SetItem(self->memo, py_key, value);
4139 Py_DECREF(py_key);
4140 return len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004141}
4142
4143
4144static int
Tim Peterscba30e22003-02-01 06:24:36 +00004145do_append(Unpicklerobject *self, int x)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004146{
4147 PyObject *value = 0, *list = 0, *append_method = 0;
4148 int len, i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004149
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004150 len=self->stack->length;
4151 if (!( len >= x && x > 0 )) return stackUnderflow();
4152 /* nothing to do */
Tim Peterscba30e22003-02-01 06:24:36 +00004153 if (len==x) return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004154
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004155 list=self->stack->data[x-1];
Guido van Rossum60456fd1997-04-09 17:36:32 +00004156
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004157 if (PyList_Check(list)) {
4158 PyObject *slice;
4159 int list_len;
Tim Peters84e87f32001-03-17 04:50:51 +00004160
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004161 slice=Pdata_popList(self->stack, x);
4162 list_len = PyList_GET_SIZE(list);
4163 i=PyList_SetSlice(list, list_len, list_len, slice);
4164 Py_DECREF(slice);
4165 return i;
4166 }
4167 else {
Guido van Rossum60456fd1997-04-09 17:36:32 +00004168
Tim Peterscba30e22003-02-01 06:24:36 +00004169 if (!( append_method = PyObject_GetAttr(list, append_str)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004170 return -1;
4171
4172 for (i = x; i < len; i++) {
4173 PyObject *junk;
4174
4175 value=self->stack->data[i];
4176 junk=0;
4177 ARG_TUP(self, value);
4178 if (self->arg) {
Tim Peterscba30e22003-02-01 06:24:36 +00004179 junk = PyObject_Call(append_method, self->arg,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004180 NULL);
4181 FREE_ARG_TUP(self);
4182 }
4183 if (! junk) {
4184 Pdata_clear(self->stack, i+1);
4185 self->stack->length=x;
4186 Py_DECREF(append_method);
4187 return -1;
4188 }
4189 Py_DECREF(junk);
4190 }
4191 self->stack->length=x;
4192 Py_DECREF(append_method);
4193 }
4194
4195 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004196}
4197
4198
4199static int
Tim Peterscba30e22003-02-01 06:24:36 +00004200load_append(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004201{
4202 return do_append(self, self->stack->length - 1);
Guido van Rossum60456fd1997-04-09 17:36:32 +00004203}
4204
4205
4206static int
Tim Peterscba30e22003-02-01 06:24:36 +00004207load_appends(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004208{
4209 return do_append(self, marker(self));
Guido van Rossum60456fd1997-04-09 17:36:32 +00004210}
4211
4212
4213static int
Tim Peterscba30e22003-02-01 06:24:36 +00004214do_setitems(Unpicklerobject *self, int x)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004215{
4216 PyObject *value = 0, *key = 0, *dict = 0;
4217 int len, i, r=0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004218
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004219 if (!( (len=self->stack->length) >= x
4220 && x > 0 )) return stackUnderflow();
Guido van Rossum60456fd1997-04-09 17:36:32 +00004221
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004222 dict=self->stack->data[x-1];
Guido van Rossum60456fd1997-04-09 17:36:32 +00004223
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004224 for (i = x+1; i < len; i += 2) {
4225 key =self->stack->data[i-1];
4226 value=self->stack->data[i ];
4227 if (PyObject_SetItem(dict, key, value) < 0) {
4228 r=-1;
4229 break;
4230 }
4231 }
4232
4233 Pdata_clear(self->stack, x);
4234
4235 return r;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004236}
4237
4238
Tim Peters84e87f32001-03-17 04:50:51 +00004239static int
Tim Peterscba30e22003-02-01 06:24:36 +00004240load_setitem(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004241{
4242 return do_setitems(self, self->stack->length - 2);
4243}
Guido van Rossum60456fd1997-04-09 17:36:32 +00004244
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004245static int
Tim Peterscba30e22003-02-01 06:24:36 +00004246load_setitems(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004247{
4248 return do_setitems(self, marker(self));
Guido van Rossum60456fd1997-04-09 17:36:32 +00004249}
4250
Tim Peters84e87f32001-03-17 04:50:51 +00004251
Guido van Rossum60456fd1997-04-09 17:36:32 +00004252static int
Tim Peterscba30e22003-02-01 06:24:36 +00004253load_build(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004254{
Tim Peters080c88b2003-02-15 03:01:11 +00004255 PyObject *state, *inst, *slotstate;
4256 PyObject *__setstate__;
4257 PyObject *d_key, *d_value;
Martin v. Löwis18e16552006-02-15 17:27:45 +00004258 Py_ssize_t i;
Tim Peters080c88b2003-02-15 03:01:11 +00004259 int res = -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004260
Tim Peters080c88b2003-02-15 03:01:11 +00004261 /* Stack is ... instance, state. We want to leave instance at
4262 * the stack top, possibly mutated via instance.__setstate__(state).
4263 */
4264 if (self->stack->length < 2)
4265 return stackUnderflow();
4266 PDATA_POP(self->stack, state);
4267 if (state == NULL)
4268 return -1;
4269 inst = self->stack->data[self->stack->length - 1];
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004270
Tim Peters080c88b2003-02-15 03:01:11 +00004271 __setstate__ = PyObject_GetAttr(inst, __setstate___str);
4272 if (__setstate__ != NULL) {
4273 PyObject *junk = NULL;
4274
4275 /* The explicit __setstate__ is responsible for everything. */
4276 ARG_TUP(self, state);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004277 if (self->arg) {
4278 junk = PyObject_Call(__setstate__, self->arg, NULL);
4279 FREE_ARG_TUP(self);
4280 }
4281 Py_DECREF(__setstate__);
Tim Peters080c88b2003-02-15 03:01:11 +00004282 if (junk == NULL)
4283 return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004284 Py_DECREF(junk);
4285 return 0;
4286 }
Jeremy Hyltonf00368f2003-06-16 20:19:49 +00004287 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4288 return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004289 PyErr_Clear();
Tim Peters080c88b2003-02-15 03:01:11 +00004290
4291 /* A default __setstate__. First see whether state embeds a
4292 * slot state dict too (a proto 2 addition).
4293 */
4294 if (PyTuple_Check(state) && PyTuple_Size(state) == 2) {
4295 PyObject *temp = state;
4296 state = PyTuple_GET_ITEM(temp, 0);
4297 slotstate = PyTuple_GET_ITEM(temp, 1);
4298 Py_INCREF(state);
4299 Py_INCREF(slotstate);
4300 Py_DECREF(temp);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004301 }
Tim Peters080c88b2003-02-15 03:01:11 +00004302 else
4303 slotstate = NULL;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004304
Tim Peters080c88b2003-02-15 03:01:11 +00004305 /* Set inst.__dict__ from the state dict (if any). */
4306 if (state != Py_None) {
4307 PyObject *dict;
4308 if (! PyDict_Check(state)) {
4309 PyErr_SetString(UnpicklingError, "state is not a "
4310 "dictionary");
4311 goto finally;
4312 }
4313 dict = PyObject_GetAttr(inst, __dict___str);
4314 if (dict == NULL)
4315 goto finally;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004316
Tim Peters080c88b2003-02-15 03:01:11 +00004317 i = 0;
4318 while (PyDict_Next(state, &i, &d_key, &d_value)) {
4319 if (PyObject_SetItem(dict, d_key, d_value) < 0)
4320 goto finally;
4321 }
4322 Py_DECREF(dict);
4323 }
4324
4325 /* Also set instance attributes from the slotstate dict (if any). */
4326 if (slotstate != NULL) {
4327 if (! PyDict_Check(slotstate)) {
4328 PyErr_SetString(UnpicklingError, "slot state is not "
4329 "a dictionary");
4330 goto finally;
4331 }
4332 i = 0;
4333 while (PyDict_Next(slotstate, &i, &d_key, &d_value)) {
4334 if (PyObject_SetAttr(inst, d_key, d_value) < 0)
4335 goto finally;
4336 }
4337 }
4338 res = 0;
4339
4340 finally:
4341 Py_DECREF(state);
4342 Py_XDECREF(slotstate);
4343 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004344}
4345
4346
4347static int
Tim Peterscba30e22003-02-01 06:24:36 +00004348load_mark(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004349{
4350 int s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004351
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004352 /* Note that we split the (pickle.py) stack into two stacks, an
4353 object stack and a mark stack. Here we push a mark onto the
4354 mark stack.
4355 */
Guido van Rossum60456fd1997-04-09 17:36:32 +00004356
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004357 if ((self->num_marks + 1) >= self->marks_size) {
4358 s=self->marks_size+20;
4359 if (s <= self->num_marks) s=self->num_marks + 1;
4360 if (self->marks == NULL)
4361 self->marks=(int *)malloc(s * sizeof(int));
4362 else
Tim Peterscba30e22003-02-01 06:24:36 +00004363 self->marks=(int *)realloc(self->marks,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004364 s * sizeof(int));
4365 if (! self->marks) {
4366 PyErr_NoMemory();
4367 return -1;
4368 }
4369 self->marks_size = s;
4370 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00004371
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004372 self->marks[self->num_marks++] = self->stack->length;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004373
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004374 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004375}
4376
Guido van Rossum60456fd1997-04-09 17:36:32 +00004377static int
Tim Peterscba30e22003-02-01 06:24:36 +00004378load_reduce(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004379{
4380 PyObject *callable = 0, *arg_tup = 0, *ob = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004381
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004382 PDATA_POP(self->stack, arg_tup);
4383 if (! arg_tup) return -1;
4384 PDATA_POP(self->stack, callable);
4385 if (callable) {
4386 ob = Instance_New(callable, arg_tup);
4387 Py_DECREF(callable);
4388 }
4389 Py_DECREF(arg_tup);
Guido van Rossum60456fd1997-04-09 17:36:32 +00004390
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004391 if (! ob) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004392
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004393 PDATA_PUSH(self->stack, ob, -1);
4394 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004395}
Tim Peters84e87f32001-03-17 04:50:51 +00004396
Tim Peters4190fb82003-02-02 16:09:05 +00004397/* Just raises an error if we don't know the protocol specified. PROTO
4398 * is the first opcode for protocols >= 2.
4399 */
4400static int
4401load_proto(Unpicklerobject *self)
4402{
4403 int i;
4404 char *protobyte;
4405
4406 i = self->read_func(self, &protobyte, 1);
4407 if (i < 0)
4408 return -1;
4409
4410 i = calc_binint(protobyte, 1);
4411 /* No point checking for < 0, since calc_binint returns an unsigned
4412 * int when chewing on 1 byte.
4413 */
4414 assert(i >= 0);
Tim Peters8587b3c2003-02-13 15:44:41 +00004415 if (i <= HIGHEST_PROTOCOL)
Tim Peters4190fb82003-02-02 16:09:05 +00004416 return 0;
4417
4418 PyErr_Format(PyExc_ValueError, "unsupported pickle protocol: %d", i);
4419 return -1;
4420}
4421
Guido van Rossum60456fd1997-04-09 17:36:32 +00004422static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00004423load(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004424{
4425 PyObject *err = 0, *val = 0;
4426 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004427
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004428 self->num_marks = 0;
4429 if (self->stack->length) Pdata_clear(self->stack, 0);
Guido van Rossum60456fd1997-04-09 17:36:32 +00004430
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004431 while (1) {
Tim Peters0bc93f52003-02-02 18:29:33 +00004432 if (self->read_func(self, &s, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004433 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004434
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004435 switch (s[0]) {
4436 case NONE:
4437 if (load_none(self) < 0)
4438 break;
4439 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004440
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004441 case BININT:
4442 if (load_binint(self) < 0)
4443 break;
4444 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004445
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004446 case BININT1:
4447 if (load_binint1(self) < 0)
4448 break;
4449 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004450
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004451 case BININT2:
4452 if (load_binint2(self) < 0)
4453 break;
4454 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004455
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004456 case INT:
4457 if (load_int(self) < 0)
4458 break;
4459 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004460
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004461 case LONG:
4462 if (load_long(self) < 0)
4463 break;
4464 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004465
Tim Petersee1a53c2003-02-02 02:57:53 +00004466 case LONG1:
4467 if (load_counted_long(self, 1) < 0)
4468 break;
4469 continue;
4470
4471 case LONG4:
4472 if (load_counted_long(self, 4) < 0)
4473 break;
4474 continue;
4475
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004476 case FLOAT:
4477 if (load_float(self) < 0)
4478 break;
4479 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004480
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004481 case BINFLOAT:
4482 if (load_binfloat(self) < 0)
4483 break;
4484 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004485
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004486 case BINSTRING:
4487 if (load_binstring(self) < 0)
4488 break;
4489 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004490
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004491 case SHORT_BINSTRING:
4492 if (load_short_binstring(self) < 0)
4493 break;
4494 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004495
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004496 case STRING:
4497 if (load_string(self) < 0)
4498 break;
4499 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004500
Martin v. Löwis339d0f72001-08-17 18:39:25 +00004501#ifdef Py_USING_UNICODE
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004502 case UNICODE:
4503 if (load_unicode(self) < 0)
4504 break;
4505 continue;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00004506
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004507 case BINUNICODE:
4508 if (load_binunicode(self) < 0)
4509 break;
4510 continue;
Martin v. Löwis339d0f72001-08-17 18:39:25 +00004511#endif
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00004512
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004513 case EMPTY_TUPLE:
Tim Peters1d63c9f2003-02-02 20:29:39 +00004514 if (load_counted_tuple(self, 0) < 0)
4515 break;
4516 continue;
4517
4518 case TUPLE1:
4519 if (load_counted_tuple(self, 1) < 0)
4520 break;
4521 continue;
4522
4523 case TUPLE2:
4524 if (load_counted_tuple(self, 2) < 0)
4525 break;
4526 continue;
4527
4528 case TUPLE3:
4529 if (load_counted_tuple(self, 3) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004530 break;
4531 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004532
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004533 case TUPLE:
4534 if (load_tuple(self) < 0)
4535 break;
4536 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004537
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004538 case EMPTY_LIST:
4539 if (load_empty_list(self) < 0)
4540 break;
4541 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004542
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004543 case LIST:
4544 if (load_list(self) < 0)
4545 break;
4546 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004547
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004548 case EMPTY_DICT:
4549 if (load_empty_dict(self) < 0)
4550 break;
4551 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004552
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004553 case DICT:
4554 if (load_dict(self) < 0)
4555 break;
4556 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004557
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004558 case OBJ:
4559 if (load_obj(self) < 0)
4560 break;
4561 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004562
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004563 case INST:
4564 if (load_inst(self) < 0)
4565 break;
4566 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004567
Tim Peterseab7db32003-02-13 18:24:14 +00004568 case NEWOBJ:
4569 if (load_newobj(self) < 0)
4570 break;
4571 continue;
4572
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004573 case GLOBAL:
4574 if (load_global(self) < 0)
4575 break;
4576 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004577
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004578 case APPEND:
4579 if (load_append(self) < 0)
4580 break;
4581 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004582
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004583 case APPENDS:
4584 if (load_appends(self) < 0)
4585 break;
4586 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00004587
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004588 case BUILD:
4589 if (load_build(self) < 0)
4590 break;
4591 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00004592
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004593 case DUP:
4594 if (load_dup(self) < 0)
4595 break;
4596 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004597
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004598 case BINGET:
4599 if (load_binget(self) < 0)
4600 break;
4601 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004602
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004603 case LONG_BINGET:
4604 if (load_long_binget(self) < 0)
4605 break;
4606 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00004607
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004608 case GET:
4609 if (load_get(self) < 0)
4610 break;
4611 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004612
Tim Peters2d629652003-02-04 05:06:17 +00004613 case EXT1:
4614 if (load_extension(self, 1) < 0)
4615 break;
4616 continue;
4617
4618 case EXT2:
4619 if (load_extension(self, 2) < 0)
4620 break;
4621 continue;
4622
4623 case EXT4:
4624 if (load_extension(self, 4) < 0)
4625 break;
4626 continue;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004627 case MARK:
4628 if (load_mark(self) < 0)
4629 break;
4630 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004631
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004632 case BINPUT:
4633 if (load_binput(self) < 0)
4634 break;
4635 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004636
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004637 case LONG_BINPUT:
4638 if (load_long_binput(self) < 0)
4639 break;
4640 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00004641
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004642 case PUT:
4643 if (load_put(self) < 0)
4644 break;
4645 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004646
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004647 case POP:
4648 if (load_pop(self) < 0)
4649 break;
4650 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004651
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004652 case POP_MARK:
4653 if (load_pop_mark(self) < 0)
4654 break;
4655 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004656
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004657 case SETITEM:
4658 if (load_setitem(self) < 0)
4659 break;
4660 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004661
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004662 case SETITEMS:
4663 if (load_setitems(self) < 0)
4664 break;
4665 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004666
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004667 case STOP:
4668 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004669
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004670 case PERSID:
4671 if (load_persid(self) < 0)
4672 break;
4673 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004674
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004675 case BINPERSID:
4676 if (load_binpersid(self) < 0)
4677 break;
4678 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004679
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004680 case REDUCE:
4681 if (load_reduce(self) < 0)
4682 break;
4683 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004684
Tim Peters4190fb82003-02-02 16:09:05 +00004685 case PROTO:
4686 if (load_proto(self) < 0)
4687 break;
4688 continue;
4689
Tim Peters3c67d792003-02-02 17:59:11 +00004690 case NEWTRUE:
4691 if (load_bool(self, Py_True) < 0)
4692 break;
4693 continue;
4694
4695 case NEWFALSE:
4696 if (load_bool(self, Py_False) < 0)
4697 break;
4698 continue;
4699
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004700 case '\0':
4701 /* end of file */
4702 PyErr_SetNone(PyExc_EOFError);
4703 break;
Tim Peterscba30e22003-02-01 06:24:36 +00004704
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004705 default:
Tim Peterscba30e22003-02-01 06:24:36 +00004706 cPickle_ErrFormat(UnpicklingError,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004707 "invalid load key, '%s'.",
4708 "c", s[0]);
4709 return NULL;
4710 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00004711
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004712 break;
4713 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00004714
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004715 if ((err = PyErr_Occurred())) {
4716 if (err == PyExc_EOFError) {
4717 PyErr_SetNone(PyExc_EOFError);
4718 }
4719 return NULL;
4720 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00004721
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004722 PDATA_POP(self->stack, val);
4723 return val;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004724}
Tim Peters84e87f32001-03-17 04:50:51 +00004725
Guido van Rossum60456fd1997-04-09 17:36:32 +00004726
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004727/* No-load functions to support noload, which is used to
4728 find persistent references. */
4729
4730static int
Tim Peterscba30e22003-02-01 06:24:36 +00004731noload_obj(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004732{
4733 int i;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004734
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004735 if ((i = marker(self)) < 0) return -1;
4736 return Pdata_clear(self->stack, i+1);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004737}
4738
4739
4740static int
Tim Peterscba30e22003-02-01 06:24:36 +00004741noload_inst(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004742{
4743 int i;
4744 char *s;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004745
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004746 if ((i = marker(self)) < 0) return -1;
4747 Pdata_clear(self->stack, i);
Tim Peters0bc93f52003-02-02 18:29:33 +00004748 if (self->readline_func(self, &s) < 0) return -1;
4749 if (self->readline_func(self, &s) < 0) return -1;
Tim Peterseab7db32003-02-13 18:24:14 +00004750 PDATA_APPEND(self->stack, Py_None, -1);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004751 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004752}
4753
4754static int
Tim Peterseab7db32003-02-13 18:24:14 +00004755noload_newobj(Unpicklerobject *self)
4756{
4757 PyObject *obj;
4758
4759 PDATA_POP(self->stack, obj); /* pop argtuple */
4760 if (obj == NULL) return -1;
4761 Py_DECREF(obj);
4762
4763 PDATA_POP(self->stack, obj); /* pop cls */
4764 if (obj == NULL) return -1;
4765 Py_DECREF(obj);
4766
4767 PDATA_APPEND(self->stack, Py_None, -1);
4768 return 0;
4769}
4770
4771static int
Tim Peterscba30e22003-02-01 06:24:36 +00004772noload_global(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004773{
4774 char *s;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004775
Tim Peters0bc93f52003-02-02 18:29:33 +00004776 if (self->readline_func(self, &s) < 0) return -1;
4777 if (self->readline_func(self, &s) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004778 PDATA_APPEND(self->stack, Py_None,-1);
4779 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004780}
4781
4782static int
Tim Peterscba30e22003-02-01 06:24:36 +00004783noload_reduce(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004784{
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004785
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004786 if (self->stack->length < 2) return stackUnderflow();
4787 Pdata_clear(self->stack, self->stack->length-2);
4788 PDATA_APPEND(self->stack, Py_None,-1);
4789 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004790}
4791
4792static int
4793noload_build(Unpicklerobject *self) {
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004794
Guido van Rossum053b8df1998-11-25 16:18:00 +00004795 if (self->stack->length < 1) return stackUnderflow();
4796 Pdata_clear(self->stack, self->stack->length-1);
Guido van Rossum50f385c1998-12-04 18:48:44 +00004797 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004798}
4799
Tim Peters2d629652003-02-04 05:06:17 +00004800static int
4801noload_extension(Unpicklerobject *self, int nbytes)
4802{
4803 char *codebytes;
4804
4805 assert(nbytes == 1 || nbytes == 2 || nbytes == 4);
4806 if (self->read_func(self, &codebytes, nbytes) < 0) return -1;
4807 PDATA_APPEND(self->stack, Py_None, -1);
4808 return 0;
4809}
4810
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004811
4812static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00004813noload(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004814{
4815 PyObject *err = 0, *val = 0;
4816 char *s;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004817
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004818 self->num_marks = 0;
4819 Pdata_clear(self->stack, 0);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004820
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004821 while (1) {
Tim Peters0bc93f52003-02-02 18:29:33 +00004822 if (self->read_func(self, &s, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004823 break;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004824
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004825 switch (s[0]) {
4826 case NONE:
4827 if (load_none(self) < 0)
4828 break;
4829 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004830
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004831 case BININT:
4832 if (load_binint(self) < 0)
4833 break;
4834 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004835
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004836 case BININT1:
4837 if (load_binint1(self) < 0)
4838 break;
4839 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004840
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004841 case BININT2:
4842 if (load_binint2(self) < 0)
4843 break;
4844 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004845
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004846 case INT:
4847 if (load_int(self) < 0)
4848 break;
4849 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004850
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004851 case LONG:
4852 if (load_long(self) < 0)
4853 break;
4854 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004855
Tim Peters4190fb82003-02-02 16:09:05 +00004856 case LONG1:
4857 if (load_counted_long(self, 1) < 0)
4858 break;
4859 continue;
4860
4861 case LONG4:
4862 if (load_counted_long(self, 4) < 0)
4863 break;
4864 continue;
4865
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004866 case FLOAT:
4867 if (load_float(self) < 0)
4868 break;
4869 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004870
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004871 case BINFLOAT:
4872 if (load_binfloat(self) < 0)
4873 break;
4874 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004875
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004876 case BINSTRING:
4877 if (load_binstring(self) < 0)
4878 break;
4879 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004880
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004881 case SHORT_BINSTRING:
4882 if (load_short_binstring(self) < 0)
4883 break;
4884 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004885
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004886 case STRING:
4887 if (load_string(self) < 0)
4888 break;
4889 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004890
Martin v. Löwis339d0f72001-08-17 18:39:25 +00004891#ifdef Py_USING_UNICODE
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004892 case UNICODE:
4893 if (load_unicode(self) < 0)
4894 break;
4895 continue;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00004896
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004897 case BINUNICODE:
4898 if (load_binunicode(self) < 0)
4899 break;
4900 continue;
Martin v. Löwis339d0f72001-08-17 18:39:25 +00004901#endif
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00004902
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004903 case EMPTY_TUPLE:
Tim Peters1d63c9f2003-02-02 20:29:39 +00004904 if (load_counted_tuple(self, 0) < 0)
4905 break;
4906 continue;
4907
4908 case TUPLE1:
4909 if (load_counted_tuple(self, 1) < 0)
4910 break;
4911 continue;
4912
4913 case TUPLE2:
4914 if (load_counted_tuple(self, 2) < 0)
4915 break;
4916 continue;
4917
4918 case TUPLE3:
4919 if (load_counted_tuple(self, 3) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004920 break;
4921 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004922
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004923 case TUPLE:
4924 if (load_tuple(self) < 0)
4925 break;
4926 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004927
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004928 case EMPTY_LIST:
4929 if (load_empty_list(self) < 0)
4930 break;
4931 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004932
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004933 case LIST:
4934 if (load_list(self) < 0)
4935 break;
4936 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004937
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004938 case EMPTY_DICT:
4939 if (load_empty_dict(self) < 0)
4940 break;
4941 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004942
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004943 case DICT:
4944 if (load_dict(self) < 0)
4945 break;
4946 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004947
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004948 case OBJ:
4949 if (noload_obj(self) < 0)
4950 break;
4951 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004952
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004953 case INST:
4954 if (noload_inst(self) < 0)
4955 break;
4956 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004957
Tim Peterseab7db32003-02-13 18:24:14 +00004958 case NEWOBJ:
4959 if (noload_newobj(self) < 0)
4960 break;
4961 continue;
4962
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004963 case GLOBAL:
4964 if (noload_global(self) < 0)
4965 break;
4966 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004967
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004968 case APPEND:
4969 if (load_append(self) < 0)
4970 break;
4971 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004972
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004973 case APPENDS:
4974 if (load_appends(self) < 0)
4975 break;
4976 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00004977
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004978 case BUILD:
4979 if (noload_build(self) < 0)
4980 break;
4981 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00004982
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004983 case DUP:
4984 if (load_dup(self) < 0)
4985 break;
4986 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004987
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004988 case BINGET:
4989 if (load_binget(self) < 0)
4990 break;
4991 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004992
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004993 case LONG_BINGET:
4994 if (load_long_binget(self) < 0)
4995 break;
4996 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00004997
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004998 case GET:
4999 if (load_get(self) < 0)
5000 break;
5001 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005002
Tim Peters2d629652003-02-04 05:06:17 +00005003 case EXT1:
5004 if (noload_extension(self, 1) < 0)
5005 break;
5006 continue;
5007
5008 case EXT2:
5009 if (noload_extension(self, 2) < 0)
5010 break;
5011 continue;
5012
5013 case EXT4:
5014 if (noload_extension(self, 4) < 0)
5015 break;
5016 continue;
5017
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005018 case MARK:
5019 if (load_mark(self) < 0)
5020 break;
5021 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005022
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005023 case BINPUT:
5024 if (load_binput(self) < 0)
5025 break;
5026 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005027
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005028 case LONG_BINPUT:
5029 if (load_long_binput(self) < 0)
5030 break;
5031 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00005032
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005033 case PUT:
5034 if (load_put(self) < 0)
5035 break;
5036 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005037
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005038 case POP:
5039 if (load_pop(self) < 0)
5040 break;
5041 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005042
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005043 case POP_MARK:
5044 if (load_pop_mark(self) < 0)
5045 break;
5046 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005047
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005048 case SETITEM:
5049 if (load_setitem(self) < 0)
5050 break;
5051 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005052
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005053 case SETITEMS:
5054 if (load_setitems(self) < 0)
5055 break;
5056 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005057
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005058 case STOP:
5059 break;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005060
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005061 case PERSID:
5062 if (load_persid(self) < 0)
5063 break;
5064 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005065
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005066 case BINPERSID:
5067 if (load_binpersid(self) < 0)
5068 break;
5069 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005070
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005071 case REDUCE:
5072 if (noload_reduce(self) < 0)
5073 break;
5074 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005075
Tim Peters4190fb82003-02-02 16:09:05 +00005076 case PROTO:
5077 if (load_proto(self) < 0)
5078 break;
5079 continue;
5080
Tim Peters3c67d792003-02-02 17:59:11 +00005081 case NEWTRUE:
5082 if (load_bool(self, Py_True) < 0)
5083 break;
5084 continue;
5085
5086 case NEWFALSE:
5087 if (load_bool(self, Py_False) < 0)
5088 break;
5089 continue;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005090 default:
Tim Peterscba30e22003-02-01 06:24:36 +00005091 cPickle_ErrFormat(UnpicklingError,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005092 "invalid load key, '%s'.",
5093 "c", s[0]);
5094 return NULL;
5095 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005096
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005097 break;
5098 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005099
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005100 if ((err = PyErr_Occurred())) {
5101 if (err == PyExc_EOFError) {
5102 PyErr_SetNone(PyExc_EOFError);
5103 }
5104 return NULL;
5105 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005106
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005107 PDATA_POP(self->stack, val);
5108 return val;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005109}
Tim Peters84e87f32001-03-17 04:50:51 +00005110
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005111
Guido van Rossum60456fd1997-04-09 17:36:32 +00005112static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00005113Unpickler_load(Unpicklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005114{
Tim Peterscba30e22003-02-01 06:24:36 +00005115 if (!( PyArg_ParseTuple(args, ":load")))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005116 return NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005117
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005118 return load(self);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005119}
5120
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005121static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00005122Unpickler_noload(Unpicklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005123{
Tim Peterscba30e22003-02-01 06:24:36 +00005124 if (!( PyArg_ParseTuple(args, ":noload")))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005125 return NULL;
Guido van Rossumfdde96c1997-12-04 01:13:01 +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[] = {
Neal Norwitzb0493252002-03-31 14:44:22 +00005132 {"load", (PyCFunction)Unpickler_load, METH_VARARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00005133 PyDoc_STR("load() -- Load a pickle")
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005134 },
Neal Norwitzb0493252002-03-31 14:44:22 +00005135 {"noload", (PyCFunction)Unpickler_noload, METH_VARARGS,
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
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005172 Py_INCREF(f);
5173 self->file = f;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005174
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005175 /* Set read, readline based on type of f */
5176 if (PyFile_Check(f)) {
5177 self->fp = PyFile_AsFile(f);
5178 if (self->fp == NULL) {
Tim Peterscba30e22003-02-01 06:24:36 +00005179 PyErr_SetString(PyExc_ValueError,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005180 "I/O operation on closed file");
5181 goto err;
5182 }
5183 self->read_func = read_file;
5184 self->readline_func = readline_file;
Guido van Rossumc91fcaa1999-03-29 20:00:14 +00005185 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005186 else if (PycStringIO_InputCheck(f)) {
5187 self->fp = NULL;
5188 self->read_func = read_cStringIO;
5189 self->readline_func = readline_cStringIO;
5190 }
5191 else {
Guido van Rossum60456fd1997-04-09 17:36:32 +00005192
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005193 self->fp = NULL;
5194 self->read_func = read_other;
5195 self->readline_func = readline_other;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005196
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005197 if (!( (self->readline = PyObject_GetAttr(f, readline_str)) &&
5198 (self->read = PyObject_GetAttr(f, read_str)))) {
5199 PyErr_Clear();
Tim Peterscba30e22003-02-01 06:24:36 +00005200 PyErr_SetString( PyExc_TypeError,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005201 "argument must have 'read' and "
5202 "'readline' attributes" );
5203 goto err;
5204 }
5205 }
Jeremy Hylton7b5ce7f2003-04-09 21:25:30 +00005206 PyObject_GC_Track(self);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005207
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005208 return self;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005209
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005210 err:
5211 Py_DECREF((PyObject *)self);
5212 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005213}
5214
5215
5216static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00005217get_Unpickler(PyObject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005218{
5219 PyObject *file;
Tim Peters84e87f32001-03-17 04:50:51 +00005220
Tim Peterscba30e22003-02-01 06:24:36 +00005221 if (!( PyArg_ParseTuple(args, "O:Unpickler", &file)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005222 return NULL;
5223 return (PyObject *)newUnpicklerobject(file);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005224}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005225
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005226
Guido van Rossum60456fd1997-04-09 17:36:32 +00005227static void
Tim Peterscba30e22003-02-01 06:24:36 +00005228Unpickler_dealloc(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005229{
Jeremy Hylton7b5ce7f2003-04-09 21:25:30 +00005230 PyObject_GC_UnTrack((PyObject *)self);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005231 Py_XDECREF(self->readline);
5232 Py_XDECREF(self->read);
5233 Py_XDECREF(self->file);
5234 Py_XDECREF(self->memo);
5235 Py_XDECREF(self->stack);
5236 Py_XDECREF(self->pers_func);
5237 Py_XDECREF(self->arg);
5238 Py_XDECREF(self->last_string);
Jeremy Hyltonfff093f2003-07-11 19:42:49 +00005239 Py_XDECREF(self->find_class);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005240
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005241 if (self->marks) {
5242 free(self->marks);
5243 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005244
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005245 if (self->buf_size) {
5246 free(self->buf);
5247 }
Tim Peters84e87f32001-03-17 04:50:51 +00005248
Tim Peters3cfe7542003-05-21 21:29:48 +00005249 self->ob_type->tp_free((PyObject *)self);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005250}
5251
Jeremy Hylton7b5ce7f2003-04-09 21:25:30 +00005252static int
5253Unpickler_traverse(Unpicklerobject *self, visitproc visit, void *arg)
5254{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005255 Py_VISIT(self->readline);
5256 Py_VISIT(self->read);
5257 Py_VISIT(self->file);
5258 Py_VISIT(self->memo);
5259 Py_VISIT(self->stack);
5260 Py_VISIT(self->pers_func);
5261 Py_VISIT(self->arg);
5262 Py_VISIT(self->last_string);
5263 Py_VISIT(self->find_class);
Jeremy Hylton7b5ce7f2003-04-09 21:25:30 +00005264 return 0;
5265}
5266
5267static int
5268Unpickler_clear(Unpicklerobject *self)
5269{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005270 Py_CLEAR(self->readline);
5271 Py_CLEAR(self->read);
5272 Py_CLEAR(self->file);
5273 Py_CLEAR(self->memo);
5274 Py_CLEAR(self->stack);
5275 Py_CLEAR(self->pers_func);
5276 Py_CLEAR(self->arg);
5277 Py_CLEAR(self->last_string);
5278 Py_CLEAR(self->find_class);
Jeremy Hylton7b5ce7f2003-04-09 21:25:30 +00005279 return 0;
5280}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005281
5282static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00005283Unpickler_getattr(Unpicklerobject *self, char *name)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005284{
5285 if (!strcmp(name, "persistent_load")) {
5286 if (!self->pers_func) {
5287 PyErr_SetString(PyExc_AttributeError, name);
5288 return NULL;
5289 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005290
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005291 Py_INCREF(self->pers_func);
5292 return self->pers_func;
5293 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005294
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005295 if (!strcmp(name, "find_global")) {
5296 if (!self->find_class) {
5297 PyErr_SetString(PyExc_AttributeError, name);
5298 return NULL;
5299 }
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00005300
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005301 Py_INCREF(self->find_class);
5302 return self->find_class;
5303 }
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00005304
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005305 if (!strcmp(name, "memo")) {
5306 if (!self->memo) {
5307 PyErr_SetString(PyExc_AttributeError, name);
5308 return NULL;
5309 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005310
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005311 Py_INCREF(self->memo);
5312 return self->memo;
5313 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005314
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005315 if (!strcmp(name, "UnpicklingError")) {
5316 Py_INCREF(UnpicklingError);
5317 return UnpicklingError;
5318 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005319
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005320 return Py_FindMethod(Unpickler_methods, (PyObject *)self, name);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005321}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005322
Guido van Rossum60456fd1997-04-09 17:36:32 +00005323
5324static int
Tim Peterscba30e22003-02-01 06:24:36 +00005325Unpickler_setattr(Unpicklerobject *self, char *name, PyObject *value)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005326{
Jeremy Hyltonce616e41998-08-13 23:13:52 +00005327
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005328 if (!strcmp(name, "persistent_load")) {
5329 Py_XDECREF(self->pers_func);
5330 self->pers_func = value;
5331 Py_XINCREF(value);
5332 return 0;
5333 }
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00005334
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005335 if (!strcmp(name, "find_global")) {
5336 Py_XDECREF(self->find_class);
5337 self->find_class = value;
5338 Py_XINCREF(value);
5339 return 0;
5340 }
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00005341
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005342 if (! value) {
5343 PyErr_SetString(PyExc_TypeError,
5344 "attribute deletion is not supported");
5345 return -1;
5346 }
Jeremy Hyltonce616e41998-08-13 23:13:52 +00005347
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005348 if (strcmp(name, "memo") == 0) {
5349 if (!PyDict_Check(value)) {
Tim Peterscba30e22003-02-01 06:24:36 +00005350 PyErr_SetString(PyExc_TypeError,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005351 "memo must be a dictionary");
5352 return -1;
5353 }
5354 Py_XDECREF(self->memo);
5355 self->memo = value;
5356 Py_INCREF(value);
5357 return 0;
5358 }
Jeremy Hyltonce616e41998-08-13 23:13:52 +00005359
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005360 PyErr_SetString(PyExc_AttributeError, name);
5361 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005362}
5363
Tim Peters5bd2a792003-02-01 16:45:06 +00005364/* ---------------------------------------------------------------------------
5365 * Module-level functions.
5366 */
Guido van Rossum60456fd1997-04-09 17:36:32 +00005367
Martin v. Löwis544f1192004-07-27 05:22:33 +00005368/* dump(obj, file, protocol=0). */
Guido van Rossum60456fd1997-04-09 17:36:32 +00005369static PyObject *
Martin v. Löwis544f1192004-07-27 05:22:33 +00005370cpm_dump(PyObject *self, PyObject *args, PyObject *kwds)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005371{
Martin v. Löwis15e62742006-02-27 16:46:16 +00005372 static char *kwlist[] = {"obj", "file", "protocol", NULL};
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005373 PyObject *ob, *file, *res = NULL;
5374 Picklerobject *pickler = 0;
Tim Peters5bd2a792003-02-01 16:45:06 +00005375 int proto = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005376
Martin v. Löwis544f1192004-07-27 05:22:33 +00005377 if (!( PyArg_ParseTupleAndKeywords(args, kwds, "OO|i", kwlist,
5378 &ob, &file, &proto)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005379 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005380
Tim Peters5bd2a792003-02-01 16:45:06 +00005381 if (!( pickler = newPicklerobject(file, proto)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005382 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005383
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005384 if (dump(pickler, ob) < 0)
5385 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005386
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005387 Py_INCREF(Py_None);
5388 res = Py_None;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005389
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005390 finally:
5391 Py_XDECREF(pickler);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005392
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005393 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005394}
5395
5396
Martin v. Löwis544f1192004-07-27 05:22:33 +00005397/* dumps(obj, protocol=0). */
Guido van Rossum60456fd1997-04-09 17:36:32 +00005398static PyObject *
Martin v. Löwis544f1192004-07-27 05:22:33 +00005399cpm_dumps(PyObject *self, PyObject *args, PyObject *kwds)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005400{
Martin v. Löwis15e62742006-02-27 16:46:16 +00005401 static char *kwlist[] = {"obj", "protocol", NULL};
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005402 PyObject *ob, *file = 0, *res = NULL;
5403 Picklerobject *pickler = 0;
Tim Peters5bd2a792003-02-01 16:45:06 +00005404 int proto = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005405
Martin v. Löwis544f1192004-07-27 05:22:33 +00005406 if (!( PyArg_ParseTupleAndKeywords(args, kwds, "O|i:dumps", kwlist,
5407 &ob, &proto)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005408 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005409
Tim Peterscba30e22003-02-01 06:24:36 +00005410 if (!( file = PycStringIO->NewOutput(128)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005411 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005412
Tim Peters5bd2a792003-02-01 16:45:06 +00005413 if (!( pickler = newPicklerobject(file, proto)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005414 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005415
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005416 if (dump(pickler, ob) < 0)
5417 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005418
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005419 res = PycStringIO->cgetvalue(file);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005420
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005421 finally:
5422 Py_XDECREF(pickler);
5423 Py_XDECREF(file);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005424
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005425 return res;
Tim Peters84e87f32001-03-17 04:50:51 +00005426}
5427
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005428
Tim Peters5bd2a792003-02-01 16:45:06 +00005429/* load(fileobj). */
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005430static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00005431cpm_load(PyObject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005432{
5433 Unpicklerobject *unpickler = 0;
5434 PyObject *ob, *res = NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005435
Tim Peterscba30e22003-02-01 06:24:36 +00005436 if (!( PyArg_ParseTuple(args, "O:load", &ob)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005437 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005438
Tim Peterscba30e22003-02-01 06:24:36 +00005439 if (!( unpickler = newUnpicklerobject(ob)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005440 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005441
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005442 res = load(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005443
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005444 finally:
5445 Py_XDECREF(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005446
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005447 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005448}
5449
5450
Tim Peters5bd2a792003-02-01 16:45:06 +00005451/* loads(string) */
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005452static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00005453cpm_loads(PyObject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005454{
5455 PyObject *ob, *file = 0, *res = NULL;
5456 Unpicklerobject *unpickler = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005457
Tim Peterscba30e22003-02-01 06:24:36 +00005458 if (!( PyArg_ParseTuple(args, "S:loads", &ob)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005459 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005460
Tim Peterscba30e22003-02-01 06:24:36 +00005461 if (!( file = PycStringIO->NewInput(ob)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005462 goto finally;
Tim Peters84e87f32001-03-17 04:50:51 +00005463
Tim Peterscba30e22003-02-01 06:24:36 +00005464 if (!( unpickler = newUnpicklerobject(file)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005465 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005466
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005467 res = load(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005468
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005469 finally:
5470 Py_XDECREF(file);
5471 Py_XDECREF(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005472
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005473 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005474}
5475
5476
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005477PyDoc_STRVAR(Unpicklertype__doc__,
5478"Objects that know how to unpickle");
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005479
Guido van Rossum9716aaa1997-12-08 15:15:16 +00005480static PyTypeObject Unpicklertype = {
5481 PyObject_HEAD_INIT(NULL)
Jeremy Hylton7b5ce7f2003-04-09 21:25:30 +00005482 0, /*ob_size*/
5483 "cPickle.Unpickler", /*tp_name*/
5484 sizeof(Unpicklerobject), /*tp_basicsize*/
5485 0,
5486 (destructor)Unpickler_dealloc, /* tp_dealloc */
5487 0, /* tp_print */
5488 (getattrfunc)Unpickler_getattr, /* tp_getattr */
5489 (setattrfunc)Unpickler_setattr, /* tp_setattr */
5490 0, /* tp_compare */
5491 0, /* tp_repr */
5492 0, /* tp_as_number */
5493 0, /* tp_as_sequence */
5494 0, /* tp_as_mapping */
5495 0, /* tp_hash */
5496 0, /* tp_call */
5497 0, /* tp_str */
5498 0, /* tp_getattro */
5499 0, /* tp_setattro */
5500 0, /* tp_as_buffer */
5501 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
5502 Unpicklertype__doc__, /* tp_doc */
5503 (traverseproc)Unpickler_traverse, /* tp_traverse */
5504 (inquiry)Unpickler_clear, /* tp_clear */
Guido van Rossum9716aaa1997-12-08 15:15:16 +00005505};
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005506
Guido van Rossum60456fd1997-04-09 17:36:32 +00005507static struct PyMethodDef cPickle_methods[] = {
Martin v. Löwis544f1192004-07-27 05:22:33 +00005508 {"dump", (PyCFunction)cpm_dump, METH_VARARGS | METH_KEYWORDS,
5509 PyDoc_STR("dump(obj, file, protocol=0) -- "
Tim Peters5bd2a792003-02-01 16:45:06 +00005510 "Write an object in pickle format to the given file.\n"
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005511 "\n"
Tim Peters5bd2a792003-02-01 16:45:06 +00005512 "See the Pickler docstring for the meaning of optional argument proto.")
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005513 },
Tim Peters5bd2a792003-02-01 16:45:06 +00005514
Martin v. Löwis544f1192004-07-27 05:22:33 +00005515 {"dumps", (PyCFunction)cpm_dumps, METH_VARARGS | METH_KEYWORDS,
5516 PyDoc_STR("dumps(obj, protocol=0) -- "
Tim Peters5bd2a792003-02-01 16:45:06 +00005517 "Return a string containing an object in pickle format.\n"
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005518 "\n"
Tim Peters5bd2a792003-02-01 16:45:06 +00005519 "See the Pickler docstring for the meaning of optional argument proto.")
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005520 },
Tim Peters5bd2a792003-02-01 16:45:06 +00005521
Neal Norwitzb0493252002-03-31 14:44:22 +00005522 {"load", (PyCFunction)cpm_load, METH_VARARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00005523 PyDoc_STR("load(file) -- Load a pickle from the given file")},
Tim Peters5bd2a792003-02-01 16:45:06 +00005524
Neal Norwitzb0493252002-03-31 14:44:22 +00005525 {"loads", (PyCFunction)cpm_loads, METH_VARARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00005526 PyDoc_STR("loads(string) -- Load a pickle from the given string")},
Tim Peters5bd2a792003-02-01 16:45:06 +00005527
Martin v. Löwis544f1192004-07-27 05:22:33 +00005528 {"Pickler", (PyCFunction)get_Pickler, METH_VARARGS | METH_KEYWORDS,
5529 PyDoc_STR("Pickler(file, protocol=0) -- Create a pickler.\n"
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005530 "\n"
Tim Peters5bd2a792003-02-01 16:45:06 +00005531 "This takes a file-like object for writing a pickle data stream.\n"
5532 "The optional proto argument tells the pickler to use the given\n"
5533 "protocol; supported protocols are 0, 1, 2. The default\n"
5534 "protocol is 0, to be backwards compatible. (Protocol 0 is the\n"
5535 "only protocol that can be written to a file opened in text\n"
5536 "mode and read back successfully. When using a protocol higher\n"
5537 "than 0, make sure the file is opened in binary mode, both when\n"
5538 "pickling and unpickling.)\n"
5539 "\n"
5540 "Protocol 1 is more efficient than protocol 0; protocol 2 is\n"
5541 "more efficient than protocol 1.\n"
5542 "\n"
5543 "Specifying a negative protocol version selects the highest\n"
5544 "protocol version supported. The higher the protocol used, the\n"
5545 "more recent the version of Python needed to read the pickle\n"
5546 "produced.\n"
5547 "\n"
5548 "The file parameter must have a write() method that accepts a single\n"
5549 "string argument. It can thus be an open file object, a StringIO\n"
5550 "object, or any other custom object that meets this interface.\n")
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005551 },
Tim Peters5bd2a792003-02-01 16:45:06 +00005552
Neal Norwitzb0493252002-03-31 14:44:22 +00005553 {"Unpickler", (PyCFunction)get_Unpickler, METH_VARARGS,
Tim Peters5bd2a792003-02-01 16:45:06 +00005554 PyDoc_STR("Unpickler(file) -- Create an unpickler.")},
5555
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005556 { NULL, NULL }
5557};
5558
Guido van Rossum60456fd1997-04-09 17:36:32 +00005559static int
Tim Peterscba30e22003-02-01 06:24:36 +00005560init_stuff(PyObject *module_dict)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005561{
5562 PyObject *copy_reg, *t, *r;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005563
Martin v. Löwis43c9d8a2002-04-01 12:34:33 +00005564#define INIT_STR(S) if (!( S ## _str=PyString_InternFromString(#S))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005565
Tim Peters3cfe7542003-05-21 21:29:48 +00005566 if (PyType_Ready(&Unpicklertype) < 0)
5567 return -1;
5568 if (PyType_Ready(&Picklertype) < 0)
5569 return -1;
5570
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005571 INIT_STR(__class__);
5572 INIT_STR(__getinitargs__);
5573 INIT_STR(__dict__);
5574 INIT_STR(__getstate__);
5575 INIT_STR(__setstate__);
5576 INIT_STR(__name__);
5577 INIT_STR(__main__);
5578 INIT_STR(__reduce__);
Guido van Rossumb289b872003-02-19 01:45:13 +00005579 INIT_STR(__reduce_ex__);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005580 INIT_STR(write);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005581 INIT_STR(append);
5582 INIT_STR(read);
5583 INIT_STR(readline);
5584 INIT_STR(copy_reg);
5585 INIT_STR(dispatch_table);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005586
Tim Peterscba30e22003-02-01 06:24:36 +00005587 if (!( copy_reg = PyImport_ImportModule("copy_reg")))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005588 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005589
Tim Peters1f1b2d22003-02-01 02:16:37 +00005590 /* This is special because we want to use a different
5591 one in restricted mode. */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005592 dispatch_table = PyObject_GetAttr(copy_reg, dispatch_table_str);
Tim Peters5b7da392003-02-04 00:21:07 +00005593 if (!dispatch_table) return -1;
5594
5595 extension_registry = PyObject_GetAttrString(copy_reg,
Guido van Rossumd4b920c2003-02-04 01:54:49 +00005596 "_extension_registry");
Tim Peters5b7da392003-02-04 00:21:07 +00005597 if (!extension_registry) return -1;
5598
5599 inverted_registry = PyObject_GetAttrString(copy_reg,
Guido van Rossumd4b920c2003-02-04 01:54:49 +00005600 "_inverted_registry");
Tim Peters5b7da392003-02-04 00:21:07 +00005601 if (!inverted_registry) return -1;
5602
5603 extension_cache = PyObject_GetAttrString(copy_reg,
Guido van Rossumd4b920c2003-02-04 01:54:49 +00005604 "_extension_cache");
Tim Peters5b7da392003-02-04 00:21:07 +00005605 if (!extension_cache) return -1;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005606
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005607 Py_DECREF(copy_reg);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005608
Tim Peters731098b2003-02-04 20:56:09 +00005609 if (!(empty_tuple = PyTuple_New(0)))
5610 return -1;
5611
5612 two_tuple = PyTuple_New(2);
5613 if (two_tuple == NULL)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005614 return -1;
Tim Peters3e667d52003-02-04 21:47:44 +00005615 /* We use this temp container with no regard to refcounts, or to
5616 * keeping containees alive. Exempt from GC, because we don't
5617 * want anything looking at two_tuple() by magic.
5618 */
5619 PyObject_GC_UnTrack(two_tuple);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005620
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005621 /* Ugh */
5622 if (!( t=PyImport_ImportModule("__builtin__"))) return -1;
5623 if (PyDict_SetItemString(module_dict, "__builtins__", t) < 0)
5624 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005625
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005626 if (!( t=PyDict_New())) return -1;
5627 if (!( r=PyRun_String(
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005628 "def __str__(self):\n"
5629 " return self.args and ('%s' % self.args[0]) or '(what)'\n",
5630 Py_file_input,
5631 module_dict, t) )) return -1;
5632 Py_DECREF(r);
Guido van Rossumc03158b1999-06-09 15:23:31 +00005633
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005634 PickleError = PyErr_NewException("cPickle.PickleError", NULL, t);
Tim Peterscba30e22003-02-01 06:24:36 +00005635 if (!PickleError)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005636 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00005637
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005638 Py_DECREF(t);
Guido van Rossumc03158b1999-06-09 15:23:31 +00005639
Tim Peterscba30e22003-02-01 06:24:36 +00005640 PicklingError = PyErr_NewException("cPickle.PicklingError",
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005641 PickleError, NULL);
Tim Peterscba30e22003-02-01 06:24:36 +00005642 if (!PicklingError)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005643 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00005644
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005645 if (!( t=PyDict_New())) return -1;
5646 if (!( r=PyRun_String(
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005647 "def __str__(self):\n"
5648 " a=self.args\n"
5649 " a=a and type(a[0]) or '(what)'\n"
5650 " return 'Cannot pickle %s objects' % a\n"
5651 , Py_file_input,
5652 module_dict, t) )) return -1;
5653 Py_DECREF(r);
Tim Peters84e87f32001-03-17 04:50:51 +00005654
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005655 if (!( UnpickleableError = PyErr_NewException(
Tim Peterscba30e22003-02-01 06:24:36 +00005656 "cPickle.UnpickleableError", PicklingError, t)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005657 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00005658
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005659 Py_DECREF(t);
Guido van Rossumc03158b1999-06-09 15:23:31 +00005660
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005661 if (!( UnpicklingError = PyErr_NewException("cPickle.UnpicklingError",
Tim Peterscba30e22003-02-01 06:24:36 +00005662 PickleError, NULL)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005663 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00005664
Martin v. Löwis658009a2002-09-16 17:26:24 +00005665 if (!( BadPickleGet = PyErr_NewException("cPickle.BadPickleGet",
5666 UnpicklingError, NULL)))
5667 return -1;
Tim Peterscba30e22003-02-01 06:24:36 +00005668
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005669 if (PyDict_SetItemString(module_dict, "PickleError",
5670 PickleError) < 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, "PicklingError",
5674 PicklingError) < 0)
5675 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00005676
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005677 if (PyDict_SetItemString(module_dict, "UnpicklingError",
5678 UnpicklingError) < 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, "UnpickleableError",
5682 UnpickleableError) < 0)
5683 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005684
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005685 if (PyDict_SetItemString(module_dict, "BadPickleGet",
5686 BadPickleGet) < 0)
5687 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00005688
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005689 PycString_IMPORT;
Guido van Rossum053b8df1998-11-25 16:18:00 +00005690
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005691 return 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005692}
5693
Mark Hammondfe51c6d2002-08-02 02:27:13 +00005694#ifndef PyMODINIT_FUNC /* declarations for DLL import/export */
5695#define PyMODINIT_FUNC void
Guido van Rossumf9ffb031999-02-04 14:54:04 +00005696#endif
Mark Hammondfe51c6d2002-08-02 02:27:13 +00005697PyMODINIT_FUNC
Tim Peterscba30e22003-02-01 06:24:36 +00005698initcPickle(void)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005699{
5700 PyObject *m, *d, *di, *v, *k;
Martin v. Löwis18e16552006-02-15 17:27:45 +00005701 Py_ssize_t i;
Tim Peters5b7da392003-02-04 00:21:07 +00005702 char *rev = "1.71"; /* XXX when does this change? */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005703 PyObject *format_version;
5704 PyObject *compatible_formats;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005705
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005706 Picklertype.ob_type = &PyType_Type;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005707 Unpicklertype.ob_type = &PyType_Type;
5708 PdataType.ob_type = &PyType_Type;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005709
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005710 /* Initialize some pieces. We need to do this before module creation,
Tim Peters5b7da392003-02-04 00:21:07 +00005711 * so we're forced to use a temporary dictionary. :(
5712 */
5713 di = PyDict_New();
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005714 if (!di) return;
5715 if (init_stuff(di) < 0) return;
Guido van Rossumebba4202000-09-07 14:35:37 +00005716
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005717 /* Create the module and add the functions */
5718 m = Py_InitModule4("cPickle", cPickle_methods,
5719 cPickle_module_documentation,
5720 (PyObject*)NULL,PYTHON_API_VERSION);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00005721 if (m == NULL)
5722 return;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005723
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005724 /* Add some symbolic constants to the module */
5725 d = PyModule_GetDict(m);
Tim Peters5b7da392003-02-04 00:21:07 +00005726 v = PyString_FromString(rev);
5727 PyDict_SetItemString(d, "__version__", v);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005728 Py_XDECREF(v);
Barry Warsaw93d29b61997-01-14 17:45:08 +00005729
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005730 /* Copy data from di. Waaa. */
5731 for (i=0; PyDict_Next(di, &i, &k, &v); ) {
5732 if (PyObject_SetItem(d, k, v) < 0) {
5733 Py_DECREF(di);
5734 return;
5735 }
5736 }
5737 Py_DECREF(di);
Guido van Rossumebba4202000-09-07 14:35:37 +00005738
Tim Peters8587b3c2003-02-13 15:44:41 +00005739 i = PyModule_AddIntConstant(m, "HIGHEST_PROTOCOL", HIGHEST_PROTOCOL);
5740 if (i < 0)
5741 return;
5742
Tim Peters5b7da392003-02-04 00:21:07 +00005743 /* These are purely informational; no code uses them. */
5744 /* File format version we write. */
5745 format_version = PyString_FromString("2.0");
5746 /* Format versions we can read. */
5747 compatible_formats = Py_BuildValue("[sssss]",
5748 "1.0", /* Original protocol 0 */
5749 "1.1", /* Protocol 0 + INST */
5750 "1.2", /* Original protocol 1 */
5751 "1.3", /* Protocol 1 + BINFLOAT */
Tim Peters0dd23aa2003-02-04 00:30:46 +00005752 "2.0"); /* Original protocol 2 */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005753 PyDict_SetItemString(d, "format_version", format_version);
5754 PyDict_SetItemString(d, "compatible_formats", compatible_formats);
5755 Py_XDECREF(format_version);
5756 Py_XDECREF(compatible_formats);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005757}