blob: afa75fd5fe36ff2931e91cc26340c78a429f411c [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
Georg Brandldffbf5f2008-05-20 07:49:57 +0000108/* copy_reg.dispatch_table, {type_object: pickling_function} */
Tim Peters5b7da392003-02-04 00:21:07 +0000109static PyObject *dispatch_table;
110
111/* For EXT[124] opcodes. */
Georg Brandldffbf5f2008-05-20 07:49:57 +0000112/* copy_reg._extension_registry, {(module_name, function_name): code} */
Tim Peters5b7da392003-02-04 00:21:07 +0000113static PyObject *extension_registry;
Georg Brandldffbf5f2008-05-20 07:49:57 +0000114/* copy_reg._inverted_registry, {code: (module_name, function_name)} */
Tim Peters5b7da392003-02-04 00:21:07 +0000115static PyObject *inverted_registry;
Georg Brandldffbf5f2008-05-20 07:49:57 +0000116/* copy_reg._extension_cache, {code: object} */
Tim Peters5b7da392003-02-04 00:21:07 +0000117static PyObject *extension_cache;
118
Georg Brandldffbf5f2008-05-20 07:49:57 +0000119/* For looking up name pairs in copy_reg._extension_registry. */
Tim Peters731098b2003-02-04 20:56:09 +0000120static 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,
Neal Norwitzb183a252006-04-10 01:03:32 +0000126 *read_str, *readline_str, *__main___str,
Alexandre Vassalotti9510e4a2008-05-11 08:25:28 +0000127 *copyreg_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öwis68192102007-07-21 06:55:02 +0000154 PyVarObject_HEAD_INIT(NULL, 0) "cPickle.Pdata", sizeof(Pdata), 0,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000155 (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
Christian Heimese93237d2007-12-19 02:37:44 +0000159#define Pdata_Check(O) (Py_TYPE(O) == &PdataType)
Guido van Rossum053b8df1998-11-25 16:18:00 +0000160
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++) {
Neal Norwitzb59d08c2006-07-22 16:20:49 +0000199 Py_CLEAR(*p);
Tim Peters1d63c9f2003-02-02 20:29:39 +0000200 }
201 self->length = clearto;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000202
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000203 return 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000204}
205
Tim Peters84e87f32001-03-17 04:50:51 +0000206static int
Tim Peterscba30e22003-02-01 06:24:36 +0000207Pdata_grow(Pdata *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000208{
Tim Peters1d63c9f2003-02-02 20:29:39 +0000209 int bigger;
210 size_t nbytes;
Neal Norwitzb59d08c2006-07-22 16:20:49 +0000211 PyObject **tmp;
Tim Peters1d63c9f2003-02-02 20:29:39 +0000212
Tim Peters1d63c9f2003-02-02 20:29:39 +0000213 bigger = self->size << 1;
Tim Peterse0a39072003-02-03 15:45:56 +0000214 if (bigger <= 0) /* was 0, or new value overflows */
Tim Peters1d63c9f2003-02-02 20:29:39 +0000215 goto nomemory;
216 if ((int)(size_t)bigger != bigger)
217 goto nomemory;
218 nbytes = (size_t)bigger * sizeof(PyObject *);
219 if (nbytes / sizeof(PyObject *) != (size_t)bigger)
220 goto nomemory;
Neal Norwitzb59d08c2006-07-22 16:20:49 +0000221 tmp = realloc(self->data, nbytes);
222 if (tmp == NULL)
Tim Peters1d63c9f2003-02-02 20:29:39 +0000223 goto nomemory;
Neal Norwitzb59d08c2006-07-22 16:20:49 +0000224 self->data = tmp;
Tim Peters1d63c9f2003-02-02 20:29:39 +0000225 self->size = bigger;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000226 return 0;
Tim Peters1d63c9f2003-02-02 20:29:39 +0000227
228 nomemory:
Tim Peters1d63c9f2003-02-02 20:29:39 +0000229 PyErr_NoMemory();
230 return -1;
Tim Peters84e87f32001-03-17 04:50:51 +0000231}
Guido van Rossum053b8df1998-11-25 16:18:00 +0000232
Tim Peterse0a39072003-02-03 15:45:56 +0000233/* D is a Pdata*. Pop the topmost element and store it into V, which
234 * must be an lvalue holding PyObject*. On stack underflow, UnpicklingError
Tim Peters1d63c9f2003-02-02 20:29:39 +0000235 * is raised and V is set to NULL. D and V may be evaluated several times.
236 */
237#define PDATA_POP(D, V) { \
Tim Peterse0a39072003-02-03 15:45:56 +0000238 if ((D)->length) \
239 (V) = (D)->data[--((D)->length)]; \
240 else { \
241 PyErr_SetString(UnpicklingError, "bad pickle data"); \
242 (V) = NULL; \
243 } \
Guido van Rossum053b8df1998-11-25 16:18:00 +0000244}
245
Tim Peterse0a39072003-02-03 15:45:56 +0000246/* PDATA_PUSH and PDATA_APPEND both push rvalue PyObject* O on to Pdata*
247 * D. If the Pdata stack can't be grown to hold the new value, both
248 * raise MemoryError and execute "return ER". The difference is in ownership
249 * of O after: _PUSH transfers ownership of O from the caller to the stack
250 * (no incref of O is done, and in case of error O is decrefed), while
251 * _APPEND pushes a new reference.
252 */
253
254/* Push O on stack D, giving ownership of O to the stack. */
255#define PDATA_PUSH(D, O, ER) { \
256 if (((Pdata*)(D))->length == ((Pdata*)(D))->size && \
257 Pdata_grow((Pdata*)(D)) < 0) { \
258 Py_DECREF(O); \
259 return ER; \
260 } \
261 ((Pdata*)(D))->data[((Pdata*)(D))->length++] = (O); \
262}
263
264/* Push O on stack D, pushing a new reference. */
265#define PDATA_APPEND(D, O, ER) { \
266 if (((Pdata*)(D))->length == ((Pdata*)(D))->size && \
267 Pdata_grow((Pdata*)(D)) < 0) \
268 return ER; \
269 Py_INCREF(O); \
270 ((Pdata*)(D))->data[((Pdata*)(D))->length++] = (O); \
271}
272
273
Guido van Rossum053b8df1998-11-25 16:18:00 +0000274static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +0000275Pdata_popTuple(Pdata *self, int start)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000276{
277 PyObject *r;
278 int i, j, l;
Tim Peterscba30e22003-02-01 06:24:36 +0000279
Tim Peters1d63c9f2003-02-02 20:29:39 +0000280 l = self->length-start;
281 r = PyTuple_New(l);
282 if (r == NULL)
283 return NULL;
284 for (i = start, j = 0 ; j < l; i++, j++)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000285 PyTuple_SET_ITEM(r, j, self->data[i]);
Tim Peterscba30e22003-02-01 06:24:36 +0000286
Tim Peters1d63c9f2003-02-02 20:29:39 +0000287 self->length = start;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000288 return r;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000289}
290
291static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +0000292Pdata_popList(Pdata *self, int start)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000293{
294 PyObject *r;
295 int i, j, l;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000296
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000297 l=self->length-start;
298 if (!( r=PyList_New(l))) return NULL;
299 for (i=start, j=0 ; j < l; i++, j++)
300 PyList_SET_ITEM(r, j, self->data[i]);
Guido van Rossum053b8df1998-11-25 16:18:00 +0000301
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000302 self->length=start;
303 return r;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000304}
305
Guido van Rossum053b8df1998-11-25 16:18:00 +0000306/*************************************************************************/
307
308#define ARG_TUP(self, o) { \
309 if (self->arg || (self->arg=PyTuple_New(1))) { \
310 Py_XDECREF(PyTuple_GET_ITEM(self->arg,0)); \
311 PyTuple_SET_ITEM(self->arg,0,o); \
312 } \
313 else { \
314 Py_DECREF(o); \
315 } \
316}
317
318#define FREE_ARG_TUP(self) { \
Christian Heimese93237d2007-12-19 02:37:44 +0000319 if (Py_REFCNT(self->arg) > 1) { \
Guido van Rossum053b8df1998-11-25 16:18:00 +0000320 Py_DECREF(self->arg); \
321 self->arg=NULL; \
322 } \
323 }
324
Thomas Wouters3b6448f2000-07-22 23:56:07 +0000325typedef struct Picklerobject {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000326 PyObject_HEAD
327 FILE *fp;
328 PyObject *write;
329 PyObject *file;
330 PyObject *memo;
331 PyObject *arg;
332 PyObject *pers_func;
333 PyObject *inst_pers_func;
Tim Peters797ec242003-02-01 06:22:36 +0000334
335 /* pickle protocol number, >= 0 */
336 int proto;
337
338 /* bool, true if proto > 0 */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000339 int bin;
Tim Peters797ec242003-02-01 06:22:36 +0000340
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000341 int fast; /* Fast mode doesn't save in memo, don't use if circ ref */
Martin v. Löwis5a395302002-08-04 08:20:23 +0000342 int nesting;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000343 int (*write_func)(struct Picklerobject *, const char *, Py_ssize_t);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000344 char *write_buf;
345 int buf_size;
346 PyObject *dispatch_table;
347 int fast_container; /* count nested container dumps */
348 PyObject *fast_memo;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000349} Picklerobject;
350
Barry Warsaw52acb492001-12-21 20:04:22 +0000351#ifndef PY_CPICKLE_FAST_LIMIT
352#define PY_CPICKLE_FAST_LIMIT 50
353#endif
Jeremy Hyltona0fb1772001-10-12 04:11:06 +0000354
Jeremy Hylton938ace62002-07-17 16:30:39 +0000355static PyTypeObject Picklertype;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000356
Thomas Wouters3b6448f2000-07-22 23:56:07 +0000357typedef struct Unpicklerobject {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000358 PyObject_HEAD
359 FILE *fp;
360 PyObject *file;
361 PyObject *readline;
362 PyObject *read;
363 PyObject *memo;
364 PyObject *arg;
365 Pdata *stack;
366 PyObject *mark;
367 PyObject *pers_func;
368 PyObject *last_string;
369 int *marks;
370 int num_marks;
371 int marks_size;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000372 Py_ssize_t (*read_func)(struct Unpicklerobject *, char **, Py_ssize_t);
373 Py_ssize_t (*readline_func)(struct Unpicklerobject *, char **);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000374 int buf_size;
375 char *buf;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000376 PyObject *find_class;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000377} Unpicklerobject;
Tim Peters84e87f32001-03-17 04:50:51 +0000378
Jeremy Hylton938ace62002-07-17 16:30:39 +0000379static PyTypeObject Unpicklertype;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000380
Thomas Wouters4789b3a2000-07-24 11:36:47 +0000381/* Forward decls that need the above structs */
382static int save(Picklerobject *, PyObject *, int);
383static int put2(Picklerobject *, PyObject *);
384
Guido van Rossumd385d591997-04-09 17:47:47 +0000385static
Guido van Rossum60456fd1997-04-09 17:36:32 +0000386PyObject *
Thomas Wouters3b6448f2000-07-22 23:56:07 +0000387cPickle_ErrFormat(PyObject *ErrType, char *stringformat, char *format, ...)
388{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000389 va_list va;
390 PyObject *args=0, *retval=0;
391 va_start(va, format);
Tim Peterscba30e22003-02-01 06:24:36 +0000392
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000393 if (format) args = Py_VaBuildValue(format, va);
394 va_end(va);
395 if (format && ! args) return NULL;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000396 if (stringformat && !(retval=PyString_FromString(stringformat)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000397 return NULL;
Tim Peterscba30e22003-02-01 06:24:36 +0000398
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000399 if (retval) {
400 if (args) {
401 PyObject *v;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000402 v=PyString_Format(retval, args);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000403 Py_DECREF(retval);
404 Py_DECREF(args);
405 if (! v) return NULL;
406 retval=v;
407 }
408 }
409 else
410 if (args) retval=args;
411 else {
412 PyErr_SetObject(ErrType,Py_None);
413 return NULL;
414 }
415 PyErr_SetObject(ErrType,retval);
416 Py_DECREF(retval);
417 return NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000418}
419
Tim Peters84e87f32001-03-17 04:50:51 +0000420static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000421write_file(Picklerobject *self, const char *s, Py_ssize_t n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000422{
423 size_t nbyteswritten;
Tim Peters84e87f32001-03-17 04:50:51 +0000424
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000425 if (s == NULL) {
426 return 0;
427 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000428
Martin v. Löwis18e16552006-02-15 17:27:45 +0000429 if (n > INT_MAX) {
430 /* String too large */
431 return -1;
432 }
433
Gregory P. Smithc20adf82008-04-07 06:33:21 +0000434 PyFile_IncUseCount((PyFileObject *)self->file);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000435 Py_BEGIN_ALLOW_THREADS
Jeremy Hylton7fa4bfa2002-07-18 20:58:57 +0000436 nbyteswritten = fwrite(s, sizeof(char), n, self->fp);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000437 Py_END_ALLOW_THREADS
Gregory P. Smithc20adf82008-04-07 06:33:21 +0000438 PyFile_DecUseCount((PyFileObject *)self->file);
Jeremy Hylton7fa4bfa2002-07-18 20:58:57 +0000439 if (nbyteswritten != (size_t)n) {
440 PyErr_SetFromErrno(PyExc_IOError);
441 return -1;
442 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000443
Martin v. Löwis18e16552006-02-15 17:27:45 +0000444 return (int)n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000445}
446
Tim Peters84e87f32001-03-17 04:50:51 +0000447static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000448write_cStringIO(Picklerobject *self, const char *s, Py_ssize_t n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000449{
450 if (s == NULL) {
451 return 0;
452 }
Tim Peterscba30e22003-02-01 06:24:36 +0000453
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000454 if (PycStringIO->cwrite((PyObject *)self->file, s, n) != n) {
455 return -1;
456 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000457
Martin v. Löwis18e16552006-02-15 17:27:45 +0000458 return (int)n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000459}
460
Tim Peters84e87f32001-03-17 04:50:51 +0000461static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000462write_none(Picklerobject *self, const char *s, Py_ssize_t n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000463{
464 if (s == NULL) return 0;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000465 if (n > INT_MAX) return -1;
466 return (int)n;
Guido van Rossum142eeb81997-08-13 03:14:41 +0000467}
468
Tim Peters84e87f32001-03-17 04:50:51 +0000469static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000470write_other(Picklerobject *self, const char *s, Py_ssize_t _n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000471{
472 PyObject *py_str = 0, *junk = 0;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000473 int n;
Tim Peterscba30e22003-02-01 06:24:36 +0000474
Martin v. Löwis18e16552006-02-15 17:27:45 +0000475 if (_n > INT_MAX)
476 return -1;
477 n = (int)_n;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000478 if (s == NULL) {
479 if (!( self->buf_size )) return 0;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000480 py_str = PyString_FromStringAndSize(self->write_buf,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000481 self->buf_size);
Tim Peterscba30e22003-02-01 06:24:36 +0000482 if (!py_str)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000483 return -1;
484 }
485 else {
486 if (self->buf_size && (n + self->buf_size) > WRITE_BUF_SIZE) {
487 if (write_other(self, NULL, 0) < 0)
488 return -1;
489 }
Tim Peterscba30e22003-02-01 06:24:36 +0000490
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000491 if (n > WRITE_BUF_SIZE) {
492 if (!( py_str =
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000493 PyString_FromStringAndSize(s, n)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000494 return -1;
495 }
496 else {
497 memcpy(self->write_buf + self->buf_size, s, n);
498 self->buf_size += n;
499 return n;
500 }
501 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000502
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000503 if (self->write) {
504 /* object with write method */
505 ARG_TUP(self, py_str);
506 if (self->arg) {
507 junk = PyObject_Call(self->write, self->arg, NULL);
508 FREE_ARG_TUP(self);
509 }
510 if (junk) Py_DECREF(junk);
511 else return -1;
512 }
513 else
514 PDATA_PUSH(self->file, py_str, -1);
Guido van Rossum60456fd1997-04-09 17:36:32 +0000515
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000516 self->buf_size = 0;
517 return n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000518}
519
520
Martin v. Löwis18e16552006-02-15 17:27:45 +0000521static Py_ssize_t
522read_file(Unpicklerobject *self, char **s, Py_ssize_t n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000523{
524 size_t nbytesread;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000525
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000526 if (self->buf_size == 0) {
527 int size;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000528
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000529 size = ((n < 32) ? 32 : n);
Tim Petersee1a53c2003-02-02 02:57:53 +0000530 if (!( self->buf = (char *)malloc(size))) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000531 PyErr_NoMemory();
532 return -1;
533 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000534
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000535 self->buf_size = size;
536 }
537 else if (n > self->buf_size) {
Kristján Valur Jónsson17b8e972007-04-25 00:10:50 +0000538 char *newbuf = (char *)realloc(self->buf, n);
539 if (!newbuf) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000540 PyErr_NoMemory();
541 return -1;
542 }
Kristján Valur Jónsson17b8e972007-04-25 00:10:50 +0000543 self->buf = newbuf;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000544 self->buf_size = n;
545 }
Tim Peters84e87f32001-03-17 04:50:51 +0000546
Gregory P. Smithc20adf82008-04-07 06:33:21 +0000547 PyFile_IncUseCount((PyFileObject *)self->file);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000548 Py_BEGIN_ALLOW_THREADS
549 nbytesread = fread(self->buf, sizeof(char), n, self->fp);
550 Py_END_ALLOW_THREADS
Gregory P. Smithc20adf82008-04-07 06:33:21 +0000551 PyFile_DecUseCount((PyFileObject *)self->file);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000552 if (nbytesread != (size_t)n) {
553 if (feof(self->fp)) {
554 PyErr_SetNone(PyExc_EOFError);
555 return -1;
556 }
Tim Peterscba30e22003-02-01 06:24:36 +0000557
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000558 PyErr_SetFromErrno(PyExc_IOError);
559 return -1;
560 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000561
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000562 *s = self->buf;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000563
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000564 return n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000565}
566
567
Martin v. Löwis18e16552006-02-15 17:27:45 +0000568static Py_ssize_t
Tim Peterscba30e22003-02-01 06:24:36 +0000569readline_file(Unpicklerobject *self, char **s)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000570{
571 int i;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000572
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000573 if (self->buf_size == 0) {
Tim Petersee1a53c2003-02-02 02:57:53 +0000574 if (!( self->buf = (char *)malloc(40))) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000575 PyErr_NoMemory();
576 return -1;
577 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000578 self->buf_size = 40;
579 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000580
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000581 i = 0;
582 while (1) {
Tim Petersee1a53c2003-02-02 02:57:53 +0000583 int bigger;
Kristján Valur Jónsson17b8e972007-04-25 00:10:50 +0000584 char *newbuf;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000585 for (; i < (self->buf_size - 1); i++) {
Tim Peterscba30e22003-02-01 06:24:36 +0000586 if (feof(self->fp) ||
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000587 (self->buf[i] = getc(self->fp)) == '\n') {
588 self->buf[i + 1] = '\0';
589 *s = self->buf;
590 return i + 1;
591 }
592 }
Tim Petersee1a53c2003-02-02 02:57:53 +0000593 bigger = self->buf_size << 1;
594 if (bigger <= 0) { /* overflow */
595 PyErr_NoMemory();
596 return -1;
597 }
Kristján Valur Jónsson17b8e972007-04-25 00:10:50 +0000598 newbuf = (char *)realloc(self->buf, bigger);
599 if (!newbuf) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000600 PyErr_NoMemory();
601 return -1;
602 }
Kristján Valur Jónsson17b8e972007-04-25 00:10:50 +0000603 self->buf = newbuf;
Tim Petersee1a53c2003-02-02 02:57:53 +0000604 self->buf_size = bigger;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000605 }
Tim Peters84e87f32001-03-17 04:50:51 +0000606}
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000607
608
Martin v. Löwis18e16552006-02-15 17:27:45 +0000609static Py_ssize_t
610read_cStringIO(Unpicklerobject *self, char **s, Py_ssize_t n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000611{
612 char *ptr;
Tim Peterscba30e22003-02-01 06:24:36 +0000613
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000614 if (PycStringIO->cread((PyObject *)self->file, &ptr, n) != n) {
615 PyErr_SetNone(PyExc_EOFError);
616 return -1;
617 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000618
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000619 *s = ptr;
Tim Peterscba30e22003-02-01 06:24:36 +0000620
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000621 return n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000622}
623
624
Martin v. Löwis18e16552006-02-15 17:27:45 +0000625static Py_ssize_t
Tim Peterscba30e22003-02-01 06:24:36 +0000626readline_cStringIO(Unpicklerobject *self, char **s)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000627{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000628 Py_ssize_t n;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000629 char *ptr;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000630
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000631 if ((n = PycStringIO->creadline((PyObject *)self->file, &ptr)) < 0) {
632 return -1;
633 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000634
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000635 *s = ptr;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000636
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000637 return n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000638}
639
640
Martin v. Löwis18e16552006-02-15 17:27:45 +0000641static Py_ssize_t
642read_other(Unpicklerobject *self, char **s, Py_ssize_t n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000643{
644 PyObject *bytes, *str=0;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000645
Martin v. Löwis18e16552006-02-15 17:27:45 +0000646 if (!( bytes = PyInt_FromSsize_t(n))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000647
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000648 ARG_TUP(self, bytes);
649 if (self->arg) {
650 str = PyObject_Call(self->read, self->arg, NULL);
651 FREE_ARG_TUP(self);
652 }
653 if (! str) return -1;
Tim Peterscba30e22003-02-01 06:24:36 +0000654
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000655 Py_XDECREF(self->last_string);
656 self->last_string = str;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000657
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000658 if (! (*s = PyString_AsString(str))) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000659 return n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000660}
661
662
Martin v. Löwis18e16552006-02-15 17:27:45 +0000663static Py_ssize_t
Tim Peterscba30e22003-02-01 06:24:36 +0000664readline_other(Unpicklerobject *self, char **s)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000665{
666 PyObject *str;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000667 Py_ssize_t str_size;
Tim Peterscba30e22003-02-01 06:24:36 +0000668
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000669 if (!( str = PyObject_CallObject(self->readline, empty_tuple))) {
670 return -1;
671 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000672
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000673 if ((str_size = PyString_Size(str)) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000674 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000675
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000676 Py_XDECREF(self->last_string);
677 self->last_string = str;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000678
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000679 if (! (*s = PyString_AsString(str)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000680 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000681
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000682 return str_size;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000683}
684
Tim Petersee1a53c2003-02-02 02:57:53 +0000685/* Copy the first n bytes from s into newly malloc'ed memory, plus a
686 * trailing 0 byte. Return a pointer to that, or NULL if out of memory.
687 * The caller is responsible for free()'ing the return value.
688 */
Guido van Rossum60456fd1997-04-09 17:36:32 +0000689static char *
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000690pystrndup(const char *s, int n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000691{
Tim Petersee1a53c2003-02-02 02:57:53 +0000692 char *r = (char *)malloc(n+1);
693 if (r == NULL)
694 return (char*)PyErr_NoMemory();
695 memcpy(r, s, n);
696 r[n] = 0;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000697 return r;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000698}
699
700
701static int
Tim Peterscba30e22003-02-01 06:24:36 +0000702get(Picklerobject *self, PyObject *id)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000703{
704 PyObject *value, *mv;
705 long c_value;
706 char s[30];
707 size_t len;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000708
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000709 if (!( mv = PyDict_GetItem(self->memo, id))) {
710 PyErr_SetObject(PyExc_KeyError, id);
711 return -1;
712 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000713
Tim Peterscba30e22003-02-01 06:24:36 +0000714 if (!( value = PyTuple_GetItem(mv, 0)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000715 return -1;
Tim Peters84e87f32001-03-17 04:50:51 +0000716
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000717 if (!( PyInt_Check(value))) {
718 PyErr_SetString(PicklingError, "no int where int expected in memo");
719 return -1;
720 }
721 c_value = PyInt_AS_LONG((PyIntObject*)value);
Guido van Rossum60456fd1997-04-09 17:36:32 +0000722
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000723 if (!self->bin) {
724 s[0] = GET;
725 PyOS_snprintf(s + 1, sizeof(s) - 1, "%ld\n", c_value);
726 len = strlen(s);
727 }
728 else if (Pdata_Check(self->file)) {
729 if (write_other(self, NULL, 0) < 0) return -1;
730 PDATA_APPEND(self->file, mv, -1);
731 return 0;
732 }
733 else {
734 if (c_value < 256) {
735 s[0] = BINGET;
736 s[1] = (int)(c_value & 0xff);
737 len = 2;
738 }
739 else {
740 s[0] = LONG_BINGET;
741 s[1] = (int)(c_value & 0xff);
742 s[2] = (int)((c_value >> 8) & 0xff);
743 s[3] = (int)((c_value >> 16) & 0xff);
744 s[4] = (int)((c_value >> 24) & 0xff);
745 len = 5;
746 }
747 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000748
Tim Peters0bc93f52003-02-02 18:29:33 +0000749 if (self->write_func(self, s, len) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000750 return -1;
Tim Peterscba30e22003-02-01 06:24:36 +0000751
Jeremy Hyltona0fb1772001-10-12 04:11:06 +0000752 return 0;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000753}
Jeremy Hyltonce616e41998-08-13 23:13:52 +0000754
Guido van Rossum60456fd1997-04-09 17:36:32 +0000755
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000756static int
Tim Peterscba30e22003-02-01 06:24:36 +0000757put(Picklerobject *self, PyObject *ob)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000758{
Christian Heimese93237d2007-12-19 02:37:44 +0000759 if (Py_REFCNT(ob) < 2 || self->fast)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000760 return 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000761
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000762 return put2(self, ob);
763}
Guido van Rossum053b8df1998-11-25 16:18:00 +0000764
Guido van Rossum053b8df1998-11-25 16:18:00 +0000765
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000766static int
Tim Peterscba30e22003-02-01 06:24:36 +0000767put2(Picklerobject *self, PyObject *ob)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000768{
769 char c_str[30];
770 int p;
771 size_t len;
772 int res = -1;
773 PyObject *py_ob_id = 0, *memo_len = 0, *t = 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000774
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000775 if (self->fast)
776 return 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000777
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000778 if ((p = PyDict_Size(self->memo)) < 0)
779 goto finally;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000780
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000781 /* Make sure memo keys are positive! */
Tim Peters70b02d72003-02-02 17:26:40 +0000782 /* XXX Why?
783 * XXX And does "positive" really mean non-negative?
784 * XXX pickle.py starts with PUT index 0, not 1. This makes for
785 * XXX gratuitous differences between the pickling modules.
786 */
Tim Peterscba30e22003-02-01 06:24:36 +0000787 p++;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000788
Tim Peterscba30e22003-02-01 06:24:36 +0000789 if (!( py_ob_id = PyLong_FromVoidPtr(ob)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000790 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000791
Tim Peterscba30e22003-02-01 06:24:36 +0000792 if (!( memo_len = PyInt_FromLong(p)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000793 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000794
Tim Peterscba30e22003-02-01 06:24:36 +0000795 if (!( t = PyTuple_New(2)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000796 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000797
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000798 PyTuple_SET_ITEM(t, 0, memo_len);
799 Py_INCREF(memo_len);
800 PyTuple_SET_ITEM(t, 1, ob);
801 Py_INCREF(ob);
802
803 if (PyDict_SetItem(self->memo, py_ob_id, t) < 0)
804 goto finally;
805
806 if (!self->bin) {
807 c_str[0] = PUT;
808 PyOS_snprintf(c_str + 1, sizeof(c_str) - 1, "%d\n", p);
809 len = strlen(c_str);
810 }
811 else if (Pdata_Check(self->file)) {
812 if (write_other(self, NULL, 0) < 0) return -1;
813 PDATA_APPEND(self->file, memo_len, -1);
814 res=0; /* Job well done ;) */
815 goto finally;
816 }
817 else {
818 if (p >= 256) {
819 c_str[0] = LONG_BINPUT;
820 c_str[1] = (int)(p & 0xff);
821 c_str[2] = (int)((p >> 8) & 0xff);
822 c_str[3] = (int)((p >> 16) & 0xff);
823 c_str[4] = (int)((p >> 24) & 0xff);
824 len = 5;
825 }
826 else {
827 c_str[0] = BINPUT;
828 c_str[1] = p;
829 len = 2;
830 }
831 }
832
Tim Peters0bc93f52003-02-02 18:29:33 +0000833 if (self->write_func(self, c_str, len) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000834 goto finally;
835
836 res = 0;
837
838 finally:
839 Py_XDECREF(py_ob_id);
840 Py_XDECREF(memo_len);
841 Py_XDECREF(t);
842
843 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000844}
845
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000846static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +0000847whichmodule(PyObject *global, PyObject *global_name)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000848{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000849 Py_ssize_t i, j;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000850 PyObject *module = 0, *modules_dict = 0,
851 *global_name_attr = 0, *name = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000852
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000853 module = PyObject_GetAttrString(global, "__module__");
Jeremy Hyltonf00368f2003-06-16 20:19:49 +0000854 if (module)
855 return module;
856 if (PyErr_ExceptionMatches(PyExc_AttributeError))
857 PyErr_Clear();
858 else
859 return NULL;
Guido van Rossum45188231997-09-28 05:38:51 +0000860
Tim Peterscba30e22003-02-01 06:24:36 +0000861 if (!( modules_dict = PySys_GetObject("modules")))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000862 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000863
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000864 i = 0;
865 while ((j = PyDict_Next(modules_dict, &i, &name, &module))) {
Guido van Rossum142eeb81997-08-13 03:14:41 +0000866
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000867 if (PyObject_Compare(name, __main___str)==0) continue;
Tim Peters84e87f32001-03-17 04:50:51 +0000868
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000869 global_name_attr = PyObject_GetAttr(module, global_name);
870 if (!global_name_attr) {
Jeremy Hyltonf00368f2003-06-16 20:19:49 +0000871 if (PyErr_ExceptionMatches(PyExc_AttributeError))
872 PyErr_Clear();
873 else
874 return NULL;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000875 continue;
876 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000877
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000878 if (global_name_attr != global) {
879 Py_DECREF(global_name_attr);
880 continue;
881 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000882
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000883 Py_DECREF(global_name_attr);
Guido van Rossum60456fd1997-04-09 17:36:32 +0000884
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000885 break;
886 }
Guido van Rossum142eeb81997-08-13 03:14:41 +0000887
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000888 /* The following implements the rule in pickle.py added in 1.5
889 that used __main__ if no module is found. I don't actually
890 like this rule. jlf
891 */
892 if (!j) {
893 j=1;
894 name=__main___str;
895 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000896
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000897 Py_INCREF(name);
898 return name;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000899}
900
901
Guido van Rossum60456fd1997-04-09 17:36:32 +0000902static int
Jeremy Hylton499ab6a2001-10-15 21:37:58 +0000903fast_save_enter(Picklerobject *self, PyObject *obj)
904{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000905 /* if fast_container < 0, we're doing an error exit. */
906 if (++self->fast_container >= PY_CPICKLE_FAST_LIMIT) {
907 PyObject *key = NULL;
908 if (self->fast_memo == NULL) {
909 self->fast_memo = PyDict_New();
910 if (self->fast_memo == NULL) {
911 self->fast_container = -1;
912 return 0;
913 }
914 }
915 key = PyLong_FromVoidPtr(obj);
916 if (key == NULL)
917 return 0;
918 if (PyDict_GetItem(self->fast_memo, key)) {
Tim Peters4e52ca82002-12-07 02:43:28 +0000919 Py_DECREF(key);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000920 PyErr_Format(PyExc_ValueError,
Tim Peters92c8bb32003-02-13 23:00:26 +0000921 "fast mode: can't pickle cyclic objects "
922 "including object type %s at %p",
Christian Heimese93237d2007-12-19 02:37:44 +0000923 Py_TYPE(obj)->tp_name, obj);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000924 self->fast_container = -1;
925 return 0;
926 }
927 if (PyDict_SetItem(self->fast_memo, key, Py_None) < 0) {
Tim Peters4e52ca82002-12-07 02:43:28 +0000928 Py_DECREF(key);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000929 self->fast_container = -1;
930 return 0;
931 }
Tim Peters4e52ca82002-12-07 02:43:28 +0000932 Py_DECREF(key);
Jeremy Hylton499ab6a2001-10-15 21:37:58 +0000933 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000934 return 1;
Jeremy Hylton499ab6a2001-10-15 21:37:58 +0000935}
936
Tim Peterscba30e22003-02-01 06:24:36 +0000937int
Jeremy Hylton499ab6a2001-10-15 21:37:58 +0000938fast_save_leave(Picklerobject *self, PyObject *obj)
939{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000940 if (self->fast_container-- >= PY_CPICKLE_FAST_LIMIT) {
941 PyObject *key = PyLong_FromVoidPtr(obj);
942 if (key == NULL)
943 return 0;
944 if (PyDict_DelItem(self->fast_memo, key) < 0) {
Tim Peters4e52ca82002-12-07 02:43:28 +0000945 Py_DECREF(key);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000946 return 0;
947 }
Tim Peters4e52ca82002-12-07 02:43:28 +0000948 Py_DECREF(key);
Jeremy Hylton499ab6a2001-10-15 21:37:58 +0000949 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000950 return 1;
Jeremy Hylton499ab6a2001-10-15 21:37:58 +0000951}
952
953static int
Tim Peterscba30e22003-02-01 06:24:36 +0000954save_none(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000955{
956 static char none = NONE;
Tim Peters0bc93f52003-02-02 18:29:33 +0000957 if (self->write_func(self, &none, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000958 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000959
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000960 return 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000961}
962
Guido van Rossum77f6a652002-04-03 22:41:51 +0000963static int
Tim Peterscba30e22003-02-01 06:24:36 +0000964save_bool(Picklerobject *self, PyObject *args)
Guido van Rossum77f6a652002-04-03 22:41:51 +0000965{
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000966 static const char *buf[2] = {FALSE, TRUE};
Guido van Rossume2763392002-04-05 19:30:08 +0000967 static char len[2] = {sizeof(FALSE)-1, sizeof(TRUE)-1};
Guido van Rossum77f6a652002-04-03 22:41:51 +0000968 long l = PyInt_AS_LONG((PyIntObject *)args);
969
Tim Peters3c67d792003-02-02 17:59:11 +0000970 if (self->proto >= 2) {
971 char opcode = l ? NEWTRUE : NEWFALSE;
972 if (self->write_func(self, &opcode, 1) < 0)
973 return -1;
974 }
975 else if (self->write_func(self, buf[l], len[l]) < 0)
Guido van Rossum77f6a652002-04-03 22:41:51 +0000976 return -1;
Guido van Rossum77f6a652002-04-03 22:41:51 +0000977 return 0;
978}
Tim Peters84e87f32001-03-17 04:50:51 +0000979
Guido van Rossum60456fd1997-04-09 17:36:32 +0000980static int
Tim Peterscba30e22003-02-01 06:24:36 +0000981save_int(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000982{
983 char c_str[32];
984 long l = PyInt_AS_LONG((PyIntObject *)args);
985 int len = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000986
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000987 if (!self->bin
Guido van Rossumde8d6d71997-05-13 18:00:44 +0000988#if SIZEOF_LONG > 4
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000989 || l > 0x7fffffffL
990 || l < -0x80000000L
Guido van Rossumde8d6d71997-05-13 18:00:44 +0000991#endif
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000992 ) {
993 /* Text-mode pickle, or long too big to fit in the 4-byte
994 * signed BININT format: store as a string.
995 */
996 c_str[0] = INT;
997 PyOS_snprintf(c_str + 1, sizeof(c_str) - 1, "%ld\n", l);
Tim Peters0bc93f52003-02-02 18:29:33 +0000998 if (self->write_func(self, c_str, strlen(c_str)) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000999 return -1;
1000 }
1001 else {
1002 /* Binary pickle and l fits in a signed 4-byte int. */
1003 c_str[1] = (int)( l & 0xff);
1004 c_str[2] = (int)((l >> 8) & 0xff);
1005 c_str[3] = (int)((l >> 16) & 0xff);
1006 c_str[4] = (int)((l >> 24) & 0xff);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001007
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001008 if ((c_str[4] == 0) && (c_str[3] == 0)) {
1009 if (c_str[2] == 0) {
1010 c_str[0] = BININT1;
1011 len = 2;
1012 }
1013 else {
1014 c_str[0] = BININT2;
1015 len = 3;
1016 }
1017 }
1018 else {
1019 c_str[0] = BININT;
1020 len = 5;
1021 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001022
Tim Peters0bc93f52003-02-02 18:29:33 +00001023 if (self->write_func(self, c_str, len) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001024 return -1;
1025 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001026
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001027 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001028}
1029
1030
1031static int
Tim Peterscba30e22003-02-01 06:24:36 +00001032save_long(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001033{
Armin Rigo7ccbca92006-10-04 12:17:45 +00001034 Py_ssize_t size;
Tim Petersee1a53c2003-02-02 02:57:53 +00001035 int res = -1;
1036 PyObject *repr = NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001037
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001038 static char l = LONG;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001039
Tim Petersee1a53c2003-02-02 02:57:53 +00001040 if (self->proto >= 2) {
1041 /* Linear-time pickling. */
1042 size_t nbits;
1043 size_t nbytes;
1044 unsigned char *pdata;
1045 char c_str[5];
1046 int i;
1047 int sign = _PyLong_Sign(args);
1048
1049 if (sign == 0) {
1050 /* It's 0 -- an empty bytestring. */
1051 c_str[0] = LONG1;
1052 c_str[1] = 0;
1053 i = self->write_func(self, c_str, 2);
1054 if (i < 0) goto finally;
1055 res = 0;
1056 goto finally;
1057 }
1058 nbits = _PyLong_NumBits(args);
1059 if (nbits == (size_t)-1 && PyErr_Occurred())
1060 goto finally;
1061 /* How many bytes do we need? There are nbits >> 3 full
1062 * bytes of data, and nbits & 7 leftover bits. If there
1063 * are any leftover bits, then we clearly need another
1064 * byte. Wnat's not so obvious is that we *probably*
1065 * need another byte even if there aren't any leftovers:
1066 * the most-significant bit of the most-significant byte
1067 * acts like a sign bit, and it's usually got a sense
1068 * opposite of the one we need. The exception is longs
1069 * of the form -(2**(8*j-1)) for j > 0. Such a long is
1070 * its own 256's-complement, so has the right sign bit
1071 * even without the extra byte. That's a pain to check
1072 * for in advance, though, so we always grab an extra
1073 * byte at the start, and cut it back later if possible.
1074 */
1075 nbytes = (nbits >> 3) + 1;
Armin Rigo7ccbca92006-10-04 12:17:45 +00001076 if (nbytes > INT_MAX) {
Tim Petersee1a53c2003-02-02 02:57:53 +00001077 PyErr_SetString(PyExc_OverflowError, "long too large "
1078 "to pickle");
1079 goto finally;
1080 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001081 repr = PyString_FromStringAndSize(NULL, (int)nbytes);
Tim Petersee1a53c2003-02-02 02:57:53 +00001082 if (repr == NULL) goto finally;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001083 pdata = (unsigned char *)PyString_AS_STRING(repr);
Tim Petersee1a53c2003-02-02 02:57:53 +00001084 i = _PyLong_AsByteArray((PyLongObject *)args,
1085 pdata, nbytes,
1086 1 /* little endian */, 1 /* signed */);
1087 if (i < 0) goto finally;
1088 /* If the long is negative, this may be a byte more than
1089 * needed. This is so iff the MSB is all redundant sign
1090 * bits.
1091 */
1092 if (sign < 0 && nbytes > 1 && pdata[nbytes - 1] == 0xff &&
1093 (pdata[nbytes - 2] & 0x80) != 0)
1094 --nbytes;
1095
1096 if (nbytes < 256) {
1097 c_str[0] = LONG1;
1098 c_str[1] = (char)nbytes;
1099 size = 2;
1100 }
1101 else {
1102 c_str[0] = LONG4;
1103 size = (int)nbytes;
1104 for (i = 1; i < 5; i++) {
1105 c_str[i] = (char)(size & 0xff);
1106 size >>= 8;
1107 }
1108 size = 5;
1109 }
1110 i = self->write_func(self, c_str, size);
1111 if (i < 0) goto finally;
1112 i = self->write_func(self, (char *)pdata, (int)nbytes);
1113 if (i < 0) goto finally;
1114 res = 0;
1115 goto finally;
1116 }
1117
1118 /* proto < 2: write the repr and newline. This is quadratic-time
1119 * (in the number of digits), in both directions.
1120 */
Tim Peterscba30e22003-02-01 06:24:36 +00001121 if (!( repr = PyObject_Repr(args)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001122 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001123
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001124 if ((size = PyString_Size(repr)) < 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, &l, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001128 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001129
Tim Peters0bc93f52003-02-02 18:29:33 +00001130 if (self->write_func(self,
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001131 PyString_AS_STRING((PyStringObject *)repr),
Tim Peters0bc93f52003-02-02 18:29:33 +00001132 size) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001133 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001134
Tim Peters0bc93f52003-02-02 18:29:33 +00001135 if (self->write_func(self, "\n", 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001136 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001137
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001138 res = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001139
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001140 finally:
1141 Py_XDECREF(repr);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001142 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001143}
1144
1145
1146static int
Tim Peterscba30e22003-02-01 06:24:36 +00001147save_float(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001148{
1149 double x = PyFloat_AS_DOUBLE((PyFloatObject *)args);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001150
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001151 if (self->bin) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001152 char str[9];
Tim Peters9905b942003-03-20 20:53:32 +00001153 str[0] = BINFLOAT;
1154 if (_PyFloat_Pack8(x, (unsigned char *)&str[1], 0) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001155 return -1;
Tim Peters0bc93f52003-02-02 18:29:33 +00001156 if (self->write_func(self, str, 9) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001157 return -1;
1158 }
1159 else {
1160 char c_str[250];
1161 c_str[0] = FLOAT;
Georg Brandlde9b6242006-04-30 11:13:56 +00001162 PyOS_ascii_formatd(c_str + 1, sizeof(c_str) - 2, "%.17g", x);
1163 /* Extend the formatted string with a newline character */
1164 strcat(c_str, "\n");
Guido van Rossum60456fd1997-04-09 17:36:32 +00001165
Tim Peters0bc93f52003-02-02 18:29:33 +00001166 if (self->write_func(self, c_str, strlen(c_str)) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001167 return -1;
1168 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001169
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001170 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001171}
1172
1173
1174static int
Tim Peterscba30e22003-02-01 06:24:36 +00001175save_string(Picklerobject *self, PyObject *args, int doput)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001176{
1177 int size, len;
1178 PyObject *repr=0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001179
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001180 if ((size = PyString_Size(args)) < 0)
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 (!self->bin) {
1184 char *repr_str;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001185
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001186 static char string = STRING;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001187
Tim Peterscba30e22003-02-01 06:24:36 +00001188 if (!( repr = PyObject_Repr(args)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001189 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001190
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001191 if ((len = PyString_Size(repr)) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001192 goto err;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001193 repr_str = PyString_AS_STRING((PyStringObject *)repr);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001194
Tim Peters0bc93f52003-02-02 18:29:33 +00001195 if (self->write_func(self, &string, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001196 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001197
Tim Peters0bc93f52003-02-02 18:29:33 +00001198 if (self->write_func(self, repr_str, len) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001199 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001200
Tim Peters0bc93f52003-02-02 18:29:33 +00001201 if (self->write_func(self, "\n", 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001202 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001203
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001204 Py_XDECREF(repr);
1205 }
1206 else {
1207 int i;
1208 char c_str[5];
Guido van Rossum60456fd1997-04-09 17:36:32 +00001209
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001210 if ((size = PyString_Size(args)) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001211 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001212
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001213 if (size < 256) {
1214 c_str[0] = SHORT_BINSTRING;
1215 c_str[1] = size;
1216 len = 2;
1217 }
Armin Rigo7ccbca92006-10-04 12:17:45 +00001218 else if (size <= INT_MAX) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001219 c_str[0] = BINSTRING;
1220 for (i = 1; i < 5; i++)
1221 c_str[i] = (int)(size >> ((i - 1) * 8));
1222 len = 5;
1223 }
Armin Rigo7ccbca92006-10-04 12:17:45 +00001224 else
1225 return -1; /* string too large */
Guido van Rossum60456fd1997-04-09 17:36:32 +00001226
Tim Peters0bc93f52003-02-02 18:29:33 +00001227 if (self->write_func(self, c_str, len) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001228 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001229
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001230 if (size > 128 && Pdata_Check(self->file)) {
1231 if (write_other(self, NULL, 0) < 0) return -1;
1232 PDATA_APPEND(self->file, args, -1);
1233 }
1234 else {
Tim Peters0bc93f52003-02-02 18:29:33 +00001235 if (self->write_func(self,
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001236 PyString_AS_STRING(
1237 (PyStringObject *)args),
Tim Peters0bc93f52003-02-02 18:29:33 +00001238 size) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001239 return -1;
1240 }
1241 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001242
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001243 if (doput)
1244 if (put(self, args) < 0)
1245 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001246
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001247 return 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +00001248
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001249 err:
1250 Py_XDECREF(repr);
1251 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001252}
1253
1254
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001255#ifdef Py_USING_UNICODE
Guido van Rossumfb10c3f2000-12-19 02:08:38 +00001256/* A copy of PyUnicode_EncodeRawUnicodeEscape() that also translates
1257 backslash and newline characters to \uXXXX escapes. */
1258static PyObject *
1259modified_EncodeRawUnicodeEscape(const Py_UNICODE *s, int size)
1260{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001261 PyObject *repr;
1262 char *p;
1263 char *q;
Guido van Rossumfb10c3f2000-12-19 02:08:38 +00001264
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001265 static const char *hexdigit = "0123456789ABCDEF";
Guido van Rossumfb10c3f2000-12-19 02:08:38 +00001266
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001267 repr = PyString_FromStringAndSize(NULL, 6 * size);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001268 if (repr == NULL)
1269 return NULL;
1270 if (size == 0)
1271 return repr;
1272
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001273 p = q = PyString_AS_STRING(repr);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001274 while (size-- > 0) {
1275 Py_UNICODE ch = *s++;
1276 /* Map 16-bit characters to '\uxxxx' */
1277 if (ch >= 256 || ch == '\\' || ch == '\n') {
1278 *p++ = '\\';
1279 *p++ = 'u';
1280 *p++ = hexdigit[(ch >> 12) & 0xf];
1281 *p++ = hexdigit[(ch >> 8) & 0xf];
1282 *p++ = hexdigit[(ch >> 4) & 0xf];
1283 *p++ = hexdigit[ch & 15];
1284 }
1285 /* Copy everything else as-is */
1286 else
1287 *p++ = (char) ch;
1288 }
1289 *p = '\0';
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001290 _PyString_Resize(&repr, p - q);
Guido van Rossumfb10c3f2000-12-19 02:08:38 +00001291 return repr;
Guido van Rossumfb10c3f2000-12-19 02:08:38 +00001292}
1293
1294
Guido van Rossum60456fd1997-04-09 17:36:32 +00001295static int
Tim Peterscba30e22003-02-01 06:24:36 +00001296save_unicode(Picklerobject *self, PyObject *args, int doput)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001297{
Armin Rigo7ccbca92006-10-04 12:17:45 +00001298 Py_ssize_t size, len;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001299 PyObject *repr=0;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001300
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001301 if (!PyUnicode_Check(args))
1302 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001303
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001304 if (!self->bin) {
1305 char *repr_str;
1306 static char string = UNICODE;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001307
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001308 repr = modified_EncodeRawUnicodeEscape(
1309 PyUnicode_AS_UNICODE(args), PyUnicode_GET_SIZE(args));
Tim Peterscba30e22003-02-01 06:24:36 +00001310 if (!repr)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001311 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001312
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001313 if ((len = PyString_Size(repr)) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001314 goto err;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001315 repr_str = PyString_AS_STRING((PyStringObject *)repr);
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001316
Tim Peters0bc93f52003-02-02 18:29:33 +00001317 if (self->write_func(self, &string, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001318 goto err;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001319
Tim Peters0bc93f52003-02-02 18:29:33 +00001320 if (self->write_func(self, repr_str, len) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001321 goto err;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001322
Tim Peters0bc93f52003-02-02 18:29:33 +00001323 if (self->write_func(self, "\n", 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001324 goto err;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001325
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001326 Py_XDECREF(repr);
1327 }
1328 else {
1329 int i;
1330 char c_str[5];
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001331
Tim Peterscba30e22003-02-01 06:24:36 +00001332 if (!( repr = PyUnicode_AsUTF8String(args)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001333 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001334
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001335 if ((size = PyString_Size(repr)) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001336 goto err;
Armin Rigo7ccbca92006-10-04 12:17:45 +00001337 if (size > INT_MAX)
1338 return -1; /* string too large */
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001339
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001340 c_str[0] = BINUNICODE;
1341 for (i = 1; i < 5; i++)
1342 c_str[i] = (int)(size >> ((i - 1) * 8));
1343 len = 5;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001344
Tim Peters0bc93f52003-02-02 18:29:33 +00001345 if (self->write_func(self, c_str, len) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001346 goto err;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001347
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001348 if (size > 128 && Pdata_Check(self->file)) {
1349 if (write_other(self, NULL, 0) < 0)
1350 goto err;
1351 PDATA_APPEND(self->file, repr, -1);
1352 }
1353 else {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001354 if (self->write_func(self, PyString_AS_STRING(repr),
Tim Peters0bc93f52003-02-02 18:29:33 +00001355 size) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001356 goto err;
1357 }
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001358
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001359 Py_DECREF(repr);
1360 }
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001361
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001362 if (doput)
1363 if (put(self, args) < 0)
1364 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001365
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001366 return 0;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001367
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001368 err:
1369 Py_XDECREF(repr);
1370 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001371}
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001372#endif
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001373
Tim Peters1d63c9f2003-02-02 20:29:39 +00001374/* A helper for save_tuple. Push the len elements in tuple t on the stack. */
1375static int
Tim Peters67920142003-02-05 03:46:17 +00001376store_tuple_elements(Picklerobject *self, PyObject *t, int len)
Tim Peters1d63c9f2003-02-02 20:29:39 +00001377{
1378 int i;
1379 int res = -1; /* guilty until proved innocent */
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001380
Tim Peters1d63c9f2003-02-02 20:29:39 +00001381 assert(PyTuple_Size(t) == len);
1382
1383 for (i = 0; i < len; i++) {
1384 PyObject *element = PyTuple_GET_ITEM(t, i);
1385
1386 if (element == NULL)
1387 goto finally;
1388 if (save(self, element, 0) < 0)
1389 goto finally;
1390 }
1391 res = 0;
1392
1393 finally:
1394 return res;
1395}
1396
1397/* Tuples are ubiquitous in the pickle protocols, so many techniques are
1398 * used across protocols to minimize the space needed to pickle them.
Tim Peters67920142003-02-05 03:46:17 +00001399 * Tuples are also the only builtin immutable type that can be recursive
Tim Peters1d63c9f2003-02-02 20:29:39 +00001400 * (a tuple can be reached from itself), and that requires some subtle
1401 * magic so that it works in all cases. IOW, this is a long routine.
1402 */
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001403static int
Tim Peterscba30e22003-02-01 06:24:36 +00001404save_tuple(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001405{
Tim Peters1d63c9f2003-02-02 20:29:39 +00001406 PyObject *py_tuple_id = NULL;
1407 int len, i;
1408 int res = -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001409
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001410 static char tuple = TUPLE;
Tim Peters1d63c9f2003-02-02 20:29:39 +00001411 static char pop = POP;
1412 static char pop_mark = POP_MARK;
1413 static char len2opcode[] = {EMPTY_TUPLE, TUPLE1, TUPLE2, TUPLE3};
Guido van Rossum60456fd1997-04-09 17:36:32 +00001414
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001415 if ((len = PyTuple_Size(args)) < 0)
1416 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001417
Tim Peters1d63c9f2003-02-02 20:29:39 +00001418 if (len == 0) {
1419 char c_str[2];
Tim Peters84e87f32001-03-17 04:50:51 +00001420
Tim Peters1d63c9f2003-02-02 20:29:39 +00001421 if (self->proto) {
1422 c_str[0] = EMPTY_TUPLE;
1423 len = 1;
1424 }
1425 else {
1426 c_str[0] = MARK;
1427 c_str[1] = TUPLE;
1428 len = 2;
1429 }
1430 if (self->write_func(self, c_str, len) >= 0)
1431 res = 0;
1432 /* Don't memoize an empty tuple. */
1433 goto finally;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001434 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001435
Tim Peters1d63c9f2003-02-02 20:29:39 +00001436 /* A non-empty tuple. */
1437
1438 /* id(tuple) isn't in the memo now. If it shows up there after
1439 * saving the tuple elements, the tuple must be recursive, in
1440 * which case we'll pop everything we put on the stack, and fetch
1441 * its value from the memo.
1442 */
1443 py_tuple_id = PyLong_FromVoidPtr(args);
1444 if (py_tuple_id == NULL)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001445 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001446
Tim Peters1d63c9f2003-02-02 20:29:39 +00001447 if (len <= 3 && self->proto >= 2) {
1448 /* Use TUPLE{1,2,3} opcodes. */
Tim Peters67920142003-02-05 03:46:17 +00001449 if (store_tuple_elements(self, args, len) < 0)
Tim Peters1d63c9f2003-02-02 20:29:39 +00001450 goto finally;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001451 if (PyDict_GetItem(self->memo, py_tuple_id)) {
Tim Peters1d63c9f2003-02-02 20:29:39 +00001452 /* pop the len elements */
1453 for (i = 0; i < len; ++i)
1454 if (self->write_func(self, &pop, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001455 goto finally;
Tim Peters1d63c9f2003-02-02 20:29:39 +00001456 /* fetch from memo */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001457 if (get(self, py_tuple_id) < 0)
1458 goto finally;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001459 res = 0;
1460 goto finally;
1461 }
Tim Peters1d63c9f2003-02-02 20:29:39 +00001462 /* Not recursive. */
1463 if (self->write_func(self, len2opcode + len, 1) < 0)
1464 goto finally;
1465 goto memoize;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001466 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001467
Tim Peters1d63c9f2003-02-02 20:29:39 +00001468 /* proto < 2 and len > 0, or proto >= 2 and len > 3.
1469 * Generate MARK elt1 elt2 ... TUPLE
1470 */
1471 if (self->write_func(self, &MARKv, 1) < 0)
1472 goto finally;
1473
Tim Peters67920142003-02-05 03:46:17 +00001474 if (store_tuple_elements(self, args, len) < 0)
Tim Peters1d63c9f2003-02-02 20:29:39 +00001475 goto finally;
1476
1477 if (PyDict_GetItem(self->memo, py_tuple_id)) {
1478 /* pop the stack stuff we pushed */
1479 if (self->bin) {
1480 if (self->write_func(self, &pop_mark, 1) < 0)
1481 goto finally;
1482 }
1483 else {
1484 /* Note that we pop one more than len, to remove
1485 * the MARK too.
1486 */
1487 for (i = 0; i <= len; i++)
1488 if (self->write_func(self, &pop, 1) < 0)
1489 goto finally;
1490 }
1491 /* fetch from memo */
1492 if (get(self, py_tuple_id) >= 0)
1493 res = 0;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001494 goto finally;
1495 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001496
Tim Peters1d63c9f2003-02-02 20:29:39 +00001497 /* Not recursive. */
1498 if (self->write_func(self, &tuple, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001499 goto finally;
Tim Peters84e87f32001-03-17 04:50:51 +00001500
Tim Peters1d63c9f2003-02-02 20:29:39 +00001501 memoize:
1502 if (put(self, args) >= 0)
1503 res = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001504
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001505 finally:
1506 Py_XDECREF(py_tuple_id);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001507 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001508}
1509
Tim Peters1092d642003-02-11 21:06:20 +00001510/* iter is an iterator giving items, and we batch up chunks of
1511 * MARK item item ... item APPENDS
1512 * opcode sequences. Calling code should have arranged to first create an
1513 * empty list, or list-like object, for the APPENDS to operate on.
1514 * Returns 0 on success, <0 on error.
1515 */
1516static int
1517batch_list(Picklerobject *self, PyObject *iter)
1518{
1519 PyObject *obj;
1520 PyObject *slice[BATCHSIZE];
1521 int i, n;
1522
1523 static char append = APPEND;
1524 static char appends = APPENDS;
1525
1526 assert(iter != NULL);
1527
1528 if (self->proto == 0) {
1529 /* APPENDS isn't available; do one at a time. */
1530 for (;;) {
1531 obj = PyIter_Next(iter);
1532 if (obj == NULL) {
1533 if (PyErr_Occurred())
1534 return -1;
1535 break;
1536 }
1537 i = save(self, obj, 0);
1538 Py_DECREF(obj);
1539 if (i < 0)
1540 return -1;
1541 if (self->write_func(self, &append, 1) < 0)
1542 return -1;
Tim Peters1092d642003-02-11 21:06:20 +00001543 }
1544 return 0;
1545 }
1546
1547 /* proto > 0: write in batches of BATCHSIZE. */
1548 do {
1549 /* Get next group of (no more than) BATCHSIZE elements. */
1550 for (n = 0; n < BATCHSIZE; ++n) {
1551 obj = PyIter_Next(iter);
1552 if (obj == NULL) {
1553 if (PyErr_Occurred())
1554 goto BatchFailed;
1555 break;
1556 }
1557 slice[n] = obj;
1558 }
1559
1560 if (n > 1) {
1561 /* Pump out MARK, slice[0:n], APPENDS. */
1562 if (self->write_func(self, &MARKv, 1) < 0)
1563 goto BatchFailed;
1564 for (i = 0; i < n; ++i) {
1565 if (save(self, slice[i], 0) < 0)
1566 goto BatchFailed;
1567 }
1568 if (self->write_func(self, &appends, 1) < 0)
1569 goto BatchFailed;
1570 }
1571 else if (n == 1) {
1572 if (save(self, slice[0], 0) < 0)
1573 goto BatchFailed;
1574 if (self->write_func(self, &append, 1) < 0)
1575 goto BatchFailed;
1576 }
1577
1578 for (i = 0; i < n; ++i) {
1579 Py_DECREF(slice[i]);
1580 }
Tim Peters90975f12003-02-12 05:28:58 +00001581 } while (n == BATCHSIZE);
Tim Peters1092d642003-02-11 21:06:20 +00001582 return 0;
1583
1584BatchFailed:
1585 while (--n >= 0) {
1586 Py_DECREF(slice[n]);
1587 }
1588 return -1;
1589}
1590
Guido van Rossum60456fd1997-04-09 17:36:32 +00001591static int
Tim Peterscba30e22003-02-01 06:24:36 +00001592save_list(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001593{
Tim Peters1092d642003-02-11 21:06:20 +00001594 int res = -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001595 char s[3];
Tim Peters1092d642003-02-11 21:06:20 +00001596 int len;
1597 PyObject *iter;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001598
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001599 if (self->fast && !fast_save_enter(self, args))
1600 goto finally;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00001601
Tim Peters1092d642003-02-11 21:06:20 +00001602 /* Create an empty list. */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001603 if (self->bin) {
1604 s[0] = EMPTY_LIST;
Tim Peters1092d642003-02-11 21:06:20 +00001605 len = 1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001606 }
1607 else {
1608 s[0] = MARK;
1609 s[1] = LIST;
Tim Peters1092d642003-02-11 21:06:20 +00001610 len = 2;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001611 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001612
Tim Peters1092d642003-02-11 21:06:20 +00001613 if (self->write_func(self, s, len) < 0)
1614 goto finally;
1615
1616 /* Get list length, and bow out early if empty. */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001617 if ((len = PyList_Size(args)) < 0)
1618 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001619
Tim Peters1092d642003-02-11 21:06:20 +00001620 /* Memoize. */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001621 if (len == 0) {
Tim Peters90975f12003-02-12 05:28:58 +00001622 if (put(self, args) >= 0)
1623 res = 0;
1624 goto finally;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001625 }
Tim Peters90975f12003-02-12 05:28:58 +00001626 if (put2(self, args) < 0)
1627 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001628
Tim Peters1092d642003-02-11 21:06:20 +00001629 /* Materialize the list elements. */
1630 iter = PyObject_GetIter(args);
1631 if (iter == NULL)
1632 goto finally;
1633 res = batch_list(self, iter);
1634 Py_DECREF(iter);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001635
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001636 finally:
1637 if (self->fast && !fast_save_leave(self, args))
1638 res = -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001639
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001640 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001641}
1642
1643
Tim Peters42f08ac2003-02-11 22:43:24 +00001644/* iter is an iterator giving (key, value) pairs, and we batch up chunks of
1645 * MARK key value ... key value SETITEMS
1646 * opcode sequences. Calling code should have arranged to first create an
1647 * empty dict, or dict-like object, for the SETITEMS to operate on.
1648 * Returns 0 on success, <0 on error.
1649 *
1650 * This is very much like batch_list(). The difference between saving
1651 * elements directly, and picking apart two-tuples, is so long-winded at
1652 * the C level, though, that attempts to combine these routines were too
1653 * ugly to bear.
1654 */
1655static int
1656batch_dict(Picklerobject *self, PyObject *iter)
1657{
1658 PyObject *p;
1659 PyObject *slice[BATCHSIZE];
1660 int i, n;
1661
1662 static char setitem = SETITEM;
1663 static char setitems = SETITEMS;
1664
1665 assert(iter != NULL);
1666
1667 if (self->proto == 0) {
1668 /* SETITEMS isn't available; do one at a time. */
1669 for (;;) {
1670 p = PyIter_Next(iter);
1671 if (p == NULL) {
1672 if (PyErr_Occurred())
1673 return -1;
1674 break;
1675 }
1676 if (!PyTuple_Check(p) || PyTuple_Size(p) != 2) {
1677 PyErr_SetString(PyExc_TypeError, "dict items "
1678 "iterator must return 2-tuples");
1679 return -1;
1680 }
1681 i = save(self, PyTuple_GET_ITEM(p, 0), 0);
1682 if (i >= 0)
1683 i = save(self, PyTuple_GET_ITEM(p, 1), 0);
1684 Py_DECREF(p);
1685 if (i < 0)
1686 return -1;
1687 if (self->write_func(self, &setitem, 1) < 0)
1688 return -1;
Tim Peters42f08ac2003-02-11 22:43:24 +00001689 }
1690 return 0;
1691 }
1692
1693 /* proto > 0: write in batches of BATCHSIZE. */
1694 do {
1695 /* Get next group of (no more than) BATCHSIZE elements. */
1696 for (n = 0; n < BATCHSIZE; ++n) {
1697 p = PyIter_Next(iter);
1698 if (p == NULL) {
1699 if (PyErr_Occurred())
1700 goto BatchFailed;
1701 break;
1702 }
1703 if (!PyTuple_Check(p) || PyTuple_Size(p) != 2) {
1704 PyErr_SetString(PyExc_TypeError, "dict items "
1705 "iterator must return 2-tuples");
1706 goto BatchFailed;
1707 }
1708 slice[n] = p;
1709 }
1710
1711 if (n > 1) {
1712 /* Pump out MARK, slice[0:n], SETITEMS. */
1713 if (self->write_func(self, &MARKv, 1) < 0)
1714 goto BatchFailed;
1715 for (i = 0; i < n; ++i) {
1716 p = slice[i];
1717 if (save(self, PyTuple_GET_ITEM(p, 0), 0) < 0)
1718 goto BatchFailed;
1719 if (save(self, PyTuple_GET_ITEM(p, 1), 0) < 0)
1720 goto BatchFailed;
1721 }
1722 if (self->write_func(self, &setitems, 1) < 0)
1723 goto BatchFailed;
1724 }
1725 else if (n == 1) {
1726 p = slice[0];
1727 if (save(self, PyTuple_GET_ITEM(p, 0), 0) < 0)
1728 goto BatchFailed;
1729 if (save(self, PyTuple_GET_ITEM(p, 1), 0) < 0)
1730 goto BatchFailed;
1731 if (self->write_func(self, &setitem, 1) < 0)
1732 goto BatchFailed;
1733 }
1734
1735 for (i = 0; i < n; ++i) {
1736 Py_DECREF(slice[i]);
1737 }
Tim Peters90975f12003-02-12 05:28:58 +00001738 } while (n == BATCHSIZE);
Tim Peters42f08ac2003-02-11 22:43:24 +00001739 return 0;
1740
1741BatchFailed:
1742 while (--n >= 0) {
1743 Py_DECREF(slice[n]);
1744 }
1745 return -1;
1746}
1747
Guido van Rossum60456fd1997-04-09 17:36:32 +00001748static int
Tim Peterscba30e22003-02-01 06:24:36 +00001749save_dict(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001750{
Tim Peters42f08ac2003-02-11 22:43:24 +00001751 int res = -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001752 char s[3];
Tim Peters42f08ac2003-02-11 22:43:24 +00001753 int len;
1754 PyObject *iter;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001755
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001756 if (self->fast && !fast_save_enter(self, args))
1757 goto finally;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00001758
Tim Peters42f08ac2003-02-11 22:43:24 +00001759 /* Create an empty dict. */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001760 if (self->bin) {
1761 s[0] = EMPTY_DICT;
1762 len = 1;
1763 }
1764 else {
1765 s[0] = MARK;
1766 s[1] = DICT;
1767 len = 2;
1768 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001769
Tim Peters0bc93f52003-02-02 18:29:33 +00001770 if (self->write_func(self, s, len) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001771 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001772
Tim Peters42f08ac2003-02-11 22:43:24 +00001773 /* Get dict size, and bow out early if empty. */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001774 if ((len = PyDict_Size(args)) < 0)
1775 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001776
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001777 if (len == 0) {
Tim Peters90975f12003-02-12 05:28:58 +00001778 if (put(self, args) >= 0)
1779 res = 0;
1780 goto finally;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001781 }
Tim Peters90975f12003-02-12 05:28:58 +00001782 if (put2(self, args) < 0)
1783 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001784
Tim Peters42f08ac2003-02-11 22:43:24 +00001785 /* Materialize the dict items. */
1786 iter = PyObject_CallMethod(args, "iteritems", "()");
1787 if (iter == NULL)
1788 goto finally;
1789 res = batch_dict(self, iter);
1790 Py_DECREF(iter);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001791
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001792 finally:
1793 if (self->fast && !fast_save_leave(self, args))
1794 res = -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001795
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001796 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001797}
1798
1799
Tim Peters84e87f32001-03-17 04:50:51 +00001800static int
Tim Peterscba30e22003-02-01 06:24:36 +00001801save_inst(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001802{
1803 PyObject *class = 0, *module = 0, *name = 0, *state = 0,
1804 *getinitargs_func = 0, *getstate_func = 0, *class_args = 0;
1805 char *module_str, *name_str;
1806 int module_size, name_size, res = -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001807
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001808 static char inst = INST, obj = OBJ, build = BUILD;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001809
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001810 if (self->fast && !fast_save_enter(self, args))
1811 goto finally;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00001812
Tim Peters0bc93f52003-02-02 18:29:33 +00001813 if (self->write_func(self, &MARKv, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001814 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001815
Tim Peterscba30e22003-02-01 06:24:36 +00001816 if (!( class = PyObject_GetAttr(args, __class___str)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001817 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001818
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001819 if (self->bin) {
1820 if (save(self, class, 0) < 0)
1821 goto finally;
1822 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001823
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001824 if ((getinitargs_func = PyObject_GetAttr(args, __getinitargs___str))) {
1825 PyObject *element = 0;
1826 int i, len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001827
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001828 if (!( class_args =
Tim Peterscba30e22003-02-01 06:24:36 +00001829 PyObject_Call(getinitargs_func, empty_tuple, NULL)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001830 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001831
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001832 if ((len = PyObject_Size(class_args)) < 0)
1833 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001834
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001835 for (i = 0; i < len; i++) {
Tim Peterscba30e22003-02-01 06:24:36 +00001836 if (!( element = PySequence_GetItem(class_args, i)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001837 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001838
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001839 if (save(self, element, 0) < 0) {
1840 Py_DECREF(element);
1841 goto finally;
1842 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001843
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001844 Py_DECREF(element);
1845 }
1846 }
1847 else {
Jeremy Hyltonf00368f2003-06-16 20:19:49 +00001848 if (PyErr_ExceptionMatches(PyExc_AttributeError))
1849 PyErr_Clear();
1850 else
1851 goto finally;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001852 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001853
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001854 if (!self->bin) {
1855 if (!( name = ((PyClassObject *)class)->cl_name )) {
1856 PyErr_SetString(PicklingError, "class has no name");
1857 goto finally;
1858 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001859
Tim Peterscba30e22003-02-01 06:24:36 +00001860 if (!( module = whichmodule(class, name)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001861 goto finally;
Guido van Rossum053b8df1998-11-25 16:18:00 +00001862
Tim Peters84e87f32001-03-17 04:50:51 +00001863
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001864 if ((module_size = PyString_Size(module)) < 0 ||
1865 (name_size = PyString_Size(name)) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001866 goto finally;
Guido van Rossum053b8df1998-11-25 16:18:00 +00001867
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001868 module_str = PyString_AS_STRING((PyStringObject *)module);
1869 name_str = PyString_AS_STRING((PyStringObject *)name);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001870
Tim Peters0bc93f52003-02-02 18:29:33 +00001871 if (self->write_func(self, &inst, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001872 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001873
Tim Peters0bc93f52003-02-02 18:29:33 +00001874 if (self->write_func(self, module_str, module_size) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001875 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001876
Tim Peters0bc93f52003-02-02 18:29:33 +00001877 if (self->write_func(self, "\n", 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001878 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001879
Tim Peters0bc93f52003-02-02 18:29:33 +00001880 if (self->write_func(self, name_str, name_size) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001881 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001882
Tim Peters0bc93f52003-02-02 18:29:33 +00001883 if (self->write_func(self, "\n", 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001884 goto finally;
1885 }
Tim Peters0bc93f52003-02-02 18:29:33 +00001886 else if (self->write_func(self, &obj, 1) < 0) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001887 goto finally;
1888 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001889
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001890 if ((getstate_func = PyObject_GetAttr(args, __getstate___str))) {
1891 state = PyObject_Call(getstate_func, empty_tuple, NULL);
Tim Peterscba30e22003-02-01 06:24:36 +00001892 if (!state)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001893 goto finally;
1894 }
1895 else {
Jeremy Hyltonf00368f2003-06-16 20:19:49 +00001896 if (PyErr_ExceptionMatches(PyExc_AttributeError))
1897 PyErr_Clear();
1898 else
1899 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001900
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001901 if (!( state = PyObject_GetAttr(args, __dict___str))) {
Jeremy Hyltonf00368f2003-06-16 20:19:49 +00001902 if (PyErr_ExceptionMatches(PyExc_AttributeError))
1903 PyErr_Clear();
1904 else
1905 goto finally;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001906 res = 0;
1907 goto finally;
1908 }
1909 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001910
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001911 if (!PyDict_Check(state)) {
1912 if (put2(self, args) < 0)
1913 goto finally;
1914 }
1915 else {
1916 if (put(self, args) < 0)
1917 goto finally;
1918 }
Tim Peters84e87f32001-03-17 04:50:51 +00001919
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001920 if (save(self, state, 0) < 0)
1921 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001922
Tim Peters0bc93f52003-02-02 18:29:33 +00001923 if (self->write_func(self, &build, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001924 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001925
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001926 res = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001927
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001928 finally:
1929 if (self->fast && !fast_save_leave(self, args))
1930 res = -1;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00001931
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001932 Py_XDECREF(module);
1933 Py_XDECREF(class);
1934 Py_XDECREF(state);
1935 Py_XDECREF(getinitargs_func);
1936 Py_XDECREF(getstate_func);
1937 Py_XDECREF(class_args);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001938
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001939 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001940}
1941
1942
Guido van Rossum60456fd1997-04-09 17:36:32 +00001943static int
Tim Peterscba30e22003-02-01 06:24:36 +00001944save_global(Picklerobject *self, PyObject *args, PyObject *name)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001945{
Jeremy Hylton9ee91f12002-07-11 22:02:33 +00001946 PyObject *global_name = 0, *module = 0, *mod = 0, *klass = 0;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001947 char *name_str, *module_str;
1948 int module_size, name_size, res = -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001949
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001950 static char global = GLOBAL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001951
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001952 if (name) {
1953 global_name = name;
1954 Py_INCREF(global_name);
1955 }
1956 else {
Tim Peterscba30e22003-02-01 06:24:36 +00001957 if (!( global_name = PyObject_GetAttr(args, __name___str)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001958 goto finally;
1959 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001960
Tim Peterscba30e22003-02-01 06:24:36 +00001961 if (!( module = whichmodule(args, global_name)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001962 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001963
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001964 if ((module_size = PyString_Size(module)) < 0 ||
1965 (name_size = PyString_Size(global_name)) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001966 goto finally;
Guido van Rossum053b8df1998-11-25 16:18:00 +00001967
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001968 module_str = PyString_AS_STRING((PyStringObject *)module);
1969 name_str = PyString_AS_STRING((PyStringObject *)global_name);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001970
Guido van Rossum75bfd052002-12-24 18:10:07 +00001971 /* XXX This can be doing a relative import. Clearly it shouldn't,
1972 but I don't know how to stop it. :-( */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001973 mod = PyImport_ImportModule(module_str);
1974 if (mod == NULL) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001975 cPickle_ErrFormat(PicklingError,
Tim Petersb9ce7cd2003-02-18 20:50:45 +00001976 "Can't pickle %s: import of module %s "
1977 "failed",
1978 "OS", args, module);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001979 goto finally;
1980 }
Jeremy Hylton0e1f7a82002-07-11 22:01:40 +00001981 klass = PyObject_GetAttrString(mod, name_str);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001982 if (klass == NULL) {
1983 cPickle_ErrFormat(PicklingError,
Tim Petersb9ce7cd2003-02-18 20:50:45 +00001984 "Can't pickle %s: attribute lookup %s.%s "
1985 "failed",
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001986 "OSS", args, module, global_name);
1987 goto finally;
1988 }
1989 if (klass != args) {
Jeremy Hylton0e1f7a82002-07-11 22:01:40 +00001990 Py_DECREF(klass);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001991 cPickle_ErrFormat(PicklingError,
Tim Peters731098b2003-02-04 20:56:09 +00001992 "Can't pickle %s: it's not the same object "
1993 "as %s.%s",
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001994 "OSS", args, module, global_name);
1995 goto finally;
1996 }
Jeremy Hylton0e1f7a82002-07-11 22:01:40 +00001997 Py_DECREF(klass);
Guido van Rossuma92d16a2001-08-18 21:22:07 +00001998
Tim Peters731098b2003-02-04 20:56:09 +00001999 if (self->proto >= 2) {
2000 /* See whether this is in the extension registry, and if
2001 * so generate an EXT opcode.
2002 */
2003 PyObject *py_code; /* extension code as Python object */
Tim Peters3e667d52003-02-04 21:47:44 +00002004 long code; /* extension code as C value */
Tim Peters731098b2003-02-04 20:56:09 +00002005 char c_str[5];
2006 int n;
2007
2008 PyTuple_SET_ITEM(two_tuple, 0, module);
2009 PyTuple_SET_ITEM(two_tuple, 1, global_name);
2010 py_code = PyDict_GetItem(extension_registry, two_tuple);
2011 if (py_code == NULL)
2012 goto gen_global; /* not registered */
2013
2014 /* Verify py_code has the right type and value. */
2015 if (!PyInt_Check(py_code)) {
2016 cPickle_ErrFormat(PicklingError, "Can't pickle %s: "
Tim Peters6288e232003-02-05 03:53:10 +00002017 "extension code %s isn't an integer",
Tim Peters731098b2003-02-04 20:56:09 +00002018 "OO", args, py_code);
2019 goto finally;
2020 }
2021 code = PyInt_AS_LONG(py_code);
2022 if (code <= 0 || code > 0x7fffffffL) {
2023 cPickle_ErrFormat(PicklingError, "Can't pickle %s: "
2024 "extension code %ld is out of range",
2025 "Ol", args, code);
2026 goto finally;
2027 }
2028
2029 /* Generate an EXT opcode. */
2030 if (code <= 0xff) {
2031 c_str[0] = EXT1;
2032 c_str[1] = (char)code;
2033 n = 2;
2034 }
2035 else if (code <= 0xffff) {
2036 c_str[0] = EXT2;
2037 c_str[1] = (char)(code & 0xff);
2038 c_str[2] = (char)((code >> 8) & 0xff);
2039 n = 3;
2040 }
2041 else {
2042 c_str[0] = EXT4;
2043 c_str[1] = (char)(code & 0xff);
2044 c_str[2] = (char)((code >> 8) & 0xff);
2045 c_str[3] = (char)((code >> 16) & 0xff);
2046 c_str[4] = (char)((code >> 24) & 0xff);
2047 n = 5;
2048 }
2049
2050 if (self->write_func(self, c_str, n) >= 0)
2051 res = 0;
2052 goto finally; /* and don't memoize */
2053 }
2054
2055 gen_global:
Tim Peters0bc93f52003-02-02 18:29:33 +00002056 if (self->write_func(self, &global, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002057 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002058
Tim Peters0bc93f52003-02-02 18:29:33 +00002059 if (self->write_func(self, module_str, module_size) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002060 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002061
Tim Peters0bc93f52003-02-02 18:29:33 +00002062 if (self->write_func(self, "\n", 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002063 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002064
Tim Peters0bc93f52003-02-02 18:29:33 +00002065 if (self->write_func(self, name_str, name_size) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002066 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002067
Tim Peters0bc93f52003-02-02 18:29:33 +00002068 if (self->write_func(self, "\n", 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002069 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002070
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002071 if (put(self, args) < 0)
2072 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002073
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002074 res = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002075
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002076 finally:
2077 Py_XDECREF(module);
2078 Py_XDECREF(global_name);
2079 Py_XDECREF(mod);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002080
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002081 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002082}
2083
Guido van Rossum60456fd1997-04-09 17:36:32 +00002084static int
Tim Peterscba30e22003-02-01 06:24:36 +00002085save_pers(Picklerobject *self, PyObject *args, PyObject *f)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002086{
2087 PyObject *pid = 0;
2088 int size, res = -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002089
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002090 static char persid = PERSID, binpersid = BINPERSID;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002091
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002092 Py_INCREF(args);
2093 ARG_TUP(self, args);
2094 if (self->arg) {
2095 pid = PyObject_Call(f, self->arg, NULL);
2096 FREE_ARG_TUP(self);
2097 }
2098 if (! pid) return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002099
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002100 if (pid != Py_None) {
2101 if (!self->bin) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002102 if (!PyString_Check(pid)) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002103 PyErr_SetString(PicklingError,
2104 "persistent id must be string");
2105 goto finally;
2106 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002107
Tim Peters0bc93f52003-02-02 18:29:33 +00002108 if (self->write_func(self, &persid, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002109 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002110
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002111 if ((size = PyString_Size(pid)) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002112 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002113
Tim Peters0bc93f52003-02-02 18:29:33 +00002114 if (self->write_func(self,
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002115 PyString_AS_STRING(
2116 (PyStringObject *)pid),
Tim Peters0bc93f52003-02-02 18:29:33 +00002117 size) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002118 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002119
Tim Peters0bc93f52003-02-02 18:29:33 +00002120 if (self->write_func(self, "\n", 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002121 goto finally;
Tim Peters84e87f32001-03-17 04:50:51 +00002122
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002123 res = 1;
2124 goto finally;
2125 }
2126 else if (save(self, pid, 1) >= 0) {
Tim Peters0bc93f52003-02-02 18:29:33 +00002127 if (self->write_func(self, &binpersid, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002128 res = -1;
2129 else
2130 res = 1;
2131 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002132
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002133 goto finally;
2134 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002135
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002136 res = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002137
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002138 finally:
2139 Py_XDECREF(pid);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002140
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002141 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002142}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002143
Tim Peters71fcda52003-02-14 23:05:28 +00002144/* We're saving ob, and args is the 2-thru-5 tuple returned by the
2145 * appropriate __reduce__ method for ob.
2146 */
Tim Peters84e87f32001-03-17 04:50:51 +00002147static int
Tim Peters71fcda52003-02-14 23:05:28 +00002148save_reduce(Picklerobject *self, PyObject *args, PyObject *ob)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002149{
Tim Peters71fcda52003-02-14 23:05:28 +00002150 PyObject *callable;
2151 PyObject *argtup;
2152 PyObject *state = NULL;
2153 PyObject *listitems = NULL;
2154 PyObject *dictitems = NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002155
Tim Peters71fcda52003-02-14 23:05:28 +00002156 int use_newobj = self->proto >= 2;
2157
2158 static char reduce = REDUCE;
2159 static char build = BUILD;
2160 static char newobj = NEWOBJ;
2161
2162 if (! PyArg_UnpackTuple(args, "save_reduce", 2, 5,
2163 &callable,
2164 &argtup,
2165 &state,
2166 &listitems,
2167 &dictitems))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002168 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002169
Raymond Hettingera6b45cc2004-12-07 07:05:57 +00002170 if (!PyTuple_Check(argtup)) {
2171 PyErr_SetString(PicklingError,
2172 "args from reduce() should be a tuple");
2173 return -1;
2174 }
2175
Tim Peters71fcda52003-02-14 23:05:28 +00002176 if (state == Py_None)
2177 state = NULL;
2178 if (listitems == Py_None)
2179 listitems = NULL;
2180 if (dictitems == Py_None)
2181 dictitems = NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002182
Tim Peters71fcda52003-02-14 23:05:28 +00002183 /* Protocol 2 special case: if callable's name is __newobj__, use
2184 * NEWOBJ. This consumes a lot of code.
2185 */
2186 if (use_newobj) {
2187 PyObject *temp = PyObject_GetAttr(callable, __name___str);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002188
Tim Peters71fcda52003-02-14 23:05:28 +00002189 if (temp == NULL) {
Jeremy Hyltonf00368f2003-06-16 20:19:49 +00002190 if (PyErr_ExceptionMatches(PyExc_AttributeError))
2191 PyErr_Clear();
2192 else
2193 return -1;
Tim Peters71fcda52003-02-14 23:05:28 +00002194 use_newobj = 0;
2195 }
2196 else {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002197 use_newobj = PyString_Check(temp) &&
2198 strcmp(PyString_AS_STRING(temp),
Tim Peters71fcda52003-02-14 23:05:28 +00002199 "__newobj__") == 0;
2200 Py_DECREF(temp);
2201 }
2202 }
2203 if (use_newobj) {
2204 PyObject *cls;
2205 PyObject *newargtup;
2206 int n, i;
2207
2208 /* Sanity checks. */
2209 n = PyTuple_Size(argtup);
2210 if (n < 1) {
2211 PyErr_SetString(PicklingError, "__newobj__ arglist "
2212 "is empty");
2213 return -1;
2214 }
2215
2216 cls = PyTuple_GET_ITEM(argtup, 0);
2217 if (! PyObject_HasAttrString(cls, "__new__")) {
2218 PyErr_SetString(PicklingError, "args[0] from "
2219 "__newobj__ args has no __new__");
2220 return -1;
2221 }
2222
2223 /* XXX How could ob be NULL? */
2224 if (ob != NULL) {
2225 PyObject *ob_dot_class;
2226
2227 ob_dot_class = PyObject_GetAttr(ob, __class___str);
Jeremy Hyltonf00368f2003-06-16 20:19:49 +00002228 if (ob_dot_class == NULL) {
2229 if (PyErr_ExceptionMatches(
2230 PyExc_AttributeError))
2231 PyErr_Clear();
2232 else
2233 return -1;
2234 }
Tim Peters71fcda52003-02-14 23:05:28 +00002235 i = ob_dot_class != cls; /* true iff a problem */
2236 Py_XDECREF(ob_dot_class);
2237 if (i) {
2238 PyErr_SetString(PicklingError, "args[0] from "
2239 "__newobj__ args has the wrong class");
2240 return -1;
2241 }
2242 }
2243
2244 /* Save the class and its __new__ arguments. */
2245 if (save(self, cls, 0) < 0)
2246 return -1;
2247
2248 newargtup = PyTuple_New(n-1); /* argtup[1:] */
2249 if (newargtup == NULL)
2250 return -1;
2251 for (i = 1; i < n; ++i) {
2252 PyObject *temp = PyTuple_GET_ITEM(argtup, i);
2253 Py_INCREF(temp);
2254 PyTuple_SET_ITEM(newargtup, i-1, temp);
2255 }
Neal Norwitz5a29dd32007-10-05 05:01:38 +00002256 i = save(self, newargtup, 0);
Tim Peters71fcda52003-02-14 23:05:28 +00002257 Py_DECREF(newargtup);
2258 if (i < 0)
2259 return -1;
2260
2261 /* Add NEWOBJ opcode. */
2262 if (self->write_func(self, &newobj, 1) < 0)
2263 return -1;
2264 }
2265 else {
2266 /* Not using NEWOBJ. */
2267 if (save(self, callable, 0) < 0 ||
2268 save(self, argtup, 0) < 0 ||
2269 self->write_func(self, &reduce, 1) < 0)
2270 return -1;
2271 }
2272
2273 /* Memoize. */
2274 /* XXX How can ob be NULL? */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002275 if (ob != NULL) {
2276 if (state && !PyDict_Check(state)) {
2277 if (put2(self, ob) < 0)
2278 return -1;
2279 }
Tim Peters71fcda52003-02-14 23:05:28 +00002280 else if (put(self, ob) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002281 return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002282 }
Tim Peters84e87f32001-03-17 04:50:51 +00002283
Guido van Rossum60456fd1997-04-09 17:36:32 +00002284
Tim Peters71fcda52003-02-14 23:05:28 +00002285 if (listitems && batch_list(self, listitems) < 0)
2286 return -1;
2287
2288 if (dictitems && batch_dict(self, dictitems) < 0)
2289 return -1;
2290
2291 if (state) {
2292 if (save(self, state, 0) < 0 ||
2293 self->write_func(self, &build, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002294 return -1;
2295 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002296
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002297 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002298}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002299
Guido van Rossum60456fd1997-04-09 17:36:32 +00002300static int
Tim Peters1d63c9f2003-02-02 20:29:39 +00002301save(Picklerobject *self, PyObject *args, int pers_save)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002302{
2303 PyTypeObject *type;
Tim Peters71fcda52003-02-14 23:05:28 +00002304 PyObject *py_ob_id = 0, *__reduce__ = 0, *t = 0;
2305 PyObject *arg_tup;
2306 int res = -1;
2307 int tmp, size;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002308
Martin v. Löwis5a395302002-08-04 08:20:23 +00002309 if (self->nesting++ > Py_GetRecursionLimit()){
2310 PyErr_SetString(PyExc_RuntimeError,
2311 "maximum recursion depth exceeded");
2312 goto finally;
2313 }
2314
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002315 if (!pers_save && self->pers_func) {
2316 if ((tmp = save_pers(self, args, self->pers_func)) != 0) {
2317 res = tmp;
2318 goto finally;
2319 }
2320 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002321
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002322 if (args == Py_None) {
2323 res = save_none(self, args);
2324 goto finally;
2325 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002326
Christian Heimese93237d2007-12-19 02:37:44 +00002327 type = Py_TYPE(args);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002328
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002329 switch (type->tp_name[0]) {
Guido van Rossum77f6a652002-04-03 22:41:51 +00002330 case 'b':
2331 if (args == Py_False || args == Py_True) {
2332 res = save_bool(self, args);
2333 goto finally;
2334 }
2335 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002336 case 'i':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002337 if (type == &PyInt_Type) {
2338 res = save_int(self, args);
2339 goto finally;
2340 }
2341 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002342
Guido van Rossum60456fd1997-04-09 17:36:32 +00002343 case 'l':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002344 if (type == &PyLong_Type) {
2345 res = save_long(self, args);
2346 goto finally;
2347 }
2348 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002349
Guido van Rossum60456fd1997-04-09 17:36:32 +00002350 case 'f':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002351 if (type == &PyFloat_Type) {
2352 res = save_float(self, args);
2353 goto finally;
2354 }
2355 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002356
Guido van Rossum60456fd1997-04-09 17:36:32 +00002357 case 't':
Tim Peters1d63c9f2003-02-02 20:29:39 +00002358 if (type == &PyTuple_Type && PyTuple_Size(args) == 0) {
2359 res = save_tuple(self, args);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002360 goto finally;
2361 }
2362 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002363
Guido van Rossum60456fd1997-04-09 17:36:32 +00002364 case 's':
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002365 if ((type == &PyString_Type) && (PyString_GET_SIZE(args) < 2)) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002366 res = save_string(self, args, 0);
2367 goto finally;
2368 }
Facundo Batista14618862008-06-22 15:27:10 +00002369 break;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00002370
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002371#ifdef Py_USING_UNICODE
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00002372 case 'u':
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002373 if ((type == &PyUnicode_Type) && (PyString_GET_SIZE(args) < 2)) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002374 res = save_unicode(self, args, 0);
2375 goto finally;
2376 }
Facundo Batista14618862008-06-22 15:27:10 +00002377 break;
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002378#endif
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002379 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002380
Christian Heimese93237d2007-12-19 02:37:44 +00002381 if (Py_REFCNT(args) > 1) {
Tim Peterscba30e22003-02-01 06:24:36 +00002382 if (!( py_ob_id = PyLong_FromVoidPtr(args)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002383 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002384
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002385 if (PyDict_GetItem(self->memo, py_ob_id)) {
2386 if (get(self, py_ob_id) < 0)
2387 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002388
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002389 res = 0;
2390 goto finally;
2391 }
2392 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002393
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002394 switch (type->tp_name[0]) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00002395 case 's':
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002396 if (type == &PyString_Type) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002397 res = save_string(self, args, 1);
2398 goto finally;
2399 }
2400 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002401
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002402#ifdef Py_USING_UNICODE
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00002403 case 'u':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002404 if (type == &PyUnicode_Type) {
2405 res = save_unicode(self, args, 1);
2406 goto finally;
2407 }
2408 break;
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002409#endif
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00002410
Guido van Rossum60456fd1997-04-09 17:36:32 +00002411 case 't':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002412 if (type == &PyTuple_Type) {
2413 res = save_tuple(self, args);
2414 goto finally;
2415 }
2416 if (type == &PyType_Type) {
2417 res = save_global(self, args, NULL);
2418 goto finally;
2419 }
2420 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002421
Guido van Rossum60456fd1997-04-09 17:36:32 +00002422 case 'l':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002423 if (type == &PyList_Type) {
2424 res = save_list(self, args);
2425 goto finally;
2426 }
2427 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002428
2429 case 'd':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002430 if (type == &PyDict_Type) {
2431 res = save_dict(self, args);
2432 goto finally;
2433 }
2434 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002435
2436 case 'i':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002437 if (type == &PyInstance_Type) {
2438 res = save_inst(self, args);
2439 goto finally;
2440 }
2441 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002442
2443 case 'c':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002444 if (type == &PyClass_Type) {
2445 res = save_global(self, args, NULL);
2446 goto finally;
2447 }
2448 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002449
2450 case 'f':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002451 if (type == &PyFunction_Type) {
2452 res = save_global(self, args, NULL);
Christian Tismer2460c622004-02-26 16:21:45 +00002453 if (res && PyErr_ExceptionMatches(PickleError)) {
2454 /* fall back to reduce */
2455 PyErr_Clear();
2456 break;
2457 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002458 goto finally;
2459 }
2460 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002461
2462 case 'b':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002463 if (type == &PyCFunction_Type) {
2464 res = save_global(self, args, NULL);
2465 goto finally;
2466 }
2467 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002468
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002469 if (!pers_save && self->inst_pers_func) {
2470 if ((tmp = save_pers(self, args, self->inst_pers_func)) != 0) {
2471 res = tmp;
2472 goto finally;
2473 }
2474 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002475
Jeremy Hylton39c61162002-07-16 19:47:43 +00002476 if (PyType_IsSubtype(type, &PyType_Type)) {
2477 res = save_global(self, args, NULL);
2478 goto finally;
2479 }
2480
Guido van Rossumb289b872003-02-19 01:45:13 +00002481 /* Get a reduction callable, and call it. This may come from
Georg Brandldffbf5f2008-05-20 07:49:57 +00002482 * copy_reg.dispatch_table, the object's __reduce_ex__ method,
Guido van Rossumb289b872003-02-19 01:45:13 +00002483 * or the object's __reduce__ method.
Tim Peters71fcda52003-02-14 23:05:28 +00002484 */
Tim Peters5aa3da62003-02-13 21:03:57 +00002485 __reduce__ = PyDict_GetItem(dispatch_table, (PyObject *)type);
2486 if (__reduce__ != NULL) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002487 Py_INCREF(__reduce__);
Guido van Rossumb289b872003-02-19 01:45:13 +00002488 Py_INCREF(args);
2489 ARG_TUP(self, args);
2490 if (self->arg) {
2491 t = PyObject_Call(__reduce__, self->arg, NULL);
2492 FREE_ARG_TUP(self);
2493 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002494 }
2495 else {
Guido van Rossumb289b872003-02-19 01:45:13 +00002496 /* Check for a __reduce_ex__ method. */
2497 __reduce__ = PyObject_GetAttr(args, __reduce_ex___str);
2498 if (__reduce__ != NULL) {
2499 t = PyInt_FromLong(self->proto);
2500 if (t != NULL) {
2501 ARG_TUP(self, t);
2502 t = NULL;
2503 if (self->arg) {
2504 t = PyObject_Call(__reduce__,
2505 self->arg, NULL);
2506 FREE_ARG_TUP(self);
2507 }
2508 }
2509 }
2510 else {
Jeremy Hyltonf00368f2003-06-16 20:19:49 +00002511 if (PyErr_ExceptionMatches(PyExc_AttributeError))
2512 PyErr_Clear();
2513 else
2514 goto finally;
Guido van Rossumb289b872003-02-19 01:45:13 +00002515 /* Check for a __reduce__ method. */
2516 __reduce__ = PyObject_GetAttr(args, __reduce___str);
2517 if (__reduce__ != NULL) {
2518 t = PyObject_Call(__reduce__,
2519 empty_tuple, NULL);
2520 }
2521 else {
2522 PyErr_SetObject(UnpickleableError, args);
2523 goto finally;
2524 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002525 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002526 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002527
Tim Peters71fcda52003-02-14 23:05:28 +00002528 if (t == NULL)
2529 goto finally;
Tim Peters84e87f32001-03-17 04:50:51 +00002530
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002531 if (PyString_Check(t)) {
Tim Peters71fcda52003-02-14 23:05:28 +00002532 res = save_global(self, args, t);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002533 goto finally;
2534 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002535
Tim Peters71fcda52003-02-14 23:05:28 +00002536 if (! PyTuple_Check(t)) {
2537 cPickle_ErrFormat(PicklingError, "Value returned by "
2538 "%s must be string or tuple",
2539 "O", __reduce__);
2540 goto finally;
2541 }
2542
2543 size = PyTuple_Size(t);
2544 if (size < 2 || size > 5) {
2545 cPickle_ErrFormat(PicklingError, "tuple returned by "
2546 "%s must contain 2 through 5 elements",
2547 "O", __reduce__);
2548 goto finally;
2549 }
2550
2551 arg_tup = PyTuple_GET_ITEM(t, 1);
2552 if (!(PyTuple_Check(arg_tup) || arg_tup == Py_None)) {
2553 cPickle_ErrFormat(PicklingError, "Second element of "
2554 "tuple returned by %s must be a tuple",
2555 "O", __reduce__);
2556 goto finally;
2557 }
2558
2559 res = save_reduce(self, t, args);
Guido van Rossum60456fd1997-04-09 17:36:32 +00002560
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002561 finally:
Martin v. Löwis5a395302002-08-04 08:20:23 +00002562 self->nesting--;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002563 Py_XDECREF(py_ob_id);
2564 Py_XDECREF(__reduce__);
2565 Py_XDECREF(t);
Tim Peters84e87f32001-03-17 04:50:51 +00002566
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002567 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002568}
2569
2570
2571static int
Tim Peterscba30e22003-02-01 06:24:36 +00002572dump(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002573{
2574 static char stop = STOP;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002575
Tim Peters4190fb82003-02-02 16:09:05 +00002576 if (self->proto >= 2) {
2577 char bytes[2];
2578
2579 bytes[0] = PROTO;
Tim Peters87482ea2003-02-02 16:16:30 +00002580 assert(self->proto >= 0 && self->proto < 256);
2581 bytes[1] = (char)self->proto;
Tim Peters4190fb82003-02-02 16:09:05 +00002582 if (self->write_func(self, bytes, 2) < 0)
2583 return -1;
2584 }
2585
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002586 if (save(self, args, 0) < 0)
2587 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002588
Tim Peters4190fb82003-02-02 16:09:05 +00002589 if (self->write_func(self, &stop, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002590 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002591
Tim Peters4190fb82003-02-02 16:09:05 +00002592 if (self->write_func(self, NULL, 0) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002593 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002594
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002595 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002596}
2597
2598static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00002599Pickle_clear_memo(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002600{
Tim Peterscba30e22003-02-01 06:24:36 +00002601 if (self->memo)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002602 PyDict_Clear(self->memo);
2603 Py_INCREF(Py_None);
2604 return Py_None;
2605}
2606
2607static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00002608Pickle_getvalue(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002609{
2610 int l, i, rsize, ssize, clear=1, lm;
2611 long ik;
2612 PyObject *k, *r;
2613 char *s, *p, *have_get;
2614 Pdata *data;
2615
2616 /* Can be called by Python code or C code */
Tim Peterscba30e22003-02-01 06:24:36 +00002617 if (args && !PyArg_ParseTuple(args, "|i:getvalue", &clear))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002618 return NULL;
2619
2620 /* Check to make sure we are based on a list */
2621 if (! Pdata_Check(self->file)) {
2622 PyErr_SetString(PicklingError,
2623 "Attempt to getvalue() a non-list-based pickler");
2624 return NULL;
2625 }
2626
2627 /* flush write buffer */
2628 if (write_other(self, NULL, 0) < 0) return NULL;
2629
2630 data=(Pdata*)self->file;
2631 l=data->length;
2632
2633 /* set up an array to hold get/put status */
Tim Petersac5687a2003-02-02 18:08:34 +00002634 lm = PyDict_Size(self->memo);
2635 if (lm < 0) return NULL;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002636 lm++;
Tim Petersac5687a2003-02-02 18:08:34 +00002637 have_get = malloc(lm);
2638 if (have_get == NULL) return PyErr_NoMemory();
2639 memset(have_get, 0, lm);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002640
2641 /* Scan for gets. */
Tim Petersac5687a2003-02-02 18:08:34 +00002642 for (rsize = 0, i = l; --i >= 0; ) {
2643 k = data->data[i];
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002644
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002645 if (PyString_Check(k))
2646 rsize += PyString_GET_SIZE(k);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002647
2648 else if (PyInt_Check(k)) { /* put */
Tim Petersac5687a2003-02-02 18:08:34 +00002649 ik = PyInt_AS_LONG((PyIntObject*)k);
2650 if (ik >= lm || ik == 0) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002651 PyErr_SetString(PicklingError,
2652 "Invalid get data");
Neal Norwitz98a96002006-07-23 07:57:11 +00002653 goto err;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002654 }
Tim Petersac5687a2003-02-02 18:08:34 +00002655 if (have_get[ik]) /* with matching get */
2656 rsize += ik < 256 ? 2 : 5;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002657 }
2658
2659 else if (! (PyTuple_Check(k) &&
2660 PyTuple_GET_SIZE(k) == 2 &&
Tim Petersac5687a2003-02-02 18:08:34 +00002661 PyInt_Check((k = PyTuple_GET_ITEM(k, 0))))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002662 ) {
2663 PyErr_SetString(PicklingError,
2664 "Unexpected data in internal list");
Neal Norwitz98a96002006-07-23 07:57:11 +00002665 goto err;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002666 }
2667
2668 else { /* put */
Tim Petersac5687a2003-02-02 18:08:34 +00002669 ik = PyInt_AS_LONG((PyIntObject *)k);
2670 if (ik >= lm || ik == 0) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002671 PyErr_SetString(PicklingError,
2672 "Invalid get data");
2673 return NULL;
2674 }
Tim Petersac5687a2003-02-02 18:08:34 +00002675 have_get[ik] = 1;
2676 rsize += ik < 256 ? 2 : 5;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002677 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002678 }
2679
2680 /* Now generate the result */
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002681 r = PyString_FromStringAndSize(NULL, rsize);
Tim Petersac5687a2003-02-02 18:08:34 +00002682 if (r == NULL) goto err;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002683 s = PyString_AS_STRING((PyStringObject *)r);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002684
Tim Petersac5687a2003-02-02 18:08:34 +00002685 for (i = 0; i < l; i++) {
2686 k = data->data[i];
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002687
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002688 if (PyString_Check(k)) {
2689 ssize = PyString_GET_SIZE(k);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002690 if (ssize) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002691 p=PyString_AS_STRING((PyStringObject *)k);
Tim Petersac5687a2003-02-02 18:08:34 +00002692 while (--ssize >= 0)
2693 *s++ = *p++;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002694 }
2695 }
2696
2697 else if (PyTuple_Check(k)) { /* get */
Tim Petersac5687a2003-02-02 18:08:34 +00002698 ik = PyInt_AS_LONG((PyIntObject *)
2699 PyTuple_GET_ITEM(k, 0));
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002700 if (ik < 256) {
2701 *s++ = BINGET;
2702 *s++ = (int)(ik & 0xff);
2703 }
2704 else {
2705 *s++ = LONG_BINGET;
2706 *s++ = (int)(ik & 0xff);
2707 *s++ = (int)((ik >> 8) & 0xff);
2708 *s++ = (int)((ik >> 16) & 0xff);
2709 *s++ = (int)((ik >> 24) & 0xff);
2710 }
2711 }
2712
2713 else { /* put */
Tim Petersac5687a2003-02-02 18:08:34 +00002714 ik = PyInt_AS_LONG((PyIntObject*)k);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002715
2716 if (have_get[ik]) { /* with matching get */
2717 if (ik < 256) {
2718 *s++ = BINPUT;
2719 *s++ = (int)(ik & 0xff);
2720 }
2721 else {
2722 *s++ = LONG_BINPUT;
2723 *s++ = (int)(ik & 0xff);
2724 *s++ = (int)((ik >> 8) & 0xff);
2725 *s++ = (int)((ik >> 16) & 0xff);
2726 *s++ = (int)((ik >> 24) & 0xff);
2727 }
2728 }
2729 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002730 }
2731
2732 if (clear) {
2733 PyDict_Clear(self->memo);
Tim Petersac5687a2003-02-02 18:08:34 +00002734 Pdata_clear(data, 0);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002735 }
2736
2737 free(have_get);
2738 return r;
2739 err:
2740 free(have_get);
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00002741 return NULL;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002742}
2743
2744static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00002745Pickler_dump(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002746{
2747 PyObject *ob;
2748 int get=0;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002749
Tim Peterscba30e22003-02-01 06:24:36 +00002750 if (!( PyArg_ParseTuple(args, "O|i:dump", &ob, &get)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002751 return NULL;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002752
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002753 if (dump(self, ob) < 0)
2754 return NULL;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002755
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002756 if (get) return Pickle_getvalue(self, NULL);
Guido van Rossum053b8df1998-11-25 16:18:00 +00002757
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002758 /* XXX Why does dump() return self? */
2759 Py_INCREF(self);
2760 return (PyObject*)self;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002761}
2762
2763
Tim Peterscba30e22003-02-01 06:24:36 +00002764static struct PyMethodDef Pickler_methods[] =
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002765{
Neal Norwitzb0493252002-03-31 14:44:22 +00002766 {"dump", (PyCFunction)Pickler_dump, METH_VARARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00002767 PyDoc_STR("dump(object) -- "
2768 "Write an object in pickle format to the object's pickle stream")},
Fred Drake0ebacc82002-05-01 20:36:39 +00002769 {"clear_memo", (PyCFunction)Pickle_clear_memo, METH_NOARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00002770 PyDoc_STR("clear_memo() -- Clear the picklers memo")},
Neal Norwitzb0493252002-03-31 14:44:22 +00002771 {"getvalue", (PyCFunction)Pickle_getvalue, METH_VARARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00002772 PyDoc_STR("getvalue() -- Finish picking a list-based pickle")},
Guido van Rossum60456fd1997-04-09 17:36:32 +00002773 {NULL, NULL} /* sentinel */
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002774};
2775
2776
2777static Picklerobject *
Tim Peters5bd2a792003-02-01 16:45:06 +00002778newPicklerobject(PyObject *file, int proto)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002779{
2780 Picklerobject *self;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002781
Tim Peters5bd2a792003-02-01 16:45:06 +00002782 if (proto < 0)
Tim Peters8587b3c2003-02-13 15:44:41 +00002783 proto = HIGHEST_PROTOCOL;
2784 if (proto > HIGHEST_PROTOCOL) {
Tim Peters5bd2a792003-02-01 16:45:06 +00002785 PyErr_Format(PyExc_ValueError, "pickle protocol %d asked for; "
2786 "the highest available protocol is %d",
Tim Peters8587b3c2003-02-13 15:44:41 +00002787 proto, HIGHEST_PROTOCOL);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002788 return NULL;
Tim Peters5bd2a792003-02-01 16:45:06 +00002789 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002790
Jeremy Hylton4cf63192003-04-09 21:05:12 +00002791 self = PyObject_GC_New(Picklerobject, &Picklertype);
Tim Peters5bd2a792003-02-01 16:45:06 +00002792 if (self == NULL)
2793 return NULL;
2794 self->proto = proto;
2795 self->bin = proto > 0;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002796 self->fp = NULL;
2797 self->write = NULL;
2798 self->memo = NULL;
2799 self->arg = NULL;
2800 self->pers_func = NULL;
2801 self->inst_pers_func = NULL;
2802 self->write_buf = NULL;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002803 self->fast = 0;
Martin v. Löwis5a395302002-08-04 08:20:23 +00002804 self->nesting = 0;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002805 self->fast_container = 0;
2806 self->fast_memo = NULL;
2807 self->buf_size = 0;
2808 self->dispatch_table = NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002809
Tim Peters5bd2a792003-02-01 16:45:06 +00002810 self->file = NULL;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002811 if (file)
2812 Py_INCREF(file);
Tim Peters5bd2a792003-02-01 16:45:06 +00002813 else {
2814 file = Pdata_New();
2815 if (file == NULL)
2816 goto err;
2817 }
2818 self->file = file;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002819
Tim Peterscba30e22003-02-01 06:24:36 +00002820 if (!( self->memo = PyDict_New()))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002821 goto err;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002822
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002823 if (PyFile_Check(file)) {
2824 self->fp = PyFile_AsFile(file);
2825 if (self->fp == NULL) {
Tim Peters5bd2a792003-02-01 16:45:06 +00002826 PyErr_SetString(PyExc_ValueError,
2827 "I/O operation on closed file");
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002828 goto err;
2829 }
2830 self->write_func = write_file;
Guido van Rossumc91fcaa1999-03-29 20:00:14 +00002831 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002832 else if (PycStringIO_OutputCheck(file)) {
2833 self->write_func = write_cStringIO;
2834 }
2835 else if (file == Py_None) {
2836 self->write_func = write_none;
2837 }
2838 else {
2839 self->write_func = write_other;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002840
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002841 if (! Pdata_Check(file)) {
2842 self->write = PyObject_GetAttr(file, write_str);
2843 if (!self->write) {
2844 PyErr_Clear();
Tim Peterscba30e22003-02-01 06:24:36 +00002845 PyErr_SetString(PyExc_TypeError,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002846 "argument must have 'write' "
2847 "attribute");
2848 goto err;
2849 }
2850 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002851
Tim Peters5bd2a792003-02-01 16:45:06 +00002852 self->write_buf = (char *)PyMem_Malloc(WRITE_BUF_SIZE);
2853 if (self->write_buf == NULL) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002854 PyErr_NoMemory();
2855 goto err;
2856 }
2857 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002858
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002859 if (PyEval_GetRestricted()) {
2860 /* Restricted execution, get private tables */
Alexandre Vassalotti9510e4a2008-05-11 08:25:28 +00002861 PyObject *m = PyImport_Import(copyreg_str);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002862
Tim Peters5b7da392003-02-04 00:21:07 +00002863 if (m == NULL)
2864 goto err;
2865 self->dispatch_table = PyObject_GetAttr(m, dispatch_table_str);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002866 Py_DECREF(m);
Tim Peters5b7da392003-02-04 00:21:07 +00002867 if (self->dispatch_table == NULL)
2868 goto err;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002869 }
2870 else {
Tim Peters5b7da392003-02-04 00:21:07 +00002871 self->dispatch_table = dispatch_table;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002872 Py_INCREF(dispatch_table);
2873 }
Jeremy Hylton4cf63192003-04-09 21:05:12 +00002874 PyObject_GC_Track(self);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002875
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002876 return self;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002877
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002878 err:
Tim Peters5bd2a792003-02-01 16:45:06 +00002879 Py_DECREF(self);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002880 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002881}
2882
2883
2884static PyObject *
Martin v. Löwis544f1192004-07-27 05:22:33 +00002885get_Pickler(PyObject *self, PyObject *args, PyObject *kwds)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002886{
Martin v. Löwis15e62742006-02-27 16:46:16 +00002887 static char *kwlist[] = {"file", "protocol", NULL};
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002888 PyObject *file = NULL;
Tim Peters5bd2a792003-02-01 16:45:06 +00002889 int proto = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002890
Tim Peters92c8bb32003-02-13 23:00:26 +00002891 /* XXX
Martin v. Löwis544f1192004-07-27 05:22:33 +00002892 * The documented signature is Pickler(file, protocol=0), but this
Tim Peters92c8bb32003-02-13 23:00:26 +00002893 * accepts Pickler() and Pickler(integer) too. The meaning then
2894 * is clear as mud, undocumented, and not supported by pickle.py.
2895 * I'm told Zope uses this, but I haven't traced into this code
2896 * far enough to figure out what it means.
Tim Peters5bd2a792003-02-01 16:45:06 +00002897 */
2898 if (!PyArg_ParseTuple(args, "|i:Pickler", &proto)) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002899 PyErr_Clear();
Tim Peters5bd2a792003-02-01 16:45:06 +00002900 proto = 0;
Martin v. Löwis544f1192004-07-27 05:22:33 +00002901 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|i:Pickler",
2902 kwlist, &file, &proto))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002903 return NULL;
2904 }
Tim Peters5bd2a792003-02-01 16:45:06 +00002905 return (PyObject *)newPicklerobject(file, proto);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002906}
2907
2908
2909static void
Tim Peterscba30e22003-02-01 06:24:36 +00002910Pickler_dealloc(Picklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002911{
Jeremy Hylton4cf63192003-04-09 21:05:12 +00002912 PyObject_GC_UnTrack(self);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002913 Py_XDECREF(self->write);
2914 Py_XDECREF(self->memo);
2915 Py_XDECREF(self->fast_memo);
2916 Py_XDECREF(self->arg);
2917 Py_XDECREF(self->file);
2918 Py_XDECREF(self->pers_func);
2919 Py_XDECREF(self->inst_pers_func);
2920 Py_XDECREF(self->dispatch_table);
Tim Peters5bd2a792003-02-01 16:45:06 +00002921 PyMem_Free(self->write_buf);
Christian Heimese93237d2007-12-19 02:37:44 +00002922 Py_TYPE(self)->tp_free((PyObject *)self);
Jeremy Hylton4cf63192003-04-09 21:05:12 +00002923}
2924
2925static int
2926Pickler_traverse(Picklerobject *self, visitproc visit, void *arg)
2927{
Thomas Woutersc6e55062006-04-15 21:47:09 +00002928 Py_VISIT(self->write);
2929 Py_VISIT(self->memo);
2930 Py_VISIT(self->fast_memo);
2931 Py_VISIT(self->arg);
2932 Py_VISIT(self->file);
2933 Py_VISIT(self->pers_func);
2934 Py_VISIT(self->inst_pers_func);
2935 Py_VISIT(self->dispatch_table);
Jeremy Hylton4cf63192003-04-09 21:05:12 +00002936 return 0;
2937}
2938
2939static int
2940Pickler_clear(Picklerobject *self)
2941{
Thomas Woutersedf17d82006-04-15 17:28:34 +00002942 Py_CLEAR(self->write);
2943 Py_CLEAR(self->memo);
2944 Py_CLEAR(self->fast_memo);
2945 Py_CLEAR(self->arg);
2946 Py_CLEAR(self->file);
2947 Py_CLEAR(self->pers_func);
2948 Py_CLEAR(self->inst_pers_func);
2949 Py_CLEAR(self->dispatch_table);
Jeremy Hylton4cf63192003-04-09 21:05:12 +00002950 return 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002951}
2952
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002953static PyObject *
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00002954Pickler_get_pers_func(Picklerobject *p)
2955{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002956 if (p->pers_func == NULL)
2957 PyErr_SetString(PyExc_AttributeError, "persistent_id");
2958 else
2959 Py_INCREF(p->pers_func);
2960 return p->pers_func;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002961}
2962
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00002963static int
2964Pickler_set_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->pers_func);
2972 Py_INCREF(v);
2973 p->pers_func = v;
2974 return 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002975}
2976
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00002977static int
2978Pickler_set_inst_pers_func(Picklerobject *p, PyObject *v)
2979{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002980 if (v == NULL) {
2981 PyErr_SetString(PyExc_TypeError,
2982 "attribute deletion is not supported");
2983 return -1;
2984 }
2985 Py_XDECREF(p->inst_pers_func);
2986 Py_INCREF(v);
2987 p->inst_pers_func = v;
2988 return 0;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00002989}
2990
2991static PyObject *
2992Pickler_get_memo(Picklerobject *p)
2993{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002994 if (p->memo == NULL)
2995 PyErr_SetString(PyExc_AttributeError, "memo");
2996 else
2997 Py_INCREF(p->memo);
2998 return p->memo;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00002999}
3000
3001static int
3002Pickler_set_memo(Picklerobject *p, PyObject *v)
3003{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003004 if (v == NULL) {
3005 PyErr_SetString(PyExc_TypeError,
3006 "attribute deletion is not supported");
3007 return -1;
3008 }
3009 if (!PyDict_Check(v)) {
3010 PyErr_SetString(PyExc_TypeError, "memo must be a dictionary");
3011 return -1;
3012 }
3013 Py_XDECREF(p->memo);
3014 Py_INCREF(v);
3015 p->memo = v;
3016 return 0;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003017}
3018
3019static PyObject *
3020Pickler_get_error(Picklerobject *p)
3021{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003022 /* why is this an attribute on the Pickler? */
3023 Py_INCREF(PicklingError);
3024 return PicklingError;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003025}
3026
3027static PyMemberDef Pickler_members[] = {
3028 {"binary", T_INT, offsetof(Picklerobject, bin)},
3029 {"fast", T_INT, offsetof(Picklerobject, fast)},
Jeremy Hylton3eb46f32001-10-16 17:10:49 +00003030 {NULL}
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003031};
3032
3033static PyGetSetDef Pickler_getsets[] = {
Tim Peterscba30e22003-02-01 06:24:36 +00003034 {"persistent_id", (getter)Pickler_get_pers_func,
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003035 (setter)Pickler_set_pers_func},
3036 {"inst_persistent_id", NULL, (setter)Pickler_set_inst_pers_func},
3037 {"memo", (getter)Pickler_get_memo, (setter)Pickler_set_memo},
Jeremy Hylton3eb46f32001-10-16 17:10:49 +00003038 {"PicklingError", (getter)Pickler_get_error, NULL},
3039 {NULL}
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003040};
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003041
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003042PyDoc_STRVAR(Picklertype__doc__,
3043"Objects that know how to pickle objects\n");
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003044
Guido van Rossum9716aaa1997-12-08 15:15:16 +00003045static PyTypeObject Picklertype = {
Martin v. Löwis68192102007-07-21 06:55:02 +00003046 PyVarObject_HEAD_INIT(NULL, 0)
Guido van Rossum14648392001-12-08 18:02:58 +00003047 "cPickle.Pickler", /*tp_name*/
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003048 sizeof(Picklerobject), /*tp_basicsize*/
3049 0,
3050 (destructor)Pickler_dealloc, /* tp_dealloc */
3051 0, /* tp_print */
3052 0, /* tp_getattr */
3053 0, /* tp_setattr */
3054 0, /* tp_compare */
3055 0, /* tp_repr */
3056 0, /* tp_as_number */
3057 0, /* tp_as_sequence */
3058 0, /* tp_as_mapping */
3059 0, /* tp_hash */
3060 0, /* tp_call */
3061 0, /* tp_str */
Jason Tishlerfb8595d2003-01-06 12:41:26 +00003062 PyObject_GenericGetAttr, /* tp_getattro */
3063 PyObject_GenericSetAttr, /* tp_setattro */
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003064 0, /* tp_as_buffer */
Jeremy Hylton4cf63192003-04-09 21:05:12 +00003065 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003066 Picklertype__doc__, /* tp_doc */
Jeremy Hylton4cf63192003-04-09 21:05:12 +00003067 (traverseproc)Pickler_traverse, /* tp_traverse */
3068 (inquiry)Pickler_clear, /* tp_clear */
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003069 0, /* tp_richcompare */
3070 0, /* tp_weaklistoffset */
3071 0, /* tp_iter */
3072 0, /* tp_iternext */
3073 Pickler_methods, /* tp_methods */
3074 Pickler_members, /* tp_members */
3075 Pickler_getsets, /* tp_getset */
Guido van Rossum9716aaa1997-12-08 15:15:16 +00003076};
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003077
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003078static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00003079find_class(PyObject *py_module_name, PyObject *py_global_name, PyObject *fc)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003080{
3081 PyObject *global = 0, *module;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003082
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003083 if (fc) {
3084 if (fc==Py_None) {
Tim Peters92c8bb32003-02-13 23:00:26 +00003085 PyErr_SetString(UnpicklingError, "Global and instance "
3086 "pickles are not supported.");
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003087 return NULL;
3088 }
Georg Brandl684fd0c2006-05-25 19:15:31 +00003089 return PyObject_CallFunctionObjArgs(fc, py_module_name,
3090 py_global_name, NULL);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003091 }
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00003092
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003093 module = PySys_GetObject("modules");
3094 if (module == NULL)
3095 return NULL;
Jeremy Hyltond1055231998-08-11 19:52:51 +00003096
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003097 module = PyDict_GetItem(module, py_module_name);
3098 if (module == NULL) {
3099 module = PyImport_Import(py_module_name);
3100 if (!module)
3101 return NULL;
3102 global = PyObject_GetAttr(module, py_global_name);
3103 Py_DECREF(module);
3104 }
3105 else
3106 global = PyObject_GetAttr(module, py_global_name);
3107 return global;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003108}
3109
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003110static int
Tim Peterscba30e22003-02-01 06:24:36 +00003111marker(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003112{
3113 if (self->num_marks < 1) {
3114 PyErr_SetString(UnpicklingError, "could not find MARK");
3115 return -1;
3116 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003117
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003118 return self->marks[--self->num_marks];
Guido van Rossum60456fd1997-04-09 17:36:32 +00003119}
3120
Tim Peters84e87f32001-03-17 04:50:51 +00003121
Guido van Rossum60456fd1997-04-09 17:36:32 +00003122static int
Tim Peterscba30e22003-02-01 06:24:36 +00003123load_none(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003124{
3125 PDATA_APPEND(self->stack, Py_None, -1);
3126 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003127}
3128
Guido van Rossumf9ffb031999-02-04 14:54:04 +00003129static int
Tim Peterscba30e22003-02-01 06:24:36 +00003130bad_readline(void)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003131{
3132 PyErr_SetString(UnpicklingError, "pickle data was truncated");
3133 return -1;
Guido van Rossumf9ffb031999-02-04 14:54:04 +00003134}
Guido van Rossum60456fd1997-04-09 17:36:32 +00003135
3136static int
Tim Peterscba30e22003-02-01 06:24:36 +00003137load_int(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003138{
3139 PyObject *py_int = 0;
3140 char *endptr, *s;
3141 int len, res = -1;
3142 long l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003143
Tim Peters0bc93f52003-02-02 18:29:33 +00003144 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003145 if (len < 2) return bad_readline();
3146 if (!( s=pystrndup(s,len))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003147
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003148 errno = 0;
3149 l = strtol(s, &endptr, 0);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003150
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003151 if (errno || (*endptr != '\n') || (endptr[1] != '\0')) {
3152 /* Hm, maybe we've got something long. Let's try reading
3153 it as a Python long object. */
3154 errno = 0;
3155 py_int = PyLong_FromString(s, NULL, 0);
3156 if (py_int == NULL) {
3157 PyErr_SetString(PyExc_ValueError,
3158 "could not convert string to int");
3159 goto finally;
3160 }
3161 }
3162 else {
Guido van Rossume2763392002-04-05 19:30:08 +00003163 if (len == 3 && (l == 0 || l == 1)) {
3164 if (!( py_int = PyBool_FromLong(l))) goto finally;
3165 }
3166 else {
3167 if (!( py_int = PyInt_FromLong(l))) goto finally;
3168 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003169 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003170
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003171 free(s);
3172 PDATA_PUSH(self->stack, py_int, -1);
3173 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003174
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003175 finally:
3176 free(s);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003177
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003178 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003179}
3180
Tim Peters3c67d792003-02-02 17:59:11 +00003181static int
3182load_bool(Unpicklerobject *self, PyObject *boolean)
3183{
3184 assert(boolean == Py_True || boolean == Py_False);
Tim Peterse0a39072003-02-03 15:45:56 +00003185 PDATA_APPEND(self->stack, boolean, -1);
Tim Peters3c67d792003-02-02 17:59:11 +00003186 return 0;
3187}
3188
Tim Petersee1a53c2003-02-02 02:57:53 +00003189/* s contains x bytes of a little-endian integer. Return its value as a
3190 * C int. Obscure: when x is 1 or 2, this is an unsigned little-endian
3191 * int, but when x is 4 it's a signed one. This is an historical source
3192 * of x-platform bugs.
3193 */
Tim Peters84e87f32001-03-17 04:50:51 +00003194static long
Tim Petersee1a53c2003-02-02 02:57:53 +00003195calc_binint(char *s, int x)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003196{
3197 unsigned char c;
3198 int i;
3199 long l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003200
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003201 for (i = 0, l = 0L; i < x; i++) {
3202 c = (unsigned char)s[i];
3203 l |= (long)c << (i * 8);
3204 }
Tim Petersbfa18f72001-04-10 01:54:42 +00003205#if SIZEOF_LONG > 4
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003206 /* Unlike BININT1 and BININT2, BININT (more accurately BININT4)
3207 * is signed, so on a box with longs bigger than 4 bytes we need
3208 * to extend a BININT's sign bit to the full width.
3209 */
3210 if (x == 4 && l & (1L << 31))
3211 l |= (~0L) << 32;
Tim Petersbfa18f72001-04-10 01:54:42 +00003212#endif
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003213 return l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003214}
3215
3216
3217static int
Tim Peterscba30e22003-02-01 06:24:36 +00003218load_binintx(Unpicklerobject *self, char *s, int x)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003219{
3220 PyObject *py_int = 0;
3221 long l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003222
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003223 l = calc_binint(s, x);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003224
Tim Peterscba30e22003-02-01 06:24:36 +00003225 if (!( py_int = PyInt_FromLong(l)))
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 PDATA_PUSH(self->stack, py_int, -1);
3229 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003230}
3231
3232
3233static int
Tim Peterscba30e22003-02-01 06:24:36 +00003234load_binint(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003235{
3236 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003237
Tim Peters0bc93f52003-02-02 18:29:33 +00003238 if (self->read_func(self, &s, 4) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003239 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003240
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003241 return load_binintx(self, s, 4);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003242}
3243
3244
3245static int
Tim Peterscba30e22003-02-01 06:24:36 +00003246load_binint1(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003247{
3248 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003249
Tim Peters0bc93f52003-02-02 18:29:33 +00003250 if (self->read_func(self, &s, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003251 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003252
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003253 return load_binintx(self, s, 1);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003254}
3255
3256
3257static int
Tim Peterscba30e22003-02-01 06:24:36 +00003258load_binint2(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003259{
3260 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003261
Tim Peters0bc93f52003-02-02 18:29:33 +00003262 if (self->read_func(self, &s, 2) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003263 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003264
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003265 return load_binintx(self, s, 2);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003266}
Tim Peters84e87f32001-03-17 04:50:51 +00003267
Guido van Rossum60456fd1997-04-09 17:36:32 +00003268static int
Tim Peterscba30e22003-02-01 06:24:36 +00003269load_long(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003270{
3271 PyObject *l = 0;
3272 char *end, *s;
3273 int len, res = -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003274
Tim Peters0bc93f52003-02-02 18:29:33 +00003275 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003276 if (len < 2) return bad_readline();
3277 if (!( s=pystrndup(s,len))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003278
Tim Peterscba30e22003-02-01 06:24:36 +00003279 if (!( l = PyLong_FromString(s, &end, 0)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003280 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003281
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003282 free(s);
3283 PDATA_PUSH(self->stack, l, -1);
3284 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003285
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003286 finally:
3287 free(s);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003288
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003289 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003290}
3291
Tim Petersee1a53c2003-02-02 02:57:53 +00003292/* 'size' bytes contain the # of bytes of little-endian 256's-complement
3293 * data following.
3294 */
3295static int
3296load_counted_long(Unpicklerobject *self, int size)
3297{
Martin v. Löwis18e16552006-02-15 17:27:45 +00003298 Py_ssize_t i;
Tim Petersee1a53c2003-02-02 02:57:53 +00003299 char *nbytes;
3300 unsigned char *pdata;
3301 PyObject *along;
3302
3303 assert(size == 1 || size == 4);
3304 i = self->read_func(self, &nbytes, size);
3305 if (i < 0) return -1;
3306
3307 size = calc_binint(nbytes, size);
3308 if (size < 0) {
3309 /* Corrupt or hostile pickle -- we never write one like
3310 * this.
3311 */
Tim Peters0c7c48e2003-02-03 22:07:24 +00003312 PyErr_SetString(UnpicklingError, "LONG pickle has negative "
Tim Petersee1a53c2003-02-02 02:57:53 +00003313 "byte count");
3314 return -1;
3315 }
3316
3317 if (size == 0)
3318 along = PyLong_FromLong(0L);
3319 else {
3320 /* Read the raw little-endian bytes & convert. */
Neal Norwitz529baf22003-02-02 17:08:33 +00003321 i = self->read_func(self, (char **)&pdata, size);
Tim Petersee1a53c2003-02-02 02:57:53 +00003322 if (i < 0) return -1;
3323 along = _PyLong_FromByteArray(pdata, (size_t)size,
3324 1 /* little endian */, 1 /* signed */);
3325 }
3326 if (along == NULL)
3327 return -1;
3328 PDATA_PUSH(self->stack, along, -1);
3329 return 0;
3330}
Tim Peters84e87f32001-03-17 04:50:51 +00003331
Guido van Rossum60456fd1997-04-09 17:36:32 +00003332static int
Tim Peterscba30e22003-02-01 06:24:36 +00003333load_float(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003334{
3335 PyObject *py_float = 0;
3336 char *endptr, *s;
3337 int len, res = -1;
3338 double d;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003339
Tim Peters0bc93f52003-02-02 18:29:33 +00003340 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003341 if (len < 2) return bad_readline();
3342 if (!( s=pystrndup(s,len))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003343
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003344 errno = 0;
Martin v. Löwis737ea822004-06-08 18:52:54 +00003345 d = PyOS_ascii_strtod(s, &endptr);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003346
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003347 if (errno || (endptr[0] != '\n') || (endptr[1] != '\0')) {
3348 PyErr_SetString(PyExc_ValueError,
3349 "could not convert string to float");
3350 goto finally;
3351 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003352
Tim Peterscba30e22003-02-01 06:24:36 +00003353 if (!( py_float = PyFloat_FromDouble(d)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003354 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003355
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003356 free(s);
3357 PDATA_PUSH(self->stack, py_float, -1);
3358 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003359
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003360 finally:
3361 free(s);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003362
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003363 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003364}
3365
Guido van Rossum60456fd1997-04-09 17:36:32 +00003366static int
Tim Peterscba30e22003-02-01 06:24:36 +00003367load_binfloat(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003368{
Tim Peters9905b942003-03-20 20:53:32 +00003369 PyObject *py_float;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003370 double x;
3371 char *p;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003372
Tim Peters0bc93f52003-02-02 18:29:33 +00003373 if (self->read_func(self, &p, 8) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003374 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003375
Tim Peters9905b942003-03-20 20:53:32 +00003376 x = _PyFloat_Unpack8((unsigned char *)p, 0);
3377 if (x == -1.0 && PyErr_Occurred())
3378 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003379
Tim Peters9905b942003-03-20 20:53:32 +00003380 py_float = PyFloat_FromDouble(x);
3381 if (py_float == NULL)
3382 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003383
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003384 PDATA_PUSH(self->stack, py_float, -1);
3385 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003386}
Guido van Rossum60456fd1997-04-09 17:36:32 +00003387
3388static int
Tim Peterscba30e22003-02-01 06:24:36 +00003389load_string(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003390{
3391 PyObject *str = 0;
Martin v. Löwis8a8da792002-08-14 07:46:28 +00003392 int len, res = -1;
3393 char *s, *p;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003394
Tim Peters0bc93f52003-02-02 18:29:33 +00003395 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003396 if (len < 2) return bad_readline();
3397 if (!( s=pystrndup(s,len))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003398
Martin v. Löwis8a8da792002-08-14 07:46:28 +00003399
3400 /* Strip outermost quotes */
3401 while (s[len-1] <= ' ')
3402 len--;
3403 if(s[0]=='"' && s[len-1]=='"'){
3404 s[len-1] = '\0';
3405 p = s + 1 ;
3406 len -= 2;
3407 } else if(s[0]=='\'' && s[len-1]=='\''){
3408 s[len-1] = '\0';
3409 p = s + 1 ;
3410 len -= 2;
3411 } else
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003412 goto insecure;
3413 /********************************************/
Guido van Rossum9716aaa1997-12-08 15:15:16 +00003414
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003415 str = PyString_DecodeEscape(p, len, NULL, 0, NULL);
Neal Norwitz99dfe3c2006-08-02 04:27:11 +00003416 free(s);
Martin v. Löwis8a8da792002-08-14 07:46:28 +00003417 if (str) {
3418 PDATA_PUSH(self->stack, str, -1);
3419 res = 0;
3420 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003421 return res;
Tim Peters84e87f32001-03-17 04:50:51 +00003422
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003423 insecure:
3424 free(s);
3425 PyErr_SetString(PyExc_ValueError,"insecure string pickle");
3426 return -1;
Tim Peters84e87f32001-03-17 04:50:51 +00003427}
Guido van Rossum60456fd1997-04-09 17:36:32 +00003428
3429
3430static int
Tim Peterscba30e22003-02-01 06:24:36 +00003431load_binstring(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003432{
3433 PyObject *py_string = 0;
3434 long l;
3435 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003436
Tim Peters0bc93f52003-02-02 18:29:33 +00003437 if (self->read_func(self, &s, 4) < 0) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003438
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003439 l = calc_binint(s, 4);
Gregory P. Smith9d534572008-06-11 07:41:16 +00003440 if (l < 0) {
3441 /* Corrupt or hostile pickle -- we never write one like
3442 * this.
3443 */
3444 PyErr_SetString(UnpicklingError,
3445 "BINSTRING pickle has negative byte count");
3446 return -1;
3447 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003448
Tim Peters0bc93f52003-02-02 18:29:33 +00003449 if (self->read_func(self, &s, l) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003450 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003451
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003452 if (!( py_string = PyString_FromStringAndSize(s, l)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003453 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;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003457}
3458
3459
3460static int
Tim Peterscba30e22003-02-01 06:24:36 +00003461load_short_binstring(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003462{
3463 PyObject *py_string = 0;
3464 unsigned char l;
3465 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003466
Tim Peters0bc93f52003-02-02 18:29:33 +00003467 if (self->read_func(self, &s, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003468 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003469
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003470 l = (unsigned char)s[0];
Guido van Rossum60456fd1997-04-09 17:36:32 +00003471
Tim Peters0bc93f52003-02-02 18:29:33 +00003472 if (self->read_func(self, &s, l) < 0) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003473
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003474 if (!( py_string = PyString_FromStringAndSize(s, l))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003475
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003476 PDATA_PUSH(self->stack, py_string, -1);
3477 return 0;
Tim Peters84e87f32001-03-17 04:50:51 +00003478}
Guido van Rossum60456fd1997-04-09 17:36:32 +00003479
3480
Martin v. Löwis339d0f72001-08-17 18:39:25 +00003481#ifdef Py_USING_UNICODE
Guido van Rossum60456fd1997-04-09 17:36:32 +00003482static int
Tim Peterscba30e22003-02-01 06:24:36 +00003483load_unicode(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003484{
3485 PyObject *str = 0;
3486 int len, res = -1;
3487 char *s;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003488
Tim Peters0bc93f52003-02-02 18:29:33 +00003489 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003490 if (len < 1) return bad_readline();
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003491
Tim Peterscba30e22003-02-01 06:24:36 +00003492 if (!( str = PyUnicode_DecodeRawUnicodeEscape(s, len - 1, NULL)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003493 goto finally;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003494
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003495 PDATA_PUSH(self->stack, str, -1);
3496 return 0;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003497
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003498 finally:
3499 return res;
Tim Peters84e87f32001-03-17 04:50:51 +00003500}
Martin v. Löwis339d0f72001-08-17 18:39:25 +00003501#endif
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003502
3503
Martin v. Löwis339d0f72001-08-17 18:39:25 +00003504#ifdef Py_USING_UNICODE
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003505static int
Tim Peterscba30e22003-02-01 06:24:36 +00003506load_binunicode(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003507{
3508 PyObject *unicode;
3509 long l;
3510 char *s;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003511
Tim Peters0bc93f52003-02-02 18:29:33 +00003512 if (self->read_func(self, &s, 4) < 0) return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003513
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003514 l = calc_binint(s, 4);
Gregory P. Smith9d534572008-06-11 07:41:16 +00003515 if (l < 0) {
3516 /* Corrupt or hostile pickle -- we never write one like
3517 * this.
3518 */
3519 PyErr_SetString(UnpicklingError,
3520 "BINUNICODE pickle has negative byte count");
3521 return -1;
3522 }
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003523
Tim Peters0bc93f52003-02-02 18:29:33 +00003524 if (self->read_func(self, &s, l) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003525 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003526
Tim Peterscba30e22003-02-01 06:24:36 +00003527 if (!( unicode = PyUnicode_DecodeUTF8(s, l, NULL)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003528 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003529
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003530 PDATA_PUSH(self->stack, unicode, -1);
3531 return 0;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003532}
Martin v. Löwis339d0f72001-08-17 18:39:25 +00003533#endif
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003534
3535
3536static int
Tim Peterscba30e22003-02-01 06:24:36 +00003537load_tuple(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003538{
3539 PyObject *tup;
3540 int i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003541
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003542 if ((i = marker(self)) < 0) return -1;
3543 if (!( tup=Pdata_popTuple(self->stack, i))) return -1;
3544 PDATA_PUSH(self->stack, tup, -1);
3545 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003546}
3547
3548static int
Tim Peters1d63c9f2003-02-02 20:29:39 +00003549load_counted_tuple(Unpicklerobject *self, int len)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003550{
Tim Peters1d63c9f2003-02-02 20:29:39 +00003551 PyObject *tup = PyTuple_New(len);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003552
Tim Peters1d63c9f2003-02-02 20:29:39 +00003553 if (tup == NULL)
3554 return -1;
3555
3556 while (--len >= 0) {
3557 PyObject *element;
3558
3559 PDATA_POP(self->stack, element);
3560 if (element == NULL)
3561 return -1;
3562 PyTuple_SET_ITEM(tup, len, element);
3563 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003564 PDATA_PUSH(self->stack, tup, -1);
3565 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003566}
3567
3568static int
Tim Peterscba30e22003-02-01 06:24:36 +00003569load_empty_list(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003570{
3571 PyObject *list;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003572
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003573 if (!( list=PyList_New(0))) return -1;
3574 PDATA_PUSH(self->stack, list, -1);
3575 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003576}
3577
3578static int
Tim Peterscba30e22003-02-01 06:24:36 +00003579load_empty_dict(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003580{
3581 PyObject *dict;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003582
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003583 if (!( dict=PyDict_New())) return -1;
3584 PDATA_PUSH(self->stack, dict, -1);
3585 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003586}
3587
3588
3589static int
Tim Peterscba30e22003-02-01 06:24:36 +00003590load_list(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003591{
3592 PyObject *list = 0;
3593 int i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003594
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003595 if ((i = marker(self)) < 0) return -1;
3596 if (!( list=Pdata_popList(self->stack, i))) return -1;
3597 PDATA_PUSH(self->stack, list, -1);
3598 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003599}
3600
3601static int
Tim Peterscba30e22003-02-01 06:24:36 +00003602load_dict(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003603{
3604 PyObject *dict, *key, *value;
3605 int i, j, k;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003606
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003607 if ((i = marker(self)) < 0) return -1;
3608 j=self->stack->length;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003609
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003610 if (!( dict = PyDict_New())) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003611
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003612 for (k = i+1; k < j; k += 2) {
3613 key =self->stack->data[k-1];
3614 value=self->stack->data[k ];
3615 if (PyDict_SetItem(dict, key, value) < 0) {
3616 Py_DECREF(dict);
3617 return -1;
3618 }
3619 }
3620 Pdata_clear(self->stack, i);
3621 PDATA_PUSH(self->stack, dict, -1);
3622 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003623}
3624
3625static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00003626Instance_New(PyObject *cls, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003627{
Tim Peters1f1b2d22003-02-01 02:16:37 +00003628 PyObject *r = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003629
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003630 if (PyClass_Check(cls)) {
3631 int l;
Tim Peters84e87f32001-03-17 04:50:51 +00003632
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003633 if ((l=PyObject_Size(args)) < 0) goto err;
3634 if (!( l )) {
3635 PyObject *__getinitargs__;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003636
Tim Peterscba30e22003-02-01 06:24:36 +00003637 __getinitargs__ = PyObject_GetAttr(cls,
Jeremy Hyltonf00368f2003-06-16 20:19:49 +00003638 __getinitargs___str);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003639 if (!__getinitargs__) {
Tim Peterscba30e22003-02-01 06:24:36 +00003640 /* We have a class with no __getinitargs__,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003641 so bypass usual construction */
3642 PyObject *inst;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003643
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003644 PyErr_Clear();
Tim Peterscba30e22003-02-01 06:24:36 +00003645 if (!( inst=PyInstance_NewRaw(cls, NULL)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003646 goto err;
3647 return inst;
3648 }
3649 Py_DECREF(__getinitargs__);
3650 }
Tim Peters84e87f32001-03-17 04:50:51 +00003651
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003652 if ((r=PyInstance_New(cls, args, NULL))) return r;
3653 else goto err;
3654 }
Tim Peters84e87f32001-03-17 04:50:51 +00003655
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003656 if ((r=PyObject_CallObject(cls, args))) return r;
Guido van Rossum142eeb81997-08-13 03:14:41 +00003657
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003658 err:
3659 {
Neal Norwitz0f8b31a2006-06-28 06:28:31 +00003660 PyObject *tp, *v, *tb, *tmp_value;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003661
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003662 PyErr_Fetch(&tp, &v, &tb);
Neal Norwitz0f8b31a2006-06-28 06:28:31 +00003663 tmp_value = v;
3664 /* NULL occurs when there was a KeyboardInterrupt */
3665 if (tmp_value == NULL)
3666 tmp_value = Py_None;
3667 if ((r = PyTuple_Pack(3, tmp_value, cls, args))) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003668 Py_XDECREF(v);
3669 v=r;
3670 }
3671 PyErr_Restore(tp,v,tb);
3672 }
3673 return NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003674}
Tim Peters84e87f32001-03-17 04:50:51 +00003675
Guido van Rossum60456fd1997-04-09 17:36:32 +00003676
3677static int
Tim Peterscba30e22003-02-01 06:24:36 +00003678load_obj(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003679{
3680 PyObject *class, *tup, *obj=0;
3681 int i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003682
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003683 if ((i = marker(self)) < 0) return -1;
3684 if (!( tup=Pdata_popTuple(self->stack, i+1))) return -1;
3685 PDATA_POP(self->stack, class);
3686 if (class) {
3687 obj = Instance_New(class, tup);
3688 Py_DECREF(class);
3689 }
3690 Py_DECREF(tup);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003691
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003692 if (! obj) return -1;
3693 PDATA_PUSH(self->stack, obj, -1);
3694 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003695}
3696
3697
3698static int
Tim Peterscba30e22003-02-01 06:24:36 +00003699load_inst(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003700{
3701 PyObject *tup, *class=0, *obj=0, *module_name, *class_name;
3702 int i, len;
3703 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003704
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003705 if ((i = marker(self)) < 0) return -1;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003706
Tim Peters0bc93f52003-02-02 18:29:33 +00003707 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003708 if (len < 2) return bad_readline();
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003709 module_name = PyString_FromStringAndSize(s, len - 1);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003710 if (!module_name) return -1;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003711
Tim Peters0bc93f52003-02-02 18:29:33 +00003712 if ((len = self->readline_func(self, &s)) >= 0) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003713 if (len < 2) return bad_readline();
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003714 if ((class_name = PyString_FromStringAndSize(s, len - 1))) {
Tim Peterscba30e22003-02-01 06:24:36 +00003715 class = find_class(module_name, class_name,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003716 self->find_class);
3717 Py_DECREF(class_name);
3718 }
3719 }
3720 Py_DECREF(module_name);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003721
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003722 if (! class) return -1;
Tim Peters84e87f32001-03-17 04:50:51 +00003723
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003724 if ((tup=Pdata_popTuple(self->stack, i))) {
3725 obj = Instance_New(class, tup);
3726 Py_DECREF(tup);
3727 }
3728 Py_DECREF(class);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003729
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003730 if (! obj) return -1;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003731
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003732 PDATA_PUSH(self->stack, obj, -1);
3733 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003734}
3735
Tim Peterseab7db32003-02-13 18:24:14 +00003736static int
3737load_newobj(Unpicklerobject *self)
3738{
3739 PyObject *args = NULL;
3740 PyObject *clsraw = NULL;
3741 PyTypeObject *cls; /* clsraw cast to its true type */
3742 PyObject *obj;
3743
3744 /* Stack is ... cls argtuple, and we want to call
3745 * cls.__new__(cls, *argtuple).
3746 */
3747 PDATA_POP(self->stack, args);
3748 if (args == NULL) goto Fail;
3749 if (! PyTuple_Check(args)) {
3750 PyErr_SetString(UnpicklingError, "NEWOBJ expected an arg "
3751 "tuple.");
3752 goto Fail;
3753 }
3754
3755 PDATA_POP(self->stack, clsraw);
3756 cls = (PyTypeObject *)clsraw;
3757 if (cls == NULL) goto Fail;
3758 if (! PyType_Check(cls)) {
3759 PyErr_SetString(UnpicklingError, "NEWOBJ class argument "
3760 "isn't a type object");
3761 goto Fail;
3762 }
3763 if (cls->tp_new == NULL) {
3764 PyErr_SetString(UnpicklingError, "NEWOBJ class argument "
3765 "has NULL tp_new");
3766 goto Fail;
3767 }
3768
3769 /* Call __new__. */
3770 obj = cls->tp_new(cls, args, NULL);
3771 if (obj == NULL) goto Fail;
3772
3773 Py_DECREF(args);
3774 Py_DECREF(clsraw);
3775 PDATA_PUSH(self->stack, obj, -1);
3776 return 0;
3777
3778 Fail:
3779 Py_XDECREF(args);
3780 Py_XDECREF(clsraw);
3781 return -1;
3782}
Guido van Rossum60456fd1997-04-09 17:36:32 +00003783
3784static int
Tim Peterscba30e22003-02-01 06:24:36 +00003785load_global(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003786{
3787 PyObject *class = 0, *module_name = 0, *class_name = 0;
3788 int len;
3789 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003790
Tim Peters0bc93f52003-02-02 18:29:33 +00003791 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003792 if (len < 2) return bad_readline();
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003793 module_name = PyString_FromStringAndSize(s, len - 1);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003794 if (!module_name) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003795
Tim Peters0bc93f52003-02-02 18:29:33 +00003796 if ((len = self->readline_func(self, &s)) >= 0) {
Tim Peters4e52ca82002-12-07 02:43:28 +00003797 if (len < 2) {
3798 Py_DECREF(module_name);
3799 return bad_readline();
3800 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003801 if ((class_name = PyString_FromStringAndSize(s, len - 1))) {
Tim Peterscba30e22003-02-01 06:24:36 +00003802 class = find_class(module_name, class_name,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003803 self->find_class);
3804 Py_DECREF(class_name);
3805 }
3806 }
3807 Py_DECREF(module_name);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003808
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003809 if (! class) return -1;
3810 PDATA_PUSH(self->stack, class, -1);
3811 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003812}
3813
3814
3815static int
Tim Peterscba30e22003-02-01 06:24:36 +00003816load_persid(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003817{
3818 PyObject *pid = 0;
3819 int len;
3820 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003821
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003822 if (self->pers_func) {
Tim Peters0bc93f52003-02-02 18:29:33 +00003823 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003824 if (len < 2) return bad_readline();
3825
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003826 pid = PyString_FromStringAndSize(s, len - 1);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003827 if (!pid) return -1;
3828
3829 if (PyList_Check(self->pers_func)) {
3830 if (PyList_Append(self->pers_func, pid) < 0) {
3831 Py_DECREF(pid);
3832 return -1;
3833 }
3834 }
3835 else {
3836 ARG_TUP(self, pid);
3837 if (self->arg) {
Tim Peterscba30e22003-02-01 06:24:36 +00003838 pid = PyObject_Call(self->pers_func, self->arg,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003839 NULL);
3840 FREE_ARG_TUP(self);
3841 }
3842 }
3843
3844 if (! pid) return -1;
3845
3846 PDATA_PUSH(self->stack, pid, -1);
3847 return 0;
3848 }
3849 else {
3850 PyErr_SetString(UnpicklingError,
3851 "A load persistent id instruction was encountered,\n"
3852 "but no persistent_load function was specified.");
3853 return -1;
3854 }
3855}
3856
3857static int
Tim Peterscba30e22003-02-01 06:24:36 +00003858load_binpersid(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003859{
3860 PyObject *pid = 0;
3861
3862 if (self->pers_func) {
3863 PDATA_POP(self->stack, pid);
3864 if (! pid) return -1;
3865
3866 if (PyList_Check(self->pers_func)) {
3867 if (PyList_Append(self->pers_func, pid) < 0) {
3868 Py_DECREF(pid);
3869 return -1;
3870 }
3871 }
3872 else {
3873 ARG_TUP(self, pid);
3874 if (self->arg) {
3875 pid = PyObject_Call(self->pers_func, self->arg,
3876 NULL);
3877 FREE_ARG_TUP(self);
3878 }
3879 if (! pid) return -1;
3880 }
3881
3882 PDATA_PUSH(self->stack, pid, -1);
3883 return 0;
3884 }
3885 else {
3886 PyErr_SetString(UnpicklingError,
3887 "A load persistent id instruction was encountered,\n"
3888 "but no persistent_load function was specified.");
3889 return -1;
3890 }
3891}
3892
3893
3894static int
Tim Peterscba30e22003-02-01 06:24:36 +00003895load_pop(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003896{
3897 int len;
3898
3899 if (!( (len=self->stack->length) > 0 )) return stackUnderflow();
3900
3901 /* Note that we split the (pickle.py) stack into two stacks,
3902 an object stack and a mark stack. We have to be clever and
3903 pop the right one. We do this by looking at the top of the
3904 mark stack.
3905 */
3906
3907 if ((self->num_marks > 0) &&
3908 (self->marks[self->num_marks - 1] == len))
3909 self->num_marks--;
3910 else {
3911 len--;
3912 Py_DECREF(self->stack->data[len]);
3913 self->stack->length=len;
3914 }
3915
3916 return 0;
3917}
3918
3919
3920static int
Tim Peterscba30e22003-02-01 06:24:36 +00003921load_pop_mark(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003922{
3923 int i;
3924
3925 if ((i = marker(self)) < 0)
3926 return -1;
3927
3928 Pdata_clear(self->stack, i);
3929
3930 return 0;
3931}
3932
3933
3934static int
Tim Peterscba30e22003-02-01 06:24:36 +00003935load_dup(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003936{
3937 PyObject *last;
3938 int len;
3939
3940 if ((len = self->stack->length) <= 0) return stackUnderflow();
3941 last=self->stack->data[len-1];
3942 Py_INCREF(last);
3943 PDATA_PUSH(self->stack, last, -1);
3944 return 0;
3945}
3946
3947
3948static int
Tim Peterscba30e22003-02-01 06:24:36 +00003949load_get(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003950{
3951 PyObject *py_str = 0, *value = 0;
3952 int len;
3953 char *s;
3954 int rc;
3955
Tim Peters0bc93f52003-02-02 18:29:33 +00003956 if ((len = self->readline_func(self, &s)) < 0) return -1;
Guido van Rossumf9ffb031999-02-04 14:54:04 +00003957 if (len < 2) return bad_readline();
Tim Peters84e87f32001-03-17 04:50:51 +00003958
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003959 if (!( py_str = PyString_FromStringAndSize(s, len - 1))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003960
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003961 value = PyDict_GetItem(self->memo, py_str);
3962 if (! value) {
3963 PyErr_SetObject(BadPickleGet, py_str);
3964 rc = -1;
Tim Peters92c8bb32003-02-13 23:00:26 +00003965 }
3966 else {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003967 PDATA_APPEND(self->stack, value, -1);
3968 rc = 0;
3969 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003970
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003971 Py_DECREF(py_str);
3972 return rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003973}
3974
3975
3976static int
Tim Peterscba30e22003-02-01 06:24:36 +00003977load_binget(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003978{
3979 PyObject *py_key = 0, *value = 0;
3980 unsigned char key;
3981 char *s;
3982 int rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003983
Tim Peters0bc93f52003-02-02 18:29:33 +00003984 if (self->read_func(self, &s, 1) < 0) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003985
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003986 key = (unsigned char)s[0];
3987 if (!( py_key = PyInt_FromLong((long)key))) return -1;
Guido van Rossumea2b7152000-05-09 18:14:50 +00003988
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003989 value = PyDict_GetItem(self->memo, py_key);
3990 if (! value) {
3991 PyErr_SetObject(BadPickleGet, py_key);
3992 rc = -1;
Tim Peters92c8bb32003-02-13 23:00:26 +00003993 }
3994 else {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003995 PDATA_APPEND(self->stack, value, -1);
3996 rc = 0;
3997 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003998
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003999 Py_DECREF(py_key);
4000 return rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004001}
4002
4003
4004static int
Tim Peterscba30e22003-02-01 06:24:36 +00004005load_long_binget(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004006{
4007 PyObject *py_key = 0, *value = 0;
4008 unsigned char c;
4009 char *s;
4010 long key;
4011 int rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004012
Tim Peters0bc93f52003-02-02 18:29:33 +00004013 if (self->read_func(self, &s, 4) < 0) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004014
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004015 c = (unsigned char)s[0];
4016 key = (long)c;
4017 c = (unsigned char)s[1];
4018 key |= (long)c << 8;
4019 c = (unsigned char)s[2];
4020 key |= (long)c << 16;
4021 c = (unsigned char)s[3];
4022 key |= (long)c << 24;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004023
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004024 if (!( py_key = PyInt_FromLong((long)key))) return -1;
4025
4026 value = PyDict_GetItem(self->memo, py_key);
4027 if (! value) {
4028 PyErr_SetObject(BadPickleGet, py_key);
4029 rc = -1;
Tim Peters92c8bb32003-02-13 23:00:26 +00004030 }
4031 else {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004032 PDATA_APPEND(self->stack, value, -1);
4033 rc = 0;
4034 }
4035
4036 Py_DECREF(py_key);
4037 return rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004038}
4039
Tim Peters2d629652003-02-04 05:06:17 +00004040/* Push an object from the extension registry (EXT[124]). nbytes is
4041 * the number of bytes following the opcode, holding the index (code) value.
4042 */
4043static int
4044load_extension(Unpicklerobject *self, int nbytes)
4045{
4046 char *codebytes; /* the nbytes bytes after the opcode */
4047 long code; /* calc_binint returns long */
4048 PyObject *py_code; /* code as a Python int */
4049 PyObject *obj; /* the object to push */
4050 PyObject *pair; /* (module_name, class_name) */
4051 PyObject *module_name, *class_name;
4052
4053 assert(nbytes == 1 || nbytes == 2 || nbytes == 4);
4054 if (self->read_func(self, &codebytes, nbytes) < 0) return -1;
4055 code = calc_binint(codebytes, nbytes);
4056 if (code <= 0) { /* note that 0 is forbidden */
4057 /* Corrupt or hostile pickle. */
4058 PyErr_SetString(UnpicklingError, "EXT specifies code <= 0");
4059 return -1;
4060 }
4061
4062 /* Look for the code in the cache. */
4063 py_code = PyInt_FromLong(code);
4064 if (py_code == NULL) return -1;
4065 obj = PyDict_GetItem(extension_cache, py_code);
4066 if (obj != NULL) {
4067 /* Bingo. */
4068 Py_DECREF(py_code);
4069 PDATA_APPEND(self->stack, obj, -1);
4070 return 0;
4071 }
4072
4073 /* Look up the (module_name, class_name) pair. */
4074 pair = PyDict_GetItem(inverted_registry, py_code);
4075 if (pair == NULL) {
4076 Py_DECREF(py_code);
4077 PyErr_Format(PyExc_ValueError, "unregistered extension "
4078 "code %ld", code);
4079 return -1;
4080 }
4081 /* Since the extension registry is manipulable via Python code,
Tim Petersfa05ce32003-02-04 05:20:32 +00004082 * confirm that pair is really a 2-tuple of strings.
Tim Peters2d629652003-02-04 05:06:17 +00004083 */
4084 if (!PyTuple_Check(pair) || PyTuple_Size(pair) != 2 ||
Gregory P. Smithdd96db62008-06-09 04:58:54 +00004085 !PyString_Check(module_name = PyTuple_GET_ITEM(pair, 0)) ||
4086 !PyString_Check(class_name = PyTuple_GET_ITEM(pair, 1))) {
Tim Peters2d629652003-02-04 05:06:17 +00004087 Py_DECREF(py_code);
4088 PyErr_Format(PyExc_ValueError, "_inverted_registry[%ld] "
4089 "isn't a 2-tuple of strings", code);
4090 return -1;
4091 }
4092 /* Load the object. */
4093 obj = find_class(module_name, class_name, self->find_class);
4094 if (obj == NULL) {
4095 Py_DECREF(py_code);
4096 return -1;
4097 }
4098 /* Cache code -> obj. */
4099 code = PyDict_SetItem(extension_cache, py_code, obj);
4100 Py_DECREF(py_code);
4101 if (code < 0) {
4102 Py_DECREF(obj);
4103 return -1;
4104 }
4105 PDATA_PUSH(self->stack, obj, -1);
4106 return 0;
4107}
Guido van Rossum60456fd1997-04-09 17:36:32 +00004108
4109static int
Tim Peterscba30e22003-02-01 06:24:36 +00004110load_put(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004111{
4112 PyObject *py_str = 0, *value = 0;
4113 int len, l;
4114 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004115
Tim Peters0bc93f52003-02-02 18:29:33 +00004116 if ((l = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004117 if (l < 2) return bad_readline();
4118 if (!( len=self->stack->length )) return stackUnderflow();
Gregory P. Smithdd96db62008-06-09 04:58:54 +00004119 if (!( py_str = PyString_FromStringAndSize(s, l - 1))) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004120 value=self->stack->data[len-1];
4121 l=PyDict_SetItem(self->memo, py_str, value);
4122 Py_DECREF(py_str);
4123 return l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004124}
4125
4126
4127static int
Tim Peterscba30e22003-02-01 06:24:36 +00004128load_binput(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004129{
4130 PyObject *py_key = 0, *value = 0;
4131 unsigned char key;
4132 char *s;
4133 int len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004134
Tim Peters0bc93f52003-02-02 18:29:33 +00004135 if (self->read_func(self, &s, 1) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004136 if (!( (len=self->stack->length) > 0 )) return stackUnderflow();
Guido van Rossum60456fd1997-04-09 17:36:32 +00004137
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004138 key = (unsigned char)s[0];
Guido van Rossum053b8df1998-11-25 16:18:00 +00004139
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004140 if (!( py_key = PyInt_FromLong((long)key))) return -1;
4141 value=self->stack->data[len-1];
4142 len=PyDict_SetItem(self->memo, py_key, value);
4143 Py_DECREF(py_key);
4144 return len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004145}
4146
4147
4148static int
Tim Peterscba30e22003-02-01 06:24:36 +00004149load_long_binput(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004150{
4151 PyObject *py_key = 0, *value = 0;
4152 long key;
4153 unsigned char c;
4154 char *s;
4155 int len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004156
Tim Peters0bc93f52003-02-02 18:29:33 +00004157 if (self->read_func(self, &s, 4) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004158 if (!( len=self->stack->length )) return stackUnderflow();
Guido van Rossum60456fd1997-04-09 17:36:32 +00004159
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004160 c = (unsigned char)s[0];
4161 key = (long)c;
4162 c = (unsigned char)s[1];
4163 key |= (long)c << 8;
4164 c = (unsigned char)s[2];
4165 key |= (long)c << 16;
4166 c = (unsigned char)s[3];
4167 key |= (long)c << 24;
Tim Peters84e87f32001-03-17 04:50:51 +00004168
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004169 if (!( py_key = PyInt_FromLong(key))) return -1;
4170 value=self->stack->data[len-1];
4171 len=PyDict_SetItem(self->memo, py_key, value);
4172 Py_DECREF(py_key);
4173 return len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004174}
4175
4176
4177static int
Tim Peterscba30e22003-02-01 06:24:36 +00004178do_append(Unpicklerobject *self, int x)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004179{
4180 PyObject *value = 0, *list = 0, *append_method = 0;
4181 int len, i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004182
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004183 len=self->stack->length;
4184 if (!( len >= x && x > 0 )) return stackUnderflow();
4185 /* nothing to do */
Tim Peterscba30e22003-02-01 06:24:36 +00004186 if (len==x) return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004187
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004188 list=self->stack->data[x-1];
Guido van Rossum60456fd1997-04-09 17:36:32 +00004189
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004190 if (PyList_Check(list)) {
4191 PyObject *slice;
4192 int list_len;
Tim Peters84e87f32001-03-17 04:50:51 +00004193
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004194 slice=Pdata_popList(self->stack, x);
Neal Norwitzb59d08c2006-07-22 16:20:49 +00004195 if (! slice) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004196 list_len = PyList_GET_SIZE(list);
4197 i=PyList_SetSlice(list, list_len, list_len, slice);
4198 Py_DECREF(slice);
4199 return i;
4200 }
4201 else {
Guido van Rossum60456fd1997-04-09 17:36:32 +00004202
Tim Peterscba30e22003-02-01 06:24:36 +00004203 if (!( append_method = PyObject_GetAttr(list, append_str)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004204 return -1;
4205
4206 for (i = x; i < len; i++) {
4207 PyObject *junk;
4208
4209 value=self->stack->data[i];
4210 junk=0;
4211 ARG_TUP(self, value);
4212 if (self->arg) {
Tim Peterscba30e22003-02-01 06:24:36 +00004213 junk = PyObject_Call(append_method, self->arg,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004214 NULL);
4215 FREE_ARG_TUP(self);
4216 }
4217 if (! junk) {
4218 Pdata_clear(self->stack, i+1);
4219 self->stack->length=x;
4220 Py_DECREF(append_method);
4221 return -1;
4222 }
4223 Py_DECREF(junk);
4224 }
4225 self->stack->length=x;
4226 Py_DECREF(append_method);
4227 }
4228
4229 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004230}
4231
4232
4233static int
Tim Peterscba30e22003-02-01 06:24:36 +00004234load_append(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004235{
4236 return do_append(self, self->stack->length - 1);
Guido van Rossum60456fd1997-04-09 17:36:32 +00004237}
4238
4239
4240static int
Tim Peterscba30e22003-02-01 06:24:36 +00004241load_appends(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004242{
4243 return do_append(self, marker(self));
Guido van Rossum60456fd1997-04-09 17:36:32 +00004244}
4245
4246
4247static int
Tim Peterscba30e22003-02-01 06:24:36 +00004248do_setitems(Unpicklerobject *self, int x)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004249{
4250 PyObject *value = 0, *key = 0, *dict = 0;
4251 int len, i, r=0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004252
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004253 if (!( (len=self->stack->length) >= x
4254 && x > 0 )) return stackUnderflow();
Guido van Rossum60456fd1997-04-09 17:36:32 +00004255
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004256 dict=self->stack->data[x-1];
Guido van Rossum60456fd1997-04-09 17:36:32 +00004257
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004258 for (i = x+1; i < len; i += 2) {
4259 key =self->stack->data[i-1];
4260 value=self->stack->data[i ];
4261 if (PyObject_SetItem(dict, key, value) < 0) {
4262 r=-1;
4263 break;
4264 }
4265 }
4266
4267 Pdata_clear(self->stack, x);
4268
4269 return r;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004270}
4271
4272
Tim Peters84e87f32001-03-17 04:50:51 +00004273static int
Tim Peterscba30e22003-02-01 06:24:36 +00004274load_setitem(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004275{
4276 return do_setitems(self, self->stack->length - 2);
4277}
Guido van Rossum60456fd1997-04-09 17:36:32 +00004278
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004279static int
Tim Peterscba30e22003-02-01 06:24:36 +00004280load_setitems(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004281{
4282 return do_setitems(self, marker(self));
Guido van Rossum60456fd1997-04-09 17:36:32 +00004283}
4284
Tim Peters84e87f32001-03-17 04:50:51 +00004285
Guido van Rossum60456fd1997-04-09 17:36:32 +00004286static int
Tim Peterscba30e22003-02-01 06:24:36 +00004287load_build(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004288{
Tim Peters080c88b2003-02-15 03:01:11 +00004289 PyObject *state, *inst, *slotstate;
4290 PyObject *__setstate__;
4291 PyObject *d_key, *d_value;
Martin v. Löwis18e16552006-02-15 17:27:45 +00004292 Py_ssize_t i;
Tim Peters080c88b2003-02-15 03:01:11 +00004293 int res = -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004294
Tim Peters080c88b2003-02-15 03:01:11 +00004295 /* Stack is ... instance, state. We want to leave instance at
4296 * the stack top, possibly mutated via instance.__setstate__(state).
4297 */
4298 if (self->stack->length < 2)
4299 return stackUnderflow();
4300 PDATA_POP(self->stack, state);
4301 if (state == NULL)
4302 return -1;
4303 inst = self->stack->data[self->stack->length - 1];
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004304
Tim Peters080c88b2003-02-15 03:01:11 +00004305 __setstate__ = PyObject_GetAttr(inst, __setstate___str);
4306 if (__setstate__ != NULL) {
4307 PyObject *junk = NULL;
4308
4309 /* The explicit __setstate__ is responsible for everything. */
4310 ARG_TUP(self, state);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004311 if (self->arg) {
4312 junk = PyObject_Call(__setstate__, self->arg, NULL);
4313 FREE_ARG_TUP(self);
4314 }
4315 Py_DECREF(__setstate__);
Tim Peters080c88b2003-02-15 03:01:11 +00004316 if (junk == NULL)
4317 return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004318 Py_DECREF(junk);
4319 return 0;
4320 }
Jeremy Hyltonf00368f2003-06-16 20:19:49 +00004321 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4322 return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004323 PyErr_Clear();
Tim Peters080c88b2003-02-15 03:01:11 +00004324
4325 /* A default __setstate__. First see whether state embeds a
4326 * slot state dict too (a proto 2 addition).
4327 */
4328 if (PyTuple_Check(state) && PyTuple_Size(state) == 2) {
4329 PyObject *temp = state;
4330 state = PyTuple_GET_ITEM(temp, 0);
4331 slotstate = PyTuple_GET_ITEM(temp, 1);
4332 Py_INCREF(state);
4333 Py_INCREF(slotstate);
4334 Py_DECREF(temp);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004335 }
Tim Peters080c88b2003-02-15 03:01:11 +00004336 else
4337 slotstate = NULL;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004338
Tim Peters080c88b2003-02-15 03:01:11 +00004339 /* Set inst.__dict__ from the state dict (if any). */
4340 if (state != Py_None) {
4341 PyObject *dict;
4342 if (! PyDict_Check(state)) {
4343 PyErr_SetString(UnpicklingError, "state is not a "
4344 "dictionary");
4345 goto finally;
4346 }
4347 dict = PyObject_GetAttr(inst, __dict___str);
4348 if (dict == NULL)
4349 goto finally;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004350
Tim Peters080c88b2003-02-15 03:01:11 +00004351 i = 0;
4352 while (PyDict_Next(state, &i, &d_key, &d_value)) {
4353 if (PyObject_SetItem(dict, d_key, d_value) < 0)
4354 goto finally;
4355 }
4356 Py_DECREF(dict);
4357 }
4358
4359 /* Also set instance attributes from the slotstate dict (if any). */
4360 if (slotstate != NULL) {
4361 if (! PyDict_Check(slotstate)) {
4362 PyErr_SetString(UnpicklingError, "slot state is not "
4363 "a dictionary");
4364 goto finally;
4365 }
4366 i = 0;
4367 while (PyDict_Next(slotstate, &i, &d_key, &d_value)) {
4368 if (PyObject_SetAttr(inst, d_key, d_value) < 0)
4369 goto finally;
4370 }
4371 }
4372 res = 0;
4373
4374 finally:
4375 Py_DECREF(state);
4376 Py_XDECREF(slotstate);
4377 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004378}
4379
4380
4381static int
Tim Peterscba30e22003-02-01 06:24:36 +00004382load_mark(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004383{
4384 int s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004385
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004386 /* Note that we split the (pickle.py) stack into two stacks, an
4387 object stack and a mark stack. Here we push a mark onto the
4388 mark stack.
4389 */
Guido van Rossum60456fd1997-04-09 17:36:32 +00004390
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004391 if ((self->num_marks + 1) >= self->marks_size) {
Kristján Valur Jónsson17b8e972007-04-25 00:10:50 +00004392 int *marks;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004393 s=self->marks_size+20;
4394 if (s <= self->num_marks) s=self->num_marks + 1;
4395 if (self->marks == NULL)
Kristján Valur Jónsson17b8e972007-04-25 00:10:50 +00004396 marks=(int *)malloc(s * sizeof(int));
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004397 else
Kristján Valur Jónsson17b8e972007-04-25 00:10:50 +00004398 marks=(int *)realloc(self->marks,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004399 s * sizeof(int));
Kristján Valur Jónsson17b8e972007-04-25 00:10:50 +00004400 if (!marks) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004401 PyErr_NoMemory();
4402 return -1;
4403 }
Kristján Valur Jónsson17b8e972007-04-25 00:10:50 +00004404 self->marks = marks;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004405 self->marks_size = s;
4406 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00004407
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004408 self->marks[self->num_marks++] = self->stack->length;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004409
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004410 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004411}
4412
Guido van Rossum60456fd1997-04-09 17:36:32 +00004413static int
Tim Peterscba30e22003-02-01 06:24:36 +00004414load_reduce(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004415{
4416 PyObject *callable = 0, *arg_tup = 0, *ob = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004417
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004418 PDATA_POP(self->stack, arg_tup);
4419 if (! arg_tup) return -1;
4420 PDATA_POP(self->stack, callable);
4421 if (callable) {
4422 ob = Instance_New(callable, arg_tup);
4423 Py_DECREF(callable);
4424 }
4425 Py_DECREF(arg_tup);
Guido van Rossum60456fd1997-04-09 17:36:32 +00004426
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004427 if (! ob) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004428
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004429 PDATA_PUSH(self->stack, ob, -1);
4430 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004431}
Tim Peters84e87f32001-03-17 04:50:51 +00004432
Tim Peters4190fb82003-02-02 16:09:05 +00004433/* Just raises an error if we don't know the protocol specified. PROTO
4434 * is the first opcode for protocols >= 2.
4435 */
4436static int
4437load_proto(Unpicklerobject *self)
4438{
4439 int i;
4440 char *protobyte;
4441
4442 i = self->read_func(self, &protobyte, 1);
4443 if (i < 0)
4444 return -1;
4445
4446 i = calc_binint(protobyte, 1);
4447 /* No point checking for < 0, since calc_binint returns an unsigned
4448 * int when chewing on 1 byte.
4449 */
4450 assert(i >= 0);
Tim Peters8587b3c2003-02-13 15:44:41 +00004451 if (i <= HIGHEST_PROTOCOL)
Tim Peters4190fb82003-02-02 16:09:05 +00004452 return 0;
4453
4454 PyErr_Format(PyExc_ValueError, "unsupported pickle protocol: %d", i);
4455 return -1;
4456}
4457
Guido van Rossum60456fd1997-04-09 17:36:32 +00004458static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00004459load(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004460{
4461 PyObject *err = 0, *val = 0;
4462 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004463
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004464 self->num_marks = 0;
4465 if (self->stack->length) Pdata_clear(self->stack, 0);
Guido van Rossum60456fd1997-04-09 17:36:32 +00004466
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004467 while (1) {
Tim Peters0bc93f52003-02-02 18:29:33 +00004468 if (self->read_func(self, &s, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004469 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004470
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004471 switch (s[0]) {
4472 case NONE:
4473 if (load_none(self) < 0)
4474 break;
4475 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004476
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004477 case BININT:
4478 if (load_binint(self) < 0)
4479 break;
4480 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004481
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004482 case BININT1:
4483 if (load_binint1(self) < 0)
4484 break;
4485 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004486
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004487 case BININT2:
4488 if (load_binint2(self) < 0)
4489 break;
4490 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004491
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004492 case INT:
4493 if (load_int(self) < 0)
4494 break;
4495 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004496
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004497 case LONG:
4498 if (load_long(self) < 0)
4499 break;
4500 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004501
Tim Petersee1a53c2003-02-02 02:57:53 +00004502 case LONG1:
4503 if (load_counted_long(self, 1) < 0)
4504 break;
4505 continue;
4506
4507 case LONG4:
4508 if (load_counted_long(self, 4) < 0)
4509 break;
4510 continue;
4511
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004512 case FLOAT:
4513 if (load_float(self) < 0)
4514 break;
4515 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004516
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004517 case BINFLOAT:
4518 if (load_binfloat(self) < 0)
4519 break;
4520 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004521
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004522 case BINSTRING:
4523 if (load_binstring(self) < 0)
4524 break;
4525 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004526
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004527 case SHORT_BINSTRING:
4528 if (load_short_binstring(self) < 0)
4529 break;
4530 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004531
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004532 case STRING:
4533 if (load_string(self) < 0)
4534 break;
4535 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004536
Martin v. Löwis339d0f72001-08-17 18:39:25 +00004537#ifdef Py_USING_UNICODE
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004538 case UNICODE:
4539 if (load_unicode(self) < 0)
4540 break;
4541 continue;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00004542
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004543 case BINUNICODE:
4544 if (load_binunicode(self) < 0)
4545 break;
4546 continue;
Martin v. Löwis339d0f72001-08-17 18:39:25 +00004547#endif
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00004548
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004549 case EMPTY_TUPLE:
Tim Peters1d63c9f2003-02-02 20:29:39 +00004550 if (load_counted_tuple(self, 0) < 0)
4551 break;
4552 continue;
4553
4554 case TUPLE1:
4555 if (load_counted_tuple(self, 1) < 0)
4556 break;
4557 continue;
4558
4559 case TUPLE2:
4560 if (load_counted_tuple(self, 2) < 0)
4561 break;
4562 continue;
4563
4564 case TUPLE3:
4565 if (load_counted_tuple(self, 3) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004566 break;
4567 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004568
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004569 case TUPLE:
4570 if (load_tuple(self) < 0)
4571 break;
4572 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004573
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004574 case EMPTY_LIST:
4575 if (load_empty_list(self) < 0)
4576 break;
4577 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004578
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004579 case LIST:
4580 if (load_list(self) < 0)
4581 break;
4582 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004583
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004584 case EMPTY_DICT:
4585 if (load_empty_dict(self) < 0)
4586 break;
4587 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004588
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004589 case DICT:
4590 if (load_dict(self) < 0)
4591 break;
4592 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004593
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004594 case OBJ:
4595 if (load_obj(self) < 0)
4596 break;
4597 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004598
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004599 case INST:
4600 if (load_inst(self) < 0)
4601 break;
4602 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004603
Tim Peterseab7db32003-02-13 18:24:14 +00004604 case NEWOBJ:
4605 if (load_newobj(self) < 0)
4606 break;
4607 continue;
4608
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004609 case GLOBAL:
4610 if (load_global(self) < 0)
4611 break;
4612 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004613
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004614 case APPEND:
4615 if (load_append(self) < 0)
4616 break;
4617 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004618
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004619 case APPENDS:
4620 if (load_appends(self) < 0)
4621 break;
4622 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00004623
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004624 case BUILD:
4625 if (load_build(self) < 0)
4626 break;
4627 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00004628
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004629 case DUP:
4630 if (load_dup(self) < 0)
4631 break;
4632 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004633
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004634 case BINGET:
4635 if (load_binget(self) < 0)
4636 break;
4637 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004638
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004639 case LONG_BINGET:
4640 if (load_long_binget(self) < 0)
4641 break;
4642 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00004643
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004644 case GET:
4645 if (load_get(self) < 0)
4646 break;
4647 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004648
Tim Peters2d629652003-02-04 05:06:17 +00004649 case EXT1:
4650 if (load_extension(self, 1) < 0)
4651 break;
4652 continue;
4653
4654 case EXT2:
4655 if (load_extension(self, 2) < 0)
4656 break;
4657 continue;
4658
4659 case EXT4:
4660 if (load_extension(self, 4) < 0)
4661 break;
4662 continue;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004663 case MARK:
4664 if (load_mark(self) < 0)
4665 break;
4666 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004667
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004668 case BINPUT:
4669 if (load_binput(self) < 0)
4670 break;
4671 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004672
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004673 case LONG_BINPUT:
4674 if (load_long_binput(self) < 0)
4675 break;
4676 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00004677
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004678 case PUT:
4679 if (load_put(self) < 0)
4680 break;
4681 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004682
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004683 case POP:
4684 if (load_pop(self) < 0)
4685 break;
4686 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004687
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004688 case POP_MARK:
4689 if (load_pop_mark(self) < 0)
4690 break;
4691 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004692
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004693 case SETITEM:
4694 if (load_setitem(self) < 0)
4695 break;
4696 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004697
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004698 case SETITEMS:
4699 if (load_setitems(self) < 0)
4700 break;
4701 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004702
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004703 case STOP:
4704 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004705
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004706 case PERSID:
4707 if (load_persid(self) < 0)
4708 break;
4709 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004710
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004711 case BINPERSID:
4712 if (load_binpersid(self) < 0)
4713 break;
4714 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004715
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004716 case REDUCE:
4717 if (load_reduce(self) < 0)
4718 break;
4719 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004720
Tim Peters4190fb82003-02-02 16:09:05 +00004721 case PROTO:
4722 if (load_proto(self) < 0)
4723 break;
4724 continue;
4725
Tim Peters3c67d792003-02-02 17:59:11 +00004726 case NEWTRUE:
4727 if (load_bool(self, Py_True) < 0)
4728 break;
4729 continue;
4730
4731 case NEWFALSE:
4732 if (load_bool(self, Py_False) < 0)
4733 break;
4734 continue;
4735
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004736 case '\0':
4737 /* end of file */
4738 PyErr_SetNone(PyExc_EOFError);
4739 break;
Tim Peterscba30e22003-02-01 06:24:36 +00004740
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004741 default:
Tim Peterscba30e22003-02-01 06:24:36 +00004742 cPickle_ErrFormat(UnpicklingError,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004743 "invalid load key, '%s'.",
4744 "c", s[0]);
4745 return NULL;
4746 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00004747
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004748 break;
4749 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00004750
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004751 if ((err = PyErr_Occurred())) {
4752 if (err == PyExc_EOFError) {
4753 PyErr_SetNone(PyExc_EOFError);
4754 }
4755 return NULL;
4756 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00004757
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004758 PDATA_POP(self->stack, val);
4759 return val;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004760}
Tim Peters84e87f32001-03-17 04:50:51 +00004761
Guido van Rossum60456fd1997-04-09 17:36:32 +00004762
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004763/* No-load functions to support noload, which is used to
4764 find persistent references. */
4765
4766static int
Tim Peterscba30e22003-02-01 06:24:36 +00004767noload_obj(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004768{
4769 int i;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004770
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004771 if ((i = marker(self)) < 0) return -1;
4772 return Pdata_clear(self->stack, i+1);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004773}
4774
4775
4776static int
Tim Peterscba30e22003-02-01 06:24:36 +00004777noload_inst(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004778{
4779 int i;
4780 char *s;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004781
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004782 if ((i = marker(self)) < 0) return -1;
4783 Pdata_clear(self->stack, i);
Tim Peters0bc93f52003-02-02 18:29:33 +00004784 if (self->readline_func(self, &s) < 0) return -1;
4785 if (self->readline_func(self, &s) < 0) return -1;
Tim Peterseab7db32003-02-13 18:24:14 +00004786 PDATA_APPEND(self->stack, Py_None, -1);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004787 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004788}
4789
4790static int
Tim Peterseab7db32003-02-13 18:24:14 +00004791noload_newobj(Unpicklerobject *self)
4792{
4793 PyObject *obj;
4794
4795 PDATA_POP(self->stack, obj); /* pop argtuple */
4796 if (obj == NULL) return -1;
4797 Py_DECREF(obj);
4798
4799 PDATA_POP(self->stack, obj); /* pop cls */
4800 if (obj == NULL) return -1;
4801 Py_DECREF(obj);
4802
4803 PDATA_APPEND(self->stack, Py_None, -1);
4804 return 0;
4805}
4806
4807static int
Tim Peterscba30e22003-02-01 06:24:36 +00004808noload_global(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004809{
4810 char *s;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004811
Tim Peters0bc93f52003-02-02 18:29:33 +00004812 if (self->readline_func(self, &s) < 0) return -1;
4813 if (self->readline_func(self, &s) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004814 PDATA_APPEND(self->stack, Py_None,-1);
4815 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004816}
4817
4818static int
Tim Peterscba30e22003-02-01 06:24:36 +00004819noload_reduce(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004820{
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004821
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004822 if (self->stack->length < 2) return stackUnderflow();
4823 Pdata_clear(self->stack, self->stack->length-2);
4824 PDATA_APPEND(self->stack, Py_None,-1);
4825 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004826}
4827
4828static int
4829noload_build(Unpicklerobject *self) {
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004830
Guido van Rossum053b8df1998-11-25 16:18:00 +00004831 if (self->stack->length < 1) return stackUnderflow();
4832 Pdata_clear(self->stack, self->stack->length-1);
Guido van Rossum50f385c1998-12-04 18:48:44 +00004833 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004834}
4835
Tim Peters2d629652003-02-04 05:06:17 +00004836static int
4837noload_extension(Unpicklerobject *self, int nbytes)
4838{
4839 char *codebytes;
4840
4841 assert(nbytes == 1 || nbytes == 2 || nbytes == 4);
4842 if (self->read_func(self, &codebytes, nbytes) < 0) return -1;
4843 PDATA_APPEND(self->stack, Py_None, -1);
4844 return 0;
4845}
4846
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004847
4848static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00004849noload(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004850{
4851 PyObject *err = 0, *val = 0;
4852 char *s;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004853
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004854 self->num_marks = 0;
4855 Pdata_clear(self->stack, 0);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004856
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004857 while (1) {
Tim Peters0bc93f52003-02-02 18:29:33 +00004858 if (self->read_func(self, &s, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004859 break;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004860
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004861 switch (s[0]) {
4862 case NONE:
4863 if (load_none(self) < 0)
4864 break;
4865 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004866
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004867 case BININT:
4868 if (load_binint(self) < 0)
4869 break;
4870 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004871
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004872 case BININT1:
4873 if (load_binint1(self) < 0)
4874 break;
4875 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004876
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004877 case BININT2:
4878 if (load_binint2(self) < 0)
4879 break;
4880 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004881
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004882 case INT:
4883 if (load_int(self) < 0)
4884 break;
4885 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004886
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004887 case LONG:
4888 if (load_long(self) < 0)
4889 break;
4890 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004891
Tim Peters4190fb82003-02-02 16:09:05 +00004892 case LONG1:
4893 if (load_counted_long(self, 1) < 0)
4894 break;
4895 continue;
4896
4897 case LONG4:
4898 if (load_counted_long(self, 4) < 0)
4899 break;
4900 continue;
4901
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004902 case FLOAT:
4903 if (load_float(self) < 0)
4904 break;
4905 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004906
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004907 case BINFLOAT:
4908 if (load_binfloat(self) < 0)
4909 break;
4910 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004911
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004912 case BINSTRING:
4913 if (load_binstring(self) < 0)
4914 break;
4915 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004916
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004917 case SHORT_BINSTRING:
4918 if (load_short_binstring(self) < 0)
4919 break;
4920 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004921
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004922 case STRING:
4923 if (load_string(self) < 0)
4924 break;
4925 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004926
Martin v. Löwis339d0f72001-08-17 18:39:25 +00004927#ifdef Py_USING_UNICODE
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004928 case UNICODE:
4929 if (load_unicode(self) < 0)
4930 break;
4931 continue;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00004932
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004933 case BINUNICODE:
4934 if (load_binunicode(self) < 0)
4935 break;
4936 continue;
Martin v. Löwis339d0f72001-08-17 18:39:25 +00004937#endif
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00004938
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004939 case EMPTY_TUPLE:
Tim Peters1d63c9f2003-02-02 20:29:39 +00004940 if (load_counted_tuple(self, 0) < 0)
4941 break;
4942 continue;
4943
4944 case TUPLE1:
4945 if (load_counted_tuple(self, 1) < 0)
4946 break;
4947 continue;
4948
4949 case TUPLE2:
4950 if (load_counted_tuple(self, 2) < 0)
4951 break;
4952 continue;
4953
4954 case TUPLE3:
4955 if (load_counted_tuple(self, 3) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004956 break;
4957 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004958
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004959 case TUPLE:
4960 if (load_tuple(self) < 0)
4961 break;
4962 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004963
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004964 case EMPTY_LIST:
4965 if (load_empty_list(self) < 0)
4966 break;
4967 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004968
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004969 case LIST:
4970 if (load_list(self) < 0)
4971 break;
4972 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004973
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004974 case EMPTY_DICT:
4975 if (load_empty_dict(self) < 0)
4976 break;
4977 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004978
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004979 case DICT:
4980 if (load_dict(self) < 0)
4981 break;
4982 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004983
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004984 case OBJ:
4985 if (noload_obj(self) < 0)
4986 break;
4987 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004988
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004989 case INST:
4990 if (noload_inst(self) < 0)
4991 break;
4992 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004993
Tim Peterseab7db32003-02-13 18:24:14 +00004994 case NEWOBJ:
4995 if (noload_newobj(self) < 0)
4996 break;
4997 continue;
4998
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004999 case GLOBAL:
5000 if (noload_global(self) < 0)
5001 break;
5002 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005003
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005004 case APPEND:
5005 if (load_append(self) < 0)
5006 break;
5007 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005008
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005009 case APPENDS:
5010 if (load_appends(self) < 0)
5011 break;
5012 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00005013
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005014 case BUILD:
5015 if (noload_build(self) < 0)
5016 break;
5017 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00005018
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005019 case DUP:
5020 if (load_dup(self) < 0)
5021 break;
5022 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005023
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005024 case BINGET:
5025 if (load_binget(self) < 0)
5026 break;
5027 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005028
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005029 case LONG_BINGET:
5030 if (load_long_binget(self) < 0)
5031 break;
5032 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00005033
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005034 case GET:
5035 if (load_get(self) < 0)
5036 break;
5037 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005038
Tim Peters2d629652003-02-04 05:06:17 +00005039 case EXT1:
5040 if (noload_extension(self, 1) < 0)
5041 break;
5042 continue;
5043
5044 case EXT2:
5045 if (noload_extension(self, 2) < 0)
5046 break;
5047 continue;
5048
5049 case EXT4:
5050 if (noload_extension(self, 4) < 0)
5051 break;
5052 continue;
5053
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005054 case MARK:
5055 if (load_mark(self) < 0)
5056 break;
5057 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005058
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005059 case BINPUT:
5060 if (load_binput(self) < 0)
5061 break;
5062 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005063
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005064 case LONG_BINPUT:
5065 if (load_long_binput(self) < 0)
5066 break;
5067 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00005068
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005069 case PUT:
5070 if (load_put(self) < 0)
5071 break;
5072 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005073
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005074 case POP:
5075 if (load_pop(self) < 0)
5076 break;
5077 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005078
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005079 case POP_MARK:
5080 if (load_pop_mark(self) < 0)
5081 break;
5082 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005083
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005084 case SETITEM:
5085 if (load_setitem(self) < 0)
5086 break;
5087 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005088
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005089 case SETITEMS:
5090 if (load_setitems(self) < 0)
5091 break;
5092 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005093
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005094 case STOP:
5095 break;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005096
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005097 case PERSID:
5098 if (load_persid(self) < 0)
5099 break;
5100 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005101
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005102 case BINPERSID:
5103 if (load_binpersid(self) < 0)
5104 break;
5105 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005106
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005107 case REDUCE:
5108 if (noload_reduce(self) < 0)
5109 break;
5110 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005111
Tim Peters4190fb82003-02-02 16:09:05 +00005112 case PROTO:
5113 if (load_proto(self) < 0)
5114 break;
5115 continue;
5116
Tim Peters3c67d792003-02-02 17:59:11 +00005117 case NEWTRUE:
5118 if (load_bool(self, Py_True) < 0)
5119 break;
5120 continue;
5121
5122 case NEWFALSE:
5123 if (load_bool(self, Py_False) < 0)
5124 break;
5125 continue;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005126 default:
Tim Peterscba30e22003-02-01 06:24:36 +00005127 cPickle_ErrFormat(UnpicklingError,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005128 "invalid load key, '%s'.",
5129 "c", s[0]);
5130 return NULL;
5131 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005132
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005133 break;
5134 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005135
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005136 if ((err = PyErr_Occurred())) {
5137 if (err == PyExc_EOFError) {
5138 PyErr_SetNone(PyExc_EOFError);
5139 }
5140 return NULL;
5141 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005142
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005143 PDATA_POP(self->stack, val);
5144 return val;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005145}
Tim Peters84e87f32001-03-17 04:50:51 +00005146
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005147
Guido van Rossum60456fd1997-04-09 17:36:32 +00005148static PyObject *
Georg Brandl96a8c392006-05-29 21:04:52 +00005149Unpickler_load(Unpicklerobject *self, PyObject *unused)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005150{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005151 return load(self);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005152}
5153
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005154static PyObject *
Georg Brandl96a8c392006-05-29 21:04:52 +00005155Unpickler_noload(Unpicklerobject *self, PyObject *unused)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005156{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005157 return noload(self);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005158}
5159
Guido van Rossum60456fd1997-04-09 17:36:32 +00005160
5161static struct PyMethodDef Unpickler_methods[] = {
Georg Brandl96a8c392006-05-29 21:04:52 +00005162 {"load", (PyCFunction)Unpickler_load, METH_NOARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00005163 PyDoc_STR("load() -- Load a pickle")
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005164 },
Georg Brandl96a8c392006-05-29 21:04:52 +00005165 {"noload", (PyCFunction)Unpickler_noload, METH_NOARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00005166 PyDoc_STR(
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005167 "noload() -- not load a pickle, but go through most of the motions\n"
5168 "\n"
5169 "This function can be used to read past a pickle without instantiating\n"
5170 "any objects or importing any modules. It can also be used to find all\n"
5171 "persistent references without instantiating any objects or importing\n"
Neal Norwitz200788c2002-08-13 22:20:41 +00005172 "any modules.\n")
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005173 },
Guido van Rossum60456fd1997-04-09 17:36:32 +00005174 {NULL, NULL} /* sentinel */
5175};
5176
5177
5178static Unpicklerobject *
Tim Peterscba30e22003-02-01 06:24:36 +00005179newUnpicklerobject(PyObject *f)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005180{
5181 Unpicklerobject *self;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005182
Jeremy Hylton7b5ce7f2003-04-09 21:25:30 +00005183 if (!( self = PyObject_GC_New(Unpicklerobject, &Unpicklertype)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005184 return NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005185
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005186 self->file = NULL;
5187 self->arg = NULL;
5188 self->stack = (Pdata*)Pdata_New();
5189 self->pers_func = NULL;
5190 self->last_string = NULL;
5191 self->marks = NULL;
5192 self->num_marks = 0;
5193 self->marks_size = 0;
5194 self->buf_size = 0;
5195 self->read = NULL;
5196 self->readline = NULL;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005197 self->find_class = NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005198
Tim Peterscba30e22003-02-01 06:24:36 +00005199 if (!( self->memo = PyDict_New()))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005200 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005201
Neal Norwitzb59d08c2006-07-22 16:20:49 +00005202 if (!self->stack)
5203 goto err;
5204
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005205 Py_INCREF(f);
5206 self->file = f;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005207
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005208 /* Set read, readline based on type of f */
5209 if (PyFile_Check(f)) {
5210 self->fp = PyFile_AsFile(f);
5211 if (self->fp == NULL) {
Tim Peterscba30e22003-02-01 06:24:36 +00005212 PyErr_SetString(PyExc_ValueError,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005213 "I/O operation on closed file");
5214 goto err;
5215 }
5216 self->read_func = read_file;
5217 self->readline_func = readline_file;
Guido van Rossumc91fcaa1999-03-29 20:00:14 +00005218 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005219 else if (PycStringIO_InputCheck(f)) {
5220 self->fp = NULL;
5221 self->read_func = read_cStringIO;
5222 self->readline_func = readline_cStringIO;
5223 }
5224 else {
Guido van Rossum60456fd1997-04-09 17:36:32 +00005225
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005226 self->fp = NULL;
5227 self->read_func = read_other;
5228 self->readline_func = readline_other;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005229
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005230 if (!( (self->readline = PyObject_GetAttr(f, readline_str)) &&
5231 (self->read = PyObject_GetAttr(f, read_str)))) {
5232 PyErr_Clear();
Tim Peterscba30e22003-02-01 06:24:36 +00005233 PyErr_SetString( PyExc_TypeError,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005234 "argument must have 'read' and "
5235 "'readline' attributes" );
5236 goto err;
5237 }
5238 }
Jeremy Hylton7b5ce7f2003-04-09 21:25:30 +00005239 PyObject_GC_Track(self);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005240
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005241 return self;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005242
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005243 err:
5244 Py_DECREF((PyObject *)self);
5245 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005246}
5247
5248
5249static PyObject *
Georg Brandl96a8c392006-05-29 21:04:52 +00005250get_Unpickler(PyObject *self, PyObject *file)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005251{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005252 return (PyObject *)newUnpicklerobject(file);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005253}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005254
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005255
Guido van Rossum60456fd1997-04-09 17:36:32 +00005256static void
Tim Peterscba30e22003-02-01 06:24:36 +00005257Unpickler_dealloc(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005258{
Jeremy Hylton7b5ce7f2003-04-09 21:25:30 +00005259 PyObject_GC_UnTrack((PyObject *)self);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005260 Py_XDECREF(self->readline);
5261 Py_XDECREF(self->read);
5262 Py_XDECREF(self->file);
5263 Py_XDECREF(self->memo);
5264 Py_XDECREF(self->stack);
5265 Py_XDECREF(self->pers_func);
5266 Py_XDECREF(self->arg);
5267 Py_XDECREF(self->last_string);
Jeremy Hyltonfff093f2003-07-11 19:42:49 +00005268 Py_XDECREF(self->find_class);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005269
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005270 if (self->marks) {
5271 free(self->marks);
5272 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005273
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005274 if (self->buf_size) {
5275 free(self->buf);
5276 }
Tim Peters84e87f32001-03-17 04:50:51 +00005277
Christian Heimese93237d2007-12-19 02:37:44 +00005278 Py_TYPE(self)->tp_free((PyObject *)self);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005279}
5280
Jeremy Hylton7b5ce7f2003-04-09 21:25:30 +00005281static int
5282Unpickler_traverse(Unpicklerobject *self, visitproc visit, void *arg)
5283{
Thomas Woutersc6e55062006-04-15 21:47:09 +00005284 Py_VISIT(self->readline);
5285 Py_VISIT(self->read);
5286 Py_VISIT(self->file);
5287 Py_VISIT(self->memo);
5288 Py_VISIT(self->stack);
5289 Py_VISIT(self->pers_func);
5290 Py_VISIT(self->arg);
5291 Py_VISIT(self->last_string);
5292 Py_VISIT(self->find_class);
Jeremy Hylton7b5ce7f2003-04-09 21:25:30 +00005293 return 0;
5294}
5295
5296static int
5297Unpickler_clear(Unpicklerobject *self)
5298{
Thomas Woutersedf17d82006-04-15 17:28:34 +00005299 Py_CLEAR(self->readline);
5300 Py_CLEAR(self->read);
5301 Py_CLEAR(self->file);
5302 Py_CLEAR(self->memo);
5303 Py_CLEAR(self->stack);
5304 Py_CLEAR(self->pers_func);
5305 Py_CLEAR(self->arg);
5306 Py_CLEAR(self->last_string);
5307 Py_CLEAR(self->find_class);
Jeremy Hylton7b5ce7f2003-04-09 21:25:30 +00005308 return 0;
5309}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005310
5311static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00005312Unpickler_getattr(Unpicklerobject *self, char *name)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005313{
5314 if (!strcmp(name, "persistent_load")) {
5315 if (!self->pers_func) {
5316 PyErr_SetString(PyExc_AttributeError, name);
5317 return NULL;
5318 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005319
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005320 Py_INCREF(self->pers_func);
5321 return self->pers_func;
5322 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005323
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005324 if (!strcmp(name, "find_global")) {
5325 if (!self->find_class) {
5326 PyErr_SetString(PyExc_AttributeError, name);
5327 return NULL;
5328 }
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00005329
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005330 Py_INCREF(self->find_class);
5331 return self->find_class;
5332 }
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00005333
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005334 if (!strcmp(name, "memo")) {
5335 if (!self->memo) {
5336 PyErr_SetString(PyExc_AttributeError, name);
5337 return NULL;
5338 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005339
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005340 Py_INCREF(self->memo);
5341 return self->memo;
5342 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005343
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005344 if (!strcmp(name, "UnpicklingError")) {
5345 Py_INCREF(UnpicklingError);
5346 return UnpicklingError;
5347 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005348
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005349 return Py_FindMethod(Unpickler_methods, (PyObject *)self, name);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005350}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005351
Guido van Rossum60456fd1997-04-09 17:36:32 +00005352
5353static int
Tim Peterscba30e22003-02-01 06:24:36 +00005354Unpickler_setattr(Unpicklerobject *self, char *name, PyObject *value)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005355{
Jeremy Hyltonce616e41998-08-13 23:13:52 +00005356
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005357 if (!strcmp(name, "persistent_load")) {
5358 Py_XDECREF(self->pers_func);
5359 self->pers_func = value;
5360 Py_XINCREF(value);
5361 return 0;
5362 }
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00005363
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005364 if (!strcmp(name, "find_global")) {
5365 Py_XDECREF(self->find_class);
5366 self->find_class = value;
5367 Py_XINCREF(value);
5368 return 0;
5369 }
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00005370
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005371 if (! value) {
5372 PyErr_SetString(PyExc_TypeError,
5373 "attribute deletion is not supported");
5374 return -1;
5375 }
Jeremy Hyltonce616e41998-08-13 23:13:52 +00005376
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005377 if (strcmp(name, "memo") == 0) {
5378 if (!PyDict_Check(value)) {
Tim Peterscba30e22003-02-01 06:24:36 +00005379 PyErr_SetString(PyExc_TypeError,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005380 "memo must be a dictionary");
5381 return -1;
5382 }
5383 Py_XDECREF(self->memo);
5384 self->memo = value;
5385 Py_INCREF(value);
5386 return 0;
5387 }
Jeremy Hyltonce616e41998-08-13 23:13:52 +00005388
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005389 PyErr_SetString(PyExc_AttributeError, name);
5390 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005391}
5392
Tim Peters5bd2a792003-02-01 16:45:06 +00005393/* ---------------------------------------------------------------------------
5394 * Module-level functions.
5395 */
Guido van Rossum60456fd1997-04-09 17:36:32 +00005396
Martin v. Löwis544f1192004-07-27 05:22:33 +00005397/* dump(obj, file, protocol=0). */
Guido van Rossum60456fd1997-04-09 17:36:32 +00005398static PyObject *
Martin v. Löwis544f1192004-07-27 05:22:33 +00005399cpm_dump(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", "file", "protocol", NULL};
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005402 PyObject *ob, *file, *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, "OO|i", kwlist,
5407 &ob, &file, &proto)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005408 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005409
Tim Peters5bd2a792003-02-01 16:45:06 +00005410 if (!( pickler = newPicklerobject(file, proto)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005411 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005412
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005413 if (dump(pickler, ob) < 0)
5414 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005415
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005416 Py_INCREF(Py_None);
5417 res = Py_None;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005418
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005419 finally:
5420 Py_XDECREF(pickler);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005421
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005422 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005423}
5424
5425
Martin v. Löwis544f1192004-07-27 05:22:33 +00005426/* dumps(obj, protocol=0). */
Guido van Rossum60456fd1997-04-09 17:36:32 +00005427static PyObject *
Martin v. Löwis544f1192004-07-27 05:22:33 +00005428cpm_dumps(PyObject *self, PyObject *args, PyObject *kwds)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005429{
Martin v. Löwis15e62742006-02-27 16:46:16 +00005430 static char *kwlist[] = {"obj", "protocol", NULL};
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005431 PyObject *ob, *file = 0, *res = NULL;
5432 Picklerobject *pickler = 0;
Tim Peters5bd2a792003-02-01 16:45:06 +00005433 int proto = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005434
Martin v. Löwis544f1192004-07-27 05:22:33 +00005435 if (!( PyArg_ParseTupleAndKeywords(args, kwds, "O|i:dumps", kwlist,
5436 &ob, &proto)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005437 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005438
Tim Peterscba30e22003-02-01 06:24:36 +00005439 if (!( file = PycStringIO->NewOutput(128)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005440 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005441
Tim Peters5bd2a792003-02-01 16:45:06 +00005442 if (!( pickler = newPicklerobject(file, proto)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005443 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005444
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005445 if (dump(pickler, ob) < 0)
5446 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005447
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005448 res = PycStringIO->cgetvalue(file);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005449
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005450 finally:
5451 Py_XDECREF(pickler);
5452 Py_XDECREF(file);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005453
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005454 return res;
Tim Peters84e87f32001-03-17 04:50:51 +00005455}
5456
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005457
Tim Peters5bd2a792003-02-01 16:45:06 +00005458/* load(fileobj). */
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005459static PyObject *
Georg Brandl96a8c392006-05-29 21:04:52 +00005460cpm_load(PyObject *self, PyObject *ob)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005461{
5462 Unpicklerobject *unpickler = 0;
Georg Brandl96a8c392006-05-29 21:04:52 +00005463 PyObject *res = NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005464
Tim Peterscba30e22003-02-01 06:24:36 +00005465 if (!( unpickler = newUnpicklerobject(ob)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005466 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005467
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005468 res = load(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005469
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005470 finally:
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
Tim Peters5bd2a792003-02-01 16:45:06 +00005477/* loads(string) */
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005478static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00005479cpm_loads(PyObject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005480{
5481 PyObject *ob, *file = 0, *res = NULL;
5482 Unpicklerobject *unpickler = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005483
Tim Peterscba30e22003-02-01 06:24:36 +00005484 if (!( PyArg_ParseTuple(args, "S:loads", &ob)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005485 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005486
Tim Peterscba30e22003-02-01 06:24:36 +00005487 if (!( file = PycStringIO->NewInput(ob)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005488 goto finally;
Tim Peters84e87f32001-03-17 04:50:51 +00005489
Tim Peterscba30e22003-02-01 06:24:36 +00005490 if (!( unpickler = newUnpicklerobject(file)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005491 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005492
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005493 res = load(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005494
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005495 finally:
5496 Py_XDECREF(file);
5497 Py_XDECREF(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005498
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005499 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005500}
5501
5502
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005503PyDoc_STRVAR(Unpicklertype__doc__,
5504"Objects that know how to unpickle");
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005505
Guido van Rossum9716aaa1997-12-08 15:15:16 +00005506static PyTypeObject Unpicklertype = {
Martin v. Löwis68192102007-07-21 06:55:02 +00005507 PyVarObject_HEAD_INIT(NULL, 0)
Jeremy Hylton7b5ce7f2003-04-09 21:25:30 +00005508 "cPickle.Unpickler", /*tp_name*/
5509 sizeof(Unpicklerobject), /*tp_basicsize*/
5510 0,
5511 (destructor)Unpickler_dealloc, /* tp_dealloc */
5512 0, /* tp_print */
5513 (getattrfunc)Unpickler_getattr, /* tp_getattr */
5514 (setattrfunc)Unpickler_setattr, /* tp_setattr */
5515 0, /* tp_compare */
5516 0, /* tp_repr */
5517 0, /* tp_as_number */
5518 0, /* tp_as_sequence */
5519 0, /* tp_as_mapping */
5520 0, /* tp_hash */
5521 0, /* tp_call */
5522 0, /* tp_str */
5523 0, /* tp_getattro */
5524 0, /* tp_setattro */
5525 0, /* tp_as_buffer */
5526 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
5527 Unpicklertype__doc__, /* tp_doc */
5528 (traverseproc)Unpickler_traverse, /* tp_traverse */
5529 (inquiry)Unpickler_clear, /* tp_clear */
Guido van Rossum9716aaa1997-12-08 15:15:16 +00005530};
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005531
Guido van Rossum60456fd1997-04-09 17:36:32 +00005532static struct PyMethodDef cPickle_methods[] = {
Martin v. Löwis544f1192004-07-27 05:22:33 +00005533 {"dump", (PyCFunction)cpm_dump, METH_VARARGS | METH_KEYWORDS,
5534 PyDoc_STR("dump(obj, file, protocol=0) -- "
Tim Peters5bd2a792003-02-01 16:45:06 +00005535 "Write an object in pickle format to the given file.\n"
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005536 "\n"
Tim Peters5bd2a792003-02-01 16:45:06 +00005537 "See the Pickler docstring for the meaning of optional argument proto.")
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005538 },
Tim Peters5bd2a792003-02-01 16:45:06 +00005539
Martin v. Löwis544f1192004-07-27 05:22:33 +00005540 {"dumps", (PyCFunction)cpm_dumps, METH_VARARGS | METH_KEYWORDS,
5541 PyDoc_STR("dumps(obj, protocol=0) -- "
Tim Peters5bd2a792003-02-01 16:45:06 +00005542 "Return a string containing an object in pickle format.\n"
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005543 "\n"
Tim Peters5bd2a792003-02-01 16:45:06 +00005544 "See the Pickler docstring for the meaning of optional argument proto.")
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005545 },
Tim Peters5bd2a792003-02-01 16:45:06 +00005546
Georg Brandl96a8c392006-05-29 21:04:52 +00005547 {"load", (PyCFunction)cpm_load, METH_O,
Neal Norwitz200788c2002-08-13 22:20:41 +00005548 PyDoc_STR("load(file) -- Load a pickle from the given file")},
Tim Peters5bd2a792003-02-01 16:45:06 +00005549
Neal Norwitzb0493252002-03-31 14:44:22 +00005550 {"loads", (PyCFunction)cpm_loads, METH_VARARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00005551 PyDoc_STR("loads(string) -- Load a pickle from the given string")},
Tim Peters5bd2a792003-02-01 16:45:06 +00005552
Martin v. Löwis544f1192004-07-27 05:22:33 +00005553 {"Pickler", (PyCFunction)get_Pickler, METH_VARARGS | METH_KEYWORDS,
5554 PyDoc_STR("Pickler(file, protocol=0) -- Create a pickler.\n"
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005555 "\n"
Tim Peters5bd2a792003-02-01 16:45:06 +00005556 "This takes a file-like object for writing a pickle data stream.\n"
5557 "The optional proto argument tells the pickler to use the given\n"
5558 "protocol; supported protocols are 0, 1, 2. The default\n"
5559 "protocol is 0, to be backwards compatible. (Protocol 0 is the\n"
5560 "only protocol that can be written to a file opened in text\n"
5561 "mode and read back successfully. When using a protocol higher\n"
5562 "than 0, make sure the file is opened in binary mode, both when\n"
5563 "pickling and unpickling.)\n"
5564 "\n"
5565 "Protocol 1 is more efficient than protocol 0; protocol 2 is\n"
5566 "more efficient than protocol 1.\n"
5567 "\n"
5568 "Specifying a negative protocol version selects the highest\n"
5569 "protocol version supported. The higher the protocol used, the\n"
5570 "more recent the version of Python needed to read the pickle\n"
5571 "produced.\n"
5572 "\n"
5573 "The file parameter must have a write() method that accepts a single\n"
5574 "string argument. It can thus be an open file object, a StringIO\n"
5575 "object, or any other custom object that meets this interface.\n")
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005576 },
Tim Peters5bd2a792003-02-01 16:45:06 +00005577
Georg Brandl96a8c392006-05-29 21:04:52 +00005578 {"Unpickler", (PyCFunction)get_Unpickler, METH_O,
Tim Peters5bd2a792003-02-01 16:45:06 +00005579 PyDoc_STR("Unpickler(file) -- Create an unpickler.")},
5580
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005581 { NULL, NULL }
5582};
5583
Guido van Rossum60456fd1997-04-09 17:36:32 +00005584static int
Tim Peterscba30e22003-02-01 06:24:36 +00005585init_stuff(PyObject *module_dict)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005586{
Alexandre Vassalotti9510e4a2008-05-11 08:25:28 +00005587 PyObject *copyreg, *t, *r;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005588
Gregory P. Smithdd96db62008-06-09 04:58:54 +00005589#define INIT_STR(S) if (!( S ## _str=PyString_InternFromString(#S))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005590
Tim Peters3cfe7542003-05-21 21:29:48 +00005591 if (PyType_Ready(&Unpicklertype) < 0)
5592 return -1;
5593 if (PyType_Ready(&Picklertype) < 0)
5594 return -1;
5595
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005596 INIT_STR(__class__);
5597 INIT_STR(__getinitargs__);
5598 INIT_STR(__dict__);
5599 INIT_STR(__getstate__);
5600 INIT_STR(__setstate__);
5601 INIT_STR(__name__);
5602 INIT_STR(__main__);
5603 INIT_STR(__reduce__);
Guido van Rossumb289b872003-02-19 01:45:13 +00005604 INIT_STR(__reduce_ex__);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005605 INIT_STR(write);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005606 INIT_STR(append);
5607 INIT_STR(read);
5608 INIT_STR(readline);
Alexandre Vassalotti9510e4a2008-05-11 08:25:28 +00005609 INIT_STR(copyreg);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005610 INIT_STR(dispatch_table);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005611
Georg Brandldffbf5f2008-05-20 07:49:57 +00005612 if (!( copyreg = PyImport_ImportModule("copy_reg")))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005613 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005614
Tim Peters1f1b2d22003-02-01 02:16:37 +00005615 /* This is special because we want to use a different
5616 one in restricted mode. */
Alexandre Vassalotti9510e4a2008-05-11 08:25:28 +00005617 dispatch_table = PyObject_GetAttr(copyreg, dispatch_table_str);
Tim Peters5b7da392003-02-04 00:21:07 +00005618 if (!dispatch_table) return -1;
5619
Alexandre Vassalotti9510e4a2008-05-11 08:25:28 +00005620 extension_registry = PyObject_GetAttrString(copyreg,
Guido van Rossumd4b920c2003-02-04 01:54:49 +00005621 "_extension_registry");
Tim Peters5b7da392003-02-04 00:21:07 +00005622 if (!extension_registry) return -1;
5623
Alexandre Vassalotti9510e4a2008-05-11 08:25:28 +00005624 inverted_registry = PyObject_GetAttrString(copyreg,
Guido van Rossumd4b920c2003-02-04 01:54:49 +00005625 "_inverted_registry");
Tim Peters5b7da392003-02-04 00:21:07 +00005626 if (!inverted_registry) return -1;
5627
Alexandre Vassalotti9510e4a2008-05-11 08:25:28 +00005628 extension_cache = PyObject_GetAttrString(copyreg,
Guido van Rossumd4b920c2003-02-04 01:54:49 +00005629 "_extension_cache");
Tim Peters5b7da392003-02-04 00:21:07 +00005630 if (!extension_cache) return -1;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005631
Alexandre Vassalotti9510e4a2008-05-11 08:25:28 +00005632 Py_DECREF(copyreg);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005633
Tim Peters731098b2003-02-04 20:56:09 +00005634 if (!(empty_tuple = PyTuple_New(0)))
5635 return -1;
5636
5637 two_tuple = PyTuple_New(2);
5638 if (two_tuple == NULL)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005639 return -1;
Tim Peters3e667d52003-02-04 21:47:44 +00005640 /* We use this temp container with no regard to refcounts, or to
5641 * keeping containees alive. Exempt from GC, because we don't
5642 * want anything looking at two_tuple() by magic.
5643 */
5644 PyObject_GC_UnTrack(two_tuple);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005645
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005646 /* Ugh */
5647 if (!( t=PyImport_ImportModule("__builtin__"))) return -1;
5648 if (PyDict_SetItemString(module_dict, "__builtins__", t) < 0)
5649 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005650
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005651 if (!( t=PyDict_New())) return -1;
5652 if (!( r=PyRun_String(
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005653 "def __str__(self):\n"
5654 " return self.args and ('%s' % self.args[0]) or '(what)'\n",
5655 Py_file_input,
5656 module_dict, t) )) return -1;
5657 Py_DECREF(r);
Guido van Rossumc03158b1999-06-09 15:23:31 +00005658
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005659 PickleError = PyErr_NewException("cPickle.PickleError", NULL, t);
Tim Peterscba30e22003-02-01 06:24:36 +00005660 if (!PickleError)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005661 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00005662
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005663 Py_DECREF(t);
Guido van Rossumc03158b1999-06-09 15:23:31 +00005664
Tim Peterscba30e22003-02-01 06:24:36 +00005665 PicklingError = PyErr_NewException("cPickle.PicklingError",
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005666 PickleError, NULL);
Tim Peterscba30e22003-02-01 06:24:36 +00005667 if (!PicklingError)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005668 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00005669
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005670 if (!( t=PyDict_New())) return -1;
5671 if (!( r=PyRun_String(
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005672 "def __str__(self):\n"
5673 " a=self.args\n"
5674 " a=a and type(a[0]) or '(what)'\n"
5675 " return 'Cannot pickle %s objects' % a\n"
5676 , Py_file_input,
5677 module_dict, t) )) return -1;
5678 Py_DECREF(r);
Tim Peters84e87f32001-03-17 04:50:51 +00005679
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005680 if (!( UnpickleableError = PyErr_NewException(
Tim Peterscba30e22003-02-01 06:24:36 +00005681 "cPickle.UnpickleableError", PicklingError, t)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005682 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00005683
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005684 Py_DECREF(t);
Guido van Rossumc03158b1999-06-09 15:23:31 +00005685
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005686 if (!( UnpicklingError = PyErr_NewException("cPickle.UnpicklingError",
Tim Peterscba30e22003-02-01 06:24:36 +00005687 PickleError, NULL)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005688 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00005689
Martin v. Löwis658009a2002-09-16 17:26:24 +00005690 if (!( BadPickleGet = PyErr_NewException("cPickle.BadPickleGet",
5691 UnpicklingError, NULL)))
5692 return -1;
Tim Peterscba30e22003-02-01 06:24:36 +00005693
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005694 if (PyDict_SetItemString(module_dict, "PickleError",
5695 PickleError) < 0)
5696 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00005697
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005698 if (PyDict_SetItemString(module_dict, "PicklingError",
5699 PicklingError) < 0)
5700 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00005701
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005702 if (PyDict_SetItemString(module_dict, "UnpicklingError",
5703 UnpicklingError) < 0)
5704 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005705
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005706 if (PyDict_SetItemString(module_dict, "UnpickleableError",
5707 UnpickleableError) < 0)
5708 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005709
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005710 if (PyDict_SetItemString(module_dict, "BadPickleGet",
5711 BadPickleGet) < 0)
5712 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00005713
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005714 PycString_IMPORT;
Guido van Rossum053b8df1998-11-25 16:18:00 +00005715
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005716 return 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005717}
5718
Mark Hammondfe51c6d2002-08-02 02:27:13 +00005719#ifndef PyMODINIT_FUNC /* declarations for DLL import/export */
5720#define PyMODINIT_FUNC void
Guido van Rossumf9ffb031999-02-04 14:54:04 +00005721#endif
Mark Hammondfe51c6d2002-08-02 02:27:13 +00005722PyMODINIT_FUNC
Tim Peterscba30e22003-02-01 06:24:36 +00005723initcPickle(void)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005724{
5725 PyObject *m, *d, *di, *v, *k;
Martin v. Löwis18e16552006-02-15 17:27:45 +00005726 Py_ssize_t i;
Tim Peters5b7da392003-02-04 00:21:07 +00005727 char *rev = "1.71"; /* XXX when does this change? */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005728 PyObject *format_version;
5729 PyObject *compatible_formats;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005730
Alexandre Vassalotti3c4971c2008-05-16 19:14:31 +00005731 /* XXX: Should mention that the pickle module will include the C
5732 XXX: optimized implementation automatically. */
5733 if (PyErr_WarnPy3k("the cPickle module has been removed in "
5734 "Python 3.0", 2) < 0)
5735 return;
5736
Christian Heimese93237d2007-12-19 02:37:44 +00005737 Py_TYPE(&Picklertype) = &PyType_Type;
5738 Py_TYPE(&Unpicklertype) = &PyType_Type;
5739 Py_TYPE(&PdataType) = &PyType_Type;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005740
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005741 /* Initialize some pieces. We need to do this before module creation,
Tim Peters5b7da392003-02-04 00:21:07 +00005742 * so we're forced to use a temporary dictionary. :(
5743 */
5744 di = PyDict_New();
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005745 if (!di) return;
5746 if (init_stuff(di) < 0) return;
Guido van Rossumebba4202000-09-07 14:35:37 +00005747
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005748 /* Create the module and add the functions */
5749 m = Py_InitModule4("cPickle", cPickle_methods,
5750 cPickle_module_documentation,
5751 (PyObject*)NULL,PYTHON_API_VERSION);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00005752 if (m == NULL)
5753 return;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005754
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005755 /* Add some symbolic constants to the module */
5756 d = PyModule_GetDict(m);
Gregory P. Smithdd96db62008-06-09 04:58:54 +00005757 v = PyString_FromString(rev);
Tim Peters5b7da392003-02-04 00:21:07 +00005758 PyDict_SetItemString(d, "__version__", v);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005759 Py_XDECREF(v);
Barry Warsaw93d29b61997-01-14 17:45:08 +00005760
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005761 /* Copy data from di. Waaa. */
5762 for (i=0; PyDict_Next(di, &i, &k, &v); ) {
5763 if (PyObject_SetItem(d, k, v) < 0) {
5764 Py_DECREF(di);
5765 return;
5766 }
5767 }
5768 Py_DECREF(di);
Guido van Rossumebba4202000-09-07 14:35:37 +00005769
Tim Peters8587b3c2003-02-13 15:44:41 +00005770 i = PyModule_AddIntConstant(m, "HIGHEST_PROTOCOL", HIGHEST_PROTOCOL);
5771 if (i < 0)
5772 return;
5773
Tim Peters5b7da392003-02-04 00:21:07 +00005774 /* These are purely informational; no code uses them. */
5775 /* File format version we write. */
Gregory P. Smithdd96db62008-06-09 04:58:54 +00005776 format_version = PyString_FromString("2.0");
Tim Peters5b7da392003-02-04 00:21:07 +00005777 /* Format versions we can read. */
5778 compatible_formats = Py_BuildValue("[sssss]",
5779 "1.0", /* Original protocol 0 */
5780 "1.1", /* Protocol 0 + INST */
5781 "1.2", /* Original protocol 1 */
5782 "1.3", /* Protocol 1 + BINFLOAT */
Tim Peters0dd23aa2003-02-04 00:30:46 +00005783 "2.0"); /* Original protocol 2 */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005784 PyDict_SetItemString(d, "format_version", format_version);
5785 PyDict_SetItemString(d, "compatible_formats", compatible_formats);
5786 Py_XDECREF(format_version);
5787 Py_XDECREF(compatible_formats);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005788}