blob: 727dcc946d76cd37d76ff04125b84e8ed916dbb1 [file] [log] [blame]
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001#include "Python.h"
2#include "cStringIO.h"
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003#include "structmember.h"
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005PyDoc_STRVAR(cPickle_module_documentation,
Tim Peters64c04d12003-02-01 06:27:59 +00006"C implementation and optimization of the Python pickle module.");
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007
Guido van Rossum142eeb81997-08-13 03:14:41 +00008#ifndef Py_eval_input
9#include <graminit.h>
10#define Py_eval_input eval_input
Guido van Rossumc6ef2041997-08-21 02:30:45 +000011#endif /* Py_eval_input */
Guido van Rossum142eeb81997-08-13 03:14:41 +000012
Guido van Rossum60456fd1997-04-09 17:36:32 +000013#define DEL_LIST_SLICE(list, from, to) (PyList_SetSlice(list, from, to, NULL))
Guido van Rossum2f4caa41997-01-06 22:59:08 +000014
Guido van Rossum60456fd1997-04-09 17:36:32 +000015#define WRITE_BUF_SIZE 256
16
Tim Peters5bd2a792003-02-01 16:45:06 +000017/* Bump this when new opcodes are added to the pickle protocol. */
Tim Peters8587b3c2003-02-13 15:44:41 +000018#define HIGHEST_PROTOCOL 2
Tim Peters5bd2a792003-02-01 16:45:06 +000019
Tim Peters797ec242003-02-01 06:22:36 +000020/*
21 * Pickle opcodes. These must be kept in synch with pickle.py. Extensive
22 * docs are in pickletools.py.
23 */
Guido van Rossum60456fd1997-04-09 17:36:32 +000024#define MARK '('
25#define STOP '.'
26#define POP '0'
27#define POP_MARK '1'
28#define DUP '2'
29#define FLOAT 'F'
Guido van Rossum60456fd1997-04-09 17:36:32 +000030#define BINFLOAT 'G'
Guido van Rossum60456fd1997-04-09 17:36:32 +000031#define INT 'I'
32#define BININT 'J'
33#define BININT1 'K'
34#define LONG 'L'
35#define BININT2 'M'
36#define NONE 'N'
37#define PERSID 'P'
38#define BINPERSID 'Q'
39#define REDUCE 'R'
40#define STRING 'S'
41#define BINSTRING 'T'
Guido van Rossum2f4caa41997-01-06 22:59:08 +000042#define SHORT_BINSTRING 'U'
Guido van Rossum5fccb7c2000-03-10 23:11:40 +000043#define UNICODE 'V'
44#define BINUNICODE 'X'
Guido van Rossum60456fd1997-04-09 17:36:32 +000045#define APPEND 'a'
46#define BUILD 'b'
47#define GLOBAL 'c'
48#define DICT 'd'
49#define EMPTY_DICT '}'
50#define APPENDS 'e'
51#define GET 'g'
52#define BINGET 'h'
53#define INST 'i'
54#define LONG_BINGET 'j'
55#define LIST 'l'
56#define EMPTY_LIST ']'
57#define OBJ 'o'
58#define PUT 'p'
59#define BINPUT 'q'
60#define LONG_BINPUT 'r'
61#define SETITEM 's'
62#define TUPLE 't'
63#define EMPTY_TUPLE ')'
64#define SETITEMS 'u'
Tim Peters797ec242003-02-01 06:22:36 +000065
66/* Protocol 2. */
67#define PROTO '\x80' /* identify pickle protocol */
68#define NEWOBJ '\x81' /* build object by applying cls.__new__ to argtuple */
69#define EXT1 '\x82' /* push object from extension registry; 1-byte index */
70#define EXT2 '\x83' /* ditto, but 2-byte index */
71#define EXT4 '\x84' /* ditto, but 4-byte index */
72#define TUPLE1 '\x85' /* build 1-tuple from stack top */
73#define TUPLE2 '\x86' /* build 2-tuple from two topmost stack items */
74#define TUPLE3 '\x87' /* build 3-tuple from three topmost stack items */
75#define NEWTRUE '\x88' /* push True */
76#define NEWFALSE '\x89' /* push False */
77#define LONG1 '\x8a' /* push long from < 256 bytes */
78#define LONG4 '\x8b' /* push really big long */
79
80/* There aren't opcodes -- they're ways to pickle bools before protocol 2,
81 * so that unpicklers written before bools were introduced unpickle them
82 * as ints, but unpicklers after can recognize that bools were intended.
83 * Note that protocol 2 added direct ways to pickle bools.
84 */
Jack Jansen3a967022002-06-26 20:40:42 +000085#undef TRUE
Guido van Rossume2763392002-04-05 19:30:08 +000086#define TRUE "I01\n"
Jack Jansen3a967022002-06-26 20:40:42 +000087#undef FALSE
Guido van Rossume2763392002-04-05 19:30:08 +000088#define FALSE "I00\n"
Guido van Rossum77f6a652002-04-03 22:41:51 +000089
Tim Peters1092d642003-02-11 21:06:20 +000090/* Keep in synch with pickle.Pickler._BATCHSIZE. This is how many elements
Tim Peters42f08ac2003-02-11 22:43:24 +000091 * batch_list/dict() pumps out before doing APPENDS/SETITEMS. Nothing will
92 * break if this gets out of synch with pickle.py, but it's unclear that
93 * would help anything either.
Tim Peters1092d642003-02-11 21:06:20 +000094 */
95#define BATCHSIZE 1000
96
Guido van Rossum60456fd1997-04-09 17:36:32 +000097static char MARKv = MARK;
Guido van Rossum2f4caa41997-01-06 22:59:08 +000098
Guido van Rossumc03158b1999-06-09 15:23:31 +000099static PyObject *PickleError;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000100static PyObject *PicklingError;
Guido van Rossumc03158b1999-06-09 15:23:31 +0000101static PyObject *UnpickleableError;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000102static PyObject *UnpicklingError;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000103static PyObject *BadPickleGet;
104
Tim Peters5b7da392003-02-04 00:21:07 +0000105/* As the name says, an empty tuple. */
Guido van Rossum60456fd1997-04-09 17:36:32 +0000106static PyObject *empty_tuple;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000107
Tim Peters5b7da392003-02-04 00:21:07 +0000108/* copy_reg.dispatch_table, {type_object: pickling_function} */
109static PyObject *dispatch_table;
110
111/* For EXT[124] opcodes. */
Guido van Rossumd4b920c2003-02-04 01:54:49 +0000112/* copy_reg._extension_registry, {(module_name, function_name): code} */
Tim Peters5b7da392003-02-04 00:21:07 +0000113static PyObject *extension_registry;
Guido van Rossumd4b920c2003-02-04 01:54:49 +0000114/* copy_reg._inverted_registry, {code: (module_name, function_name)} */
Tim Peters5b7da392003-02-04 00:21:07 +0000115static PyObject *inverted_registry;
Guido van Rossumd4b920c2003-02-04 01:54:49 +0000116/* copy_reg._extension_cache, {code: object} */
Tim Peters5b7da392003-02-04 00:21:07 +0000117static PyObject *extension_cache;
118
Tim Peters731098b2003-02-04 20:56:09 +0000119/* For looking up name pairs in copy_reg._extension_registry. */
120static PyObject *two_tuple;
121
Guido van Rossum60456fd1997-04-09 17:36:32 +0000122static PyObject *__class___str, *__getinitargs___str, *__dict___str,
123 *__getstate___str, *__setstate___str, *__name___str, *__reduce___str,
Guido van Rossumb289b872003-02-19 01:45:13 +0000124 *__reduce_ex___str,
Tim Peters1f1b2d22003-02-01 02:16:37 +0000125 *write_str, *append_str,
Guido van Rossum9716aaa1997-12-08 15:15:16 +0000126 *read_str, *readline_str, *__main___str, *__basicnew___str,
Tim Peters1f1b2d22003-02-01 02:16:37 +0000127 *copy_reg_str, *dispatch_table_str;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000128
Guido van Rossum053b8df1998-11-25 16:18:00 +0000129/*************************************************************************
130 Internal Data type for pickle data. */
131
132typedef struct {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000133 PyObject_HEAD
Tim Peters1d63c9f2003-02-02 20:29:39 +0000134 int length; /* number of initial slots in data currently used */
135 int size; /* number of slots in data allocated */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000136 PyObject **data;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000137} Pdata;
138
Tim Peters84e87f32001-03-17 04:50:51 +0000139static void
Tim Peterscba30e22003-02-01 06:24:36 +0000140Pdata_dealloc(Pdata *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000141{
142 int i;
143 PyObject **p;
Tim Peterscba30e22003-02-01 06:24:36 +0000144
Tim Peters1d63c9f2003-02-02 20:29:39 +0000145 for (i = self->length, p = self->data; --i >= 0; p++) {
146 Py_DECREF(*p);
147 }
148 if (self->data)
149 free(self->data);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000150 PyObject_Del(self);
Guido van Rossum053b8df1998-11-25 16:18:00 +0000151}
152
153static PyTypeObject PdataType = {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000154 PyObject_HEAD_INIT(NULL) 0, "cPickle.Pdata", sizeof(Pdata), 0,
155 (destructor)Pdata_dealloc,
156 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0L,0L,0L,0L, ""
Guido van Rossum053b8df1998-11-25 16:18:00 +0000157};
158
159#define Pdata_Check(O) ((O)->ob_type == &PdataType)
160
161static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +0000162Pdata_New(void)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000163{
164 Pdata *self;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000165
Tim Peters1d63c9f2003-02-02 20:29:39 +0000166 if (!(self = PyObject_New(Pdata, &PdataType)))
167 return NULL;
168 self->size = 8;
169 self->length = 0;
170 self->data = malloc(self->size * sizeof(PyObject*));
171 if (self->data)
172 return (PyObject*)self;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000173 Py_DECREF(self);
174 return PyErr_NoMemory();
Guido van Rossum053b8df1998-11-25 16:18:00 +0000175}
176
Tim Peters84e87f32001-03-17 04:50:51 +0000177static int
Tim Peterscba30e22003-02-01 06:24:36 +0000178stackUnderflow(void)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000179{
180 PyErr_SetString(UnpicklingError, "unpickling stack underflow");
181 return -1;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000182}
183
Tim Peters1d63c9f2003-02-02 20:29:39 +0000184/* Retain only the initial clearto items. If clearto >= the current
185 * number of items, this is a (non-erroneous) NOP.
186 */
Guido van Rossum053b8df1998-11-25 16:18:00 +0000187static int
Tim Peterscba30e22003-02-01 06:24:36 +0000188Pdata_clear(Pdata *self, int clearto)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000189{
190 int i;
191 PyObject **p;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000192
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000193 if (clearto < 0) return stackUnderflow();
194 if (clearto >= self->length) return 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000195
Tim Peters1d63c9f2003-02-02 20:29:39 +0000196 for (i = self->length, p = self->data + clearto;
197 --i >= clearto;
198 p++) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000199 Py_DECREF(*p);
Tim Peters1d63c9f2003-02-02 20:29:39 +0000200 }
201 self->length = clearto;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000202
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000203 return 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000204}
205
Tim Peters84e87f32001-03-17 04:50:51 +0000206static int
Tim Peterscba30e22003-02-01 06:24:36 +0000207Pdata_grow(Pdata *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000208{
Tim Peters1d63c9f2003-02-02 20:29:39 +0000209 int bigger;
210 size_t nbytes;
211
Tim Peters1d63c9f2003-02-02 20:29:39 +0000212 bigger = self->size << 1;
Tim Peterse0a39072003-02-03 15:45:56 +0000213 if (bigger <= 0) /* was 0, or new value overflows */
Tim Peters1d63c9f2003-02-02 20:29:39 +0000214 goto nomemory;
215 if ((int)(size_t)bigger != bigger)
216 goto nomemory;
217 nbytes = (size_t)bigger * sizeof(PyObject *);
218 if (nbytes / sizeof(PyObject *) != (size_t)bigger)
219 goto nomemory;
220 self->data = realloc(self->data, nbytes);
221 if (self->data == NULL)
222 goto nomemory;
223 self->size = bigger;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000224 return 0;
Tim Peters1d63c9f2003-02-02 20:29:39 +0000225
226 nomemory:
227 self->size = 0;
228 PyErr_NoMemory();
229 return -1;
Tim Peters84e87f32001-03-17 04:50:51 +0000230}
Guido van Rossum053b8df1998-11-25 16:18:00 +0000231
Tim Peterse0a39072003-02-03 15:45:56 +0000232/* D is a Pdata*. Pop the topmost element and store it into V, which
233 * must be an lvalue holding PyObject*. On stack underflow, UnpicklingError
Tim Peters1d63c9f2003-02-02 20:29:39 +0000234 * is raised and V is set to NULL. D and V may be evaluated several times.
235 */
236#define PDATA_POP(D, V) { \
Tim Peterse0a39072003-02-03 15:45:56 +0000237 if ((D)->length) \
238 (V) = (D)->data[--((D)->length)]; \
239 else { \
240 PyErr_SetString(UnpicklingError, "bad pickle data"); \
241 (V) = NULL; \
242 } \
Guido van Rossum053b8df1998-11-25 16:18:00 +0000243}
244
Tim Peterse0a39072003-02-03 15:45:56 +0000245/* PDATA_PUSH and PDATA_APPEND both push rvalue PyObject* O on to Pdata*
246 * D. If the Pdata stack can't be grown to hold the new value, both
247 * raise MemoryError and execute "return ER". The difference is in ownership
248 * of O after: _PUSH transfers ownership of O from the caller to the stack
249 * (no incref of O is done, and in case of error O is decrefed), while
250 * _APPEND pushes a new reference.
251 */
252
253/* Push O on stack D, giving ownership of O to the stack. */
254#define PDATA_PUSH(D, O, ER) { \
255 if (((Pdata*)(D))->length == ((Pdata*)(D))->size && \
256 Pdata_grow((Pdata*)(D)) < 0) { \
257 Py_DECREF(O); \
258 return ER; \
259 } \
260 ((Pdata*)(D))->data[((Pdata*)(D))->length++] = (O); \
261}
262
263/* Push O on stack D, pushing a new reference. */
264#define PDATA_APPEND(D, O, ER) { \
265 if (((Pdata*)(D))->length == ((Pdata*)(D))->size && \
266 Pdata_grow((Pdata*)(D)) < 0) \
267 return ER; \
268 Py_INCREF(O); \
269 ((Pdata*)(D))->data[((Pdata*)(D))->length++] = (O); \
270}
271
272
Guido van Rossum053b8df1998-11-25 16:18:00 +0000273static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +0000274Pdata_popTuple(Pdata *self, int start)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000275{
276 PyObject *r;
277 int i, j, l;
Tim Peterscba30e22003-02-01 06:24:36 +0000278
Tim Peters1d63c9f2003-02-02 20:29:39 +0000279 l = self->length-start;
280 r = PyTuple_New(l);
281 if (r == NULL)
282 return NULL;
283 for (i = start, j = 0 ; j < l; i++, j++)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000284 PyTuple_SET_ITEM(r, j, self->data[i]);
Tim Peterscba30e22003-02-01 06:24:36 +0000285
Tim Peters1d63c9f2003-02-02 20:29:39 +0000286 self->length = start;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000287 return r;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000288}
289
290static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +0000291Pdata_popList(Pdata *self, int start)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000292{
293 PyObject *r;
294 int i, j, l;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000295
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000296 l=self->length-start;
297 if (!( r=PyList_New(l))) return NULL;
298 for (i=start, j=0 ; j < l; i++, j++)
299 PyList_SET_ITEM(r, j, self->data[i]);
Guido van Rossum053b8df1998-11-25 16:18:00 +0000300
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000301 self->length=start;
302 return r;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000303}
304
Guido van Rossum053b8df1998-11-25 16:18:00 +0000305/*************************************************************************/
306
307#define ARG_TUP(self, o) { \
308 if (self->arg || (self->arg=PyTuple_New(1))) { \
309 Py_XDECREF(PyTuple_GET_ITEM(self->arg,0)); \
310 PyTuple_SET_ITEM(self->arg,0,o); \
311 } \
312 else { \
313 Py_DECREF(o); \
314 } \
315}
316
317#define FREE_ARG_TUP(self) { \
318 if (self->arg->ob_refcnt > 1) { \
319 Py_DECREF(self->arg); \
320 self->arg=NULL; \
321 } \
322 }
323
Thomas Wouters3b6448f2000-07-22 23:56:07 +0000324typedef struct Picklerobject {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000325 PyObject_HEAD
326 FILE *fp;
327 PyObject *write;
328 PyObject *file;
329 PyObject *memo;
330 PyObject *arg;
331 PyObject *pers_func;
332 PyObject *inst_pers_func;
Tim Peters797ec242003-02-01 06:22:36 +0000333
334 /* pickle protocol number, >= 0 */
335 int proto;
336
337 /* bool, true if proto > 0 */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000338 int bin;
Tim Peters797ec242003-02-01 06:22:36 +0000339
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000340 int fast; /* Fast mode doesn't save in memo, don't use if circ ref */
Martin v. Löwis5a395302002-08-04 08:20:23 +0000341 int nesting;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000342 int (*write_func)(struct Picklerobject *, const char *, Py_ssize_t);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000343 char *write_buf;
344 int buf_size;
345 PyObject *dispatch_table;
346 int fast_container; /* count nested container dumps */
347 PyObject *fast_memo;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000348} Picklerobject;
349
Barry Warsaw52acb492001-12-21 20:04:22 +0000350#ifndef PY_CPICKLE_FAST_LIMIT
351#define PY_CPICKLE_FAST_LIMIT 50
352#endif
Jeremy Hyltona0fb1772001-10-12 04:11:06 +0000353
Jeremy Hylton938ace62002-07-17 16:30:39 +0000354static PyTypeObject Picklertype;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000355
Thomas Wouters3b6448f2000-07-22 23:56:07 +0000356typedef struct Unpicklerobject {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000357 PyObject_HEAD
358 FILE *fp;
359 PyObject *file;
360 PyObject *readline;
361 PyObject *read;
362 PyObject *memo;
363 PyObject *arg;
364 Pdata *stack;
365 PyObject *mark;
366 PyObject *pers_func;
367 PyObject *last_string;
368 int *marks;
369 int num_marks;
370 int marks_size;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000371 Py_ssize_t (*read_func)(struct Unpicklerobject *, char **, Py_ssize_t);
372 Py_ssize_t (*readline_func)(struct Unpicklerobject *, char **);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000373 int buf_size;
374 char *buf;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000375 PyObject *find_class;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000376} Unpicklerobject;
Tim Peters84e87f32001-03-17 04:50:51 +0000377
Jeremy Hylton938ace62002-07-17 16:30:39 +0000378static PyTypeObject Unpicklertype;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000379
Thomas Wouters4789b3a2000-07-24 11:36:47 +0000380/* Forward decls that need the above structs */
381static int save(Picklerobject *, PyObject *, int);
382static int put2(Picklerobject *, PyObject *);
383
Guido van Rossumd385d591997-04-09 17:47:47 +0000384static
Guido van Rossum60456fd1997-04-09 17:36:32 +0000385PyObject *
Thomas Wouters3b6448f2000-07-22 23:56:07 +0000386cPickle_ErrFormat(PyObject *ErrType, char *stringformat, char *format, ...)
387{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000388 va_list va;
389 PyObject *args=0, *retval=0;
390 va_start(va, format);
Tim Peterscba30e22003-02-01 06:24:36 +0000391
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000392 if (format) args = Py_VaBuildValue(format, va);
393 va_end(va);
394 if (format && ! args) return NULL;
Tim Peterscba30e22003-02-01 06:24:36 +0000395 if (stringformat && !(retval=PyString_FromString(stringformat)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000396 return NULL;
Tim Peterscba30e22003-02-01 06:24:36 +0000397
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000398 if (retval) {
399 if (args) {
400 PyObject *v;
401 v=PyString_Format(retval, args);
402 Py_DECREF(retval);
403 Py_DECREF(args);
404 if (! v) return NULL;
405 retval=v;
406 }
407 }
408 else
409 if (args) retval=args;
410 else {
411 PyErr_SetObject(ErrType,Py_None);
412 return NULL;
413 }
414 PyErr_SetObject(ErrType,retval);
415 Py_DECREF(retval);
416 return NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000417}
418
Tim Peters84e87f32001-03-17 04:50:51 +0000419static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000420write_file(Picklerobject *self, const char *s, Py_ssize_t n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000421{
422 size_t nbyteswritten;
Tim Peters84e87f32001-03-17 04:50:51 +0000423
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000424 if (s == NULL) {
425 return 0;
426 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000427
Martin v. Löwis18e16552006-02-15 17:27:45 +0000428 if (n > INT_MAX) {
429 /* String too large */
430 return -1;
431 }
432
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000433 Py_BEGIN_ALLOW_THREADS
Jeremy Hylton7fa4bfa2002-07-18 20:58:57 +0000434 nbyteswritten = fwrite(s, sizeof(char), n, self->fp);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000435 Py_END_ALLOW_THREADS
Jeremy Hylton7fa4bfa2002-07-18 20:58:57 +0000436 if (nbyteswritten != (size_t)n) {
437 PyErr_SetFromErrno(PyExc_IOError);
438 return -1;
439 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000440
Martin v. Löwis18e16552006-02-15 17:27:45 +0000441 return (int)n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000442}
443
Tim Peters84e87f32001-03-17 04:50:51 +0000444static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000445write_cStringIO(Picklerobject *self, const char *s, Py_ssize_t n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000446{
447 if (s == NULL) {
448 return 0;
449 }
Tim Peterscba30e22003-02-01 06:24:36 +0000450
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000451 if (PycStringIO->cwrite((PyObject *)self->file, s, n) != n) {
452 return -1;
453 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000454
Martin v. Löwis18e16552006-02-15 17:27:45 +0000455 return (int)n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000456}
457
Tim Peters84e87f32001-03-17 04:50:51 +0000458static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000459write_none(Picklerobject *self, const char *s, Py_ssize_t n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000460{
461 if (s == NULL) return 0;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000462 if (n > INT_MAX) return -1;
463 return (int)n;
Guido van Rossum142eeb81997-08-13 03:14:41 +0000464}
465
Tim Peters84e87f32001-03-17 04:50:51 +0000466static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000467write_other(Picklerobject *self, const char *s, Py_ssize_t _n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000468{
469 PyObject *py_str = 0, *junk = 0;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000470 int n;
Tim Peterscba30e22003-02-01 06:24:36 +0000471
Martin v. Löwis18e16552006-02-15 17:27:45 +0000472 if (_n > INT_MAX)
473 return -1;
474 n = (int)_n;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000475 if (s == NULL) {
476 if (!( self->buf_size )) return 0;
Tim Peterscba30e22003-02-01 06:24:36 +0000477 py_str = PyString_FromStringAndSize(self->write_buf,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000478 self->buf_size);
Tim Peterscba30e22003-02-01 06:24:36 +0000479 if (!py_str)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000480 return -1;
481 }
482 else {
483 if (self->buf_size && (n + self->buf_size) > WRITE_BUF_SIZE) {
484 if (write_other(self, NULL, 0) < 0)
485 return -1;
486 }
Tim Peterscba30e22003-02-01 06:24:36 +0000487
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000488 if (n > WRITE_BUF_SIZE) {
489 if (!( py_str =
Tim Peterscba30e22003-02-01 06:24:36 +0000490 PyString_FromStringAndSize(s, n)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000491 return -1;
492 }
493 else {
494 memcpy(self->write_buf + self->buf_size, s, n);
495 self->buf_size += n;
496 return n;
497 }
498 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000499
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000500 if (self->write) {
501 /* object with write method */
502 ARG_TUP(self, py_str);
503 if (self->arg) {
504 junk = PyObject_Call(self->write, self->arg, NULL);
505 FREE_ARG_TUP(self);
506 }
507 if (junk) Py_DECREF(junk);
508 else return -1;
509 }
510 else
511 PDATA_PUSH(self->file, py_str, -1);
Guido van Rossum60456fd1997-04-09 17:36:32 +0000512
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000513 self->buf_size = 0;
514 return n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000515}
516
517
Martin v. Löwis18e16552006-02-15 17:27:45 +0000518static Py_ssize_t
519read_file(Unpicklerobject *self, char **s, Py_ssize_t n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000520{
521 size_t nbytesread;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000522
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000523 if (self->buf_size == 0) {
524 int size;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000525
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000526 size = ((n < 32) ? 32 : n);
Tim Petersee1a53c2003-02-02 02:57:53 +0000527 if (!( self->buf = (char *)malloc(size))) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000528 PyErr_NoMemory();
529 return -1;
530 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000531
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000532 self->buf_size = size;
533 }
534 else if (n > self->buf_size) {
Tim Petersee1a53c2003-02-02 02:57:53 +0000535 self->buf = (char *)realloc(self->buf, n);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000536 if (!self->buf) {
537 PyErr_NoMemory();
538 return -1;
539 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000540 self->buf_size = n;
541 }
Tim Peters84e87f32001-03-17 04:50:51 +0000542
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000543 Py_BEGIN_ALLOW_THREADS
544 nbytesread = fread(self->buf, sizeof(char), n, self->fp);
545 Py_END_ALLOW_THREADS
546 if (nbytesread != (size_t)n) {
547 if (feof(self->fp)) {
548 PyErr_SetNone(PyExc_EOFError);
549 return -1;
550 }
Tim Peterscba30e22003-02-01 06:24:36 +0000551
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000552 PyErr_SetFromErrno(PyExc_IOError);
553 return -1;
554 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000555
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000556 *s = self->buf;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000557
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000558 return n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000559}
560
561
Martin v. Löwis18e16552006-02-15 17:27:45 +0000562static Py_ssize_t
Tim Peterscba30e22003-02-01 06:24:36 +0000563readline_file(Unpicklerobject *self, char **s)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000564{
565 int i;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000566
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000567 if (self->buf_size == 0) {
Tim Petersee1a53c2003-02-02 02:57:53 +0000568 if (!( self->buf = (char *)malloc(40))) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000569 PyErr_NoMemory();
570 return -1;
571 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000572 self->buf_size = 40;
573 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000574
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000575 i = 0;
576 while (1) {
Tim Petersee1a53c2003-02-02 02:57:53 +0000577 int bigger;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000578 for (; i < (self->buf_size - 1); i++) {
Tim Peterscba30e22003-02-01 06:24:36 +0000579 if (feof(self->fp) ||
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000580 (self->buf[i] = getc(self->fp)) == '\n') {
581 self->buf[i + 1] = '\0';
582 *s = self->buf;
583 return i + 1;
584 }
585 }
Tim Petersee1a53c2003-02-02 02:57:53 +0000586 bigger = self->buf_size << 1;
587 if (bigger <= 0) { /* overflow */
588 PyErr_NoMemory();
589 return -1;
590 }
591 self->buf = (char *)realloc(self->buf, bigger);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000592 if (!self->buf) {
593 PyErr_NoMemory();
594 return -1;
595 }
Tim Petersee1a53c2003-02-02 02:57:53 +0000596 self->buf_size = bigger;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000597 }
Tim Peters84e87f32001-03-17 04:50:51 +0000598}
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000599
600
Martin v. Löwis18e16552006-02-15 17:27:45 +0000601static Py_ssize_t
602read_cStringIO(Unpicklerobject *self, char **s, Py_ssize_t n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000603{
604 char *ptr;
Tim Peterscba30e22003-02-01 06:24:36 +0000605
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000606 if (PycStringIO->cread((PyObject *)self->file, &ptr, n) != n) {
607 PyErr_SetNone(PyExc_EOFError);
608 return -1;
609 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000610
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000611 *s = ptr;
Tim Peterscba30e22003-02-01 06:24:36 +0000612
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000613 return n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000614}
615
616
Martin v. Löwis18e16552006-02-15 17:27:45 +0000617static Py_ssize_t
Tim Peterscba30e22003-02-01 06:24:36 +0000618readline_cStringIO(Unpicklerobject *self, char **s)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000619{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000620 Py_ssize_t n;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000621 char *ptr;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000622
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000623 if ((n = PycStringIO->creadline((PyObject *)self->file, &ptr)) < 0) {
624 return -1;
625 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000626
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000627 *s = ptr;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000628
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000629 return n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000630}
631
632
Martin v. Löwis18e16552006-02-15 17:27:45 +0000633static Py_ssize_t
634read_other(Unpicklerobject *self, char **s, Py_ssize_t n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000635{
636 PyObject *bytes, *str=0;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000637
Martin v. Löwis18e16552006-02-15 17:27:45 +0000638 if (!( bytes = PyInt_FromSsize_t(n))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000639
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000640 ARG_TUP(self, bytes);
641 if (self->arg) {
642 str = PyObject_Call(self->read, self->arg, NULL);
643 FREE_ARG_TUP(self);
644 }
645 if (! str) return -1;
Tim Peterscba30e22003-02-01 06:24:36 +0000646
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000647 Py_XDECREF(self->last_string);
648 self->last_string = str;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000649
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000650 if (! (*s = PyString_AsString(str))) return -1;
651 return n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000652}
653
654
Martin v. Löwis18e16552006-02-15 17:27:45 +0000655static Py_ssize_t
Tim Peterscba30e22003-02-01 06:24:36 +0000656readline_other(Unpicklerobject *self, char **s)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000657{
658 PyObject *str;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000659 Py_ssize_t str_size;
Tim Peterscba30e22003-02-01 06:24:36 +0000660
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000661 if (!( str = PyObject_CallObject(self->readline, empty_tuple))) {
662 return -1;
663 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000664
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000665 if ((str_size = PyString_Size(str)) < 0)
666 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000667
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000668 Py_XDECREF(self->last_string);
669 self->last_string = str;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000670
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000671 if (! (*s = PyString_AsString(str)))
672 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000673
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000674 return str_size;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000675}
676
Tim Petersee1a53c2003-02-02 02:57:53 +0000677/* Copy the first n bytes from s into newly malloc'ed memory, plus a
678 * trailing 0 byte. Return a pointer to that, or NULL if out of memory.
679 * The caller is responsible for free()'ing the return value.
680 */
Guido van Rossum60456fd1997-04-09 17:36:32 +0000681static char *
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000682pystrndup(const char *s, int n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000683{
Tim Petersee1a53c2003-02-02 02:57:53 +0000684 char *r = (char *)malloc(n+1);
685 if (r == NULL)
686 return (char*)PyErr_NoMemory();
687 memcpy(r, s, n);
688 r[n] = 0;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000689 return r;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000690}
691
692
693static int
Tim Peterscba30e22003-02-01 06:24:36 +0000694get(Picklerobject *self, PyObject *id)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000695{
696 PyObject *value, *mv;
697 long c_value;
698 char s[30];
699 size_t len;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000700
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000701 if (!( mv = PyDict_GetItem(self->memo, id))) {
702 PyErr_SetObject(PyExc_KeyError, id);
703 return -1;
704 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000705
Tim Peterscba30e22003-02-01 06:24:36 +0000706 if (!( value = PyTuple_GetItem(mv, 0)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000707 return -1;
Tim Peters84e87f32001-03-17 04:50:51 +0000708
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000709 if (!( PyInt_Check(value))) {
710 PyErr_SetString(PicklingError, "no int where int expected in memo");
711 return -1;
712 }
713 c_value = PyInt_AS_LONG((PyIntObject*)value);
Guido van Rossum60456fd1997-04-09 17:36:32 +0000714
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000715 if (!self->bin) {
716 s[0] = GET;
717 PyOS_snprintf(s + 1, sizeof(s) - 1, "%ld\n", c_value);
718 len = strlen(s);
719 }
720 else if (Pdata_Check(self->file)) {
721 if (write_other(self, NULL, 0) < 0) return -1;
722 PDATA_APPEND(self->file, mv, -1);
723 return 0;
724 }
725 else {
726 if (c_value < 256) {
727 s[0] = BINGET;
728 s[1] = (int)(c_value & 0xff);
729 len = 2;
730 }
731 else {
732 s[0] = LONG_BINGET;
733 s[1] = (int)(c_value & 0xff);
734 s[2] = (int)((c_value >> 8) & 0xff);
735 s[3] = (int)((c_value >> 16) & 0xff);
736 s[4] = (int)((c_value >> 24) & 0xff);
737 len = 5;
738 }
739 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000740
Tim Peters0bc93f52003-02-02 18:29:33 +0000741 if (self->write_func(self, s, len) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000742 return -1;
Tim Peterscba30e22003-02-01 06:24:36 +0000743
Jeremy Hyltona0fb1772001-10-12 04:11:06 +0000744 return 0;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000745}
Jeremy Hyltonce616e41998-08-13 23:13:52 +0000746
Guido van Rossum60456fd1997-04-09 17:36:32 +0000747
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000748static int
Tim Peterscba30e22003-02-01 06:24:36 +0000749put(Picklerobject *self, PyObject *ob)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000750{
Tim Peterscba30e22003-02-01 06:24:36 +0000751 if (ob->ob_refcnt < 2 || self->fast)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000752 return 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000753
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000754 return put2(self, ob);
755}
Guido van Rossum053b8df1998-11-25 16:18:00 +0000756
Guido van Rossum053b8df1998-11-25 16:18:00 +0000757
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000758static int
Tim Peterscba30e22003-02-01 06:24:36 +0000759put2(Picklerobject *self, PyObject *ob)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000760{
761 char c_str[30];
762 int p;
763 size_t len;
764 int res = -1;
765 PyObject *py_ob_id = 0, *memo_len = 0, *t = 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000766
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000767 if (self->fast)
768 return 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000769
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000770 if ((p = PyDict_Size(self->memo)) < 0)
771 goto finally;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000772
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000773 /* Make sure memo keys are positive! */
Tim Peters70b02d72003-02-02 17:26:40 +0000774 /* XXX Why?
775 * XXX And does "positive" really mean non-negative?
776 * XXX pickle.py starts with PUT index 0, not 1. This makes for
777 * XXX gratuitous differences between the pickling modules.
778 */
Tim Peterscba30e22003-02-01 06:24:36 +0000779 p++;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000780
Tim Peterscba30e22003-02-01 06:24:36 +0000781 if (!( py_ob_id = PyLong_FromVoidPtr(ob)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000782 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000783
Tim Peterscba30e22003-02-01 06:24:36 +0000784 if (!( memo_len = PyInt_FromLong(p)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000785 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000786
Tim Peterscba30e22003-02-01 06:24:36 +0000787 if (!( t = PyTuple_New(2)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000788 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000789
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000790 PyTuple_SET_ITEM(t, 0, memo_len);
791 Py_INCREF(memo_len);
792 PyTuple_SET_ITEM(t, 1, ob);
793 Py_INCREF(ob);
794
795 if (PyDict_SetItem(self->memo, py_ob_id, t) < 0)
796 goto finally;
797
798 if (!self->bin) {
799 c_str[0] = PUT;
800 PyOS_snprintf(c_str + 1, sizeof(c_str) - 1, "%d\n", p);
801 len = strlen(c_str);
802 }
803 else if (Pdata_Check(self->file)) {
804 if (write_other(self, NULL, 0) < 0) return -1;
805 PDATA_APPEND(self->file, memo_len, -1);
806 res=0; /* Job well done ;) */
807 goto finally;
808 }
809 else {
810 if (p >= 256) {
811 c_str[0] = LONG_BINPUT;
812 c_str[1] = (int)(p & 0xff);
813 c_str[2] = (int)((p >> 8) & 0xff);
814 c_str[3] = (int)((p >> 16) & 0xff);
815 c_str[4] = (int)((p >> 24) & 0xff);
816 len = 5;
817 }
818 else {
819 c_str[0] = BINPUT;
820 c_str[1] = p;
821 len = 2;
822 }
823 }
824
Tim Peters0bc93f52003-02-02 18:29:33 +0000825 if (self->write_func(self, c_str, len) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000826 goto finally;
827
828 res = 0;
829
830 finally:
831 Py_XDECREF(py_ob_id);
832 Py_XDECREF(memo_len);
833 Py_XDECREF(t);
834
835 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000836}
837
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000838static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +0000839whichmodule(PyObject *global, PyObject *global_name)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000840{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000841 Py_ssize_t i, j;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000842 PyObject *module = 0, *modules_dict = 0,
843 *global_name_attr = 0, *name = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000844
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000845 module = PyObject_GetAttrString(global, "__module__");
Jeremy Hyltonf00368f2003-06-16 20:19:49 +0000846 if (module)
847 return module;
848 if (PyErr_ExceptionMatches(PyExc_AttributeError))
849 PyErr_Clear();
850 else
851 return NULL;
Guido van Rossum45188231997-09-28 05:38:51 +0000852
Tim Peterscba30e22003-02-01 06:24:36 +0000853 if (!( modules_dict = PySys_GetObject("modules")))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000854 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000855
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000856 i = 0;
857 while ((j = PyDict_Next(modules_dict, &i, &name, &module))) {
Guido van Rossum142eeb81997-08-13 03:14:41 +0000858
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000859 if (PyObject_Compare(name, __main___str)==0) continue;
Tim Peters84e87f32001-03-17 04:50:51 +0000860
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000861 global_name_attr = PyObject_GetAttr(module, global_name);
862 if (!global_name_attr) {
Jeremy Hyltonf00368f2003-06-16 20:19:49 +0000863 if (PyErr_ExceptionMatches(PyExc_AttributeError))
864 PyErr_Clear();
865 else
866 return NULL;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000867 continue;
868 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000869
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000870 if (global_name_attr != global) {
871 Py_DECREF(global_name_attr);
872 continue;
873 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000874
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000875 Py_DECREF(global_name_attr);
Guido van Rossum60456fd1997-04-09 17:36:32 +0000876
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000877 break;
878 }
Guido van Rossum142eeb81997-08-13 03:14:41 +0000879
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000880 /* The following implements the rule in pickle.py added in 1.5
881 that used __main__ if no module is found. I don't actually
882 like this rule. jlf
883 */
884 if (!j) {
885 j=1;
886 name=__main___str;
887 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000888
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000889 Py_INCREF(name);
890 return name;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000891}
892
893
Guido van Rossum60456fd1997-04-09 17:36:32 +0000894static int
Jeremy Hylton499ab6a2001-10-15 21:37:58 +0000895fast_save_enter(Picklerobject *self, PyObject *obj)
896{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000897 /* if fast_container < 0, we're doing an error exit. */
898 if (++self->fast_container >= PY_CPICKLE_FAST_LIMIT) {
899 PyObject *key = NULL;
900 if (self->fast_memo == NULL) {
901 self->fast_memo = PyDict_New();
902 if (self->fast_memo == NULL) {
903 self->fast_container = -1;
904 return 0;
905 }
906 }
907 key = PyLong_FromVoidPtr(obj);
908 if (key == NULL)
909 return 0;
910 if (PyDict_GetItem(self->fast_memo, key)) {
Tim Peters4e52ca82002-12-07 02:43:28 +0000911 Py_DECREF(key);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000912 PyErr_Format(PyExc_ValueError,
Tim Peters92c8bb32003-02-13 23:00:26 +0000913 "fast mode: can't pickle cyclic objects "
914 "including object type %s at %p",
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000915 obj->ob_type->tp_name, obj);
916 self->fast_container = -1;
917 return 0;
918 }
919 if (PyDict_SetItem(self->fast_memo, key, Py_None) < 0) {
Tim Peters4e52ca82002-12-07 02:43:28 +0000920 Py_DECREF(key);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000921 self->fast_container = -1;
922 return 0;
923 }
Tim Peters4e52ca82002-12-07 02:43:28 +0000924 Py_DECREF(key);
Jeremy Hylton499ab6a2001-10-15 21:37:58 +0000925 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000926 return 1;
Jeremy Hylton499ab6a2001-10-15 21:37:58 +0000927}
928
Tim Peterscba30e22003-02-01 06:24:36 +0000929int
Jeremy Hylton499ab6a2001-10-15 21:37:58 +0000930fast_save_leave(Picklerobject *self, PyObject *obj)
931{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000932 if (self->fast_container-- >= PY_CPICKLE_FAST_LIMIT) {
933 PyObject *key = PyLong_FromVoidPtr(obj);
934 if (key == NULL)
935 return 0;
936 if (PyDict_DelItem(self->fast_memo, key) < 0) {
Tim Peters4e52ca82002-12-07 02:43:28 +0000937 Py_DECREF(key);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000938 return 0;
939 }
Tim Peters4e52ca82002-12-07 02:43:28 +0000940 Py_DECREF(key);
Jeremy Hylton499ab6a2001-10-15 21:37:58 +0000941 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000942 return 1;
Jeremy Hylton499ab6a2001-10-15 21:37:58 +0000943}
944
945static int
Tim Peterscba30e22003-02-01 06:24:36 +0000946save_none(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000947{
948 static char none = NONE;
Tim Peters0bc93f52003-02-02 18:29:33 +0000949 if (self->write_func(self, &none, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000950 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000951
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000952 return 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000953}
954
Guido van Rossum77f6a652002-04-03 22:41:51 +0000955static int
Tim Peterscba30e22003-02-01 06:24:36 +0000956save_bool(Picklerobject *self, PyObject *args)
Guido van Rossum77f6a652002-04-03 22:41:51 +0000957{
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000958 static const char *buf[2] = {FALSE, TRUE};
Guido van Rossume2763392002-04-05 19:30:08 +0000959 static char len[2] = {sizeof(FALSE)-1, sizeof(TRUE)-1};
Guido van Rossum77f6a652002-04-03 22:41:51 +0000960 long l = PyInt_AS_LONG((PyIntObject *)args);
961
Tim Peters3c67d792003-02-02 17:59:11 +0000962 if (self->proto >= 2) {
963 char opcode = l ? NEWTRUE : NEWFALSE;
964 if (self->write_func(self, &opcode, 1) < 0)
965 return -1;
966 }
967 else if (self->write_func(self, buf[l], len[l]) < 0)
Guido van Rossum77f6a652002-04-03 22:41:51 +0000968 return -1;
Guido van Rossum77f6a652002-04-03 22:41:51 +0000969 return 0;
970}
Tim Peters84e87f32001-03-17 04:50:51 +0000971
Guido van Rossum60456fd1997-04-09 17:36:32 +0000972static int
Tim Peterscba30e22003-02-01 06:24:36 +0000973save_int(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000974{
975 char c_str[32];
976 long l = PyInt_AS_LONG((PyIntObject *)args);
977 int len = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000978
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000979 if (!self->bin
Guido van Rossumde8d6d71997-05-13 18:00:44 +0000980#if SIZEOF_LONG > 4
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000981 || l > 0x7fffffffL
982 || l < -0x80000000L
Guido van Rossumde8d6d71997-05-13 18:00:44 +0000983#endif
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000984 ) {
985 /* Text-mode pickle, or long too big to fit in the 4-byte
986 * signed BININT format: store as a string.
987 */
988 c_str[0] = INT;
989 PyOS_snprintf(c_str + 1, sizeof(c_str) - 1, "%ld\n", l);
Tim Peters0bc93f52003-02-02 18:29:33 +0000990 if (self->write_func(self, c_str, strlen(c_str)) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000991 return -1;
992 }
993 else {
994 /* Binary pickle and l fits in a signed 4-byte int. */
995 c_str[1] = (int)( l & 0xff);
996 c_str[2] = (int)((l >> 8) & 0xff);
997 c_str[3] = (int)((l >> 16) & 0xff);
998 c_str[4] = (int)((l >> 24) & 0xff);
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000999
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001000 if ((c_str[4] == 0) && (c_str[3] == 0)) {
1001 if (c_str[2] == 0) {
1002 c_str[0] = BININT1;
1003 len = 2;
1004 }
1005 else {
1006 c_str[0] = BININT2;
1007 len = 3;
1008 }
1009 }
1010 else {
1011 c_str[0] = BININT;
1012 len = 5;
1013 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001014
Tim Peters0bc93f52003-02-02 18:29:33 +00001015 if (self->write_func(self, c_str, len) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001016 return -1;
1017 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001018
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001019 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001020}
1021
1022
1023static int
Tim Peterscba30e22003-02-01 06:24:36 +00001024save_long(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001025{
Tim Petersee1a53c2003-02-02 02:57:53 +00001026 int size;
1027 int res = -1;
1028 PyObject *repr = NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001029
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001030 static char l = LONG;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001031
Tim Petersee1a53c2003-02-02 02:57:53 +00001032 if (self->proto >= 2) {
1033 /* Linear-time pickling. */
1034 size_t nbits;
1035 size_t nbytes;
1036 unsigned char *pdata;
1037 char c_str[5];
1038 int i;
1039 int sign = _PyLong_Sign(args);
1040
1041 if (sign == 0) {
1042 /* It's 0 -- an empty bytestring. */
1043 c_str[0] = LONG1;
1044 c_str[1] = 0;
1045 i = self->write_func(self, c_str, 2);
1046 if (i < 0) goto finally;
1047 res = 0;
1048 goto finally;
1049 }
1050 nbits = _PyLong_NumBits(args);
1051 if (nbits == (size_t)-1 && PyErr_Occurred())
1052 goto finally;
1053 /* How many bytes do we need? There are nbits >> 3 full
1054 * bytes of data, and nbits & 7 leftover bits. If there
1055 * are any leftover bits, then we clearly need another
1056 * byte. Wnat's not so obvious is that we *probably*
1057 * need another byte even if there aren't any leftovers:
1058 * the most-significant bit of the most-significant byte
1059 * acts like a sign bit, and it's usually got a sense
1060 * opposite of the one we need. The exception is longs
1061 * of the form -(2**(8*j-1)) for j > 0. Such a long is
1062 * its own 256's-complement, so has the right sign bit
1063 * even without the extra byte. That's a pain to check
1064 * for in advance, though, so we always grab an extra
1065 * byte at the start, and cut it back later if possible.
1066 */
1067 nbytes = (nbits >> 3) + 1;
1068 if ((int)nbytes < 0 || (size_t)(int)nbytes != nbytes) {
1069 PyErr_SetString(PyExc_OverflowError, "long too large "
1070 "to pickle");
1071 goto finally;
1072 }
1073 repr = PyString_FromStringAndSize(NULL, (int)nbytes);
1074 if (repr == NULL) goto finally;
1075 pdata = (unsigned char *)PyString_AS_STRING(repr);
1076 i = _PyLong_AsByteArray((PyLongObject *)args,
1077 pdata, nbytes,
1078 1 /* little endian */, 1 /* signed */);
1079 if (i < 0) goto finally;
1080 /* If the long is negative, this may be a byte more than
1081 * needed. This is so iff the MSB is all redundant sign
1082 * bits.
1083 */
1084 if (sign < 0 && nbytes > 1 && pdata[nbytes - 1] == 0xff &&
1085 (pdata[nbytes - 2] & 0x80) != 0)
1086 --nbytes;
1087
1088 if (nbytes < 256) {
1089 c_str[0] = LONG1;
1090 c_str[1] = (char)nbytes;
1091 size = 2;
1092 }
1093 else {
1094 c_str[0] = LONG4;
1095 size = (int)nbytes;
1096 for (i = 1; i < 5; i++) {
1097 c_str[i] = (char)(size & 0xff);
1098 size >>= 8;
1099 }
1100 size = 5;
1101 }
1102 i = self->write_func(self, c_str, size);
1103 if (i < 0) goto finally;
1104 i = self->write_func(self, (char *)pdata, (int)nbytes);
1105 if (i < 0) goto finally;
1106 res = 0;
1107 goto finally;
1108 }
1109
1110 /* proto < 2: write the repr and newline. This is quadratic-time
1111 * (in the number of digits), in both directions.
1112 */
Tim Peterscba30e22003-02-01 06:24:36 +00001113 if (!( repr = PyObject_Repr(args)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001114 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001115
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001116 if ((size = PyString_Size(repr)) < 0)
1117 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001118
Tim Peters0bc93f52003-02-02 18:29:33 +00001119 if (self->write_func(self, &l, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001120 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001121
Tim Peters0bc93f52003-02-02 18:29:33 +00001122 if (self->write_func(self,
1123 PyString_AS_STRING((PyStringObject *)repr),
1124 size) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001125 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001126
Tim Peters0bc93f52003-02-02 18:29:33 +00001127 if (self->write_func(self, "\n", 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001128 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001129
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001130 res = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001131
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001132 finally:
1133 Py_XDECREF(repr);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001134 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001135}
1136
1137
1138static int
Tim Peterscba30e22003-02-01 06:24:36 +00001139save_float(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001140{
1141 double x = PyFloat_AS_DOUBLE((PyFloatObject *)args);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001142
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001143 if (self->bin) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001144 char str[9];
Tim Peters9905b942003-03-20 20:53:32 +00001145 str[0] = BINFLOAT;
1146 if (_PyFloat_Pack8(x, (unsigned char *)&str[1], 0) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001147 return -1;
Tim Peters0bc93f52003-02-02 18:29:33 +00001148 if (self->write_func(self, str, 9) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001149 return -1;
1150 }
1151 else {
1152 char c_str[250];
1153 c_str[0] = FLOAT;
1154 PyOS_snprintf(c_str + 1, sizeof(c_str) - 1, "%.17g\n", x);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001155
Tim Peters0bc93f52003-02-02 18:29:33 +00001156 if (self->write_func(self, c_str, strlen(c_str)) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001157 return -1;
1158 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001159
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001160 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001161}
1162
1163
1164static int
Tim Peterscba30e22003-02-01 06:24:36 +00001165save_string(Picklerobject *self, PyObject *args, int doput)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001166{
1167 int size, len;
1168 PyObject *repr=0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001169
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001170 if ((size = PyString_Size(args)) < 0)
1171 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001172
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001173 if (!self->bin) {
1174 char *repr_str;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001175
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001176 static char string = STRING;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001177
Tim Peterscba30e22003-02-01 06:24:36 +00001178 if (!( repr = PyObject_Repr(args)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001179 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001180
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001181 if ((len = PyString_Size(repr)) < 0)
1182 goto err;
1183 repr_str = PyString_AS_STRING((PyStringObject *)repr);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001184
Tim Peters0bc93f52003-02-02 18:29:33 +00001185 if (self->write_func(self, &string, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001186 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001187
Tim Peters0bc93f52003-02-02 18:29:33 +00001188 if (self->write_func(self, repr_str, len) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001189 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001190
Tim Peters0bc93f52003-02-02 18:29:33 +00001191 if (self->write_func(self, "\n", 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001192 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001193
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001194 Py_XDECREF(repr);
1195 }
1196 else {
1197 int i;
1198 char c_str[5];
Guido van Rossum60456fd1997-04-09 17:36:32 +00001199
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001200 if ((size = PyString_Size(args)) < 0)
1201 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001202
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001203 if (size < 256) {
1204 c_str[0] = SHORT_BINSTRING;
1205 c_str[1] = size;
1206 len = 2;
1207 }
1208 else {
1209 c_str[0] = BINSTRING;
1210 for (i = 1; i < 5; i++)
1211 c_str[i] = (int)(size >> ((i - 1) * 8));
1212 len = 5;
1213 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001214
Tim Peters0bc93f52003-02-02 18:29:33 +00001215 if (self->write_func(self, c_str, len) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001216 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001217
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001218 if (size > 128 && Pdata_Check(self->file)) {
1219 if (write_other(self, NULL, 0) < 0) return -1;
1220 PDATA_APPEND(self->file, args, -1);
1221 }
1222 else {
Tim Peters0bc93f52003-02-02 18:29:33 +00001223 if (self->write_func(self,
1224 PyString_AS_STRING(
1225 (PyStringObject *)args),
1226 size) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001227 return -1;
1228 }
1229 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001230
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001231 if (doput)
1232 if (put(self, args) < 0)
1233 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001234
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001235 return 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +00001236
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001237 err:
1238 Py_XDECREF(repr);
1239 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001240}
1241
1242
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001243#ifdef Py_USING_UNICODE
Guido van Rossumfb10c3f2000-12-19 02:08:38 +00001244/* A copy of PyUnicode_EncodeRawUnicodeEscape() that also translates
1245 backslash and newline characters to \uXXXX escapes. */
1246static PyObject *
1247modified_EncodeRawUnicodeEscape(const Py_UNICODE *s, int size)
1248{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001249 PyObject *repr;
1250 char *p;
1251 char *q;
Guido van Rossumfb10c3f2000-12-19 02:08:38 +00001252
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001253 static const char *hexdigit = "0123456789ABCDEF";
Guido van Rossumfb10c3f2000-12-19 02:08:38 +00001254
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001255 repr = PyString_FromStringAndSize(NULL, 6 * size);
1256 if (repr == NULL)
1257 return NULL;
1258 if (size == 0)
1259 return repr;
1260
1261 p = q = PyString_AS_STRING(repr);
1262 while (size-- > 0) {
1263 Py_UNICODE ch = *s++;
1264 /* Map 16-bit characters to '\uxxxx' */
1265 if (ch >= 256 || ch == '\\' || ch == '\n') {
1266 *p++ = '\\';
1267 *p++ = 'u';
1268 *p++ = hexdigit[(ch >> 12) & 0xf];
1269 *p++ = hexdigit[(ch >> 8) & 0xf];
1270 *p++ = hexdigit[(ch >> 4) & 0xf];
1271 *p++ = hexdigit[ch & 15];
1272 }
1273 /* Copy everything else as-is */
1274 else
1275 *p++ = (char) ch;
1276 }
1277 *p = '\0';
Tim Peters5de98422002-04-27 18:44:32 +00001278 _PyString_Resize(&repr, p - q);
Guido van Rossumfb10c3f2000-12-19 02:08:38 +00001279 return repr;
Guido van Rossumfb10c3f2000-12-19 02:08:38 +00001280}
1281
1282
Guido van Rossum60456fd1997-04-09 17:36:32 +00001283static int
Tim Peterscba30e22003-02-01 06:24:36 +00001284save_unicode(Picklerobject *self, PyObject *args, int doput)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001285{
1286 int size, len;
1287 PyObject *repr=0;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001288
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001289 if (!PyUnicode_Check(args))
1290 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001291
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001292 if (!self->bin) {
1293 char *repr_str;
1294 static char string = UNICODE;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001295
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001296 repr = modified_EncodeRawUnicodeEscape(
1297 PyUnicode_AS_UNICODE(args), PyUnicode_GET_SIZE(args));
Tim Peterscba30e22003-02-01 06:24:36 +00001298 if (!repr)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001299 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001300
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001301 if ((len = PyString_Size(repr)) < 0)
1302 goto err;
1303 repr_str = PyString_AS_STRING((PyStringObject *)repr);
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001304
Tim Peters0bc93f52003-02-02 18:29:33 +00001305 if (self->write_func(self, &string, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001306 goto err;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001307
Tim Peters0bc93f52003-02-02 18:29:33 +00001308 if (self->write_func(self, repr_str, len) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001309 goto err;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001310
Tim Peters0bc93f52003-02-02 18:29:33 +00001311 if (self->write_func(self, "\n", 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001312 goto err;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001313
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001314 Py_XDECREF(repr);
1315 }
1316 else {
1317 int i;
1318 char c_str[5];
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001319
Tim Peterscba30e22003-02-01 06:24:36 +00001320 if (!( repr = PyUnicode_AsUTF8String(args)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001321 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001322
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001323 if ((size = PyString_Size(repr)) < 0)
1324 goto err;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001325
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001326 c_str[0] = BINUNICODE;
1327 for (i = 1; i < 5; i++)
1328 c_str[i] = (int)(size >> ((i - 1) * 8));
1329 len = 5;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001330
Tim Peters0bc93f52003-02-02 18:29:33 +00001331 if (self->write_func(self, c_str, len) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001332 goto err;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001333
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001334 if (size > 128 && Pdata_Check(self->file)) {
1335 if (write_other(self, NULL, 0) < 0)
1336 goto err;
1337 PDATA_APPEND(self->file, repr, -1);
1338 }
1339 else {
Tim Peters0bc93f52003-02-02 18:29:33 +00001340 if (self->write_func(self, PyString_AS_STRING(repr),
1341 size) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001342 goto err;
1343 }
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001344
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001345 Py_DECREF(repr);
1346 }
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001347
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001348 if (doput)
1349 if (put(self, args) < 0)
1350 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001351
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001352 return 0;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001353
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001354 err:
1355 Py_XDECREF(repr);
1356 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001357}
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001358#endif
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001359
Tim Peters1d63c9f2003-02-02 20:29:39 +00001360/* A helper for save_tuple. Push the len elements in tuple t on the stack. */
1361static int
Tim Peters67920142003-02-05 03:46:17 +00001362store_tuple_elements(Picklerobject *self, PyObject *t, int len)
Tim Peters1d63c9f2003-02-02 20:29:39 +00001363{
1364 int i;
1365 int res = -1; /* guilty until proved innocent */
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001366
Tim Peters1d63c9f2003-02-02 20:29:39 +00001367 assert(PyTuple_Size(t) == len);
1368
1369 for (i = 0; i < len; i++) {
1370 PyObject *element = PyTuple_GET_ITEM(t, i);
1371
1372 if (element == NULL)
1373 goto finally;
1374 if (save(self, element, 0) < 0)
1375 goto finally;
1376 }
1377 res = 0;
1378
1379 finally:
1380 return res;
1381}
1382
1383/* Tuples are ubiquitous in the pickle protocols, so many techniques are
1384 * used across protocols to minimize the space needed to pickle them.
Tim Peters67920142003-02-05 03:46:17 +00001385 * Tuples are also the only builtin immutable type that can be recursive
Tim Peters1d63c9f2003-02-02 20:29:39 +00001386 * (a tuple can be reached from itself), and that requires some subtle
1387 * magic so that it works in all cases. IOW, this is a long routine.
1388 */
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001389static int
Tim Peterscba30e22003-02-01 06:24:36 +00001390save_tuple(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001391{
Tim Peters1d63c9f2003-02-02 20:29:39 +00001392 PyObject *py_tuple_id = NULL;
1393 int len, i;
1394 int res = -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001395
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001396 static char tuple = TUPLE;
Tim Peters1d63c9f2003-02-02 20:29:39 +00001397 static char pop = POP;
1398 static char pop_mark = POP_MARK;
1399 static char len2opcode[] = {EMPTY_TUPLE, TUPLE1, TUPLE2, TUPLE3};
Guido van Rossum60456fd1997-04-09 17:36:32 +00001400
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001401 if ((len = PyTuple_Size(args)) < 0)
1402 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001403
Tim Peters1d63c9f2003-02-02 20:29:39 +00001404 if (len == 0) {
1405 char c_str[2];
Tim Peters84e87f32001-03-17 04:50:51 +00001406
Tim Peters1d63c9f2003-02-02 20:29:39 +00001407 if (self->proto) {
1408 c_str[0] = EMPTY_TUPLE;
1409 len = 1;
1410 }
1411 else {
1412 c_str[0] = MARK;
1413 c_str[1] = TUPLE;
1414 len = 2;
1415 }
1416 if (self->write_func(self, c_str, len) >= 0)
1417 res = 0;
1418 /* Don't memoize an empty tuple. */
1419 goto finally;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001420 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001421
Tim Peters1d63c9f2003-02-02 20:29:39 +00001422 /* A non-empty tuple. */
1423
1424 /* id(tuple) isn't in the memo now. If it shows up there after
1425 * saving the tuple elements, the tuple must be recursive, in
1426 * which case we'll pop everything we put on the stack, and fetch
1427 * its value from the memo.
1428 */
1429 py_tuple_id = PyLong_FromVoidPtr(args);
1430 if (py_tuple_id == NULL)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001431 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001432
Tim Peters1d63c9f2003-02-02 20:29:39 +00001433 if (len <= 3 && self->proto >= 2) {
1434 /* Use TUPLE{1,2,3} opcodes. */
Tim Peters67920142003-02-05 03:46:17 +00001435 if (store_tuple_elements(self, args, len) < 0)
Tim Peters1d63c9f2003-02-02 20:29:39 +00001436 goto finally;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001437 if (PyDict_GetItem(self->memo, py_tuple_id)) {
Tim Peters1d63c9f2003-02-02 20:29:39 +00001438 /* pop the len elements */
1439 for (i = 0; i < len; ++i)
1440 if (self->write_func(self, &pop, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001441 goto finally;
Tim Peters1d63c9f2003-02-02 20:29:39 +00001442 /* fetch from memo */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001443 if (get(self, py_tuple_id) < 0)
1444 goto finally;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001445 res = 0;
1446 goto finally;
1447 }
Tim Peters1d63c9f2003-02-02 20:29:39 +00001448 /* Not recursive. */
1449 if (self->write_func(self, len2opcode + len, 1) < 0)
1450 goto finally;
1451 goto memoize;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001452 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001453
Tim Peters1d63c9f2003-02-02 20:29:39 +00001454 /* proto < 2 and len > 0, or proto >= 2 and len > 3.
1455 * Generate MARK elt1 elt2 ... TUPLE
1456 */
1457 if (self->write_func(self, &MARKv, 1) < 0)
1458 goto finally;
1459
Tim Peters67920142003-02-05 03:46:17 +00001460 if (store_tuple_elements(self, args, len) < 0)
Tim Peters1d63c9f2003-02-02 20:29:39 +00001461 goto finally;
1462
1463 if (PyDict_GetItem(self->memo, py_tuple_id)) {
1464 /* pop the stack stuff we pushed */
1465 if (self->bin) {
1466 if (self->write_func(self, &pop_mark, 1) < 0)
1467 goto finally;
1468 }
1469 else {
1470 /* Note that we pop one more than len, to remove
1471 * the MARK too.
1472 */
1473 for (i = 0; i <= len; i++)
1474 if (self->write_func(self, &pop, 1) < 0)
1475 goto finally;
1476 }
1477 /* fetch from memo */
1478 if (get(self, py_tuple_id) >= 0)
1479 res = 0;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001480 goto finally;
1481 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001482
Tim Peters1d63c9f2003-02-02 20:29:39 +00001483 /* Not recursive. */
1484 if (self->write_func(self, &tuple, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001485 goto finally;
Tim Peters84e87f32001-03-17 04:50:51 +00001486
Tim Peters1d63c9f2003-02-02 20:29:39 +00001487 memoize:
1488 if (put(self, args) >= 0)
1489 res = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001490
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001491 finally:
1492 Py_XDECREF(py_tuple_id);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001493 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001494}
1495
Tim Peters1092d642003-02-11 21:06:20 +00001496/* iter is an iterator giving items, and we batch up chunks of
1497 * MARK item item ... item APPENDS
1498 * opcode sequences. Calling code should have arranged to first create an
1499 * empty list, or list-like object, for the APPENDS to operate on.
1500 * Returns 0 on success, <0 on error.
1501 */
1502static int
1503batch_list(Picklerobject *self, PyObject *iter)
1504{
1505 PyObject *obj;
1506 PyObject *slice[BATCHSIZE];
1507 int i, n;
1508
1509 static char append = APPEND;
1510 static char appends = APPENDS;
1511
1512 assert(iter != NULL);
1513
1514 if (self->proto == 0) {
1515 /* APPENDS isn't available; do one at a time. */
1516 for (;;) {
1517 obj = PyIter_Next(iter);
1518 if (obj == NULL) {
1519 if (PyErr_Occurred())
1520 return -1;
1521 break;
1522 }
1523 i = save(self, obj, 0);
1524 Py_DECREF(obj);
1525 if (i < 0)
1526 return -1;
1527 if (self->write_func(self, &append, 1) < 0)
1528 return -1;
Tim Peters1092d642003-02-11 21:06:20 +00001529 }
1530 return 0;
1531 }
1532
1533 /* proto > 0: write in batches of BATCHSIZE. */
1534 do {
1535 /* Get next group of (no more than) BATCHSIZE elements. */
1536 for (n = 0; n < BATCHSIZE; ++n) {
1537 obj = PyIter_Next(iter);
1538 if (obj == NULL) {
1539 if (PyErr_Occurred())
1540 goto BatchFailed;
1541 break;
1542 }
1543 slice[n] = obj;
1544 }
1545
1546 if (n > 1) {
1547 /* Pump out MARK, slice[0:n], APPENDS. */
1548 if (self->write_func(self, &MARKv, 1) < 0)
1549 goto BatchFailed;
1550 for (i = 0; i < n; ++i) {
1551 if (save(self, slice[i], 0) < 0)
1552 goto BatchFailed;
1553 }
1554 if (self->write_func(self, &appends, 1) < 0)
1555 goto BatchFailed;
1556 }
1557 else if (n == 1) {
1558 if (save(self, slice[0], 0) < 0)
1559 goto BatchFailed;
1560 if (self->write_func(self, &append, 1) < 0)
1561 goto BatchFailed;
1562 }
1563
1564 for (i = 0; i < n; ++i) {
1565 Py_DECREF(slice[i]);
1566 }
Tim Peters90975f12003-02-12 05:28:58 +00001567 } while (n == BATCHSIZE);
Tim Peters1092d642003-02-11 21:06:20 +00001568 return 0;
1569
1570BatchFailed:
1571 while (--n >= 0) {
1572 Py_DECREF(slice[n]);
1573 }
1574 return -1;
1575}
1576
Guido van Rossum60456fd1997-04-09 17:36:32 +00001577static int
Tim Peterscba30e22003-02-01 06:24:36 +00001578save_list(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001579{
Tim Peters1092d642003-02-11 21:06:20 +00001580 int res = -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001581 char s[3];
Tim Peters1092d642003-02-11 21:06:20 +00001582 int len;
1583 PyObject *iter;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001584
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001585 if (self->fast && !fast_save_enter(self, args))
1586 goto finally;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00001587
Tim Peters1092d642003-02-11 21:06:20 +00001588 /* Create an empty list. */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001589 if (self->bin) {
1590 s[0] = EMPTY_LIST;
Tim Peters1092d642003-02-11 21:06:20 +00001591 len = 1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001592 }
1593 else {
1594 s[0] = MARK;
1595 s[1] = LIST;
Tim Peters1092d642003-02-11 21:06:20 +00001596 len = 2;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001597 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001598
Tim Peters1092d642003-02-11 21:06:20 +00001599 if (self->write_func(self, s, len) < 0)
1600 goto finally;
1601
1602 /* Get list length, and bow out early if empty. */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001603 if ((len = PyList_Size(args)) < 0)
1604 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001605
Tim Peters1092d642003-02-11 21:06:20 +00001606 /* Memoize. */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001607 if (len == 0) {
Tim Peters90975f12003-02-12 05:28:58 +00001608 if (put(self, args) >= 0)
1609 res = 0;
1610 goto finally;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001611 }
Tim Peters90975f12003-02-12 05:28:58 +00001612 if (put2(self, args) < 0)
1613 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001614
Tim Peters1092d642003-02-11 21:06:20 +00001615 /* Materialize the list elements. */
1616 iter = PyObject_GetIter(args);
1617 if (iter == NULL)
1618 goto finally;
1619 res = batch_list(self, iter);
1620 Py_DECREF(iter);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001621
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001622 finally:
1623 if (self->fast && !fast_save_leave(self, args))
1624 res = -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001625
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001626 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001627}
1628
1629
Tim Peters42f08ac2003-02-11 22:43:24 +00001630/* iter is an iterator giving (key, value) pairs, and we batch up chunks of
1631 * MARK key value ... key value SETITEMS
1632 * opcode sequences. Calling code should have arranged to first create an
1633 * empty dict, or dict-like object, for the SETITEMS to operate on.
1634 * Returns 0 on success, <0 on error.
1635 *
1636 * This is very much like batch_list(). The difference between saving
1637 * elements directly, and picking apart two-tuples, is so long-winded at
1638 * the C level, though, that attempts to combine these routines were too
1639 * ugly to bear.
1640 */
1641static int
1642batch_dict(Picklerobject *self, PyObject *iter)
1643{
1644 PyObject *p;
1645 PyObject *slice[BATCHSIZE];
1646 int i, n;
1647
1648 static char setitem = SETITEM;
1649 static char setitems = SETITEMS;
1650
1651 assert(iter != NULL);
1652
1653 if (self->proto == 0) {
1654 /* SETITEMS isn't available; do one at a time. */
1655 for (;;) {
1656 p = PyIter_Next(iter);
1657 if (p == NULL) {
1658 if (PyErr_Occurred())
1659 return -1;
1660 break;
1661 }
1662 if (!PyTuple_Check(p) || PyTuple_Size(p) != 2) {
1663 PyErr_SetString(PyExc_TypeError, "dict items "
1664 "iterator must return 2-tuples");
1665 return -1;
1666 }
1667 i = save(self, PyTuple_GET_ITEM(p, 0), 0);
1668 if (i >= 0)
1669 i = save(self, PyTuple_GET_ITEM(p, 1), 0);
1670 Py_DECREF(p);
1671 if (i < 0)
1672 return -1;
1673 if (self->write_func(self, &setitem, 1) < 0)
1674 return -1;
Tim Peters42f08ac2003-02-11 22:43:24 +00001675 }
1676 return 0;
1677 }
1678
1679 /* proto > 0: write in batches of BATCHSIZE. */
1680 do {
1681 /* Get next group of (no more than) BATCHSIZE elements. */
1682 for (n = 0; n < BATCHSIZE; ++n) {
1683 p = PyIter_Next(iter);
1684 if (p == NULL) {
1685 if (PyErr_Occurred())
1686 goto BatchFailed;
1687 break;
1688 }
1689 if (!PyTuple_Check(p) || PyTuple_Size(p) != 2) {
1690 PyErr_SetString(PyExc_TypeError, "dict items "
1691 "iterator must return 2-tuples");
1692 goto BatchFailed;
1693 }
1694 slice[n] = p;
1695 }
1696
1697 if (n > 1) {
1698 /* Pump out MARK, slice[0:n], SETITEMS. */
1699 if (self->write_func(self, &MARKv, 1) < 0)
1700 goto BatchFailed;
1701 for (i = 0; i < n; ++i) {
1702 p = slice[i];
1703 if (save(self, PyTuple_GET_ITEM(p, 0), 0) < 0)
1704 goto BatchFailed;
1705 if (save(self, PyTuple_GET_ITEM(p, 1), 0) < 0)
1706 goto BatchFailed;
1707 }
1708 if (self->write_func(self, &setitems, 1) < 0)
1709 goto BatchFailed;
1710 }
1711 else if (n == 1) {
1712 p = slice[0];
1713 if (save(self, PyTuple_GET_ITEM(p, 0), 0) < 0)
1714 goto BatchFailed;
1715 if (save(self, PyTuple_GET_ITEM(p, 1), 0) < 0)
1716 goto BatchFailed;
1717 if (self->write_func(self, &setitem, 1) < 0)
1718 goto BatchFailed;
1719 }
1720
1721 for (i = 0; i < n; ++i) {
1722 Py_DECREF(slice[i]);
1723 }
Tim Peters90975f12003-02-12 05:28:58 +00001724 } while (n == BATCHSIZE);
Tim Peters42f08ac2003-02-11 22:43:24 +00001725 return 0;
1726
1727BatchFailed:
1728 while (--n >= 0) {
1729 Py_DECREF(slice[n]);
1730 }
1731 return -1;
1732}
1733
Guido van Rossum60456fd1997-04-09 17:36:32 +00001734static int
Tim Peterscba30e22003-02-01 06:24:36 +00001735save_dict(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001736{
Tim Peters42f08ac2003-02-11 22:43:24 +00001737 int res = -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001738 char s[3];
Tim Peters42f08ac2003-02-11 22:43:24 +00001739 int len;
1740 PyObject *iter;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001741
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001742 if (self->fast && !fast_save_enter(self, args))
1743 goto finally;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00001744
Tim Peters42f08ac2003-02-11 22:43:24 +00001745 /* Create an empty dict. */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001746 if (self->bin) {
1747 s[0] = EMPTY_DICT;
1748 len = 1;
1749 }
1750 else {
1751 s[0] = MARK;
1752 s[1] = DICT;
1753 len = 2;
1754 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001755
Tim Peters0bc93f52003-02-02 18:29:33 +00001756 if (self->write_func(self, s, len) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001757 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001758
Tim Peters42f08ac2003-02-11 22:43:24 +00001759 /* Get dict size, and bow out early if empty. */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001760 if ((len = PyDict_Size(args)) < 0)
1761 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001762
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001763 if (len == 0) {
Tim Peters90975f12003-02-12 05:28:58 +00001764 if (put(self, args) >= 0)
1765 res = 0;
1766 goto finally;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001767 }
Tim Peters90975f12003-02-12 05:28:58 +00001768 if (put2(self, args) < 0)
1769 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001770
Tim Peters42f08ac2003-02-11 22:43:24 +00001771 /* Materialize the dict items. */
1772 iter = PyObject_CallMethod(args, "iteritems", "()");
1773 if (iter == NULL)
1774 goto finally;
1775 res = batch_dict(self, iter);
1776 Py_DECREF(iter);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001777
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001778 finally:
1779 if (self->fast && !fast_save_leave(self, args))
1780 res = -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001781
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001782 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001783}
1784
1785
Tim Peters84e87f32001-03-17 04:50:51 +00001786static int
Tim Peterscba30e22003-02-01 06:24:36 +00001787save_inst(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001788{
1789 PyObject *class = 0, *module = 0, *name = 0, *state = 0,
1790 *getinitargs_func = 0, *getstate_func = 0, *class_args = 0;
1791 char *module_str, *name_str;
1792 int module_size, name_size, res = -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001793
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001794 static char inst = INST, obj = OBJ, build = BUILD;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001795
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001796 if (self->fast && !fast_save_enter(self, args))
1797 goto finally;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00001798
Tim Peters0bc93f52003-02-02 18:29:33 +00001799 if (self->write_func(self, &MARKv, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001800 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001801
Tim Peterscba30e22003-02-01 06:24:36 +00001802 if (!( class = PyObject_GetAttr(args, __class___str)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001803 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001804
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001805 if (self->bin) {
1806 if (save(self, class, 0) < 0)
1807 goto finally;
1808 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001809
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001810 if ((getinitargs_func = PyObject_GetAttr(args, __getinitargs___str))) {
1811 PyObject *element = 0;
1812 int i, len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001813
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001814 if (!( class_args =
Tim Peterscba30e22003-02-01 06:24:36 +00001815 PyObject_Call(getinitargs_func, empty_tuple, NULL)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001816 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001817
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001818 if ((len = PyObject_Size(class_args)) < 0)
1819 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001820
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001821 for (i = 0; i < len; i++) {
Tim Peterscba30e22003-02-01 06:24:36 +00001822 if (!( element = PySequence_GetItem(class_args, i)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001823 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001824
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001825 if (save(self, element, 0) < 0) {
1826 Py_DECREF(element);
1827 goto finally;
1828 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001829
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001830 Py_DECREF(element);
1831 }
1832 }
1833 else {
Jeremy Hyltonf00368f2003-06-16 20:19:49 +00001834 if (PyErr_ExceptionMatches(PyExc_AttributeError))
1835 PyErr_Clear();
1836 else
1837 goto finally;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001838 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001839
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001840 if (!self->bin) {
1841 if (!( name = ((PyClassObject *)class)->cl_name )) {
1842 PyErr_SetString(PicklingError, "class has no name");
1843 goto finally;
1844 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001845
Tim Peterscba30e22003-02-01 06:24:36 +00001846 if (!( module = whichmodule(class, name)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001847 goto finally;
Guido van Rossum053b8df1998-11-25 16:18:00 +00001848
Tim Peters84e87f32001-03-17 04:50:51 +00001849
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001850 if ((module_size = PyString_Size(module)) < 0 ||
1851 (name_size = PyString_Size(name)) < 0)
1852 goto finally;
Guido van Rossum053b8df1998-11-25 16:18:00 +00001853
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001854 module_str = PyString_AS_STRING((PyStringObject *)module);
1855 name_str = PyString_AS_STRING((PyStringObject *)name);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001856
Tim Peters0bc93f52003-02-02 18:29:33 +00001857 if (self->write_func(self, &inst, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001858 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001859
Tim Peters0bc93f52003-02-02 18:29:33 +00001860 if (self->write_func(self, module_str, module_size) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001861 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001862
Tim Peters0bc93f52003-02-02 18:29:33 +00001863 if (self->write_func(self, "\n", 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001864 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001865
Tim Peters0bc93f52003-02-02 18:29:33 +00001866 if (self->write_func(self, name_str, name_size) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001867 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001868
Tim Peters0bc93f52003-02-02 18:29:33 +00001869 if (self->write_func(self, "\n", 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001870 goto finally;
1871 }
Tim Peters0bc93f52003-02-02 18:29:33 +00001872 else if (self->write_func(self, &obj, 1) < 0) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001873 goto finally;
1874 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001875
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001876 if ((getstate_func = PyObject_GetAttr(args, __getstate___str))) {
1877 state = PyObject_Call(getstate_func, empty_tuple, NULL);
Tim Peterscba30e22003-02-01 06:24:36 +00001878 if (!state)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001879 goto finally;
1880 }
1881 else {
Jeremy Hyltonf00368f2003-06-16 20:19:49 +00001882 if (PyErr_ExceptionMatches(PyExc_AttributeError))
1883 PyErr_Clear();
1884 else
1885 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001886
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001887 if (!( state = PyObject_GetAttr(args, __dict___str))) {
Jeremy Hyltonf00368f2003-06-16 20:19:49 +00001888 if (PyErr_ExceptionMatches(PyExc_AttributeError))
1889 PyErr_Clear();
1890 else
1891 goto finally;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001892 res = 0;
1893 goto finally;
1894 }
1895 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001896
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001897 if (!PyDict_Check(state)) {
1898 if (put2(self, args) < 0)
1899 goto finally;
1900 }
1901 else {
1902 if (put(self, args) < 0)
1903 goto finally;
1904 }
Tim Peters84e87f32001-03-17 04:50:51 +00001905
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001906 if (save(self, state, 0) < 0)
1907 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001908
Tim Peters0bc93f52003-02-02 18:29:33 +00001909 if (self->write_func(self, &build, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001910 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001911
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001912 res = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001913
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001914 finally:
1915 if (self->fast && !fast_save_leave(self, args))
1916 res = -1;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00001917
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001918 Py_XDECREF(module);
1919 Py_XDECREF(class);
1920 Py_XDECREF(state);
1921 Py_XDECREF(getinitargs_func);
1922 Py_XDECREF(getstate_func);
1923 Py_XDECREF(class_args);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001924
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001925 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001926}
1927
1928
Guido van Rossum60456fd1997-04-09 17:36:32 +00001929static int
Tim Peterscba30e22003-02-01 06:24:36 +00001930save_global(Picklerobject *self, PyObject *args, PyObject *name)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001931{
Jeremy Hylton9ee91f12002-07-11 22:02:33 +00001932 PyObject *global_name = 0, *module = 0, *mod = 0, *klass = 0;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001933 char *name_str, *module_str;
1934 int module_size, name_size, res = -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001935
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001936 static char global = GLOBAL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001937
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001938 if (name) {
1939 global_name = name;
1940 Py_INCREF(global_name);
1941 }
1942 else {
Tim Peterscba30e22003-02-01 06:24:36 +00001943 if (!( global_name = PyObject_GetAttr(args, __name___str)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001944 goto finally;
1945 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001946
Tim Peterscba30e22003-02-01 06:24:36 +00001947 if (!( module = whichmodule(args, global_name)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001948 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001949
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001950 if ((module_size = PyString_Size(module)) < 0 ||
1951 (name_size = PyString_Size(global_name)) < 0)
1952 goto finally;
Guido van Rossum053b8df1998-11-25 16:18:00 +00001953
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001954 module_str = PyString_AS_STRING((PyStringObject *)module);
1955 name_str = PyString_AS_STRING((PyStringObject *)global_name);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001956
Guido van Rossum75bfd052002-12-24 18:10:07 +00001957 /* XXX This can be doing a relative import. Clearly it shouldn't,
1958 but I don't know how to stop it. :-( */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001959 mod = PyImport_ImportModule(module_str);
1960 if (mod == NULL) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001961 cPickle_ErrFormat(PicklingError,
Tim Petersb9ce7cd2003-02-18 20:50:45 +00001962 "Can't pickle %s: import of module %s "
1963 "failed",
1964 "OS", args, module);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001965 goto finally;
1966 }
Jeremy Hylton0e1f7a82002-07-11 22:01:40 +00001967 klass = PyObject_GetAttrString(mod, name_str);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001968 if (klass == NULL) {
1969 cPickle_ErrFormat(PicklingError,
Tim Petersb9ce7cd2003-02-18 20:50:45 +00001970 "Can't pickle %s: attribute lookup %s.%s "
1971 "failed",
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001972 "OSS", args, module, global_name);
1973 goto finally;
1974 }
1975 if (klass != args) {
Jeremy Hylton0e1f7a82002-07-11 22:01:40 +00001976 Py_DECREF(klass);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001977 cPickle_ErrFormat(PicklingError,
Tim Peters731098b2003-02-04 20:56:09 +00001978 "Can't pickle %s: it's not the same object "
1979 "as %s.%s",
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001980 "OSS", args, module, global_name);
1981 goto finally;
1982 }
Jeremy Hylton0e1f7a82002-07-11 22:01:40 +00001983 Py_DECREF(klass);
Guido van Rossuma92d16a2001-08-18 21:22:07 +00001984
Tim Peters731098b2003-02-04 20:56:09 +00001985 if (self->proto >= 2) {
1986 /* See whether this is in the extension registry, and if
1987 * so generate an EXT opcode.
1988 */
1989 PyObject *py_code; /* extension code as Python object */
Tim Peters3e667d52003-02-04 21:47:44 +00001990 long code; /* extension code as C value */
Tim Peters731098b2003-02-04 20:56:09 +00001991 char c_str[5];
1992 int n;
1993
1994 PyTuple_SET_ITEM(two_tuple, 0, module);
1995 PyTuple_SET_ITEM(two_tuple, 1, global_name);
1996 py_code = PyDict_GetItem(extension_registry, two_tuple);
1997 if (py_code == NULL)
1998 goto gen_global; /* not registered */
1999
2000 /* Verify py_code has the right type and value. */
2001 if (!PyInt_Check(py_code)) {
2002 cPickle_ErrFormat(PicklingError, "Can't pickle %s: "
Tim Peters6288e232003-02-05 03:53:10 +00002003 "extension code %s isn't an integer",
Tim Peters731098b2003-02-04 20:56:09 +00002004 "OO", args, py_code);
2005 goto finally;
2006 }
2007 code = PyInt_AS_LONG(py_code);
2008 if (code <= 0 || code > 0x7fffffffL) {
2009 cPickle_ErrFormat(PicklingError, "Can't pickle %s: "
2010 "extension code %ld is out of range",
2011 "Ol", args, code);
2012 goto finally;
2013 }
2014
2015 /* Generate an EXT opcode. */
2016 if (code <= 0xff) {
2017 c_str[0] = EXT1;
2018 c_str[1] = (char)code;
2019 n = 2;
2020 }
2021 else if (code <= 0xffff) {
2022 c_str[0] = EXT2;
2023 c_str[1] = (char)(code & 0xff);
2024 c_str[2] = (char)((code >> 8) & 0xff);
2025 n = 3;
2026 }
2027 else {
2028 c_str[0] = EXT4;
2029 c_str[1] = (char)(code & 0xff);
2030 c_str[2] = (char)((code >> 8) & 0xff);
2031 c_str[3] = (char)((code >> 16) & 0xff);
2032 c_str[4] = (char)((code >> 24) & 0xff);
2033 n = 5;
2034 }
2035
2036 if (self->write_func(self, c_str, n) >= 0)
2037 res = 0;
2038 goto finally; /* and don't memoize */
2039 }
2040
2041 gen_global:
Tim Peters0bc93f52003-02-02 18:29:33 +00002042 if (self->write_func(self, &global, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002043 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002044
Tim Peters0bc93f52003-02-02 18:29:33 +00002045 if (self->write_func(self, module_str, module_size) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002046 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002047
Tim Peters0bc93f52003-02-02 18:29:33 +00002048 if (self->write_func(self, "\n", 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002049 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002050
Tim Peters0bc93f52003-02-02 18:29:33 +00002051 if (self->write_func(self, name_str, name_size) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002052 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002053
Tim Peters0bc93f52003-02-02 18:29:33 +00002054 if (self->write_func(self, "\n", 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002055 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002056
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002057 if (put(self, args) < 0)
2058 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002059
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002060 res = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002061
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002062 finally:
2063 Py_XDECREF(module);
2064 Py_XDECREF(global_name);
2065 Py_XDECREF(mod);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002066
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002067 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002068}
2069
Guido van Rossum60456fd1997-04-09 17:36:32 +00002070static int
Tim Peterscba30e22003-02-01 06:24:36 +00002071save_pers(Picklerobject *self, PyObject *args, PyObject *f)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002072{
2073 PyObject *pid = 0;
2074 int size, res = -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002075
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002076 static char persid = PERSID, binpersid = BINPERSID;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002077
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002078 Py_INCREF(args);
2079 ARG_TUP(self, args);
2080 if (self->arg) {
2081 pid = PyObject_Call(f, self->arg, NULL);
2082 FREE_ARG_TUP(self);
2083 }
2084 if (! pid) return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002085
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002086 if (pid != Py_None) {
2087 if (!self->bin) {
2088 if (!PyString_Check(pid)) {
2089 PyErr_SetString(PicklingError,
2090 "persistent id must be string");
2091 goto finally;
2092 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002093
Tim Peters0bc93f52003-02-02 18:29:33 +00002094 if (self->write_func(self, &persid, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002095 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002096
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002097 if ((size = PyString_Size(pid)) < 0)
2098 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002099
Tim Peters0bc93f52003-02-02 18:29:33 +00002100 if (self->write_func(self,
2101 PyString_AS_STRING(
2102 (PyStringObject *)pid),
2103 size) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002104 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002105
Tim Peters0bc93f52003-02-02 18:29:33 +00002106 if (self->write_func(self, "\n", 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002107 goto finally;
Tim Peters84e87f32001-03-17 04:50:51 +00002108
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002109 res = 1;
2110 goto finally;
2111 }
2112 else if (save(self, pid, 1) >= 0) {
Tim Peters0bc93f52003-02-02 18:29:33 +00002113 if (self->write_func(self, &binpersid, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002114 res = -1;
2115 else
2116 res = 1;
2117 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002118
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002119 goto finally;
2120 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002121
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002122 res = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002123
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002124 finally:
2125 Py_XDECREF(pid);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002126
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002127 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002128}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002129
Tim Peters71fcda52003-02-14 23:05:28 +00002130/* We're saving ob, and args is the 2-thru-5 tuple returned by the
2131 * appropriate __reduce__ method for ob.
2132 */
Tim Peters84e87f32001-03-17 04:50:51 +00002133static int
Tim Peters71fcda52003-02-14 23:05:28 +00002134save_reduce(Picklerobject *self, PyObject *args, PyObject *ob)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002135{
Tim Peters71fcda52003-02-14 23:05:28 +00002136 PyObject *callable;
2137 PyObject *argtup;
2138 PyObject *state = NULL;
2139 PyObject *listitems = NULL;
2140 PyObject *dictitems = NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002141
Tim Peters71fcda52003-02-14 23:05:28 +00002142 int use_newobj = self->proto >= 2;
2143
2144 static char reduce = REDUCE;
2145 static char build = BUILD;
2146 static char newobj = NEWOBJ;
2147
2148 if (! PyArg_UnpackTuple(args, "save_reduce", 2, 5,
2149 &callable,
2150 &argtup,
2151 &state,
2152 &listitems,
2153 &dictitems))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002154 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002155
Raymond Hettingera6b45cc2004-12-07 07:05:57 +00002156 if (!PyTuple_Check(argtup)) {
2157 PyErr_SetString(PicklingError,
2158 "args from reduce() should be a tuple");
2159 return -1;
2160 }
2161
Tim Peters71fcda52003-02-14 23:05:28 +00002162 if (state == Py_None)
2163 state = NULL;
2164 if (listitems == Py_None)
2165 listitems = NULL;
2166 if (dictitems == Py_None)
2167 dictitems = NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002168
Tim Peters71fcda52003-02-14 23:05:28 +00002169 /* Protocol 2 special case: if callable's name is __newobj__, use
2170 * NEWOBJ. This consumes a lot of code.
2171 */
2172 if (use_newobj) {
2173 PyObject *temp = PyObject_GetAttr(callable, __name___str);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002174
Tim Peters71fcda52003-02-14 23:05:28 +00002175 if (temp == NULL) {
Jeremy Hyltonf00368f2003-06-16 20:19:49 +00002176 if (PyErr_ExceptionMatches(PyExc_AttributeError))
2177 PyErr_Clear();
2178 else
2179 return -1;
Tim Peters71fcda52003-02-14 23:05:28 +00002180 use_newobj = 0;
2181 }
2182 else {
2183 use_newobj = PyString_Check(temp) &&
2184 strcmp(PyString_AS_STRING(temp),
2185 "__newobj__") == 0;
2186 Py_DECREF(temp);
2187 }
2188 }
2189 if (use_newobj) {
2190 PyObject *cls;
2191 PyObject *newargtup;
2192 int n, i;
2193
2194 /* Sanity checks. */
2195 n = PyTuple_Size(argtup);
2196 if (n < 1) {
2197 PyErr_SetString(PicklingError, "__newobj__ arglist "
2198 "is empty");
2199 return -1;
2200 }
2201
2202 cls = PyTuple_GET_ITEM(argtup, 0);
2203 if (! PyObject_HasAttrString(cls, "__new__")) {
2204 PyErr_SetString(PicklingError, "args[0] from "
2205 "__newobj__ args has no __new__");
2206 return -1;
2207 }
2208
2209 /* XXX How could ob be NULL? */
2210 if (ob != NULL) {
2211 PyObject *ob_dot_class;
2212
2213 ob_dot_class = PyObject_GetAttr(ob, __class___str);
Jeremy Hyltonf00368f2003-06-16 20:19:49 +00002214 if (ob_dot_class == NULL) {
2215 if (PyErr_ExceptionMatches(
2216 PyExc_AttributeError))
2217 PyErr_Clear();
2218 else
2219 return -1;
2220 }
Tim Peters71fcda52003-02-14 23:05:28 +00002221 i = ob_dot_class != cls; /* true iff a problem */
2222 Py_XDECREF(ob_dot_class);
2223 if (i) {
2224 PyErr_SetString(PicklingError, "args[0] from "
2225 "__newobj__ args has the wrong class");
2226 return -1;
2227 }
2228 }
2229
2230 /* Save the class and its __new__ arguments. */
2231 if (save(self, cls, 0) < 0)
2232 return -1;
2233
2234 newargtup = PyTuple_New(n-1); /* argtup[1:] */
2235 if (newargtup == NULL)
2236 return -1;
2237 for (i = 1; i < n; ++i) {
2238 PyObject *temp = PyTuple_GET_ITEM(argtup, i);
2239 Py_INCREF(temp);
2240 PyTuple_SET_ITEM(newargtup, i-1, temp);
2241 }
2242 i = save(self, newargtup, 0) < 0;
2243 Py_DECREF(newargtup);
2244 if (i < 0)
2245 return -1;
2246
2247 /* Add NEWOBJ opcode. */
2248 if (self->write_func(self, &newobj, 1) < 0)
2249 return -1;
2250 }
2251 else {
2252 /* Not using NEWOBJ. */
2253 if (save(self, callable, 0) < 0 ||
2254 save(self, argtup, 0) < 0 ||
2255 self->write_func(self, &reduce, 1) < 0)
2256 return -1;
2257 }
2258
2259 /* Memoize. */
2260 /* XXX How can ob be NULL? */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002261 if (ob != NULL) {
2262 if (state && !PyDict_Check(state)) {
2263 if (put2(self, ob) < 0)
2264 return -1;
2265 }
Tim Peters71fcda52003-02-14 23:05:28 +00002266 else if (put(self, ob) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002267 return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002268 }
Tim Peters84e87f32001-03-17 04:50:51 +00002269
Guido van Rossum60456fd1997-04-09 17:36:32 +00002270
Tim Peters71fcda52003-02-14 23:05:28 +00002271 if (listitems && batch_list(self, listitems) < 0)
2272 return -1;
2273
2274 if (dictitems && batch_dict(self, dictitems) < 0)
2275 return -1;
2276
2277 if (state) {
2278 if (save(self, state, 0) < 0 ||
2279 self->write_func(self, &build, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002280 return -1;
2281 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002282
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002283 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002284}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002285
Guido van Rossum60456fd1997-04-09 17:36:32 +00002286static int
Tim Peters1d63c9f2003-02-02 20:29:39 +00002287save(Picklerobject *self, PyObject *args, int pers_save)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002288{
2289 PyTypeObject *type;
Tim Peters71fcda52003-02-14 23:05:28 +00002290 PyObject *py_ob_id = 0, *__reduce__ = 0, *t = 0;
2291 PyObject *arg_tup;
2292 int res = -1;
2293 int tmp, size;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002294
Martin v. Löwis5a395302002-08-04 08:20:23 +00002295 if (self->nesting++ > Py_GetRecursionLimit()){
2296 PyErr_SetString(PyExc_RuntimeError,
2297 "maximum recursion depth exceeded");
2298 goto finally;
2299 }
2300
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002301 if (!pers_save && self->pers_func) {
2302 if ((tmp = save_pers(self, args, self->pers_func)) != 0) {
2303 res = tmp;
2304 goto finally;
2305 }
2306 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002307
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002308 if (args == Py_None) {
2309 res = save_none(self, args);
2310 goto finally;
2311 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002312
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002313 type = args->ob_type;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002314
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002315 switch (type->tp_name[0]) {
Guido van Rossum77f6a652002-04-03 22:41:51 +00002316 case 'b':
2317 if (args == Py_False || args == Py_True) {
2318 res = save_bool(self, args);
2319 goto finally;
2320 }
2321 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002322 case 'i':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002323 if (type == &PyInt_Type) {
2324 res = save_int(self, args);
2325 goto finally;
2326 }
2327 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002328
Guido van Rossum60456fd1997-04-09 17:36:32 +00002329 case 'l':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002330 if (type == &PyLong_Type) {
2331 res = save_long(self, args);
2332 goto finally;
2333 }
2334 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002335
Guido van Rossum60456fd1997-04-09 17:36:32 +00002336 case 'f':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002337 if (type == &PyFloat_Type) {
2338 res = save_float(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 't':
Tim Peters1d63c9f2003-02-02 20:29:39 +00002344 if (type == &PyTuple_Type && PyTuple_Size(args) == 0) {
2345 res = save_tuple(self, args);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002346 goto finally;
2347 }
2348 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002349
Guido van Rossum60456fd1997-04-09 17:36:32 +00002350 case 's':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002351 if ((type == &PyString_Type) && (PyString_GET_SIZE(args) < 2)) {
2352 res = save_string(self, args, 0);
2353 goto finally;
2354 }
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00002355
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002356#ifdef Py_USING_UNICODE
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00002357 case 'u':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002358 if ((type == &PyUnicode_Type) && (PyString_GET_SIZE(args) < 2)) {
2359 res = save_unicode(self, args, 0);
2360 goto finally;
2361 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002362#endif
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002363 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002364
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002365 if (args->ob_refcnt > 1) {
Tim Peterscba30e22003-02-01 06:24:36 +00002366 if (!( py_ob_id = PyLong_FromVoidPtr(args)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002367 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002368
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002369 if (PyDict_GetItem(self->memo, py_ob_id)) {
2370 if (get(self, py_ob_id) < 0)
2371 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002372
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002373 res = 0;
2374 goto finally;
2375 }
2376 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002377
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002378 switch (type->tp_name[0]) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00002379 case 's':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002380 if (type == &PyString_Type) {
2381 res = save_string(self, args, 1);
2382 goto finally;
2383 }
2384 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002385
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002386#ifdef Py_USING_UNICODE
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00002387 case 'u':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002388 if (type == &PyUnicode_Type) {
2389 res = save_unicode(self, args, 1);
2390 goto finally;
2391 }
2392 break;
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002393#endif
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00002394
Guido van Rossum60456fd1997-04-09 17:36:32 +00002395 case 't':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002396 if (type == &PyTuple_Type) {
2397 res = save_tuple(self, args);
2398 goto finally;
2399 }
2400 if (type == &PyType_Type) {
2401 res = save_global(self, args, NULL);
2402 goto finally;
2403 }
2404 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002405
Guido van Rossum60456fd1997-04-09 17:36:32 +00002406 case 'l':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002407 if (type == &PyList_Type) {
2408 res = save_list(self, args);
2409 goto finally;
2410 }
2411 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002412
2413 case 'd':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002414 if (type == &PyDict_Type) {
2415 res = save_dict(self, args);
2416 goto finally;
2417 }
2418 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002419
2420 case 'i':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002421 if (type == &PyInstance_Type) {
2422 res = save_inst(self, args);
2423 goto finally;
2424 }
2425 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002426
2427 case 'c':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002428 if (type == &PyClass_Type) {
2429 res = save_global(self, args, NULL);
2430 goto finally;
2431 }
2432 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002433
2434 case 'f':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002435 if (type == &PyFunction_Type) {
2436 res = save_global(self, args, NULL);
Christian Tismer2460c622004-02-26 16:21:45 +00002437 if (res && PyErr_ExceptionMatches(PickleError)) {
2438 /* fall back to reduce */
2439 PyErr_Clear();
2440 break;
2441 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002442 goto finally;
2443 }
2444 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002445
2446 case 'b':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002447 if (type == &PyCFunction_Type) {
2448 res = save_global(self, args, NULL);
2449 goto finally;
2450 }
2451 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002452
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002453 if (!pers_save && self->inst_pers_func) {
2454 if ((tmp = save_pers(self, args, self->inst_pers_func)) != 0) {
2455 res = tmp;
2456 goto finally;
2457 }
2458 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002459
Jeremy Hylton39c61162002-07-16 19:47:43 +00002460 if (PyType_IsSubtype(type, &PyType_Type)) {
2461 res = save_global(self, args, NULL);
2462 goto finally;
2463 }
2464
Guido van Rossumb289b872003-02-19 01:45:13 +00002465 /* Get a reduction callable, and call it. This may come from
2466 * copy_reg.dispatch_table, the object's __reduce_ex__ method,
2467 * or the object's __reduce__ method.
Tim Peters71fcda52003-02-14 23:05:28 +00002468 */
Tim Peters5aa3da62003-02-13 21:03:57 +00002469 __reduce__ = PyDict_GetItem(dispatch_table, (PyObject *)type);
2470 if (__reduce__ != NULL) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002471 Py_INCREF(__reduce__);
Guido van Rossumb289b872003-02-19 01:45:13 +00002472 Py_INCREF(args);
2473 ARG_TUP(self, args);
2474 if (self->arg) {
2475 t = PyObject_Call(__reduce__, self->arg, NULL);
2476 FREE_ARG_TUP(self);
2477 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002478 }
2479 else {
Guido van Rossumb289b872003-02-19 01:45:13 +00002480 /* Check for a __reduce_ex__ method. */
2481 __reduce__ = PyObject_GetAttr(args, __reduce_ex___str);
2482 if (__reduce__ != NULL) {
2483 t = PyInt_FromLong(self->proto);
2484 if (t != NULL) {
2485 ARG_TUP(self, t);
2486 t = NULL;
2487 if (self->arg) {
2488 t = PyObject_Call(__reduce__,
2489 self->arg, NULL);
2490 FREE_ARG_TUP(self);
2491 }
2492 }
2493 }
2494 else {
Jeremy Hyltonf00368f2003-06-16 20:19:49 +00002495 if (PyErr_ExceptionMatches(PyExc_AttributeError))
2496 PyErr_Clear();
2497 else
2498 goto finally;
Guido van Rossumb289b872003-02-19 01:45:13 +00002499 /* Check for a __reduce__ method. */
2500 __reduce__ = PyObject_GetAttr(args, __reduce___str);
2501 if (__reduce__ != NULL) {
2502 t = PyObject_Call(__reduce__,
2503 empty_tuple, NULL);
2504 }
2505 else {
2506 PyErr_SetObject(UnpickleableError, args);
2507 goto finally;
2508 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002509 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002510 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002511
Tim Peters71fcda52003-02-14 23:05:28 +00002512 if (t == NULL)
2513 goto finally;
Tim Peters84e87f32001-03-17 04:50:51 +00002514
Tim Peters71fcda52003-02-14 23:05:28 +00002515 if (PyString_Check(t)) {
2516 res = save_global(self, args, t);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002517 goto finally;
2518 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002519
Tim Peters71fcda52003-02-14 23:05:28 +00002520 if (! PyTuple_Check(t)) {
2521 cPickle_ErrFormat(PicklingError, "Value returned by "
2522 "%s must be string or tuple",
2523 "O", __reduce__);
2524 goto finally;
2525 }
2526
2527 size = PyTuple_Size(t);
2528 if (size < 2 || size > 5) {
2529 cPickle_ErrFormat(PicklingError, "tuple returned by "
2530 "%s must contain 2 through 5 elements",
2531 "O", __reduce__);
2532 goto finally;
2533 }
2534
2535 arg_tup = PyTuple_GET_ITEM(t, 1);
2536 if (!(PyTuple_Check(arg_tup) || arg_tup == Py_None)) {
2537 cPickle_ErrFormat(PicklingError, "Second element of "
2538 "tuple returned by %s must be a tuple",
2539 "O", __reduce__);
2540 goto finally;
2541 }
2542
2543 res = save_reduce(self, t, args);
Guido van Rossum60456fd1997-04-09 17:36:32 +00002544
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002545 finally:
Martin v. Löwis5a395302002-08-04 08:20:23 +00002546 self->nesting--;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002547 Py_XDECREF(py_ob_id);
2548 Py_XDECREF(__reduce__);
2549 Py_XDECREF(t);
Tim Peters84e87f32001-03-17 04:50:51 +00002550
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002551 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002552}
2553
2554
2555static int
Tim Peterscba30e22003-02-01 06:24:36 +00002556dump(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002557{
2558 static char stop = STOP;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002559
Tim Peters4190fb82003-02-02 16:09:05 +00002560 if (self->proto >= 2) {
2561 char bytes[2];
2562
2563 bytes[0] = PROTO;
Tim Peters87482ea2003-02-02 16:16:30 +00002564 assert(self->proto >= 0 && self->proto < 256);
2565 bytes[1] = (char)self->proto;
Tim Peters4190fb82003-02-02 16:09:05 +00002566 if (self->write_func(self, bytes, 2) < 0)
2567 return -1;
2568 }
2569
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002570 if (save(self, args, 0) < 0)
2571 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002572
Tim Peters4190fb82003-02-02 16:09:05 +00002573 if (self->write_func(self, &stop, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002574 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002575
Tim Peters4190fb82003-02-02 16:09:05 +00002576 if (self->write_func(self, NULL, 0) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002577 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002578
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002579 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002580}
2581
2582static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00002583Pickle_clear_memo(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002584{
Tim Peterscba30e22003-02-01 06:24:36 +00002585 if (self->memo)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002586 PyDict_Clear(self->memo);
2587 Py_INCREF(Py_None);
2588 return Py_None;
2589}
2590
2591static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00002592Pickle_getvalue(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002593{
2594 int l, i, rsize, ssize, clear=1, lm;
2595 long ik;
2596 PyObject *k, *r;
2597 char *s, *p, *have_get;
2598 Pdata *data;
2599
2600 /* Can be called by Python code or C code */
Tim Peterscba30e22003-02-01 06:24:36 +00002601 if (args && !PyArg_ParseTuple(args, "|i:getvalue", &clear))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002602 return NULL;
2603
2604 /* Check to make sure we are based on a list */
2605 if (! Pdata_Check(self->file)) {
2606 PyErr_SetString(PicklingError,
2607 "Attempt to getvalue() a non-list-based pickler");
2608 return NULL;
2609 }
2610
2611 /* flush write buffer */
2612 if (write_other(self, NULL, 0) < 0) return NULL;
2613
2614 data=(Pdata*)self->file;
2615 l=data->length;
2616
2617 /* set up an array to hold get/put status */
Tim Petersac5687a2003-02-02 18:08:34 +00002618 lm = PyDict_Size(self->memo);
2619 if (lm < 0) return NULL;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002620 lm++;
Tim Petersac5687a2003-02-02 18:08:34 +00002621 have_get = malloc(lm);
2622 if (have_get == NULL) return PyErr_NoMemory();
2623 memset(have_get, 0, lm);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002624
2625 /* Scan for gets. */
Tim Petersac5687a2003-02-02 18:08:34 +00002626 for (rsize = 0, i = l; --i >= 0; ) {
2627 k = data->data[i];
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002628
Tim Petersac5687a2003-02-02 18:08:34 +00002629 if (PyString_Check(k))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002630 rsize += PyString_GET_SIZE(k);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002631
2632 else if (PyInt_Check(k)) { /* put */
Tim Petersac5687a2003-02-02 18:08:34 +00002633 ik = PyInt_AS_LONG((PyIntObject*)k);
2634 if (ik >= lm || ik == 0) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002635 PyErr_SetString(PicklingError,
2636 "Invalid get data");
2637 return NULL;
2638 }
Tim Petersac5687a2003-02-02 18:08:34 +00002639 if (have_get[ik]) /* with matching get */
2640 rsize += ik < 256 ? 2 : 5;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002641 }
2642
2643 else if (! (PyTuple_Check(k) &&
2644 PyTuple_GET_SIZE(k) == 2 &&
Tim Petersac5687a2003-02-02 18:08:34 +00002645 PyInt_Check((k = PyTuple_GET_ITEM(k, 0))))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002646 ) {
2647 PyErr_SetString(PicklingError,
2648 "Unexpected data in internal list");
2649 return NULL;
2650 }
2651
2652 else { /* put */
Tim Petersac5687a2003-02-02 18:08:34 +00002653 ik = PyInt_AS_LONG((PyIntObject *)k);
2654 if (ik >= lm || ik == 0) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002655 PyErr_SetString(PicklingError,
2656 "Invalid get data");
2657 return NULL;
2658 }
Tim Petersac5687a2003-02-02 18:08:34 +00002659 have_get[ik] = 1;
2660 rsize += ik < 256 ? 2 : 5;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002661 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002662 }
2663
2664 /* Now generate the result */
Tim Petersac5687a2003-02-02 18:08:34 +00002665 r = PyString_FromStringAndSize(NULL, rsize);
2666 if (r == NULL) goto err;
2667 s = PyString_AS_STRING((PyStringObject *)r);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002668
Tim Petersac5687a2003-02-02 18:08:34 +00002669 for (i = 0; i < l; i++) {
2670 k = data->data[i];
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002671
2672 if (PyString_Check(k)) {
Tim Petersac5687a2003-02-02 18:08:34 +00002673 ssize = PyString_GET_SIZE(k);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002674 if (ssize) {
Tim Petersac5687a2003-02-02 18:08:34 +00002675 p=PyString_AS_STRING((PyStringObject *)k);
2676 while (--ssize >= 0)
2677 *s++ = *p++;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002678 }
2679 }
2680
2681 else if (PyTuple_Check(k)) { /* get */
Tim Petersac5687a2003-02-02 18:08:34 +00002682 ik = PyInt_AS_LONG((PyIntObject *)
2683 PyTuple_GET_ITEM(k, 0));
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002684 if (ik < 256) {
2685 *s++ = BINGET;
2686 *s++ = (int)(ik & 0xff);
2687 }
2688 else {
2689 *s++ = LONG_BINGET;
2690 *s++ = (int)(ik & 0xff);
2691 *s++ = (int)((ik >> 8) & 0xff);
2692 *s++ = (int)((ik >> 16) & 0xff);
2693 *s++ = (int)((ik >> 24) & 0xff);
2694 }
2695 }
2696
2697 else { /* put */
Tim Petersac5687a2003-02-02 18:08:34 +00002698 ik = PyInt_AS_LONG((PyIntObject*)k);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002699
2700 if (have_get[ik]) { /* with matching get */
2701 if (ik < 256) {
2702 *s++ = BINPUT;
2703 *s++ = (int)(ik & 0xff);
2704 }
2705 else {
2706 *s++ = LONG_BINPUT;
2707 *s++ = (int)(ik & 0xff);
2708 *s++ = (int)((ik >> 8) & 0xff);
2709 *s++ = (int)((ik >> 16) & 0xff);
2710 *s++ = (int)((ik >> 24) & 0xff);
2711 }
2712 }
2713 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002714 }
2715
2716 if (clear) {
2717 PyDict_Clear(self->memo);
Tim Petersac5687a2003-02-02 18:08:34 +00002718 Pdata_clear(data, 0);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002719 }
2720
2721 free(have_get);
2722 return r;
2723 err:
2724 free(have_get);
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00002725 return NULL;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002726}
2727
2728static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00002729Pickler_dump(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002730{
2731 PyObject *ob;
2732 int get=0;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002733
Tim Peterscba30e22003-02-01 06:24:36 +00002734 if (!( PyArg_ParseTuple(args, "O|i:dump", &ob, &get)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002735 return NULL;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002736
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002737 if (dump(self, ob) < 0)
2738 return NULL;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002739
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002740 if (get) return Pickle_getvalue(self, NULL);
Guido van Rossum053b8df1998-11-25 16:18:00 +00002741
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002742 /* XXX Why does dump() return self? */
2743 Py_INCREF(self);
2744 return (PyObject*)self;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002745}
2746
2747
Tim Peterscba30e22003-02-01 06:24:36 +00002748static struct PyMethodDef Pickler_methods[] =
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002749{
Neal Norwitzb0493252002-03-31 14:44:22 +00002750 {"dump", (PyCFunction)Pickler_dump, METH_VARARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00002751 PyDoc_STR("dump(object) -- "
2752 "Write an object in pickle format to the object's pickle stream")},
Fred Drake0ebacc82002-05-01 20:36:39 +00002753 {"clear_memo", (PyCFunction)Pickle_clear_memo, METH_NOARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00002754 PyDoc_STR("clear_memo() -- Clear the picklers memo")},
Neal Norwitzb0493252002-03-31 14:44:22 +00002755 {"getvalue", (PyCFunction)Pickle_getvalue, METH_VARARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00002756 PyDoc_STR("getvalue() -- Finish picking a list-based pickle")},
Guido van Rossum60456fd1997-04-09 17:36:32 +00002757 {NULL, NULL} /* sentinel */
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002758};
2759
2760
2761static Picklerobject *
Tim Peters5bd2a792003-02-01 16:45:06 +00002762newPicklerobject(PyObject *file, int proto)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002763{
2764 Picklerobject *self;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002765
Tim Peters5bd2a792003-02-01 16:45:06 +00002766 if (proto < 0)
Tim Peters8587b3c2003-02-13 15:44:41 +00002767 proto = HIGHEST_PROTOCOL;
2768 if (proto > HIGHEST_PROTOCOL) {
Tim Peters5bd2a792003-02-01 16:45:06 +00002769 PyErr_Format(PyExc_ValueError, "pickle protocol %d asked for; "
2770 "the highest available protocol is %d",
Tim Peters8587b3c2003-02-13 15:44:41 +00002771 proto, HIGHEST_PROTOCOL);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002772 return NULL;
Tim Peters5bd2a792003-02-01 16:45:06 +00002773 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002774
Jeremy Hylton4cf63192003-04-09 21:05:12 +00002775 self = PyObject_GC_New(Picklerobject, &Picklertype);
Tim Peters5bd2a792003-02-01 16:45:06 +00002776 if (self == NULL)
2777 return NULL;
2778 self->proto = proto;
2779 self->bin = proto > 0;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002780 self->fp = NULL;
2781 self->write = NULL;
2782 self->memo = NULL;
2783 self->arg = NULL;
2784 self->pers_func = NULL;
2785 self->inst_pers_func = NULL;
2786 self->write_buf = NULL;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002787 self->fast = 0;
Martin v. Löwis5a395302002-08-04 08:20:23 +00002788 self->nesting = 0;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002789 self->fast_container = 0;
2790 self->fast_memo = NULL;
2791 self->buf_size = 0;
2792 self->dispatch_table = NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002793
Tim Peters5bd2a792003-02-01 16:45:06 +00002794 self->file = NULL;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002795 if (file)
2796 Py_INCREF(file);
Tim Peters5bd2a792003-02-01 16:45:06 +00002797 else {
2798 file = Pdata_New();
2799 if (file == NULL)
2800 goto err;
2801 }
2802 self->file = file;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002803
Tim Peterscba30e22003-02-01 06:24:36 +00002804 if (!( self->memo = PyDict_New()))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002805 goto err;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002806
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002807 if (PyFile_Check(file)) {
2808 self->fp = PyFile_AsFile(file);
2809 if (self->fp == NULL) {
Tim Peters5bd2a792003-02-01 16:45:06 +00002810 PyErr_SetString(PyExc_ValueError,
2811 "I/O operation on closed file");
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002812 goto err;
2813 }
2814 self->write_func = write_file;
Guido van Rossumc91fcaa1999-03-29 20:00:14 +00002815 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002816 else if (PycStringIO_OutputCheck(file)) {
2817 self->write_func = write_cStringIO;
2818 }
2819 else if (file == Py_None) {
2820 self->write_func = write_none;
2821 }
2822 else {
2823 self->write_func = write_other;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002824
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002825 if (! Pdata_Check(file)) {
2826 self->write = PyObject_GetAttr(file, write_str);
2827 if (!self->write) {
2828 PyErr_Clear();
Tim Peterscba30e22003-02-01 06:24:36 +00002829 PyErr_SetString(PyExc_TypeError,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002830 "argument must have 'write' "
2831 "attribute");
2832 goto err;
2833 }
2834 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002835
Tim Peters5bd2a792003-02-01 16:45:06 +00002836 self->write_buf = (char *)PyMem_Malloc(WRITE_BUF_SIZE);
2837 if (self->write_buf == NULL) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002838 PyErr_NoMemory();
2839 goto err;
2840 }
2841 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002842
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002843 if (PyEval_GetRestricted()) {
2844 /* Restricted execution, get private tables */
Tim Peters5b7da392003-02-04 00:21:07 +00002845 PyObject *m = PyImport_Import(copy_reg_str);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002846
Tim Peters5b7da392003-02-04 00:21:07 +00002847 if (m == NULL)
2848 goto err;
2849 self->dispatch_table = PyObject_GetAttr(m, dispatch_table_str);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002850 Py_DECREF(m);
Tim Peters5b7da392003-02-04 00:21:07 +00002851 if (self->dispatch_table == NULL)
2852 goto err;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002853 }
2854 else {
Tim Peters5b7da392003-02-04 00:21:07 +00002855 self->dispatch_table = dispatch_table;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002856 Py_INCREF(dispatch_table);
2857 }
Jeremy Hylton4cf63192003-04-09 21:05:12 +00002858 PyObject_GC_Track(self);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002859
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002860 return self;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002861
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002862 err:
Tim Peters5bd2a792003-02-01 16:45:06 +00002863 Py_DECREF(self);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002864 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002865}
2866
2867
2868static PyObject *
Martin v. Löwis544f1192004-07-27 05:22:33 +00002869get_Pickler(PyObject *self, PyObject *args, PyObject *kwds)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002870{
Martin v. Löwis15e62742006-02-27 16:46:16 +00002871 static char *kwlist[] = {"file", "protocol", NULL};
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002872 PyObject *file = NULL;
Tim Peters5bd2a792003-02-01 16:45:06 +00002873 int proto = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002874
Tim Peters92c8bb32003-02-13 23:00:26 +00002875 /* XXX
Martin v. Löwis544f1192004-07-27 05:22:33 +00002876 * The documented signature is Pickler(file, protocol=0), but this
Tim Peters92c8bb32003-02-13 23:00:26 +00002877 * accepts Pickler() and Pickler(integer) too. The meaning then
2878 * is clear as mud, undocumented, and not supported by pickle.py.
2879 * I'm told Zope uses this, but I haven't traced into this code
2880 * far enough to figure out what it means.
Tim Peters5bd2a792003-02-01 16:45:06 +00002881 */
2882 if (!PyArg_ParseTuple(args, "|i:Pickler", &proto)) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002883 PyErr_Clear();
Tim Peters5bd2a792003-02-01 16:45:06 +00002884 proto = 0;
Martin v. Löwis544f1192004-07-27 05:22:33 +00002885 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|i:Pickler",
2886 kwlist, &file, &proto))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002887 return NULL;
2888 }
Tim Peters5bd2a792003-02-01 16:45:06 +00002889 return (PyObject *)newPicklerobject(file, proto);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002890}
2891
2892
2893static void
Tim Peterscba30e22003-02-01 06:24:36 +00002894Pickler_dealloc(Picklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002895{
Jeremy Hylton4cf63192003-04-09 21:05:12 +00002896 PyObject_GC_UnTrack(self);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002897 Py_XDECREF(self->write);
2898 Py_XDECREF(self->memo);
2899 Py_XDECREF(self->fast_memo);
2900 Py_XDECREF(self->arg);
2901 Py_XDECREF(self->file);
2902 Py_XDECREF(self->pers_func);
2903 Py_XDECREF(self->inst_pers_func);
2904 Py_XDECREF(self->dispatch_table);
Tim Peters5bd2a792003-02-01 16:45:06 +00002905 PyMem_Free(self->write_buf);
Tim Peters3cfe7542003-05-21 21:29:48 +00002906 self->ob_type->tp_free((PyObject *)self);
Jeremy Hylton4cf63192003-04-09 21:05:12 +00002907}
2908
2909static int
2910Pickler_traverse(Picklerobject *self, visitproc visit, void *arg)
2911{
2912 int err;
2913#define VISIT(SLOT) \
2914 if (SLOT) { \
2915 err = visit((PyObject *)(SLOT), arg); \
2916 if (err) \
2917 return err; \
2918 }
2919 VISIT(self->write);
2920 VISIT(self->memo);
2921 VISIT(self->fast_memo);
2922 VISIT(self->arg);
2923 VISIT(self->file);
2924 VISIT(self->pers_func);
2925 VISIT(self->inst_pers_func);
2926 VISIT(self->dispatch_table);
2927#undef VISIT
2928 return 0;
2929}
2930
2931static int
2932Pickler_clear(Picklerobject *self)
2933{
2934#define CLEAR(SLOT) Py_XDECREF(SLOT); SLOT = NULL;
2935 CLEAR(self->write);
2936 CLEAR(self->memo);
2937 CLEAR(self->fast_memo);
2938 CLEAR(self->arg);
2939 CLEAR(self->file);
2940 CLEAR(self->pers_func);
2941 CLEAR(self->inst_pers_func);
2942 CLEAR(self->dispatch_table);
2943#undef CLEAR
2944 return 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002945}
2946
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002947static PyObject *
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00002948Pickler_get_pers_func(Picklerobject *p)
2949{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002950 if (p->pers_func == NULL)
2951 PyErr_SetString(PyExc_AttributeError, "persistent_id");
2952 else
2953 Py_INCREF(p->pers_func);
2954 return p->pers_func;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002955}
2956
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00002957static int
2958Pickler_set_pers_func(Picklerobject *p, PyObject *v)
2959{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002960 if (v == NULL) {
2961 PyErr_SetString(PyExc_TypeError,
2962 "attribute deletion is not supported");
2963 return -1;
2964 }
2965 Py_XDECREF(p->pers_func);
2966 Py_INCREF(v);
2967 p->pers_func = v;
2968 return 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002969}
2970
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00002971static int
2972Pickler_set_inst_pers_func(Picklerobject *p, PyObject *v)
2973{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002974 if (v == NULL) {
2975 PyErr_SetString(PyExc_TypeError,
2976 "attribute deletion is not supported");
2977 return -1;
2978 }
2979 Py_XDECREF(p->inst_pers_func);
2980 Py_INCREF(v);
2981 p->inst_pers_func = v;
2982 return 0;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00002983}
2984
2985static PyObject *
2986Pickler_get_memo(Picklerobject *p)
2987{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002988 if (p->memo == NULL)
2989 PyErr_SetString(PyExc_AttributeError, "memo");
2990 else
2991 Py_INCREF(p->memo);
2992 return p->memo;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00002993}
2994
2995static int
2996Pickler_set_memo(Picklerobject *p, PyObject *v)
2997{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002998 if (v == NULL) {
2999 PyErr_SetString(PyExc_TypeError,
3000 "attribute deletion is not supported");
3001 return -1;
3002 }
3003 if (!PyDict_Check(v)) {
3004 PyErr_SetString(PyExc_TypeError, "memo must be a dictionary");
3005 return -1;
3006 }
3007 Py_XDECREF(p->memo);
3008 Py_INCREF(v);
3009 p->memo = v;
3010 return 0;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003011}
3012
3013static PyObject *
3014Pickler_get_error(Picklerobject *p)
3015{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003016 /* why is this an attribute on the Pickler? */
3017 Py_INCREF(PicklingError);
3018 return PicklingError;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003019}
3020
3021static PyMemberDef Pickler_members[] = {
3022 {"binary", T_INT, offsetof(Picklerobject, bin)},
3023 {"fast", T_INT, offsetof(Picklerobject, fast)},
Jeremy Hylton3eb46f32001-10-16 17:10:49 +00003024 {NULL}
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003025};
3026
3027static PyGetSetDef Pickler_getsets[] = {
Tim Peterscba30e22003-02-01 06:24:36 +00003028 {"persistent_id", (getter)Pickler_get_pers_func,
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003029 (setter)Pickler_set_pers_func},
3030 {"inst_persistent_id", NULL, (setter)Pickler_set_inst_pers_func},
3031 {"memo", (getter)Pickler_get_memo, (setter)Pickler_set_memo},
Jeremy Hylton3eb46f32001-10-16 17:10:49 +00003032 {"PicklingError", (getter)Pickler_get_error, NULL},
3033 {NULL}
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003034};
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003035
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003036PyDoc_STRVAR(Picklertype__doc__,
3037"Objects that know how to pickle objects\n");
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003038
Guido van Rossum9716aaa1997-12-08 15:15:16 +00003039static PyTypeObject Picklertype = {
3040 PyObject_HEAD_INIT(NULL)
Guido van Rossum60456fd1997-04-09 17:36:32 +00003041 0, /*ob_size*/
Guido van Rossum14648392001-12-08 18:02:58 +00003042 "cPickle.Pickler", /*tp_name*/
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003043 sizeof(Picklerobject), /*tp_basicsize*/
3044 0,
3045 (destructor)Pickler_dealloc, /* tp_dealloc */
3046 0, /* tp_print */
3047 0, /* tp_getattr */
3048 0, /* tp_setattr */
3049 0, /* tp_compare */
3050 0, /* tp_repr */
3051 0, /* tp_as_number */
3052 0, /* tp_as_sequence */
3053 0, /* tp_as_mapping */
3054 0, /* tp_hash */
3055 0, /* tp_call */
3056 0, /* tp_str */
Jason Tishlerfb8595d2003-01-06 12:41:26 +00003057 PyObject_GenericGetAttr, /* tp_getattro */
3058 PyObject_GenericSetAttr, /* tp_setattro */
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003059 0, /* tp_as_buffer */
Jeremy Hylton4cf63192003-04-09 21:05:12 +00003060 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003061 Picklertype__doc__, /* tp_doc */
Jeremy Hylton4cf63192003-04-09 21:05:12 +00003062 (traverseproc)Pickler_traverse, /* tp_traverse */
3063 (inquiry)Pickler_clear, /* tp_clear */
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003064 0, /* tp_richcompare */
3065 0, /* tp_weaklistoffset */
3066 0, /* tp_iter */
3067 0, /* tp_iternext */
3068 Pickler_methods, /* tp_methods */
3069 Pickler_members, /* tp_members */
3070 Pickler_getsets, /* tp_getset */
Guido van Rossum9716aaa1997-12-08 15:15:16 +00003071};
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003072
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003073static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00003074find_class(PyObject *py_module_name, PyObject *py_global_name, PyObject *fc)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003075{
3076 PyObject *global = 0, *module;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003077
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003078 if (fc) {
3079 if (fc==Py_None) {
Tim Peters92c8bb32003-02-13 23:00:26 +00003080 PyErr_SetString(UnpicklingError, "Global and instance "
3081 "pickles are not supported.");
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003082 return NULL;
3083 }
Tim Peterscba30e22003-02-01 06:24:36 +00003084 return PyObject_CallFunction(fc, "OO", py_module_name,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003085 py_global_name);
3086 }
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00003087
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003088 module = PySys_GetObject("modules");
3089 if (module == NULL)
3090 return NULL;
Jeremy Hyltond1055231998-08-11 19:52:51 +00003091
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003092 module = PyDict_GetItem(module, py_module_name);
3093 if (module == NULL) {
3094 module = PyImport_Import(py_module_name);
3095 if (!module)
3096 return NULL;
3097 global = PyObject_GetAttr(module, py_global_name);
3098 Py_DECREF(module);
3099 }
3100 else
3101 global = PyObject_GetAttr(module, py_global_name);
3102 return global;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003103}
3104
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003105static int
Tim Peterscba30e22003-02-01 06:24:36 +00003106marker(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003107{
3108 if (self->num_marks < 1) {
3109 PyErr_SetString(UnpicklingError, "could not find MARK");
3110 return -1;
3111 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003112
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003113 return self->marks[--self->num_marks];
Guido van Rossum60456fd1997-04-09 17:36:32 +00003114}
3115
Tim Peters84e87f32001-03-17 04:50:51 +00003116
Guido van Rossum60456fd1997-04-09 17:36:32 +00003117static int
Tim Peterscba30e22003-02-01 06:24:36 +00003118load_none(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003119{
3120 PDATA_APPEND(self->stack, Py_None, -1);
3121 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003122}
3123
Guido van Rossumf9ffb031999-02-04 14:54:04 +00003124static int
Tim Peterscba30e22003-02-01 06:24:36 +00003125bad_readline(void)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003126{
3127 PyErr_SetString(UnpicklingError, "pickle data was truncated");
3128 return -1;
Guido van Rossumf9ffb031999-02-04 14:54:04 +00003129}
Guido van Rossum60456fd1997-04-09 17:36:32 +00003130
3131static int
Tim Peterscba30e22003-02-01 06:24:36 +00003132load_int(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003133{
3134 PyObject *py_int = 0;
3135 char *endptr, *s;
3136 int len, res = -1;
3137 long l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003138
Tim Peters0bc93f52003-02-02 18:29:33 +00003139 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003140 if (len < 2) return bad_readline();
3141 if (!( s=pystrndup(s,len))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003142
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003143 errno = 0;
3144 l = strtol(s, &endptr, 0);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003145
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003146 if (errno || (*endptr != '\n') || (endptr[1] != '\0')) {
3147 /* Hm, maybe we've got something long. Let's try reading
3148 it as a Python long object. */
3149 errno = 0;
3150 py_int = PyLong_FromString(s, NULL, 0);
3151 if (py_int == NULL) {
3152 PyErr_SetString(PyExc_ValueError,
3153 "could not convert string to int");
3154 goto finally;
3155 }
3156 }
3157 else {
Guido van Rossume2763392002-04-05 19:30:08 +00003158 if (len == 3 && (l == 0 || l == 1)) {
3159 if (!( py_int = PyBool_FromLong(l))) goto finally;
3160 }
3161 else {
3162 if (!( py_int = PyInt_FromLong(l))) goto finally;
3163 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003164 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003165
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003166 free(s);
3167 PDATA_PUSH(self->stack, py_int, -1);
3168 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003169
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003170 finally:
3171 free(s);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003172
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003173 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003174}
3175
Tim Peters3c67d792003-02-02 17:59:11 +00003176static int
3177load_bool(Unpicklerobject *self, PyObject *boolean)
3178{
3179 assert(boolean == Py_True || boolean == Py_False);
Tim Peterse0a39072003-02-03 15:45:56 +00003180 PDATA_APPEND(self->stack, boolean, -1);
Tim Peters3c67d792003-02-02 17:59:11 +00003181 return 0;
3182}
3183
Tim Petersee1a53c2003-02-02 02:57:53 +00003184/* s contains x bytes of a little-endian integer. Return its value as a
3185 * C int. Obscure: when x is 1 or 2, this is an unsigned little-endian
3186 * int, but when x is 4 it's a signed one. This is an historical source
3187 * of x-platform bugs.
3188 */
Tim Peters84e87f32001-03-17 04:50:51 +00003189static long
Tim Petersee1a53c2003-02-02 02:57:53 +00003190calc_binint(char *s, int x)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003191{
3192 unsigned char c;
3193 int i;
3194 long l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003195
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003196 for (i = 0, l = 0L; i < x; i++) {
3197 c = (unsigned char)s[i];
3198 l |= (long)c << (i * 8);
3199 }
Tim Petersbfa18f72001-04-10 01:54:42 +00003200#if SIZEOF_LONG > 4
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003201 /* Unlike BININT1 and BININT2, BININT (more accurately BININT4)
3202 * is signed, so on a box with longs bigger than 4 bytes we need
3203 * to extend a BININT's sign bit to the full width.
3204 */
3205 if (x == 4 && l & (1L << 31))
3206 l |= (~0L) << 32;
Tim Petersbfa18f72001-04-10 01:54:42 +00003207#endif
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003208 return l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003209}
3210
3211
3212static int
Tim Peterscba30e22003-02-01 06:24:36 +00003213load_binintx(Unpicklerobject *self, char *s, int x)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003214{
3215 PyObject *py_int = 0;
3216 long l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003217
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003218 l = calc_binint(s, x);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003219
Tim Peterscba30e22003-02-01 06:24:36 +00003220 if (!( py_int = PyInt_FromLong(l)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003221 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003222
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003223 PDATA_PUSH(self->stack, py_int, -1);
3224 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003225}
3226
3227
3228static int
Tim Peterscba30e22003-02-01 06:24:36 +00003229load_binint(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003230{
3231 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003232
Tim Peters0bc93f52003-02-02 18:29:33 +00003233 if (self->read_func(self, &s, 4) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003234 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003235
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003236 return load_binintx(self, s, 4);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003237}
3238
3239
3240static int
Tim Peterscba30e22003-02-01 06:24:36 +00003241load_binint1(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003242{
3243 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003244
Tim Peters0bc93f52003-02-02 18:29:33 +00003245 if (self->read_func(self, &s, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003246 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003247
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003248 return load_binintx(self, s, 1);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003249}
3250
3251
3252static int
Tim Peterscba30e22003-02-01 06:24:36 +00003253load_binint2(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003254{
3255 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003256
Tim Peters0bc93f52003-02-02 18:29:33 +00003257 if (self->read_func(self, &s, 2) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003258 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003259
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003260 return load_binintx(self, s, 2);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003261}
Tim Peters84e87f32001-03-17 04:50:51 +00003262
Guido van Rossum60456fd1997-04-09 17:36:32 +00003263static int
Tim Peterscba30e22003-02-01 06:24:36 +00003264load_long(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003265{
3266 PyObject *l = 0;
3267 char *end, *s;
3268 int len, res = -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003269
Tim Peters0bc93f52003-02-02 18:29:33 +00003270 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003271 if (len < 2) return bad_readline();
3272 if (!( s=pystrndup(s,len))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003273
Tim Peterscba30e22003-02-01 06:24:36 +00003274 if (!( l = PyLong_FromString(s, &end, 0)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003275 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003276
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003277 free(s);
3278 PDATA_PUSH(self->stack, l, -1);
3279 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003280
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003281 finally:
3282 free(s);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003283
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003284 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003285}
3286
Tim Petersee1a53c2003-02-02 02:57:53 +00003287/* 'size' bytes contain the # of bytes of little-endian 256's-complement
3288 * data following.
3289 */
3290static int
3291load_counted_long(Unpicklerobject *self, int size)
3292{
Martin v. Löwis18e16552006-02-15 17:27:45 +00003293 Py_ssize_t i;
Tim Petersee1a53c2003-02-02 02:57:53 +00003294 char *nbytes;
3295 unsigned char *pdata;
3296 PyObject *along;
3297
3298 assert(size == 1 || size == 4);
3299 i = self->read_func(self, &nbytes, size);
3300 if (i < 0) return -1;
3301
3302 size = calc_binint(nbytes, size);
3303 if (size < 0) {
3304 /* Corrupt or hostile pickle -- we never write one like
3305 * this.
3306 */
Tim Peters0c7c48e2003-02-03 22:07:24 +00003307 PyErr_SetString(UnpicklingError, "LONG pickle has negative "
Tim Petersee1a53c2003-02-02 02:57:53 +00003308 "byte count");
3309 return -1;
3310 }
3311
3312 if (size == 0)
3313 along = PyLong_FromLong(0L);
3314 else {
3315 /* Read the raw little-endian bytes & convert. */
Neal Norwitz529baf22003-02-02 17:08:33 +00003316 i = self->read_func(self, (char **)&pdata, size);
Tim Petersee1a53c2003-02-02 02:57:53 +00003317 if (i < 0) return -1;
3318 along = _PyLong_FromByteArray(pdata, (size_t)size,
3319 1 /* little endian */, 1 /* signed */);
3320 }
3321 if (along == NULL)
3322 return -1;
3323 PDATA_PUSH(self->stack, along, -1);
3324 return 0;
3325}
Tim Peters84e87f32001-03-17 04:50:51 +00003326
Guido van Rossum60456fd1997-04-09 17:36:32 +00003327static int
Tim Peterscba30e22003-02-01 06:24:36 +00003328load_float(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003329{
3330 PyObject *py_float = 0;
3331 char *endptr, *s;
3332 int len, res = -1;
3333 double d;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003334
Tim Peters0bc93f52003-02-02 18:29:33 +00003335 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003336 if (len < 2) return bad_readline();
3337 if (!( s=pystrndup(s,len))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003338
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003339 errno = 0;
Martin v. Löwis737ea822004-06-08 18:52:54 +00003340 d = PyOS_ascii_strtod(s, &endptr);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003341
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003342 if (errno || (endptr[0] != '\n') || (endptr[1] != '\0')) {
3343 PyErr_SetString(PyExc_ValueError,
3344 "could not convert string to float");
3345 goto finally;
3346 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003347
Tim Peterscba30e22003-02-01 06:24:36 +00003348 if (!( py_float = PyFloat_FromDouble(d)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003349 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003350
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003351 free(s);
3352 PDATA_PUSH(self->stack, py_float, -1);
3353 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003354
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003355 finally:
3356 free(s);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003357
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003358 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003359}
3360
Guido van Rossum60456fd1997-04-09 17:36:32 +00003361static int
Tim Peterscba30e22003-02-01 06:24:36 +00003362load_binfloat(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003363{
Tim Peters9905b942003-03-20 20:53:32 +00003364 PyObject *py_float;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003365 double x;
3366 char *p;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003367
Tim Peters0bc93f52003-02-02 18:29:33 +00003368 if (self->read_func(self, &p, 8) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003369 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003370
Tim Peters9905b942003-03-20 20:53:32 +00003371 x = _PyFloat_Unpack8((unsigned char *)p, 0);
3372 if (x == -1.0 && PyErr_Occurred())
3373 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003374
Tim Peters9905b942003-03-20 20:53:32 +00003375 py_float = PyFloat_FromDouble(x);
3376 if (py_float == NULL)
3377 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003378
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003379 PDATA_PUSH(self->stack, py_float, -1);
3380 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003381}
Guido van Rossum60456fd1997-04-09 17:36:32 +00003382
3383static int
Tim Peterscba30e22003-02-01 06:24:36 +00003384load_string(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003385{
3386 PyObject *str = 0;
Martin v. Löwis8a8da792002-08-14 07:46:28 +00003387 int len, res = -1;
3388 char *s, *p;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003389
Tim Peters0bc93f52003-02-02 18:29:33 +00003390 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003391 if (len < 2) return bad_readline();
3392 if (!( s=pystrndup(s,len))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003393
Martin v. Löwis8a8da792002-08-14 07:46:28 +00003394
3395 /* Strip outermost quotes */
3396 while (s[len-1] <= ' ')
3397 len--;
3398 if(s[0]=='"' && s[len-1]=='"'){
3399 s[len-1] = '\0';
3400 p = s + 1 ;
3401 len -= 2;
3402 } else if(s[0]=='\'' && s[len-1]=='\''){
3403 s[len-1] = '\0';
3404 p = s + 1 ;
3405 len -= 2;
3406 } else
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003407 goto insecure;
3408 /********************************************/
Guido van Rossum9716aaa1997-12-08 15:15:16 +00003409
Martin v. Löwis8a8da792002-08-14 07:46:28 +00003410 str = PyString_DecodeEscape(p, len, NULL, 0, NULL);
3411 if (str) {
3412 PDATA_PUSH(self->stack, str, -1);
3413 res = 0;
3414 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003415 free(s);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003416 return res;
Tim Peters84e87f32001-03-17 04:50:51 +00003417
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003418 insecure:
3419 free(s);
3420 PyErr_SetString(PyExc_ValueError,"insecure string pickle");
3421 return -1;
Tim Peters84e87f32001-03-17 04:50:51 +00003422}
Guido van Rossum60456fd1997-04-09 17:36:32 +00003423
3424
3425static int
Tim Peterscba30e22003-02-01 06:24:36 +00003426load_binstring(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003427{
3428 PyObject *py_string = 0;
3429 long l;
3430 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003431
Tim Peters0bc93f52003-02-02 18:29:33 +00003432 if (self->read_func(self, &s, 4) < 0) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003433
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003434 l = calc_binint(s, 4);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003435
Tim Peters0bc93f52003-02-02 18:29:33 +00003436 if (self->read_func(self, &s, l) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003437 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003438
Tim Peterscba30e22003-02-01 06:24:36 +00003439 if (!( py_string = PyString_FromStringAndSize(s, l)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003440 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003441
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003442 PDATA_PUSH(self->stack, py_string, -1);
3443 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003444}
3445
3446
3447static int
Tim Peterscba30e22003-02-01 06:24:36 +00003448load_short_binstring(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003449{
3450 PyObject *py_string = 0;
3451 unsigned char l;
3452 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003453
Tim Peters0bc93f52003-02-02 18:29:33 +00003454 if (self->read_func(self, &s, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003455 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003456
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003457 l = (unsigned char)s[0];
Guido van Rossum60456fd1997-04-09 17:36:32 +00003458
Tim Peters0bc93f52003-02-02 18:29:33 +00003459 if (self->read_func(self, &s, l) < 0) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003460
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003461 if (!( py_string = PyString_FromStringAndSize(s, l))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003462
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003463 PDATA_PUSH(self->stack, py_string, -1);
3464 return 0;
Tim Peters84e87f32001-03-17 04:50:51 +00003465}
Guido van Rossum60456fd1997-04-09 17:36:32 +00003466
3467
Martin v. Löwis339d0f72001-08-17 18:39:25 +00003468#ifdef Py_USING_UNICODE
Guido van Rossum60456fd1997-04-09 17:36:32 +00003469static int
Tim Peterscba30e22003-02-01 06:24:36 +00003470load_unicode(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003471{
3472 PyObject *str = 0;
3473 int len, res = -1;
3474 char *s;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003475
Tim Peters0bc93f52003-02-02 18:29:33 +00003476 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003477 if (len < 1) return bad_readline();
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003478
Tim Peterscba30e22003-02-01 06:24:36 +00003479 if (!( str = PyUnicode_DecodeRawUnicodeEscape(s, len - 1, NULL)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003480 goto finally;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003481
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003482 PDATA_PUSH(self->stack, str, -1);
3483 return 0;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003484
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003485 finally:
3486 return res;
Tim Peters84e87f32001-03-17 04:50:51 +00003487}
Martin v. Löwis339d0f72001-08-17 18:39:25 +00003488#endif
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003489
3490
Martin v. Löwis339d0f72001-08-17 18:39:25 +00003491#ifdef Py_USING_UNICODE
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003492static int
Tim Peterscba30e22003-02-01 06:24:36 +00003493load_binunicode(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003494{
3495 PyObject *unicode;
3496 long l;
3497 char *s;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003498
Tim Peters0bc93f52003-02-02 18:29:33 +00003499 if (self->read_func(self, &s, 4) < 0) return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003500
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003501 l = calc_binint(s, 4);
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003502
Tim Peters0bc93f52003-02-02 18:29:33 +00003503 if (self->read_func(self, &s, l) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003504 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003505
Tim Peterscba30e22003-02-01 06:24:36 +00003506 if (!( unicode = PyUnicode_DecodeUTF8(s, l, NULL)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003507 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003508
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003509 PDATA_PUSH(self->stack, unicode, -1);
3510 return 0;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003511}
Martin v. Löwis339d0f72001-08-17 18:39:25 +00003512#endif
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003513
3514
3515static int
Tim Peterscba30e22003-02-01 06:24:36 +00003516load_tuple(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003517{
3518 PyObject *tup;
3519 int i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003520
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003521 if ((i = marker(self)) < 0) return -1;
3522 if (!( tup=Pdata_popTuple(self->stack, i))) return -1;
3523 PDATA_PUSH(self->stack, tup, -1);
3524 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003525}
3526
3527static int
Tim Peters1d63c9f2003-02-02 20:29:39 +00003528load_counted_tuple(Unpicklerobject *self, int len)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003529{
Tim Peters1d63c9f2003-02-02 20:29:39 +00003530 PyObject *tup = PyTuple_New(len);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003531
Tim Peters1d63c9f2003-02-02 20:29:39 +00003532 if (tup == NULL)
3533 return -1;
3534
3535 while (--len >= 0) {
3536 PyObject *element;
3537
3538 PDATA_POP(self->stack, element);
3539 if (element == NULL)
3540 return -1;
3541 PyTuple_SET_ITEM(tup, len, element);
3542 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003543 PDATA_PUSH(self->stack, tup, -1);
3544 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003545}
3546
3547static int
Tim Peterscba30e22003-02-01 06:24:36 +00003548load_empty_list(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003549{
3550 PyObject *list;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003551
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003552 if (!( list=PyList_New(0))) return -1;
3553 PDATA_PUSH(self->stack, list, -1);
3554 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003555}
3556
3557static int
Tim Peterscba30e22003-02-01 06:24:36 +00003558load_empty_dict(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003559{
3560 PyObject *dict;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003561
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003562 if (!( dict=PyDict_New())) return -1;
3563 PDATA_PUSH(self->stack, dict, -1);
3564 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003565}
3566
3567
3568static int
Tim Peterscba30e22003-02-01 06:24:36 +00003569load_list(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003570{
3571 PyObject *list = 0;
3572 int i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003573
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003574 if ((i = marker(self)) < 0) return -1;
3575 if (!( list=Pdata_popList(self->stack, i))) return -1;
3576 PDATA_PUSH(self->stack, list, -1);
3577 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003578}
3579
3580static int
Tim Peterscba30e22003-02-01 06:24:36 +00003581load_dict(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003582{
3583 PyObject *dict, *key, *value;
3584 int i, j, k;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003585
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003586 if ((i = marker(self)) < 0) return -1;
3587 j=self->stack->length;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003588
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003589 if (!( dict = PyDict_New())) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003590
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003591 for (k = i+1; k < j; k += 2) {
3592 key =self->stack->data[k-1];
3593 value=self->stack->data[k ];
3594 if (PyDict_SetItem(dict, key, value) < 0) {
3595 Py_DECREF(dict);
3596 return -1;
3597 }
3598 }
3599 Pdata_clear(self->stack, i);
3600 PDATA_PUSH(self->stack, dict, -1);
3601 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003602}
3603
3604static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00003605Instance_New(PyObject *cls, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003606{
Tim Peters1f1b2d22003-02-01 02:16:37 +00003607 PyObject *r = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003608
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003609 if (PyClass_Check(cls)) {
3610 int l;
Tim Peters84e87f32001-03-17 04:50:51 +00003611
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003612 if ((l=PyObject_Size(args)) < 0) goto err;
3613 if (!( l )) {
3614 PyObject *__getinitargs__;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003615
Tim Peterscba30e22003-02-01 06:24:36 +00003616 __getinitargs__ = PyObject_GetAttr(cls,
Jeremy Hyltonf00368f2003-06-16 20:19:49 +00003617 __getinitargs___str);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003618 if (!__getinitargs__) {
Tim Peterscba30e22003-02-01 06:24:36 +00003619 /* We have a class with no __getinitargs__,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003620 so bypass usual construction */
3621 PyObject *inst;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003622
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003623 PyErr_Clear();
Tim Peterscba30e22003-02-01 06:24:36 +00003624 if (!( inst=PyInstance_NewRaw(cls, NULL)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003625 goto err;
3626 return inst;
3627 }
3628 Py_DECREF(__getinitargs__);
3629 }
Tim Peters84e87f32001-03-17 04:50:51 +00003630
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003631 if ((r=PyInstance_New(cls, args, NULL))) return r;
3632 else goto err;
3633 }
Tim Peters84e87f32001-03-17 04:50:51 +00003634
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003635 if ((r=PyObject_CallObject(cls, args))) return r;
Guido van Rossum142eeb81997-08-13 03:14:41 +00003636
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003637 err:
3638 {
3639 PyObject *tp, *v, *tb;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003640
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003641 PyErr_Fetch(&tp, &v, &tb);
Raymond Hettinger8ae46892003-10-12 19:09:37 +00003642 if ((r=PyTuple_Pack(3,v,cls,args))) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003643 Py_XDECREF(v);
3644 v=r;
3645 }
3646 PyErr_Restore(tp,v,tb);
3647 }
3648 return NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003649}
Tim Peters84e87f32001-03-17 04:50:51 +00003650
Guido van Rossum60456fd1997-04-09 17:36:32 +00003651
3652static int
Tim Peterscba30e22003-02-01 06:24:36 +00003653load_obj(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003654{
3655 PyObject *class, *tup, *obj=0;
3656 int i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003657
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003658 if ((i = marker(self)) < 0) return -1;
3659 if (!( tup=Pdata_popTuple(self->stack, i+1))) return -1;
3660 PDATA_POP(self->stack, class);
3661 if (class) {
3662 obj = Instance_New(class, tup);
3663 Py_DECREF(class);
3664 }
3665 Py_DECREF(tup);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003666
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003667 if (! obj) return -1;
3668 PDATA_PUSH(self->stack, obj, -1);
3669 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003670}
3671
3672
3673static int
Tim Peterscba30e22003-02-01 06:24:36 +00003674load_inst(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003675{
3676 PyObject *tup, *class=0, *obj=0, *module_name, *class_name;
3677 int i, len;
3678 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003679
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003680 if ((i = marker(self)) < 0) return -1;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003681
Tim Peters0bc93f52003-02-02 18:29:33 +00003682 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003683 if (len < 2) return bad_readline();
3684 module_name = PyString_FromStringAndSize(s, len - 1);
3685 if (!module_name) return -1;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003686
Tim Peters0bc93f52003-02-02 18:29:33 +00003687 if ((len = self->readline_func(self, &s)) >= 0) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003688 if (len < 2) return bad_readline();
3689 if ((class_name = PyString_FromStringAndSize(s, len - 1))) {
Tim Peterscba30e22003-02-01 06:24:36 +00003690 class = find_class(module_name, class_name,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003691 self->find_class);
3692 Py_DECREF(class_name);
3693 }
3694 }
3695 Py_DECREF(module_name);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003696
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003697 if (! class) return -1;
Tim Peters84e87f32001-03-17 04:50:51 +00003698
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003699 if ((tup=Pdata_popTuple(self->stack, i))) {
3700 obj = Instance_New(class, tup);
3701 Py_DECREF(tup);
3702 }
3703 Py_DECREF(class);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003704
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003705 if (! obj) return -1;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003706
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003707 PDATA_PUSH(self->stack, obj, -1);
3708 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003709}
3710
Tim Peterseab7db32003-02-13 18:24:14 +00003711static int
3712load_newobj(Unpicklerobject *self)
3713{
3714 PyObject *args = NULL;
3715 PyObject *clsraw = NULL;
3716 PyTypeObject *cls; /* clsraw cast to its true type */
3717 PyObject *obj;
3718
3719 /* Stack is ... cls argtuple, and we want to call
3720 * cls.__new__(cls, *argtuple).
3721 */
3722 PDATA_POP(self->stack, args);
3723 if (args == NULL) goto Fail;
3724 if (! PyTuple_Check(args)) {
3725 PyErr_SetString(UnpicklingError, "NEWOBJ expected an arg "
3726 "tuple.");
3727 goto Fail;
3728 }
3729
3730 PDATA_POP(self->stack, clsraw);
3731 cls = (PyTypeObject *)clsraw;
3732 if (cls == NULL) goto Fail;
3733 if (! PyType_Check(cls)) {
3734 PyErr_SetString(UnpicklingError, "NEWOBJ class argument "
3735 "isn't a type object");
3736 goto Fail;
3737 }
3738 if (cls->tp_new == NULL) {
3739 PyErr_SetString(UnpicklingError, "NEWOBJ class argument "
3740 "has NULL tp_new");
3741 goto Fail;
3742 }
3743
3744 /* Call __new__. */
3745 obj = cls->tp_new(cls, args, NULL);
3746 if (obj == NULL) goto Fail;
3747
3748 Py_DECREF(args);
3749 Py_DECREF(clsraw);
3750 PDATA_PUSH(self->stack, obj, -1);
3751 return 0;
3752
3753 Fail:
3754 Py_XDECREF(args);
3755 Py_XDECREF(clsraw);
3756 return -1;
3757}
Guido van Rossum60456fd1997-04-09 17:36:32 +00003758
3759static int
Tim Peterscba30e22003-02-01 06:24:36 +00003760load_global(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003761{
3762 PyObject *class = 0, *module_name = 0, *class_name = 0;
3763 int len;
3764 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003765
Tim Peters0bc93f52003-02-02 18:29:33 +00003766 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003767 if (len < 2) return bad_readline();
3768 module_name = PyString_FromStringAndSize(s, len - 1);
3769 if (!module_name) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003770
Tim Peters0bc93f52003-02-02 18:29:33 +00003771 if ((len = self->readline_func(self, &s)) >= 0) {
Tim Peters4e52ca82002-12-07 02:43:28 +00003772 if (len < 2) {
3773 Py_DECREF(module_name);
3774 return bad_readline();
3775 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003776 if ((class_name = PyString_FromStringAndSize(s, len - 1))) {
Tim Peterscba30e22003-02-01 06:24:36 +00003777 class = find_class(module_name, class_name,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003778 self->find_class);
3779 Py_DECREF(class_name);
3780 }
3781 }
3782 Py_DECREF(module_name);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003783
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003784 if (! class) return -1;
3785 PDATA_PUSH(self->stack, class, -1);
3786 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003787}
3788
3789
3790static int
Tim Peterscba30e22003-02-01 06:24:36 +00003791load_persid(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003792{
3793 PyObject *pid = 0;
3794 int len;
3795 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003796
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003797 if (self->pers_func) {
Tim Peters0bc93f52003-02-02 18:29:33 +00003798 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003799 if (len < 2) return bad_readline();
3800
3801 pid = PyString_FromStringAndSize(s, len - 1);
3802 if (!pid) return -1;
3803
3804 if (PyList_Check(self->pers_func)) {
3805 if (PyList_Append(self->pers_func, pid) < 0) {
3806 Py_DECREF(pid);
3807 return -1;
3808 }
3809 }
3810 else {
3811 ARG_TUP(self, pid);
3812 if (self->arg) {
Tim Peterscba30e22003-02-01 06:24:36 +00003813 pid = PyObject_Call(self->pers_func, self->arg,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003814 NULL);
3815 FREE_ARG_TUP(self);
3816 }
3817 }
3818
3819 if (! pid) return -1;
3820
3821 PDATA_PUSH(self->stack, pid, -1);
3822 return 0;
3823 }
3824 else {
3825 PyErr_SetString(UnpicklingError,
3826 "A load persistent id instruction was encountered,\n"
3827 "but no persistent_load function was specified.");
3828 return -1;
3829 }
3830}
3831
3832static int
Tim Peterscba30e22003-02-01 06:24:36 +00003833load_binpersid(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003834{
3835 PyObject *pid = 0;
3836
3837 if (self->pers_func) {
3838 PDATA_POP(self->stack, pid);
3839 if (! pid) return -1;
3840
3841 if (PyList_Check(self->pers_func)) {
3842 if (PyList_Append(self->pers_func, pid) < 0) {
3843 Py_DECREF(pid);
3844 return -1;
3845 }
3846 }
3847 else {
3848 ARG_TUP(self, pid);
3849 if (self->arg) {
3850 pid = PyObject_Call(self->pers_func, self->arg,
3851 NULL);
3852 FREE_ARG_TUP(self);
3853 }
3854 if (! pid) return -1;
3855 }
3856
3857 PDATA_PUSH(self->stack, pid, -1);
3858 return 0;
3859 }
3860 else {
3861 PyErr_SetString(UnpicklingError,
3862 "A load persistent id instruction was encountered,\n"
3863 "but no persistent_load function was specified.");
3864 return -1;
3865 }
3866}
3867
3868
3869static int
Tim Peterscba30e22003-02-01 06:24:36 +00003870load_pop(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003871{
3872 int len;
3873
3874 if (!( (len=self->stack->length) > 0 )) return stackUnderflow();
3875
3876 /* Note that we split the (pickle.py) stack into two stacks,
3877 an object stack and a mark stack. We have to be clever and
3878 pop the right one. We do this by looking at the top of the
3879 mark stack.
3880 */
3881
3882 if ((self->num_marks > 0) &&
3883 (self->marks[self->num_marks - 1] == len))
3884 self->num_marks--;
3885 else {
3886 len--;
3887 Py_DECREF(self->stack->data[len]);
3888 self->stack->length=len;
3889 }
3890
3891 return 0;
3892}
3893
3894
3895static int
Tim Peterscba30e22003-02-01 06:24:36 +00003896load_pop_mark(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003897{
3898 int i;
3899
3900 if ((i = marker(self)) < 0)
3901 return -1;
3902
3903 Pdata_clear(self->stack, i);
3904
3905 return 0;
3906}
3907
3908
3909static int
Tim Peterscba30e22003-02-01 06:24:36 +00003910load_dup(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003911{
3912 PyObject *last;
3913 int len;
3914
3915 if ((len = self->stack->length) <= 0) return stackUnderflow();
3916 last=self->stack->data[len-1];
3917 Py_INCREF(last);
3918 PDATA_PUSH(self->stack, last, -1);
3919 return 0;
3920}
3921
3922
3923static int
Tim Peterscba30e22003-02-01 06:24:36 +00003924load_get(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003925{
3926 PyObject *py_str = 0, *value = 0;
3927 int len;
3928 char *s;
3929 int rc;
3930
Tim Peters0bc93f52003-02-02 18:29:33 +00003931 if ((len = self->readline_func(self, &s)) < 0) return -1;
Guido van Rossumf9ffb031999-02-04 14:54:04 +00003932 if (len < 2) return bad_readline();
Tim Peters84e87f32001-03-17 04:50:51 +00003933
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003934 if (!( py_str = PyString_FromStringAndSize(s, len - 1))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003935
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003936 value = PyDict_GetItem(self->memo, py_str);
3937 if (! value) {
3938 PyErr_SetObject(BadPickleGet, py_str);
3939 rc = -1;
Tim Peters92c8bb32003-02-13 23:00:26 +00003940 }
3941 else {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003942 PDATA_APPEND(self->stack, value, -1);
3943 rc = 0;
3944 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003945
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003946 Py_DECREF(py_str);
3947 return rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003948}
3949
3950
3951static int
Tim Peterscba30e22003-02-01 06:24:36 +00003952load_binget(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003953{
3954 PyObject *py_key = 0, *value = 0;
3955 unsigned char key;
3956 char *s;
3957 int rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003958
Tim Peters0bc93f52003-02-02 18:29:33 +00003959 if (self->read_func(self, &s, 1) < 0) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003960
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003961 key = (unsigned char)s[0];
3962 if (!( py_key = PyInt_FromLong((long)key))) return -1;
Guido van Rossumea2b7152000-05-09 18:14:50 +00003963
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003964 value = PyDict_GetItem(self->memo, py_key);
3965 if (! value) {
3966 PyErr_SetObject(BadPickleGet, py_key);
3967 rc = -1;
Tim Peters92c8bb32003-02-13 23:00:26 +00003968 }
3969 else {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003970 PDATA_APPEND(self->stack, value, -1);
3971 rc = 0;
3972 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003973
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003974 Py_DECREF(py_key);
3975 return rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003976}
3977
3978
3979static int
Tim Peterscba30e22003-02-01 06:24:36 +00003980load_long_binget(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003981{
3982 PyObject *py_key = 0, *value = 0;
3983 unsigned char c;
3984 char *s;
3985 long key;
3986 int rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003987
Tim Peters0bc93f52003-02-02 18:29:33 +00003988 if (self->read_func(self, &s, 4) < 0) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003989
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003990 c = (unsigned char)s[0];
3991 key = (long)c;
3992 c = (unsigned char)s[1];
3993 key |= (long)c << 8;
3994 c = (unsigned char)s[2];
3995 key |= (long)c << 16;
3996 c = (unsigned char)s[3];
3997 key |= (long)c << 24;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003998
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003999 if (!( py_key = PyInt_FromLong((long)key))) return -1;
4000
4001 value = PyDict_GetItem(self->memo, py_key);
4002 if (! value) {
4003 PyErr_SetObject(BadPickleGet, py_key);
4004 rc = -1;
Tim Peters92c8bb32003-02-13 23:00:26 +00004005 }
4006 else {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004007 PDATA_APPEND(self->stack, value, -1);
4008 rc = 0;
4009 }
4010
4011 Py_DECREF(py_key);
4012 return rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004013}
4014
Tim Peters2d629652003-02-04 05:06:17 +00004015/* Push an object from the extension registry (EXT[124]). nbytes is
4016 * the number of bytes following the opcode, holding the index (code) value.
4017 */
4018static int
4019load_extension(Unpicklerobject *self, int nbytes)
4020{
4021 char *codebytes; /* the nbytes bytes after the opcode */
4022 long code; /* calc_binint returns long */
4023 PyObject *py_code; /* code as a Python int */
4024 PyObject *obj; /* the object to push */
4025 PyObject *pair; /* (module_name, class_name) */
4026 PyObject *module_name, *class_name;
4027
4028 assert(nbytes == 1 || nbytes == 2 || nbytes == 4);
4029 if (self->read_func(self, &codebytes, nbytes) < 0) return -1;
4030 code = calc_binint(codebytes, nbytes);
4031 if (code <= 0) { /* note that 0 is forbidden */
4032 /* Corrupt or hostile pickle. */
4033 PyErr_SetString(UnpicklingError, "EXT specifies code <= 0");
4034 return -1;
4035 }
4036
4037 /* Look for the code in the cache. */
4038 py_code = PyInt_FromLong(code);
4039 if (py_code == NULL) return -1;
4040 obj = PyDict_GetItem(extension_cache, py_code);
4041 if (obj != NULL) {
4042 /* Bingo. */
4043 Py_DECREF(py_code);
4044 PDATA_APPEND(self->stack, obj, -1);
4045 return 0;
4046 }
4047
4048 /* Look up the (module_name, class_name) pair. */
4049 pair = PyDict_GetItem(inverted_registry, py_code);
4050 if (pair == NULL) {
4051 Py_DECREF(py_code);
4052 PyErr_Format(PyExc_ValueError, "unregistered extension "
4053 "code %ld", code);
4054 return -1;
4055 }
4056 /* Since the extension registry is manipulable via Python code,
Tim Petersfa05ce32003-02-04 05:20:32 +00004057 * confirm that pair is really a 2-tuple of strings.
Tim Peters2d629652003-02-04 05:06:17 +00004058 */
4059 if (!PyTuple_Check(pair) || PyTuple_Size(pair) != 2 ||
4060 !PyString_Check(module_name = PyTuple_GET_ITEM(pair, 0)) ||
4061 !PyString_Check(class_name = PyTuple_GET_ITEM(pair, 1))) {
4062 Py_DECREF(py_code);
4063 PyErr_Format(PyExc_ValueError, "_inverted_registry[%ld] "
4064 "isn't a 2-tuple of strings", code);
4065 return -1;
4066 }
4067 /* Load the object. */
4068 obj = find_class(module_name, class_name, self->find_class);
4069 if (obj == NULL) {
4070 Py_DECREF(py_code);
4071 return -1;
4072 }
4073 /* Cache code -> obj. */
4074 code = PyDict_SetItem(extension_cache, py_code, obj);
4075 Py_DECREF(py_code);
4076 if (code < 0) {
4077 Py_DECREF(obj);
4078 return -1;
4079 }
4080 PDATA_PUSH(self->stack, obj, -1);
4081 return 0;
4082}
Guido van Rossum60456fd1997-04-09 17:36:32 +00004083
4084static int
Tim Peterscba30e22003-02-01 06:24:36 +00004085load_put(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004086{
4087 PyObject *py_str = 0, *value = 0;
4088 int len, l;
4089 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004090
Tim Peters0bc93f52003-02-02 18:29:33 +00004091 if ((l = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004092 if (l < 2) return bad_readline();
4093 if (!( len=self->stack->length )) return stackUnderflow();
4094 if (!( py_str = PyString_FromStringAndSize(s, l - 1))) return -1;
4095 value=self->stack->data[len-1];
4096 l=PyDict_SetItem(self->memo, py_str, value);
4097 Py_DECREF(py_str);
4098 return l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004099}
4100
4101
4102static int
Tim Peterscba30e22003-02-01 06:24:36 +00004103load_binput(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004104{
4105 PyObject *py_key = 0, *value = 0;
4106 unsigned char key;
4107 char *s;
4108 int len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004109
Tim Peters0bc93f52003-02-02 18:29:33 +00004110 if (self->read_func(self, &s, 1) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004111 if (!( (len=self->stack->length) > 0 )) return stackUnderflow();
Guido van Rossum60456fd1997-04-09 17:36:32 +00004112
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004113 key = (unsigned char)s[0];
Guido van Rossum053b8df1998-11-25 16:18:00 +00004114
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004115 if (!( py_key = PyInt_FromLong((long)key))) return -1;
4116 value=self->stack->data[len-1];
4117 len=PyDict_SetItem(self->memo, py_key, value);
4118 Py_DECREF(py_key);
4119 return len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004120}
4121
4122
4123static int
Tim Peterscba30e22003-02-01 06:24:36 +00004124load_long_binput(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004125{
4126 PyObject *py_key = 0, *value = 0;
4127 long key;
4128 unsigned char c;
4129 char *s;
4130 int len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004131
Tim Peters0bc93f52003-02-02 18:29:33 +00004132 if (self->read_func(self, &s, 4) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004133 if (!( len=self->stack->length )) return stackUnderflow();
Guido van Rossum60456fd1997-04-09 17:36:32 +00004134
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004135 c = (unsigned char)s[0];
4136 key = (long)c;
4137 c = (unsigned char)s[1];
4138 key |= (long)c << 8;
4139 c = (unsigned char)s[2];
4140 key |= (long)c << 16;
4141 c = (unsigned char)s[3];
4142 key |= (long)c << 24;
Tim Peters84e87f32001-03-17 04:50:51 +00004143
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004144 if (!( py_key = PyInt_FromLong(key))) return -1;
4145 value=self->stack->data[len-1];
4146 len=PyDict_SetItem(self->memo, py_key, value);
4147 Py_DECREF(py_key);
4148 return len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004149}
4150
4151
4152static int
Tim Peterscba30e22003-02-01 06:24:36 +00004153do_append(Unpicklerobject *self, int x)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004154{
4155 PyObject *value = 0, *list = 0, *append_method = 0;
4156 int len, i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004157
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004158 len=self->stack->length;
4159 if (!( len >= x && x > 0 )) return stackUnderflow();
4160 /* nothing to do */
Tim Peterscba30e22003-02-01 06:24:36 +00004161 if (len==x) return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004162
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004163 list=self->stack->data[x-1];
Guido van Rossum60456fd1997-04-09 17:36:32 +00004164
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004165 if (PyList_Check(list)) {
4166 PyObject *slice;
4167 int list_len;
Tim Peters84e87f32001-03-17 04:50:51 +00004168
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004169 slice=Pdata_popList(self->stack, x);
4170 list_len = PyList_GET_SIZE(list);
4171 i=PyList_SetSlice(list, list_len, list_len, slice);
4172 Py_DECREF(slice);
4173 return i;
4174 }
4175 else {
Guido van Rossum60456fd1997-04-09 17:36:32 +00004176
Tim Peterscba30e22003-02-01 06:24:36 +00004177 if (!( append_method = PyObject_GetAttr(list, append_str)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004178 return -1;
4179
4180 for (i = x; i < len; i++) {
4181 PyObject *junk;
4182
4183 value=self->stack->data[i];
4184 junk=0;
4185 ARG_TUP(self, value);
4186 if (self->arg) {
Tim Peterscba30e22003-02-01 06:24:36 +00004187 junk = PyObject_Call(append_method, self->arg,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004188 NULL);
4189 FREE_ARG_TUP(self);
4190 }
4191 if (! junk) {
4192 Pdata_clear(self->stack, i+1);
4193 self->stack->length=x;
4194 Py_DECREF(append_method);
4195 return -1;
4196 }
4197 Py_DECREF(junk);
4198 }
4199 self->stack->length=x;
4200 Py_DECREF(append_method);
4201 }
4202
4203 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004204}
4205
4206
4207static int
Tim Peterscba30e22003-02-01 06:24:36 +00004208load_append(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004209{
4210 return do_append(self, self->stack->length - 1);
Guido van Rossum60456fd1997-04-09 17:36:32 +00004211}
4212
4213
4214static int
Tim Peterscba30e22003-02-01 06:24:36 +00004215load_appends(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004216{
4217 return do_append(self, marker(self));
Guido van Rossum60456fd1997-04-09 17:36:32 +00004218}
4219
4220
4221static int
Tim Peterscba30e22003-02-01 06:24:36 +00004222do_setitems(Unpicklerobject *self, int x)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004223{
4224 PyObject *value = 0, *key = 0, *dict = 0;
4225 int len, i, r=0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004226
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004227 if (!( (len=self->stack->length) >= x
4228 && x > 0 )) return stackUnderflow();
Guido van Rossum60456fd1997-04-09 17:36:32 +00004229
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004230 dict=self->stack->data[x-1];
Guido van Rossum60456fd1997-04-09 17:36:32 +00004231
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004232 for (i = x+1; i < len; i += 2) {
4233 key =self->stack->data[i-1];
4234 value=self->stack->data[i ];
4235 if (PyObject_SetItem(dict, key, value) < 0) {
4236 r=-1;
4237 break;
4238 }
4239 }
4240
4241 Pdata_clear(self->stack, x);
4242
4243 return r;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004244}
4245
4246
Tim Peters84e87f32001-03-17 04:50:51 +00004247static int
Tim Peterscba30e22003-02-01 06:24:36 +00004248load_setitem(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004249{
4250 return do_setitems(self, self->stack->length - 2);
4251}
Guido van Rossum60456fd1997-04-09 17:36:32 +00004252
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004253static int
Tim Peterscba30e22003-02-01 06:24:36 +00004254load_setitems(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004255{
4256 return do_setitems(self, marker(self));
Guido van Rossum60456fd1997-04-09 17:36:32 +00004257}
4258
Tim Peters84e87f32001-03-17 04:50:51 +00004259
Guido van Rossum60456fd1997-04-09 17:36:32 +00004260static int
Tim Peterscba30e22003-02-01 06:24:36 +00004261load_build(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004262{
Tim Peters080c88b2003-02-15 03:01:11 +00004263 PyObject *state, *inst, *slotstate;
4264 PyObject *__setstate__;
4265 PyObject *d_key, *d_value;
Martin v. Löwis18e16552006-02-15 17:27:45 +00004266 Py_ssize_t i;
Tim Peters080c88b2003-02-15 03:01:11 +00004267 int res = -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004268
Tim Peters080c88b2003-02-15 03:01:11 +00004269 /* Stack is ... instance, state. We want to leave instance at
4270 * the stack top, possibly mutated via instance.__setstate__(state).
4271 */
4272 if (self->stack->length < 2)
4273 return stackUnderflow();
4274 PDATA_POP(self->stack, state);
4275 if (state == NULL)
4276 return -1;
4277 inst = self->stack->data[self->stack->length - 1];
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004278
Tim Peters080c88b2003-02-15 03:01:11 +00004279 __setstate__ = PyObject_GetAttr(inst, __setstate___str);
4280 if (__setstate__ != NULL) {
4281 PyObject *junk = NULL;
4282
4283 /* The explicit __setstate__ is responsible for everything. */
4284 ARG_TUP(self, state);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004285 if (self->arg) {
4286 junk = PyObject_Call(__setstate__, self->arg, NULL);
4287 FREE_ARG_TUP(self);
4288 }
4289 Py_DECREF(__setstate__);
Tim Peters080c88b2003-02-15 03:01:11 +00004290 if (junk == NULL)
4291 return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004292 Py_DECREF(junk);
4293 return 0;
4294 }
Jeremy Hyltonf00368f2003-06-16 20:19:49 +00004295 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4296 return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004297 PyErr_Clear();
Tim Peters080c88b2003-02-15 03:01:11 +00004298
4299 /* A default __setstate__. First see whether state embeds a
4300 * slot state dict too (a proto 2 addition).
4301 */
4302 if (PyTuple_Check(state) && PyTuple_Size(state) == 2) {
4303 PyObject *temp = state;
4304 state = PyTuple_GET_ITEM(temp, 0);
4305 slotstate = PyTuple_GET_ITEM(temp, 1);
4306 Py_INCREF(state);
4307 Py_INCREF(slotstate);
4308 Py_DECREF(temp);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004309 }
Tim Peters080c88b2003-02-15 03:01:11 +00004310 else
4311 slotstate = NULL;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004312
Tim Peters080c88b2003-02-15 03:01:11 +00004313 /* Set inst.__dict__ from the state dict (if any). */
4314 if (state != Py_None) {
4315 PyObject *dict;
4316 if (! PyDict_Check(state)) {
4317 PyErr_SetString(UnpicklingError, "state is not a "
4318 "dictionary");
4319 goto finally;
4320 }
4321 dict = PyObject_GetAttr(inst, __dict___str);
4322 if (dict == NULL)
4323 goto finally;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004324
Tim Peters080c88b2003-02-15 03:01:11 +00004325 i = 0;
4326 while (PyDict_Next(state, &i, &d_key, &d_value)) {
4327 if (PyObject_SetItem(dict, d_key, d_value) < 0)
4328 goto finally;
4329 }
4330 Py_DECREF(dict);
4331 }
4332
4333 /* Also set instance attributes from the slotstate dict (if any). */
4334 if (slotstate != NULL) {
4335 if (! PyDict_Check(slotstate)) {
4336 PyErr_SetString(UnpicklingError, "slot state is not "
4337 "a dictionary");
4338 goto finally;
4339 }
4340 i = 0;
4341 while (PyDict_Next(slotstate, &i, &d_key, &d_value)) {
4342 if (PyObject_SetAttr(inst, d_key, d_value) < 0)
4343 goto finally;
4344 }
4345 }
4346 res = 0;
4347
4348 finally:
4349 Py_DECREF(state);
4350 Py_XDECREF(slotstate);
4351 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004352}
4353
4354
4355static int
Tim Peterscba30e22003-02-01 06:24:36 +00004356load_mark(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004357{
4358 int s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004359
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004360 /* Note that we split the (pickle.py) stack into two stacks, an
4361 object stack and a mark stack. Here we push a mark onto the
4362 mark stack.
4363 */
Guido van Rossum60456fd1997-04-09 17:36:32 +00004364
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004365 if ((self->num_marks + 1) >= self->marks_size) {
4366 s=self->marks_size+20;
4367 if (s <= self->num_marks) s=self->num_marks + 1;
4368 if (self->marks == NULL)
4369 self->marks=(int *)malloc(s * sizeof(int));
4370 else
Tim Peterscba30e22003-02-01 06:24:36 +00004371 self->marks=(int *)realloc(self->marks,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004372 s * sizeof(int));
4373 if (! self->marks) {
4374 PyErr_NoMemory();
4375 return -1;
4376 }
4377 self->marks_size = s;
4378 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00004379
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004380 self->marks[self->num_marks++] = self->stack->length;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004381
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004382 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004383}
4384
Guido van Rossum60456fd1997-04-09 17:36:32 +00004385static int
Tim Peterscba30e22003-02-01 06:24:36 +00004386load_reduce(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004387{
4388 PyObject *callable = 0, *arg_tup = 0, *ob = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004389
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004390 PDATA_POP(self->stack, arg_tup);
4391 if (! arg_tup) return -1;
4392 PDATA_POP(self->stack, callable);
4393 if (callable) {
4394 ob = Instance_New(callable, arg_tup);
4395 Py_DECREF(callable);
4396 }
4397 Py_DECREF(arg_tup);
Guido van Rossum60456fd1997-04-09 17:36:32 +00004398
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004399 if (! ob) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004400
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004401 PDATA_PUSH(self->stack, ob, -1);
4402 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004403}
Tim Peters84e87f32001-03-17 04:50:51 +00004404
Tim Peters4190fb82003-02-02 16:09:05 +00004405/* Just raises an error if we don't know the protocol specified. PROTO
4406 * is the first opcode for protocols >= 2.
4407 */
4408static int
4409load_proto(Unpicklerobject *self)
4410{
4411 int i;
4412 char *protobyte;
4413
4414 i = self->read_func(self, &protobyte, 1);
4415 if (i < 0)
4416 return -1;
4417
4418 i = calc_binint(protobyte, 1);
4419 /* No point checking for < 0, since calc_binint returns an unsigned
4420 * int when chewing on 1 byte.
4421 */
4422 assert(i >= 0);
Tim Peters8587b3c2003-02-13 15:44:41 +00004423 if (i <= HIGHEST_PROTOCOL)
Tim Peters4190fb82003-02-02 16:09:05 +00004424 return 0;
4425
4426 PyErr_Format(PyExc_ValueError, "unsupported pickle protocol: %d", i);
4427 return -1;
4428}
4429
Guido van Rossum60456fd1997-04-09 17:36:32 +00004430static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00004431load(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004432{
4433 PyObject *err = 0, *val = 0;
4434 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004435
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004436 self->num_marks = 0;
4437 if (self->stack->length) Pdata_clear(self->stack, 0);
Guido van Rossum60456fd1997-04-09 17:36:32 +00004438
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004439 while (1) {
Tim Peters0bc93f52003-02-02 18:29:33 +00004440 if (self->read_func(self, &s, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004441 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004442
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004443 switch (s[0]) {
4444 case NONE:
4445 if (load_none(self) < 0)
4446 break;
4447 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004448
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004449 case BININT:
4450 if (load_binint(self) < 0)
4451 break;
4452 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004453
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004454 case BININT1:
4455 if (load_binint1(self) < 0)
4456 break;
4457 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004458
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004459 case BININT2:
4460 if (load_binint2(self) < 0)
4461 break;
4462 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004463
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004464 case INT:
4465 if (load_int(self) < 0)
4466 break;
4467 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004468
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004469 case LONG:
4470 if (load_long(self) < 0)
4471 break;
4472 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004473
Tim Petersee1a53c2003-02-02 02:57:53 +00004474 case LONG1:
4475 if (load_counted_long(self, 1) < 0)
4476 break;
4477 continue;
4478
4479 case LONG4:
4480 if (load_counted_long(self, 4) < 0)
4481 break;
4482 continue;
4483
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004484 case FLOAT:
4485 if (load_float(self) < 0)
4486 break;
4487 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004488
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004489 case BINFLOAT:
4490 if (load_binfloat(self) < 0)
4491 break;
4492 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004493
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004494 case BINSTRING:
4495 if (load_binstring(self) < 0)
4496 break;
4497 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004498
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004499 case SHORT_BINSTRING:
4500 if (load_short_binstring(self) < 0)
4501 break;
4502 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004503
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004504 case STRING:
4505 if (load_string(self) < 0)
4506 break;
4507 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004508
Martin v. Löwis339d0f72001-08-17 18:39:25 +00004509#ifdef Py_USING_UNICODE
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004510 case UNICODE:
4511 if (load_unicode(self) < 0)
4512 break;
4513 continue;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00004514
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004515 case BINUNICODE:
4516 if (load_binunicode(self) < 0)
4517 break;
4518 continue;
Martin v. Löwis339d0f72001-08-17 18:39:25 +00004519#endif
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00004520
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004521 case EMPTY_TUPLE:
Tim Peters1d63c9f2003-02-02 20:29:39 +00004522 if (load_counted_tuple(self, 0) < 0)
4523 break;
4524 continue;
4525
4526 case TUPLE1:
4527 if (load_counted_tuple(self, 1) < 0)
4528 break;
4529 continue;
4530
4531 case TUPLE2:
4532 if (load_counted_tuple(self, 2) < 0)
4533 break;
4534 continue;
4535
4536 case TUPLE3:
4537 if (load_counted_tuple(self, 3) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004538 break;
4539 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004540
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004541 case TUPLE:
4542 if (load_tuple(self) < 0)
4543 break;
4544 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004545
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004546 case EMPTY_LIST:
4547 if (load_empty_list(self) < 0)
4548 break;
4549 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004550
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004551 case LIST:
4552 if (load_list(self) < 0)
4553 break;
4554 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004555
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004556 case EMPTY_DICT:
4557 if (load_empty_dict(self) < 0)
4558 break;
4559 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004560
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004561 case DICT:
4562 if (load_dict(self) < 0)
4563 break;
4564 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004565
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004566 case OBJ:
4567 if (load_obj(self) < 0)
4568 break;
4569 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004570
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004571 case INST:
4572 if (load_inst(self) < 0)
4573 break;
4574 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004575
Tim Peterseab7db32003-02-13 18:24:14 +00004576 case NEWOBJ:
4577 if (load_newobj(self) < 0)
4578 break;
4579 continue;
4580
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004581 case GLOBAL:
4582 if (load_global(self) < 0)
4583 break;
4584 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004585
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004586 case APPEND:
4587 if (load_append(self) < 0)
4588 break;
4589 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004590
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004591 case APPENDS:
4592 if (load_appends(self) < 0)
4593 break;
4594 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00004595
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004596 case BUILD:
4597 if (load_build(self) < 0)
4598 break;
4599 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00004600
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004601 case DUP:
4602 if (load_dup(self) < 0)
4603 break;
4604 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004605
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004606 case BINGET:
4607 if (load_binget(self) < 0)
4608 break;
4609 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004610
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004611 case LONG_BINGET:
4612 if (load_long_binget(self) < 0)
4613 break;
4614 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00004615
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004616 case GET:
4617 if (load_get(self) < 0)
4618 break;
4619 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004620
Tim Peters2d629652003-02-04 05:06:17 +00004621 case EXT1:
4622 if (load_extension(self, 1) < 0)
4623 break;
4624 continue;
4625
4626 case EXT2:
4627 if (load_extension(self, 2) < 0)
4628 break;
4629 continue;
4630
4631 case EXT4:
4632 if (load_extension(self, 4) < 0)
4633 break;
4634 continue;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004635 case MARK:
4636 if (load_mark(self) < 0)
4637 break;
4638 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004639
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004640 case BINPUT:
4641 if (load_binput(self) < 0)
4642 break;
4643 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004644
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004645 case LONG_BINPUT:
4646 if (load_long_binput(self) < 0)
4647 break;
4648 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00004649
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004650 case PUT:
4651 if (load_put(self) < 0)
4652 break;
4653 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004654
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004655 case POP:
4656 if (load_pop(self) < 0)
4657 break;
4658 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004659
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004660 case POP_MARK:
4661 if (load_pop_mark(self) < 0)
4662 break;
4663 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004664
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004665 case SETITEM:
4666 if (load_setitem(self) < 0)
4667 break;
4668 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004669
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004670 case SETITEMS:
4671 if (load_setitems(self) < 0)
4672 break;
4673 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004674
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004675 case STOP:
4676 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004677
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004678 case PERSID:
4679 if (load_persid(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 BINPERSID:
4684 if (load_binpersid(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 REDUCE:
4689 if (load_reduce(self) < 0)
4690 break;
4691 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004692
Tim Peters4190fb82003-02-02 16:09:05 +00004693 case PROTO:
4694 if (load_proto(self) < 0)
4695 break;
4696 continue;
4697
Tim Peters3c67d792003-02-02 17:59:11 +00004698 case NEWTRUE:
4699 if (load_bool(self, Py_True) < 0)
4700 break;
4701 continue;
4702
4703 case NEWFALSE:
4704 if (load_bool(self, Py_False) < 0)
4705 break;
4706 continue;
4707
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004708 case '\0':
4709 /* end of file */
4710 PyErr_SetNone(PyExc_EOFError);
4711 break;
Tim Peterscba30e22003-02-01 06:24:36 +00004712
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004713 default:
Tim Peterscba30e22003-02-01 06:24:36 +00004714 cPickle_ErrFormat(UnpicklingError,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004715 "invalid load key, '%s'.",
4716 "c", s[0]);
4717 return NULL;
4718 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00004719
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004720 break;
4721 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00004722
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004723 if ((err = PyErr_Occurred())) {
4724 if (err == PyExc_EOFError) {
4725 PyErr_SetNone(PyExc_EOFError);
4726 }
4727 return NULL;
4728 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00004729
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004730 PDATA_POP(self->stack, val);
4731 return val;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004732}
Tim Peters84e87f32001-03-17 04:50:51 +00004733
Guido van Rossum60456fd1997-04-09 17:36:32 +00004734
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004735/* No-load functions to support noload, which is used to
4736 find persistent references. */
4737
4738static int
Tim Peterscba30e22003-02-01 06:24:36 +00004739noload_obj(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004740{
4741 int i;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004742
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004743 if ((i = marker(self)) < 0) return -1;
4744 return Pdata_clear(self->stack, i+1);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004745}
4746
4747
4748static int
Tim Peterscba30e22003-02-01 06:24:36 +00004749noload_inst(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004750{
4751 int i;
4752 char *s;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004753
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004754 if ((i = marker(self)) < 0) return -1;
4755 Pdata_clear(self->stack, i);
Tim Peters0bc93f52003-02-02 18:29:33 +00004756 if (self->readline_func(self, &s) < 0) return -1;
4757 if (self->readline_func(self, &s) < 0) return -1;
Tim Peterseab7db32003-02-13 18:24:14 +00004758 PDATA_APPEND(self->stack, Py_None, -1);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004759 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004760}
4761
4762static int
Tim Peterseab7db32003-02-13 18:24:14 +00004763noload_newobj(Unpicklerobject *self)
4764{
4765 PyObject *obj;
4766
4767 PDATA_POP(self->stack, obj); /* pop argtuple */
4768 if (obj == NULL) return -1;
4769 Py_DECREF(obj);
4770
4771 PDATA_POP(self->stack, obj); /* pop cls */
4772 if (obj == NULL) return -1;
4773 Py_DECREF(obj);
4774
4775 PDATA_APPEND(self->stack, Py_None, -1);
4776 return 0;
4777}
4778
4779static int
Tim Peterscba30e22003-02-01 06:24:36 +00004780noload_global(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004781{
4782 char *s;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004783
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;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004786 PDATA_APPEND(self->stack, Py_None,-1);
4787 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004788}
4789
4790static int
Tim Peterscba30e22003-02-01 06:24:36 +00004791noload_reduce(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004792{
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004793
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004794 if (self->stack->length < 2) return stackUnderflow();
4795 Pdata_clear(self->stack, self->stack->length-2);
4796 PDATA_APPEND(self->stack, Py_None,-1);
4797 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004798}
4799
4800static int
4801noload_build(Unpicklerobject *self) {
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004802
Guido van Rossum053b8df1998-11-25 16:18:00 +00004803 if (self->stack->length < 1) return stackUnderflow();
4804 Pdata_clear(self->stack, self->stack->length-1);
Guido van Rossum50f385c1998-12-04 18:48:44 +00004805 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004806}
4807
Tim Peters2d629652003-02-04 05:06:17 +00004808static int
4809noload_extension(Unpicklerobject *self, int nbytes)
4810{
4811 char *codebytes;
4812
4813 assert(nbytes == 1 || nbytes == 2 || nbytes == 4);
4814 if (self->read_func(self, &codebytes, nbytes) < 0) return -1;
4815 PDATA_APPEND(self->stack, Py_None, -1);
4816 return 0;
4817}
4818
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004819
4820static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00004821noload(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004822{
4823 PyObject *err = 0, *val = 0;
4824 char *s;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004825
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004826 self->num_marks = 0;
4827 Pdata_clear(self->stack, 0);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004828
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004829 while (1) {
Tim Peters0bc93f52003-02-02 18:29:33 +00004830 if (self->read_func(self, &s, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004831 break;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004832
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004833 switch (s[0]) {
4834 case NONE:
4835 if (load_none(self) < 0)
4836 break;
4837 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004838
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004839 case BININT:
4840 if (load_binint(self) < 0)
4841 break;
4842 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004843
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004844 case BININT1:
4845 if (load_binint1(self) < 0)
4846 break;
4847 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004848
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004849 case BININT2:
4850 if (load_binint2(self) < 0)
4851 break;
4852 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004853
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004854 case INT:
4855 if (load_int(self) < 0)
4856 break;
4857 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004858
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004859 case LONG:
4860 if (load_long(self) < 0)
4861 break;
4862 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004863
Tim Peters4190fb82003-02-02 16:09:05 +00004864 case LONG1:
4865 if (load_counted_long(self, 1) < 0)
4866 break;
4867 continue;
4868
4869 case LONG4:
4870 if (load_counted_long(self, 4) < 0)
4871 break;
4872 continue;
4873
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004874 case FLOAT:
4875 if (load_float(self) < 0)
4876 break;
4877 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004878
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004879 case BINFLOAT:
4880 if (load_binfloat(self) < 0)
4881 break;
4882 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004883
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004884 case BINSTRING:
4885 if (load_binstring(self) < 0)
4886 break;
4887 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004888
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004889 case SHORT_BINSTRING:
4890 if (load_short_binstring(self) < 0)
4891 break;
4892 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004893
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004894 case STRING:
4895 if (load_string(self) < 0)
4896 break;
4897 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004898
Martin v. Löwis339d0f72001-08-17 18:39:25 +00004899#ifdef Py_USING_UNICODE
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004900 case UNICODE:
4901 if (load_unicode(self) < 0)
4902 break;
4903 continue;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00004904
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004905 case BINUNICODE:
4906 if (load_binunicode(self) < 0)
4907 break;
4908 continue;
Martin v. Löwis339d0f72001-08-17 18:39:25 +00004909#endif
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00004910
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004911 case EMPTY_TUPLE:
Tim Peters1d63c9f2003-02-02 20:29:39 +00004912 if (load_counted_tuple(self, 0) < 0)
4913 break;
4914 continue;
4915
4916 case TUPLE1:
4917 if (load_counted_tuple(self, 1) < 0)
4918 break;
4919 continue;
4920
4921 case TUPLE2:
4922 if (load_counted_tuple(self, 2) < 0)
4923 break;
4924 continue;
4925
4926 case TUPLE3:
4927 if (load_counted_tuple(self, 3) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004928 break;
4929 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004930
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004931 case TUPLE:
4932 if (load_tuple(self) < 0)
4933 break;
4934 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004935
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004936 case EMPTY_LIST:
4937 if (load_empty_list(self) < 0)
4938 break;
4939 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004940
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004941 case LIST:
4942 if (load_list(self) < 0)
4943 break;
4944 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004945
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004946 case EMPTY_DICT:
4947 if (load_empty_dict(self) < 0)
4948 break;
4949 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004950
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004951 case DICT:
4952 if (load_dict(self) < 0)
4953 break;
4954 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004955
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004956 case OBJ:
4957 if (noload_obj(self) < 0)
4958 break;
4959 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004960
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004961 case INST:
4962 if (noload_inst(self) < 0)
4963 break;
4964 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004965
Tim Peterseab7db32003-02-13 18:24:14 +00004966 case NEWOBJ:
4967 if (noload_newobj(self) < 0)
4968 break;
4969 continue;
4970
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004971 case GLOBAL:
4972 if (noload_global(self) < 0)
4973 break;
4974 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004975
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004976 case APPEND:
4977 if (load_append(self) < 0)
4978 break;
4979 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004980
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004981 case APPENDS:
4982 if (load_appends(self) < 0)
4983 break;
4984 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00004985
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004986 case BUILD:
4987 if (noload_build(self) < 0)
4988 break;
4989 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00004990
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004991 case DUP:
4992 if (load_dup(self) < 0)
4993 break;
4994 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004995
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004996 case BINGET:
4997 if (load_binget(self) < 0)
4998 break;
4999 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005000
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005001 case LONG_BINGET:
5002 if (load_long_binget(self) < 0)
5003 break;
5004 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00005005
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005006 case GET:
5007 if (load_get(self) < 0)
5008 break;
5009 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005010
Tim Peters2d629652003-02-04 05:06:17 +00005011 case EXT1:
5012 if (noload_extension(self, 1) < 0)
5013 break;
5014 continue;
5015
5016 case EXT2:
5017 if (noload_extension(self, 2) < 0)
5018 break;
5019 continue;
5020
5021 case EXT4:
5022 if (noload_extension(self, 4) < 0)
5023 break;
5024 continue;
5025
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005026 case MARK:
5027 if (load_mark(self) < 0)
5028 break;
5029 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005030
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005031 case BINPUT:
5032 if (load_binput(self) < 0)
5033 break;
5034 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005035
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005036 case LONG_BINPUT:
5037 if (load_long_binput(self) < 0)
5038 break;
5039 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00005040
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005041 case PUT:
5042 if (load_put(self) < 0)
5043 break;
5044 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005045
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005046 case POP:
5047 if (load_pop(self) < 0)
5048 break;
5049 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005050
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005051 case POP_MARK:
5052 if (load_pop_mark(self) < 0)
5053 break;
5054 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005055
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005056 case SETITEM:
5057 if (load_setitem(self) < 0)
5058 break;
5059 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005060
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005061 case SETITEMS:
5062 if (load_setitems(self) < 0)
5063 break;
5064 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005065
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005066 case STOP:
5067 break;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005068
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005069 case PERSID:
5070 if (load_persid(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 BINPERSID:
5075 if (load_binpersid(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 REDUCE:
5080 if (noload_reduce(self) < 0)
5081 break;
5082 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005083
Tim Peters4190fb82003-02-02 16:09:05 +00005084 case PROTO:
5085 if (load_proto(self) < 0)
5086 break;
5087 continue;
5088
Tim Peters3c67d792003-02-02 17:59:11 +00005089 case NEWTRUE:
5090 if (load_bool(self, Py_True) < 0)
5091 break;
5092 continue;
5093
5094 case NEWFALSE:
5095 if (load_bool(self, Py_False) < 0)
5096 break;
5097 continue;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005098 default:
Tim Peterscba30e22003-02-01 06:24:36 +00005099 cPickle_ErrFormat(UnpicklingError,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005100 "invalid load key, '%s'.",
5101 "c", s[0]);
5102 return NULL;
5103 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005104
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005105 break;
5106 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005107
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005108 if ((err = PyErr_Occurred())) {
5109 if (err == PyExc_EOFError) {
5110 PyErr_SetNone(PyExc_EOFError);
5111 }
5112 return NULL;
5113 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005114
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005115 PDATA_POP(self->stack, val);
5116 return val;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005117}
Tim Peters84e87f32001-03-17 04:50:51 +00005118
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005119
Guido van Rossum60456fd1997-04-09 17:36:32 +00005120static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00005121Unpickler_load(Unpicklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005122{
Tim Peterscba30e22003-02-01 06:24:36 +00005123 if (!( PyArg_ParseTuple(args, ":load")))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005124 return NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005125
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005126 return load(self);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005127}
5128
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005129static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00005130Unpickler_noload(Unpicklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005131{
Tim Peterscba30e22003-02-01 06:24:36 +00005132 if (!( PyArg_ParseTuple(args, ":noload")))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005133 return NULL;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005134
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005135 return noload(self);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005136}
5137
Guido van Rossum60456fd1997-04-09 17:36:32 +00005138
5139static struct PyMethodDef Unpickler_methods[] = {
Neal Norwitzb0493252002-03-31 14:44:22 +00005140 {"load", (PyCFunction)Unpickler_load, METH_VARARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00005141 PyDoc_STR("load() -- Load a pickle")
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005142 },
Neal Norwitzb0493252002-03-31 14:44:22 +00005143 {"noload", (PyCFunction)Unpickler_noload, METH_VARARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00005144 PyDoc_STR(
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005145 "noload() -- not load a pickle, but go through most of the motions\n"
5146 "\n"
5147 "This function can be used to read past a pickle without instantiating\n"
5148 "any objects or importing any modules. It can also be used to find all\n"
5149 "persistent references without instantiating any objects or importing\n"
Neal Norwitz200788c2002-08-13 22:20:41 +00005150 "any modules.\n")
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005151 },
Guido van Rossum60456fd1997-04-09 17:36:32 +00005152 {NULL, NULL} /* sentinel */
5153};
5154
5155
5156static Unpicklerobject *
Tim Peterscba30e22003-02-01 06:24:36 +00005157newUnpicklerobject(PyObject *f)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005158{
5159 Unpicklerobject *self;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005160
Jeremy Hylton7b5ce7f2003-04-09 21:25:30 +00005161 if (!( self = PyObject_GC_New(Unpicklerobject, &Unpicklertype)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005162 return NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005163
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005164 self->file = NULL;
5165 self->arg = NULL;
5166 self->stack = (Pdata*)Pdata_New();
5167 self->pers_func = NULL;
5168 self->last_string = NULL;
5169 self->marks = NULL;
5170 self->num_marks = 0;
5171 self->marks_size = 0;
5172 self->buf_size = 0;
5173 self->read = NULL;
5174 self->readline = NULL;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005175 self->find_class = NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005176
Tim Peterscba30e22003-02-01 06:24:36 +00005177 if (!( self->memo = PyDict_New()))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005178 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005179
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005180 Py_INCREF(f);
5181 self->file = f;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005182
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005183 /* Set read, readline based on type of f */
5184 if (PyFile_Check(f)) {
5185 self->fp = PyFile_AsFile(f);
5186 if (self->fp == NULL) {
Tim Peterscba30e22003-02-01 06:24:36 +00005187 PyErr_SetString(PyExc_ValueError,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005188 "I/O operation on closed file");
5189 goto err;
5190 }
5191 self->read_func = read_file;
5192 self->readline_func = readline_file;
Guido van Rossumc91fcaa1999-03-29 20:00:14 +00005193 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005194 else if (PycStringIO_InputCheck(f)) {
5195 self->fp = NULL;
5196 self->read_func = read_cStringIO;
5197 self->readline_func = readline_cStringIO;
5198 }
5199 else {
Guido van Rossum60456fd1997-04-09 17:36:32 +00005200
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005201 self->fp = NULL;
5202 self->read_func = read_other;
5203 self->readline_func = readline_other;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005204
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005205 if (!( (self->readline = PyObject_GetAttr(f, readline_str)) &&
5206 (self->read = PyObject_GetAttr(f, read_str)))) {
5207 PyErr_Clear();
Tim Peterscba30e22003-02-01 06:24:36 +00005208 PyErr_SetString( PyExc_TypeError,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005209 "argument must have 'read' and "
5210 "'readline' attributes" );
5211 goto err;
5212 }
5213 }
Jeremy Hylton7b5ce7f2003-04-09 21:25:30 +00005214 PyObject_GC_Track(self);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005215
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005216 return self;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005217
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005218 err:
5219 Py_DECREF((PyObject *)self);
5220 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005221}
5222
5223
5224static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00005225get_Unpickler(PyObject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005226{
5227 PyObject *file;
Tim Peters84e87f32001-03-17 04:50:51 +00005228
Tim Peterscba30e22003-02-01 06:24:36 +00005229 if (!( PyArg_ParseTuple(args, "O:Unpickler", &file)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005230 return NULL;
5231 return (PyObject *)newUnpicklerobject(file);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005232}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005233
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005234
Guido van Rossum60456fd1997-04-09 17:36:32 +00005235static void
Tim Peterscba30e22003-02-01 06:24:36 +00005236Unpickler_dealloc(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005237{
Jeremy Hylton7b5ce7f2003-04-09 21:25:30 +00005238 PyObject_GC_UnTrack((PyObject *)self);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005239 Py_XDECREF(self->readline);
5240 Py_XDECREF(self->read);
5241 Py_XDECREF(self->file);
5242 Py_XDECREF(self->memo);
5243 Py_XDECREF(self->stack);
5244 Py_XDECREF(self->pers_func);
5245 Py_XDECREF(self->arg);
5246 Py_XDECREF(self->last_string);
Jeremy Hyltonfff093f2003-07-11 19:42:49 +00005247 Py_XDECREF(self->find_class);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005248
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005249 if (self->marks) {
5250 free(self->marks);
5251 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005252
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005253 if (self->buf_size) {
5254 free(self->buf);
5255 }
Tim Peters84e87f32001-03-17 04:50:51 +00005256
Tim Peters3cfe7542003-05-21 21:29:48 +00005257 self->ob_type->tp_free((PyObject *)self);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005258}
5259
Jeremy Hylton7b5ce7f2003-04-09 21:25:30 +00005260static int
5261Unpickler_traverse(Unpicklerobject *self, visitproc visit, void *arg)
5262{
5263 int err;
5264
5265#define VISIT(SLOT) \
5266 if (SLOT) { \
5267 err = visit((PyObject *)(SLOT), arg); \
5268 if (err) \
5269 return err; \
5270 }
5271 VISIT(self->readline);
5272 VISIT(self->read);
5273 VISIT(self->file);
5274 VISIT(self->memo);
5275 VISIT(self->stack);
5276 VISIT(self->pers_func);
5277 VISIT(self->arg);
5278 VISIT(self->last_string);
Jeremy Hyltonfff093f2003-07-11 19:42:49 +00005279 VISIT(self->find_class);
Jeremy Hylton7b5ce7f2003-04-09 21:25:30 +00005280#undef VISIT
5281 return 0;
5282}
5283
5284static int
5285Unpickler_clear(Unpicklerobject *self)
5286{
5287#define CLEAR(SLOT) Py_XDECREF(SLOT); SLOT = NULL
5288 CLEAR(self->readline);
5289 CLEAR(self->read);
5290 CLEAR(self->file);
5291 CLEAR(self->memo);
5292 CLEAR(self->stack);
5293 CLEAR(self->pers_func);
5294 CLEAR(self->arg);
5295 CLEAR(self->last_string);
Jeremy Hyltonfff093f2003-07-11 19:42:49 +00005296 CLEAR(self->find_class);
Jeremy Hylton7b5ce7f2003-04-09 21:25:30 +00005297#undef CLEAR
5298 return 0;
5299}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005300
5301static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00005302Unpickler_getattr(Unpicklerobject *self, char *name)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005303{
5304 if (!strcmp(name, "persistent_load")) {
5305 if (!self->pers_func) {
5306 PyErr_SetString(PyExc_AttributeError, name);
5307 return NULL;
5308 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005309
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005310 Py_INCREF(self->pers_func);
5311 return self->pers_func;
5312 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005313
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005314 if (!strcmp(name, "find_global")) {
5315 if (!self->find_class) {
5316 PyErr_SetString(PyExc_AttributeError, name);
5317 return NULL;
5318 }
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00005319
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005320 Py_INCREF(self->find_class);
5321 return self->find_class;
5322 }
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00005323
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005324 if (!strcmp(name, "memo")) {
5325 if (!self->memo) {
5326 PyErr_SetString(PyExc_AttributeError, name);
5327 return NULL;
5328 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005329
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005330 Py_INCREF(self->memo);
5331 return self->memo;
5332 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005333
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005334 if (!strcmp(name, "UnpicklingError")) {
5335 Py_INCREF(UnpicklingError);
5336 return UnpicklingError;
5337 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005338
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005339 return Py_FindMethod(Unpickler_methods, (PyObject *)self, name);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005340}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005341
Guido van Rossum60456fd1997-04-09 17:36:32 +00005342
5343static int
Tim Peterscba30e22003-02-01 06:24:36 +00005344Unpickler_setattr(Unpicklerobject *self, char *name, PyObject *value)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005345{
Jeremy Hyltonce616e41998-08-13 23:13:52 +00005346
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005347 if (!strcmp(name, "persistent_load")) {
5348 Py_XDECREF(self->pers_func);
5349 self->pers_func = value;
5350 Py_XINCREF(value);
5351 return 0;
5352 }
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00005353
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005354 if (!strcmp(name, "find_global")) {
5355 Py_XDECREF(self->find_class);
5356 self->find_class = value;
5357 Py_XINCREF(value);
5358 return 0;
5359 }
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00005360
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005361 if (! value) {
5362 PyErr_SetString(PyExc_TypeError,
5363 "attribute deletion is not supported");
5364 return -1;
5365 }
Jeremy Hyltonce616e41998-08-13 23:13:52 +00005366
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005367 if (strcmp(name, "memo") == 0) {
5368 if (!PyDict_Check(value)) {
Tim Peterscba30e22003-02-01 06:24:36 +00005369 PyErr_SetString(PyExc_TypeError,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005370 "memo must be a dictionary");
5371 return -1;
5372 }
5373 Py_XDECREF(self->memo);
5374 self->memo = value;
5375 Py_INCREF(value);
5376 return 0;
5377 }
Jeremy Hyltonce616e41998-08-13 23:13:52 +00005378
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005379 PyErr_SetString(PyExc_AttributeError, name);
5380 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005381}
5382
Tim Peters5bd2a792003-02-01 16:45:06 +00005383/* ---------------------------------------------------------------------------
5384 * Module-level functions.
5385 */
Guido van Rossum60456fd1997-04-09 17:36:32 +00005386
Martin v. Löwis544f1192004-07-27 05:22:33 +00005387/* dump(obj, file, protocol=0). */
Guido van Rossum60456fd1997-04-09 17:36:32 +00005388static PyObject *
Martin v. Löwis544f1192004-07-27 05:22:33 +00005389cpm_dump(PyObject *self, PyObject *args, PyObject *kwds)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005390{
Martin v. Löwis15e62742006-02-27 16:46:16 +00005391 static char *kwlist[] = {"obj", "file", "protocol", NULL};
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005392 PyObject *ob, *file, *res = NULL;
5393 Picklerobject *pickler = 0;
Tim Peters5bd2a792003-02-01 16:45:06 +00005394 int proto = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005395
Martin v. Löwis544f1192004-07-27 05:22:33 +00005396 if (!( PyArg_ParseTupleAndKeywords(args, kwds, "OO|i", kwlist,
5397 &ob, &file, &proto)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005398 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005399
Tim Peters5bd2a792003-02-01 16:45:06 +00005400 if (!( pickler = newPicklerobject(file, proto)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005401 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005402
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005403 if (dump(pickler, ob) < 0)
5404 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005405
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005406 Py_INCREF(Py_None);
5407 res = Py_None;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005408
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005409 finally:
5410 Py_XDECREF(pickler);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005411
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005412 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005413}
5414
5415
Martin v. Löwis544f1192004-07-27 05:22:33 +00005416/* dumps(obj, protocol=0). */
Guido van Rossum60456fd1997-04-09 17:36:32 +00005417static PyObject *
Martin v. Löwis544f1192004-07-27 05:22:33 +00005418cpm_dumps(PyObject *self, PyObject *args, PyObject *kwds)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005419{
Martin v. Löwis15e62742006-02-27 16:46:16 +00005420 static char *kwlist[] = {"obj", "protocol", NULL};
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005421 PyObject *ob, *file = 0, *res = NULL;
5422 Picklerobject *pickler = 0;
Tim Peters5bd2a792003-02-01 16:45:06 +00005423 int proto = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005424
Martin v. Löwis544f1192004-07-27 05:22:33 +00005425 if (!( PyArg_ParseTupleAndKeywords(args, kwds, "O|i:dumps", kwlist,
5426 &ob, &proto)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005427 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005428
Tim Peterscba30e22003-02-01 06:24:36 +00005429 if (!( file = PycStringIO->NewOutput(128)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005430 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005431
Tim Peters5bd2a792003-02-01 16:45:06 +00005432 if (!( pickler = newPicklerobject(file, proto)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005433 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005434
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005435 if (dump(pickler, ob) < 0)
5436 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005437
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005438 res = PycStringIO->cgetvalue(file);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005439
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005440 finally:
5441 Py_XDECREF(pickler);
5442 Py_XDECREF(file);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005443
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005444 return res;
Tim Peters84e87f32001-03-17 04:50:51 +00005445}
5446
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005447
Tim Peters5bd2a792003-02-01 16:45:06 +00005448/* load(fileobj). */
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005449static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00005450cpm_load(PyObject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005451{
5452 Unpicklerobject *unpickler = 0;
5453 PyObject *ob, *res = NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005454
Tim Peterscba30e22003-02-01 06:24:36 +00005455 if (!( PyArg_ParseTuple(args, "O:load", &ob)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005456 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005457
Tim Peterscba30e22003-02-01 06:24:36 +00005458 if (!( unpickler = newUnpicklerobject(ob)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005459 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005460
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005461 res = load(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005462
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005463 finally:
5464 Py_XDECREF(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005465
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005466 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005467}
5468
5469
Tim Peters5bd2a792003-02-01 16:45:06 +00005470/* loads(string) */
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005471static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00005472cpm_loads(PyObject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005473{
5474 PyObject *ob, *file = 0, *res = NULL;
5475 Unpicklerobject *unpickler = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005476
Tim Peterscba30e22003-02-01 06:24:36 +00005477 if (!( PyArg_ParseTuple(args, "S:loads", &ob)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005478 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005479
Tim Peterscba30e22003-02-01 06:24:36 +00005480 if (!( file = PycStringIO->NewInput(ob)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005481 goto finally;
Tim Peters84e87f32001-03-17 04:50:51 +00005482
Tim Peterscba30e22003-02-01 06:24:36 +00005483 if (!( unpickler = newUnpicklerobject(file)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005484 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005485
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005486 res = load(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005487
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005488 finally:
5489 Py_XDECREF(file);
5490 Py_XDECREF(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005491
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005492 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005493}
5494
5495
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005496PyDoc_STRVAR(Unpicklertype__doc__,
5497"Objects that know how to unpickle");
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005498
Guido van Rossum9716aaa1997-12-08 15:15:16 +00005499static PyTypeObject Unpicklertype = {
5500 PyObject_HEAD_INIT(NULL)
Jeremy Hylton7b5ce7f2003-04-09 21:25:30 +00005501 0, /*ob_size*/
5502 "cPickle.Unpickler", /*tp_name*/
5503 sizeof(Unpicklerobject), /*tp_basicsize*/
5504 0,
5505 (destructor)Unpickler_dealloc, /* tp_dealloc */
5506 0, /* tp_print */
5507 (getattrfunc)Unpickler_getattr, /* tp_getattr */
5508 (setattrfunc)Unpickler_setattr, /* tp_setattr */
5509 0, /* tp_compare */
5510 0, /* tp_repr */
5511 0, /* tp_as_number */
5512 0, /* tp_as_sequence */
5513 0, /* tp_as_mapping */
5514 0, /* tp_hash */
5515 0, /* tp_call */
5516 0, /* tp_str */
5517 0, /* tp_getattro */
5518 0, /* tp_setattro */
5519 0, /* tp_as_buffer */
5520 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
5521 Unpicklertype__doc__, /* tp_doc */
5522 (traverseproc)Unpickler_traverse, /* tp_traverse */
5523 (inquiry)Unpickler_clear, /* tp_clear */
Guido van Rossum9716aaa1997-12-08 15:15:16 +00005524};
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005525
Guido van Rossum60456fd1997-04-09 17:36:32 +00005526static struct PyMethodDef cPickle_methods[] = {
Martin v. Löwis544f1192004-07-27 05:22:33 +00005527 {"dump", (PyCFunction)cpm_dump, METH_VARARGS | METH_KEYWORDS,
5528 PyDoc_STR("dump(obj, file, protocol=0) -- "
Tim Peters5bd2a792003-02-01 16:45:06 +00005529 "Write an object in pickle format to the given file.\n"
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005530 "\n"
Tim Peters5bd2a792003-02-01 16:45:06 +00005531 "See the Pickler docstring for the meaning of optional argument proto.")
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005532 },
Tim Peters5bd2a792003-02-01 16:45:06 +00005533
Martin v. Löwis544f1192004-07-27 05:22:33 +00005534 {"dumps", (PyCFunction)cpm_dumps, METH_VARARGS | METH_KEYWORDS,
5535 PyDoc_STR("dumps(obj, protocol=0) -- "
Tim Peters5bd2a792003-02-01 16:45:06 +00005536 "Return a string containing an object in pickle format.\n"
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005537 "\n"
Tim Peters5bd2a792003-02-01 16:45:06 +00005538 "See the Pickler docstring for the meaning of optional argument proto.")
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005539 },
Tim Peters5bd2a792003-02-01 16:45:06 +00005540
Neal Norwitzb0493252002-03-31 14:44:22 +00005541 {"load", (PyCFunction)cpm_load, METH_VARARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00005542 PyDoc_STR("load(file) -- Load a pickle from the given file")},
Tim Peters5bd2a792003-02-01 16:45:06 +00005543
Neal Norwitzb0493252002-03-31 14:44:22 +00005544 {"loads", (PyCFunction)cpm_loads, METH_VARARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00005545 PyDoc_STR("loads(string) -- Load a pickle from the given string")},
Tim Peters5bd2a792003-02-01 16:45:06 +00005546
Martin v. Löwis544f1192004-07-27 05:22:33 +00005547 {"Pickler", (PyCFunction)get_Pickler, METH_VARARGS | METH_KEYWORDS,
5548 PyDoc_STR("Pickler(file, protocol=0) -- Create a pickler.\n"
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005549 "\n"
Tim Peters5bd2a792003-02-01 16:45:06 +00005550 "This takes a file-like object for writing a pickle data stream.\n"
5551 "The optional proto argument tells the pickler to use the given\n"
5552 "protocol; supported protocols are 0, 1, 2. The default\n"
5553 "protocol is 0, to be backwards compatible. (Protocol 0 is the\n"
5554 "only protocol that can be written to a file opened in text\n"
5555 "mode and read back successfully. When using a protocol higher\n"
5556 "than 0, make sure the file is opened in binary mode, both when\n"
5557 "pickling and unpickling.)\n"
5558 "\n"
5559 "Protocol 1 is more efficient than protocol 0; protocol 2 is\n"
5560 "more efficient than protocol 1.\n"
5561 "\n"
5562 "Specifying a negative protocol version selects the highest\n"
5563 "protocol version supported. The higher the protocol used, the\n"
5564 "more recent the version of Python needed to read the pickle\n"
5565 "produced.\n"
5566 "\n"
5567 "The file parameter must have a write() method that accepts a single\n"
5568 "string argument. It can thus be an open file object, a StringIO\n"
5569 "object, or any other custom object that meets this interface.\n")
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005570 },
Tim Peters5bd2a792003-02-01 16:45:06 +00005571
Neal Norwitzb0493252002-03-31 14:44:22 +00005572 {"Unpickler", (PyCFunction)get_Unpickler, METH_VARARGS,
Tim Peters5bd2a792003-02-01 16:45:06 +00005573 PyDoc_STR("Unpickler(file) -- Create an unpickler.")},
5574
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005575 { NULL, NULL }
5576};
5577
Guido van Rossum60456fd1997-04-09 17:36:32 +00005578static int
Tim Peterscba30e22003-02-01 06:24:36 +00005579init_stuff(PyObject *module_dict)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005580{
5581 PyObject *copy_reg, *t, *r;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005582
Martin v. Löwis43c9d8a2002-04-01 12:34:33 +00005583#define INIT_STR(S) if (!( S ## _str=PyString_InternFromString(#S))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005584
Tim Peters3cfe7542003-05-21 21:29:48 +00005585 if (PyType_Ready(&Unpicklertype) < 0)
5586 return -1;
5587 if (PyType_Ready(&Picklertype) < 0)
5588 return -1;
5589
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005590 INIT_STR(__class__);
5591 INIT_STR(__getinitargs__);
5592 INIT_STR(__dict__);
5593 INIT_STR(__getstate__);
5594 INIT_STR(__setstate__);
5595 INIT_STR(__name__);
5596 INIT_STR(__main__);
5597 INIT_STR(__reduce__);
Guido van Rossumb289b872003-02-19 01:45:13 +00005598 INIT_STR(__reduce_ex__);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005599 INIT_STR(write);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005600 INIT_STR(append);
5601 INIT_STR(read);
5602 INIT_STR(readline);
5603 INIT_STR(copy_reg);
5604 INIT_STR(dispatch_table);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005605 INIT_STR(__basicnew__);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005606
Tim Peterscba30e22003-02-01 06:24:36 +00005607 if (!( copy_reg = PyImport_ImportModule("copy_reg")))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005608 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005609
Tim Peters1f1b2d22003-02-01 02:16:37 +00005610 /* This is special because we want to use a different
5611 one in restricted mode. */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005612 dispatch_table = PyObject_GetAttr(copy_reg, dispatch_table_str);
Tim Peters5b7da392003-02-04 00:21:07 +00005613 if (!dispatch_table) return -1;
5614
5615 extension_registry = PyObject_GetAttrString(copy_reg,
Guido van Rossumd4b920c2003-02-04 01:54:49 +00005616 "_extension_registry");
Tim Peters5b7da392003-02-04 00:21:07 +00005617 if (!extension_registry) return -1;
5618
5619 inverted_registry = PyObject_GetAttrString(copy_reg,
Guido van Rossumd4b920c2003-02-04 01:54:49 +00005620 "_inverted_registry");
Tim Peters5b7da392003-02-04 00:21:07 +00005621 if (!inverted_registry) return -1;
5622
5623 extension_cache = PyObject_GetAttrString(copy_reg,
Guido van Rossumd4b920c2003-02-04 01:54:49 +00005624 "_extension_cache");
Tim Peters5b7da392003-02-04 00:21:07 +00005625 if (!extension_cache) return -1;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005626
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005627 Py_DECREF(copy_reg);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005628
Tim Peters731098b2003-02-04 20:56:09 +00005629 if (!(empty_tuple = PyTuple_New(0)))
5630 return -1;
5631
5632 two_tuple = PyTuple_New(2);
5633 if (two_tuple == NULL)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005634 return -1;
Tim Peters3e667d52003-02-04 21:47:44 +00005635 /* We use this temp container with no regard to refcounts, or to
5636 * keeping containees alive. Exempt from GC, because we don't
5637 * want anything looking at two_tuple() by magic.
5638 */
5639 PyObject_GC_UnTrack(two_tuple);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005640
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005641 /* Ugh */
5642 if (!( t=PyImport_ImportModule("__builtin__"))) return -1;
5643 if (PyDict_SetItemString(module_dict, "__builtins__", t) < 0)
5644 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005645
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005646 if (!( t=PyDict_New())) return -1;
5647 if (!( r=PyRun_String(
5648 "def __init__(self, *args): self.args=args\n\n"
5649 "def __str__(self):\n"
5650 " return self.args and ('%s' % self.args[0]) or '(what)'\n",
5651 Py_file_input,
5652 module_dict, t) )) return -1;
5653 Py_DECREF(r);
Guido van Rossumc03158b1999-06-09 15:23:31 +00005654
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005655 PickleError = PyErr_NewException("cPickle.PickleError", NULL, t);
Tim Peterscba30e22003-02-01 06:24:36 +00005656 if (!PickleError)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005657 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00005658
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005659 Py_DECREF(t);
Guido van Rossumc03158b1999-06-09 15:23:31 +00005660
Tim Peterscba30e22003-02-01 06:24:36 +00005661 PicklingError = PyErr_NewException("cPickle.PicklingError",
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005662 PickleError, NULL);
Tim Peterscba30e22003-02-01 06:24:36 +00005663 if (!PicklingError)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005664 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00005665
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005666 if (!( t=PyDict_New())) return -1;
5667 if (!( r=PyRun_String(
5668 "def __init__(self, *args): self.args=args\n\n"
5669 "def __str__(self):\n"
5670 " a=self.args\n"
5671 " a=a and type(a[0]) or '(what)'\n"
5672 " return 'Cannot pickle %s objects' % a\n"
5673 , Py_file_input,
5674 module_dict, t) )) return -1;
5675 Py_DECREF(r);
Tim Peters84e87f32001-03-17 04:50:51 +00005676
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005677 if (!( UnpickleableError = PyErr_NewException(
Tim Peterscba30e22003-02-01 06:24:36 +00005678 "cPickle.UnpickleableError", PicklingError, t)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005679 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00005680
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005681 Py_DECREF(t);
Guido van Rossumc03158b1999-06-09 15:23:31 +00005682
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005683 if (!( UnpicklingError = PyErr_NewException("cPickle.UnpicklingError",
Tim Peterscba30e22003-02-01 06:24:36 +00005684 PickleError, NULL)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005685 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00005686
Martin v. Löwis658009a2002-09-16 17:26:24 +00005687 if (!( BadPickleGet = PyErr_NewException("cPickle.BadPickleGet",
5688 UnpicklingError, NULL)))
5689 return -1;
Tim Peterscba30e22003-02-01 06:24:36 +00005690
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005691 if (PyDict_SetItemString(module_dict, "PickleError",
5692 PickleError) < 0)
5693 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00005694
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005695 if (PyDict_SetItemString(module_dict, "PicklingError",
5696 PicklingError) < 0)
5697 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00005698
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005699 if (PyDict_SetItemString(module_dict, "UnpicklingError",
5700 UnpicklingError) < 0)
5701 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005702
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005703 if (PyDict_SetItemString(module_dict, "UnpickleableError",
5704 UnpickleableError) < 0)
5705 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005706
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005707 if (PyDict_SetItemString(module_dict, "BadPickleGet",
5708 BadPickleGet) < 0)
5709 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00005710
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005711 PycString_IMPORT;
Guido van Rossum053b8df1998-11-25 16:18:00 +00005712
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005713 return 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005714}
5715
Mark Hammondfe51c6d2002-08-02 02:27:13 +00005716#ifndef PyMODINIT_FUNC /* declarations for DLL import/export */
5717#define PyMODINIT_FUNC void
Guido van Rossumf9ffb031999-02-04 14:54:04 +00005718#endif
Mark Hammondfe51c6d2002-08-02 02:27:13 +00005719PyMODINIT_FUNC
Tim Peterscba30e22003-02-01 06:24:36 +00005720initcPickle(void)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005721{
5722 PyObject *m, *d, *di, *v, *k;
Martin v. Löwis18e16552006-02-15 17:27:45 +00005723 Py_ssize_t i;
Tim Peters5b7da392003-02-04 00:21:07 +00005724 char *rev = "1.71"; /* XXX when does this change? */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005725 PyObject *format_version;
5726 PyObject *compatible_formats;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005727
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005728 Picklertype.ob_type = &PyType_Type;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005729 Unpicklertype.ob_type = &PyType_Type;
5730 PdataType.ob_type = &PyType_Type;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005731
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005732 /* Initialize some pieces. We need to do this before module creation,
Tim Peters5b7da392003-02-04 00:21:07 +00005733 * so we're forced to use a temporary dictionary. :(
5734 */
5735 di = PyDict_New();
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005736 if (!di) return;
5737 if (init_stuff(di) < 0) return;
Guido van Rossumebba4202000-09-07 14:35:37 +00005738
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005739 /* Create the module and add the functions */
5740 m = Py_InitModule4("cPickle", cPickle_methods,
5741 cPickle_module_documentation,
5742 (PyObject*)NULL,PYTHON_API_VERSION);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00005743 if (m == NULL)
5744 return;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005745
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005746 /* Add some symbolic constants to the module */
5747 d = PyModule_GetDict(m);
Tim Peters5b7da392003-02-04 00:21:07 +00005748 v = PyString_FromString(rev);
5749 PyDict_SetItemString(d, "__version__", v);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005750 Py_XDECREF(v);
Barry Warsaw93d29b61997-01-14 17:45:08 +00005751
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005752 /* Copy data from di. Waaa. */
5753 for (i=0; PyDict_Next(di, &i, &k, &v); ) {
5754 if (PyObject_SetItem(d, k, v) < 0) {
5755 Py_DECREF(di);
5756 return;
5757 }
5758 }
5759 Py_DECREF(di);
Guido van Rossumebba4202000-09-07 14:35:37 +00005760
Tim Peters8587b3c2003-02-13 15:44:41 +00005761 i = PyModule_AddIntConstant(m, "HIGHEST_PROTOCOL", HIGHEST_PROTOCOL);
5762 if (i < 0)
5763 return;
5764
Tim Peters5b7da392003-02-04 00:21:07 +00005765 /* These are purely informational; no code uses them. */
5766 /* File format version we write. */
5767 format_version = PyString_FromString("2.0");
5768 /* Format versions we can read. */
5769 compatible_formats = Py_BuildValue("[sssss]",
5770 "1.0", /* Original protocol 0 */
5771 "1.1", /* Protocol 0 + INST */
5772 "1.2", /* Original protocol 1 */
5773 "1.3", /* Protocol 1 + BINFLOAT */
Tim Peters0dd23aa2003-02-04 00:30:46 +00005774 "2.0"); /* Original protocol 2 */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005775 PyDict_SetItemString(d, "format_version", format_version);
5776 PyDict_SetItemString(d, "compatible_formats", compatible_formats);
5777 Py_XDECREF(format_version);
5778 Py_XDECREF(compatible_formats);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005779}