blob: 85bcc6cdcf80abd387807c138d838ea6b8eb660c [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öwis2f6ef4c2002-04-01 17:40:08 +0000342 int (*write_func)(struct Picklerobject *, char *, int);
343 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;
371 int (*read_func)(struct Unpicklerobject *, char **, int);
372 int (*readline_func)(struct Unpicklerobject *, char **);
373 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
Tim Peterscba30e22003-02-01 06:24:36 +0000420write_file(Picklerobject *self, char *s, int 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öwis2f6ef4c2002-04-01 17:40:08 +0000428 Py_BEGIN_ALLOW_THREADS
Jeremy Hylton7fa4bfa2002-07-18 20:58:57 +0000429 nbyteswritten = fwrite(s, sizeof(char), n, self->fp);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000430 Py_END_ALLOW_THREADS
Jeremy Hylton7fa4bfa2002-07-18 20:58:57 +0000431 if (nbyteswritten != (size_t)n) {
432 PyErr_SetFromErrno(PyExc_IOError);
433 return -1;
434 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000435
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000436 return n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000437}
438
Tim Peters84e87f32001-03-17 04:50:51 +0000439static int
Tim Peterscba30e22003-02-01 06:24:36 +0000440write_cStringIO(Picklerobject *self, char *s, int n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000441{
442 if (s == NULL) {
443 return 0;
444 }
Tim Peterscba30e22003-02-01 06:24:36 +0000445
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000446 if (PycStringIO->cwrite((PyObject *)self->file, s, n) != n) {
447 return -1;
448 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000449
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000450 return n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000451}
452
Tim Peters84e87f32001-03-17 04:50:51 +0000453static int
Tim Peterscba30e22003-02-01 06:24:36 +0000454write_none(Picklerobject *self, char *s, int n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000455{
456 if (s == NULL) return 0;
457 return n;
Guido van Rossum142eeb81997-08-13 03:14:41 +0000458}
459
Tim Peters84e87f32001-03-17 04:50:51 +0000460static int
Tim Peterscba30e22003-02-01 06:24:36 +0000461write_other(Picklerobject *self, char *s, int n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000462{
463 PyObject *py_str = 0, *junk = 0;
Tim Peterscba30e22003-02-01 06:24:36 +0000464
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000465 if (s == NULL) {
466 if (!( self->buf_size )) return 0;
Tim Peterscba30e22003-02-01 06:24:36 +0000467 py_str = PyString_FromStringAndSize(self->write_buf,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000468 self->buf_size);
Tim Peterscba30e22003-02-01 06:24:36 +0000469 if (!py_str)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000470 return -1;
471 }
472 else {
473 if (self->buf_size && (n + self->buf_size) > WRITE_BUF_SIZE) {
474 if (write_other(self, NULL, 0) < 0)
475 return -1;
476 }
Tim Peterscba30e22003-02-01 06:24:36 +0000477
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000478 if (n > WRITE_BUF_SIZE) {
479 if (!( py_str =
Tim Peterscba30e22003-02-01 06:24:36 +0000480 PyString_FromStringAndSize(s, n)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000481 return -1;
482 }
483 else {
484 memcpy(self->write_buf + self->buf_size, s, n);
485 self->buf_size += n;
486 return n;
487 }
488 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000489
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000490 if (self->write) {
491 /* object with write method */
492 ARG_TUP(self, py_str);
493 if (self->arg) {
494 junk = PyObject_Call(self->write, self->arg, NULL);
495 FREE_ARG_TUP(self);
496 }
497 if (junk) Py_DECREF(junk);
498 else return -1;
499 }
500 else
501 PDATA_PUSH(self->file, py_str, -1);
Guido van Rossum60456fd1997-04-09 17:36:32 +0000502
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000503 self->buf_size = 0;
504 return n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000505}
506
507
Tim Peters84e87f32001-03-17 04:50:51 +0000508static int
Tim Petersee1a53c2003-02-02 02:57:53 +0000509read_file(Unpicklerobject *self, char **s, int n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000510{
511 size_t nbytesread;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000512
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000513 if (self->buf_size == 0) {
514 int size;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000515
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000516 size = ((n < 32) ? 32 : n);
Tim Petersee1a53c2003-02-02 02:57:53 +0000517 if (!( self->buf = (char *)malloc(size))) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000518 PyErr_NoMemory();
519 return -1;
520 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000521
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000522 self->buf_size = size;
523 }
524 else if (n > self->buf_size) {
Tim Petersee1a53c2003-02-02 02:57:53 +0000525 self->buf = (char *)realloc(self->buf, n);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000526 if (!self->buf) {
527 PyErr_NoMemory();
528 return -1;
529 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000530 self->buf_size = n;
531 }
Tim Peters84e87f32001-03-17 04:50:51 +0000532
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000533 Py_BEGIN_ALLOW_THREADS
534 nbytesread = fread(self->buf, sizeof(char), n, self->fp);
535 Py_END_ALLOW_THREADS
536 if (nbytesread != (size_t)n) {
537 if (feof(self->fp)) {
538 PyErr_SetNone(PyExc_EOFError);
539 return -1;
540 }
Tim Peterscba30e22003-02-01 06:24:36 +0000541
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000542 PyErr_SetFromErrno(PyExc_IOError);
543 return -1;
544 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000545
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000546 *s = self->buf;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000547
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000548 return n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000549}
550
551
Tim Peters84e87f32001-03-17 04:50:51 +0000552static int
Tim Peterscba30e22003-02-01 06:24:36 +0000553readline_file(Unpicklerobject *self, char **s)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000554{
555 int i;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000556
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000557 if (self->buf_size == 0) {
Tim Petersee1a53c2003-02-02 02:57:53 +0000558 if (!( self->buf = (char *)malloc(40))) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000559 PyErr_NoMemory();
560 return -1;
561 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000562 self->buf_size = 40;
563 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000564
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000565 i = 0;
566 while (1) {
Tim Petersee1a53c2003-02-02 02:57:53 +0000567 int bigger;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000568 for (; i < (self->buf_size - 1); i++) {
Tim Peterscba30e22003-02-01 06:24:36 +0000569 if (feof(self->fp) ||
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000570 (self->buf[i] = getc(self->fp)) == '\n') {
571 self->buf[i + 1] = '\0';
572 *s = self->buf;
573 return i + 1;
574 }
575 }
Tim Petersee1a53c2003-02-02 02:57:53 +0000576 bigger = self->buf_size << 1;
577 if (bigger <= 0) { /* overflow */
578 PyErr_NoMemory();
579 return -1;
580 }
581 self->buf = (char *)realloc(self->buf, bigger);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000582 if (!self->buf) {
583 PyErr_NoMemory();
584 return -1;
585 }
Tim Petersee1a53c2003-02-02 02:57:53 +0000586 self->buf_size = bigger;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000587 }
Tim Peters84e87f32001-03-17 04:50:51 +0000588}
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000589
590
Tim Peters84e87f32001-03-17 04:50:51 +0000591static int
Tim Peterscba30e22003-02-01 06:24:36 +0000592read_cStringIO(Unpicklerobject *self, char **s, int n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000593{
594 char *ptr;
Tim Peterscba30e22003-02-01 06:24:36 +0000595
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000596 if (PycStringIO->cread((PyObject *)self->file, &ptr, n) != n) {
597 PyErr_SetNone(PyExc_EOFError);
598 return -1;
599 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000600
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000601 *s = ptr;
Tim Peterscba30e22003-02-01 06:24:36 +0000602
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000603 return n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000604}
605
606
Tim Peters84e87f32001-03-17 04:50:51 +0000607static int
Tim Peterscba30e22003-02-01 06:24:36 +0000608readline_cStringIO(Unpicklerobject *self, char **s)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000609{
610 int n;
611 char *ptr;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000612
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000613 if ((n = PycStringIO->creadline((PyObject *)self->file, &ptr)) < 0) {
614 return -1;
615 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000616
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000617 *s = ptr;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000618
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000619 return n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000620}
621
622
Tim Peters84e87f32001-03-17 04:50:51 +0000623static int
Tim Peterscba30e22003-02-01 06:24:36 +0000624read_other(Unpicklerobject *self, char **s, int n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000625{
626 PyObject *bytes, *str=0;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000627
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000628 if (!( bytes = PyInt_FromLong(n))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000629
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000630 ARG_TUP(self, bytes);
631 if (self->arg) {
632 str = PyObject_Call(self->read, self->arg, NULL);
633 FREE_ARG_TUP(self);
634 }
635 if (! str) return -1;
Tim Peterscba30e22003-02-01 06:24:36 +0000636
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000637 Py_XDECREF(self->last_string);
638 self->last_string = str;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000639
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000640 if (! (*s = PyString_AsString(str))) return -1;
641 return n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000642}
643
644
Tim Peters84e87f32001-03-17 04:50:51 +0000645static int
Tim Peterscba30e22003-02-01 06:24:36 +0000646readline_other(Unpicklerobject *self, char **s)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000647{
648 PyObject *str;
649 int str_size;
Tim Peterscba30e22003-02-01 06:24:36 +0000650
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000651 if (!( str = PyObject_CallObject(self->readline, empty_tuple))) {
652 return -1;
653 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000654
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000655 if ((str_size = PyString_Size(str)) < 0)
656 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000657
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000658 Py_XDECREF(self->last_string);
659 self->last_string = str;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000660
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000661 if (! (*s = PyString_AsString(str)))
662 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000663
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000664 return str_size;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000665}
666
Tim Petersee1a53c2003-02-02 02:57:53 +0000667/* Copy the first n bytes from s into newly malloc'ed memory, plus a
668 * trailing 0 byte. Return a pointer to that, or NULL if out of memory.
669 * The caller is responsible for free()'ing the return value.
670 */
Guido van Rossum60456fd1997-04-09 17:36:32 +0000671static char *
Tim Petersee1a53c2003-02-02 02:57:53 +0000672pystrndup(char *s, int n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000673{
Tim Petersee1a53c2003-02-02 02:57:53 +0000674 char *r = (char *)malloc(n+1);
675 if (r == NULL)
676 return (char*)PyErr_NoMemory();
677 memcpy(r, s, n);
678 r[n] = 0;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000679 return r;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000680}
681
682
683static int
Tim Peterscba30e22003-02-01 06:24:36 +0000684get(Picklerobject *self, PyObject *id)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000685{
686 PyObject *value, *mv;
687 long c_value;
688 char s[30];
689 size_t len;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000690
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000691 if (!( mv = PyDict_GetItem(self->memo, id))) {
692 PyErr_SetObject(PyExc_KeyError, id);
693 return -1;
694 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000695
Tim Peterscba30e22003-02-01 06:24:36 +0000696 if (!( value = PyTuple_GetItem(mv, 0)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000697 return -1;
Tim Peters84e87f32001-03-17 04:50:51 +0000698
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000699 if (!( PyInt_Check(value))) {
700 PyErr_SetString(PicklingError, "no int where int expected in memo");
701 return -1;
702 }
703 c_value = PyInt_AS_LONG((PyIntObject*)value);
Guido van Rossum60456fd1997-04-09 17:36:32 +0000704
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000705 if (!self->bin) {
706 s[0] = GET;
707 PyOS_snprintf(s + 1, sizeof(s) - 1, "%ld\n", c_value);
708 len = strlen(s);
709 }
710 else if (Pdata_Check(self->file)) {
711 if (write_other(self, NULL, 0) < 0) return -1;
712 PDATA_APPEND(self->file, mv, -1);
713 return 0;
714 }
715 else {
716 if (c_value < 256) {
717 s[0] = BINGET;
718 s[1] = (int)(c_value & 0xff);
719 len = 2;
720 }
721 else {
722 s[0] = LONG_BINGET;
723 s[1] = (int)(c_value & 0xff);
724 s[2] = (int)((c_value >> 8) & 0xff);
725 s[3] = (int)((c_value >> 16) & 0xff);
726 s[4] = (int)((c_value >> 24) & 0xff);
727 len = 5;
728 }
729 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000730
Tim Peters0bc93f52003-02-02 18:29:33 +0000731 if (self->write_func(self, s, len) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000732 return -1;
Tim Peterscba30e22003-02-01 06:24:36 +0000733
Jeremy Hyltona0fb1772001-10-12 04:11:06 +0000734 return 0;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000735}
Jeremy Hyltonce616e41998-08-13 23:13:52 +0000736
Guido van Rossum60456fd1997-04-09 17:36:32 +0000737
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000738static int
Tim Peterscba30e22003-02-01 06:24:36 +0000739put(Picklerobject *self, PyObject *ob)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000740{
Tim Peterscba30e22003-02-01 06:24:36 +0000741 if (ob->ob_refcnt < 2 || self->fast)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000742 return 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000743
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000744 return put2(self, ob);
745}
Guido van Rossum053b8df1998-11-25 16:18:00 +0000746
Guido van Rossum053b8df1998-11-25 16:18:00 +0000747
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000748static int
Tim Peterscba30e22003-02-01 06:24:36 +0000749put2(Picklerobject *self, PyObject *ob)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000750{
751 char c_str[30];
752 int p;
753 size_t len;
754 int res = -1;
755 PyObject *py_ob_id = 0, *memo_len = 0, *t = 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000756
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000757 if (self->fast)
758 return 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000759
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000760 if ((p = PyDict_Size(self->memo)) < 0)
761 goto finally;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000762
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000763 /* Make sure memo keys are positive! */
Tim Peters70b02d72003-02-02 17:26:40 +0000764 /* XXX Why?
765 * XXX And does "positive" really mean non-negative?
766 * XXX pickle.py starts with PUT index 0, not 1. This makes for
767 * XXX gratuitous differences between the pickling modules.
768 */
Tim Peterscba30e22003-02-01 06:24:36 +0000769 p++;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000770
Tim Peterscba30e22003-02-01 06:24:36 +0000771 if (!( py_ob_id = PyLong_FromVoidPtr(ob)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000772 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000773
Tim Peterscba30e22003-02-01 06:24:36 +0000774 if (!( memo_len = PyInt_FromLong(p)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000775 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000776
Tim Peterscba30e22003-02-01 06:24:36 +0000777 if (!( t = PyTuple_New(2)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000778 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000779
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000780 PyTuple_SET_ITEM(t, 0, memo_len);
781 Py_INCREF(memo_len);
782 PyTuple_SET_ITEM(t, 1, ob);
783 Py_INCREF(ob);
784
785 if (PyDict_SetItem(self->memo, py_ob_id, t) < 0)
786 goto finally;
787
788 if (!self->bin) {
789 c_str[0] = PUT;
790 PyOS_snprintf(c_str + 1, sizeof(c_str) - 1, "%d\n", p);
791 len = strlen(c_str);
792 }
793 else if (Pdata_Check(self->file)) {
794 if (write_other(self, NULL, 0) < 0) return -1;
795 PDATA_APPEND(self->file, memo_len, -1);
796 res=0; /* Job well done ;) */
797 goto finally;
798 }
799 else {
800 if (p >= 256) {
801 c_str[0] = LONG_BINPUT;
802 c_str[1] = (int)(p & 0xff);
803 c_str[2] = (int)((p >> 8) & 0xff);
804 c_str[3] = (int)((p >> 16) & 0xff);
805 c_str[4] = (int)((p >> 24) & 0xff);
806 len = 5;
807 }
808 else {
809 c_str[0] = BINPUT;
810 c_str[1] = p;
811 len = 2;
812 }
813 }
814
Tim Peters0bc93f52003-02-02 18:29:33 +0000815 if (self->write_func(self, c_str, len) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000816 goto finally;
817
818 res = 0;
819
820 finally:
821 Py_XDECREF(py_ob_id);
822 Py_XDECREF(memo_len);
823 Py_XDECREF(t);
824
825 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000826}
827
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000828static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +0000829whichmodule(PyObject *global, PyObject *global_name)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000830{
831 int i, j;
832 PyObject *module = 0, *modules_dict = 0,
833 *global_name_attr = 0, *name = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000834
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000835 module = PyObject_GetAttrString(global, "__module__");
836 if (module) return module;
837 PyErr_Clear();
Guido van Rossum45188231997-09-28 05:38:51 +0000838
Tim Peterscba30e22003-02-01 06:24:36 +0000839 if (!( modules_dict = PySys_GetObject("modules")))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000840 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000841
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000842 i = 0;
843 while ((j = PyDict_Next(modules_dict, &i, &name, &module))) {
Guido van Rossum142eeb81997-08-13 03:14:41 +0000844
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000845 if (PyObject_Compare(name, __main___str)==0) continue;
Tim Peters84e87f32001-03-17 04:50:51 +0000846
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000847 global_name_attr = PyObject_GetAttr(module, global_name);
848 if (!global_name_attr) {
849 PyErr_Clear();
850 continue;
851 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000852
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000853 if (global_name_attr != global) {
854 Py_DECREF(global_name_attr);
855 continue;
856 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000857
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000858 Py_DECREF(global_name_attr);
Guido van Rossum60456fd1997-04-09 17:36:32 +0000859
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000860 break;
861 }
Guido van Rossum142eeb81997-08-13 03:14:41 +0000862
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000863 /* The following implements the rule in pickle.py added in 1.5
864 that used __main__ if no module is found. I don't actually
865 like this rule. jlf
866 */
867 if (!j) {
868 j=1;
869 name=__main___str;
870 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000871
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000872 Py_INCREF(name);
873 return name;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000874}
875
876
Guido van Rossum60456fd1997-04-09 17:36:32 +0000877static int
Jeremy Hylton499ab6a2001-10-15 21:37:58 +0000878fast_save_enter(Picklerobject *self, PyObject *obj)
879{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000880 /* if fast_container < 0, we're doing an error exit. */
881 if (++self->fast_container >= PY_CPICKLE_FAST_LIMIT) {
882 PyObject *key = NULL;
883 if (self->fast_memo == NULL) {
884 self->fast_memo = PyDict_New();
885 if (self->fast_memo == NULL) {
886 self->fast_container = -1;
887 return 0;
888 }
889 }
890 key = PyLong_FromVoidPtr(obj);
891 if (key == NULL)
892 return 0;
893 if (PyDict_GetItem(self->fast_memo, key)) {
Tim Peters4e52ca82002-12-07 02:43:28 +0000894 Py_DECREF(key);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000895 PyErr_Format(PyExc_ValueError,
Tim Peters92c8bb32003-02-13 23:00:26 +0000896 "fast mode: can't pickle cyclic objects "
897 "including object type %s at %p",
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000898 obj->ob_type->tp_name, obj);
899 self->fast_container = -1;
900 return 0;
901 }
902 if (PyDict_SetItem(self->fast_memo, key, Py_None) < 0) {
Tim Peters4e52ca82002-12-07 02:43:28 +0000903 Py_DECREF(key);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000904 self->fast_container = -1;
905 return 0;
906 }
Tim Peters4e52ca82002-12-07 02:43:28 +0000907 Py_DECREF(key);
Jeremy Hylton499ab6a2001-10-15 21:37:58 +0000908 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000909 return 1;
Jeremy Hylton499ab6a2001-10-15 21:37:58 +0000910}
911
Tim Peterscba30e22003-02-01 06:24:36 +0000912int
Jeremy Hylton499ab6a2001-10-15 21:37:58 +0000913fast_save_leave(Picklerobject *self, PyObject *obj)
914{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000915 if (self->fast_container-- >= PY_CPICKLE_FAST_LIMIT) {
916 PyObject *key = PyLong_FromVoidPtr(obj);
917 if (key == NULL)
918 return 0;
919 if (PyDict_DelItem(self->fast_memo, key) < 0) {
Tim Peters4e52ca82002-12-07 02:43:28 +0000920 Py_DECREF(key);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000921 return 0;
922 }
Tim Peters4e52ca82002-12-07 02:43:28 +0000923 Py_DECREF(key);
Jeremy Hylton499ab6a2001-10-15 21:37:58 +0000924 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000925 return 1;
Jeremy Hylton499ab6a2001-10-15 21:37:58 +0000926}
927
928static int
Tim Peterscba30e22003-02-01 06:24:36 +0000929save_none(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000930{
931 static char none = NONE;
Tim Peters0bc93f52003-02-02 18:29:33 +0000932 if (self->write_func(self, &none, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000933 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000934
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000935 return 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000936}
937
Guido van Rossum77f6a652002-04-03 22:41:51 +0000938static int
Tim Peterscba30e22003-02-01 06:24:36 +0000939save_bool(Picklerobject *self, PyObject *args)
Guido van Rossum77f6a652002-04-03 22:41:51 +0000940{
Guido van Rossume2763392002-04-05 19:30:08 +0000941 static char *buf[2] = {FALSE, TRUE};
942 static char len[2] = {sizeof(FALSE)-1, sizeof(TRUE)-1};
Guido van Rossum77f6a652002-04-03 22:41:51 +0000943 long l = PyInt_AS_LONG((PyIntObject *)args);
944
Tim Peters3c67d792003-02-02 17:59:11 +0000945 if (self->proto >= 2) {
946 char opcode = l ? NEWTRUE : NEWFALSE;
947 if (self->write_func(self, &opcode, 1) < 0)
948 return -1;
949 }
950 else if (self->write_func(self, buf[l], len[l]) < 0)
Guido van Rossum77f6a652002-04-03 22:41:51 +0000951 return -1;
Guido van Rossum77f6a652002-04-03 22:41:51 +0000952 return 0;
953}
Tim Peters84e87f32001-03-17 04:50:51 +0000954
Guido van Rossum60456fd1997-04-09 17:36:32 +0000955static int
Tim Peterscba30e22003-02-01 06:24:36 +0000956save_int(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000957{
958 char c_str[32];
959 long l = PyInt_AS_LONG((PyIntObject *)args);
960 int len = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000961
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000962 if (!self->bin
Guido van Rossumde8d6d71997-05-13 18:00:44 +0000963#if SIZEOF_LONG > 4
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000964 || l > 0x7fffffffL
965 || l < -0x80000000L
Guido van Rossumde8d6d71997-05-13 18:00:44 +0000966#endif
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000967 ) {
968 /* Text-mode pickle, or long too big to fit in the 4-byte
969 * signed BININT format: store as a string.
970 */
971 c_str[0] = INT;
972 PyOS_snprintf(c_str + 1, sizeof(c_str) - 1, "%ld\n", l);
Tim Peters0bc93f52003-02-02 18:29:33 +0000973 if (self->write_func(self, c_str, strlen(c_str)) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000974 return -1;
975 }
976 else {
977 /* Binary pickle and l fits in a signed 4-byte int. */
978 c_str[1] = (int)( l & 0xff);
979 c_str[2] = (int)((l >> 8) & 0xff);
980 c_str[3] = (int)((l >> 16) & 0xff);
981 c_str[4] = (int)((l >> 24) & 0xff);
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000982
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000983 if ((c_str[4] == 0) && (c_str[3] == 0)) {
984 if (c_str[2] == 0) {
985 c_str[0] = BININT1;
986 len = 2;
987 }
988 else {
989 c_str[0] = BININT2;
990 len = 3;
991 }
992 }
993 else {
994 c_str[0] = BININT;
995 len = 5;
996 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000997
Tim Peters0bc93f52003-02-02 18:29:33 +0000998 if (self->write_func(self, c_str, len) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000999 return -1;
1000 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001001
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001002 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001003}
1004
1005
1006static int
Tim Peterscba30e22003-02-01 06:24:36 +00001007save_long(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001008{
Tim Petersee1a53c2003-02-02 02:57:53 +00001009 int size;
1010 int res = -1;
1011 PyObject *repr = NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001012
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001013 static char l = LONG;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001014
Tim Petersee1a53c2003-02-02 02:57:53 +00001015 if (self->proto >= 2) {
1016 /* Linear-time pickling. */
1017 size_t nbits;
1018 size_t nbytes;
1019 unsigned char *pdata;
1020 char c_str[5];
1021 int i;
1022 int sign = _PyLong_Sign(args);
1023
1024 if (sign == 0) {
1025 /* It's 0 -- an empty bytestring. */
1026 c_str[0] = LONG1;
1027 c_str[1] = 0;
1028 i = self->write_func(self, c_str, 2);
1029 if (i < 0) goto finally;
1030 res = 0;
1031 goto finally;
1032 }
1033 nbits = _PyLong_NumBits(args);
1034 if (nbits == (size_t)-1 && PyErr_Occurred())
1035 goto finally;
1036 /* How many bytes do we need? There are nbits >> 3 full
1037 * bytes of data, and nbits & 7 leftover bits. If there
1038 * are any leftover bits, then we clearly need another
1039 * byte. Wnat's not so obvious is that we *probably*
1040 * need another byte even if there aren't any leftovers:
1041 * the most-significant bit of the most-significant byte
1042 * acts like a sign bit, and it's usually got a sense
1043 * opposite of the one we need. The exception is longs
1044 * of the form -(2**(8*j-1)) for j > 0. Such a long is
1045 * its own 256's-complement, so has the right sign bit
1046 * even without the extra byte. That's a pain to check
1047 * for in advance, though, so we always grab an extra
1048 * byte at the start, and cut it back later if possible.
1049 */
1050 nbytes = (nbits >> 3) + 1;
1051 if ((int)nbytes < 0 || (size_t)(int)nbytes != nbytes) {
1052 PyErr_SetString(PyExc_OverflowError, "long too large "
1053 "to pickle");
1054 goto finally;
1055 }
1056 repr = PyString_FromStringAndSize(NULL, (int)nbytes);
1057 if (repr == NULL) goto finally;
1058 pdata = (unsigned char *)PyString_AS_STRING(repr);
1059 i = _PyLong_AsByteArray((PyLongObject *)args,
1060 pdata, nbytes,
1061 1 /* little endian */, 1 /* signed */);
1062 if (i < 0) goto finally;
1063 /* If the long is negative, this may be a byte more than
1064 * needed. This is so iff the MSB is all redundant sign
1065 * bits.
1066 */
1067 if (sign < 0 && nbytes > 1 && pdata[nbytes - 1] == 0xff &&
1068 (pdata[nbytes - 2] & 0x80) != 0)
1069 --nbytes;
1070
1071 if (nbytes < 256) {
1072 c_str[0] = LONG1;
1073 c_str[1] = (char)nbytes;
1074 size = 2;
1075 }
1076 else {
1077 c_str[0] = LONG4;
1078 size = (int)nbytes;
1079 for (i = 1; i < 5; i++) {
1080 c_str[i] = (char)(size & 0xff);
1081 size >>= 8;
1082 }
1083 size = 5;
1084 }
1085 i = self->write_func(self, c_str, size);
1086 if (i < 0) goto finally;
1087 i = self->write_func(self, (char *)pdata, (int)nbytes);
1088 if (i < 0) goto finally;
1089 res = 0;
1090 goto finally;
1091 }
1092
1093 /* proto < 2: write the repr and newline. This is quadratic-time
1094 * (in the number of digits), in both directions.
1095 */
Tim Peterscba30e22003-02-01 06:24:36 +00001096 if (!( repr = PyObject_Repr(args)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001097 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001098
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001099 if ((size = PyString_Size(repr)) < 0)
1100 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001101
Tim Peters0bc93f52003-02-02 18:29:33 +00001102 if (self->write_func(self, &l, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001103 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001104
Tim Peters0bc93f52003-02-02 18:29:33 +00001105 if (self->write_func(self,
1106 PyString_AS_STRING((PyStringObject *)repr),
1107 size) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001108 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001109
Tim Peters0bc93f52003-02-02 18:29:33 +00001110 if (self->write_func(self, "\n", 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001111 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001112
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001113 res = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001114
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001115 finally:
1116 Py_XDECREF(repr);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001117 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001118}
1119
1120
1121static int
Tim Peterscba30e22003-02-01 06:24:36 +00001122save_float(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001123{
1124 double x = PyFloat_AS_DOUBLE((PyFloatObject *)args);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001125
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001126 if (self->bin) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001127 char str[9];
Tim Peters9905b942003-03-20 20:53:32 +00001128 str[0] = BINFLOAT;
1129 if (_PyFloat_Pack8(x, (unsigned char *)&str[1], 0) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001130 return -1;
Tim Peters0bc93f52003-02-02 18:29:33 +00001131 if (self->write_func(self, str, 9) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001132 return -1;
1133 }
1134 else {
1135 char c_str[250];
1136 c_str[0] = FLOAT;
1137 PyOS_snprintf(c_str + 1, sizeof(c_str) - 1, "%.17g\n", x);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001138
Tim Peters0bc93f52003-02-02 18:29:33 +00001139 if (self->write_func(self, c_str, strlen(c_str)) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001140 return -1;
1141 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001142
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001143 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001144}
1145
1146
1147static int
Tim Peterscba30e22003-02-01 06:24:36 +00001148save_string(Picklerobject *self, PyObject *args, int doput)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001149{
1150 int size, len;
1151 PyObject *repr=0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001152
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001153 if ((size = PyString_Size(args)) < 0)
1154 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001155
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001156 if (!self->bin) {
1157 char *repr_str;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001158
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001159 static char string = STRING;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001160
Tim Peterscba30e22003-02-01 06:24:36 +00001161 if (!( repr = PyObject_Repr(args)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001162 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001163
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001164 if ((len = PyString_Size(repr)) < 0)
1165 goto err;
1166 repr_str = PyString_AS_STRING((PyStringObject *)repr);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001167
Tim Peters0bc93f52003-02-02 18:29:33 +00001168 if (self->write_func(self, &string, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001169 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001170
Tim Peters0bc93f52003-02-02 18:29:33 +00001171 if (self->write_func(self, repr_str, len) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001172 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001173
Tim Peters0bc93f52003-02-02 18:29:33 +00001174 if (self->write_func(self, "\n", 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001175 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001176
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001177 Py_XDECREF(repr);
1178 }
1179 else {
1180 int i;
1181 char c_str[5];
Guido van Rossum60456fd1997-04-09 17:36:32 +00001182
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001183 if ((size = PyString_Size(args)) < 0)
1184 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001185
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001186 if (size < 256) {
1187 c_str[0] = SHORT_BINSTRING;
1188 c_str[1] = size;
1189 len = 2;
1190 }
1191 else {
1192 c_str[0] = BINSTRING;
1193 for (i = 1; i < 5; i++)
1194 c_str[i] = (int)(size >> ((i - 1) * 8));
1195 len = 5;
1196 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001197
Tim Peters0bc93f52003-02-02 18:29:33 +00001198 if (self->write_func(self, c_str, len) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001199 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001200
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001201 if (size > 128 && Pdata_Check(self->file)) {
1202 if (write_other(self, NULL, 0) < 0) return -1;
1203 PDATA_APPEND(self->file, args, -1);
1204 }
1205 else {
Tim Peters0bc93f52003-02-02 18:29:33 +00001206 if (self->write_func(self,
1207 PyString_AS_STRING(
1208 (PyStringObject *)args),
1209 size) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001210 return -1;
1211 }
1212 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001213
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001214 if (doput)
1215 if (put(self, args) < 0)
1216 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001217
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001218 return 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +00001219
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001220 err:
1221 Py_XDECREF(repr);
1222 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001223}
1224
1225
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001226#ifdef Py_USING_UNICODE
Guido van Rossumfb10c3f2000-12-19 02:08:38 +00001227/* A copy of PyUnicode_EncodeRawUnicodeEscape() that also translates
1228 backslash and newline characters to \uXXXX escapes. */
1229static PyObject *
1230modified_EncodeRawUnicodeEscape(const Py_UNICODE *s, int size)
1231{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001232 PyObject *repr;
1233 char *p;
1234 char *q;
Guido van Rossumfb10c3f2000-12-19 02:08:38 +00001235
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001236 static const char *hexdigit = "0123456789ABCDEF";
Guido van Rossumfb10c3f2000-12-19 02:08:38 +00001237
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001238 repr = PyString_FromStringAndSize(NULL, 6 * size);
1239 if (repr == NULL)
1240 return NULL;
1241 if (size == 0)
1242 return repr;
1243
1244 p = q = PyString_AS_STRING(repr);
1245 while (size-- > 0) {
1246 Py_UNICODE ch = *s++;
1247 /* Map 16-bit characters to '\uxxxx' */
1248 if (ch >= 256 || ch == '\\' || ch == '\n') {
1249 *p++ = '\\';
1250 *p++ = 'u';
1251 *p++ = hexdigit[(ch >> 12) & 0xf];
1252 *p++ = hexdigit[(ch >> 8) & 0xf];
1253 *p++ = hexdigit[(ch >> 4) & 0xf];
1254 *p++ = hexdigit[ch & 15];
1255 }
1256 /* Copy everything else as-is */
1257 else
1258 *p++ = (char) ch;
1259 }
1260 *p = '\0';
Tim Peters5de98422002-04-27 18:44:32 +00001261 _PyString_Resize(&repr, p - q);
Guido van Rossumfb10c3f2000-12-19 02:08:38 +00001262 return repr;
Guido van Rossumfb10c3f2000-12-19 02:08:38 +00001263}
1264
1265
Guido van Rossum60456fd1997-04-09 17:36:32 +00001266static int
Tim Peterscba30e22003-02-01 06:24:36 +00001267save_unicode(Picklerobject *self, PyObject *args, int doput)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001268{
1269 int size, len;
1270 PyObject *repr=0;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001271
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001272 if (!PyUnicode_Check(args))
1273 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001274
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001275 if (!self->bin) {
1276 char *repr_str;
1277 static char string = UNICODE;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001278
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001279 repr = modified_EncodeRawUnicodeEscape(
1280 PyUnicode_AS_UNICODE(args), PyUnicode_GET_SIZE(args));
Tim Peterscba30e22003-02-01 06:24:36 +00001281 if (!repr)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001282 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001283
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001284 if ((len = PyString_Size(repr)) < 0)
1285 goto err;
1286 repr_str = PyString_AS_STRING((PyStringObject *)repr);
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001287
Tim Peters0bc93f52003-02-02 18:29:33 +00001288 if (self->write_func(self, &string, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001289 goto err;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001290
Tim Peters0bc93f52003-02-02 18:29:33 +00001291 if (self->write_func(self, repr_str, len) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001292 goto err;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001293
Tim Peters0bc93f52003-02-02 18:29:33 +00001294 if (self->write_func(self, "\n", 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001295 goto err;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001296
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001297 Py_XDECREF(repr);
1298 }
1299 else {
1300 int i;
1301 char c_str[5];
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001302
Tim Peterscba30e22003-02-01 06:24:36 +00001303 if (!( repr = PyUnicode_AsUTF8String(args)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001304 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001305
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001306 if ((size = PyString_Size(repr)) < 0)
1307 goto err;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001308
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001309 c_str[0] = BINUNICODE;
1310 for (i = 1; i < 5; i++)
1311 c_str[i] = (int)(size >> ((i - 1) * 8));
1312 len = 5;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001313
Tim Peters0bc93f52003-02-02 18:29:33 +00001314 if (self->write_func(self, c_str, len) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001315 goto err;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001316
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001317 if (size > 128 && Pdata_Check(self->file)) {
1318 if (write_other(self, NULL, 0) < 0)
1319 goto err;
1320 PDATA_APPEND(self->file, repr, -1);
1321 }
1322 else {
Tim Peters0bc93f52003-02-02 18:29:33 +00001323 if (self->write_func(self, PyString_AS_STRING(repr),
1324 size) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001325 goto err;
1326 }
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001327
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001328 Py_DECREF(repr);
1329 }
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001330
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001331 if (doput)
1332 if (put(self, args) < 0)
1333 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001334
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001335 return 0;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001336
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001337 err:
1338 Py_XDECREF(repr);
1339 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001340}
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001341#endif
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001342
Tim Peters1d63c9f2003-02-02 20:29:39 +00001343/* A helper for save_tuple. Push the len elements in tuple t on the stack. */
1344static int
Tim Peters67920142003-02-05 03:46:17 +00001345store_tuple_elements(Picklerobject *self, PyObject *t, int len)
Tim Peters1d63c9f2003-02-02 20:29:39 +00001346{
1347 int i;
1348 int res = -1; /* guilty until proved innocent */
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001349
Tim Peters1d63c9f2003-02-02 20:29:39 +00001350 assert(PyTuple_Size(t) == len);
1351
1352 for (i = 0; i < len; i++) {
1353 PyObject *element = PyTuple_GET_ITEM(t, i);
1354
1355 if (element == NULL)
1356 goto finally;
1357 if (save(self, element, 0) < 0)
1358 goto finally;
1359 }
1360 res = 0;
1361
1362 finally:
1363 return res;
1364}
1365
1366/* Tuples are ubiquitous in the pickle protocols, so many techniques are
1367 * used across protocols to minimize the space needed to pickle them.
Tim Peters67920142003-02-05 03:46:17 +00001368 * Tuples are also the only builtin immutable type that can be recursive
Tim Peters1d63c9f2003-02-02 20:29:39 +00001369 * (a tuple can be reached from itself), and that requires some subtle
1370 * magic so that it works in all cases. IOW, this is a long routine.
1371 */
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001372static int
Tim Peterscba30e22003-02-01 06:24:36 +00001373save_tuple(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001374{
Tim Peters1d63c9f2003-02-02 20:29:39 +00001375 PyObject *py_tuple_id = NULL;
1376 int len, i;
1377 int res = -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001378
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001379 static char tuple = TUPLE;
Tim Peters1d63c9f2003-02-02 20:29:39 +00001380 static char pop = POP;
1381 static char pop_mark = POP_MARK;
1382 static char len2opcode[] = {EMPTY_TUPLE, TUPLE1, TUPLE2, TUPLE3};
Guido van Rossum60456fd1997-04-09 17:36:32 +00001383
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001384 if ((len = PyTuple_Size(args)) < 0)
1385 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001386
Tim Peters1d63c9f2003-02-02 20:29:39 +00001387 if (len == 0) {
1388 char c_str[2];
Tim Peters84e87f32001-03-17 04:50:51 +00001389
Tim Peters1d63c9f2003-02-02 20:29:39 +00001390 if (self->proto) {
1391 c_str[0] = EMPTY_TUPLE;
1392 len = 1;
1393 }
1394 else {
1395 c_str[0] = MARK;
1396 c_str[1] = TUPLE;
1397 len = 2;
1398 }
1399 if (self->write_func(self, c_str, len) >= 0)
1400 res = 0;
1401 /* Don't memoize an empty tuple. */
1402 goto finally;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001403 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001404
Tim Peters1d63c9f2003-02-02 20:29:39 +00001405 /* A non-empty tuple. */
1406
1407 /* id(tuple) isn't in the memo now. If it shows up there after
1408 * saving the tuple elements, the tuple must be recursive, in
1409 * which case we'll pop everything we put on the stack, and fetch
1410 * its value from the memo.
1411 */
1412 py_tuple_id = PyLong_FromVoidPtr(args);
1413 if (py_tuple_id == NULL)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001414 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001415
Tim Peters1d63c9f2003-02-02 20:29:39 +00001416 if (len <= 3 && self->proto >= 2) {
1417 /* Use TUPLE{1,2,3} opcodes. */
Tim Peters67920142003-02-05 03:46:17 +00001418 if (store_tuple_elements(self, args, len) < 0)
Tim Peters1d63c9f2003-02-02 20:29:39 +00001419 goto finally;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001420 if (PyDict_GetItem(self->memo, py_tuple_id)) {
Tim Peters1d63c9f2003-02-02 20:29:39 +00001421 /* pop the len elements */
1422 for (i = 0; i < len; ++i)
1423 if (self->write_func(self, &pop, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001424 goto finally;
Tim Peters1d63c9f2003-02-02 20:29:39 +00001425 /* fetch from memo */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001426 if (get(self, py_tuple_id) < 0)
1427 goto finally;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001428 res = 0;
1429 goto finally;
1430 }
Tim Peters1d63c9f2003-02-02 20:29:39 +00001431 /* Not recursive. */
1432 if (self->write_func(self, len2opcode + len, 1) < 0)
1433 goto finally;
1434 goto memoize;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001435 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001436
Tim Peters1d63c9f2003-02-02 20:29:39 +00001437 /* proto < 2 and len > 0, or proto >= 2 and len > 3.
1438 * Generate MARK elt1 elt2 ... TUPLE
1439 */
1440 if (self->write_func(self, &MARKv, 1) < 0)
1441 goto finally;
1442
Tim Peters67920142003-02-05 03:46:17 +00001443 if (store_tuple_elements(self, args, len) < 0)
Tim Peters1d63c9f2003-02-02 20:29:39 +00001444 goto finally;
1445
1446 if (PyDict_GetItem(self->memo, py_tuple_id)) {
1447 /* pop the stack stuff we pushed */
1448 if (self->bin) {
1449 if (self->write_func(self, &pop_mark, 1) < 0)
1450 goto finally;
1451 }
1452 else {
1453 /* Note that we pop one more than len, to remove
1454 * the MARK too.
1455 */
1456 for (i = 0; i <= len; i++)
1457 if (self->write_func(self, &pop, 1) < 0)
1458 goto finally;
1459 }
1460 /* fetch from memo */
1461 if (get(self, py_tuple_id) >= 0)
1462 res = 0;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001463 goto finally;
1464 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001465
Tim Peters1d63c9f2003-02-02 20:29:39 +00001466 /* Not recursive. */
1467 if (self->write_func(self, &tuple, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001468 goto finally;
Tim Peters84e87f32001-03-17 04:50:51 +00001469
Tim Peters1d63c9f2003-02-02 20:29:39 +00001470 memoize:
1471 if (put(self, args) >= 0)
1472 res = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001473
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001474 finally:
1475 Py_XDECREF(py_tuple_id);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001476 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001477}
1478
Tim Peters1092d642003-02-11 21:06:20 +00001479/* iter is an iterator giving items, and we batch up chunks of
1480 * MARK item item ... item APPENDS
1481 * opcode sequences. Calling code should have arranged to first create an
1482 * empty list, or list-like object, for the APPENDS to operate on.
1483 * Returns 0 on success, <0 on error.
1484 */
1485static int
1486batch_list(Picklerobject *self, PyObject *iter)
1487{
1488 PyObject *obj;
1489 PyObject *slice[BATCHSIZE];
1490 int i, n;
1491
1492 static char append = APPEND;
1493 static char appends = APPENDS;
1494
1495 assert(iter != NULL);
1496
1497 if (self->proto == 0) {
1498 /* APPENDS isn't available; do one at a time. */
1499 for (;;) {
1500 obj = PyIter_Next(iter);
1501 if (obj == NULL) {
1502 if (PyErr_Occurred())
1503 return -1;
1504 break;
1505 }
1506 i = save(self, obj, 0);
1507 Py_DECREF(obj);
1508 if (i < 0)
1509 return -1;
1510 if (self->write_func(self, &append, 1) < 0)
1511 return -1;
Tim Peters1092d642003-02-11 21:06:20 +00001512 }
1513 return 0;
1514 }
1515
1516 /* proto > 0: write in batches of BATCHSIZE. */
1517 do {
1518 /* Get next group of (no more than) BATCHSIZE elements. */
1519 for (n = 0; n < BATCHSIZE; ++n) {
1520 obj = PyIter_Next(iter);
1521 if (obj == NULL) {
1522 if (PyErr_Occurred())
1523 goto BatchFailed;
1524 break;
1525 }
1526 slice[n] = obj;
1527 }
1528
1529 if (n > 1) {
1530 /* Pump out MARK, slice[0:n], APPENDS. */
1531 if (self->write_func(self, &MARKv, 1) < 0)
1532 goto BatchFailed;
1533 for (i = 0; i < n; ++i) {
1534 if (save(self, slice[i], 0) < 0)
1535 goto BatchFailed;
1536 }
1537 if (self->write_func(self, &appends, 1) < 0)
1538 goto BatchFailed;
1539 }
1540 else if (n == 1) {
1541 if (save(self, slice[0], 0) < 0)
1542 goto BatchFailed;
1543 if (self->write_func(self, &append, 1) < 0)
1544 goto BatchFailed;
1545 }
1546
1547 for (i = 0; i < n; ++i) {
1548 Py_DECREF(slice[i]);
1549 }
Tim Peters90975f12003-02-12 05:28:58 +00001550 } while (n == BATCHSIZE);
Tim Peters1092d642003-02-11 21:06:20 +00001551 return 0;
1552
1553BatchFailed:
1554 while (--n >= 0) {
1555 Py_DECREF(slice[n]);
1556 }
1557 return -1;
1558}
1559
Guido van Rossum60456fd1997-04-09 17:36:32 +00001560static int
Tim Peterscba30e22003-02-01 06:24:36 +00001561save_list(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001562{
Tim Peters1092d642003-02-11 21:06:20 +00001563 int res = -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001564 char s[3];
Tim Peters1092d642003-02-11 21:06:20 +00001565 int len;
1566 PyObject *iter;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001567
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001568 if (self->fast && !fast_save_enter(self, args))
1569 goto finally;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00001570
Tim Peters1092d642003-02-11 21:06:20 +00001571 /* Create an empty list. */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001572 if (self->bin) {
1573 s[0] = EMPTY_LIST;
Tim Peters1092d642003-02-11 21:06:20 +00001574 len = 1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001575 }
1576 else {
1577 s[0] = MARK;
1578 s[1] = LIST;
Tim Peters1092d642003-02-11 21:06:20 +00001579 len = 2;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001580 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001581
Tim Peters1092d642003-02-11 21:06:20 +00001582 if (self->write_func(self, s, len) < 0)
1583 goto finally;
1584
1585 /* Get list length, and bow out early if empty. */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001586 if ((len = PyList_Size(args)) < 0)
1587 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001588
Tim Peters1092d642003-02-11 21:06:20 +00001589 /* Memoize. */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001590 if (len == 0) {
Tim Peters90975f12003-02-12 05:28:58 +00001591 if (put(self, args) >= 0)
1592 res = 0;
1593 goto finally;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001594 }
Tim Peters90975f12003-02-12 05:28:58 +00001595 if (put2(self, args) < 0)
1596 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001597
Tim Peters1092d642003-02-11 21:06:20 +00001598 /* Materialize the list elements. */
1599 iter = PyObject_GetIter(args);
1600 if (iter == NULL)
1601 goto finally;
1602 res = batch_list(self, iter);
1603 Py_DECREF(iter);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001604
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001605 finally:
1606 if (self->fast && !fast_save_leave(self, args))
1607 res = -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001608
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001609 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001610}
1611
1612
Tim Peters42f08ac2003-02-11 22:43:24 +00001613/* iter is an iterator giving (key, value) pairs, and we batch up chunks of
1614 * MARK key value ... key value SETITEMS
1615 * opcode sequences. Calling code should have arranged to first create an
1616 * empty dict, or dict-like object, for the SETITEMS to operate on.
1617 * Returns 0 on success, <0 on error.
1618 *
1619 * This is very much like batch_list(). The difference between saving
1620 * elements directly, and picking apart two-tuples, is so long-winded at
1621 * the C level, though, that attempts to combine these routines were too
1622 * ugly to bear.
1623 */
1624static int
1625batch_dict(Picklerobject *self, PyObject *iter)
1626{
1627 PyObject *p;
1628 PyObject *slice[BATCHSIZE];
1629 int i, n;
1630
1631 static char setitem = SETITEM;
1632 static char setitems = SETITEMS;
1633
1634 assert(iter != NULL);
1635
1636 if (self->proto == 0) {
1637 /* SETITEMS isn't available; do one at a time. */
1638 for (;;) {
1639 p = PyIter_Next(iter);
1640 if (p == NULL) {
1641 if (PyErr_Occurred())
1642 return -1;
1643 break;
1644 }
1645 if (!PyTuple_Check(p) || PyTuple_Size(p) != 2) {
1646 PyErr_SetString(PyExc_TypeError, "dict items "
1647 "iterator must return 2-tuples");
1648 return -1;
1649 }
1650 i = save(self, PyTuple_GET_ITEM(p, 0), 0);
1651 if (i >= 0)
1652 i = save(self, PyTuple_GET_ITEM(p, 1), 0);
1653 Py_DECREF(p);
1654 if (i < 0)
1655 return -1;
1656 if (self->write_func(self, &setitem, 1) < 0)
1657 return -1;
Tim Peters42f08ac2003-02-11 22:43:24 +00001658 }
1659 return 0;
1660 }
1661
1662 /* proto > 0: write in batches of BATCHSIZE. */
1663 do {
1664 /* Get next group of (no more than) BATCHSIZE elements. */
1665 for (n = 0; n < BATCHSIZE; ++n) {
1666 p = PyIter_Next(iter);
1667 if (p == NULL) {
1668 if (PyErr_Occurred())
1669 goto BatchFailed;
1670 break;
1671 }
1672 if (!PyTuple_Check(p) || PyTuple_Size(p) != 2) {
1673 PyErr_SetString(PyExc_TypeError, "dict items "
1674 "iterator must return 2-tuples");
1675 goto BatchFailed;
1676 }
1677 slice[n] = p;
1678 }
1679
1680 if (n > 1) {
1681 /* Pump out MARK, slice[0:n], SETITEMS. */
1682 if (self->write_func(self, &MARKv, 1) < 0)
1683 goto BatchFailed;
1684 for (i = 0; i < n; ++i) {
1685 p = slice[i];
1686 if (save(self, PyTuple_GET_ITEM(p, 0), 0) < 0)
1687 goto BatchFailed;
1688 if (save(self, PyTuple_GET_ITEM(p, 1), 0) < 0)
1689 goto BatchFailed;
1690 }
1691 if (self->write_func(self, &setitems, 1) < 0)
1692 goto BatchFailed;
1693 }
1694 else if (n == 1) {
1695 p = slice[0];
1696 if (save(self, PyTuple_GET_ITEM(p, 0), 0) < 0)
1697 goto BatchFailed;
1698 if (save(self, PyTuple_GET_ITEM(p, 1), 0) < 0)
1699 goto BatchFailed;
1700 if (self->write_func(self, &setitem, 1) < 0)
1701 goto BatchFailed;
1702 }
1703
1704 for (i = 0; i < n; ++i) {
1705 Py_DECREF(slice[i]);
1706 }
Tim Peters90975f12003-02-12 05:28:58 +00001707 } while (n == BATCHSIZE);
Tim Peters42f08ac2003-02-11 22:43:24 +00001708 return 0;
1709
1710BatchFailed:
1711 while (--n >= 0) {
1712 Py_DECREF(slice[n]);
1713 }
1714 return -1;
1715}
1716
Guido van Rossum60456fd1997-04-09 17:36:32 +00001717static int
Tim Peterscba30e22003-02-01 06:24:36 +00001718save_dict(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001719{
Tim Peters42f08ac2003-02-11 22:43:24 +00001720 int res = -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001721 char s[3];
Tim Peters42f08ac2003-02-11 22:43:24 +00001722 int len;
1723 PyObject *iter;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001724
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001725 if (self->fast && !fast_save_enter(self, args))
1726 goto finally;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00001727
Tim Peters42f08ac2003-02-11 22:43:24 +00001728 /* Create an empty dict. */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001729 if (self->bin) {
1730 s[0] = EMPTY_DICT;
1731 len = 1;
1732 }
1733 else {
1734 s[0] = MARK;
1735 s[1] = DICT;
1736 len = 2;
1737 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001738
Tim Peters0bc93f52003-02-02 18:29:33 +00001739 if (self->write_func(self, s, len) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001740 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001741
Tim Peters42f08ac2003-02-11 22:43:24 +00001742 /* Get dict size, and bow out early if empty. */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001743 if ((len = PyDict_Size(args)) < 0)
1744 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001745
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001746 if (len == 0) {
Tim Peters90975f12003-02-12 05:28:58 +00001747 if (put(self, args) >= 0)
1748 res = 0;
1749 goto finally;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001750 }
Tim Peters90975f12003-02-12 05:28:58 +00001751 if (put2(self, args) < 0)
1752 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001753
Tim Peters42f08ac2003-02-11 22:43:24 +00001754 /* Materialize the dict items. */
1755 iter = PyObject_CallMethod(args, "iteritems", "()");
1756 if (iter == NULL)
1757 goto finally;
1758 res = batch_dict(self, iter);
1759 Py_DECREF(iter);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001760
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001761 finally:
1762 if (self->fast && !fast_save_leave(self, args))
1763 res = -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001764
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001765 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001766}
1767
1768
Tim Peters84e87f32001-03-17 04:50:51 +00001769static int
Tim Peterscba30e22003-02-01 06:24:36 +00001770save_inst(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001771{
1772 PyObject *class = 0, *module = 0, *name = 0, *state = 0,
1773 *getinitargs_func = 0, *getstate_func = 0, *class_args = 0;
1774 char *module_str, *name_str;
1775 int module_size, name_size, res = -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001776
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001777 static char inst = INST, obj = OBJ, build = BUILD;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001778
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001779 if (self->fast && !fast_save_enter(self, args))
1780 goto finally;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00001781
Tim Peters0bc93f52003-02-02 18:29:33 +00001782 if (self->write_func(self, &MARKv, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001783 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001784
Tim Peterscba30e22003-02-01 06:24:36 +00001785 if (!( class = PyObject_GetAttr(args, __class___str)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001786 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001787
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001788 if (self->bin) {
1789 if (save(self, class, 0) < 0)
1790 goto finally;
1791 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001792
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001793 if ((getinitargs_func = PyObject_GetAttr(args, __getinitargs___str))) {
1794 PyObject *element = 0;
1795 int i, len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001796
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001797 if (!( class_args =
Tim Peterscba30e22003-02-01 06:24:36 +00001798 PyObject_Call(getinitargs_func, empty_tuple, NULL)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001799 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001800
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001801 if ((len = PyObject_Size(class_args)) < 0)
1802 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001803
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001804 for (i = 0; i < len; i++) {
Tim Peterscba30e22003-02-01 06:24:36 +00001805 if (!( element = PySequence_GetItem(class_args, i)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001806 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001807
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001808 if (save(self, element, 0) < 0) {
1809 Py_DECREF(element);
1810 goto finally;
1811 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001812
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001813 Py_DECREF(element);
1814 }
1815 }
1816 else {
1817 PyErr_Clear();
1818 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001819
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001820 if (!self->bin) {
1821 if (!( name = ((PyClassObject *)class)->cl_name )) {
1822 PyErr_SetString(PicklingError, "class has no name");
1823 goto finally;
1824 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001825
Tim Peterscba30e22003-02-01 06:24:36 +00001826 if (!( module = whichmodule(class, name)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001827 goto finally;
Guido van Rossum053b8df1998-11-25 16:18:00 +00001828
Tim Peters84e87f32001-03-17 04:50:51 +00001829
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001830 if ((module_size = PyString_Size(module)) < 0 ||
1831 (name_size = PyString_Size(name)) < 0)
1832 goto finally;
Guido van Rossum053b8df1998-11-25 16:18:00 +00001833
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001834 module_str = PyString_AS_STRING((PyStringObject *)module);
1835 name_str = PyString_AS_STRING((PyStringObject *)name);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001836
Tim Peters0bc93f52003-02-02 18:29:33 +00001837 if (self->write_func(self, &inst, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001838 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001839
Tim Peters0bc93f52003-02-02 18:29:33 +00001840 if (self->write_func(self, module_str, module_size) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001841 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001842
Tim Peters0bc93f52003-02-02 18:29:33 +00001843 if (self->write_func(self, "\n", 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001844 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001845
Tim Peters0bc93f52003-02-02 18:29:33 +00001846 if (self->write_func(self, name_str, name_size) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001847 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001848
Tim Peters0bc93f52003-02-02 18:29:33 +00001849 if (self->write_func(self, "\n", 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001850 goto finally;
1851 }
Tim Peters0bc93f52003-02-02 18:29:33 +00001852 else if (self->write_func(self, &obj, 1) < 0) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001853 goto finally;
1854 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001855
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001856 if ((getstate_func = PyObject_GetAttr(args, __getstate___str))) {
1857 state = PyObject_Call(getstate_func, empty_tuple, NULL);
Tim Peterscba30e22003-02-01 06:24:36 +00001858 if (!state)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001859 goto finally;
1860 }
1861 else {
1862 PyErr_Clear();
Guido van Rossum60456fd1997-04-09 17:36:32 +00001863
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001864 if (!( state = PyObject_GetAttr(args, __dict___str))) {
1865 PyErr_Clear();
1866 res = 0;
1867 goto finally;
1868 }
1869 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001870
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001871 if (!PyDict_Check(state)) {
1872 if (put2(self, args) < 0)
1873 goto finally;
1874 }
1875 else {
1876 if (put(self, args) < 0)
1877 goto finally;
1878 }
Tim Peters84e87f32001-03-17 04:50:51 +00001879
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001880 if (save(self, state, 0) < 0)
1881 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001882
Tim Peters0bc93f52003-02-02 18:29:33 +00001883 if (self->write_func(self, &build, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001884 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001885
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001886 res = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001887
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001888 finally:
1889 if (self->fast && !fast_save_leave(self, args))
1890 res = -1;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00001891
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001892 Py_XDECREF(module);
1893 Py_XDECREF(class);
1894 Py_XDECREF(state);
1895 Py_XDECREF(getinitargs_func);
1896 Py_XDECREF(getstate_func);
1897 Py_XDECREF(class_args);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001898
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001899 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001900}
1901
1902
Guido van Rossum60456fd1997-04-09 17:36:32 +00001903static int
Tim Peterscba30e22003-02-01 06:24:36 +00001904save_global(Picklerobject *self, PyObject *args, PyObject *name)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001905{
Jeremy Hylton9ee91f12002-07-11 22:02:33 +00001906 PyObject *global_name = 0, *module = 0, *mod = 0, *klass = 0;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001907 char *name_str, *module_str;
1908 int module_size, name_size, res = -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001909
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001910 static char global = GLOBAL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001911
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001912 if (name) {
1913 global_name = name;
1914 Py_INCREF(global_name);
1915 }
1916 else {
Tim Peterscba30e22003-02-01 06:24:36 +00001917 if (!( global_name = PyObject_GetAttr(args, __name___str)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001918 goto finally;
1919 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001920
Tim Peterscba30e22003-02-01 06:24:36 +00001921 if (!( module = whichmodule(args, global_name)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001922 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001923
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001924 if ((module_size = PyString_Size(module)) < 0 ||
1925 (name_size = PyString_Size(global_name)) < 0)
1926 goto finally;
Guido van Rossum053b8df1998-11-25 16:18:00 +00001927
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001928 module_str = PyString_AS_STRING((PyStringObject *)module);
1929 name_str = PyString_AS_STRING((PyStringObject *)global_name);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001930
Guido van Rossum75bfd052002-12-24 18:10:07 +00001931 /* XXX This can be doing a relative import. Clearly it shouldn't,
1932 but I don't know how to stop it. :-( */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001933 mod = PyImport_ImportModule(module_str);
1934 if (mod == NULL) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001935 cPickle_ErrFormat(PicklingError,
Tim Petersb9ce7cd2003-02-18 20:50:45 +00001936 "Can't pickle %s: import of module %s "
1937 "failed",
1938 "OS", args, module);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001939 goto finally;
1940 }
Jeremy Hylton0e1f7a82002-07-11 22:01:40 +00001941 klass = PyObject_GetAttrString(mod, name_str);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001942 if (klass == NULL) {
1943 cPickle_ErrFormat(PicklingError,
Tim Petersb9ce7cd2003-02-18 20:50:45 +00001944 "Can't pickle %s: attribute lookup %s.%s "
1945 "failed",
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001946 "OSS", args, module, global_name);
1947 goto finally;
1948 }
1949 if (klass != args) {
Jeremy Hylton0e1f7a82002-07-11 22:01:40 +00001950 Py_DECREF(klass);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001951 cPickle_ErrFormat(PicklingError,
Tim Peters731098b2003-02-04 20:56:09 +00001952 "Can't pickle %s: it's not the same object "
1953 "as %s.%s",
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001954 "OSS", args, module, global_name);
1955 goto finally;
1956 }
Jeremy Hylton0e1f7a82002-07-11 22:01:40 +00001957 Py_DECREF(klass);
Guido van Rossuma92d16a2001-08-18 21:22:07 +00001958
Tim Peters731098b2003-02-04 20:56:09 +00001959 if (self->proto >= 2) {
1960 /* See whether this is in the extension registry, and if
1961 * so generate an EXT opcode.
1962 */
1963 PyObject *py_code; /* extension code as Python object */
Tim Peters3e667d52003-02-04 21:47:44 +00001964 long code; /* extension code as C value */
Tim Peters731098b2003-02-04 20:56:09 +00001965 char c_str[5];
1966 int n;
1967
1968 PyTuple_SET_ITEM(two_tuple, 0, module);
1969 PyTuple_SET_ITEM(two_tuple, 1, global_name);
1970 py_code = PyDict_GetItem(extension_registry, two_tuple);
1971 if (py_code == NULL)
1972 goto gen_global; /* not registered */
1973
1974 /* Verify py_code has the right type and value. */
1975 if (!PyInt_Check(py_code)) {
1976 cPickle_ErrFormat(PicklingError, "Can't pickle %s: "
Tim Peters6288e232003-02-05 03:53:10 +00001977 "extension code %s isn't an integer",
Tim Peters731098b2003-02-04 20:56:09 +00001978 "OO", args, py_code);
1979 goto finally;
1980 }
1981 code = PyInt_AS_LONG(py_code);
1982 if (code <= 0 || code > 0x7fffffffL) {
1983 cPickle_ErrFormat(PicklingError, "Can't pickle %s: "
1984 "extension code %ld is out of range",
1985 "Ol", args, code);
1986 goto finally;
1987 }
1988
1989 /* Generate an EXT opcode. */
1990 if (code <= 0xff) {
1991 c_str[0] = EXT1;
1992 c_str[1] = (char)code;
1993 n = 2;
1994 }
1995 else if (code <= 0xffff) {
1996 c_str[0] = EXT2;
1997 c_str[1] = (char)(code & 0xff);
1998 c_str[2] = (char)((code >> 8) & 0xff);
1999 n = 3;
2000 }
2001 else {
2002 c_str[0] = EXT4;
2003 c_str[1] = (char)(code & 0xff);
2004 c_str[2] = (char)((code >> 8) & 0xff);
2005 c_str[3] = (char)((code >> 16) & 0xff);
2006 c_str[4] = (char)((code >> 24) & 0xff);
2007 n = 5;
2008 }
2009
2010 if (self->write_func(self, c_str, n) >= 0)
2011 res = 0;
2012 goto finally; /* and don't memoize */
2013 }
2014
2015 gen_global:
Tim Peters0bc93f52003-02-02 18:29:33 +00002016 if (self->write_func(self, &global, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002017 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002018
Tim Peters0bc93f52003-02-02 18:29:33 +00002019 if (self->write_func(self, module_str, module_size) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002020 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002021
Tim Peters0bc93f52003-02-02 18:29:33 +00002022 if (self->write_func(self, "\n", 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002023 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002024
Tim Peters0bc93f52003-02-02 18:29:33 +00002025 if (self->write_func(self, name_str, name_size) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002026 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002027
Tim Peters0bc93f52003-02-02 18:29:33 +00002028 if (self->write_func(self, "\n", 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002029 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002030
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002031 if (put(self, args) < 0)
2032 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002033
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002034 res = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002035
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002036 finally:
2037 Py_XDECREF(module);
2038 Py_XDECREF(global_name);
2039 Py_XDECREF(mod);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002040
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002041 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002042}
2043
Guido van Rossum60456fd1997-04-09 17:36:32 +00002044static int
Tim Peterscba30e22003-02-01 06:24:36 +00002045save_pers(Picklerobject *self, PyObject *args, PyObject *f)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002046{
2047 PyObject *pid = 0;
2048 int size, res = -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002049
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002050 static char persid = PERSID, binpersid = BINPERSID;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002051
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002052 Py_INCREF(args);
2053 ARG_TUP(self, args);
2054 if (self->arg) {
2055 pid = PyObject_Call(f, self->arg, NULL);
2056 FREE_ARG_TUP(self);
2057 }
2058 if (! pid) return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002059
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002060 if (pid != Py_None) {
2061 if (!self->bin) {
2062 if (!PyString_Check(pid)) {
2063 PyErr_SetString(PicklingError,
2064 "persistent id must be string");
2065 goto finally;
2066 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002067
Tim Peters0bc93f52003-02-02 18:29:33 +00002068 if (self->write_func(self, &persid, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002069 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002070
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002071 if ((size = PyString_Size(pid)) < 0)
2072 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002073
Tim Peters0bc93f52003-02-02 18:29:33 +00002074 if (self->write_func(self,
2075 PyString_AS_STRING(
2076 (PyStringObject *)pid),
2077 size) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002078 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002079
Tim Peters0bc93f52003-02-02 18:29:33 +00002080 if (self->write_func(self, "\n", 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002081 goto finally;
Tim Peters84e87f32001-03-17 04:50:51 +00002082
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002083 res = 1;
2084 goto finally;
2085 }
2086 else if (save(self, pid, 1) >= 0) {
Tim Peters0bc93f52003-02-02 18:29:33 +00002087 if (self->write_func(self, &binpersid, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002088 res = -1;
2089 else
2090 res = 1;
2091 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002092
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002093 goto finally;
2094 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002095
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002096 res = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002097
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002098 finally:
2099 Py_XDECREF(pid);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002100
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002101 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002102}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002103
Tim Peters71fcda52003-02-14 23:05:28 +00002104/* We're saving ob, and args is the 2-thru-5 tuple returned by the
2105 * appropriate __reduce__ method for ob.
2106 */
Tim Peters84e87f32001-03-17 04:50:51 +00002107static int
Tim Peters71fcda52003-02-14 23:05:28 +00002108save_reduce(Picklerobject *self, PyObject *args, PyObject *ob)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002109{
Tim Peters71fcda52003-02-14 23:05:28 +00002110 PyObject *callable;
2111 PyObject *argtup;
2112 PyObject *state = NULL;
2113 PyObject *listitems = NULL;
2114 PyObject *dictitems = NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002115
Tim Peters71fcda52003-02-14 23:05:28 +00002116 int use_newobj = self->proto >= 2;
2117
2118 static char reduce = REDUCE;
2119 static char build = BUILD;
2120 static char newobj = NEWOBJ;
2121
2122 if (! PyArg_UnpackTuple(args, "save_reduce", 2, 5,
2123 &callable,
2124 &argtup,
2125 &state,
2126 &listitems,
2127 &dictitems))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002128 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002129
Tim Peters71fcda52003-02-14 23:05:28 +00002130 if (state == Py_None)
2131 state = NULL;
2132 if (listitems == Py_None)
2133 listitems = NULL;
2134 if (dictitems == Py_None)
2135 dictitems = NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002136
Tim Peters71fcda52003-02-14 23:05:28 +00002137 /* Protocol 2 special case: if callable's name is __newobj__, use
2138 * NEWOBJ. This consumes a lot of code.
2139 */
2140 if (use_newobj) {
2141 PyObject *temp = PyObject_GetAttr(callable, __name___str);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002142
Tim Peters71fcda52003-02-14 23:05:28 +00002143 if (temp == NULL) {
2144 PyErr_Clear();
2145 use_newobj = 0;
2146 }
2147 else {
2148 use_newobj = PyString_Check(temp) &&
2149 strcmp(PyString_AS_STRING(temp),
2150 "__newobj__") == 0;
2151 Py_DECREF(temp);
2152 }
2153 }
2154 if (use_newobj) {
2155 PyObject *cls;
2156 PyObject *newargtup;
2157 int n, i;
2158
2159 /* Sanity checks. */
2160 n = PyTuple_Size(argtup);
2161 if (n < 1) {
2162 PyErr_SetString(PicklingError, "__newobj__ arglist "
2163 "is empty");
2164 return -1;
2165 }
2166
2167 cls = PyTuple_GET_ITEM(argtup, 0);
2168 if (! PyObject_HasAttrString(cls, "__new__")) {
2169 PyErr_SetString(PicklingError, "args[0] from "
2170 "__newobj__ args has no __new__");
2171 return -1;
2172 }
2173
2174 /* XXX How could ob be NULL? */
2175 if (ob != NULL) {
2176 PyObject *ob_dot_class;
2177
2178 ob_dot_class = PyObject_GetAttr(ob, __class___str);
2179 if (ob_dot_class == NULL)
2180 PyErr_Clear();
2181 i = ob_dot_class != cls; /* true iff a problem */
2182 Py_XDECREF(ob_dot_class);
2183 if (i) {
2184 PyErr_SetString(PicklingError, "args[0] from "
2185 "__newobj__ args has the wrong class");
2186 return -1;
2187 }
2188 }
2189
2190 /* Save the class and its __new__ arguments. */
2191 if (save(self, cls, 0) < 0)
2192 return -1;
2193
2194 newargtup = PyTuple_New(n-1); /* argtup[1:] */
2195 if (newargtup == NULL)
2196 return -1;
2197 for (i = 1; i < n; ++i) {
2198 PyObject *temp = PyTuple_GET_ITEM(argtup, i);
2199 Py_INCREF(temp);
2200 PyTuple_SET_ITEM(newargtup, i-1, temp);
2201 }
2202 i = save(self, newargtup, 0) < 0;
2203 Py_DECREF(newargtup);
2204 if (i < 0)
2205 return -1;
2206
2207 /* Add NEWOBJ opcode. */
2208 if (self->write_func(self, &newobj, 1) < 0)
2209 return -1;
2210 }
2211 else {
2212 /* Not using NEWOBJ. */
2213 if (save(self, callable, 0) < 0 ||
2214 save(self, argtup, 0) < 0 ||
2215 self->write_func(self, &reduce, 1) < 0)
2216 return -1;
2217 }
2218
2219 /* Memoize. */
2220 /* XXX How can ob be NULL? */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002221 if (ob != NULL) {
2222 if (state && !PyDict_Check(state)) {
2223 if (put2(self, ob) < 0)
2224 return -1;
2225 }
Tim Peters71fcda52003-02-14 23:05:28 +00002226 else if (put(self, ob) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002227 return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002228 }
Tim Peters84e87f32001-03-17 04:50:51 +00002229
Guido van Rossum60456fd1997-04-09 17:36:32 +00002230
Tim Peters71fcda52003-02-14 23:05:28 +00002231 if (listitems && batch_list(self, listitems) < 0)
2232 return -1;
2233
2234 if (dictitems && batch_dict(self, dictitems) < 0)
2235 return -1;
2236
2237 if (state) {
2238 if (save(self, state, 0) < 0 ||
2239 self->write_func(self, &build, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002240 return -1;
2241 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002242
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002243 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002244}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002245
Guido van Rossum60456fd1997-04-09 17:36:32 +00002246static int
Tim Peters1d63c9f2003-02-02 20:29:39 +00002247save(Picklerobject *self, PyObject *args, int pers_save)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002248{
2249 PyTypeObject *type;
Tim Peters71fcda52003-02-14 23:05:28 +00002250 PyObject *py_ob_id = 0, *__reduce__ = 0, *t = 0;
2251 PyObject *arg_tup;
2252 int res = -1;
2253 int tmp, size;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002254
Martin v. Löwis5a395302002-08-04 08:20:23 +00002255 if (self->nesting++ > Py_GetRecursionLimit()){
2256 PyErr_SetString(PyExc_RuntimeError,
2257 "maximum recursion depth exceeded");
2258 goto finally;
2259 }
2260
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002261 if (!pers_save && self->pers_func) {
2262 if ((tmp = save_pers(self, args, self->pers_func)) != 0) {
2263 res = tmp;
2264 goto finally;
2265 }
2266 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002267
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002268 if (args == Py_None) {
2269 res = save_none(self, args);
2270 goto finally;
2271 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002272
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002273 type = args->ob_type;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002274
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002275 switch (type->tp_name[0]) {
Guido van Rossum77f6a652002-04-03 22:41:51 +00002276 case 'b':
2277 if (args == Py_False || args == Py_True) {
2278 res = save_bool(self, args);
2279 goto finally;
2280 }
2281 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002282 case 'i':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002283 if (type == &PyInt_Type) {
2284 res = save_int(self, args);
2285 goto finally;
2286 }
2287 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002288
Guido van Rossum60456fd1997-04-09 17:36:32 +00002289 case 'l':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002290 if (type == &PyLong_Type) {
2291 res = save_long(self, args);
2292 goto finally;
2293 }
2294 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002295
Guido van Rossum60456fd1997-04-09 17:36:32 +00002296 case 'f':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002297 if (type == &PyFloat_Type) {
2298 res = save_float(self, args);
2299 goto finally;
2300 }
2301 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002302
Guido van Rossum60456fd1997-04-09 17:36:32 +00002303 case 't':
Tim Peters1d63c9f2003-02-02 20:29:39 +00002304 if (type == &PyTuple_Type && PyTuple_Size(args) == 0) {
2305 res = save_tuple(self, args);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002306 goto finally;
2307 }
2308 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002309
Guido van Rossum60456fd1997-04-09 17:36:32 +00002310 case 's':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002311 if ((type == &PyString_Type) && (PyString_GET_SIZE(args) < 2)) {
2312 res = save_string(self, args, 0);
2313 goto finally;
2314 }
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00002315
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002316#ifdef Py_USING_UNICODE
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00002317 case 'u':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002318 if ((type == &PyUnicode_Type) && (PyString_GET_SIZE(args) < 2)) {
2319 res = save_unicode(self, args, 0);
2320 goto finally;
2321 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002322#endif
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002323 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002324
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002325 if (args->ob_refcnt > 1) {
Tim Peterscba30e22003-02-01 06:24:36 +00002326 if (!( py_ob_id = PyLong_FromVoidPtr(args)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002327 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002328
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002329 if (PyDict_GetItem(self->memo, py_ob_id)) {
2330 if (get(self, py_ob_id) < 0)
2331 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002332
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002333 res = 0;
2334 goto finally;
2335 }
2336 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002337
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002338 switch (type->tp_name[0]) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00002339 case 's':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002340 if (type == &PyString_Type) {
2341 res = save_string(self, args, 1);
2342 goto finally;
2343 }
2344 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002345
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002346#ifdef Py_USING_UNICODE
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00002347 case 'u':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002348 if (type == &PyUnicode_Type) {
2349 res = save_unicode(self, args, 1);
2350 goto finally;
2351 }
2352 break;
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002353#endif
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00002354
Guido van Rossum60456fd1997-04-09 17:36:32 +00002355 case 't':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002356 if (type == &PyTuple_Type) {
2357 res = save_tuple(self, args);
2358 goto finally;
2359 }
2360 if (type == &PyType_Type) {
2361 res = save_global(self, args, NULL);
2362 goto finally;
2363 }
2364 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002365
Guido van Rossum60456fd1997-04-09 17:36:32 +00002366 case 'l':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002367 if (type == &PyList_Type) {
2368 res = save_list(self, args);
2369 goto finally;
2370 }
2371 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002372
2373 case 'd':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002374 if (type == &PyDict_Type) {
2375 res = save_dict(self, args);
2376 goto finally;
2377 }
2378 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002379
2380 case 'i':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002381 if (type == &PyInstance_Type) {
2382 res = save_inst(self, args);
2383 goto finally;
2384 }
2385 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002386
2387 case 'c':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002388 if (type == &PyClass_Type) {
2389 res = save_global(self, args, NULL);
2390 goto finally;
2391 }
2392 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002393
2394 case 'f':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002395 if (type == &PyFunction_Type) {
2396 res = save_global(self, args, NULL);
2397 goto finally;
2398 }
2399 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002400
2401 case 'b':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002402 if (type == &PyCFunction_Type) {
2403 res = save_global(self, args, NULL);
2404 goto finally;
2405 }
2406 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002407
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002408 if (!pers_save && self->inst_pers_func) {
2409 if ((tmp = save_pers(self, args, self->inst_pers_func)) != 0) {
2410 res = tmp;
2411 goto finally;
2412 }
2413 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002414
Jeremy Hylton39c61162002-07-16 19:47:43 +00002415 if (PyType_IsSubtype(type, &PyType_Type)) {
2416 res = save_global(self, args, NULL);
2417 goto finally;
2418 }
2419
Guido van Rossumb289b872003-02-19 01:45:13 +00002420 /* Get a reduction callable, and call it. This may come from
2421 * copy_reg.dispatch_table, the object's __reduce_ex__ method,
2422 * or the object's __reduce__ method.
Tim Peters71fcda52003-02-14 23:05:28 +00002423 */
Tim Peters5aa3da62003-02-13 21:03:57 +00002424 __reduce__ = PyDict_GetItem(dispatch_table, (PyObject *)type);
2425 if (__reduce__ != NULL) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002426 Py_INCREF(__reduce__);
Guido van Rossumb289b872003-02-19 01:45:13 +00002427 Py_INCREF(args);
2428 ARG_TUP(self, args);
2429 if (self->arg) {
2430 t = PyObject_Call(__reduce__, self->arg, NULL);
2431 FREE_ARG_TUP(self);
2432 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002433 }
2434 else {
Guido van Rossumb289b872003-02-19 01:45:13 +00002435 /* Check for a __reduce_ex__ method. */
2436 __reduce__ = PyObject_GetAttr(args, __reduce_ex___str);
2437 if (__reduce__ != NULL) {
2438 t = PyInt_FromLong(self->proto);
2439 if (t != NULL) {
2440 ARG_TUP(self, t);
2441 t = NULL;
2442 if (self->arg) {
2443 t = PyObject_Call(__reduce__,
2444 self->arg, NULL);
2445 FREE_ARG_TUP(self);
2446 }
2447 }
2448 }
2449 else {
Tim Peters5aa3da62003-02-13 21:03:57 +00002450 PyErr_Clear();
Guido van Rossumb289b872003-02-19 01:45:13 +00002451 /* Check for a __reduce__ method. */
2452 __reduce__ = PyObject_GetAttr(args, __reduce___str);
2453 if (__reduce__ != NULL) {
2454 t = PyObject_Call(__reduce__,
2455 empty_tuple, NULL);
2456 }
2457 else {
2458 PyErr_SetObject(UnpickleableError, args);
2459 goto finally;
2460 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002461 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002462 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002463
Tim Peters71fcda52003-02-14 23:05:28 +00002464 if (t == NULL)
2465 goto finally;
Tim Peters84e87f32001-03-17 04:50:51 +00002466
Tim Peters71fcda52003-02-14 23:05:28 +00002467 if (PyString_Check(t)) {
2468 res = save_global(self, args, t);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002469 goto finally;
2470 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002471
Tim Peters71fcda52003-02-14 23:05:28 +00002472 if (! PyTuple_Check(t)) {
2473 cPickle_ErrFormat(PicklingError, "Value returned by "
2474 "%s must be string or tuple",
2475 "O", __reduce__);
2476 goto finally;
2477 }
2478
2479 size = PyTuple_Size(t);
2480 if (size < 2 || size > 5) {
2481 cPickle_ErrFormat(PicklingError, "tuple returned by "
2482 "%s must contain 2 through 5 elements",
2483 "O", __reduce__);
2484 goto finally;
2485 }
2486
2487 arg_tup = PyTuple_GET_ITEM(t, 1);
2488 if (!(PyTuple_Check(arg_tup) || arg_tup == Py_None)) {
2489 cPickle_ErrFormat(PicklingError, "Second element of "
2490 "tuple returned by %s must be a tuple",
2491 "O", __reduce__);
2492 goto finally;
2493 }
2494
2495 res = save_reduce(self, t, args);
Guido van Rossum60456fd1997-04-09 17:36:32 +00002496
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002497 finally:
Martin v. Löwis5a395302002-08-04 08:20:23 +00002498 self->nesting--;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002499 Py_XDECREF(py_ob_id);
2500 Py_XDECREF(__reduce__);
2501 Py_XDECREF(t);
Tim Peters84e87f32001-03-17 04:50:51 +00002502
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002503 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002504}
2505
2506
2507static int
Tim Peterscba30e22003-02-01 06:24:36 +00002508dump(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002509{
2510 static char stop = STOP;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002511
Tim Peters4190fb82003-02-02 16:09:05 +00002512 if (self->proto >= 2) {
2513 char bytes[2];
2514
2515 bytes[0] = PROTO;
Tim Peters87482ea2003-02-02 16:16:30 +00002516 assert(self->proto >= 0 && self->proto < 256);
2517 bytes[1] = (char)self->proto;
Tim Peters4190fb82003-02-02 16:09:05 +00002518 if (self->write_func(self, bytes, 2) < 0)
2519 return -1;
2520 }
2521
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002522 if (save(self, args, 0) < 0)
2523 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002524
Tim Peters4190fb82003-02-02 16:09:05 +00002525 if (self->write_func(self, &stop, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002526 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002527
Tim Peters4190fb82003-02-02 16:09:05 +00002528 if (self->write_func(self, NULL, 0) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002529 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002530
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002531 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002532}
2533
2534static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00002535Pickle_clear_memo(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002536{
Tim Peterscba30e22003-02-01 06:24:36 +00002537 if (self->memo)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002538 PyDict_Clear(self->memo);
2539 Py_INCREF(Py_None);
2540 return Py_None;
2541}
2542
2543static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00002544Pickle_getvalue(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002545{
2546 int l, i, rsize, ssize, clear=1, lm;
2547 long ik;
2548 PyObject *k, *r;
2549 char *s, *p, *have_get;
2550 Pdata *data;
2551
2552 /* Can be called by Python code or C code */
Tim Peterscba30e22003-02-01 06:24:36 +00002553 if (args && !PyArg_ParseTuple(args, "|i:getvalue", &clear))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002554 return NULL;
2555
2556 /* Check to make sure we are based on a list */
2557 if (! Pdata_Check(self->file)) {
2558 PyErr_SetString(PicklingError,
2559 "Attempt to getvalue() a non-list-based pickler");
2560 return NULL;
2561 }
2562
2563 /* flush write buffer */
2564 if (write_other(self, NULL, 0) < 0) return NULL;
2565
2566 data=(Pdata*)self->file;
2567 l=data->length;
2568
2569 /* set up an array to hold get/put status */
Tim Petersac5687a2003-02-02 18:08:34 +00002570 lm = PyDict_Size(self->memo);
2571 if (lm < 0) return NULL;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002572 lm++;
Tim Petersac5687a2003-02-02 18:08:34 +00002573 have_get = malloc(lm);
2574 if (have_get == NULL) return PyErr_NoMemory();
2575 memset(have_get, 0, lm);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002576
2577 /* Scan for gets. */
Tim Petersac5687a2003-02-02 18:08:34 +00002578 for (rsize = 0, i = l; --i >= 0; ) {
2579 k = data->data[i];
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002580
Tim Petersac5687a2003-02-02 18:08:34 +00002581 if (PyString_Check(k))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002582 rsize += PyString_GET_SIZE(k);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002583
2584 else if (PyInt_Check(k)) { /* put */
Tim Petersac5687a2003-02-02 18:08:34 +00002585 ik = PyInt_AS_LONG((PyIntObject*)k);
2586 if (ik >= lm || ik == 0) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002587 PyErr_SetString(PicklingError,
2588 "Invalid get data");
2589 return NULL;
2590 }
Tim Petersac5687a2003-02-02 18:08:34 +00002591 if (have_get[ik]) /* with matching get */
2592 rsize += ik < 256 ? 2 : 5;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002593 }
2594
2595 else if (! (PyTuple_Check(k) &&
2596 PyTuple_GET_SIZE(k) == 2 &&
Tim Petersac5687a2003-02-02 18:08:34 +00002597 PyInt_Check((k = PyTuple_GET_ITEM(k, 0))))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002598 ) {
2599 PyErr_SetString(PicklingError,
2600 "Unexpected data in internal list");
2601 return NULL;
2602 }
2603
2604 else { /* put */
Tim Petersac5687a2003-02-02 18:08:34 +00002605 ik = PyInt_AS_LONG((PyIntObject *)k);
2606 if (ik >= lm || ik == 0) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002607 PyErr_SetString(PicklingError,
2608 "Invalid get data");
2609 return NULL;
2610 }
Tim Petersac5687a2003-02-02 18:08:34 +00002611 have_get[ik] = 1;
2612 rsize += ik < 256 ? 2 : 5;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002613 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002614 }
2615
2616 /* Now generate the result */
Tim Petersac5687a2003-02-02 18:08:34 +00002617 r = PyString_FromStringAndSize(NULL, rsize);
2618 if (r == NULL) goto err;
2619 s = PyString_AS_STRING((PyStringObject *)r);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002620
Tim Petersac5687a2003-02-02 18:08:34 +00002621 for (i = 0; i < l; i++) {
2622 k = data->data[i];
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002623
2624 if (PyString_Check(k)) {
Tim Petersac5687a2003-02-02 18:08:34 +00002625 ssize = PyString_GET_SIZE(k);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002626 if (ssize) {
Tim Petersac5687a2003-02-02 18:08:34 +00002627 p=PyString_AS_STRING((PyStringObject *)k);
2628 while (--ssize >= 0)
2629 *s++ = *p++;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002630 }
2631 }
2632
2633 else if (PyTuple_Check(k)) { /* get */
Tim Petersac5687a2003-02-02 18:08:34 +00002634 ik = PyInt_AS_LONG((PyIntObject *)
2635 PyTuple_GET_ITEM(k, 0));
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002636 if (ik < 256) {
2637 *s++ = BINGET;
2638 *s++ = (int)(ik & 0xff);
2639 }
2640 else {
2641 *s++ = LONG_BINGET;
2642 *s++ = (int)(ik & 0xff);
2643 *s++ = (int)((ik >> 8) & 0xff);
2644 *s++ = (int)((ik >> 16) & 0xff);
2645 *s++ = (int)((ik >> 24) & 0xff);
2646 }
2647 }
2648
2649 else { /* put */
Tim Petersac5687a2003-02-02 18:08:34 +00002650 ik = PyInt_AS_LONG((PyIntObject*)k);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002651
2652 if (have_get[ik]) { /* with matching get */
2653 if (ik < 256) {
2654 *s++ = BINPUT;
2655 *s++ = (int)(ik & 0xff);
2656 }
2657 else {
2658 *s++ = LONG_BINPUT;
2659 *s++ = (int)(ik & 0xff);
2660 *s++ = (int)((ik >> 8) & 0xff);
2661 *s++ = (int)((ik >> 16) & 0xff);
2662 *s++ = (int)((ik >> 24) & 0xff);
2663 }
2664 }
2665 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002666 }
2667
2668 if (clear) {
2669 PyDict_Clear(self->memo);
Tim Petersac5687a2003-02-02 18:08:34 +00002670 Pdata_clear(data, 0);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002671 }
2672
2673 free(have_get);
2674 return r;
2675 err:
2676 free(have_get);
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00002677 return NULL;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002678}
2679
2680static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00002681Pickler_dump(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002682{
2683 PyObject *ob;
2684 int get=0;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002685
Tim Peterscba30e22003-02-01 06:24:36 +00002686 if (!( PyArg_ParseTuple(args, "O|i:dump", &ob, &get)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002687 return NULL;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002688
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002689 if (dump(self, ob) < 0)
2690 return NULL;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002691
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002692 if (get) return Pickle_getvalue(self, NULL);
Guido van Rossum053b8df1998-11-25 16:18:00 +00002693
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002694 /* XXX Why does dump() return self? */
2695 Py_INCREF(self);
2696 return (PyObject*)self;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002697}
2698
2699
Tim Peterscba30e22003-02-01 06:24:36 +00002700static struct PyMethodDef Pickler_methods[] =
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002701{
Neal Norwitzb0493252002-03-31 14:44:22 +00002702 {"dump", (PyCFunction)Pickler_dump, METH_VARARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00002703 PyDoc_STR("dump(object) -- "
2704 "Write an object in pickle format to the object's pickle stream")},
Fred Drake0ebacc82002-05-01 20:36:39 +00002705 {"clear_memo", (PyCFunction)Pickle_clear_memo, METH_NOARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00002706 PyDoc_STR("clear_memo() -- Clear the picklers memo")},
Neal Norwitzb0493252002-03-31 14:44:22 +00002707 {"getvalue", (PyCFunction)Pickle_getvalue, METH_VARARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00002708 PyDoc_STR("getvalue() -- Finish picking a list-based pickle")},
Guido van Rossum60456fd1997-04-09 17:36:32 +00002709 {NULL, NULL} /* sentinel */
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002710};
2711
2712
2713static Picklerobject *
Tim Peters5bd2a792003-02-01 16:45:06 +00002714newPicklerobject(PyObject *file, int proto)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002715{
2716 Picklerobject *self;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002717
Tim Peters5bd2a792003-02-01 16:45:06 +00002718 if (proto < 0)
Tim Peters8587b3c2003-02-13 15:44:41 +00002719 proto = HIGHEST_PROTOCOL;
2720 if (proto > HIGHEST_PROTOCOL) {
Tim Peters5bd2a792003-02-01 16:45:06 +00002721 PyErr_Format(PyExc_ValueError, "pickle protocol %d asked for; "
2722 "the highest available protocol is %d",
Tim Peters8587b3c2003-02-13 15:44:41 +00002723 proto, HIGHEST_PROTOCOL);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002724 return NULL;
Tim Peters5bd2a792003-02-01 16:45:06 +00002725 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002726
Tim Peters5bd2a792003-02-01 16:45:06 +00002727 self = PyObject_New(Picklerobject, &Picklertype);
2728 if (self == NULL)
2729 return NULL;
2730 self->proto = proto;
2731 self->bin = proto > 0;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002732 self->fp = NULL;
2733 self->write = NULL;
2734 self->memo = NULL;
2735 self->arg = NULL;
2736 self->pers_func = NULL;
2737 self->inst_pers_func = NULL;
2738 self->write_buf = NULL;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002739 self->fast = 0;
Martin v. Löwis5a395302002-08-04 08:20:23 +00002740 self->nesting = 0;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002741 self->fast_container = 0;
2742 self->fast_memo = NULL;
2743 self->buf_size = 0;
2744 self->dispatch_table = NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002745
Tim Peters5bd2a792003-02-01 16:45:06 +00002746 self->file = NULL;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002747 if (file)
2748 Py_INCREF(file);
Tim Peters5bd2a792003-02-01 16:45:06 +00002749 else {
2750 file = Pdata_New();
2751 if (file == NULL)
2752 goto err;
2753 }
2754 self->file = file;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002755
Tim Peterscba30e22003-02-01 06:24:36 +00002756 if (!( self->memo = PyDict_New()))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002757 goto err;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002758
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002759 if (PyFile_Check(file)) {
2760 self->fp = PyFile_AsFile(file);
2761 if (self->fp == NULL) {
Tim Peters5bd2a792003-02-01 16:45:06 +00002762 PyErr_SetString(PyExc_ValueError,
2763 "I/O operation on closed file");
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002764 goto err;
2765 }
2766 self->write_func = write_file;
Guido van Rossumc91fcaa1999-03-29 20:00:14 +00002767 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002768 else if (PycStringIO_OutputCheck(file)) {
2769 self->write_func = write_cStringIO;
2770 }
2771 else if (file == Py_None) {
2772 self->write_func = write_none;
2773 }
2774 else {
2775 self->write_func = write_other;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002776
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002777 if (! Pdata_Check(file)) {
2778 self->write = PyObject_GetAttr(file, write_str);
2779 if (!self->write) {
2780 PyErr_Clear();
Tim Peterscba30e22003-02-01 06:24:36 +00002781 PyErr_SetString(PyExc_TypeError,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002782 "argument must have 'write' "
2783 "attribute");
2784 goto err;
2785 }
2786 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002787
Tim Peters5bd2a792003-02-01 16:45:06 +00002788 self->write_buf = (char *)PyMem_Malloc(WRITE_BUF_SIZE);
2789 if (self->write_buf == NULL) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002790 PyErr_NoMemory();
2791 goto err;
2792 }
2793 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002794
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002795 if (PyEval_GetRestricted()) {
2796 /* Restricted execution, get private tables */
Tim Peters5b7da392003-02-04 00:21:07 +00002797 PyObject *m = PyImport_Import(copy_reg_str);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002798
Tim Peters5b7da392003-02-04 00:21:07 +00002799 if (m == NULL)
2800 goto err;
2801 self->dispatch_table = PyObject_GetAttr(m, dispatch_table_str);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002802 Py_DECREF(m);
Tim Peters5b7da392003-02-04 00:21:07 +00002803 if (self->dispatch_table == NULL)
2804 goto err;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002805 }
2806 else {
Tim Peters5b7da392003-02-04 00:21:07 +00002807 self->dispatch_table = dispatch_table;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002808 Py_INCREF(dispatch_table);
2809 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002810
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002811 return self;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002812
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002813 err:
Tim Peters5bd2a792003-02-01 16:45:06 +00002814 Py_DECREF(self);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002815 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002816}
2817
2818
2819static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00002820get_Pickler(PyObject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002821{
2822 PyObject *file = NULL;
Tim Peters5bd2a792003-02-01 16:45:06 +00002823 int proto = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002824
Tim Peters92c8bb32003-02-13 23:00:26 +00002825 /* XXX
2826 * The documented signature is Pickler(file, proto=0), but this
2827 * accepts Pickler() and Pickler(integer) too. The meaning then
2828 * is clear as mud, undocumented, and not supported by pickle.py.
2829 * I'm told Zope uses this, but I haven't traced into this code
2830 * far enough to figure out what it means.
Tim Peters5bd2a792003-02-01 16:45:06 +00002831 */
2832 if (!PyArg_ParseTuple(args, "|i:Pickler", &proto)) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002833 PyErr_Clear();
Tim Peters5bd2a792003-02-01 16:45:06 +00002834 proto = 0;
2835 if (!PyArg_ParseTuple(args, "O|i:Pickler", &file, &proto))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002836 return NULL;
2837 }
Tim Peters5bd2a792003-02-01 16:45:06 +00002838 return (PyObject *)newPicklerobject(file, proto);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002839}
2840
2841
2842static void
Tim Peterscba30e22003-02-01 06:24:36 +00002843Pickler_dealloc(Picklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002844{
2845 Py_XDECREF(self->write);
2846 Py_XDECREF(self->memo);
2847 Py_XDECREF(self->fast_memo);
2848 Py_XDECREF(self->arg);
2849 Py_XDECREF(self->file);
2850 Py_XDECREF(self->pers_func);
2851 Py_XDECREF(self->inst_pers_func);
2852 Py_XDECREF(self->dispatch_table);
Tim Peters5bd2a792003-02-01 16:45:06 +00002853 PyMem_Free(self->write_buf);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002854 PyObject_Del(self);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002855}
2856
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002857static PyObject *
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00002858Pickler_get_pers_func(Picklerobject *p)
2859{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002860 if (p->pers_func == NULL)
2861 PyErr_SetString(PyExc_AttributeError, "persistent_id");
2862 else
2863 Py_INCREF(p->pers_func);
2864 return p->pers_func;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002865}
2866
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00002867static int
2868Pickler_set_pers_func(Picklerobject *p, PyObject *v)
2869{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002870 if (v == NULL) {
2871 PyErr_SetString(PyExc_TypeError,
2872 "attribute deletion is not supported");
2873 return -1;
2874 }
2875 Py_XDECREF(p->pers_func);
2876 Py_INCREF(v);
2877 p->pers_func = v;
2878 return 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002879}
2880
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00002881static int
2882Pickler_set_inst_pers_func(Picklerobject *p, PyObject *v)
2883{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002884 if (v == NULL) {
2885 PyErr_SetString(PyExc_TypeError,
2886 "attribute deletion is not supported");
2887 return -1;
2888 }
2889 Py_XDECREF(p->inst_pers_func);
2890 Py_INCREF(v);
2891 p->inst_pers_func = v;
2892 return 0;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00002893}
2894
2895static PyObject *
2896Pickler_get_memo(Picklerobject *p)
2897{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002898 if (p->memo == NULL)
2899 PyErr_SetString(PyExc_AttributeError, "memo");
2900 else
2901 Py_INCREF(p->memo);
2902 return p->memo;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00002903}
2904
2905static int
2906Pickler_set_memo(Picklerobject *p, PyObject *v)
2907{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002908 if (v == NULL) {
2909 PyErr_SetString(PyExc_TypeError,
2910 "attribute deletion is not supported");
2911 return -1;
2912 }
2913 if (!PyDict_Check(v)) {
2914 PyErr_SetString(PyExc_TypeError, "memo must be a dictionary");
2915 return -1;
2916 }
2917 Py_XDECREF(p->memo);
2918 Py_INCREF(v);
2919 p->memo = v;
2920 return 0;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00002921}
2922
2923static PyObject *
2924Pickler_get_error(Picklerobject *p)
2925{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002926 /* why is this an attribute on the Pickler? */
2927 Py_INCREF(PicklingError);
2928 return PicklingError;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00002929}
2930
2931static PyMemberDef Pickler_members[] = {
2932 {"binary", T_INT, offsetof(Picklerobject, bin)},
2933 {"fast", T_INT, offsetof(Picklerobject, fast)},
Jeremy Hylton3eb46f32001-10-16 17:10:49 +00002934 {NULL}
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00002935};
2936
2937static PyGetSetDef Pickler_getsets[] = {
Tim Peterscba30e22003-02-01 06:24:36 +00002938 {"persistent_id", (getter)Pickler_get_pers_func,
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00002939 (setter)Pickler_set_pers_func},
2940 {"inst_persistent_id", NULL, (setter)Pickler_set_inst_pers_func},
2941 {"memo", (getter)Pickler_get_memo, (setter)Pickler_set_memo},
Jeremy Hylton3eb46f32001-10-16 17:10:49 +00002942 {"PicklingError", (getter)Pickler_get_error, NULL},
2943 {NULL}
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00002944};
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002945
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002946PyDoc_STRVAR(Picklertype__doc__,
2947"Objects that know how to pickle objects\n");
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002948
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002949static PyTypeObject Picklertype = {
2950 PyObject_HEAD_INIT(NULL)
Guido van Rossum60456fd1997-04-09 17:36:32 +00002951 0, /*ob_size*/
Guido van Rossum14648392001-12-08 18:02:58 +00002952 "cPickle.Pickler", /*tp_name*/
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00002953 sizeof(Picklerobject), /*tp_basicsize*/
2954 0,
2955 (destructor)Pickler_dealloc, /* tp_dealloc */
2956 0, /* tp_print */
2957 0, /* tp_getattr */
2958 0, /* tp_setattr */
2959 0, /* tp_compare */
2960 0, /* tp_repr */
2961 0, /* tp_as_number */
2962 0, /* tp_as_sequence */
2963 0, /* tp_as_mapping */
2964 0, /* tp_hash */
2965 0, /* tp_call */
2966 0, /* tp_str */
Jason Tishlerfb8595d2003-01-06 12:41:26 +00002967 PyObject_GenericGetAttr, /* tp_getattro */
2968 PyObject_GenericSetAttr, /* tp_setattro */
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00002969 0, /* tp_as_buffer */
2970 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
2971 Picklertype__doc__, /* tp_doc */
2972 0, /* tp_traverse */
2973 0, /* tp_clear */
2974 0, /* tp_richcompare */
2975 0, /* tp_weaklistoffset */
2976 0, /* tp_iter */
2977 0, /* tp_iternext */
2978 Pickler_methods, /* tp_methods */
2979 Pickler_members, /* tp_members */
2980 Pickler_getsets, /* tp_getset */
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002981};
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002982
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002983static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00002984find_class(PyObject *py_module_name, PyObject *py_global_name, PyObject *fc)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002985{
2986 PyObject *global = 0, *module;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002987
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002988 if (fc) {
2989 if (fc==Py_None) {
Tim Peters92c8bb32003-02-13 23:00:26 +00002990 PyErr_SetString(UnpicklingError, "Global and instance "
2991 "pickles are not supported.");
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002992 return NULL;
2993 }
Tim Peterscba30e22003-02-01 06:24:36 +00002994 return PyObject_CallFunction(fc, "OO", py_module_name,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002995 py_global_name);
2996 }
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00002997
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002998 module = PySys_GetObject("modules");
2999 if (module == NULL)
3000 return NULL;
Jeremy Hyltond1055231998-08-11 19:52:51 +00003001
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003002 module = PyDict_GetItem(module, py_module_name);
3003 if (module == NULL) {
3004 module = PyImport_Import(py_module_name);
3005 if (!module)
3006 return NULL;
3007 global = PyObject_GetAttr(module, py_global_name);
3008 Py_DECREF(module);
3009 }
3010 else
3011 global = PyObject_GetAttr(module, py_global_name);
3012 return global;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003013}
3014
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003015static int
Tim Peterscba30e22003-02-01 06:24:36 +00003016marker(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003017{
3018 if (self->num_marks < 1) {
3019 PyErr_SetString(UnpicklingError, "could not find MARK");
3020 return -1;
3021 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003022
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003023 return self->marks[--self->num_marks];
Guido van Rossum60456fd1997-04-09 17:36:32 +00003024}
3025
Tim Peters84e87f32001-03-17 04:50:51 +00003026
Guido van Rossum60456fd1997-04-09 17:36:32 +00003027static int
Tim Peterscba30e22003-02-01 06:24:36 +00003028load_none(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003029{
3030 PDATA_APPEND(self->stack, Py_None, -1);
3031 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003032}
3033
Guido van Rossumf9ffb031999-02-04 14:54:04 +00003034static int
Tim Peterscba30e22003-02-01 06:24:36 +00003035bad_readline(void)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003036{
3037 PyErr_SetString(UnpicklingError, "pickle data was truncated");
3038 return -1;
Guido van Rossumf9ffb031999-02-04 14:54:04 +00003039}
Guido van Rossum60456fd1997-04-09 17:36:32 +00003040
3041static int
Tim Peterscba30e22003-02-01 06:24:36 +00003042load_int(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003043{
3044 PyObject *py_int = 0;
3045 char *endptr, *s;
3046 int len, res = -1;
3047 long l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003048
Tim Peters0bc93f52003-02-02 18:29:33 +00003049 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003050 if (len < 2) return bad_readline();
3051 if (!( s=pystrndup(s,len))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003052
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003053 errno = 0;
3054 l = strtol(s, &endptr, 0);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003055
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003056 if (errno || (*endptr != '\n') || (endptr[1] != '\0')) {
3057 /* Hm, maybe we've got something long. Let's try reading
3058 it as a Python long object. */
3059 errno = 0;
3060 py_int = PyLong_FromString(s, NULL, 0);
3061 if (py_int == NULL) {
3062 PyErr_SetString(PyExc_ValueError,
3063 "could not convert string to int");
3064 goto finally;
3065 }
3066 }
3067 else {
Guido van Rossume2763392002-04-05 19:30:08 +00003068 if (len == 3 && (l == 0 || l == 1)) {
3069 if (!( py_int = PyBool_FromLong(l))) goto finally;
3070 }
3071 else {
3072 if (!( py_int = PyInt_FromLong(l))) goto finally;
3073 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003074 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003075
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003076 free(s);
3077 PDATA_PUSH(self->stack, py_int, -1);
3078 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003079
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003080 finally:
3081 free(s);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003082
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003083 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003084}
3085
Tim Peters3c67d792003-02-02 17:59:11 +00003086static int
3087load_bool(Unpicklerobject *self, PyObject *boolean)
3088{
3089 assert(boolean == Py_True || boolean == Py_False);
Tim Peterse0a39072003-02-03 15:45:56 +00003090 PDATA_APPEND(self->stack, boolean, -1);
Tim Peters3c67d792003-02-02 17:59:11 +00003091 return 0;
3092}
3093
Tim Petersee1a53c2003-02-02 02:57:53 +00003094/* s contains x bytes of a little-endian integer. Return its value as a
3095 * C int. Obscure: when x is 1 or 2, this is an unsigned little-endian
3096 * int, but when x is 4 it's a signed one. This is an historical source
3097 * of x-platform bugs.
3098 */
Tim Peters84e87f32001-03-17 04:50:51 +00003099static long
Tim Petersee1a53c2003-02-02 02:57:53 +00003100calc_binint(char *s, int x)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003101{
3102 unsigned char c;
3103 int i;
3104 long l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003105
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003106 for (i = 0, l = 0L; i < x; i++) {
3107 c = (unsigned char)s[i];
3108 l |= (long)c << (i * 8);
3109 }
Tim Petersbfa18f72001-04-10 01:54:42 +00003110#if SIZEOF_LONG > 4
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003111 /* Unlike BININT1 and BININT2, BININT (more accurately BININT4)
3112 * is signed, so on a box with longs bigger than 4 bytes we need
3113 * to extend a BININT's sign bit to the full width.
3114 */
3115 if (x == 4 && l & (1L << 31))
3116 l |= (~0L) << 32;
Tim Petersbfa18f72001-04-10 01:54:42 +00003117#endif
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003118 return l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003119}
3120
3121
3122static int
Tim Peterscba30e22003-02-01 06:24:36 +00003123load_binintx(Unpicklerobject *self, char *s, int x)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003124{
3125 PyObject *py_int = 0;
3126 long l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003127
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003128 l = calc_binint(s, x);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003129
Tim Peterscba30e22003-02-01 06:24:36 +00003130 if (!( py_int = PyInt_FromLong(l)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003131 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003132
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003133 PDATA_PUSH(self->stack, py_int, -1);
3134 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003135}
3136
3137
3138static int
Tim Peterscba30e22003-02-01 06:24:36 +00003139load_binint(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003140{
3141 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003142
Tim Peters0bc93f52003-02-02 18:29:33 +00003143 if (self->read_func(self, &s, 4) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003144 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003145
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003146 return load_binintx(self, s, 4);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003147}
3148
3149
3150static int
Tim Peterscba30e22003-02-01 06:24:36 +00003151load_binint1(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003152{
3153 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003154
Tim Peters0bc93f52003-02-02 18:29:33 +00003155 if (self->read_func(self, &s, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003156 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003157
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003158 return load_binintx(self, s, 1);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003159}
3160
3161
3162static int
Tim Peterscba30e22003-02-01 06:24:36 +00003163load_binint2(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003164{
3165 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003166
Tim Peters0bc93f52003-02-02 18:29:33 +00003167 if (self->read_func(self, &s, 2) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003168 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003169
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003170 return load_binintx(self, s, 2);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003171}
Tim Peters84e87f32001-03-17 04:50:51 +00003172
Guido van Rossum60456fd1997-04-09 17:36:32 +00003173static int
Tim Peterscba30e22003-02-01 06:24:36 +00003174load_long(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003175{
3176 PyObject *l = 0;
3177 char *end, *s;
3178 int len, res = -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003179
Tim Peters0bc93f52003-02-02 18:29:33 +00003180 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003181 if (len < 2) return bad_readline();
3182 if (!( s=pystrndup(s,len))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003183
Tim Peterscba30e22003-02-01 06:24:36 +00003184 if (!( l = PyLong_FromString(s, &end, 0)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003185 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003186
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003187 free(s);
3188 PDATA_PUSH(self->stack, l, -1);
3189 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003190
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003191 finally:
3192 free(s);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003193
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003194 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003195}
3196
Tim Petersee1a53c2003-02-02 02:57:53 +00003197/* 'size' bytes contain the # of bytes of little-endian 256's-complement
3198 * data following.
3199 */
3200static int
3201load_counted_long(Unpicklerobject *self, int size)
3202{
3203 int i;
3204 char *nbytes;
3205 unsigned char *pdata;
3206 PyObject *along;
3207
3208 assert(size == 1 || size == 4);
3209 i = self->read_func(self, &nbytes, size);
3210 if (i < 0) return -1;
3211
3212 size = calc_binint(nbytes, size);
3213 if (size < 0) {
3214 /* Corrupt or hostile pickle -- we never write one like
3215 * this.
3216 */
Tim Peters0c7c48e2003-02-03 22:07:24 +00003217 PyErr_SetString(UnpicklingError, "LONG pickle has negative "
Tim Petersee1a53c2003-02-02 02:57:53 +00003218 "byte count");
3219 return -1;
3220 }
3221
3222 if (size == 0)
3223 along = PyLong_FromLong(0L);
3224 else {
3225 /* Read the raw little-endian bytes & convert. */
Neal Norwitz529baf22003-02-02 17:08:33 +00003226 i = self->read_func(self, (char **)&pdata, size);
Tim Petersee1a53c2003-02-02 02:57:53 +00003227 if (i < 0) return -1;
3228 along = _PyLong_FromByteArray(pdata, (size_t)size,
3229 1 /* little endian */, 1 /* signed */);
3230 }
3231 if (along == NULL)
3232 return -1;
3233 PDATA_PUSH(self->stack, along, -1);
3234 return 0;
3235}
Tim Peters84e87f32001-03-17 04:50:51 +00003236
Guido van Rossum60456fd1997-04-09 17:36:32 +00003237static int
Tim Peterscba30e22003-02-01 06:24:36 +00003238load_float(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003239{
3240 PyObject *py_float = 0;
3241 char *endptr, *s;
3242 int len, res = -1;
3243 double d;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003244
Tim Peters0bc93f52003-02-02 18:29:33 +00003245 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003246 if (len < 2) return bad_readline();
3247 if (!( s=pystrndup(s,len))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003248
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003249 errno = 0;
3250 d = strtod(s, &endptr);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003251
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003252 if (errno || (endptr[0] != '\n') || (endptr[1] != '\0')) {
3253 PyErr_SetString(PyExc_ValueError,
3254 "could not convert string to float");
3255 goto finally;
3256 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003257
Tim Peterscba30e22003-02-01 06:24:36 +00003258 if (!( py_float = PyFloat_FromDouble(d)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003259 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003260
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003261 free(s);
3262 PDATA_PUSH(self->stack, py_float, -1);
3263 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003264
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003265 finally:
3266 free(s);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003267
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003268 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003269}
3270
Guido van Rossum60456fd1997-04-09 17:36:32 +00003271static int
Tim Peterscba30e22003-02-01 06:24:36 +00003272load_binfloat(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003273{
Tim Peters9905b942003-03-20 20:53:32 +00003274 PyObject *py_float;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003275 double x;
3276 char *p;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003277
Tim Peters0bc93f52003-02-02 18:29:33 +00003278 if (self->read_func(self, &p, 8) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003279 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003280
Tim Peters9905b942003-03-20 20:53:32 +00003281 x = _PyFloat_Unpack8((unsigned char *)p, 0);
3282 if (x == -1.0 && PyErr_Occurred())
3283 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003284
Tim Peters9905b942003-03-20 20:53:32 +00003285 py_float = PyFloat_FromDouble(x);
3286 if (py_float == NULL)
3287 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003288
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003289 PDATA_PUSH(self->stack, py_float, -1);
3290 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003291}
Guido van Rossum60456fd1997-04-09 17:36:32 +00003292
3293static int
Tim Peterscba30e22003-02-01 06:24:36 +00003294load_string(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003295{
3296 PyObject *str = 0;
Martin v. Löwis8a8da792002-08-14 07:46:28 +00003297 int len, res = -1;
3298 char *s, *p;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003299
Tim Peters0bc93f52003-02-02 18:29:33 +00003300 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003301 if (len < 2) return bad_readline();
3302 if (!( s=pystrndup(s,len))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003303
Martin v. Löwis8a8da792002-08-14 07:46:28 +00003304
3305 /* Strip outermost quotes */
3306 while (s[len-1] <= ' ')
3307 len--;
3308 if(s[0]=='"' && s[len-1]=='"'){
3309 s[len-1] = '\0';
3310 p = s + 1 ;
3311 len -= 2;
3312 } else if(s[0]=='\'' && s[len-1]=='\''){
3313 s[len-1] = '\0';
3314 p = s + 1 ;
3315 len -= 2;
3316 } else
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003317 goto insecure;
3318 /********************************************/
Guido van Rossum9716aaa1997-12-08 15:15:16 +00003319
Martin v. Löwis8a8da792002-08-14 07:46:28 +00003320 str = PyString_DecodeEscape(p, len, NULL, 0, NULL);
3321 if (str) {
3322 PDATA_PUSH(self->stack, str, -1);
3323 res = 0;
3324 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003325 free(s);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003326 return res;
Tim Peters84e87f32001-03-17 04:50:51 +00003327
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003328 insecure:
3329 free(s);
3330 PyErr_SetString(PyExc_ValueError,"insecure string pickle");
3331 return -1;
Tim Peters84e87f32001-03-17 04:50:51 +00003332}
Guido van Rossum60456fd1997-04-09 17:36:32 +00003333
3334
3335static int
Tim Peterscba30e22003-02-01 06:24:36 +00003336load_binstring(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003337{
3338 PyObject *py_string = 0;
3339 long l;
3340 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003341
Tim Peters0bc93f52003-02-02 18:29:33 +00003342 if (self->read_func(self, &s, 4) < 0) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003343
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003344 l = calc_binint(s, 4);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003345
Tim Peters0bc93f52003-02-02 18:29:33 +00003346 if (self->read_func(self, &s, l) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003347 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003348
Tim Peterscba30e22003-02-01 06:24:36 +00003349 if (!( py_string = PyString_FromStringAndSize(s, l)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003350 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003351
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003352 PDATA_PUSH(self->stack, py_string, -1);
3353 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003354}
3355
3356
3357static int
Tim Peterscba30e22003-02-01 06:24:36 +00003358load_short_binstring(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003359{
3360 PyObject *py_string = 0;
3361 unsigned char l;
3362 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003363
Tim Peters0bc93f52003-02-02 18:29:33 +00003364 if (self->read_func(self, &s, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003365 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003366
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003367 l = (unsigned char)s[0];
Guido van Rossum60456fd1997-04-09 17:36:32 +00003368
Tim Peters0bc93f52003-02-02 18:29:33 +00003369 if (self->read_func(self, &s, l) < 0) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003370
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003371 if (!( py_string = PyString_FromStringAndSize(s, l))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003372
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003373 PDATA_PUSH(self->stack, py_string, -1);
3374 return 0;
Tim Peters84e87f32001-03-17 04:50:51 +00003375}
Guido van Rossum60456fd1997-04-09 17:36:32 +00003376
3377
Martin v. Löwis339d0f72001-08-17 18:39:25 +00003378#ifdef Py_USING_UNICODE
Guido van Rossum60456fd1997-04-09 17:36:32 +00003379static int
Tim Peterscba30e22003-02-01 06:24:36 +00003380load_unicode(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003381{
3382 PyObject *str = 0;
3383 int len, res = -1;
3384 char *s;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003385
Tim Peters0bc93f52003-02-02 18:29:33 +00003386 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003387 if (len < 1) return bad_readline();
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003388
Tim Peterscba30e22003-02-01 06:24:36 +00003389 if (!( str = PyUnicode_DecodeRawUnicodeEscape(s, len - 1, NULL)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003390 goto finally;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003391
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003392 PDATA_PUSH(self->stack, str, -1);
3393 return 0;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003394
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003395 finally:
3396 return res;
Tim Peters84e87f32001-03-17 04:50:51 +00003397}
Martin v. Löwis339d0f72001-08-17 18:39:25 +00003398#endif
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003399
3400
Martin v. Löwis339d0f72001-08-17 18:39:25 +00003401#ifdef Py_USING_UNICODE
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003402static int
Tim Peterscba30e22003-02-01 06:24:36 +00003403load_binunicode(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003404{
3405 PyObject *unicode;
3406 long l;
3407 char *s;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003408
Tim Peters0bc93f52003-02-02 18:29:33 +00003409 if (self->read_func(self, &s, 4) < 0) return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003410
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003411 l = calc_binint(s, 4);
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003412
Tim Peters0bc93f52003-02-02 18:29:33 +00003413 if (self->read_func(self, &s, l) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003414 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003415
Tim Peterscba30e22003-02-01 06:24:36 +00003416 if (!( unicode = PyUnicode_DecodeUTF8(s, l, NULL)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003417 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003418
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003419 PDATA_PUSH(self->stack, unicode, -1);
3420 return 0;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003421}
Martin v. Löwis339d0f72001-08-17 18:39:25 +00003422#endif
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003423
3424
3425static int
Tim Peterscba30e22003-02-01 06:24:36 +00003426load_tuple(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003427{
3428 PyObject *tup;
3429 int i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003430
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003431 if ((i = marker(self)) < 0) return -1;
3432 if (!( tup=Pdata_popTuple(self->stack, i))) return -1;
3433 PDATA_PUSH(self->stack, tup, -1);
3434 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003435}
3436
3437static int
Tim Peters1d63c9f2003-02-02 20:29:39 +00003438load_counted_tuple(Unpicklerobject *self, int len)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003439{
Tim Peters1d63c9f2003-02-02 20:29:39 +00003440 PyObject *tup = PyTuple_New(len);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003441
Tim Peters1d63c9f2003-02-02 20:29:39 +00003442 if (tup == NULL)
3443 return -1;
3444
3445 while (--len >= 0) {
3446 PyObject *element;
3447
3448 PDATA_POP(self->stack, element);
3449 if (element == NULL)
3450 return -1;
3451 PyTuple_SET_ITEM(tup, len, element);
3452 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003453 PDATA_PUSH(self->stack, tup, -1);
3454 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003455}
3456
3457static int
Tim Peterscba30e22003-02-01 06:24:36 +00003458load_empty_list(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003459{
3460 PyObject *list;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003461
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003462 if (!( list=PyList_New(0))) return -1;
3463 PDATA_PUSH(self->stack, list, -1);
3464 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003465}
3466
3467static int
Tim Peterscba30e22003-02-01 06:24:36 +00003468load_empty_dict(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003469{
3470 PyObject *dict;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003471
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003472 if (!( dict=PyDict_New())) return -1;
3473 PDATA_PUSH(self->stack, dict, -1);
3474 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003475}
3476
3477
3478static int
Tim Peterscba30e22003-02-01 06:24:36 +00003479load_list(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003480{
3481 PyObject *list = 0;
3482 int i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003483
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003484 if ((i = marker(self)) < 0) return -1;
3485 if (!( list=Pdata_popList(self->stack, i))) return -1;
3486 PDATA_PUSH(self->stack, list, -1);
3487 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003488}
3489
3490static int
Tim Peterscba30e22003-02-01 06:24:36 +00003491load_dict(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003492{
3493 PyObject *dict, *key, *value;
3494 int i, j, k;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003495
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003496 if ((i = marker(self)) < 0) return -1;
3497 j=self->stack->length;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003498
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003499 if (!( dict = PyDict_New())) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003500
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003501 for (k = i+1; k < j; k += 2) {
3502 key =self->stack->data[k-1];
3503 value=self->stack->data[k ];
3504 if (PyDict_SetItem(dict, key, value) < 0) {
3505 Py_DECREF(dict);
3506 return -1;
3507 }
3508 }
3509 Pdata_clear(self->stack, i);
3510 PDATA_PUSH(self->stack, dict, -1);
3511 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003512}
3513
3514static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00003515Instance_New(PyObject *cls, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003516{
Tim Peters1f1b2d22003-02-01 02:16:37 +00003517 PyObject *r = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003518
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003519 if (PyClass_Check(cls)) {
3520 int l;
Tim Peters84e87f32001-03-17 04:50:51 +00003521
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003522 if ((l=PyObject_Size(args)) < 0) goto err;
3523 if (!( l )) {
3524 PyObject *__getinitargs__;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003525
Tim Peterscba30e22003-02-01 06:24:36 +00003526 __getinitargs__ = PyObject_GetAttr(cls,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003527 __getinitargs___str);
3528 if (!__getinitargs__) {
Tim Peterscba30e22003-02-01 06:24:36 +00003529 /* We have a class with no __getinitargs__,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003530 so bypass usual construction */
3531 PyObject *inst;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003532
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003533 PyErr_Clear();
Tim Peterscba30e22003-02-01 06:24:36 +00003534 if (!( inst=PyInstance_NewRaw(cls, NULL)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003535 goto err;
3536 return inst;
3537 }
3538 Py_DECREF(__getinitargs__);
3539 }
Tim Peters84e87f32001-03-17 04:50:51 +00003540
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003541 if ((r=PyInstance_New(cls, args, NULL))) return r;
3542 else goto err;
3543 }
Tim Peters84e87f32001-03-17 04:50:51 +00003544
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003545 if (args==Py_None) {
3546 /* Special case, call cls.__basicnew__() */
3547 PyObject *basicnew;
Tim Peters84e87f32001-03-17 04:50:51 +00003548
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003549 basicnew = PyObject_GetAttr(cls, __basicnew___str);
3550 if (!basicnew) return NULL;
3551 r=PyObject_CallObject(basicnew, NULL);
3552 Py_DECREF(basicnew);
3553 if (r) return r;
3554 }
Guido van Rossum9716aaa1997-12-08 15:15:16 +00003555
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003556 if ((r=PyObject_CallObject(cls, args))) return r;
Guido van Rossum142eeb81997-08-13 03:14:41 +00003557
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003558 err:
3559 {
3560 PyObject *tp, *v, *tb;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003561
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003562 PyErr_Fetch(&tp, &v, &tb);
3563 if ((r=Py_BuildValue("OOO",v,cls,args))) {
3564 Py_XDECREF(v);
3565 v=r;
3566 }
3567 PyErr_Restore(tp,v,tb);
3568 }
3569 return NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003570}
Tim Peters84e87f32001-03-17 04:50:51 +00003571
Guido van Rossum60456fd1997-04-09 17:36:32 +00003572
3573static int
Tim Peterscba30e22003-02-01 06:24:36 +00003574load_obj(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003575{
3576 PyObject *class, *tup, *obj=0;
3577 int i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003578
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003579 if ((i = marker(self)) < 0) return -1;
3580 if (!( tup=Pdata_popTuple(self->stack, i+1))) return -1;
3581 PDATA_POP(self->stack, class);
3582 if (class) {
3583 obj = Instance_New(class, tup);
3584 Py_DECREF(class);
3585 }
3586 Py_DECREF(tup);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003587
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003588 if (! obj) return -1;
3589 PDATA_PUSH(self->stack, obj, -1);
3590 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003591}
3592
3593
3594static int
Tim Peterscba30e22003-02-01 06:24:36 +00003595load_inst(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003596{
3597 PyObject *tup, *class=0, *obj=0, *module_name, *class_name;
3598 int i, len;
3599 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003600
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003601 if ((i = marker(self)) < 0) return -1;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003602
Tim Peters0bc93f52003-02-02 18:29:33 +00003603 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003604 if (len < 2) return bad_readline();
3605 module_name = PyString_FromStringAndSize(s, len - 1);
3606 if (!module_name) return -1;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003607
Tim Peters0bc93f52003-02-02 18:29:33 +00003608 if ((len = self->readline_func(self, &s)) >= 0) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003609 if (len < 2) return bad_readline();
3610 if ((class_name = PyString_FromStringAndSize(s, len - 1))) {
Tim Peterscba30e22003-02-01 06:24:36 +00003611 class = find_class(module_name, class_name,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003612 self->find_class);
3613 Py_DECREF(class_name);
3614 }
3615 }
3616 Py_DECREF(module_name);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003617
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003618 if (! class) return -1;
Tim Peters84e87f32001-03-17 04:50:51 +00003619
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003620 if ((tup=Pdata_popTuple(self->stack, i))) {
3621 obj = Instance_New(class, tup);
3622 Py_DECREF(tup);
3623 }
3624 Py_DECREF(class);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003625
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003626 if (! obj) return -1;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003627
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003628 PDATA_PUSH(self->stack, obj, -1);
3629 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003630}
3631
Tim Peterseab7db32003-02-13 18:24:14 +00003632static int
3633load_newobj(Unpicklerobject *self)
3634{
3635 PyObject *args = NULL;
3636 PyObject *clsraw = NULL;
3637 PyTypeObject *cls; /* clsraw cast to its true type */
3638 PyObject *obj;
3639
3640 /* Stack is ... cls argtuple, and we want to call
3641 * cls.__new__(cls, *argtuple).
3642 */
3643 PDATA_POP(self->stack, args);
3644 if (args == NULL) goto Fail;
3645 if (! PyTuple_Check(args)) {
3646 PyErr_SetString(UnpicklingError, "NEWOBJ expected an arg "
3647 "tuple.");
3648 goto Fail;
3649 }
3650
3651 PDATA_POP(self->stack, clsraw);
3652 cls = (PyTypeObject *)clsraw;
3653 if (cls == NULL) goto Fail;
3654 if (! PyType_Check(cls)) {
3655 PyErr_SetString(UnpicklingError, "NEWOBJ class argument "
3656 "isn't a type object");
3657 goto Fail;
3658 }
3659 if (cls->tp_new == NULL) {
3660 PyErr_SetString(UnpicklingError, "NEWOBJ class argument "
3661 "has NULL tp_new");
3662 goto Fail;
3663 }
3664
3665 /* Call __new__. */
3666 obj = cls->tp_new(cls, args, NULL);
3667 if (obj == NULL) goto Fail;
3668
3669 Py_DECREF(args);
3670 Py_DECREF(clsraw);
3671 PDATA_PUSH(self->stack, obj, -1);
3672 return 0;
3673
3674 Fail:
3675 Py_XDECREF(args);
3676 Py_XDECREF(clsraw);
3677 return -1;
3678}
Guido van Rossum60456fd1997-04-09 17:36:32 +00003679
3680static int
Tim Peterscba30e22003-02-01 06:24:36 +00003681load_global(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003682{
3683 PyObject *class = 0, *module_name = 0, *class_name = 0;
3684 int len;
3685 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003686
Tim Peters0bc93f52003-02-02 18:29:33 +00003687 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003688 if (len < 2) return bad_readline();
3689 module_name = PyString_FromStringAndSize(s, len - 1);
3690 if (!module_name) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003691
Tim Peters0bc93f52003-02-02 18:29:33 +00003692 if ((len = self->readline_func(self, &s)) >= 0) {
Tim Peters4e52ca82002-12-07 02:43:28 +00003693 if (len < 2) {
3694 Py_DECREF(module_name);
3695 return bad_readline();
3696 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003697 if ((class_name = PyString_FromStringAndSize(s, len - 1))) {
Tim Peterscba30e22003-02-01 06:24:36 +00003698 class = find_class(module_name, class_name,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003699 self->find_class);
3700 Py_DECREF(class_name);
3701 }
3702 }
3703 Py_DECREF(module_name);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003704
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003705 if (! class) return -1;
3706 PDATA_PUSH(self->stack, class, -1);
3707 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003708}
3709
3710
3711static int
Tim Peterscba30e22003-02-01 06:24:36 +00003712load_persid(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003713{
3714 PyObject *pid = 0;
3715 int len;
3716 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003717
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003718 if (self->pers_func) {
Tim Peters0bc93f52003-02-02 18:29:33 +00003719 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003720 if (len < 2) return bad_readline();
3721
3722 pid = PyString_FromStringAndSize(s, len - 1);
3723 if (!pid) return -1;
3724
3725 if (PyList_Check(self->pers_func)) {
3726 if (PyList_Append(self->pers_func, pid) < 0) {
3727 Py_DECREF(pid);
3728 return -1;
3729 }
3730 }
3731 else {
3732 ARG_TUP(self, pid);
3733 if (self->arg) {
Tim Peterscba30e22003-02-01 06:24:36 +00003734 pid = PyObject_Call(self->pers_func, self->arg,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003735 NULL);
3736 FREE_ARG_TUP(self);
3737 }
3738 }
3739
3740 if (! pid) return -1;
3741
3742 PDATA_PUSH(self->stack, pid, -1);
3743 return 0;
3744 }
3745 else {
3746 PyErr_SetString(UnpicklingError,
3747 "A load persistent id instruction was encountered,\n"
3748 "but no persistent_load function was specified.");
3749 return -1;
3750 }
3751}
3752
3753static int
Tim Peterscba30e22003-02-01 06:24:36 +00003754load_binpersid(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003755{
3756 PyObject *pid = 0;
3757
3758 if (self->pers_func) {
3759 PDATA_POP(self->stack, pid);
3760 if (! pid) return -1;
3761
3762 if (PyList_Check(self->pers_func)) {
3763 if (PyList_Append(self->pers_func, pid) < 0) {
3764 Py_DECREF(pid);
3765 return -1;
3766 }
3767 }
3768 else {
3769 ARG_TUP(self, pid);
3770 if (self->arg) {
3771 pid = PyObject_Call(self->pers_func, self->arg,
3772 NULL);
3773 FREE_ARG_TUP(self);
3774 }
3775 if (! pid) return -1;
3776 }
3777
3778 PDATA_PUSH(self->stack, pid, -1);
3779 return 0;
3780 }
3781 else {
3782 PyErr_SetString(UnpicklingError,
3783 "A load persistent id instruction was encountered,\n"
3784 "but no persistent_load function was specified.");
3785 return -1;
3786 }
3787}
3788
3789
3790static int
Tim Peterscba30e22003-02-01 06:24:36 +00003791load_pop(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003792{
3793 int len;
3794
3795 if (!( (len=self->stack->length) > 0 )) return stackUnderflow();
3796
3797 /* Note that we split the (pickle.py) stack into two stacks,
3798 an object stack and a mark stack. We have to be clever and
3799 pop the right one. We do this by looking at the top of the
3800 mark stack.
3801 */
3802
3803 if ((self->num_marks > 0) &&
3804 (self->marks[self->num_marks - 1] == len))
3805 self->num_marks--;
3806 else {
3807 len--;
3808 Py_DECREF(self->stack->data[len]);
3809 self->stack->length=len;
3810 }
3811
3812 return 0;
3813}
3814
3815
3816static int
Tim Peterscba30e22003-02-01 06:24:36 +00003817load_pop_mark(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003818{
3819 int i;
3820
3821 if ((i = marker(self)) < 0)
3822 return -1;
3823
3824 Pdata_clear(self->stack, i);
3825
3826 return 0;
3827}
3828
3829
3830static int
Tim Peterscba30e22003-02-01 06:24:36 +00003831load_dup(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003832{
3833 PyObject *last;
3834 int len;
3835
3836 if ((len = self->stack->length) <= 0) return stackUnderflow();
3837 last=self->stack->data[len-1];
3838 Py_INCREF(last);
3839 PDATA_PUSH(self->stack, last, -1);
3840 return 0;
3841}
3842
3843
3844static int
Tim Peterscba30e22003-02-01 06:24:36 +00003845load_get(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003846{
3847 PyObject *py_str = 0, *value = 0;
3848 int len;
3849 char *s;
3850 int rc;
3851
Tim Peters0bc93f52003-02-02 18:29:33 +00003852 if ((len = self->readline_func(self, &s)) < 0) return -1;
Guido van Rossumf9ffb031999-02-04 14:54:04 +00003853 if (len < 2) return bad_readline();
Tim Peters84e87f32001-03-17 04:50:51 +00003854
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003855 if (!( py_str = PyString_FromStringAndSize(s, len - 1))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003856
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003857 value = PyDict_GetItem(self->memo, py_str);
3858 if (! value) {
3859 PyErr_SetObject(BadPickleGet, py_str);
3860 rc = -1;
Tim Peters92c8bb32003-02-13 23:00:26 +00003861 }
3862 else {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003863 PDATA_APPEND(self->stack, value, -1);
3864 rc = 0;
3865 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003866
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003867 Py_DECREF(py_str);
3868 return rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003869}
3870
3871
3872static int
Tim Peterscba30e22003-02-01 06:24:36 +00003873load_binget(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003874{
3875 PyObject *py_key = 0, *value = 0;
3876 unsigned char key;
3877 char *s;
3878 int rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003879
Tim Peters0bc93f52003-02-02 18:29:33 +00003880 if (self->read_func(self, &s, 1) < 0) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003881
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003882 key = (unsigned char)s[0];
3883 if (!( py_key = PyInt_FromLong((long)key))) return -1;
Guido van Rossumea2b7152000-05-09 18:14:50 +00003884
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003885 value = PyDict_GetItem(self->memo, py_key);
3886 if (! value) {
3887 PyErr_SetObject(BadPickleGet, py_key);
3888 rc = -1;
Tim Peters92c8bb32003-02-13 23:00:26 +00003889 }
3890 else {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003891 PDATA_APPEND(self->stack, value, -1);
3892 rc = 0;
3893 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003894
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003895 Py_DECREF(py_key);
3896 return rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003897}
3898
3899
3900static int
Tim Peterscba30e22003-02-01 06:24:36 +00003901load_long_binget(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003902{
3903 PyObject *py_key = 0, *value = 0;
3904 unsigned char c;
3905 char *s;
3906 long key;
3907 int rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003908
Tim Peters0bc93f52003-02-02 18:29:33 +00003909 if (self->read_func(self, &s, 4) < 0) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003910
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003911 c = (unsigned char)s[0];
3912 key = (long)c;
3913 c = (unsigned char)s[1];
3914 key |= (long)c << 8;
3915 c = (unsigned char)s[2];
3916 key |= (long)c << 16;
3917 c = (unsigned char)s[3];
3918 key |= (long)c << 24;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003919
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003920 if (!( py_key = PyInt_FromLong((long)key))) return -1;
3921
3922 value = PyDict_GetItem(self->memo, py_key);
3923 if (! value) {
3924 PyErr_SetObject(BadPickleGet, py_key);
3925 rc = -1;
Tim Peters92c8bb32003-02-13 23:00:26 +00003926 }
3927 else {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003928 PDATA_APPEND(self->stack, value, -1);
3929 rc = 0;
3930 }
3931
3932 Py_DECREF(py_key);
3933 return rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003934}
3935
Tim Peters2d629652003-02-04 05:06:17 +00003936/* Push an object from the extension registry (EXT[124]). nbytes is
3937 * the number of bytes following the opcode, holding the index (code) value.
3938 */
3939static int
3940load_extension(Unpicklerobject *self, int nbytes)
3941{
3942 char *codebytes; /* the nbytes bytes after the opcode */
3943 long code; /* calc_binint returns long */
3944 PyObject *py_code; /* code as a Python int */
3945 PyObject *obj; /* the object to push */
3946 PyObject *pair; /* (module_name, class_name) */
3947 PyObject *module_name, *class_name;
3948
3949 assert(nbytes == 1 || nbytes == 2 || nbytes == 4);
3950 if (self->read_func(self, &codebytes, nbytes) < 0) return -1;
3951 code = calc_binint(codebytes, nbytes);
3952 if (code <= 0) { /* note that 0 is forbidden */
3953 /* Corrupt or hostile pickle. */
3954 PyErr_SetString(UnpicklingError, "EXT specifies code <= 0");
3955 return -1;
3956 }
3957
3958 /* Look for the code in the cache. */
3959 py_code = PyInt_FromLong(code);
3960 if (py_code == NULL) return -1;
3961 obj = PyDict_GetItem(extension_cache, py_code);
3962 if (obj != NULL) {
3963 /* Bingo. */
3964 Py_DECREF(py_code);
3965 PDATA_APPEND(self->stack, obj, -1);
3966 return 0;
3967 }
3968
3969 /* Look up the (module_name, class_name) pair. */
3970 pair = PyDict_GetItem(inverted_registry, py_code);
3971 if (pair == NULL) {
3972 Py_DECREF(py_code);
3973 PyErr_Format(PyExc_ValueError, "unregistered extension "
3974 "code %ld", code);
3975 return -1;
3976 }
3977 /* Since the extension registry is manipulable via Python code,
Tim Petersfa05ce32003-02-04 05:20:32 +00003978 * confirm that pair is really a 2-tuple of strings.
Tim Peters2d629652003-02-04 05:06:17 +00003979 */
3980 if (!PyTuple_Check(pair) || PyTuple_Size(pair) != 2 ||
3981 !PyString_Check(module_name = PyTuple_GET_ITEM(pair, 0)) ||
3982 !PyString_Check(class_name = PyTuple_GET_ITEM(pair, 1))) {
3983 Py_DECREF(py_code);
3984 PyErr_Format(PyExc_ValueError, "_inverted_registry[%ld] "
3985 "isn't a 2-tuple of strings", code);
3986 return -1;
3987 }
3988 /* Load the object. */
3989 obj = find_class(module_name, class_name, self->find_class);
3990 if (obj == NULL) {
3991 Py_DECREF(py_code);
3992 return -1;
3993 }
3994 /* Cache code -> obj. */
3995 code = PyDict_SetItem(extension_cache, py_code, obj);
3996 Py_DECREF(py_code);
3997 if (code < 0) {
3998 Py_DECREF(obj);
3999 return -1;
4000 }
4001 PDATA_PUSH(self->stack, obj, -1);
4002 return 0;
4003}
Guido van Rossum60456fd1997-04-09 17:36:32 +00004004
4005static int
Tim Peterscba30e22003-02-01 06:24:36 +00004006load_put(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004007{
4008 PyObject *py_str = 0, *value = 0;
4009 int len, l;
4010 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004011
Tim Peters0bc93f52003-02-02 18:29:33 +00004012 if ((l = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004013 if (l < 2) return bad_readline();
4014 if (!( len=self->stack->length )) return stackUnderflow();
4015 if (!( py_str = PyString_FromStringAndSize(s, l - 1))) return -1;
4016 value=self->stack->data[len-1];
4017 l=PyDict_SetItem(self->memo, py_str, value);
4018 Py_DECREF(py_str);
4019 return l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004020}
4021
4022
4023static int
Tim Peterscba30e22003-02-01 06:24:36 +00004024load_binput(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004025{
4026 PyObject *py_key = 0, *value = 0;
4027 unsigned char key;
4028 char *s;
4029 int len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004030
Tim Peters0bc93f52003-02-02 18:29:33 +00004031 if (self->read_func(self, &s, 1) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004032 if (!( (len=self->stack->length) > 0 )) return stackUnderflow();
Guido van Rossum60456fd1997-04-09 17:36:32 +00004033
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004034 key = (unsigned char)s[0];
Guido van Rossum053b8df1998-11-25 16:18:00 +00004035
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004036 if (!( py_key = PyInt_FromLong((long)key))) return -1;
4037 value=self->stack->data[len-1];
4038 len=PyDict_SetItem(self->memo, py_key, value);
4039 Py_DECREF(py_key);
4040 return len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004041}
4042
4043
4044static int
Tim Peterscba30e22003-02-01 06:24:36 +00004045load_long_binput(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004046{
4047 PyObject *py_key = 0, *value = 0;
4048 long key;
4049 unsigned char c;
4050 char *s;
4051 int len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004052
Tim Peters0bc93f52003-02-02 18:29:33 +00004053 if (self->read_func(self, &s, 4) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004054 if (!( len=self->stack->length )) return stackUnderflow();
Guido van Rossum60456fd1997-04-09 17:36:32 +00004055
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004056 c = (unsigned char)s[0];
4057 key = (long)c;
4058 c = (unsigned char)s[1];
4059 key |= (long)c << 8;
4060 c = (unsigned char)s[2];
4061 key |= (long)c << 16;
4062 c = (unsigned char)s[3];
4063 key |= (long)c << 24;
Tim Peters84e87f32001-03-17 04:50:51 +00004064
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004065 if (!( py_key = PyInt_FromLong(key))) return -1;
4066 value=self->stack->data[len-1];
4067 len=PyDict_SetItem(self->memo, py_key, value);
4068 Py_DECREF(py_key);
4069 return len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004070}
4071
4072
4073static int
Tim Peterscba30e22003-02-01 06:24:36 +00004074do_append(Unpicklerobject *self, int x)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004075{
4076 PyObject *value = 0, *list = 0, *append_method = 0;
4077 int len, i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004078
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004079 len=self->stack->length;
4080 if (!( len >= x && x > 0 )) return stackUnderflow();
4081 /* nothing to do */
Tim Peterscba30e22003-02-01 06:24:36 +00004082 if (len==x) return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004083
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004084 list=self->stack->data[x-1];
Guido van Rossum60456fd1997-04-09 17:36:32 +00004085
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004086 if (PyList_Check(list)) {
4087 PyObject *slice;
4088 int list_len;
Tim Peters84e87f32001-03-17 04:50:51 +00004089
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004090 slice=Pdata_popList(self->stack, x);
4091 list_len = PyList_GET_SIZE(list);
4092 i=PyList_SetSlice(list, list_len, list_len, slice);
4093 Py_DECREF(slice);
4094 return i;
4095 }
4096 else {
Guido van Rossum60456fd1997-04-09 17:36:32 +00004097
Tim Peterscba30e22003-02-01 06:24:36 +00004098 if (!( append_method = PyObject_GetAttr(list, append_str)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004099 return -1;
4100
4101 for (i = x; i < len; i++) {
4102 PyObject *junk;
4103
4104 value=self->stack->data[i];
4105 junk=0;
4106 ARG_TUP(self, value);
4107 if (self->arg) {
Tim Peterscba30e22003-02-01 06:24:36 +00004108 junk = PyObject_Call(append_method, self->arg,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004109 NULL);
4110 FREE_ARG_TUP(self);
4111 }
4112 if (! junk) {
4113 Pdata_clear(self->stack, i+1);
4114 self->stack->length=x;
4115 Py_DECREF(append_method);
4116 return -1;
4117 }
4118 Py_DECREF(junk);
4119 }
4120 self->stack->length=x;
4121 Py_DECREF(append_method);
4122 }
4123
4124 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004125}
4126
4127
4128static int
Tim Peterscba30e22003-02-01 06:24:36 +00004129load_append(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004130{
4131 return do_append(self, self->stack->length - 1);
Guido van Rossum60456fd1997-04-09 17:36:32 +00004132}
4133
4134
4135static int
Tim Peterscba30e22003-02-01 06:24:36 +00004136load_appends(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004137{
4138 return do_append(self, marker(self));
Guido van Rossum60456fd1997-04-09 17:36:32 +00004139}
4140
4141
4142static int
Tim Peterscba30e22003-02-01 06:24:36 +00004143do_setitems(Unpicklerobject *self, int x)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004144{
4145 PyObject *value = 0, *key = 0, *dict = 0;
4146 int len, i, r=0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004147
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004148 if (!( (len=self->stack->length) >= x
4149 && x > 0 )) return stackUnderflow();
Guido van Rossum60456fd1997-04-09 17:36:32 +00004150
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004151 dict=self->stack->data[x-1];
Guido van Rossum60456fd1997-04-09 17:36:32 +00004152
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004153 for (i = x+1; i < len; i += 2) {
4154 key =self->stack->data[i-1];
4155 value=self->stack->data[i ];
4156 if (PyObject_SetItem(dict, key, value) < 0) {
4157 r=-1;
4158 break;
4159 }
4160 }
4161
4162 Pdata_clear(self->stack, x);
4163
4164 return r;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004165}
4166
4167
Tim Peters84e87f32001-03-17 04:50:51 +00004168static int
Tim Peterscba30e22003-02-01 06:24:36 +00004169load_setitem(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004170{
4171 return do_setitems(self, self->stack->length - 2);
4172}
Guido van Rossum60456fd1997-04-09 17:36:32 +00004173
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004174static int
Tim Peterscba30e22003-02-01 06:24:36 +00004175load_setitems(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004176{
4177 return do_setitems(self, marker(self));
Guido van Rossum60456fd1997-04-09 17:36:32 +00004178}
4179
Tim Peters84e87f32001-03-17 04:50:51 +00004180
Guido van Rossum60456fd1997-04-09 17:36:32 +00004181static int
Tim Peterscba30e22003-02-01 06:24:36 +00004182load_build(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004183{
Tim Peters080c88b2003-02-15 03:01:11 +00004184 PyObject *state, *inst, *slotstate;
4185 PyObject *__setstate__;
4186 PyObject *d_key, *d_value;
4187 int i;
4188 int res = -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004189
Tim Peters080c88b2003-02-15 03:01:11 +00004190 /* Stack is ... instance, state. We want to leave instance at
4191 * the stack top, possibly mutated via instance.__setstate__(state).
4192 */
4193 if (self->stack->length < 2)
4194 return stackUnderflow();
4195 PDATA_POP(self->stack, state);
4196 if (state == NULL)
4197 return -1;
4198 inst = self->stack->data[self->stack->length - 1];
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004199
Tim Peters080c88b2003-02-15 03:01:11 +00004200 __setstate__ = PyObject_GetAttr(inst, __setstate___str);
4201 if (__setstate__ != NULL) {
4202 PyObject *junk = NULL;
4203
4204 /* The explicit __setstate__ is responsible for everything. */
4205 ARG_TUP(self, state);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004206 if (self->arg) {
4207 junk = PyObject_Call(__setstate__, self->arg, NULL);
4208 FREE_ARG_TUP(self);
4209 }
4210 Py_DECREF(__setstate__);
Tim Peters080c88b2003-02-15 03:01:11 +00004211 if (junk == NULL)
4212 return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004213 Py_DECREF(junk);
4214 return 0;
4215 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004216 PyErr_Clear();
Tim Peters080c88b2003-02-15 03:01:11 +00004217
4218 /* A default __setstate__. First see whether state embeds a
4219 * slot state dict too (a proto 2 addition).
4220 */
4221 if (PyTuple_Check(state) && PyTuple_Size(state) == 2) {
4222 PyObject *temp = state;
4223 state = PyTuple_GET_ITEM(temp, 0);
4224 slotstate = PyTuple_GET_ITEM(temp, 1);
4225 Py_INCREF(state);
4226 Py_INCREF(slotstate);
4227 Py_DECREF(temp);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004228 }
Tim Peters080c88b2003-02-15 03:01:11 +00004229 else
4230 slotstate = NULL;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004231
Tim Peters080c88b2003-02-15 03:01:11 +00004232 /* Set inst.__dict__ from the state dict (if any). */
4233 if (state != Py_None) {
4234 PyObject *dict;
4235 if (! PyDict_Check(state)) {
4236 PyErr_SetString(UnpicklingError, "state is not a "
4237 "dictionary");
4238 goto finally;
4239 }
4240 dict = PyObject_GetAttr(inst, __dict___str);
4241 if (dict == NULL)
4242 goto finally;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004243
Tim Peters080c88b2003-02-15 03:01:11 +00004244 i = 0;
4245 while (PyDict_Next(state, &i, &d_key, &d_value)) {
4246 if (PyObject_SetItem(dict, d_key, d_value) < 0)
4247 goto finally;
4248 }
4249 Py_DECREF(dict);
4250 }
4251
4252 /* Also set instance attributes from the slotstate dict (if any). */
4253 if (slotstate != NULL) {
4254 if (! PyDict_Check(slotstate)) {
4255 PyErr_SetString(UnpicklingError, "slot state is not "
4256 "a dictionary");
4257 goto finally;
4258 }
4259 i = 0;
4260 while (PyDict_Next(slotstate, &i, &d_key, &d_value)) {
4261 if (PyObject_SetAttr(inst, d_key, d_value) < 0)
4262 goto finally;
4263 }
4264 }
4265 res = 0;
4266
4267 finally:
4268 Py_DECREF(state);
4269 Py_XDECREF(slotstate);
4270 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004271}
4272
4273
4274static int
Tim Peterscba30e22003-02-01 06:24:36 +00004275load_mark(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004276{
4277 int s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004278
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004279 /* Note that we split the (pickle.py) stack into two stacks, an
4280 object stack and a mark stack. Here we push a mark onto the
4281 mark stack.
4282 */
Guido van Rossum60456fd1997-04-09 17:36:32 +00004283
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004284 if ((self->num_marks + 1) >= self->marks_size) {
4285 s=self->marks_size+20;
4286 if (s <= self->num_marks) s=self->num_marks + 1;
4287 if (self->marks == NULL)
4288 self->marks=(int *)malloc(s * sizeof(int));
4289 else
Tim Peterscba30e22003-02-01 06:24:36 +00004290 self->marks=(int *)realloc(self->marks,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004291 s * sizeof(int));
4292 if (! self->marks) {
4293 PyErr_NoMemory();
4294 return -1;
4295 }
4296 self->marks_size = s;
4297 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00004298
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004299 self->marks[self->num_marks++] = self->stack->length;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004300
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004301 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004302}
4303
Guido van Rossum60456fd1997-04-09 17:36:32 +00004304static int
Tim Peterscba30e22003-02-01 06:24:36 +00004305load_reduce(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004306{
4307 PyObject *callable = 0, *arg_tup = 0, *ob = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004308
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004309 PDATA_POP(self->stack, arg_tup);
4310 if (! arg_tup) return -1;
4311 PDATA_POP(self->stack, callable);
4312 if (callable) {
4313 ob = Instance_New(callable, arg_tup);
4314 Py_DECREF(callable);
4315 }
4316 Py_DECREF(arg_tup);
Guido van Rossum60456fd1997-04-09 17:36:32 +00004317
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004318 if (! ob) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004319
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004320 PDATA_PUSH(self->stack, ob, -1);
4321 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004322}
Tim Peters84e87f32001-03-17 04:50:51 +00004323
Tim Peters4190fb82003-02-02 16:09:05 +00004324/* Just raises an error if we don't know the protocol specified. PROTO
4325 * is the first opcode for protocols >= 2.
4326 */
4327static int
4328load_proto(Unpicklerobject *self)
4329{
4330 int i;
4331 char *protobyte;
4332
4333 i = self->read_func(self, &protobyte, 1);
4334 if (i < 0)
4335 return -1;
4336
4337 i = calc_binint(protobyte, 1);
4338 /* No point checking for < 0, since calc_binint returns an unsigned
4339 * int when chewing on 1 byte.
4340 */
4341 assert(i >= 0);
Tim Peters8587b3c2003-02-13 15:44:41 +00004342 if (i <= HIGHEST_PROTOCOL)
Tim Peters4190fb82003-02-02 16:09:05 +00004343 return 0;
4344
4345 PyErr_Format(PyExc_ValueError, "unsupported pickle protocol: %d", i);
4346 return -1;
4347}
4348
Guido van Rossum60456fd1997-04-09 17:36:32 +00004349static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00004350load(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004351{
4352 PyObject *err = 0, *val = 0;
4353 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004354
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004355 self->num_marks = 0;
4356 if (self->stack->length) Pdata_clear(self->stack, 0);
Guido van Rossum60456fd1997-04-09 17:36:32 +00004357
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004358 while (1) {
Tim Peters0bc93f52003-02-02 18:29:33 +00004359 if (self->read_func(self, &s, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004360 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004361
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004362 switch (s[0]) {
4363 case NONE:
4364 if (load_none(self) < 0)
4365 break;
4366 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004367
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004368 case BININT:
4369 if (load_binint(self) < 0)
4370 break;
4371 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004372
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004373 case BININT1:
4374 if (load_binint1(self) < 0)
4375 break;
4376 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004377
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004378 case BININT2:
4379 if (load_binint2(self) < 0)
4380 break;
4381 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004382
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004383 case INT:
4384 if (load_int(self) < 0)
4385 break;
4386 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004387
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004388 case LONG:
4389 if (load_long(self) < 0)
4390 break;
4391 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004392
Tim Petersee1a53c2003-02-02 02:57:53 +00004393 case LONG1:
4394 if (load_counted_long(self, 1) < 0)
4395 break;
4396 continue;
4397
4398 case LONG4:
4399 if (load_counted_long(self, 4) < 0)
4400 break;
4401 continue;
4402
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004403 case FLOAT:
4404 if (load_float(self) < 0)
4405 break;
4406 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004407
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004408 case BINFLOAT:
4409 if (load_binfloat(self) < 0)
4410 break;
4411 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004412
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004413 case BINSTRING:
4414 if (load_binstring(self) < 0)
4415 break;
4416 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004417
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004418 case SHORT_BINSTRING:
4419 if (load_short_binstring(self) < 0)
4420 break;
4421 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004422
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004423 case STRING:
4424 if (load_string(self) < 0)
4425 break;
4426 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004427
Martin v. Löwis339d0f72001-08-17 18:39:25 +00004428#ifdef Py_USING_UNICODE
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004429 case UNICODE:
4430 if (load_unicode(self) < 0)
4431 break;
4432 continue;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00004433
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004434 case BINUNICODE:
4435 if (load_binunicode(self) < 0)
4436 break;
4437 continue;
Martin v. Löwis339d0f72001-08-17 18:39:25 +00004438#endif
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00004439
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004440 case EMPTY_TUPLE:
Tim Peters1d63c9f2003-02-02 20:29:39 +00004441 if (load_counted_tuple(self, 0) < 0)
4442 break;
4443 continue;
4444
4445 case TUPLE1:
4446 if (load_counted_tuple(self, 1) < 0)
4447 break;
4448 continue;
4449
4450 case TUPLE2:
4451 if (load_counted_tuple(self, 2) < 0)
4452 break;
4453 continue;
4454
4455 case TUPLE3:
4456 if (load_counted_tuple(self, 3) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004457 break;
4458 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004459
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004460 case TUPLE:
4461 if (load_tuple(self) < 0)
4462 break;
4463 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004464
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004465 case EMPTY_LIST:
4466 if (load_empty_list(self) < 0)
4467 break;
4468 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004469
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004470 case LIST:
4471 if (load_list(self) < 0)
4472 break;
4473 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004474
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004475 case EMPTY_DICT:
4476 if (load_empty_dict(self) < 0)
4477 break;
4478 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004479
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004480 case DICT:
4481 if (load_dict(self) < 0)
4482 break;
4483 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004484
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004485 case OBJ:
4486 if (load_obj(self) < 0)
4487 break;
4488 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004489
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004490 case INST:
4491 if (load_inst(self) < 0)
4492 break;
4493 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004494
Tim Peterseab7db32003-02-13 18:24:14 +00004495 case NEWOBJ:
4496 if (load_newobj(self) < 0)
4497 break;
4498 continue;
4499
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004500 case GLOBAL:
4501 if (load_global(self) < 0)
4502 break;
4503 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004504
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004505 case APPEND:
4506 if (load_append(self) < 0)
4507 break;
4508 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004509
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004510 case APPENDS:
4511 if (load_appends(self) < 0)
4512 break;
4513 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00004514
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004515 case BUILD:
4516 if (load_build(self) < 0)
4517 break;
4518 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00004519
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004520 case DUP:
4521 if (load_dup(self) < 0)
4522 break;
4523 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004524
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004525 case BINGET:
4526 if (load_binget(self) < 0)
4527 break;
4528 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004529
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004530 case LONG_BINGET:
4531 if (load_long_binget(self) < 0)
4532 break;
4533 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00004534
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004535 case GET:
4536 if (load_get(self) < 0)
4537 break;
4538 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004539
Tim Peters2d629652003-02-04 05:06:17 +00004540 case EXT1:
4541 if (load_extension(self, 1) < 0)
4542 break;
4543 continue;
4544
4545 case EXT2:
4546 if (load_extension(self, 2) < 0)
4547 break;
4548 continue;
4549
4550 case EXT4:
4551 if (load_extension(self, 4) < 0)
4552 break;
4553 continue;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004554 case MARK:
4555 if (load_mark(self) < 0)
4556 break;
4557 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004558
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004559 case BINPUT:
4560 if (load_binput(self) < 0)
4561 break;
4562 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004563
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004564 case LONG_BINPUT:
4565 if (load_long_binput(self) < 0)
4566 break;
4567 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00004568
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004569 case PUT:
4570 if (load_put(self) < 0)
4571 break;
4572 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004573
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004574 case POP:
4575 if (load_pop(self) < 0)
4576 break;
4577 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004578
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004579 case POP_MARK:
4580 if (load_pop_mark(self) < 0)
4581 break;
4582 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004583
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004584 case SETITEM:
4585 if (load_setitem(self) < 0)
4586 break;
4587 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004588
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004589 case SETITEMS:
4590 if (load_setitems(self) < 0)
4591 break;
4592 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004593
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004594 case STOP:
4595 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004596
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004597 case PERSID:
4598 if (load_persid(self) < 0)
4599 break;
4600 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004601
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004602 case BINPERSID:
4603 if (load_binpersid(self) < 0)
4604 break;
4605 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004606
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004607 case REDUCE:
4608 if (load_reduce(self) < 0)
4609 break;
4610 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004611
Tim Peters4190fb82003-02-02 16:09:05 +00004612 case PROTO:
4613 if (load_proto(self) < 0)
4614 break;
4615 continue;
4616
Tim Peters3c67d792003-02-02 17:59:11 +00004617 case NEWTRUE:
4618 if (load_bool(self, Py_True) < 0)
4619 break;
4620 continue;
4621
4622 case NEWFALSE:
4623 if (load_bool(self, Py_False) < 0)
4624 break;
4625 continue;
4626
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004627 case '\0':
4628 /* end of file */
4629 PyErr_SetNone(PyExc_EOFError);
4630 break;
Tim Peterscba30e22003-02-01 06:24:36 +00004631
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004632 default:
Tim Peterscba30e22003-02-01 06:24:36 +00004633 cPickle_ErrFormat(UnpicklingError,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004634 "invalid load key, '%s'.",
4635 "c", s[0]);
4636 return NULL;
4637 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00004638
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004639 break;
4640 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00004641
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004642 if ((err = PyErr_Occurred())) {
4643 if (err == PyExc_EOFError) {
4644 PyErr_SetNone(PyExc_EOFError);
4645 }
4646 return NULL;
4647 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00004648
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004649 PDATA_POP(self->stack, val);
4650 return val;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004651}
Tim Peters84e87f32001-03-17 04:50:51 +00004652
Guido van Rossum60456fd1997-04-09 17:36:32 +00004653
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004654/* No-load functions to support noload, which is used to
4655 find persistent references. */
4656
4657static int
Tim Peterscba30e22003-02-01 06:24:36 +00004658noload_obj(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004659{
4660 int i;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004661
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004662 if ((i = marker(self)) < 0) return -1;
4663 return Pdata_clear(self->stack, i+1);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004664}
4665
4666
4667static int
Tim Peterscba30e22003-02-01 06:24:36 +00004668noload_inst(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004669{
4670 int i;
4671 char *s;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004672
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004673 if ((i = marker(self)) < 0) return -1;
4674 Pdata_clear(self->stack, i);
Tim Peters0bc93f52003-02-02 18:29:33 +00004675 if (self->readline_func(self, &s) < 0) return -1;
4676 if (self->readline_func(self, &s) < 0) return -1;
Tim Peterseab7db32003-02-13 18:24:14 +00004677 PDATA_APPEND(self->stack, Py_None, -1);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004678 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004679}
4680
4681static int
Tim Peterseab7db32003-02-13 18:24:14 +00004682noload_newobj(Unpicklerobject *self)
4683{
4684 PyObject *obj;
4685
4686 PDATA_POP(self->stack, obj); /* pop argtuple */
4687 if (obj == NULL) return -1;
4688 Py_DECREF(obj);
4689
4690 PDATA_POP(self->stack, obj); /* pop cls */
4691 if (obj == NULL) return -1;
4692 Py_DECREF(obj);
4693
4694 PDATA_APPEND(self->stack, Py_None, -1);
4695 return 0;
4696}
4697
4698static int
Tim Peterscba30e22003-02-01 06:24:36 +00004699noload_global(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004700{
4701 char *s;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004702
Tim Peters0bc93f52003-02-02 18:29:33 +00004703 if (self->readline_func(self, &s) < 0) return -1;
4704 if (self->readline_func(self, &s) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004705 PDATA_APPEND(self->stack, Py_None,-1);
4706 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004707}
4708
4709static int
Tim Peterscba30e22003-02-01 06:24:36 +00004710noload_reduce(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004711{
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004712
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004713 if (self->stack->length < 2) return stackUnderflow();
4714 Pdata_clear(self->stack, self->stack->length-2);
4715 PDATA_APPEND(self->stack, Py_None,-1);
4716 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004717}
4718
4719static int
4720noload_build(Unpicklerobject *self) {
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004721
Guido van Rossum053b8df1998-11-25 16:18:00 +00004722 if (self->stack->length < 1) return stackUnderflow();
4723 Pdata_clear(self->stack, self->stack->length-1);
Guido van Rossum50f385c1998-12-04 18:48:44 +00004724 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004725}
4726
Tim Peters2d629652003-02-04 05:06:17 +00004727static int
4728noload_extension(Unpicklerobject *self, int nbytes)
4729{
4730 char *codebytes;
4731
4732 assert(nbytes == 1 || nbytes == 2 || nbytes == 4);
4733 if (self->read_func(self, &codebytes, nbytes) < 0) return -1;
4734 PDATA_APPEND(self->stack, Py_None, -1);
4735 return 0;
4736}
4737
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004738
4739static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00004740noload(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004741{
4742 PyObject *err = 0, *val = 0;
4743 char *s;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004744
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004745 self->num_marks = 0;
4746 Pdata_clear(self->stack, 0);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004747
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004748 while (1) {
Tim Peters0bc93f52003-02-02 18:29:33 +00004749 if (self->read_func(self, &s, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004750 break;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004751
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004752 switch (s[0]) {
4753 case NONE:
4754 if (load_none(self) < 0)
4755 break;
4756 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004757
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004758 case BININT:
4759 if (load_binint(self) < 0)
4760 break;
4761 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004762
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004763 case BININT1:
4764 if (load_binint1(self) < 0)
4765 break;
4766 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004767
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004768 case BININT2:
4769 if (load_binint2(self) < 0)
4770 break;
4771 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004772
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004773 case INT:
4774 if (load_int(self) < 0)
4775 break;
4776 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004777
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004778 case LONG:
4779 if (load_long(self) < 0)
4780 break;
4781 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004782
Tim Peters4190fb82003-02-02 16:09:05 +00004783 case LONG1:
4784 if (load_counted_long(self, 1) < 0)
4785 break;
4786 continue;
4787
4788 case LONG4:
4789 if (load_counted_long(self, 4) < 0)
4790 break;
4791 continue;
4792
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004793 case FLOAT:
4794 if (load_float(self) < 0)
4795 break;
4796 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004797
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004798 case BINFLOAT:
4799 if (load_binfloat(self) < 0)
4800 break;
4801 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004802
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004803 case BINSTRING:
4804 if (load_binstring(self) < 0)
4805 break;
4806 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004807
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004808 case SHORT_BINSTRING:
4809 if (load_short_binstring(self) < 0)
4810 break;
4811 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004812
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004813 case STRING:
4814 if (load_string(self) < 0)
4815 break;
4816 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004817
Martin v. Löwis339d0f72001-08-17 18:39:25 +00004818#ifdef Py_USING_UNICODE
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004819 case UNICODE:
4820 if (load_unicode(self) < 0)
4821 break;
4822 continue;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00004823
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004824 case BINUNICODE:
4825 if (load_binunicode(self) < 0)
4826 break;
4827 continue;
Martin v. Löwis339d0f72001-08-17 18:39:25 +00004828#endif
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00004829
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004830 case EMPTY_TUPLE:
Tim Peters1d63c9f2003-02-02 20:29:39 +00004831 if (load_counted_tuple(self, 0) < 0)
4832 break;
4833 continue;
4834
4835 case TUPLE1:
4836 if (load_counted_tuple(self, 1) < 0)
4837 break;
4838 continue;
4839
4840 case TUPLE2:
4841 if (load_counted_tuple(self, 2) < 0)
4842 break;
4843 continue;
4844
4845 case TUPLE3:
4846 if (load_counted_tuple(self, 3) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004847 break;
4848 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004849
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004850 case TUPLE:
4851 if (load_tuple(self) < 0)
4852 break;
4853 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004854
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004855 case EMPTY_LIST:
4856 if (load_empty_list(self) < 0)
4857 break;
4858 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004859
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004860 case LIST:
4861 if (load_list(self) < 0)
4862 break;
4863 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004864
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004865 case EMPTY_DICT:
4866 if (load_empty_dict(self) < 0)
4867 break;
4868 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004869
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004870 case DICT:
4871 if (load_dict(self) < 0)
4872 break;
4873 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004874
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004875 case OBJ:
4876 if (noload_obj(self) < 0)
4877 break;
4878 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004879
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004880 case INST:
4881 if (noload_inst(self) < 0)
4882 break;
4883 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004884
Tim Peterseab7db32003-02-13 18:24:14 +00004885 case NEWOBJ:
4886 if (noload_newobj(self) < 0)
4887 break;
4888 continue;
4889
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004890 case GLOBAL:
4891 if (noload_global(self) < 0)
4892 break;
4893 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004894
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004895 case APPEND:
4896 if (load_append(self) < 0)
4897 break;
4898 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004899
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004900 case APPENDS:
4901 if (load_appends(self) < 0)
4902 break;
4903 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00004904
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004905 case BUILD:
4906 if (noload_build(self) < 0)
4907 break;
4908 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00004909
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004910 case DUP:
4911 if (load_dup(self) < 0)
4912 break;
4913 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004914
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004915 case BINGET:
4916 if (load_binget(self) < 0)
4917 break;
4918 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004919
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004920 case LONG_BINGET:
4921 if (load_long_binget(self) < 0)
4922 break;
4923 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00004924
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004925 case GET:
4926 if (load_get(self) < 0)
4927 break;
4928 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004929
Tim Peters2d629652003-02-04 05:06:17 +00004930 case EXT1:
4931 if (noload_extension(self, 1) < 0)
4932 break;
4933 continue;
4934
4935 case EXT2:
4936 if (noload_extension(self, 2) < 0)
4937 break;
4938 continue;
4939
4940 case EXT4:
4941 if (noload_extension(self, 4) < 0)
4942 break;
4943 continue;
4944
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004945 case MARK:
4946 if (load_mark(self) < 0)
4947 break;
4948 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004949
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004950 case BINPUT:
4951 if (load_binput(self) < 0)
4952 break;
4953 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004954
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004955 case LONG_BINPUT:
4956 if (load_long_binput(self) < 0)
4957 break;
4958 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00004959
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004960 case PUT:
4961 if (load_put(self) < 0)
4962 break;
4963 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004964
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004965 case POP:
4966 if (load_pop(self) < 0)
4967 break;
4968 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004969
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004970 case POP_MARK:
4971 if (load_pop_mark(self) < 0)
4972 break;
4973 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004974
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004975 case SETITEM:
4976 if (load_setitem(self) < 0)
4977 break;
4978 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004979
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004980 case SETITEMS:
4981 if (load_setitems(self) < 0)
4982 break;
4983 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004984
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004985 case STOP:
4986 break;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004987
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004988 case PERSID:
4989 if (load_persid(self) < 0)
4990 break;
4991 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004992
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004993 case BINPERSID:
4994 if (load_binpersid(self) < 0)
4995 break;
4996 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004997
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004998 case REDUCE:
4999 if (noload_reduce(self) < 0)
5000 break;
5001 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005002
Tim Peters4190fb82003-02-02 16:09:05 +00005003 case PROTO:
5004 if (load_proto(self) < 0)
5005 break;
5006 continue;
5007
Tim Peters3c67d792003-02-02 17:59:11 +00005008 case NEWTRUE:
5009 if (load_bool(self, Py_True) < 0)
5010 break;
5011 continue;
5012
5013 case NEWFALSE:
5014 if (load_bool(self, Py_False) < 0)
5015 break;
5016 continue;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005017 default:
Tim Peterscba30e22003-02-01 06:24:36 +00005018 cPickle_ErrFormat(UnpicklingError,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005019 "invalid load key, '%s'.",
5020 "c", s[0]);
5021 return NULL;
5022 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005023
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005024 break;
5025 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005026
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005027 if ((err = PyErr_Occurred())) {
5028 if (err == PyExc_EOFError) {
5029 PyErr_SetNone(PyExc_EOFError);
5030 }
5031 return NULL;
5032 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005033
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005034 PDATA_POP(self->stack, val);
5035 return val;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005036}
Tim Peters84e87f32001-03-17 04:50:51 +00005037
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005038
Guido van Rossum60456fd1997-04-09 17:36:32 +00005039static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00005040Unpickler_load(Unpicklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005041{
Tim Peterscba30e22003-02-01 06:24:36 +00005042 if (!( PyArg_ParseTuple(args, ":load")))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005043 return NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005044
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005045 return load(self);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005046}
5047
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005048static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00005049Unpickler_noload(Unpicklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005050{
Tim Peterscba30e22003-02-01 06:24:36 +00005051 if (!( PyArg_ParseTuple(args, ":noload")))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005052 return NULL;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005053
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005054 return noload(self);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005055}
5056
Guido van Rossum60456fd1997-04-09 17:36:32 +00005057
5058static struct PyMethodDef Unpickler_methods[] = {
Neal Norwitzb0493252002-03-31 14:44:22 +00005059 {"load", (PyCFunction)Unpickler_load, METH_VARARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00005060 PyDoc_STR("load() -- Load a pickle")
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005061 },
Neal Norwitzb0493252002-03-31 14:44:22 +00005062 {"noload", (PyCFunction)Unpickler_noload, METH_VARARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00005063 PyDoc_STR(
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005064 "noload() -- not load a pickle, but go through most of the motions\n"
5065 "\n"
5066 "This function can be used to read past a pickle without instantiating\n"
5067 "any objects or importing any modules. It can also be used to find all\n"
5068 "persistent references without instantiating any objects or importing\n"
Neal Norwitz200788c2002-08-13 22:20:41 +00005069 "any modules.\n")
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005070 },
Guido van Rossum60456fd1997-04-09 17:36:32 +00005071 {NULL, NULL} /* sentinel */
5072};
5073
5074
5075static Unpicklerobject *
Tim Peterscba30e22003-02-01 06:24:36 +00005076newUnpicklerobject(PyObject *f)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005077{
5078 Unpicklerobject *self;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005079
Tim Peterscba30e22003-02-01 06:24:36 +00005080 if (!( self = PyObject_New(Unpicklerobject, &Unpicklertype)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005081 return NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005082
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005083 self->file = NULL;
5084 self->arg = NULL;
5085 self->stack = (Pdata*)Pdata_New();
5086 self->pers_func = NULL;
5087 self->last_string = NULL;
5088 self->marks = NULL;
5089 self->num_marks = 0;
5090 self->marks_size = 0;
5091 self->buf_size = 0;
5092 self->read = NULL;
5093 self->readline = NULL;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005094 self->find_class = NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005095
Tim Peterscba30e22003-02-01 06:24:36 +00005096 if (!( self->memo = PyDict_New()))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005097 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005098
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005099 Py_INCREF(f);
5100 self->file = f;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005101
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005102 /* Set read, readline based on type of f */
5103 if (PyFile_Check(f)) {
5104 self->fp = PyFile_AsFile(f);
5105 if (self->fp == NULL) {
Tim Peterscba30e22003-02-01 06:24:36 +00005106 PyErr_SetString(PyExc_ValueError,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005107 "I/O operation on closed file");
5108 goto err;
5109 }
5110 self->read_func = read_file;
5111 self->readline_func = readline_file;
Guido van Rossumc91fcaa1999-03-29 20:00:14 +00005112 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005113 else if (PycStringIO_InputCheck(f)) {
5114 self->fp = NULL;
5115 self->read_func = read_cStringIO;
5116 self->readline_func = readline_cStringIO;
5117 }
5118 else {
Guido van Rossum60456fd1997-04-09 17:36:32 +00005119
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005120 self->fp = NULL;
5121 self->read_func = read_other;
5122 self->readline_func = readline_other;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005123
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005124 if (!( (self->readline = PyObject_GetAttr(f, readline_str)) &&
5125 (self->read = PyObject_GetAttr(f, read_str)))) {
5126 PyErr_Clear();
Tim Peterscba30e22003-02-01 06:24:36 +00005127 PyErr_SetString( PyExc_TypeError,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005128 "argument must have 'read' and "
5129 "'readline' attributes" );
5130 goto err;
5131 }
5132 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00005133
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005134 return self;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005135
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005136 err:
5137 Py_DECREF((PyObject *)self);
5138 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005139}
5140
5141
5142static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00005143get_Unpickler(PyObject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005144{
5145 PyObject *file;
Tim Peters84e87f32001-03-17 04:50:51 +00005146
Tim Peterscba30e22003-02-01 06:24:36 +00005147 if (!( PyArg_ParseTuple(args, "O:Unpickler", &file)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005148 return NULL;
5149 return (PyObject *)newUnpicklerobject(file);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005150}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005151
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005152
Guido van Rossum60456fd1997-04-09 17:36:32 +00005153static void
Tim Peterscba30e22003-02-01 06:24:36 +00005154Unpickler_dealloc(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005155{
5156 Py_XDECREF(self->readline);
5157 Py_XDECREF(self->read);
5158 Py_XDECREF(self->file);
5159 Py_XDECREF(self->memo);
5160 Py_XDECREF(self->stack);
5161 Py_XDECREF(self->pers_func);
5162 Py_XDECREF(self->arg);
5163 Py_XDECREF(self->last_string);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005164
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005165 if (self->marks) {
5166 free(self->marks);
5167 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005168
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005169 if (self->buf_size) {
5170 free(self->buf);
5171 }
Tim Peters84e87f32001-03-17 04:50:51 +00005172
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005173 PyObject_Del(self);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005174}
5175
5176
5177static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00005178Unpickler_getattr(Unpicklerobject *self, char *name)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005179{
5180 if (!strcmp(name, "persistent_load")) {
5181 if (!self->pers_func) {
5182 PyErr_SetString(PyExc_AttributeError, name);
5183 return NULL;
5184 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005185
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005186 Py_INCREF(self->pers_func);
5187 return self->pers_func;
5188 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005189
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005190 if (!strcmp(name, "find_global")) {
5191 if (!self->find_class) {
5192 PyErr_SetString(PyExc_AttributeError, name);
5193 return NULL;
5194 }
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00005195
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005196 Py_INCREF(self->find_class);
5197 return self->find_class;
5198 }
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00005199
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005200 if (!strcmp(name, "memo")) {
5201 if (!self->memo) {
5202 PyErr_SetString(PyExc_AttributeError, name);
5203 return NULL;
5204 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005205
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005206 Py_INCREF(self->memo);
5207 return self->memo;
5208 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005209
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005210 if (!strcmp(name, "UnpicklingError")) {
5211 Py_INCREF(UnpicklingError);
5212 return UnpicklingError;
5213 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005214
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005215 return Py_FindMethod(Unpickler_methods, (PyObject *)self, name);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005216}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005217
Guido van Rossum60456fd1997-04-09 17:36:32 +00005218
5219static int
Tim Peterscba30e22003-02-01 06:24:36 +00005220Unpickler_setattr(Unpicklerobject *self, char *name, PyObject *value)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005221{
Jeremy Hyltonce616e41998-08-13 23:13:52 +00005222
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005223 if (!strcmp(name, "persistent_load")) {
5224 Py_XDECREF(self->pers_func);
5225 self->pers_func = value;
5226 Py_XINCREF(value);
5227 return 0;
5228 }
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00005229
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005230 if (!strcmp(name, "find_global")) {
5231 Py_XDECREF(self->find_class);
5232 self->find_class = value;
5233 Py_XINCREF(value);
5234 return 0;
5235 }
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00005236
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005237 if (! value) {
5238 PyErr_SetString(PyExc_TypeError,
5239 "attribute deletion is not supported");
5240 return -1;
5241 }
Jeremy Hyltonce616e41998-08-13 23:13:52 +00005242
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005243 if (strcmp(name, "memo") == 0) {
5244 if (!PyDict_Check(value)) {
Tim Peterscba30e22003-02-01 06:24:36 +00005245 PyErr_SetString(PyExc_TypeError,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005246 "memo must be a dictionary");
5247 return -1;
5248 }
5249 Py_XDECREF(self->memo);
5250 self->memo = value;
5251 Py_INCREF(value);
5252 return 0;
5253 }
Jeremy Hyltonce616e41998-08-13 23:13:52 +00005254
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005255 PyErr_SetString(PyExc_AttributeError, name);
5256 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005257}
5258
Tim Peters5bd2a792003-02-01 16:45:06 +00005259/* ---------------------------------------------------------------------------
5260 * Module-level functions.
5261 */
Guido van Rossum60456fd1997-04-09 17:36:32 +00005262
Tim Peters5bd2a792003-02-01 16:45:06 +00005263/* dump(obj, file, proto=0). */
Guido van Rossum60456fd1997-04-09 17:36:32 +00005264static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00005265cpm_dump(PyObject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005266{
5267 PyObject *ob, *file, *res = NULL;
5268 Picklerobject *pickler = 0;
Tim Peters5bd2a792003-02-01 16:45:06 +00005269 int proto = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005270
Tim Peters5bd2a792003-02-01 16:45:06 +00005271 if (!( PyArg_ParseTuple(args, "OO|i", &ob, &file, &proto)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005272 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005273
Tim Peters5bd2a792003-02-01 16:45:06 +00005274 if (!( pickler = newPicklerobject(file, proto)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005275 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005276
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005277 if (dump(pickler, ob) < 0)
5278 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005279
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005280 Py_INCREF(Py_None);
5281 res = Py_None;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005282
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005283 finally:
5284 Py_XDECREF(pickler);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005285
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005286 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005287}
5288
5289
Tim Peters5bd2a792003-02-01 16:45:06 +00005290/* dumps(obj, proto=0). */
Guido van Rossum60456fd1997-04-09 17:36:32 +00005291static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00005292cpm_dumps(PyObject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005293{
5294 PyObject *ob, *file = 0, *res = NULL;
5295 Picklerobject *pickler = 0;
Tim Peters5bd2a792003-02-01 16:45:06 +00005296 int proto = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005297
Tim Peters5bd2a792003-02-01 16:45:06 +00005298 if (!( PyArg_ParseTuple(args, "O|i:dumps", &ob, &proto)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005299 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005300
Tim Peterscba30e22003-02-01 06:24:36 +00005301 if (!( file = PycStringIO->NewOutput(128)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005302 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005303
Tim Peters5bd2a792003-02-01 16:45:06 +00005304 if (!( pickler = newPicklerobject(file, proto)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005305 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005306
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005307 if (dump(pickler, ob) < 0)
5308 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005309
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005310 res = PycStringIO->cgetvalue(file);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005311
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005312 finally:
5313 Py_XDECREF(pickler);
5314 Py_XDECREF(file);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005315
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005316 return res;
Tim Peters84e87f32001-03-17 04:50:51 +00005317}
5318
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005319
Tim Peters5bd2a792003-02-01 16:45:06 +00005320/* load(fileobj). */
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005321static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00005322cpm_load(PyObject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005323{
5324 Unpicklerobject *unpickler = 0;
5325 PyObject *ob, *res = NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005326
Tim Peterscba30e22003-02-01 06:24:36 +00005327 if (!( PyArg_ParseTuple(args, "O:load", &ob)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005328 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005329
Tim Peterscba30e22003-02-01 06:24:36 +00005330 if (!( unpickler = newUnpicklerobject(ob)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005331 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005332
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005333 res = load(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005334
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005335 finally:
5336 Py_XDECREF(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005337
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005338 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005339}
5340
5341
Tim Peters5bd2a792003-02-01 16:45:06 +00005342/* loads(string) */
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005343static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00005344cpm_loads(PyObject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005345{
5346 PyObject *ob, *file = 0, *res = NULL;
5347 Unpicklerobject *unpickler = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005348
Tim Peterscba30e22003-02-01 06:24:36 +00005349 if (!( PyArg_ParseTuple(args, "S:loads", &ob)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005350 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005351
Tim Peterscba30e22003-02-01 06:24:36 +00005352 if (!( file = PycStringIO->NewInput(ob)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005353 goto finally;
Tim Peters84e87f32001-03-17 04:50:51 +00005354
Tim Peterscba30e22003-02-01 06:24:36 +00005355 if (!( unpickler = newUnpicklerobject(file)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005356 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005357
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005358 res = load(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005359
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005360 finally:
5361 Py_XDECREF(file);
5362 Py_XDECREF(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005363
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005364 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005365}
5366
5367
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005368PyDoc_STRVAR(Unpicklertype__doc__,
5369"Objects that know how to unpickle");
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005370
Guido van Rossum9716aaa1997-12-08 15:15:16 +00005371static PyTypeObject Unpicklertype = {
5372 PyObject_HEAD_INIT(NULL)
Guido van Rossum60456fd1997-04-09 17:36:32 +00005373 0, /*ob_size*/
Guido van Rossum14648392001-12-08 18:02:58 +00005374 "cPickle.Unpickler", /*tp_name*/
Guido van Rossum60456fd1997-04-09 17:36:32 +00005375 sizeof(Unpicklerobject), /*tp_basicsize*/
5376 0, /*tp_itemsize*/
5377 /* methods */
5378 (destructor)Unpickler_dealloc, /*tp_dealloc*/
5379 (printfunc)0, /*tp_print*/
5380 (getattrfunc)Unpickler_getattr, /*tp_getattr*/
5381 (setattrfunc)Unpickler_setattr, /*tp_setattr*/
5382 (cmpfunc)0, /*tp_compare*/
5383 (reprfunc)0, /*tp_repr*/
5384 0, /*tp_as_number*/
5385 0, /*tp_as_sequence*/
5386 0, /*tp_as_mapping*/
5387 (hashfunc)0, /*tp_hash*/
5388 (ternaryfunc)0, /*tp_call*/
5389 (reprfunc)0, /*tp_str*/
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005390
Guido van Rossum60456fd1997-04-09 17:36:32 +00005391 /* Space for future expansion */
5392 0L,0L,0L,0L,
5393 Unpicklertype__doc__ /* Documentation string */
Guido van Rossum9716aaa1997-12-08 15:15:16 +00005394};
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005395
Guido van Rossum60456fd1997-04-09 17:36:32 +00005396static struct PyMethodDef cPickle_methods[] = {
Neal Norwitzb0493252002-03-31 14:44:22 +00005397 {"dump", (PyCFunction)cpm_dump, METH_VARARGS,
Tim Peters5bd2a792003-02-01 16:45:06 +00005398 PyDoc_STR("dump(object, file, proto=0) -- "
5399 "Write an object in pickle format to the given file.\n"
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005400 "\n"
Tim Peters5bd2a792003-02-01 16:45:06 +00005401 "See the Pickler docstring for the meaning of optional argument proto.")
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005402 },
Tim Peters5bd2a792003-02-01 16:45:06 +00005403
Neal Norwitzb0493252002-03-31 14:44:22 +00005404 {"dumps", (PyCFunction)cpm_dumps, METH_VARARGS,
Tim Peters5bd2a792003-02-01 16:45:06 +00005405 PyDoc_STR("dumps(object, proto=0) -- "
5406 "Return a string containing an object in pickle format.\n"
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005407 "\n"
Tim Peters5bd2a792003-02-01 16:45:06 +00005408 "See the Pickler docstring for the meaning of optional argument proto.")
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005409 },
Tim Peters5bd2a792003-02-01 16:45:06 +00005410
Neal Norwitzb0493252002-03-31 14:44:22 +00005411 {"load", (PyCFunction)cpm_load, METH_VARARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00005412 PyDoc_STR("load(file) -- Load a pickle from the given file")},
Tim Peters5bd2a792003-02-01 16:45:06 +00005413
Neal Norwitzb0493252002-03-31 14:44:22 +00005414 {"loads", (PyCFunction)cpm_loads, METH_VARARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00005415 PyDoc_STR("loads(string) -- Load a pickle from the given string")},
Tim Peters5bd2a792003-02-01 16:45:06 +00005416
Neal Norwitzb0493252002-03-31 14:44:22 +00005417 {"Pickler", (PyCFunction)get_Pickler, METH_VARARGS,
Tim Peters5bd2a792003-02-01 16:45:06 +00005418 PyDoc_STR("Pickler(file, proto=0) -- Create a pickler.\n"
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005419 "\n"
Tim Peters5bd2a792003-02-01 16:45:06 +00005420 "This takes a file-like object for writing a pickle data stream.\n"
5421 "The optional proto argument tells the pickler to use the given\n"
5422 "protocol; supported protocols are 0, 1, 2. The default\n"
5423 "protocol is 0, to be backwards compatible. (Protocol 0 is the\n"
5424 "only protocol that can be written to a file opened in text\n"
5425 "mode and read back successfully. When using a protocol higher\n"
5426 "than 0, make sure the file is opened in binary mode, both when\n"
5427 "pickling and unpickling.)\n"
5428 "\n"
5429 "Protocol 1 is more efficient than protocol 0; protocol 2 is\n"
5430 "more efficient than protocol 1.\n"
5431 "\n"
5432 "Specifying a negative protocol version selects the highest\n"
5433 "protocol version supported. The higher the protocol used, the\n"
5434 "more recent the version of Python needed to read the pickle\n"
5435 "produced.\n"
5436 "\n"
5437 "The file parameter must have a write() method that accepts a single\n"
5438 "string argument. It can thus be an open file object, a StringIO\n"
5439 "object, or any other custom object that meets this interface.\n")
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005440 },
Tim Peters5bd2a792003-02-01 16:45:06 +00005441
Neal Norwitzb0493252002-03-31 14:44:22 +00005442 {"Unpickler", (PyCFunction)get_Unpickler, METH_VARARGS,
Tim Peters5bd2a792003-02-01 16:45:06 +00005443 PyDoc_STR("Unpickler(file) -- Create an unpickler.")},
5444
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005445 { NULL, NULL }
5446};
5447
Guido van Rossum60456fd1997-04-09 17:36:32 +00005448static int
Tim Peterscba30e22003-02-01 06:24:36 +00005449init_stuff(PyObject *module_dict)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005450{
5451 PyObject *copy_reg, *t, *r;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005452
Martin v. Löwis43c9d8a2002-04-01 12:34:33 +00005453#define INIT_STR(S) if (!( S ## _str=PyString_InternFromString(#S))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005454
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005455 INIT_STR(__class__);
5456 INIT_STR(__getinitargs__);
5457 INIT_STR(__dict__);
5458 INIT_STR(__getstate__);
5459 INIT_STR(__setstate__);
5460 INIT_STR(__name__);
5461 INIT_STR(__main__);
5462 INIT_STR(__reduce__);
Guido van Rossumb289b872003-02-19 01:45:13 +00005463 INIT_STR(__reduce_ex__);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005464 INIT_STR(write);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005465 INIT_STR(append);
5466 INIT_STR(read);
5467 INIT_STR(readline);
5468 INIT_STR(copy_reg);
5469 INIT_STR(dispatch_table);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005470 INIT_STR(__basicnew__);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005471
Tim Peterscba30e22003-02-01 06:24:36 +00005472 if (!( copy_reg = PyImport_ImportModule("copy_reg")))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005473 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005474
Tim Peters1f1b2d22003-02-01 02:16:37 +00005475 /* This is special because we want to use a different
5476 one in restricted mode. */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005477 dispatch_table = PyObject_GetAttr(copy_reg, dispatch_table_str);
Tim Peters5b7da392003-02-04 00:21:07 +00005478 if (!dispatch_table) return -1;
5479
5480 extension_registry = PyObject_GetAttrString(copy_reg,
Guido van Rossumd4b920c2003-02-04 01:54:49 +00005481 "_extension_registry");
Tim Peters5b7da392003-02-04 00:21:07 +00005482 if (!extension_registry) return -1;
5483
5484 inverted_registry = PyObject_GetAttrString(copy_reg,
Guido van Rossumd4b920c2003-02-04 01:54:49 +00005485 "_inverted_registry");
Tim Peters5b7da392003-02-04 00:21:07 +00005486 if (!inverted_registry) return -1;
5487
5488 extension_cache = PyObject_GetAttrString(copy_reg,
Guido van Rossumd4b920c2003-02-04 01:54:49 +00005489 "_extension_cache");
Tim Peters5b7da392003-02-04 00:21:07 +00005490 if (!extension_cache) return -1;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005491
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005492 Py_DECREF(copy_reg);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005493
Tim Peters731098b2003-02-04 20:56:09 +00005494 if (!(empty_tuple = PyTuple_New(0)))
5495 return -1;
5496
5497 two_tuple = PyTuple_New(2);
5498 if (two_tuple == NULL)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005499 return -1;
Tim Peters3e667d52003-02-04 21:47:44 +00005500 /* We use this temp container with no regard to refcounts, or to
5501 * keeping containees alive. Exempt from GC, because we don't
5502 * want anything looking at two_tuple() by magic.
5503 */
5504 PyObject_GC_UnTrack(two_tuple);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005505
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005506 /* Ugh */
5507 if (!( t=PyImport_ImportModule("__builtin__"))) return -1;
5508 if (PyDict_SetItemString(module_dict, "__builtins__", t) < 0)
5509 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005510
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005511 if (!( t=PyDict_New())) return -1;
5512 if (!( r=PyRun_String(
5513 "def __init__(self, *args): self.args=args\n\n"
5514 "def __str__(self):\n"
5515 " return self.args and ('%s' % self.args[0]) or '(what)'\n",
5516 Py_file_input,
5517 module_dict, t) )) return -1;
5518 Py_DECREF(r);
Guido van Rossumc03158b1999-06-09 15:23:31 +00005519
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005520 PickleError = PyErr_NewException("cPickle.PickleError", NULL, t);
Tim Peterscba30e22003-02-01 06:24:36 +00005521 if (!PickleError)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005522 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00005523
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005524 Py_DECREF(t);
Guido van Rossumc03158b1999-06-09 15:23:31 +00005525
Tim Peterscba30e22003-02-01 06:24:36 +00005526 PicklingError = PyErr_NewException("cPickle.PicklingError",
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005527 PickleError, NULL);
Tim Peterscba30e22003-02-01 06:24:36 +00005528 if (!PicklingError)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005529 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00005530
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005531 if (!( t=PyDict_New())) return -1;
5532 if (!( r=PyRun_String(
5533 "def __init__(self, *args): self.args=args\n\n"
5534 "def __str__(self):\n"
5535 " a=self.args\n"
5536 " a=a and type(a[0]) or '(what)'\n"
5537 " return 'Cannot pickle %s objects' % a\n"
5538 , Py_file_input,
5539 module_dict, t) )) return -1;
5540 Py_DECREF(r);
Tim Peters84e87f32001-03-17 04:50:51 +00005541
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005542 if (!( UnpickleableError = PyErr_NewException(
Tim Peterscba30e22003-02-01 06:24:36 +00005543 "cPickle.UnpickleableError", PicklingError, t)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005544 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00005545
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005546 Py_DECREF(t);
Guido van Rossumc03158b1999-06-09 15:23:31 +00005547
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005548 if (!( UnpicklingError = PyErr_NewException("cPickle.UnpicklingError",
Tim Peterscba30e22003-02-01 06:24:36 +00005549 PickleError, NULL)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005550 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00005551
Martin v. Löwis658009a2002-09-16 17:26:24 +00005552 if (!( BadPickleGet = PyErr_NewException("cPickle.BadPickleGet",
5553 UnpicklingError, NULL)))
5554 return -1;
Tim Peterscba30e22003-02-01 06:24:36 +00005555
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005556 if (PyDict_SetItemString(module_dict, "PickleError",
5557 PickleError) < 0)
5558 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00005559
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005560 if (PyDict_SetItemString(module_dict, "PicklingError",
5561 PicklingError) < 0)
5562 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00005563
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005564 if (PyDict_SetItemString(module_dict, "UnpicklingError",
5565 UnpicklingError) < 0)
5566 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005567
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005568 if (PyDict_SetItemString(module_dict, "UnpickleableError",
5569 UnpickleableError) < 0)
5570 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005571
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005572 if (PyDict_SetItemString(module_dict, "BadPickleGet",
5573 BadPickleGet) < 0)
5574 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00005575
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005576 PycString_IMPORT;
Guido van Rossum053b8df1998-11-25 16:18:00 +00005577
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005578 return 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005579}
5580
Mark Hammondfe51c6d2002-08-02 02:27:13 +00005581#ifndef PyMODINIT_FUNC /* declarations for DLL import/export */
5582#define PyMODINIT_FUNC void
Guido van Rossumf9ffb031999-02-04 14:54:04 +00005583#endif
Mark Hammondfe51c6d2002-08-02 02:27:13 +00005584PyMODINIT_FUNC
Tim Peterscba30e22003-02-01 06:24:36 +00005585initcPickle(void)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005586{
5587 PyObject *m, *d, *di, *v, *k;
5588 int i;
Tim Peters5b7da392003-02-04 00:21:07 +00005589 char *rev = "1.71"; /* XXX when does this change? */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005590 PyObject *format_version;
5591 PyObject *compatible_formats;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005592
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005593 Picklertype.ob_type = &PyType_Type;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005594 Unpicklertype.ob_type = &PyType_Type;
5595 PdataType.ob_type = &PyType_Type;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005596
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005597 /* Initialize some pieces. We need to do this before module creation,
Tim Peters5b7da392003-02-04 00:21:07 +00005598 * so we're forced to use a temporary dictionary. :(
5599 */
5600 di = PyDict_New();
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005601 if (!di) return;
5602 if (init_stuff(di) < 0) return;
Guido van Rossumebba4202000-09-07 14:35:37 +00005603
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005604 /* Create the module and add the functions */
5605 m = Py_InitModule4("cPickle", cPickle_methods,
5606 cPickle_module_documentation,
5607 (PyObject*)NULL,PYTHON_API_VERSION);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005608
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005609 /* Add some symbolic constants to the module */
5610 d = PyModule_GetDict(m);
Tim Peters5b7da392003-02-04 00:21:07 +00005611 v = PyString_FromString(rev);
5612 PyDict_SetItemString(d, "__version__", v);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005613 Py_XDECREF(v);
Barry Warsaw93d29b61997-01-14 17:45:08 +00005614
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005615 /* Copy data from di. Waaa. */
5616 for (i=0; PyDict_Next(di, &i, &k, &v); ) {
5617 if (PyObject_SetItem(d, k, v) < 0) {
5618 Py_DECREF(di);
5619 return;
5620 }
5621 }
5622 Py_DECREF(di);
Guido van Rossumebba4202000-09-07 14:35:37 +00005623
Tim Peters8587b3c2003-02-13 15:44:41 +00005624 i = PyModule_AddIntConstant(m, "HIGHEST_PROTOCOL", HIGHEST_PROTOCOL);
5625 if (i < 0)
5626 return;
5627
Tim Peters5b7da392003-02-04 00:21:07 +00005628 /* These are purely informational; no code uses them. */
5629 /* File format version we write. */
5630 format_version = PyString_FromString("2.0");
5631 /* Format versions we can read. */
5632 compatible_formats = Py_BuildValue("[sssss]",
5633 "1.0", /* Original protocol 0 */
5634 "1.1", /* Protocol 0 + INST */
5635 "1.2", /* Original protocol 1 */
5636 "1.3", /* Protocol 1 + BINFLOAT */
Tim Peters0dd23aa2003-02-04 00:30:46 +00005637 "2.0"); /* Original protocol 2 */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005638 PyDict_SetItemString(d, "format_version", format_version);
5639 PyDict_SetItemString(d, "compatible_formats", compatible_formats);
5640 Py_XDECREF(format_version);
5641 Py_XDECREF(compatible_formats);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005642}