blob: 18baee1c395f3e129519dabbe6b8000d87cf47b3 [file] [log] [blame]
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001#include "Python.h"
2#include "cStringIO.h"
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003#include "structmember.h"
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005PyDoc_STRVAR(cPickle_module_documentation,
Tim Peters64c04d12003-02-01 06:27:59 +00006"C implementation and optimization of the Python pickle module.");
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007
Guido van Rossum142eeb81997-08-13 03:14:41 +00008#ifndef Py_eval_input
9#include <graminit.h>
10#define Py_eval_input eval_input
Guido van Rossumc6ef2041997-08-21 02:30:45 +000011#endif /* Py_eval_input */
Guido van Rossum142eeb81997-08-13 03:14:41 +000012
Guido van Rossum60456fd1997-04-09 17:36:32 +000013#define DEL_LIST_SLICE(list, from, to) (PyList_SetSlice(list, from, to, NULL))
Guido van Rossum2f4caa41997-01-06 22:59:08 +000014
Guido van Rossum60456fd1997-04-09 17:36:32 +000015#define WRITE_BUF_SIZE 256
16
Tim Peters5bd2a792003-02-01 16:45:06 +000017/* Bump this when new opcodes are added to the pickle protocol. */
Tim Peters8587b3c2003-02-13 15:44:41 +000018#define HIGHEST_PROTOCOL 2
Tim Peters5bd2a792003-02-01 16:45:06 +000019
Tim Peters797ec242003-02-01 06:22:36 +000020/*
21 * Pickle opcodes. These must be kept in synch with pickle.py. Extensive
22 * docs are in pickletools.py.
23 */
Guido van Rossum60456fd1997-04-09 17:36:32 +000024#define MARK '('
25#define STOP '.'
26#define POP '0'
27#define POP_MARK '1'
28#define DUP '2'
29#define FLOAT 'F'
Guido van Rossum60456fd1997-04-09 17:36:32 +000030#define BINFLOAT 'G'
Guido van Rossum60456fd1997-04-09 17:36:32 +000031#define INT 'I'
32#define BININT 'J'
33#define BININT1 'K'
34#define LONG 'L'
35#define BININT2 'M'
36#define NONE 'N'
37#define PERSID 'P'
38#define BINPERSID 'Q'
39#define REDUCE 'R'
40#define STRING 'S'
41#define BINSTRING 'T'
Guido van Rossum2f4caa41997-01-06 22:59:08 +000042#define SHORT_BINSTRING 'U'
Guido van Rossum5fccb7c2000-03-10 23:11:40 +000043#define UNICODE 'V'
44#define BINUNICODE 'X'
Guido van Rossum60456fd1997-04-09 17:36:32 +000045#define APPEND 'a'
46#define BUILD 'b'
47#define GLOBAL 'c'
48#define DICT 'd'
49#define EMPTY_DICT '}'
50#define APPENDS 'e'
51#define GET 'g'
52#define BINGET 'h'
53#define INST 'i'
54#define LONG_BINGET 'j'
55#define LIST 'l'
56#define EMPTY_LIST ']'
57#define OBJ 'o'
58#define PUT 'p'
59#define BINPUT 'q'
60#define LONG_BINPUT 'r'
61#define SETITEM 's'
62#define TUPLE 't'
63#define EMPTY_TUPLE ')'
64#define SETITEMS 'u'
Tim Peters797ec242003-02-01 06:22:36 +000065
66/* Protocol 2. */
67#define PROTO '\x80' /* identify pickle protocol */
68#define NEWOBJ '\x81' /* build object by applying cls.__new__ to argtuple */
69#define EXT1 '\x82' /* push object from extension registry; 1-byte index */
70#define EXT2 '\x83' /* ditto, but 2-byte index */
71#define EXT4 '\x84' /* ditto, but 4-byte index */
72#define TUPLE1 '\x85' /* build 1-tuple from stack top */
73#define TUPLE2 '\x86' /* build 2-tuple from two topmost stack items */
74#define TUPLE3 '\x87' /* build 3-tuple from three topmost stack items */
75#define NEWTRUE '\x88' /* push True */
76#define NEWFALSE '\x89' /* push False */
77#define LONG1 '\x8a' /* push long from < 256 bytes */
78#define LONG4 '\x8b' /* push really big long */
79
80/* There aren't opcodes -- they're ways to pickle bools before protocol 2,
81 * so that unpicklers written before bools were introduced unpickle them
82 * as ints, but unpicklers after can recognize that bools were intended.
83 * Note that protocol 2 added direct ways to pickle bools.
84 */
Jack Jansen3a967022002-06-26 20:40:42 +000085#undef TRUE
Guido van Rossume2763392002-04-05 19:30:08 +000086#define TRUE "I01\n"
Jack Jansen3a967022002-06-26 20:40:42 +000087#undef FALSE
Guido van Rossume2763392002-04-05 19:30:08 +000088#define FALSE "I00\n"
Guido van Rossum77f6a652002-04-03 22:41:51 +000089
Tim Peters1092d642003-02-11 21:06:20 +000090/* Keep in synch with pickle.Pickler._BATCHSIZE. This is how many elements
Tim Peters42f08ac2003-02-11 22:43:24 +000091 * batch_list/dict() pumps out before doing APPENDS/SETITEMS. Nothing will
92 * break if this gets out of synch with pickle.py, but it's unclear that
93 * would help anything either.
Tim Peters1092d642003-02-11 21:06:20 +000094 */
95#define BATCHSIZE 1000
96
Guido van Rossum60456fd1997-04-09 17:36:32 +000097static char MARKv = MARK;
Guido van Rossum2f4caa41997-01-06 22:59:08 +000098
Guido van Rossumc03158b1999-06-09 15:23:31 +000099static PyObject *PickleError;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000100static PyObject *PicklingError;
Guido van Rossumc03158b1999-06-09 15:23:31 +0000101static PyObject *UnpickleableError;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000102static PyObject *UnpicklingError;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000103static PyObject *BadPickleGet;
104
Tim Peters5b7da392003-02-04 00:21:07 +0000105/* As the name says, an empty tuple. */
Guido van Rossum60456fd1997-04-09 17:36:32 +0000106static PyObject *empty_tuple;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000107
Georg Brandldffbf5f2008-05-20 07:49:57 +0000108/* copy_reg.dispatch_table, {type_object: pickling_function} */
Tim Peters5b7da392003-02-04 00:21:07 +0000109static PyObject *dispatch_table;
110
111/* For EXT[124] opcodes. */
Georg Brandldffbf5f2008-05-20 07:49:57 +0000112/* copy_reg._extension_registry, {(module_name, function_name): code} */
Tim Peters5b7da392003-02-04 00:21:07 +0000113static PyObject *extension_registry;
Georg Brandldffbf5f2008-05-20 07:49:57 +0000114/* copy_reg._inverted_registry, {code: (module_name, function_name)} */
Tim Peters5b7da392003-02-04 00:21:07 +0000115static PyObject *inverted_registry;
Georg Brandldffbf5f2008-05-20 07:49:57 +0000116/* copy_reg._extension_cache, {code: object} */
Tim Peters5b7da392003-02-04 00:21:07 +0000117static PyObject *extension_cache;
118
Georg Brandldffbf5f2008-05-20 07:49:57 +0000119/* For looking up name pairs in copy_reg._extension_registry. */
Tim Peters731098b2003-02-04 20:56:09 +0000120static PyObject *two_tuple;
121
Guido van Rossum60456fd1997-04-09 17:36:32 +0000122static PyObject *__class___str, *__getinitargs___str, *__dict___str,
123 *__getstate___str, *__setstate___str, *__name___str, *__reduce___str,
Guido van Rossumb289b872003-02-19 01:45:13 +0000124 *__reduce_ex___str,
Tim Peters1f1b2d22003-02-01 02:16:37 +0000125 *write_str, *append_str,
Neal Norwitzb183a252006-04-10 01:03:32 +0000126 *read_str, *readline_str, *__main___str,
Alexandre Vassalotti9510e4a2008-05-11 08:25:28 +0000127 *copyreg_str, *dispatch_table_str;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000128
Guido van Rossum053b8df1998-11-25 16:18:00 +0000129/*************************************************************************
130 Internal Data type for pickle data. */
131
132typedef struct {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000133 PyObject_HEAD
Tim Peters1d63c9f2003-02-02 20:29:39 +0000134 int length; /* number of initial slots in data currently used */
135 int size; /* number of slots in data allocated */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000136 PyObject **data;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000137} Pdata;
138
Tim Peters84e87f32001-03-17 04:50:51 +0000139static void
Tim Peterscba30e22003-02-01 06:24:36 +0000140Pdata_dealloc(Pdata *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000141{
142 int i;
143 PyObject **p;
Tim Peterscba30e22003-02-01 06:24:36 +0000144
Tim Peters1d63c9f2003-02-02 20:29:39 +0000145 for (i = self->length, p = self->data; --i >= 0; p++) {
146 Py_DECREF(*p);
147 }
148 if (self->data)
149 free(self->data);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000150 PyObject_Del(self);
Guido van Rossum053b8df1998-11-25 16:18:00 +0000151}
152
153static PyTypeObject PdataType = {
Martin v. Löwis68192102007-07-21 06:55:02 +0000154 PyVarObject_HEAD_INIT(NULL, 0) "cPickle.Pdata", sizeof(Pdata), 0,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000155 (destructor)Pdata_dealloc,
156 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0L,0L,0L,0L, ""
Guido van Rossum053b8df1998-11-25 16:18:00 +0000157};
158
Christian Heimese93237d2007-12-19 02:37:44 +0000159#define Pdata_Check(O) (Py_TYPE(O) == &PdataType)
Guido van Rossum053b8df1998-11-25 16:18:00 +0000160
161static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +0000162Pdata_New(void)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000163{
164 Pdata *self;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000165
Tim Peters1d63c9f2003-02-02 20:29:39 +0000166 if (!(self = PyObject_New(Pdata, &PdataType)))
167 return NULL;
168 self->size = 8;
169 self->length = 0;
170 self->data = malloc(self->size * sizeof(PyObject*));
171 if (self->data)
172 return (PyObject*)self;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000173 Py_DECREF(self);
174 return PyErr_NoMemory();
Guido van Rossum053b8df1998-11-25 16:18:00 +0000175}
176
Tim Peters84e87f32001-03-17 04:50:51 +0000177static int
Tim Peterscba30e22003-02-01 06:24:36 +0000178stackUnderflow(void)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000179{
180 PyErr_SetString(UnpicklingError, "unpickling stack underflow");
181 return -1;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000182}
183
Tim Peters1d63c9f2003-02-02 20:29:39 +0000184/* Retain only the initial clearto items. If clearto >= the current
185 * number of items, this is a (non-erroneous) NOP.
186 */
Guido van Rossum053b8df1998-11-25 16:18:00 +0000187static int
Tim Peterscba30e22003-02-01 06:24:36 +0000188Pdata_clear(Pdata *self, int clearto)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000189{
190 int i;
191 PyObject **p;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000192
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000193 if (clearto < 0) return stackUnderflow();
194 if (clearto >= self->length) return 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000195
Tim Peters1d63c9f2003-02-02 20:29:39 +0000196 for (i = self->length, p = self->data + clearto;
197 --i >= clearto;
198 p++) {
Neal Norwitzb59d08c2006-07-22 16:20:49 +0000199 Py_CLEAR(*p);
Tim Peters1d63c9f2003-02-02 20:29:39 +0000200 }
201 self->length = clearto;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000202
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000203 return 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000204}
205
Tim Peters84e87f32001-03-17 04:50:51 +0000206static int
Tim Peterscba30e22003-02-01 06:24:36 +0000207Pdata_grow(Pdata *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000208{
Tim Peters1d63c9f2003-02-02 20:29:39 +0000209 int bigger;
210 size_t nbytes;
Neal Norwitzb59d08c2006-07-22 16:20:49 +0000211 PyObject **tmp;
Tim Peters1d63c9f2003-02-02 20:29:39 +0000212
Tim Peters1d63c9f2003-02-02 20:29:39 +0000213 bigger = self->size << 1;
Tim Peterse0a39072003-02-03 15:45:56 +0000214 if (bigger <= 0) /* was 0, or new value overflows */
Tim Peters1d63c9f2003-02-02 20:29:39 +0000215 goto nomemory;
216 if ((int)(size_t)bigger != bigger)
217 goto nomemory;
218 nbytes = (size_t)bigger * sizeof(PyObject *);
219 if (nbytes / sizeof(PyObject *) != (size_t)bigger)
220 goto nomemory;
Neal Norwitzb59d08c2006-07-22 16:20:49 +0000221 tmp = realloc(self->data, nbytes);
222 if (tmp == NULL)
Tim Peters1d63c9f2003-02-02 20:29:39 +0000223 goto nomemory;
Neal Norwitzb59d08c2006-07-22 16:20:49 +0000224 self->data = tmp;
Tim Peters1d63c9f2003-02-02 20:29:39 +0000225 self->size = bigger;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000226 return 0;
Tim Peters1d63c9f2003-02-02 20:29:39 +0000227
228 nomemory:
Tim Peters1d63c9f2003-02-02 20:29:39 +0000229 PyErr_NoMemory();
230 return -1;
Tim Peters84e87f32001-03-17 04:50:51 +0000231}
Guido van Rossum053b8df1998-11-25 16:18:00 +0000232
Tim Peterse0a39072003-02-03 15:45:56 +0000233/* D is a Pdata*. Pop the topmost element and store it into V, which
234 * must be an lvalue holding PyObject*. On stack underflow, UnpicklingError
Tim Peters1d63c9f2003-02-02 20:29:39 +0000235 * is raised and V is set to NULL. D and V may be evaluated several times.
236 */
237#define PDATA_POP(D, V) { \
Tim Peterse0a39072003-02-03 15:45:56 +0000238 if ((D)->length) \
239 (V) = (D)->data[--((D)->length)]; \
240 else { \
241 PyErr_SetString(UnpicklingError, "bad pickle data"); \
242 (V) = NULL; \
243 } \
Guido van Rossum053b8df1998-11-25 16:18:00 +0000244}
245
Tim Peterse0a39072003-02-03 15:45:56 +0000246/* PDATA_PUSH and PDATA_APPEND both push rvalue PyObject* O on to Pdata*
247 * D. If the Pdata stack can't be grown to hold the new value, both
248 * raise MemoryError and execute "return ER". The difference is in ownership
249 * of O after: _PUSH transfers ownership of O from the caller to the stack
250 * (no incref of O is done, and in case of error O is decrefed), while
251 * _APPEND pushes a new reference.
252 */
253
254/* Push O on stack D, giving ownership of O to the stack. */
255#define PDATA_PUSH(D, O, ER) { \
256 if (((Pdata*)(D))->length == ((Pdata*)(D))->size && \
257 Pdata_grow((Pdata*)(D)) < 0) { \
258 Py_DECREF(O); \
259 return ER; \
260 } \
261 ((Pdata*)(D))->data[((Pdata*)(D))->length++] = (O); \
262}
263
264/* Push O on stack D, pushing a new reference. */
265#define PDATA_APPEND(D, O, ER) { \
266 if (((Pdata*)(D))->length == ((Pdata*)(D))->size && \
267 Pdata_grow((Pdata*)(D)) < 0) \
268 return ER; \
269 Py_INCREF(O); \
270 ((Pdata*)(D))->data[((Pdata*)(D))->length++] = (O); \
271}
272
273
Guido van Rossum053b8df1998-11-25 16:18:00 +0000274static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +0000275Pdata_popTuple(Pdata *self, int start)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000276{
277 PyObject *r;
278 int i, j, l;
Tim Peterscba30e22003-02-01 06:24:36 +0000279
Tim Peters1d63c9f2003-02-02 20:29:39 +0000280 l = self->length-start;
281 r = PyTuple_New(l);
282 if (r == NULL)
283 return NULL;
284 for (i = start, j = 0 ; j < l; i++, j++)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000285 PyTuple_SET_ITEM(r, j, self->data[i]);
Tim Peterscba30e22003-02-01 06:24:36 +0000286
Tim Peters1d63c9f2003-02-02 20:29:39 +0000287 self->length = start;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000288 return r;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000289}
290
291static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +0000292Pdata_popList(Pdata *self, int start)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000293{
294 PyObject *r;
295 int i, j, l;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000296
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000297 l=self->length-start;
298 if (!( r=PyList_New(l))) return NULL;
299 for (i=start, j=0 ; j < l; i++, j++)
300 PyList_SET_ITEM(r, j, self->data[i]);
Guido van Rossum053b8df1998-11-25 16:18:00 +0000301
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000302 self->length=start;
303 return r;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000304}
305
Guido van Rossum053b8df1998-11-25 16:18:00 +0000306/*************************************************************************/
307
308#define ARG_TUP(self, o) { \
309 if (self->arg || (self->arg=PyTuple_New(1))) { \
310 Py_XDECREF(PyTuple_GET_ITEM(self->arg,0)); \
311 PyTuple_SET_ITEM(self->arg,0,o); \
312 } \
313 else { \
314 Py_DECREF(o); \
315 } \
316}
317
318#define FREE_ARG_TUP(self) { \
Christian Heimese93237d2007-12-19 02:37:44 +0000319 if (Py_REFCNT(self->arg) > 1) { \
Guido van Rossum053b8df1998-11-25 16:18:00 +0000320 Py_DECREF(self->arg); \
321 self->arg=NULL; \
322 } \
323 }
324
Thomas Wouters3b6448f2000-07-22 23:56:07 +0000325typedef struct Picklerobject {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000326 PyObject_HEAD
327 FILE *fp;
328 PyObject *write;
329 PyObject *file;
330 PyObject *memo;
331 PyObject *arg;
332 PyObject *pers_func;
333 PyObject *inst_pers_func;
Tim Peters797ec242003-02-01 06:22:36 +0000334
335 /* pickle protocol number, >= 0 */
336 int proto;
337
338 /* bool, true if proto > 0 */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000339 int bin;
Tim Peters797ec242003-02-01 06:22:36 +0000340
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000341 int fast; /* Fast mode doesn't save in memo, don't use if circ ref */
Martin v. Löwis18e16552006-02-15 17:27:45 +0000342 int (*write_func)(struct Picklerobject *, const char *, Py_ssize_t);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000343 char *write_buf;
344 int buf_size;
345 PyObject *dispatch_table;
346 int fast_container; /* count nested container dumps */
347 PyObject *fast_memo;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000348} Picklerobject;
349
Barry Warsaw52acb492001-12-21 20:04:22 +0000350#ifndef PY_CPICKLE_FAST_LIMIT
351#define PY_CPICKLE_FAST_LIMIT 50
352#endif
Jeremy Hyltona0fb1772001-10-12 04:11:06 +0000353
Jeremy Hylton938ace62002-07-17 16:30:39 +0000354static PyTypeObject Picklertype;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000355
Thomas Wouters3b6448f2000-07-22 23:56:07 +0000356typedef struct Unpicklerobject {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000357 PyObject_HEAD
358 FILE *fp;
359 PyObject *file;
360 PyObject *readline;
361 PyObject *read;
362 PyObject *memo;
363 PyObject *arg;
364 Pdata *stack;
365 PyObject *mark;
366 PyObject *pers_func;
367 PyObject *last_string;
368 int *marks;
369 int num_marks;
370 int marks_size;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000371 Py_ssize_t (*read_func)(struct Unpicklerobject *, char **, Py_ssize_t);
372 Py_ssize_t (*readline_func)(struct Unpicklerobject *, char **);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000373 int buf_size;
374 char *buf;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000375 PyObject *find_class;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000376} Unpicklerobject;
Tim Peters84e87f32001-03-17 04:50:51 +0000377
Jeremy Hylton938ace62002-07-17 16:30:39 +0000378static PyTypeObject Unpicklertype;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000379
Thomas Wouters4789b3a2000-07-24 11:36:47 +0000380/* Forward decls that need the above structs */
381static int save(Picklerobject *, PyObject *, int);
382static int put2(Picklerobject *, PyObject *);
383
Guido van Rossumd385d591997-04-09 17:47:47 +0000384static
Guido van Rossum60456fd1997-04-09 17:36:32 +0000385PyObject *
Thomas Wouters3b6448f2000-07-22 23:56:07 +0000386cPickle_ErrFormat(PyObject *ErrType, char *stringformat, char *format, ...)
387{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000388 va_list va;
389 PyObject *args=0, *retval=0;
390 va_start(va, format);
Tim Peterscba30e22003-02-01 06:24:36 +0000391
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000392 if (format) args = Py_VaBuildValue(format, va);
393 va_end(va);
394 if (format && ! args) return NULL;
Gregory P. Smithdd96db62008-06-09 04:58:54 +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;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000401 v=PyString_Format(retval, args);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000402 Py_DECREF(retval);
403 Py_DECREF(args);
404 if (! v) return NULL;
405 retval=v;
406 }
407 }
408 else
409 if (args) retval=args;
410 else {
411 PyErr_SetObject(ErrType,Py_None);
412 return NULL;
413 }
414 PyErr_SetObject(ErrType,retval);
415 Py_DECREF(retval);
416 return NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000417}
418
Tim Peters84e87f32001-03-17 04:50:51 +0000419static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000420write_file(Picklerobject *self, const char *s, Py_ssize_t n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000421{
422 size_t nbyteswritten;
Tim Peters84e87f32001-03-17 04:50:51 +0000423
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000424 if (s == NULL) {
425 return 0;
426 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000427
Martin v. Löwis18e16552006-02-15 17:27:45 +0000428 if (n > INT_MAX) {
429 /* String too large */
430 return -1;
431 }
432
Gregory P. Smithc20adf82008-04-07 06:33:21 +0000433 PyFile_IncUseCount((PyFileObject *)self->file);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000434 Py_BEGIN_ALLOW_THREADS
Jeremy Hylton7fa4bfa2002-07-18 20:58:57 +0000435 nbyteswritten = fwrite(s, sizeof(char), n, self->fp);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000436 Py_END_ALLOW_THREADS
Gregory P. Smithc20adf82008-04-07 06:33:21 +0000437 PyFile_DecUseCount((PyFileObject *)self->file);
Jeremy Hylton7fa4bfa2002-07-18 20:58:57 +0000438 if (nbyteswritten != (size_t)n) {
439 PyErr_SetFromErrno(PyExc_IOError);
440 return -1;
441 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000442
Martin v. Löwis18e16552006-02-15 17:27:45 +0000443 return (int)n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000444}
445
Tim Peters84e87f32001-03-17 04:50:51 +0000446static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000447write_cStringIO(Picklerobject *self, const char *s, Py_ssize_t n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000448{
449 if (s == NULL) {
450 return 0;
451 }
Tim Peterscba30e22003-02-01 06:24:36 +0000452
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000453 if (PycStringIO->cwrite((PyObject *)self->file, s, n) != n) {
454 return -1;
455 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000456
Martin v. Löwis18e16552006-02-15 17:27:45 +0000457 return (int)n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000458}
459
Tim Peters84e87f32001-03-17 04:50:51 +0000460static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000461write_none(Picklerobject *self, const char *s, Py_ssize_t n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000462{
463 if (s == NULL) return 0;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000464 if (n > INT_MAX) return -1;
465 return (int)n;
Guido van Rossum142eeb81997-08-13 03:14:41 +0000466}
467
Tim Peters84e87f32001-03-17 04:50:51 +0000468static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000469write_other(Picklerobject *self, const char *s, Py_ssize_t _n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000470{
471 PyObject *py_str = 0, *junk = 0;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000472 int n;
Tim Peterscba30e22003-02-01 06:24:36 +0000473
Martin v. Löwis18e16552006-02-15 17:27:45 +0000474 if (_n > INT_MAX)
475 return -1;
476 n = (int)_n;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000477 if (s == NULL) {
478 if (!( self->buf_size )) return 0;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000479 py_str = PyString_FromStringAndSize(self->write_buf,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000480 self->buf_size);
Tim Peterscba30e22003-02-01 06:24:36 +0000481 if (!py_str)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000482 return -1;
483 }
484 else {
485 if (self->buf_size && (n + self->buf_size) > WRITE_BUF_SIZE) {
486 if (write_other(self, NULL, 0) < 0)
487 return -1;
488 }
Tim Peterscba30e22003-02-01 06:24:36 +0000489
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000490 if (n > WRITE_BUF_SIZE) {
491 if (!( py_str =
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000492 PyString_FromStringAndSize(s, n)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000493 return -1;
494 }
495 else {
496 memcpy(self->write_buf + self->buf_size, s, n);
497 self->buf_size += n;
498 return n;
499 }
500 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000501
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000502 if (self->write) {
503 /* object with write method */
504 ARG_TUP(self, py_str);
505 if (self->arg) {
506 junk = PyObject_Call(self->write, self->arg, NULL);
507 FREE_ARG_TUP(self);
508 }
509 if (junk) Py_DECREF(junk);
510 else return -1;
511 }
512 else
513 PDATA_PUSH(self->file, py_str, -1);
Guido van Rossum60456fd1997-04-09 17:36:32 +0000514
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000515 self->buf_size = 0;
516 return n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000517}
518
519
Martin v. Löwis18e16552006-02-15 17:27:45 +0000520static Py_ssize_t
521read_file(Unpicklerobject *self, char **s, Py_ssize_t n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000522{
523 size_t nbytesread;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000524
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000525 if (self->buf_size == 0) {
526 int size;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000527
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000528 size = ((n < 32) ? 32 : n);
Tim Petersee1a53c2003-02-02 02:57:53 +0000529 if (!( self->buf = (char *)malloc(size))) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000530 PyErr_NoMemory();
531 return -1;
532 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000533
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000534 self->buf_size = size;
535 }
536 else if (n > self->buf_size) {
Kristján Valur Jónsson17b8e972007-04-25 00:10:50 +0000537 char *newbuf = (char *)realloc(self->buf, n);
538 if (!newbuf) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000539 PyErr_NoMemory();
540 return -1;
541 }
Kristján Valur Jónsson17b8e972007-04-25 00:10:50 +0000542 self->buf = newbuf;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000543 self->buf_size = n;
544 }
Tim Peters84e87f32001-03-17 04:50:51 +0000545
Gregory P. Smithc20adf82008-04-07 06:33:21 +0000546 PyFile_IncUseCount((PyFileObject *)self->file);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000547 Py_BEGIN_ALLOW_THREADS
548 nbytesread = fread(self->buf, sizeof(char), n, self->fp);
549 Py_END_ALLOW_THREADS
Gregory P. Smithc20adf82008-04-07 06:33:21 +0000550 PyFile_DecUseCount((PyFileObject *)self->file);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000551 if (nbytesread != (size_t)n) {
552 if (feof(self->fp)) {
553 PyErr_SetNone(PyExc_EOFError);
554 return -1;
555 }
Tim Peterscba30e22003-02-01 06:24:36 +0000556
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000557 PyErr_SetFromErrno(PyExc_IOError);
558 return -1;
559 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000560
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000561 *s = self->buf;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000562
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000563 return n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000564}
565
566
Martin v. Löwis18e16552006-02-15 17:27:45 +0000567static Py_ssize_t
Tim Peterscba30e22003-02-01 06:24:36 +0000568readline_file(Unpicklerobject *self, char **s)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000569{
570 int i;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000571
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000572 if (self->buf_size == 0) {
Tim Petersee1a53c2003-02-02 02:57:53 +0000573 if (!( self->buf = (char *)malloc(40))) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000574 PyErr_NoMemory();
575 return -1;
576 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000577 self->buf_size = 40;
578 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000579
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000580 i = 0;
581 while (1) {
Tim Petersee1a53c2003-02-02 02:57:53 +0000582 int bigger;
Kristján Valur Jónsson17b8e972007-04-25 00:10:50 +0000583 char *newbuf;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000584 for (; i < (self->buf_size - 1); i++) {
Tim Peterscba30e22003-02-01 06:24:36 +0000585 if (feof(self->fp) ||
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000586 (self->buf[i] = getc(self->fp)) == '\n') {
587 self->buf[i + 1] = '\0';
588 *s = self->buf;
589 return i + 1;
590 }
591 }
Tim Petersee1a53c2003-02-02 02:57:53 +0000592 bigger = self->buf_size << 1;
593 if (bigger <= 0) { /* overflow */
594 PyErr_NoMemory();
595 return -1;
596 }
Kristján Valur Jónsson17b8e972007-04-25 00:10:50 +0000597 newbuf = (char *)realloc(self->buf, bigger);
598 if (!newbuf) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000599 PyErr_NoMemory();
600 return -1;
601 }
Kristján Valur Jónsson17b8e972007-04-25 00:10:50 +0000602 self->buf = newbuf;
Tim Petersee1a53c2003-02-02 02:57:53 +0000603 self->buf_size = bigger;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000604 }
Tim Peters84e87f32001-03-17 04:50:51 +0000605}
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000606
607
Martin v. Löwis18e16552006-02-15 17:27:45 +0000608static Py_ssize_t
609read_cStringIO(Unpicklerobject *self, char **s, Py_ssize_t n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000610{
611 char *ptr;
Tim Peterscba30e22003-02-01 06:24:36 +0000612
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000613 if (PycStringIO->cread((PyObject *)self->file, &ptr, n) != n) {
614 PyErr_SetNone(PyExc_EOFError);
615 return -1;
616 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000617
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000618 *s = ptr;
Tim Peterscba30e22003-02-01 06:24:36 +0000619
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000620 return n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000621}
622
623
Martin v. Löwis18e16552006-02-15 17:27:45 +0000624static Py_ssize_t
Tim Peterscba30e22003-02-01 06:24:36 +0000625readline_cStringIO(Unpicklerobject *self, char **s)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000626{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000627 Py_ssize_t n;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000628 char *ptr;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000629
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000630 if ((n = PycStringIO->creadline((PyObject *)self->file, &ptr)) < 0) {
631 return -1;
632 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000633
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000634 *s = ptr;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000635
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000636 return n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000637}
638
639
Martin v. Löwis18e16552006-02-15 17:27:45 +0000640static Py_ssize_t
641read_other(Unpicklerobject *self, char **s, Py_ssize_t n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000642{
643 PyObject *bytes, *str=0;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000644
Martin v. Löwis18e16552006-02-15 17:27:45 +0000645 if (!( bytes = PyInt_FromSsize_t(n))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000646
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000647 ARG_TUP(self, bytes);
648 if (self->arg) {
649 str = PyObject_Call(self->read, self->arg, NULL);
650 FREE_ARG_TUP(self);
651 }
652 if (! str) return -1;
Tim Peterscba30e22003-02-01 06:24:36 +0000653
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000654 Py_XDECREF(self->last_string);
655 self->last_string = str;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000656
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000657 if (! (*s = PyString_AsString(str))) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000658 return n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000659}
660
661
Martin v. Löwis18e16552006-02-15 17:27:45 +0000662static Py_ssize_t
Tim Peterscba30e22003-02-01 06:24:36 +0000663readline_other(Unpicklerobject *self, char **s)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000664{
665 PyObject *str;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000666 Py_ssize_t str_size;
Tim Peterscba30e22003-02-01 06:24:36 +0000667
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000668 if (!( str = PyObject_CallObject(self->readline, empty_tuple))) {
669 return -1;
670 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000671
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000672 if ((str_size = PyString_Size(str)) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000673 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000674
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000675 Py_XDECREF(self->last_string);
676 self->last_string = str;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000677
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000678 if (! (*s = PyString_AsString(str)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000679 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000680
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000681 return str_size;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000682}
683
Tim Petersee1a53c2003-02-02 02:57:53 +0000684/* Copy the first n bytes from s into newly malloc'ed memory, plus a
685 * trailing 0 byte. Return a pointer to that, or NULL if out of memory.
686 * The caller is responsible for free()'ing the return value.
687 */
Guido van Rossum60456fd1997-04-09 17:36:32 +0000688static char *
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000689pystrndup(const char *s, int n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000690{
Tim Petersee1a53c2003-02-02 02:57:53 +0000691 char *r = (char *)malloc(n+1);
692 if (r == NULL)
693 return (char*)PyErr_NoMemory();
694 memcpy(r, s, n);
695 r[n] = 0;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000696 return r;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000697}
698
699
700static int
Tim Peterscba30e22003-02-01 06:24:36 +0000701get(Picklerobject *self, PyObject *id)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000702{
703 PyObject *value, *mv;
704 long c_value;
705 char s[30];
706 size_t len;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000707
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000708 if (!( mv = PyDict_GetItem(self->memo, id))) {
709 PyErr_SetObject(PyExc_KeyError, id);
710 return -1;
711 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000712
Tim Peterscba30e22003-02-01 06:24:36 +0000713 if (!( value = PyTuple_GetItem(mv, 0)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000714 return -1;
Tim Peters84e87f32001-03-17 04:50:51 +0000715
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000716 if (!( PyInt_Check(value))) {
717 PyErr_SetString(PicklingError, "no int where int expected in memo");
718 return -1;
719 }
720 c_value = PyInt_AS_LONG((PyIntObject*)value);
Guido van Rossum60456fd1997-04-09 17:36:32 +0000721
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000722 if (!self->bin) {
723 s[0] = GET;
724 PyOS_snprintf(s + 1, sizeof(s) - 1, "%ld\n", c_value);
725 len = strlen(s);
726 }
727 else if (Pdata_Check(self->file)) {
728 if (write_other(self, NULL, 0) < 0) return -1;
729 PDATA_APPEND(self->file, mv, -1);
730 return 0;
731 }
732 else {
733 if (c_value < 256) {
734 s[0] = BINGET;
735 s[1] = (int)(c_value & 0xff);
736 len = 2;
737 }
738 else {
739 s[0] = LONG_BINGET;
740 s[1] = (int)(c_value & 0xff);
741 s[2] = (int)((c_value >> 8) & 0xff);
742 s[3] = (int)((c_value >> 16) & 0xff);
743 s[4] = (int)((c_value >> 24) & 0xff);
744 len = 5;
745 }
746 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000747
Tim Peters0bc93f52003-02-02 18:29:33 +0000748 if (self->write_func(self, s, len) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000749 return -1;
Tim Peterscba30e22003-02-01 06:24:36 +0000750
Jeremy Hyltona0fb1772001-10-12 04:11:06 +0000751 return 0;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000752}
Jeremy Hyltonce616e41998-08-13 23:13:52 +0000753
Guido van Rossum60456fd1997-04-09 17:36:32 +0000754
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000755static int
Tim Peterscba30e22003-02-01 06:24:36 +0000756put(Picklerobject *self, PyObject *ob)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000757{
Christian Heimese93237d2007-12-19 02:37:44 +0000758 if (Py_REFCNT(ob) < 2 || self->fast)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000759 return 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000760
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000761 return put2(self, ob);
762}
Guido van Rossum053b8df1998-11-25 16:18:00 +0000763
Guido van Rossum053b8df1998-11-25 16:18:00 +0000764
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000765static int
Tim Peterscba30e22003-02-01 06:24:36 +0000766put2(Picklerobject *self, PyObject *ob)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000767{
768 char c_str[30];
769 int p;
770 size_t len;
771 int res = -1;
772 PyObject *py_ob_id = 0, *memo_len = 0, *t = 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000773
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000774 if (self->fast)
775 return 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000776
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000777 if ((p = PyDict_Size(self->memo)) < 0)
778 goto finally;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000779
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000780 /* Make sure memo keys are positive! */
Tim Peters70b02d72003-02-02 17:26:40 +0000781 /* XXX Why?
782 * XXX And does "positive" really mean non-negative?
783 * XXX pickle.py starts with PUT index 0, not 1. This makes for
784 * XXX gratuitous differences between the pickling modules.
785 */
Tim Peterscba30e22003-02-01 06:24:36 +0000786 p++;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000787
Tim Peterscba30e22003-02-01 06:24:36 +0000788 if (!( py_ob_id = PyLong_FromVoidPtr(ob)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000789 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000790
Tim Peterscba30e22003-02-01 06:24:36 +0000791 if (!( memo_len = PyInt_FromLong(p)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000792 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000793
Tim Peterscba30e22003-02-01 06:24:36 +0000794 if (!( t = PyTuple_New(2)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000795 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000796
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000797 PyTuple_SET_ITEM(t, 0, memo_len);
798 Py_INCREF(memo_len);
799 PyTuple_SET_ITEM(t, 1, ob);
800 Py_INCREF(ob);
801
802 if (PyDict_SetItem(self->memo, py_ob_id, t) < 0)
803 goto finally;
804
805 if (!self->bin) {
806 c_str[0] = PUT;
807 PyOS_snprintf(c_str + 1, sizeof(c_str) - 1, "%d\n", p);
808 len = strlen(c_str);
809 }
810 else if (Pdata_Check(self->file)) {
811 if (write_other(self, NULL, 0) < 0) return -1;
812 PDATA_APPEND(self->file, memo_len, -1);
813 res=0; /* Job well done ;) */
814 goto finally;
815 }
816 else {
817 if (p >= 256) {
818 c_str[0] = LONG_BINPUT;
819 c_str[1] = (int)(p & 0xff);
820 c_str[2] = (int)((p >> 8) & 0xff);
821 c_str[3] = (int)((p >> 16) & 0xff);
822 c_str[4] = (int)((p >> 24) & 0xff);
823 len = 5;
824 }
825 else {
826 c_str[0] = BINPUT;
827 c_str[1] = p;
828 len = 2;
829 }
830 }
831
Tim Peters0bc93f52003-02-02 18:29:33 +0000832 if (self->write_func(self, c_str, len) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000833 goto finally;
834
835 res = 0;
836
837 finally:
838 Py_XDECREF(py_ob_id);
839 Py_XDECREF(memo_len);
840 Py_XDECREF(t);
841
842 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000843}
844
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000845static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +0000846whichmodule(PyObject *global, PyObject *global_name)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000847{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000848 Py_ssize_t i, j;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000849 PyObject *module = 0, *modules_dict = 0,
850 *global_name_attr = 0, *name = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000851
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000852 module = PyObject_GetAttrString(global, "__module__");
Jeremy Hyltonf00368f2003-06-16 20:19:49 +0000853 if (module)
854 return module;
855 if (PyErr_ExceptionMatches(PyExc_AttributeError))
856 PyErr_Clear();
857 else
858 return NULL;
Guido van Rossum45188231997-09-28 05:38:51 +0000859
Tim Peterscba30e22003-02-01 06:24:36 +0000860 if (!( modules_dict = PySys_GetObject("modules")))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000861 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000862
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000863 i = 0;
864 while ((j = PyDict_Next(modules_dict, &i, &name, &module))) {
Guido van Rossum142eeb81997-08-13 03:14:41 +0000865
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000866 if (PyObject_Compare(name, __main___str)==0) continue;
Tim Peters84e87f32001-03-17 04:50:51 +0000867
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000868 global_name_attr = PyObject_GetAttr(module, global_name);
869 if (!global_name_attr) {
Jeremy Hyltonf00368f2003-06-16 20:19:49 +0000870 if (PyErr_ExceptionMatches(PyExc_AttributeError))
871 PyErr_Clear();
872 else
873 return NULL;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000874 continue;
875 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000876
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000877 if (global_name_attr != global) {
878 Py_DECREF(global_name_attr);
879 continue;
880 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000881
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000882 Py_DECREF(global_name_attr);
Guido van Rossum60456fd1997-04-09 17:36:32 +0000883
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000884 break;
885 }
Guido van Rossum142eeb81997-08-13 03:14:41 +0000886
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000887 /* The following implements the rule in pickle.py added in 1.5
888 that used __main__ if no module is found. I don't actually
889 like this rule. jlf
890 */
891 if (!j) {
892 j=1;
893 name=__main___str;
894 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000895
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000896 Py_INCREF(name);
897 return name;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000898}
899
900
Guido van Rossum60456fd1997-04-09 17:36:32 +0000901static int
Jeremy Hylton499ab6a2001-10-15 21:37:58 +0000902fast_save_enter(Picklerobject *self, PyObject *obj)
903{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000904 /* if fast_container < 0, we're doing an error exit. */
905 if (++self->fast_container >= PY_CPICKLE_FAST_LIMIT) {
906 PyObject *key = NULL;
907 if (self->fast_memo == NULL) {
908 self->fast_memo = PyDict_New();
909 if (self->fast_memo == NULL) {
910 self->fast_container = -1;
911 return 0;
912 }
913 }
914 key = PyLong_FromVoidPtr(obj);
915 if (key == NULL)
916 return 0;
917 if (PyDict_GetItem(self->fast_memo, key)) {
Tim Peters4e52ca82002-12-07 02:43:28 +0000918 Py_DECREF(key);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000919 PyErr_Format(PyExc_ValueError,
Tim Peters92c8bb32003-02-13 23:00:26 +0000920 "fast mode: can't pickle cyclic objects "
921 "including object type %s at %p",
Christian Heimese93237d2007-12-19 02:37:44 +0000922 Py_TYPE(obj)->tp_name, obj);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000923 self->fast_container = -1;
924 return 0;
925 }
926 if (PyDict_SetItem(self->fast_memo, key, Py_None) < 0) {
Tim Peters4e52ca82002-12-07 02:43:28 +0000927 Py_DECREF(key);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000928 self->fast_container = -1;
929 return 0;
930 }
Tim Peters4e52ca82002-12-07 02:43:28 +0000931 Py_DECREF(key);
Jeremy Hylton499ab6a2001-10-15 21:37:58 +0000932 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000933 return 1;
Jeremy Hylton499ab6a2001-10-15 21:37:58 +0000934}
935
Tim Peterscba30e22003-02-01 06:24:36 +0000936int
Jeremy Hylton499ab6a2001-10-15 21:37:58 +0000937fast_save_leave(Picklerobject *self, PyObject *obj)
938{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000939 if (self->fast_container-- >= PY_CPICKLE_FAST_LIMIT) {
940 PyObject *key = PyLong_FromVoidPtr(obj);
941 if (key == NULL)
942 return 0;
943 if (PyDict_DelItem(self->fast_memo, key) < 0) {
Tim Peters4e52ca82002-12-07 02:43:28 +0000944 Py_DECREF(key);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000945 return 0;
946 }
Tim Peters4e52ca82002-12-07 02:43:28 +0000947 Py_DECREF(key);
Jeremy Hylton499ab6a2001-10-15 21:37:58 +0000948 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000949 return 1;
Jeremy Hylton499ab6a2001-10-15 21:37:58 +0000950}
951
952static int
Tim Peterscba30e22003-02-01 06:24:36 +0000953save_none(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000954{
955 static char none = NONE;
Tim Peters0bc93f52003-02-02 18:29:33 +0000956 if (self->write_func(self, &none, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000957 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000958
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000959 return 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000960}
961
Guido van Rossum77f6a652002-04-03 22:41:51 +0000962static int
Tim Peterscba30e22003-02-01 06:24:36 +0000963save_bool(Picklerobject *self, PyObject *args)
Guido van Rossum77f6a652002-04-03 22:41:51 +0000964{
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000965 static const char *buf[2] = {FALSE, TRUE};
Guido van Rossume2763392002-04-05 19:30:08 +0000966 static char len[2] = {sizeof(FALSE)-1, sizeof(TRUE)-1};
Guido van Rossum77f6a652002-04-03 22:41:51 +0000967 long l = PyInt_AS_LONG((PyIntObject *)args);
968
Tim Peters3c67d792003-02-02 17:59:11 +0000969 if (self->proto >= 2) {
970 char opcode = l ? NEWTRUE : NEWFALSE;
971 if (self->write_func(self, &opcode, 1) < 0)
972 return -1;
973 }
974 else if (self->write_func(self, buf[l], len[l]) < 0)
Guido van Rossum77f6a652002-04-03 22:41:51 +0000975 return -1;
Guido van Rossum77f6a652002-04-03 22:41:51 +0000976 return 0;
977}
Tim Peters84e87f32001-03-17 04:50:51 +0000978
Guido van Rossum60456fd1997-04-09 17:36:32 +0000979static int
Tim Peterscba30e22003-02-01 06:24:36 +0000980save_int(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000981{
982 char c_str[32];
983 long l = PyInt_AS_LONG((PyIntObject *)args);
984 int len = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000985
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000986 if (!self->bin
Guido van Rossumde8d6d71997-05-13 18:00:44 +0000987#if SIZEOF_LONG > 4
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000988 || l > 0x7fffffffL
989 || l < -0x80000000L
Guido van Rossumde8d6d71997-05-13 18:00:44 +0000990#endif
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000991 ) {
992 /* Text-mode pickle, or long too big to fit in the 4-byte
993 * signed BININT format: store as a string.
994 */
995 c_str[0] = INT;
996 PyOS_snprintf(c_str + 1, sizeof(c_str) - 1, "%ld\n", l);
Tim Peters0bc93f52003-02-02 18:29:33 +0000997 if (self->write_func(self, c_str, strlen(c_str)) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000998 return -1;
999 }
1000 else {
1001 /* Binary pickle and l fits in a signed 4-byte int. */
1002 c_str[1] = (int)( l & 0xff);
1003 c_str[2] = (int)((l >> 8) & 0xff);
1004 c_str[3] = (int)((l >> 16) & 0xff);
1005 c_str[4] = (int)((l >> 24) & 0xff);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001006
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001007 if ((c_str[4] == 0) && (c_str[3] == 0)) {
1008 if (c_str[2] == 0) {
1009 c_str[0] = BININT1;
1010 len = 2;
1011 }
1012 else {
1013 c_str[0] = BININT2;
1014 len = 3;
1015 }
1016 }
1017 else {
1018 c_str[0] = BININT;
1019 len = 5;
1020 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001021
Tim Peters0bc93f52003-02-02 18:29:33 +00001022 if (self->write_func(self, c_str, len) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001023 return -1;
1024 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001025
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001026 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001027}
1028
1029
1030static int
Tim Peterscba30e22003-02-01 06:24:36 +00001031save_long(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001032{
Armin Rigo7ccbca92006-10-04 12:17:45 +00001033 Py_ssize_t size;
Tim Petersee1a53c2003-02-02 02:57:53 +00001034 int res = -1;
1035 PyObject *repr = NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001036
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001037 static char l = LONG;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001038
Tim Petersee1a53c2003-02-02 02:57:53 +00001039 if (self->proto >= 2) {
1040 /* Linear-time pickling. */
1041 size_t nbits;
1042 size_t nbytes;
1043 unsigned char *pdata;
1044 char c_str[5];
1045 int i;
1046 int sign = _PyLong_Sign(args);
1047
1048 if (sign == 0) {
1049 /* It's 0 -- an empty bytestring. */
1050 c_str[0] = LONG1;
1051 c_str[1] = 0;
1052 i = self->write_func(self, c_str, 2);
1053 if (i < 0) goto finally;
1054 res = 0;
1055 goto finally;
1056 }
1057 nbits = _PyLong_NumBits(args);
1058 if (nbits == (size_t)-1 && PyErr_Occurred())
1059 goto finally;
1060 /* How many bytes do we need? There are nbits >> 3 full
1061 * bytes of data, and nbits & 7 leftover bits. If there
1062 * are any leftover bits, then we clearly need another
1063 * byte. Wnat's not so obvious is that we *probably*
1064 * need another byte even if there aren't any leftovers:
1065 * the most-significant bit of the most-significant byte
1066 * acts like a sign bit, and it's usually got a sense
1067 * opposite of the one we need. The exception is longs
1068 * of the form -(2**(8*j-1)) for j > 0. Such a long is
1069 * its own 256's-complement, so has the right sign bit
1070 * even without the extra byte. That's a pain to check
1071 * for in advance, though, so we always grab an extra
1072 * byte at the start, and cut it back later if possible.
1073 */
1074 nbytes = (nbits >> 3) + 1;
Armin Rigo7ccbca92006-10-04 12:17:45 +00001075 if (nbytes > INT_MAX) {
Tim Petersee1a53c2003-02-02 02:57:53 +00001076 PyErr_SetString(PyExc_OverflowError, "long too large "
1077 "to pickle");
1078 goto finally;
1079 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001080 repr = PyString_FromStringAndSize(NULL, (int)nbytes);
Tim Petersee1a53c2003-02-02 02:57:53 +00001081 if (repr == NULL) goto finally;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001082 pdata = (unsigned char *)PyString_AS_STRING(repr);
Tim Petersee1a53c2003-02-02 02:57:53 +00001083 i = _PyLong_AsByteArray((PyLongObject *)args,
1084 pdata, nbytes,
1085 1 /* little endian */, 1 /* signed */);
1086 if (i < 0) goto finally;
1087 /* If the long is negative, this may be a byte more than
1088 * needed. This is so iff the MSB is all redundant sign
1089 * bits.
1090 */
1091 if (sign < 0 && nbytes > 1 && pdata[nbytes - 1] == 0xff &&
1092 (pdata[nbytes - 2] & 0x80) != 0)
1093 --nbytes;
1094
1095 if (nbytes < 256) {
1096 c_str[0] = LONG1;
1097 c_str[1] = (char)nbytes;
1098 size = 2;
1099 }
1100 else {
1101 c_str[0] = LONG4;
1102 size = (int)nbytes;
1103 for (i = 1; i < 5; i++) {
1104 c_str[i] = (char)(size & 0xff);
1105 size >>= 8;
1106 }
1107 size = 5;
1108 }
1109 i = self->write_func(self, c_str, size);
1110 if (i < 0) goto finally;
1111 i = self->write_func(self, (char *)pdata, (int)nbytes);
1112 if (i < 0) goto finally;
1113 res = 0;
1114 goto finally;
1115 }
1116
1117 /* proto < 2: write the repr and newline. This is quadratic-time
1118 * (in the number of digits), in both directions.
1119 */
Tim Peterscba30e22003-02-01 06:24:36 +00001120 if (!( repr = PyObject_Repr(args)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001121 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001122
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001123 if ((size = PyString_Size(repr)) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001124 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001125
Tim Peters0bc93f52003-02-02 18:29:33 +00001126 if (self->write_func(self, &l, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001127 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001128
Tim Peters0bc93f52003-02-02 18:29:33 +00001129 if (self->write_func(self,
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001130 PyString_AS_STRING((PyStringObject *)repr),
Tim Peters0bc93f52003-02-02 18:29:33 +00001131 size) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001132 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001133
Tim Peters0bc93f52003-02-02 18:29:33 +00001134 if (self->write_func(self, "\n", 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001135 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001136
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001137 res = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001138
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001139 finally:
1140 Py_XDECREF(repr);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001141 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001142}
1143
1144
1145static int
Tim Peterscba30e22003-02-01 06:24:36 +00001146save_float(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001147{
1148 double x = PyFloat_AS_DOUBLE((PyFloatObject *)args);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001149
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001150 if (self->bin) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001151 char str[9];
Tim Peters9905b942003-03-20 20:53:32 +00001152 str[0] = BINFLOAT;
1153 if (_PyFloat_Pack8(x, (unsigned char *)&str[1], 0) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001154 return -1;
Tim Peters0bc93f52003-02-02 18:29:33 +00001155 if (self->write_func(self, str, 9) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001156 return -1;
1157 }
1158 else {
1159 char c_str[250];
1160 c_str[0] = FLOAT;
Georg Brandlde9b6242006-04-30 11:13:56 +00001161 PyOS_ascii_formatd(c_str + 1, sizeof(c_str) - 2, "%.17g", x);
1162 /* Extend the formatted string with a newline character */
1163 strcat(c_str, "\n");
Guido van Rossum60456fd1997-04-09 17:36:32 +00001164
Tim Peters0bc93f52003-02-02 18:29:33 +00001165 if (self->write_func(self, c_str, strlen(c_str)) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001166 return -1;
1167 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001168
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001169 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001170}
1171
1172
1173static int
Tim Peterscba30e22003-02-01 06:24:36 +00001174save_string(Picklerobject *self, PyObject *args, int doput)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001175{
1176 int size, len;
1177 PyObject *repr=0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001178
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001179 if ((size = PyString_Size(args)) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001180 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001181
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001182 if (!self->bin) {
1183 char *repr_str;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001184
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001185 static char string = STRING;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001186
Tim Peterscba30e22003-02-01 06:24:36 +00001187 if (!( repr = PyObject_Repr(args)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001188 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001189
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001190 if ((len = PyString_Size(repr)) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001191 goto err;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001192 repr_str = PyString_AS_STRING((PyStringObject *)repr);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001193
Tim Peters0bc93f52003-02-02 18:29:33 +00001194 if (self->write_func(self, &string, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001195 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001196
Tim Peters0bc93f52003-02-02 18:29:33 +00001197 if (self->write_func(self, repr_str, len) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001198 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001199
Tim Peters0bc93f52003-02-02 18:29:33 +00001200 if (self->write_func(self, "\n", 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001201 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001202
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001203 Py_XDECREF(repr);
1204 }
1205 else {
1206 int i;
1207 char c_str[5];
Guido van Rossum60456fd1997-04-09 17:36:32 +00001208
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001209 if ((size = PyString_Size(args)) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001210 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001211
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001212 if (size < 256) {
1213 c_str[0] = SHORT_BINSTRING;
1214 c_str[1] = size;
1215 len = 2;
1216 }
Armin Rigo7ccbca92006-10-04 12:17:45 +00001217 else if (size <= INT_MAX) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001218 c_str[0] = BINSTRING;
1219 for (i = 1; i < 5; i++)
1220 c_str[i] = (int)(size >> ((i - 1) * 8));
1221 len = 5;
1222 }
Armin Rigo7ccbca92006-10-04 12:17:45 +00001223 else
1224 return -1; /* string too large */
Guido van Rossum60456fd1997-04-09 17:36:32 +00001225
Tim Peters0bc93f52003-02-02 18:29:33 +00001226 if (self->write_func(self, c_str, len) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001227 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001228
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001229 if (size > 128 && Pdata_Check(self->file)) {
1230 if (write_other(self, NULL, 0) < 0) return -1;
1231 PDATA_APPEND(self->file, args, -1);
1232 }
1233 else {
Tim Peters0bc93f52003-02-02 18:29:33 +00001234 if (self->write_func(self,
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001235 PyString_AS_STRING(
1236 (PyStringObject *)args),
Tim Peters0bc93f52003-02-02 18:29:33 +00001237 size) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001238 return -1;
1239 }
1240 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001241
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001242 if (doput)
1243 if (put(self, args) < 0)
1244 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001245
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001246 return 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +00001247
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001248 err:
1249 Py_XDECREF(repr);
1250 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001251}
1252
1253
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001254#ifdef Py_USING_UNICODE
Guido van Rossumfb10c3f2000-12-19 02:08:38 +00001255/* A copy of PyUnicode_EncodeRawUnicodeEscape() that also translates
1256 backslash and newline characters to \uXXXX escapes. */
1257static PyObject *
Alexandre Vassalotti966322f2008-12-27 07:16:40 +00001258modified_EncodeRawUnicodeEscape(const Py_UNICODE *s, Py_ssize_t size)
Guido van Rossumfb10c3f2000-12-19 02:08:38 +00001259{
Alexandre Vassalotti966322f2008-12-27 07:16:40 +00001260 PyObject *repr;
1261 char *p;
1262 char *q;
Guido van Rossumfb10c3f2000-12-19 02:08:38 +00001263
Alexandre Vassalotti966322f2008-12-27 07:16:40 +00001264 static const char *hexdigit = "0123456789abcdef";
1265#ifdef Py_UNICODE_WIDE
1266 const Py_ssize_t expandsize = 10;
1267#else
1268 const Py_ssize_t expandsize = 6;
1269#endif
Guido van Rossumfb10c3f2000-12-19 02:08:38 +00001270
Alexandre Vassalotti966322f2008-12-27 07:16:40 +00001271 if (size > PY_SSIZE_T_MAX / expandsize)
1272 return PyErr_NoMemory();
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001273
Alexandre Vassalotti966322f2008-12-27 07:16:40 +00001274 repr = PyString_FromStringAndSize(NULL, expandsize * size);
1275 if (repr == NULL)
1276 return NULL;
1277 if (size == 0)
Guido van Rossumfb10c3f2000-12-19 02:08:38 +00001278 return repr;
Guido van Rossumfb10c3f2000-12-19 02:08:38 +00001279
Alexandre Vassalotti966322f2008-12-27 07:16:40 +00001280 p = q = PyString_AS_STRING(repr);
1281 while (size-- > 0) {
1282 Py_UNICODE ch = *s++;
1283#ifdef Py_UNICODE_WIDE
1284 /* Map 32-bit characters to '\Uxxxxxxxx' */
1285 if (ch >= 0x10000) {
1286 *p++ = '\\';
1287 *p++ = 'U';
1288 *p++ = hexdigit[(ch >> 28) & 0xf];
1289 *p++ = hexdigit[(ch >> 24) & 0xf];
1290 *p++ = hexdigit[(ch >> 20) & 0xf];
1291 *p++ = hexdigit[(ch >> 16) & 0xf];
1292 *p++ = hexdigit[(ch >> 12) & 0xf];
1293 *p++ = hexdigit[(ch >> 8) & 0xf];
1294 *p++ = hexdigit[(ch >> 4) & 0xf];
1295 *p++ = hexdigit[ch & 15];
1296 }
1297 else
1298#else
1299 /* Map UTF-16 surrogate pairs to '\U00xxxxxx' */
1300 if (ch >= 0xD800 && ch < 0xDC00) {
1301 Py_UNICODE ch2;
1302 Py_UCS4 ucs;
1303
1304 ch2 = *s++;
1305 size--;
1306 if (ch2 >= 0xDC00 && ch2 <= 0xDFFF) {
1307 ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) + 0x00010000;
1308 *p++ = '\\';
1309 *p++ = 'U';
1310 *p++ = hexdigit[(ucs >> 28) & 0xf];
1311 *p++ = hexdigit[(ucs >> 24) & 0xf];
1312 *p++ = hexdigit[(ucs >> 20) & 0xf];
1313 *p++ = hexdigit[(ucs >> 16) & 0xf];
1314 *p++ = hexdigit[(ucs >> 12) & 0xf];
1315 *p++ = hexdigit[(ucs >> 8) & 0xf];
1316 *p++ = hexdigit[(ucs >> 4) & 0xf];
1317 *p++ = hexdigit[ucs & 0xf];
1318 continue;
1319 }
1320 /* Fall through: isolated surrogates are copied as-is */
1321 s--;
1322 size++;
1323 }
1324#endif
1325 /* Map 16-bit characters to '\uxxxx' */
1326 if (ch >= 256 || ch == '\\' || ch == '\n') {
1327 *p++ = '\\';
1328 *p++ = 'u';
1329 *p++ = hexdigit[(ch >> 12) & 0xf];
1330 *p++ = hexdigit[(ch >> 8) & 0xf];
1331 *p++ = hexdigit[(ch >> 4) & 0xf];
1332 *p++ = hexdigit[ch & 15];
1333 }
1334 /* Copy everything else as-is */
1335 else
1336 *p++ = (char) ch;
1337 }
1338 *p = '\0';
1339 _PyString_Resize(&repr, p - q);
1340 return repr;
1341}
Guido van Rossumfb10c3f2000-12-19 02:08:38 +00001342
Guido van Rossum60456fd1997-04-09 17:36:32 +00001343static int
Tim Peterscba30e22003-02-01 06:24:36 +00001344save_unicode(Picklerobject *self, PyObject *args, int doput)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001345{
Armin Rigo7ccbca92006-10-04 12:17:45 +00001346 Py_ssize_t size, len;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001347 PyObject *repr=0;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001348
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001349 if (!PyUnicode_Check(args))
1350 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001351
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001352 if (!self->bin) {
1353 char *repr_str;
1354 static char string = UNICODE;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001355
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001356 repr = modified_EncodeRawUnicodeEscape(
1357 PyUnicode_AS_UNICODE(args), PyUnicode_GET_SIZE(args));
Tim Peterscba30e22003-02-01 06:24:36 +00001358 if (!repr)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001359 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001360
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001361 if ((len = PyString_Size(repr)) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001362 goto err;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001363 repr_str = PyString_AS_STRING((PyStringObject *)repr);
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001364
Tim Peters0bc93f52003-02-02 18:29:33 +00001365 if (self->write_func(self, &string, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001366 goto err;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001367
Tim Peters0bc93f52003-02-02 18:29:33 +00001368 if (self->write_func(self, repr_str, len) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001369 goto err;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001370
Tim Peters0bc93f52003-02-02 18:29:33 +00001371 if (self->write_func(self, "\n", 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001372 goto err;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001373
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001374 Py_XDECREF(repr);
1375 }
1376 else {
1377 int i;
1378 char c_str[5];
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001379
Tim Peterscba30e22003-02-01 06:24:36 +00001380 if (!( repr = PyUnicode_AsUTF8String(args)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001381 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001382
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001383 if ((size = PyString_Size(repr)) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001384 goto err;
Armin Rigo7ccbca92006-10-04 12:17:45 +00001385 if (size > INT_MAX)
1386 return -1; /* string too large */
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001387
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001388 c_str[0] = BINUNICODE;
1389 for (i = 1; i < 5; i++)
1390 c_str[i] = (int)(size >> ((i - 1) * 8));
1391 len = 5;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001392
Tim Peters0bc93f52003-02-02 18:29:33 +00001393 if (self->write_func(self, c_str, len) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001394 goto err;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001395
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001396 if (size > 128 && Pdata_Check(self->file)) {
1397 if (write_other(self, NULL, 0) < 0)
1398 goto err;
1399 PDATA_APPEND(self->file, repr, -1);
1400 }
1401 else {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001402 if (self->write_func(self, PyString_AS_STRING(repr),
Tim Peters0bc93f52003-02-02 18:29:33 +00001403 size) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001404 goto err;
1405 }
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001406
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001407 Py_DECREF(repr);
1408 }
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001409
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001410 if (doput)
1411 if (put(self, args) < 0)
1412 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001413
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001414 return 0;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001415
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001416 err:
1417 Py_XDECREF(repr);
1418 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001419}
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001420#endif
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001421
Tim Peters1d63c9f2003-02-02 20:29:39 +00001422/* A helper for save_tuple. Push the len elements in tuple t on the stack. */
1423static int
Tim Peters67920142003-02-05 03:46:17 +00001424store_tuple_elements(Picklerobject *self, PyObject *t, int len)
Tim Peters1d63c9f2003-02-02 20:29:39 +00001425{
1426 int i;
1427 int res = -1; /* guilty until proved innocent */
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001428
Tim Peters1d63c9f2003-02-02 20:29:39 +00001429 assert(PyTuple_Size(t) == len);
1430
1431 for (i = 0; i < len; i++) {
1432 PyObject *element = PyTuple_GET_ITEM(t, i);
1433
1434 if (element == NULL)
1435 goto finally;
1436 if (save(self, element, 0) < 0)
1437 goto finally;
1438 }
1439 res = 0;
1440
1441 finally:
1442 return res;
1443}
1444
1445/* Tuples are ubiquitous in the pickle protocols, so many techniques are
1446 * used across protocols to minimize the space needed to pickle them.
Tim Peters67920142003-02-05 03:46:17 +00001447 * Tuples are also the only builtin immutable type that can be recursive
Tim Peters1d63c9f2003-02-02 20:29:39 +00001448 * (a tuple can be reached from itself), and that requires some subtle
1449 * magic so that it works in all cases. IOW, this is a long routine.
1450 */
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001451static int
Tim Peterscba30e22003-02-01 06:24:36 +00001452save_tuple(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001453{
Tim Peters1d63c9f2003-02-02 20:29:39 +00001454 PyObject *py_tuple_id = NULL;
1455 int len, i;
1456 int res = -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001457
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001458 static char tuple = TUPLE;
Tim Peters1d63c9f2003-02-02 20:29:39 +00001459 static char pop = POP;
1460 static char pop_mark = POP_MARK;
1461 static char len2opcode[] = {EMPTY_TUPLE, TUPLE1, TUPLE2, TUPLE3};
Guido van Rossum60456fd1997-04-09 17:36:32 +00001462
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001463 if ((len = PyTuple_Size(args)) < 0)
1464 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001465
Tim Peters1d63c9f2003-02-02 20:29:39 +00001466 if (len == 0) {
1467 char c_str[2];
Tim Peters84e87f32001-03-17 04:50:51 +00001468
Tim Peters1d63c9f2003-02-02 20:29:39 +00001469 if (self->proto) {
1470 c_str[0] = EMPTY_TUPLE;
1471 len = 1;
1472 }
1473 else {
1474 c_str[0] = MARK;
1475 c_str[1] = TUPLE;
1476 len = 2;
1477 }
1478 if (self->write_func(self, c_str, len) >= 0)
1479 res = 0;
1480 /* Don't memoize an empty tuple. */
1481 goto finally;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001482 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001483
Tim Peters1d63c9f2003-02-02 20:29:39 +00001484 /* A non-empty tuple. */
1485
1486 /* id(tuple) isn't in the memo now. If it shows up there after
1487 * saving the tuple elements, the tuple must be recursive, in
1488 * which case we'll pop everything we put on the stack, and fetch
1489 * its value from the memo.
1490 */
1491 py_tuple_id = PyLong_FromVoidPtr(args);
1492 if (py_tuple_id == NULL)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001493 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001494
Tim Peters1d63c9f2003-02-02 20:29:39 +00001495 if (len <= 3 && self->proto >= 2) {
1496 /* Use TUPLE{1,2,3} opcodes. */
Tim Peters67920142003-02-05 03:46:17 +00001497 if (store_tuple_elements(self, args, len) < 0)
Tim Peters1d63c9f2003-02-02 20:29:39 +00001498 goto finally;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001499 if (PyDict_GetItem(self->memo, py_tuple_id)) {
Tim Peters1d63c9f2003-02-02 20:29:39 +00001500 /* pop the len elements */
1501 for (i = 0; i < len; ++i)
1502 if (self->write_func(self, &pop, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001503 goto finally;
Tim Peters1d63c9f2003-02-02 20:29:39 +00001504 /* fetch from memo */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001505 if (get(self, py_tuple_id) < 0)
1506 goto finally;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001507 res = 0;
1508 goto finally;
1509 }
Tim Peters1d63c9f2003-02-02 20:29:39 +00001510 /* Not recursive. */
1511 if (self->write_func(self, len2opcode + len, 1) < 0)
1512 goto finally;
1513 goto memoize;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001514 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001515
Tim Peters1d63c9f2003-02-02 20:29:39 +00001516 /* proto < 2 and len > 0, or proto >= 2 and len > 3.
1517 * Generate MARK elt1 elt2 ... TUPLE
1518 */
1519 if (self->write_func(self, &MARKv, 1) < 0)
1520 goto finally;
1521
Tim Peters67920142003-02-05 03:46:17 +00001522 if (store_tuple_elements(self, args, len) < 0)
Tim Peters1d63c9f2003-02-02 20:29:39 +00001523 goto finally;
1524
1525 if (PyDict_GetItem(self->memo, py_tuple_id)) {
1526 /* pop the stack stuff we pushed */
1527 if (self->bin) {
1528 if (self->write_func(self, &pop_mark, 1) < 0)
1529 goto finally;
1530 }
1531 else {
1532 /* Note that we pop one more than len, to remove
1533 * the MARK too.
1534 */
1535 for (i = 0; i <= len; i++)
1536 if (self->write_func(self, &pop, 1) < 0)
1537 goto finally;
1538 }
1539 /* fetch from memo */
1540 if (get(self, py_tuple_id) >= 0)
1541 res = 0;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001542 goto finally;
1543 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001544
Tim Peters1d63c9f2003-02-02 20:29:39 +00001545 /* Not recursive. */
1546 if (self->write_func(self, &tuple, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001547 goto finally;
Tim Peters84e87f32001-03-17 04:50:51 +00001548
Tim Peters1d63c9f2003-02-02 20:29:39 +00001549 memoize:
1550 if (put(self, args) >= 0)
1551 res = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001552
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001553 finally:
1554 Py_XDECREF(py_tuple_id);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001555 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001556}
1557
Tim Peters1092d642003-02-11 21:06:20 +00001558/* iter is an iterator giving items, and we batch up chunks of
1559 * MARK item item ... item APPENDS
1560 * opcode sequences. Calling code should have arranged to first create an
1561 * empty list, or list-like object, for the APPENDS to operate on.
1562 * Returns 0 on success, <0 on error.
1563 */
1564static int
1565batch_list(Picklerobject *self, PyObject *iter)
1566{
Amaury Forgeot d'Arc24cb3822008-09-11 20:56:13 +00001567 PyObject *obj = NULL;
1568 PyObject *firstitem = NULL;
Tim Peters1092d642003-02-11 21:06:20 +00001569 int i, n;
1570
1571 static char append = APPEND;
1572 static char appends = APPENDS;
1573
1574 assert(iter != NULL);
1575
1576 if (self->proto == 0) {
1577 /* APPENDS isn't available; do one at a time. */
1578 for (;;) {
1579 obj = PyIter_Next(iter);
1580 if (obj == NULL) {
1581 if (PyErr_Occurred())
1582 return -1;
1583 break;
1584 }
1585 i = save(self, obj, 0);
1586 Py_DECREF(obj);
1587 if (i < 0)
1588 return -1;
1589 if (self->write_func(self, &append, 1) < 0)
1590 return -1;
Tim Peters1092d642003-02-11 21:06:20 +00001591 }
1592 return 0;
1593 }
1594
1595 /* proto > 0: write in batches of BATCHSIZE. */
1596 do {
Amaury Forgeot d'Arc24cb3822008-09-11 20:56:13 +00001597 /* Get first item */
1598 firstitem = PyIter_Next(iter);
1599 if (firstitem == NULL) {
1600 if (PyErr_Occurred())
1601 goto BatchFailed;
1602
1603 /* nothing more to add */
1604 break;
1605 }
1606
1607 /* Try to get a second item */
1608 obj = PyIter_Next(iter);
1609 if (obj == NULL) {
1610 if (PyErr_Occurred())
1611 goto BatchFailed;
1612
1613 /* Only one item to write */
1614 if (save(self, firstitem, 0) < 0)
1615 goto BatchFailed;
1616 if (self->write_func(self, &append, 1) < 0)
1617 goto BatchFailed;
1618 Py_CLEAR(firstitem);
1619 break;
1620 }
1621
1622 /* More than one item to write */
1623
1624 /* Pump out MARK, items, APPENDS. */
1625 if (self->write_func(self, &MARKv, 1) < 0)
1626 goto BatchFailed;
1627
1628 if (save(self, firstitem, 0) < 0)
1629 goto BatchFailed;
1630 Py_CLEAR(firstitem);
1631 n = 1;
1632
1633 /* Fetch and save up to BATCHSIZE items */
1634 while (obj) {
1635 if (save(self, obj, 0) < 0)
1636 goto BatchFailed;
1637 Py_CLEAR(obj);
1638 n += 1;
1639
1640 if (n == BATCHSIZE)
1641 break;
1642
Tim Peters1092d642003-02-11 21:06:20 +00001643 obj = PyIter_Next(iter);
1644 if (obj == NULL) {
1645 if (PyErr_Occurred())
1646 goto BatchFailed;
1647 break;
1648 }
Tim Peters1092d642003-02-11 21:06:20 +00001649 }
1650
Amaury Forgeot d'Arc24cb3822008-09-11 20:56:13 +00001651 if (self->write_func(self, &appends, 1) < 0)
1652 goto BatchFailed;
Tim Peters1092d642003-02-11 21:06:20 +00001653
Tim Peters90975f12003-02-12 05:28:58 +00001654 } while (n == BATCHSIZE);
Tim Peters1092d642003-02-11 21:06:20 +00001655 return 0;
1656
1657BatchFailed:
Amaury Forgeot d'Arc24cb3822008-09-11 20:56:13 +00001658 Py_XDECREF(firstitem);
1659 Py_XDECREF(obj);
Tim Peters1092d642003-02-11 21:06:20 +00001660 return -1;
1661}
1662
Guido van Rossum60456fd1997-04-09 17:36:32 +00001663static int
Tim Peterscba30e22003-02-01 06:24:36 +00001664save_list(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001665{
Tim Peters1092d642003-02-11 21:06:20 +00001666 int res = -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001667 char s[3];
Tim Peters1092d642003-02-11 21:06:20 +00001668 int len;
1669 PyObject *iter;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001670
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001671 if (self->fast && !fast_save_enter(self, args))
1672 goto finally;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00001673
Tim Peters1092d642003-02-11 21:06:20 +00001674 /* Create an empty list. */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001675 if (self->bin) {
1676 s[0] = EMPTY_LIST;
Tim Peters1092d642003-02-11 21:06:20 +00001677 len = 1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001678 }
1679 else {
1680 s[0] = MARK;
1681 s[1] = LIST;
Tim Peters1092d642003-02-11 21:06:20 +00001682 len = 2;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001683 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001684
Tim Peters1092d642003-02-11 21:06:20 +00001685 if (self->write_func(self, s, len) < 0)
1686 goto finally;
1687
1688 /* Get list length, and bow out early if empty. */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001689 if ((len = PyList_Size(args)) < 0)
1690 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001691
Tim Peters1092d642003-02-11 21:06:20 +00001692 /* Memoize. */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001693 if (len == 0) {
Tim Peters90975f12003-02-12 05:28:58 +00001694 if (put(self, args) >= 0)
1695 res = 0;
1696 goto finally;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001697 }
Tim Peters90975f12003-02-12 05:28:58 +00001698 if (put2(self, args) < 0)
1699 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001700
Tim Peters1092d642003-02-11 21:06:20 +00001701 /* Materialize the list elements. */
1702 iter = PyObject_GetIter(args);
1703 if (iter == NULL)
1704 goto finally;
Facundo Batista763d3092008-06-30 01:10:55 +00001705
1706 if (Py_EnterRecursiveCall(" while pickling an object") == 0)
1707 {
1708 res = batch_list(self, iter);
1709 Py_LeaveRecursiveCall();
1710 }
Tim Peters1092d642003-02-11 21:06:20 +00001711 Py_DECREF(iter);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001712
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001713 finally:
1714 if (self->fast && !fast_save_leave(self, args))
1715 res = -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001716
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001717 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001718}
1719
1720
Tim Peters42f08ac2003-02-11 22:43:24 +00001721/* iter is an iterator giving (key, value) pairs, and we batch up chunks of
1722 * MARK key value ... key value SETITEMS
1723 * opcode sequences. Calling code should have arranged to first create an
1724 * empty dict, or dict-like object, for the SETITEMS to operate on.
1725 * Returns 0 on success, <0 on error.
1726 *
1727 * This is very much like batch_list(). The difference between saving
1728 * elements directly, and picking apart two-tuples, is so long-winded at
1729 * the C level, though, that attempts to combine these routines were too
1730 * ugly to bear.
1731 */
1732static int
1733batch_dict(Picklerobject *self, PyObject *iter)
1734{
Amaury Forgeot d'Arc24cb3822008-09-11 20:56:13 +00001735 PyObject *p = NULL;
1736 PyObject *firstitem = NULL;
Tim Peters42f08ac2003-02-11 22:43:24 +00001737 int i, n;
1738
1739 static char setitem = SETITEM;
1740 static char setitems = SETITEMS;
1741
1742 assert(iter != NULL);
1743
1744 if (self->proto == 0) {
1745 /* SETITEMS isn't available; do one at a time. */
1746 for (;;) {
1747 p = PyIter_Next(iter);
1748 if (p == NULL) {
1749 if (PyErr_Occurred())
1750 return -1;
1751 break;
1752 }
1753 if (!PyTuple_Check(p) || PyTuple_Size(p) != 2) {
1754 PyErr_SetString(PyExc_TypeError, "dict items "
1755 "iterator must return 2-tuples");
1756 return -1;
1757 }
1758 i = save(self, PyTuple_GET_ITEM(p, 0), 0);
1759 if (i >= 0)
1760 i = save(self, PyTuple_GET_ITEM(p, 1), 0);
1761 Py_DECREF(p);
1762 if (i < 0)
1763 return -1;
1764 if (self->write_func(self, &setitem, 1) < 0)
1765 return -1;
Tim Peters42f08ac2003-02-11 22:43:24 +00001766 }
1767 return 0;
1768 }
1769
1770 /* proto > 0: write in batches of BATCHSIZE. */
1771 do {
Amaury Forgeot d'Arc24cb3822008-09-11 20:56:13 +00001772 /* Get first item */
1773 firstitem = PyIter_Next(iter);
1774 if (firstitem == NULL) {
1775 if (PyErr_Occurred())
1776 goto BatchFailed;
1777
1778 /* nothing more to add */
1779 break;
1780 }
1781 if (!PyTuple_Check(firstitem) || PyTuple_Size(firstitem) != 2) {
1782 PyErr_SetString(PyExc_TypeError, "dict items "
1783 "iterator must return 2-tuples");
1784 goto BatchFailed;
1785 }
1786
1787 /* Try to get a second item */
1788 p = PyIter_Next(iter);
1789 if (p == NULL) {
1790 if (PyErr_Occurred())
1791 goto BatchFailed;
1792
1793 /* Only one item to write */
1794 if (save(self, PyTuple_GET_ITEM(firstitem, 0), 0) < 0)
1795 goto BatchFailed;
1796 if (save(self, PyTuple_GET_ITEM(firstitem, 1), 0) < 0)
1797 goto BatchFailed;
1798 if (self->write_func(self, &setitem, 1) < 0)
1799 goto BatchFailed;
1800 Py_CLEAR(firstitem);
1801 break;
1802 }
1803
1804 /* More than one item to write */
1805
1806 /* Pump out MARK, items, SETITEMS. */
1807 if (self->write_func(self, &MARKv, 1) < 0)
1808 goto BatchFailed;
1809
1810 if (save(self, PyTuple_GET_ITEM(firstitem, 0), 0) < 0)
1811 goto BatchFailed;
1812 if (save(self, PyTuple_GET_ITEM(firstitem, 1), 0) < 0)
1813 goto BatchFailed;
1814 Py_CLEAR(firstitem);
1815 n = 1;
1816
1817 /* Fetch and save up to BATCHSIZE items */
1818 while (p) {
1819 if (!PyTuple_Check(p) || PyTuple_Size(p) != 2) {
1820 PyErr_SetString(PyExc_TypeError, "dict items "
1821 "iterator must return 2-tuples");
1822 goto BatchFailed;
1823 }
1824 if (save(self, PyTuple_GET_ITEM(p, 0), 0) < 0)
1825 goto BatchFailed;
1826 if (save(self, PyTuple_GET_ITEM(p, 1), 0) < 0)
1827 goto BatchFailed;
1828 Py_CLEAR(p);
1829 n += 1;
1830
1831 if (n == BATCHSIZE)
1832 break;
1833
Tim Peters42f08ac2003-02-11 22:43:24 +00001834 p = PyIter_Next(iter);
1835 if (p == NULL) {
1836 if (PyErr_Occurred())
1837 goto BatchFailed;
1838 break;
1839 }
Tim Peters42f08ac2003-02-11 22:43:24 +00001840 }
1841
Amaury Forgeot d'Arc24cb3822008-09-11 20:56:13 +00001842 if (self->write_func(self, &setitems, 1) < 0)
1843 goto BatchFailed;
Tim Peters42f08ac2003-02-11 22:43:24 +00001844
Tim Peters90975f12003-02-12 05:28:58 +00001845 } while (n == BATCHSIZE);
Tim Peters42f08ac2003-02-11 22:43:24 +00001846 return 0;
1847
1848BatchFailed:
Amaury Forgeot d'Arc24cb3822008-09-11 20:56:13 +00001849 Py_XDECREF(firstitem);
1850 Py_XDECREF(p);
Tim Peters42f08ac2003-02-11 22:43:24 +00001851 return -1;
1852}
1853
Guido van Rossum60456fd1997-04-09 17:36:32 +00001854static int
Tim Peterscba30e22003-02-01 06:24:36 +00001855save_dict(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001856{
Tim Peters42f08ac2003-02-11 22:43:24 +00001857 int res = -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001858 char s[3];
Tim Peters42f08ac2003-02-11 22:43:24 +00001859 int len;
1860 PyObject *iter;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001861
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001862 if (self->fast && !fast_save_enter(self, args))
1863 goto finally;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00001864
Tim Peters42f08ac2003-02-11 22:43:24 +00001865 /* Create an empty dict. */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001866 if (self->bin) {
1867 s[0] = EMPTY_DICT;
1868 len = 1;
1869 }
1870 else {
1871 s[0] = MARK;
1872 s[1] = DICT;
1873 len = 2;
1874 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001875
Tim Peters0bc93f52003-02-02 18:29:33 +00001876 if (self->write_func(self, s, len) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001877 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001878
Tim Peters42f08ac2003-02-11 22:43:24 +00001879 /* Get dict size, and bow out early if empty. */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001880 if ((len = PyDict_Size(args)) < 0)
1881 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001882
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001883 if (len == 0) {
Tim Peters90975f12003-02-12 05:28:58 +00001884 if (put(self, args) >= 0)
1885 res = 0;
1886 goto finally;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001887 }
Tim Peters90975f12003-02-12 05:28:58 +00001888 if (put2(self, args) < 0)
1889 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001890
Tim Peters42f08ac2003-02-11 22:43:24 +00001891 /* Materialize the dict items. */
1892 iter = PyObject_CallMethod(args, "iteritems", "()");
1893 if (iter == NULL)
1894 goto finally;
Facundo Batista763d3092008-06-30 01:10:55 +00001895 if (Py_EnterRecursiveCall(" while pickling an object") == 0)
1896 {
1897 res = batch_dict(self, iter);
1898 Py_LeaveRecursiveCall();
1899 }
Tim Peters42f08ac2003-02-11 22:43:24 +00001900 Py_DECREF(iter);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001901
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001902 finally:
1903 if (self->fast && !fast_save_leave(self, args))
1904 res = -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001905
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001906 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001907}
1908
1909
Tim Peters84e87f32001-03-17 04:50:51 +00001910static int
Tim Peterscba30e22003-02-01 06:24:36 +00001911save_inst(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001912{
1913 PyObject *class = 0, *module = 0, *name = 0, *state = 0,
1914 *getinitargs_func = 0, *getstate_func = 0, *class_args = 0;
1915 char *module_str, *name_str;
1916 int module_size, name_size, res = -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001917
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001918 static char inst = INST, obj = OBJ, build = BUILD;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001919
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001920 if (self->fast && !fast_save_enter(self, args))
1921 goto finally;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00001922
Tim Peters0bc93f52003-02-02 18:29:33 +00001923 if (self->write_func(self, &MARKv, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001924 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001925
Tim Peterscba30e22003-02-01 06:24:36 +00001926 if (!( class = PyObject_GetAttr(args, __class___str)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001927 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001928
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001929 if (self->bin) {
1930 if (save(self, class, 0) < 0)
1931 goto finally;
1932 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001933
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001934 if ((getinitargs_func = PyObject_GetAttr(args, __getinitargs___str))) {
1935 PyObject *element = 0;
1936 int i, len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001937
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001938 if (!( class_args =
Tim Peterscba30e22003-02-01 06:24:36 +00001939 PyObject_Call(getinitargs_func, empty_tuple, NULL)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001940 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001941
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001942 if ((len = PyObject_Size(class_args)) < 0)
1943 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001944
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001945 for (i = 0; i < len; i++) {
Tim Peterscba30e22003-02-01 06:24:36 +00001946 if (!( element = PySequence_GetItem(class_args, i)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001947 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001948
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001949 if (save(self, element, 0) < 0) {
1950 Py_DECREF(element);
1951 goto finally;
1952 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001953
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001954 Py_DECREF(element);
1955 }
1956 }
1957 else {
Jeremy Hyltonf00368f2003-06-16 20:19:49 +00001958 if (PyErr_ExceptionMatches(PyExc_AttributeError))
1959 PyErr_Clear();
1960 else
1961 goto finally;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001962 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001963
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001964 if (!self->bin) {
1965 if (!( name = ((PyClassObject *)class)->cl_name )) {
1966 PyErr_SetString(PicklingError, "class has no name");
1967 goto finally;
1968 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001969
Tim Peterscba30e22003-02-01 06:24:36 +00001970 if (!( module = whichmodule(class, name)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001971 goto finally;
Guido van Rossum053b8df1998-11-25 16:18:00 +00001972
Tim Peters84e87f32001-03-17 04:50:51 +00001973
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001974 if ((module_size = PyString_Size(module)) < 0 ||
1975 (name_size = PyString_Size(name)) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001976 goto finally;
Guido van Rossum053b8df1998-11-25 16:18:00 +00001977
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001978 module_str = PyString_AS_STRING((PyStringObject *)module);
1979 name_str = PyString_AS_STRING((PyStringObject *)name);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001980
Tim Peters0bc93f52003-02-02 18:29:33 +00001981 if (self->write_func(self, &inst, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001982 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001983
Tim Peters0bc93f52003-02-02 18:29:33 +00001984 if (self->write_func(self, module_str, module_size) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001985 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001986
Tim Peters0bc93f52003-02-02 18:29:33 +00001987 if (self->write_func(self, "\n", 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001988 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001989
Tim Peters0bc93f52003-02-02 18:29:33 +00001990 if (self->write_func(self, name_str, name_size) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001991 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001992
Tim Peters0bc93f52003-02-02 18:29:33 +00001993 if (self->write_func(self, "\n", 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001994 goto finally;
1995 }
Tim Peters0bc93f52003-02-02 18:29:33 +00001996 else if (self->write_func(self, &obj, 1) < 0) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001997 goto finally;
1998 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001999
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002000 if ((getstate_func = PyObject_GetAttr(args, __getstate___str))) {
2001 state = PyObject_Call(getstate_func, empty_tuple, NULL);
Tim Peterscba30e22003-02-01 06:24:36 +00002002 if (!state)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002003 goto finally;
2004 }
2005 else {
Jeremy Hyltonf00368f2003-06-16 20:19:49 +00002006 if (PyErr_ExceptionMatches(PyExc_AttributeError))
2007 PyErr_Clear();
2008 else
2009 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002010
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002011 if (!( state = PyObject_GetAttr(args, __dict___str))) {
Jeremy Hyltonf00368f2003-06-16 20:19:49 +00002012 if (PyErr_ExceptionMatches(PyExc_AttributeError))
2013 PyErr_Clear();
2014 else
2015 goto finally;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002016 res = 0;
2017 goto finally;
2018 }
2019 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002020
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002021 if (!PyDict_Check(state)) {
2022 if (put2(self, args) < 0)
2023 goto finally;
2024 }
2025 else {
2026 if (put(self, args) < 0)
2027 goto finally;
2028 }
Tim Peters84e87f32001-03-17 04:50:51 +00002029
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002030 if (save(self, state, 0) < 0)
2031 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002032
Tim Peters0bc93f52003-02-02 18:29:33 +00002033 if (self->write_func(self, &build, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002034 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002035
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002036 res = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002037
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002038 finally:
2039 if (self->fast && !fast_save_leave(self, args))
2040 res = -1;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00002041
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002042 Py_XDECREF(module);
2043 Py_XDECREF(class);
2044 Py_XDECREF(state);
2045 Py_XDECREF(getinitargs_func);
2046 Py_XDECREF(getstate_func);
2047 Py_XDECREF(class_args);
Guido van Rossum60456fd1997-04-09 17:36:32 +00002048
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002049 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002050}
2051
2052
Guido van Rossum60456fd1997-04-09 17:36:32 +00002053static int
Tim Peterscba30e22003-02-01 06:24:36 +00002054save_global(Picklerobject *self, PyObject *args, PyObject *name)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002055{
Jeremy Hylton9ee91f12002-07-11 22:02:33 +00002056 PyObject *global_name = 0, *module = 0, *mod = 0, *klass = 0;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002057 char *name_str, *module_str;
2058 int module_size, name_size, res = -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002059
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002060 static char global = GLOBAL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002061
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002062 if (name) {
2063 global_name = name;
2064 Py_INCREF(global_name);
2065 }
2066 else {
Tim Peterscba30e22003-02-01 06:24:36 +00002067 if (!( global_name = PyObject_GetAttr(args, __name___str)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002068 goto finally;
2069 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002070
Tim Peterscba30e22003-02-01 06:24:36 +00002071 if (!( module = whichmodule(args, global_name)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002072 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002073
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002074 if ((module_size = PyString_Size(module)) < 0 ||
2075 (name_size = PyString_Size(global_name)) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002076 goto finally;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002077
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002078 module_str = PyString_AS_STRING((PyStringObject *)module);
2079 name_str = PyString_AS_STRING((PyStringObject *)global_name);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002080
Guido van Rossum75bfd052002-12-24 18:10:07 +00002081 /* XXX This can be doing a relative import. Clearly it shouldn't,
2082 but I don't know how to stop it. :-( */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002083 mod = PyImport_ImportModule(module_str);
2084 if (mod == NULL) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002085 cPickle_ErrFormat(PicklingError,
Tim Petersb9ce7cd2003-02-18 20:50:45 +00002086 "Can't pickle %s: import of module %s "
2087 "failed",
2088 "OS", args, module);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002089 goto finally;
2090 }
Jeremy Hylton0e1f7a82002-07-11 22:01:40 +00002091 klass = PyObject_GetAttrString(mod, name_str);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002092 if (klass == NULL) {
2093 cPickle_ErrFormat(PicklingError,
Tim Petersb9ce7cd2003-02-18 20:50:45 +00002094 "Can't pickle %s: attribute lookup %s.%s "
2095 "failed",
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002096 "OSS", args, module, global_name);
2097 goto finally;
2098 }
2099 if (klass != args) {
Jeremy Hylton0e1f7a82002-07-11 22:01:40 +00002100 Py_DECREF(klass);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002101 cPickle_ErrFormat(PicklingError,
Tim Peters731098b2003-02-04 20:56:09 +00002102 "Can't pickle %s: it's not the same object "
2103 "as %s.%s",
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002104 "OSS", args, module, global_name);
2105 goto finally;
2106 }
Jeremy Hylton0e1f7a82002-07-11 22:01:40 +00002107 Py_DECREF(klass);
Guido van Rossuma92d16a2001-08-18 21:22:07 +00002108
Tim Peters731098b2003-02-04 20:56:09 +00002109 if (self->proto >= 2) {
2110 /* See whether this is in the extension registry, and if
2111 * so generate an EXT opcode.
2112 */
2113 PyObject *py_code; /* extension code as Python object */
Tim Peters3e667d52003-02-04 21:47:44 +00002114 long code; /* extension code as C value */
Tim Peters731098b2003-02-04 20:56:09 +00002115 char c_str[5];
2116 int n;
2117
2118 PyTuple_SET_ITEM(two_tuple, 0, module);
2119 PyTuple_SET_ITEM(two_tuple, 1, global_name);
2120 py_code = PyDict_GetItem(extension_registry, two_tuple);
2121 if (py_code == NULL)
2122 goto gen_global; /* not registered */
2123
2124 /* Verify py_code has the right type and value. */
2125 if (!PyInt_Check(py_code)) {
2126 cPickle_ErrFormat(PicklingError, "Can't pickle %s: "
Tim Peters6288e232003-02-05 03:53:10 +00002127 "extension code %s isn't an integer",
Tim Peters731098b2003-02-04 20:56:09 +00002128 "OO", args, py_code);
2129 goto finally;
2130 }
2131 code = PyInt_AS_LONG(py_code);
2132 if (code <= 0 || code > 0x7fffffffL) {
2133 cPickle_ErrFormat(PicklingError, "Can't pickle %s: "
2134 "extension code %ld is out of range",
2135 "Ol", args, code);
2136 goto finally;
2137 }
2138
2139 /* Generate an EXT opcode. */
2140 if (code <= 0xff) {
2141 c_str[0] = EXT1;
2142 c_str[1] = (char)code;
2143 n = 2;
2144 }
2145 else if (code <= 0xffff) {
2146 c_str[0] = EXT2;
2147 c_str[1] = (char)(code & 0xff);
2148 c_str[2] = (char)((code >> 8) & 0xff);
2149 n = 3;
2150 }
2151 else {
2152 c_str[0] = EXT4;
2153 c_str[1] = (char)(code & 0xff);
2154 c_str[2] = (char)((code >> 8) & 0xff);
2155 c_str[3] = (char)((code >> 16) & 0xff);
2156 c_str[4] = (char)((code >> 24) & 0xff);
2157 n = 5;
2158 }
2159
2160 if (self->write_func(self, c_str, n) >= 0)
2161 res = 0;
2162 goto finally; /* and don't memoize */
2163 }
2164
2165 gen_global:
Tim Peters0bc93f52003-02-02 18:29:33 +00002166 if (self->write_func(self, &global, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002167 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002168
Tim Peters0bc93f52003-02-02 18:29:33 +00002169 if (self->write_func(self, module_str, module_size) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002170 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002171
Tim Peters0bc93f52003-02-02 18:29:33 +00002172 if (self->write_func(self, "\n", 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002173 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002174
Tim Peters0bc93f52003-02-02 18:29:33 +00002175 if (self->write_func(self, name_str, name_size) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002176 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002177
Tim Peters0bc93f52003-02-02 18:29:33 +00002178 if (self->write_func(self, "\n", 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002179 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002180
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002181 if (put(self, args) < 0)
2182 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002183
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002184 res = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002185
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002186 finally:
2187 Py_XDECREF(module);
2188 Py_XDECREF(global_name);
2189 Py_XDECREF(mod);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002190
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002191 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002192}
2193
Guido van Rossum60456fd1997-04-09 17:36:32 +00002194static int
Tim Peterscba30e22003-02-01 06:24:36 +00002195save_pers(Picklerobject *self, PyObject *args, PyObject *f)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002196{
2197 PyObject *pid = 0;
2198 int size, res = -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002199
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002200 static char persid = PERSID, binpersid = BINPERSID;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002201
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002202 Py_INCREF(args);
2203 ARG_TUP(self, args);
2204 if (self->arg) {
2205 pid = PyObject_Call(f, self->arg, NULL);
2206 FREE_ARG_TUP(self);
2207 }
2208 if (! pid) return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002209
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002210 if (pid != Py_None) {
2211 if (!self->bin) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002212 if (!PyString_Check(pid)) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002213 PyErr_SetString(PicklingError,
2214 "persistent id must be string");
2215 goto finally;
2216 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002217
Tim Peters0bc93f52003-02-02 18:29:33 +00002218 if (self->write_func(self, &persid, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002219 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002220
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002221 if ((size = PyString_Size(pid)) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002222 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002223
Tim Peters0bc93f52003-02-02 18:29:33 +00002224 if (self->write_func(self,
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002225 PyString_AS_STRING(
2226 (PyStringObject *)pid),
Tim Peters0bc93f52003-02-02 18:29:33 +00002227 size) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002228 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002229
Tim Peters0bc93f52003-02-02 18:29:33 +00002230 if (self->write_func(self, "\n", 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002231 goto finally;
Tim Peters84e87f32001-03-17 04:50:51 +00002232
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002233 res = 1;
2234 goto finally;
2235 }
2236 else if (save(self, pid, 1) >= 0) {
Tim Peters0bc93f52003-02-02 18:29:33 +00002237 if (self->write_func(self, &binpersid, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002238 res = -1;
2239 else
2240 res = 1;
2241 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002242
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002243 goto finally;
2244 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002245
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002246 res = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002247
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002248 finally:
2249 Py_XDECREF(pid);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002250
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002251 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002252}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002253
Tim Peters71fcda52003-02-14 23:05:28 +00002254/* We're saving ob, and args is the 2-thru-5 tuple returned by the
2255 * appropriate __reduce__ method for ob.
2256 */
Tim Peters84e87f32001-03-17 04:50:51 +00002257static int
Amaury Forgeot d'Arcc353ea72008-10-30 21:29:12 +00002258save_reduce(Picklerobject *self, PyObject *args, PyObject *fn, PyObject *ob)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002259{
Tim Peters71fcda52003-02-14 23:05:28 +00002260 PyObject *callable;
2261 PyObject *argtup;
Amaury Forgeot d'Arcc353ea72008-10-30 21:29:12 +00002262 PyObject *state = NULL;
2263 PyObject *listitems = Py_None;
2264 PyObject *dictitems = Py_None;
2265 Py_ssize_t size;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002266
Tim Peters71fcda52003-02-14 23:05:28 +00002267 int use_newobj = self->proto >= 2;
2268
2269 static char reduce = REDUCE;
2270 static char build = BUILD;
2271 static char newobj = NEWOBJ;
2272
Amaury Forgeot d'Arcc353ea72008-10-30 21:29:12 +00002273 size = PyTuple_Size(args);
2274 if (size < 2 || size > 5) {
2275 cPickle_ErrFormat(PicklingError, "tuple returned by "
2276 "%s must contain 2 through 5 elements",
2277 "O", fn);
2278 return -1;
2279 }
2280
Tim Peters71fcda52003-02-14 23:05:28 +00002281 if (! PyArg_UnpackTuple(args, "save_reduce", 2, 5,
2282 &callable,
2283 &argtup,
2284 &state,
2285 &listitems,
2286 &dictitems))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002287 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002288
Raymond Hettingera6b45cc2004-12-07 07:05:57 +00002289 if (!PyTuple_Check(argtup)) {
Amaury Forgeot d'Arcc353ea72008-10-30 21:29:12 +00002290 cPickle_ErrFormat(PicklingError, "Second element of "
2291 "tuple returned by %s must be a tuple",
2292 "O", fn);
Raymond Hettingera6b45cc2004-12-07 07:05:57 +00002293 return -1;
2294 }
2295
Tim Peters71fcda52003-02-14 23:05:28 +00002296 if (state == Py_None)
2297 state = NULL;
Amaury Forgeot d'Arcc353ea72008-10-30 21:29:12 +00002298
Tim Peters71fcda52003-02-14 23:05:28 +00002299 if (listitems == Py_None)
2300 listitems = NULL;
Amaury Forgeot d'Arcc353ea72008-10-30 21:29:12 +00002301 else if (!PyIter_Check(listitems)) {
2302 cPickle_ErrFormat(PicklingError, "Fourth element of "
2303 "tuple returned by %s must be an iterator, not %s",
2304 "Os", fn, Py_TYPE(listitems)->tp_name);
2305 return -1;
2306 }
2307
Tim Peters71fcda52003-02-14 23:05:28 +00002308 if (dictitems == Py_None)
2309 dictitems = NULL;
Amaury Forgeot d'Arcc353ea72008-10-30 21:29:12 +00002310 else if (!PyIter_Check(dictitems)) {
2311 cPickle_ErrFormat(PicklingError, "Fifth element of "
2312 "tuple returned by %s must be an iterator, not %s",
2313 "Os", fn, Py_TYPE(dictitems)->tp_name);
2314 return -1;
2315 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002316
Tim Peters71fcda52003-02-14 23:05:28 +00002317 /* Protocol 2 special case: if callable's name is __newobj__, use
2318 * NEWOBJ. This consumes a lot of code.
2319 */
2320 if (use_newobj) {
2321 PyObject *temp = PyObject_GetAttr(callable, __name___str);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002322
Tim Peters71fcda52003-02-14 23:05:28 +00002323 if (temp == NULL) {
Jeremy Hyltonf00368f2003-06-16 20:19:49 +00002324 if (PyErr_ExceptionMatches(PyExc_AttributeError))
2325 PyErr_Clear();
2326 else
2327 return -1;
Tim Peters71fcda52003-02-14 23:05:28 +00002328 use_newobj = 0;
2329 }
2330 else {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002331 use_newobj = PyString_Check(temp) &&
2332 strcmp(PyString_AS_STRING(temp),
Tim Peters71fcda52003-02-14 23:05:28 +00002333 "__newobj__") == 0;
2334 Py_DECREF(temp);
2335 }
2336 }
2337 if (use_newobj) {
2338 PyObject *cls;
2339 PyObject *newargtup;
2340 int n, i;
2341
2342 /* Sanity checks. */
2343 n = PyTuple_Size(argtup);
2344 if (n < 1) {
2345 PyErr_SetString(PicklingError, "__newobj__ arglist "
2346 "is empty");
2347 return -1;
2348 }
2349
2350 cls = PyTuple_GET_ITEM(argtup, 0);
2351 if (! PyObject_HasAttrString(cls, "__new__")) {
2352 PyErr_SetString(PicklingError, "args[0] from "
2353 "__newobj__ args has no __new__");
2354 return -1;
2355 }
2356
2357 /* XXX How could ob be NULL? */
2358 if (ob != NULL) {
2359 PyObject *ob_dot_class;
2360
2361 ob_dot_class = PyObject_GetAttr(ob, __class___str);
Jeremy Hyltonf00368f2003-06-16 20:19:49 +00002362 if (ob_dot_class == NULL) {
2363 if (PyErr_ExceptionMatches(
2364 PyExc_AttributeError))
2365 PyErr_Clear();
2366 else
2367 return -1;
2368 }
Tim Peters71fcda52003-02-14 23:05:28 +00002369 i = ob_dot_class != cls; /* true iff a problem */
2370 Py_XDECREF(ob_dot_class);
2371 if (i) {
2372 PyErr_SetString(PicklingError, "args[0] from "
2373 "__newobj__ args has the wrong class");
2374 return -1;
2375 }
2376 }
2377
2378 /* Save the class and its __new__ arguments. */
2379 if (save(self, cls, 0) < 0)
2380 return -1;
2381
2382 newargtup = PyTuple_New(n-1); /* argtup[1:] */
2383 if (newargtup == NULL)
2384 return -1;
2385 for (i = 1; i < n; ++i) {
2386 PyObject *temp = PyTuple_GET_ITEM(argtup, i);
2387 Py_INCREF(temp);
2388 PyTuple_SET_ITEM(newargtup, i-1, temp);
2389 }
Neal Norwitz5a29dd32007-10-05 05:01:38 +00002390 i = save(self, newargtup, 0);
Tim Peters71fcda52003-02-14 23:05:28 +00002391 Py_DECREF(newargtup);
2392 if (i < 0)
2393 return -1;
2394
2395 /* Add NEWOBJ opcode. */
2396 if (self->write_func(self, &newobj, 1) < 0)
2397 return -1;
2398 }
2399 else {
2400 /* Not using NEWOBJ. */
2401 if (save(self, callable, 0) < 0 ||
2402 save(self, argtup, 0) < 0 ||
2403 self->write_func(self, &reduce, 1) < 0)
2404 return -1;
2405 }
2406
2407 /* Memoize. */
2408 /* XXX How can ob be NULL? */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002409 if (ob != NULL) {
2410 if (state && !PyDict_Check(state)) {
2411 if (put2(self, ob) < 0)
2412 return -1;
2413 }
Tim Peters71fcda52003-02-14 23:05:28 +00002414 else if (put(self, ob) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002415 return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002416 }
Tim Peters84e87f32001-03-17 04:50:51 +00002417
Guido van Rossum60456fd1997-04-09 17:36:32 +00002418
Tim Peters71fcda52003-02-14 23:05:28 +00002419 if (listitems && batch_list(self, listitems) < 0)
2420 return -1;
2421
2422 if (dictitems && batch_dict(self, dictitems) < 0)
2423 return -1;
2424
2425 if (state) {
2426 if (save(self, state, 0) < 0 ||
2427 self->write_func(self, &build, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002428 return -1;
2429 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002430
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002431 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002432}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002433
Guido van Rossum60456fd1997-04-09 17:36:32 +00002434static int
Tim Peters1d63c9f2003-02-02 20:29:39 +00002435save(Picklerobject *self, PyObject *args, int pers_save)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002436{
2437 PyTypeObject *type;
Tim Peters71fcda52003-02-14 23:05:28 +00002438 PyObject *py_ob_id = 0, *__reduce__ = 0, *t = 0;
Tim Peters71fcda52003-02-14 23:05:28 +00002439 int res = -1;
Amaury Forgeot d'Arcc353ea72008-10-30 21:29:12 +00002440 int tmp;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002441
Facundo Batista763d3092008-06-30 01:10:55 +00002442 if (Py_EnterRecursiveCall(" while pickling an object"))
2443 return -1;
Martin v. Löwis5a395302002-08-04 08:20:23 +00002444
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002445 if (!pers_save && self->pers_func) {
2446 if ((tmp = save_pers(self, args, self->pers_func)) != 0) {
2447 res = tmp;
2448 goto finally;
2449 }
2450 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002451
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002452 if (args == Py_None) {
2453 res = save_none(self, args);
2454 goto finally;
2455 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002456
Christian Heimese93237d2007-12-19 02:37:44 +00002457 type = Py_TYPE(args);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002458
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002459 switch (type->tp_name[0]) {
Guido van Rossum77f6a652002-04-03 22:41:51 +00002460 case 'b':
2461 if (args == Py_False || args == Py_True) {
2462 res = save_bool(self, args);
2463 goto finally;
2464 }
2465 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002466 case 'i':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002467 if (type == &PyInt_Type) {
2468 res = save_int(self, args);
2469 goto finally;
2470 }
2471 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002472
Guido van Rossum60456fd1997-04-09 17:36:32 +00002473 case 'l':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002474 if (type == &PyLong_Type) {
2475 res = save_long(self, args);
2476 goto finally;
2477 }
2478 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002479
Guido van Rossum60456fd1997-04-09 17:36:32 +00002480 case 'f':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002481 if (type == &PyFloat_Type) {
2482 res = save_float(self, args);
2483 goto finally;
2484 }
2485 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002486
Guido van Rossum60456fd1997-04-09 17:36:32 +00002487 case 't':
Tim Peters1d63c9f2003-02-02 20:29:39 +00002488 if (type == &PyTuple_Type && PyTuple_Size(args) == 0) {
2489 res = save_tuple(self, args);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002490 goto finally;
2491 }
2492 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002493
Guido van Rossum60456fd1997-04-09 17:36:32 +00002494 case 's':
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002495 if ((type == &PyString_Type) && (PyString_GET_SIZE(args) < 2)) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002496 res = save_string(self, args, 0);
2497 goto finally;
2498 }
Facundo Batista14618862008-06-22 15:27:10 +00002499 break;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00002500
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002501#ifdef Py_USING_UNICODE
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00002502 case 'u':
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002503 if ((type == &PyUnicode_Type) && (PyString_GET_SIZE(args) < 2)) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002504 res = save_unicode(self, args, 0);
2505 goto finally;
2506 }
Facundo Batista14618862008-06-22 15:27:10 +00002507 break;
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002508#endif
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002509 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002510
Christian Heimese93237d2007-12-19 02:37:44 +00002511 if (Py_REFCNT(args) > 1) {
Tim Peterscba30e22003-02-01 06:24:36 +00002512 if (!( py_ob_id = PyLong_FromVoidPtr(args)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002513 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002514
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002515 if (PyDict_GetItem(self->memo, py_ob_id)) {
2516 if (get(self, py_ob_id) < 0)
2517 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002518
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002519 res = 0;
2520 goto finally;
2521 }
2522 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002523
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002524 switch (type->tp_name[0]) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00002525 case 's':
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002526 if (type == &PyString_Type) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002527 res = save_string(self, args, 1);
2528 goto finally;
2529 }
2530 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002531
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002532#ifdef Py_USING_UNICODE
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00002533 case 'u':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002534 if (type == &PyUnicode_Type) {
2535 res = save_unicode(self, args, 1);
2536 goto finally;
2537 }
2538 break;
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002539#endif
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00002540
Guido van Rossum60456fd1997-04-09 17:36:32 +00002541 case 't':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002542 if (type == &PyTuple_Type) {
2543 res = save_tuple(self, args);
2544 goto finally;
2545 }
2546 if (type == &PyType_Type) {
2547 res = save_global(self, args, NULL);
2548 goto finally;
2549 }
2550 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002551
Guido van Rossum60456fd1997-04-09 17:36:32 +00002552 case 'l':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002553 if (type == &PyList_Type) {
2554 res = save_list(self, args);
2555 goto finally;
2556 }
2557 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002558
2559 case 'd':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002560 if (type == &PyDict_Type) {
2561 res = save_dict(self, args);
2562 goto finally;
2563 }
2564 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002565
2566 case 'i':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002567 if (type == &PyInstance_Type) {
2568 res = save_inst(self, args);
2569 goto finally;
2570 }
2571 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002572
2573 case 'c':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002574 if (type == &PyClass_Type) {
2575 res = save_global(self, args, NULL);
2576 goto finally;
2577 }
2578 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002579
2580 case 'f':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002581 if (type == &PyFunction_Type) {
2582 res = save_global(self, args, NULL);
Christian Tismer2460c622004-02-26 16:21:45 +00002583 if (res && PyErr_ExceptionMatches(PickleError)) {
2584 /* fall back to reduce */
2585 PyErr_Clear();
2586 break;
2587 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002588 goto finally;
2589 }
2590 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002591
2592 case 'b':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002593 if (type == &PyCFunction_Type) {
2594 res = save_global(self, args, NULL);
2595 goto finally;
2596 }
2597 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002598
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002599 if (!pers_save && self->inst_pers_func) {
2600 if ((tmp = save_pers(self, args, self->inst_pers_func)) != 0) {
2601 res = tmp;
2602 goto finally;
2603 }
2604 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002605
Jeremy Hylton39c61162002-07-16 19:47:43 +00002606 if (PyType_IsSubtype(type, &PyType_Type)) {
2607 res = save_global(self, args, NULL);
2608 goto finally;
2609 }
2610
Guido van Rossumb289b872003-02-19 01:45:13 +00002611 /* Get a reduction callable, and call it. This may come from
Georg Brandldffbf5f2008-05-20 07:49:57 +00002612 * copy_reg.dispatch_table, the object's __reduce_ex__ method,
Guido van Rossumb289b872003-02-19 01:45:13 +00002613 * or the object's __reduce__ method.
Tim Peters71fcda52003-02-14 23:05:28 +00002614 */
Tim Peters5aa3da62003-02-13 21:03:57 +00002615 __reduce__ = PyDict_GetItem(dispatch_table, (PyObject *)type);
2616 if (__reduce__ != NULL) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002617 Py_INCREF(__reduce__);
Guido van Rossumb289b872003-02-19 01:45:13 +00002618 Py_INCREF(args);
2619 ARG_TUP(self, args);
2620 if (self->arg) {
2621 t = PyObject_Call(__reduce__, self->arg, NULL);
2622 FREE_ARG_TUP(self);
2623 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002624 }
2625 else {
Guido van Rossumb289b872003-02-19 01:45:13 +00002626 /* Check for a __reduce_ex__ method. */
2627 __reduce__ = PyObject_GetAttr(args, __reduce_ex___str);
2628 if (__reduce__ != NULL) {
2629 t = PyInt_FromLong(self->proto);
2630 if (t != NULL) {
2631 ARG_TUP(self, t);
2632 t = NULL;
2633 if (self->arg) {
2634 t = PyObject_Call(__reduce__,
2635 self->arg, NULL);
2636 FREE_ARG_TUP(self);
2637 }
2638 }
2639 }
2640 else {
Jeremy Hyltonf00368f2003-06-16 20:19:49 +00002641 if (PyErr_ExceptionMatches(PyExc_AttributeError))
2642 PyErr_Clear();
2643 else
2644 goto finally;
Guido van Rossumb289b872003-02-19 01:45:13 +00002645 /* Check for a __reduce__ method. */
2646 __reduce__ = PyObject_GetAttr(args, __reduce___str);
2647 if (__reduce__ != NULL) {
2648 t = PyObject_Call(__reduce__,
2649 empty_tuple, NULL);
2650 }
2651 else {
2652 PyErr_SetObject(UnpickleableError, args);
2653 goto finally;
2654 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002655 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002656 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002657
Tim Peters71fcda52003-02-14 23:05:28 +00002658 if (t == NULL)
2659 goto finally;
Tim Peters84e87f32001-03-17 04:50:51 +00002660
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002661 if (PyString_Check(t)) {
Tim Peters71fcda52003-02-14 23:05:28 +00002662 res = save_global(self, args, t);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002663 goto finally;
2664 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002665
Amaury Forgeot d'Arcc353ea72008-10-30 21:29:12 +00002666 if (!PyTuple_Check(t)) {
Tim Peters71fcda52003-02-14 23:05:28 +00002667 cPickle_ErrFormat(PicklingError, "Value returned by "
2668 "%s must be string or tuple",
2669 "O", __reduce__);
2670 goto finally;
2671 }
2672
Amaury Forgeot d'Arcc353ea72008-10-30 21:29:12 +00002673 res = save_reduce(self, t, __reduce__, args);
Guido van Rossum60456fd1997-04-09 17:36:32 +00002674
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002675 finally:
Facundo Batista763d3092008-06-30 01:10:55 +00002676 Py_LeaveRecursiveCall();
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002677 Py_XDECREF(py_ob_id);
2678 Py_XDECREF(__reduce__);
2679 Py_XDECREF(t);
Tim Peters84e87f32001-03-17 04:50:51 +00002680
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002681 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002682}
2683
2684
2685static int
Tim Peterscba30e22003-02-01 06:24:36 +00002686dump(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002687{
2688 static char stop = STOP;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002689
Tim Peters4190fb82003-02-02 16:09:05 +00002690 if (self->proto >= 2) {
2691 char bytes[2];
2692
2693 bytes[0] = PROTO;
Tim Peters87482ea2003-02-02 16:16:30 +00002694 assert(self->proto >= 0 && self->proto < 256);
2695 bytes[1] = (char)self->proto;
Tim Peters4190fb82003-02-02 16:09:05 +00002696 if (self->write_func(self, bytes, 2) < 0)
2697 return -1;
2698 }
2699
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002700 if (save(self, args, 0) < 0)
2701 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002702
Tim Peters4190fb82003-02-02 16:09:05 +00002703 if (self->write_func(self, &stop, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002704 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002705
Tim Peters4190fb82003-02-02 16:09:05 +00002706 if (self->write_func(self, NULL, 0) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002707 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002708
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002709 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002710}
2711
2712static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00002713Pickle_clear_memo(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002714{
Tim Peterscba30e22003-02-01 06:24:36 +00002715 if (self->memo)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002716 PyDict_Clear(self->memo);
2717 Py_INCREF(Py_None);
2718 return Py_None;
2719}
2720
2721static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00002722Pickle_getvalue(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002723{
2724 int l, i, rsize, ssize, clear=1, lm;
2725 long ik;
2726 PyObject *k, *r;
2727 char *s, *p, *have_get;
2728 Pdata *data;
2729
2730 /* Can be called by Python code or C code */
Tim Peterscba30e22003-02-01 06:24:36 +00002731 if (args && !PyArg_ParseTuple(args, "|i:getvalue", &clear))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002732 return NULL;
2733
2734 /* Check to make sure we are based on a list */
2735 if (! Pdata_Check(self->file)) {
2736 PyErr_SetString(PicklingError,
2737 "Attempt to getvalue() a non-list-based pickler");
2738 return NULL;
2739 }
2740
2741 /* flush write buffer */
2742 if (write_other(self, NULL, 0) < 0) return NULL;
2743
2744 data=(Pdata*)self->file;
2745 l=data->length;
2746
2747 /* set up an array to hold get/put status */
Tim Petersac5687a2003-02-02 18:08:34 +00002748 lm = PyDict_Size(self->memo);
2749 if (lm < 0) return NULL;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002750 lm++;
Tim Petersac5687a2003-02-02 18:08:34 +00002751 have_get = malloc(lm);
2752 if (have_get == NULL) return PyErr_NoMemory();
2753 memset(have_get, 0, lm);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002754
2755 /* Scan for gets. */
Tim Petersac5687a2003-02-02 18:08:34 +00002756 for (rsize = 0, i = l; --i >= 0; ) {
2757 k = data->data[i];
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002758
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002759 if (PyString_Check(k))
2760 rsize += PyString_GET_SIZE(k);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002761
2762 else if (PyInt_Check(k)) { /* put */
Tim Petersac5687a2003-02-02 18:08:34 +00002763 ik = PyInt_AS_LONG((PyIntObject*)k);
2764 if (ik >= lm || ik == 0) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002765 PyErr_SetString(PicklingError,
2766 "Invalid get data");
Neal Norwitz98a96002006-07-23 07:57:11 +00002767 goto err;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002768 }
Tim Petersac5687a2003-02-02 18:08:34 +00002769 if (have_get[ik]) /* with matching get */
2770 rsize += ik < 256 ? 2 : 5;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002771 }
2772
2773 else if (! (PyTuple_Check(k) &&
2774 PyTuple_GET_SIZE(k) == 2 &&
Tim Petersac5687a2003-02-02 18:08:34 +00002775 PyInt_Check((k = PyTuple_GET_ITEM(k, 0))))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002776 ) {
2777 PyErr_SetString(PicklingError,
2778 "Unexpected data in internal list");
Neal Norwitz98a96002006-07-23 07:57:11 +00002779 goto err;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002780 }
2781
2782 else { /* put */
Tim Petersac5687a2003-02-02 18:08:34 +00002783 ik = PyInt_AS_LONG((PyIntObject *)k);
2784 if (ik >= lm || ik == 0) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002785 PyErr_SetString(PicklingError,
2786 "Invalid get data");
2787 return NULL;
2788 }
Tim Petersac5687a2003-02-02 18:08:34 +00002789 have_get[ik] = 1;
2790 rsize += ik < 256 ? 2 : 5;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002791 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002792 }
2793
2794 /* Now generate the result */
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002795 r = PyString_FromStringAndSize(NULL, rsize);
Tim Petersac5687a2003-02-02 18:08:34 +00002796 if (r == NULL) goto err;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002797 s = PyString_AS_STRING((PyStringObject *)r);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002798
Tim Petersac5687a2003-02-02 18:08:34 +00002799 for (i = 0; i < l; i++) {
2800 k = data->data[i];
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002801
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002802 if (PyString_Check(k)) {
2803 ssize = PyString_GET_SIZE(k);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002804 if (ssize) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002805 p=PyString_AS_STRING((PyStringObject *)k);
Tim Petersac5687a2003-02-02 18:08:34 +00002806 while (--ssize >= 0)
2807 *s++ = *p++;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002808 }
2809 }
2810
2811 else if (PyTuple_Check(k)) { /* get */
Tim Petersac5687a2003-02-02 18:08:34 +00002812 ik = PyInt_AS_LONG((PyIntObject *)
2813 PyTuple_GET_ITEM(k, 0));
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002814 if (ik < 256) {
2815 *s++ = BINGET;
2816 *s++ = (int)(ik & 0xff);
2817 }
2818 else {
2819 *s++ = LONG_BINGET;
2820 *s++ = (int)(ik & 0xff);
2821 *s++ = (int)((ik >> 8) & 0xff);
2822 *s++ = (int)((ik >> 16) & 0xff);
2823 *s++ = (int)((ik >> 24) & 0xff);
2824 }
2825 }
2826
2827 else { /* put */
Tim Petersac5687a2003-02-02 18:08:34 +00002828 ik = PyInt_AS_LONG((PyIntObject*)k);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002829
2830 if (have_get[ik]) { /* with matching get */
2831 if (ik < 256) {
2832 *s++ = BINPUT;
2833 *s++ = (int)(ik & 0xff);
2834 }
2835 else {
2836 *s++ = LONG_BINPUT;
2837 *s++ = (int)(ik & 0xff);
2838 *s++ = (int)((ik >> 8) & 0xff);
2839 *s++ = (int)((ik >> 16) & 0xff);
2840 *s++ = (int)((ik >> 24) & 0xff);
2841 }
2842 }
2843 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002844 }
2845
2846 if (clear) {
2847 PyDict_Clear(self->memo);
Tim Petersac5687a2003-02-02 18:08:34 +00002848 Pdata_clear(data, 0);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002849 }
2850
2851 free(have_get);
2852 return r;
2853 err:
2854 free(have_get);
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00002855 return NULL;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002856}
2857
2858static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00002859Pickler_dump(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002860{
2861 PyObject *ob;
2862 int get=0;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002863
Tim Peterscba30e22003-02-01 06:24:36 +00002864 if (!( PyArg_ParseTuple(args, "O|i:dump", &ob, &get)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002865 return NULL;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002866
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002867 if (dump(self, ob) < 0)
2868 return NULL;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002869
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002870 if (get) return Pickle_getvalue(self, NULL);
Guido van Rossum053b8df1998-11-25 16:18:00 +00002871
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002872 /* XXX Why does dump() return self? */
2873 Py_INCREF(self);
2874 return (PyObject*)self;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002875}
2876
2877
Tim Peterscba30e22003-02-01 06:24:36 +00002878static struct PyMethodDef Pickler_methods[] =
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002879{
Neal Norwitzb0493252002-03-31 14:44:22 +00002880 {"dump", (PyCFunction)Pickler_dump, METH_VARARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00002881 PyDoc_STR("dump(object) -- "
2882 "Write an object in pickle format to the object's pickle stream")},
Fred Drake0ebacc82002-05-01 20:36:39 +00002883 {"clear_memo", (PyCFunction)Pickle_clear_memo, METH_NOARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00002884 PyDoc_STR("clear_memo() -- Clear the picklers memo")},
Neal Norwitzb0493252002-03-31 14:44:22 +00002885 {"getvalue", (PyCFunction)Pickle_getvalue, METH_VARARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00002886 PyDoc_STR("getvalue() -- Finish picking a list-based pickle")},
Guido van Rossum60456fd1997-04-09 17:36:32 +00002887 {NULL, NULL} /* sentinel */
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002888};
2889
2890
2891static Picklerobject *
Tim Peters5bd2a792003-02-01 16:45:06 +00002892newPicklerobject(PyObject *file, int proto)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002893{
2894 Picklerobject *self;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002895
Tim Peters5bd2a792003-02-01 16:45:06 +00002896 if (proto < 0)
Tim Peters8587b3c2003-02-13 15:44:41 +00002897 proto = HIGHEST_PROTOCOL;
2898 if (proto > HIGHEST_PROTOCOL) {
Tim Peters5bd2a792003-02-01 16:45:06 +00002899 PyErr_Format(PyExc_ValueError, "pickle protocol %d asked for; "
2900 "the highest available protocol is %d",
Tim Peters8587b3c2003-02-13 15:44:41 +00002901 proto, HIGHEST_PROTOCOL);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002902 return NULL;
Tim Peters5bd2a792003-02-01 16:45:06 +00002903 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002904
Jeremy Hylton4cf63192003-04-09 21:05:12 +00002905 self = PyObject_GC_New(Picklerobject, &Picklertype);
Tim Peters5bd2a792003-02-01 16:45:06 +00002906 if (self == NULL)
2907 return NULL;
2908 self->proto = proto;
2909 self->bin = proto > 0;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002910 self->fp = NULL;
2911 self->write = NULL;
2912 self->memo = NULL;
2913 self->arg = NULL;
2914 self->pers_func = NULL;
2915 self->inst_pers_func = NULL;
2916 self->write_buf = NULL;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002917 self->fast = 0;
2918 self->fast_container = 0;
2919 self->fast_memo = NULL;
2920 self->buf_size = 0;
2921 self->dispatch_table = NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002922
Tim Peters5bd2a792003-02-01 16:45:06 +00002923 self->file = NULL;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002924 if (file)
2925 Py_INCREF(file);
Tim Peters5bd2a792003-02-01 16:45:06 +00002926 else {
2927 file = Pdata_New();
2928 if (file == NULL)
2929 goto err;
2930 }
2931 self->file = file;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002932
Tim Peterscba30e22003-02-01 06:24:36 +00002933 if (!( self->memo = PyDict_New()))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002934 goto err;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002935
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002936 if (PyFile_Check(file)) {
2937 self->fp = PyFile_AsFile(file);
2938 if (self->fp == NULL) {
Tim Peters5bd2a792003-02-01 16:45:06 +00002939 PyErr_SetString(PyExc_ValueError,
2940 "I/O operation on closed file");
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002941 goto err;
2942 }
2943 self->write_func = write_file;
Guido van Rossumc91fcaa1999-03-29 20:00:14 +00002944 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002945 else if (PycStringIO_OutputCheck(file)) {
2946 self->write_func = write_cStringIO;
2947 }
2948 else if (file == Py_None) {
2949 self->write_func = write_none;
2950 }
2951 else {
2952 self->write_func = write_other;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002953
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002954 if (! Pdata_Check(file)) {
2955 self->write = PyObject_GetAttr(file, write_str);
2956 if (!self->write) {
2957 PyErr_Clear();
Tim Peterscba30e22003-02-01 06:24:36 +00002958 PyErr_SetString(PyExc_TypeError,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002959 "argument must have 'write' "
2960 "attribute");
2961 goto err;
2962 }
2963 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002964
Tim Peters5bd2a792003-02-01 16:45:06 +00002965 self->write_buf = (char *)PyMem_Malloc(WRITE_BUF_SIZE);
2966 if (self->write_buf == NULL) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002967 PyErr_NoMemory();
2968 goto err;
2969 }
2970 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002971
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002972 if (PyEval_GetRestricted()) {
2973 /* Restricted execution, get private tables */
Alexandre Vassalotti9510e4a2008-05-11 08:25:28 +00002974 PyObject *m = PyImport_Import(copyreg_str);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002975
Tim Peters5b7da392003-02-04 00:21:07 +00002976 if (m == NULL)
2977 goto err;
2978 self->dispatch_table = PyObject_GetAttr(m, dispatch_table_str);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002979 Py_DECREF(m);
Tim Peters5b7da392003-02-04 00:21:07 +00002980 if (self->dispatch_table == NULL)
2981 goto err;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002982 }
2983 else {
Tim Peters5b7da392003-02-04 00:21:07 +00002984 self->dispatch_table = dispatch_table;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002985 Py_INCREF(dispatch_table);
2986 }
Jeremy Hylton4cf63192003-04-09 21:05:12 +00002987 PyObject_GC_Track(self);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002988
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002989 return self;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002990
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002991 err:
Tim Peters5bd2a792003-02-01 16:45:06 +00002992 Py_DECREF(self);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002993 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002994}
2995
2996
2997static PyObject *
Martin v. Löwis544f1192004-07-27 05:22:33 +00002998get_Pickler(PyObject *self, PyObject *args, PyObject *kwds)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002999{
Martin v. Löwis15e62742006-02-27 16:46:16 +00003000 static char *kwlist[] = {"file", "protocol", NULL};
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003001 PyObject *file = NULL;
Tim Peters5bd2a792003-02-01 16:45:06 +00003002 int proto = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003003
Tim Peters92c8bb32003-02-13 23:00:26 +00003004 /* XXX
Martin v. Löwis544f1192004-07-27 05:22:33 +00003005 * The documented signature is Pickler(file, protocol=0), but this
Tim Peters92c8bb32003-02-13 23:00:26 +00003006 * accepts Pickler() and Pickler(integer) too. The meaning then
3007 * is clear as mud, undocumented, and not supported by pickle.py.
3008 * I'm told Zope uses this, but I haven't traced into this code
3009 * far enough to figure out what it means.
Tim Peters5bd2a792003-02-01 16:45:06 +00003010 */
3011 if (!PyArg_ParseTuple(args, "|i:Pickler", &proto)) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003012 PyErr_Clear();
Tim Peters5bd2a792003-02-01 16:45:06 +00003013 proto = 0;
Martin v. Löwis544f1192004-07-27 05:22:33 +00003014 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|i:Pickler",
3015 kwlist, &file, &proto))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003016 return NULL;
3017 }
Tim Peters5bd2a792003-02-01 16:45:06 +00003018 return (PyObject *)newPicklerobject(file, proto);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003019}
3020
3021
3022static void
Tim Peterscba30e22003-02-01 06:24:36 +00003023Pickler_dealloc(Picklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003024{
Jeremy Hylton4cf63192003-04-09 21:05:12 +00003025 PyObject_GC_UnTrack(self);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003026 Py_XDECREF(self->write);
3027 Py_XDECREF(self->memo);
3028 Py_XDECREF(self->fast_memo);
3029 Py_XDECREF(self->arg);
3030 Py_XDECREF(self->file);
3031 Py_XDECREF(self->pers_func);
3032 Py_XDECREF(self->inst_pers_func);
3033 Py_XDECREF(self->dispatch_table);
Tim Peters5bd2a792003-02-01 16:45:06 +00003034 PyMem_Free(self->write_buf);
Christian Heimese93237d2007-12-19 02:37:44 +00003035 Py_TYPE(self)->tp_free((PyObject *)self);
Jeremy Hylton4cf63192003-04-09 21:05:12 +00003036}
3037
3038static int
3039Pickler_traverse(Picklerobject *self, visitproc visit, void *arg)
3040{
Thomas Woutersc6e55062006-04-15 21:47:09 +00003041 Py_VISIT(self->write);
3042 Py_VISIT(self->memo);
3043 Py_VISIT(self->fast_memo);
3044 Py_VISIT(self->arg);
3045 Py_VISIT(self->file);
3046 Py_VISIT(self->pers_func);
3047 Py_VISIT(self->inst_pers_func);
3048 Py_VISIT(self->dispatch_table);
Jeremy Hylton4cf63192003-04-09 21:05:12 +00003049 return 0;
3050}
3051
3052static int
3053Pickler_clear(Picklerobject *self)
3054{
Thomas Woutersedf17d82006-04-15 17:28:34 +00003055 Py_CLEAR(self->write);
3056 Py_CLEAR(self->memo);
3057 Py_CLEAR(self->fast_memo);
3058 Py_CLEAR(self->arg);
3059 Py_CLEAR(self->file);
3060 Py_CLEAR(self->pers_func);
3061 Py_CLEAR(self->inst_pers_func);
3062 Py_CLEAR(self->dispatch_table);
Jeremy Hylton4cf63192003-04-09 21:05:12 +00003063 return 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003064}
3065
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003066static PyObject *
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003067Pickler_get_pers_func(Picklerobject *p)
3068{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003069 if (p->pers_func == NULL)
3070 PyErr_SetString(PyExc_AttributeError, "persistent_id");
3071 else
3072 Py_INCREF(p->pers_func);
3073 return p->pers_func;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003074}
3075
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003076static int
3077Pickler_set_pers_func(Picklerobject *p, PyObject *v)
3078{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003079 if (v == NULL) {
3080 PyErr_SetString(PyExc_TypeError,
3081 "attribute deletion is not supported");
3082 return -1;
3083 }
3084 Py_XDECREF(p->pers_func);
3085 Py_INCREF(v);
3086 p->pers_func = v;
3087 return 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003088}
3089
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003090static int
3091Pickler_set_inst_pers_func(Picklerobject *p, PyObject *v)
3092{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003093 if (v == NULL) {
3094 PyErr_SetString(PyExc_TypeError,
3095 "attribute deletion is not supported");
3096 return -1;
3097 }
3098 Py_XDECREF(p->inst_pers_func);
3099 Py_INCREF(v);
3100 p->inst_pers_func = v;
3101 return 0;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003102}
3103
3104static PyObject *
3105Pickler_get_memo(Picklerobject *p)
3106{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003107 if (p->memo == NULL)
3108 PyErr_SetString(PyExc_AttributeError, "memo");
3109 else
3110 Py_INCREF(p->memo);
3111 return p->memo;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003112}
3113
3114static int
3115Pickler_set_memo(Picklerobject *p, PyObject *v)
3116{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003117 if (v == NULL) {
3118 PyErr_SetString(PyExc_TypeError,
3119 "attribute deletion is not supported");
3120 return -1;
3121 }
3122 if (!PyDict_Check(v)) {
3123 PyErr_SetString(PyExc_TypeError, "memo must be a dictionary");
3124 return -1;
3125 }
3126 Py_XDECREF(p->memo);
3127 Py_INCREF(v);
3128 p->memo = v;
3129 return 0;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003130}
3131
3132static PyObject *
3133Pickler_get_error(Picklerobject *p)
3134{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003135 /* why is this an attribute on the Pickler? */
3136 Py_INCREF(PicklingError);
3137 return PicklingError;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003138}
3139
3140static PyMemberDef Pickler_members[] = {
3141 {"binary", T_INT, offsetof(Picklerobject, bin)},
3142 {"fast", T_INT, offsetof(Picklerobject, fast)},
Jeremy Hylton3eb46f32001-10-16 17:10:49 +00003143 {NULL}
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003144};
3145
3146static PyGetSetDef Pickler_getsets[] = {
Tim Peterscba30e22003-02-01 06:24:36 +00003147 {"persistent_id", (getter)Pickler_get_pers_func,
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003148 (setter)Pickler_set_pers_func},
3149 {"inst_persistent_id", NULL, (setter)Pickler_set_inst_pers_func},
3150 {"memo", (getter)Pickler_get_memo, (setter)Pickler_set_memo},
Jeremy Hylton3eb46f32001-10-16 17:10:49 +00003151 {"PicklingError", (getter)Pickler_get_error, NULL},
3152 {NULL}
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003153};
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003154
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003155PyDoc_STRVAR(Picklertype__doc__,
3156"Objects that know how to pickle objects\n");
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003157
Guido van Rossum9716aaa1997-12-08 15:15:16 +00003158static PyTypeObject Picklertype = {
Martin v. Löwis68192102007-07-21 06:55:02 +00003159 PyVarObject_HEAD_INIT(NULL, 0)
Guido van Rossum14648392001-12-08 18:02:58 +00003160 "cPickle.Pickler", /*tp_name*/
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003161 sizeof(Picklerobject), /*tp_basicsize*/
3162 0,
3163 (destructor)Pickler_dealloc, /* tp_dealloc */
3164 0, /* tp_print */
3165 0, /* tp_getattr */
3166 0, /* tp_setattr */
3167 0, /* tp_compare */
3168 0, /* tp_repr */
3169 0, /* tp_as_number */
3170 0, /* tp_as_sequence */
3171 0, /* tp_as_mapping */
3172 0, /* tp_hash */
3173 0, /* tp_call */
3174 0, /* tp_str */
Jason Tishlerfb8595d2003-01-06 12:41:26 +00003175 PyObject_GenericGetAttr, /* tp_getattro */
3176 PyObject_GenericSetAttr, /* tp_setattro */
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003177 0, /* tp_as_buffer */
Jeremy Hylton4cf63192003-04-09 21:05:12 +00003178 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003179 Picklertype__doc__, /* tp_doc */
Jeremy Hylton4cf63192003-04-09 21:05:12 +00003180 (traverseproc)Pickler_traverse, /* tp_traverse */
3181 (inquiry)Pickler_clear, /* tp_clear */
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003182 0, /* tp_richcompare */
3183 0, /* tp_weaklistoffset */
3184 0, /* tp_iter */
3185 0, /* tp_iternext */
3186 Pickler_methods, /* tp_methods */
3187 Pickler_members, /* tp_members */
3188 Pickler_getsets, /* tp_getset */
Guido van Rossum9716aaa1997-12-08 15:15:16 +00003189};
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003190
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003191static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00003192find_class(PyObject *py_module_name, PyObject *py_global_name, PyObject *fc)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003193{
3194 PyObject *global = 0, *module;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003195
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003196 if (fc) {
3197 if (fc==Py_None) {
Tim Peters92c8bb32003-02-13 23:00:26 +00003198 PyErr_SetString(UnpicklingError, "Global and instance "
3199 "pickles are not supported.");
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003200 return NULL;
3201 }
Georg Brandl684fd0c2006-05-25 19:15:31 +00003202 return PyObject_CallFunctionObjArgs(fc, py_module_name,
3203 py_global_name, NULL);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003204 }
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00003205
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003206 module = PySys_GetObject("modules");
3207 if (module == NULL)
3208 return NULL;
Jeremy Hyltond1055231998-08-11 19:52:51 +00003209
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003210 module = PyDict_GetItem(module, py_module_name);
3211 if (module == NULL) {
3212 module = PyImport_Import(py_module_name);
3213 if (!module)
3214 return NULL;
3215 global = PyObject_GetAttr(module, py_global_name);
3216 Py_DECREF(module);
3217 }
3218 else
3219 global = PyObject_GetAttr(module, py_global_name);
3220 return global;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003221}
3222
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003223static int
Tim Peterscba30e22003-02-01 06:24:36 +00003224marker(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003225{
3226 if (self->num_marks < 1) {
3227 PyErr_SetString(UnpicklingError, "could not find MARK");
3228 return -1;
3229 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003230
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003231 return self->marks[--self->num_marks];
Guido van Rossum60456fd1997-04-09 17:36:32 +00003232}
3233
Tim Peters84e87f32001-03-17 04:50:51 +00003234
Guido van Rossum60456fd1997-04-09 17:36:32 +00003235static int
Tim Peterscba30e22003-02-01 06:24:36 +00003236load_none(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003237{
3238 PDATA_APPEND(self->stack, Py_None, -1);
3239 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003240}
3241
Guido van Rossumf9ffb031999-02-04 14:54:04 +00003242static int
Tim Peterscba30e22003-02-01 06:24:36 +00003243bad_readline(void)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003244{
3245 PyErr_SetString(UnpicklingError, "pickle data was truncated");
3246 return -1;
Guido van Rossumf9ffb031999-02-04 14:54:04 +00003247}
Guido van Rossum60456fd1997-04-09 17:36:32 +00003248
3249static int
Tim Peterscba30e22003-02-01 06:24:36 +00003250load_int(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003251{
3252 PyObject *py_int = 0;
3253 char *endptr, *s;
3254 int len, res = -1;
3255 long l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003256
Tim Peters0bc93f52003-02-02 18:29:33 +00003257 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003258 if (len < 2) return bad_readline();
3259 if (!( s=pystrndup(s,len))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003260
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003261 errno = 0;
3262 l = strtol(s, &endptr, 0);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003263
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003264 if (errno || (*endptr != '\n') || (endptr[1] != '\0')) {
3265 /* Hm, maybe we've got something long. Let's try reading
3266 it as a Python long object. */
3267 errno = 0;
3268 py_int = PyLong_FromString(s, NULL, 0);
3269 if (py_int == NULL) {
3270 PyErr_SetString(PyExc_ValueError,
3271 "could not convert string to int");
3272 goto finally;
3273 }
3274 }
3275 else {
Guido van Rossume2763392002-04-05 19:30:08 +00003276 if (len == 3 && (l == 0 || l == 1)) {
3277 if (!( py_int = PyBool_FromLong(l))) goto finally;
3278 }
3279 else {
3280 if (!( py_int = PyInt_FromLong(l))) goto finally;
3281 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003282 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003283
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003284 free(s);
3285 PDATA_PUSH(self->stack, py_int, -1);
3286 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003287
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003288 finally:
3289 free(s);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003290
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003291 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003292}
3293
Tim Peters3c67d792003-02-02 17:59:11 +00003294static int
3295load_bool(Unpicklerobject *self, PyObject *boolean)
3296{
3297 assert(boolean == Py_True || boolean == Py_False);
Tim Peterse0a39072003-02-03 15:45:56 +00003298 PDATA_APPEND(self->stack, boolean, -1);
Tim Peters3c67d792003-02-02 17:59:11 +00003299 return 0;
3300}
3301
Tim Petersee1a53c2003-02-02 02:57:53 +00003302/* s contains x bytes of a little-endian integer. Return its value as a
3303 * C int. Obscure: when x is 1 or 2, this is an unsigned little-endian
3304 * int, but when x is 4 it's a signed one. This is an historical source
3305 * of x-platform bugs.
3306 */
Tim Peters84e87f32001-03-17 04:50:51 +00003307static long
Tim Petersee1a53c2003-02-02 02:57:53 +00003308calc_binint(char *s, int x)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003309{
3310 unsigned char c;
3311 int i;
3312 long l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003313
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003314 for (i = 0, l = 0L; i < x; i++) {
3315 c = (unsigned char)s[i];
3316 l |= (long)c << (i * 8);
3317 }
Tim Petersbfa18f72001-04-10 01:54:42 +00003318#if SIZEOF_LONG > 4
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003319 /* Unlike BININT1 and BININT2, BININT (more accurately BININT4)
3320 * is signed, so on a box with longs bigger than 4 bytes we need
3321 * to extend a BININT's sign bit to the full width.
3322 */
3323 if (x == 4 && l & (1L << 31))
3324 l |= (~0L) << 32;
Tim Petersbfa18f72001-04-10 01:54:42 +00003325#endif
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003326 return l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003327}
3328
3329
3330static int
Tim Peterscba30e22003-02-01 06:24:36 +00003331load_binintx(Unpicklerobject *self, char *s, int x)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003332{
3333 PyObject *py_int = 0;
3334 long l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003335
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003336 l = calc_binint(s, x);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003337
Tim Peterscba30e22003-02-01 06:24:36 +00003338 if (!( py_int = PyInt_FromLong(l)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003339 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003340
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003341 PDATA_PUSH(self->stack, py_int, -1);
3342 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003343}
3344
3345
3346static int
Tim Peterscba30e22003-02-01 06:24:36 +00003347load_binint(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003348{
3349 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003350
Tim Peters0bc93f52003-02-02 18:29:33 +00003351 if (self->read_func(self, &s, 4) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003352 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003353
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003354 return load_binintx(self, s, 4);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003355}
3356
3357
3358static int
Tim Peterscba30e22003-02-01 06:24:36 +00003359load_binint1(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003360{
3361 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003362
Tim Peters0bc93f52003-02-02 18:29:33 +00003363 if (self->read_func(self, &s, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003364 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003365
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003366 return load_binintx(self, s, 1);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003367}
3368
3369
3370static int
Tim Peterscba30e22003-02-01 06:24:36 +00003371load_binint2(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003372{
3373 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003374
Tim Peters0bc93f52003-02-02 18:29:33 +00003375 if (self->read_func(self, &s, 2) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003376 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003377
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003378 return load_binintx(self, s, 2);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003379}
Tim Peters84e87f32001-03-17 04:50:51 +00003380
Guido van Rossum60456fd1997-04-09 17:36:32 +00003381static int
Tim Peterscba30e22003-02-01 06:24:36 +00003382load_long(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003383{
3384 PyObject *l = 0;
3385 char *end, *s;
3386 int len, res = -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003387
Tim Peters0bc93f52003-02-02 18:29:33 +00003388 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003389 if (len < 2) return bad_readline();
3390 if (!( s=pystrndup(s,len))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003391
Tim Peterscba30e22003-02-01 06:24:36 +00003392 if (!( l = PyLong_FromString(s, &end, 0)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003393 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003394
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003395 free(s);
3396 PDATA_PUSH(self->stack, l, -1);
3397 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003398
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003399 finally:
3400 free(s);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003401
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003402 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003403}
3404
Tim Petersee1a53c2003-02-02 02:57:53 +00003405/* 'size' bytes contain the # of bytes of little-endian 256's-complement
3406 * data following.
3407 */
3408static int
3409load_counted_long(Unpicklerobject *self, int size)
3410{
Martin v. Löwis18e16552006-02-15 17:27:45 +00003411 Py_ssize_t i;
Tim Petersee1a53c2003-02-02 02:57:53 +00003412 char *nbytes;
3413 unsigned char *pdata;
3414 PyObject *along;
3415
3416 assert(size == 1 || size == 4);
3417 i = self->read_func(self, &nbytes, size);
3418 if (i < 0) return -1;
3419
3420 size = calc_binint(nbytes, size);
3421 if (size < 0) {
3422 /* Corrupt or hostile pickle -- we never write one like
3423 * this.
3424 */
Tim Peters0c7c48e2003-02-03 22:07:24 +00003425 PyErr_SetString(UnpicklingError, "LONG pickle has negative "
Tim Petersee1a53c2003-02-02 02:57:53 +00003426 "byte count");
3427 return -1;
3428 }
3429
3430 if (size == 0)
3431 along = PyLong_FromLong(0L);
3432 else {
3433 /* Read the raw little-endian bytes & convert. */
Neal Norwitz529baf22003-02-02 17:08:33 +00003434 i = self->read_func(self, (char **)&pdata, size);
Tim Petersee1a53c2003-02-02 02:57:53 +00003435 if (i < 0) return -1;
3436 along = _PyLong_FromByteArray(pdata, (size_t)size,
3437 1 /* little endian */, 1 /* signed */);
3438 }
3439 if (along == NULL)
3440 return -1;
3441 PDATA_PUSH(self->stack, along, -1);
3442 return 0;
3443}
Tim Peters84e87f32001-03-17 04:50:51 +00003444
Guido van Rossum60456fd1997-04-09 17:36:32 +00003445static int
Tim Peterscba30e22003-02-01 06:24:36 +00003446load_float(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003447{
3448 PyObject *py_float = 0;
3449 char *endptr, *s;
3450 int len, res = -1;
3451 double d;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003452
Tim Peters0bc93f52003-02-02 18:29:33 +00003453 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003454 if (len < 2) return bad_readline();
3455 if (!( s=pystrndup(s,len))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003456
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003457 errno = 0;
Martin v. Löwis737ea822004-06-08 18:52:54 +00003458 d = PyOS_ascii_strtod(s, &endptr);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003459
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003460 if (errno || (endptr[0] != '\n') || (endptr[1] != '\0')) {
3461 PyErr_SetString(PyExc_ValueError,
3462 "could not convert string to float");
3463 goto finally;
3464 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003465
Tim Peterscba30e22003-02-01 06:24:36 +00003466 if (!( py_float = PyFloat_FromDouble(d)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003467 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003468
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003469 free(s);
3470 PDATA_PUSH(self->stack, py_float, -1);
3471 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003472
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003473 finally:
3474 free(s);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003475
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003476 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003477}
3478
Guido van Rossum60456fd1997-04-09 17:36:32 +00003479static int
Tim Peterscba30e22003-02-01 06:24:36 +00003480load_binfloat(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003481{
Tim Peters9905b942003-03-20 20:53:32 +00003482 PyObject *py_float;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003483 double x;
3484 char *p;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003485
Tim Peters0bc93f52003-02-02 18:29:33 +00003486 if (self->read_func(self, &p, 8) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003487 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003488
Tim Peters9905b942003-03-20 20:53:32 +00003489 x = _PyFloat_Unpack8((unsigned char *)p, 0);
3490 if (x == -1.0 && PyErr_Occurred())
3491 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003492
Tim Peters9905b942003-03-20 20:53:32 +00003493 py_float = PyFloat_FromDouble(x);
3494 if (py_float == NULL)
3495 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003496
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003497 PDATA_PUSH(self->stack, py_float, -1);
3498 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003499}
Guido van Rossum60456fd1997-04-09 17:36:32 +00003500
3501static int
Tim Peterscba30e22003-02-01 06:24:36 +00003502load_string(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003503{
3504 PyObject *str = 0;
Martin v. Löwis8a8da792002-08-14 07:46:28 +00003505 int len, res = -1;
3506 char *s, *p;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003507
Tim Peters0bc93f52003-02-02 18:29:33 +00003508 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003509 if (len < 2) return bad_readline();
3510 if (!( s=pystrndup(s,len))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003511
Martin v. Löwis8a8da792002-08-14 07:46:28 +00003512
3513 /* Strip outermost quotes */
3514 while (s[len-1] <= ' ')
3515 len--;
3516 if(s[0]=='"' && s[len-1]=='"'){
3517 s[len-1] = '\0';
3518 p = s + 1 ;
3519 len -= 2;
3520 } else if(s[0]=='\'' && s[len-1]=='\''){
3521 s[len-1] = '\0';
3522 p = s + 1 ;
3523 len -= 2;
3524 } else
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003525 goto insecure;
3526 /********************************************/
Guido van Rossum9716aaa1997-12-08 15:15:16 +00003527
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003528 str = PyString_DecodeEscape(p, len, NULL, 0, NULL);
Neal Norwitz99dfe3c2006-08-02 04:27:11 +00003529 free(s);
Martin v. Löwis8a8da792002-08-14 07:46:28 +00003530 if (str) {
3531 PDATA_PUSH(self->stack, str, -1);
3532 res = 0;
3533 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003534 return res;
Tim Peters84e87f32001-03-17 04:50:51 +00003535
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003536 insecure:
3537 free(s);
3538 PyErr_SetString(PyExc_ValueError,"insecure string pickle");
3539 return -1;
Tim Peters84e87f32001-03-17 04:50:51 +00003540}
Guido van Rossum60456fd1997-04-09 17:36:32 +00003541
3542
3543static int
Tim Peterscba30e22003-02-01 06:24:36 +00003544load_binstring(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003545{
3546 PyObject *py_string = 0;
3547 long l;
3548 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003549
Tim Peters0bc93f52003-02-02 18:29:33 +00003550 if (self->read_func(self, &s, 4) < 0) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003551
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003552 l = calc_binint(s, 4);
Gregory P. Smith9d534572008-06-11 07:41:16 +00003553 if (l < 0) {
3554 /* Corrupt or hostile pickle -- we never write one like
3555 * this.
3556 */
3557 PyErr_SetString(UnpicklingError,
3558 "BINSTRING pickle has negative byte count");
3559 return -1;
3560 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003561
Tim Peters0bc93f52003-02-02 18:29:33 +00003562 if (self->read_func(self, &s, l) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003563 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003564
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003565 if (!( py_string = PyString_FromStringAndSize(s, l)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003566 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003567
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003568 PDATA_PUSH(self->stack, py_string, -1);
3569 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003570}
3571
3572
3573static int
Tim Peterscba30e22003-02-01 06:24:36 +00003574load_short_binstring(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003575{
3576 PyObject *py_string = 0;
3577 unsigned char l;
3578 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003579
Tim Peters0bc93f52003-02-02 18:29:33 +00003580 if (self->read_func(self, &s, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003581 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003582
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003583 l = (unsigned char)s[0];
Guido van Rossum60456fd1997-04-09 17:36:32 +00003584
Tim Peters0bc93f52003-02-02 18:29:33 +00003585 if (self->read_func(self, &s, l) < 0) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003586
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003587 if (!( py_string = PyString_FromStringAndSize(s, l))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003588
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003589 PDATA_PUSH(self->stack, py_string, -1);
3590 return 0;
Tim Peters84e87f32001-03-17 04:50:51 +00003591}
Guido van Rossum60456fd1997-04-09 17:36:32 +00003592
3593
Martin v. Löwis339d0f72001-08-17 18:39:25 +00003594#ifdef Py_USING_UNICODE
Guido van Rossum60456fd1997-04-09 17:36:32 +00003595static int
Tim Peterscba30e22003-02-01 06:24:36 +00003596load_unicode(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003597{
3598 PyObject *str = 0;
3599 int len, res = -1;
3600 char *s;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003601
Tim Peters0bc93f52003-02-02 18:29:33 +00003602 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003603 if (len < 1) return bad_readline();
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003604
Tim Peterscba30e22003-02-01 06:24:36 +00003605 if (!( str = PyUnicode_DecodeRawUnicodeEscape(s, len - 1, NULL)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003606 goto finally;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003607
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003608 PDATA_PUSH(self->stack, str, -1);
3609 return 0;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003610
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003611 finally:
3612 return res;
Tim Peters84e87f32001-03-17 04:50:51 +00003613}
Martin v. Löwis339d0f72001-08-17 18:39:25 +00003614#endif
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003615
3616
Martin v. Löwis339d0f72001-08-17 18:39:25 +00003617#ifdef Py_USING_UNICODE
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003618static int
Tim Peterscba30e22003-02-01 06:24:36 +00003619load_binunicode(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003620{
3621 PyObject *unicode;
3622 long l;
3623 char *s;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003624
Tim Peters0bc93f52003-02-02 18:29:33 +00003625 if (self->read_func(self, &s, 4) < 0) return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003626
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003627 l = calc_binint(s, 4);
Gregory P. Smith9d534572008-06-11 07:41:16 +00003628 if (l < 0) {
3629 /* Corrupt or hostile pickle -- we never write one like
3630 * this.
3631 */
3632 PyErr_SetString(UnpicklingError,
3633 "BINUNICODE pickle has negative byte count");
3634 return -1;
3635 }
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003636
Tim Peters0bc93f52003-02-02 18:29:33 +00003637 if (self->read_func(self, &s, l) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003638 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003639
Tim Peterscba30e22003-02-01 06:24:36 +00003640 if (!( unicode = PyUnicode_DecodeUTF8(s, l, NULL)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003641 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003642
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003643 PDATA_PUSH(self->stack, unicode, -1);
3644 return 0;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003645}
Martin v. Löwis339d0f72001-08-17 18:39:25 +00003646#endif
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003647
3648
3649static int
Tim Peterscba30e22003-02-01 06:24:36 +00003650load_tuple(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003651{
3652 PyObject *tup;
3653 int i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003654
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003655 if ((i = marker(self)) < 0) return -1;
3656 if (!( tup=Pdata_popTuple(self->stack, i))) return -1;
3657 PDATA_PUSH(self->stack, tup, -1);
3658 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003659}
3660
3661static int
Tim Peters1d63c9f2003-02-02 20:29:39 +00003662load_counted_tuple(Unpicklerobject *self, int len)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003663{
Tim Peters1d63c9f2003-02-02 20:29:39 +00003664 PyObject *tup = PyTuple_New(len);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003665
Tim Peters1d63c9f2003-02-02 20:29:39 +00003666 if (tup == NULL)
3667 return -1;
3668
3669 while (--len >= 0) {
3670 PyObject *element;
3671
3672 PDATA_POP(self->stack, element);
3673 if (element == NULL)
3674 return -1;
3675 PyTuple_SET_ITEM(tup, len, element);
3676 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003677 PDATA_PUSH(self->stack, tup, -1);
3678 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003679}
3680
3681static int
Tim Peterscba30e22003-02-01 06:24:36 +00003682load_empty_list(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003683{
3684 PyObject *list;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003685
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003686 if (!( list=PyList_New(0))) return -1;
3687 PDATA_PUSH(self->stack, list, -1);
3688 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003689}
3690
3691static int
Tim Peterscba30e22003-02-01 06:24:36 +00003692load_empty_dict(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003693{
3694 PyObject *dict;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003695
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003696 if (!( dict=PyDict_New())) return -1;
3697 PDATA_PUSH(self->stack, dict, -1);
3698 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003699}
3700
3701
3702static int
Tim Peterscba30e22003-02-01 06:24:36 +00003703load_list(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003704{
3705 PyObject *list = 0;
3706 int i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003707
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003708 if ((i = marker(self)) < 0) return -1;
3709 if (!( list=Pdata_popList(self->stack, i))) return -1;
3710 PDATA_PUSH(self->stack, list, -1);
3711 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003712}
3713
3714static int
Tim Peterscba30e22003-02-01 06:24:36 +00003715load_dict(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003716{
3717 PyObject *dict, *key, *value;
3718 int i, j, k;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003719
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003720 if ((i = marker(self)) < 0) return -1;
3721 j=self->stack->length;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003722
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003723 if (!( dict = PyDict_New())) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003724
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003725 for (k = i+1; k < j; k += 2) {
3726 key =self->stack->data[k-1];
3727 value=self->stack->data[k ];
3728 if (PyDict_SetItem(dict, key, value) < 0) {
3729 Py_DECREF(dict);
3730 return -1;
3731 }
3732 }
3733 Pdata_clear(self->stack, i);
3734 PDATA_PUSH(self->stack, dict, -1);
3735 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003736}
3737
3738static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00003739Instance_New(PyObject *cls, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003740{
Tim Peters1f1b2d22003-02-01 02:16:37 +00003741 PyObject *r = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003742
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003743 if (PyClass_Check(cls)) {
3744 int l;
Tim Peters84e87f32001-03-17 04:50:51 +00003745
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003746 if ((l=PyObject_Size(args)) < 0) goto err;
3747 if (!( l )) {
3748 PyObject *__getinitargs__;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003749
Tim Peterscba30e22003-02-01 06:24:36 +00003750 __getinitargs__ = PyObject_GetAttr(cls,
Jeremy Hyltonf00368f2003-06-16 20:19:49 +00003751 __getinitargs___str);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003752 if (!__getinitargs__) {
Tim Peterscba30e22003-02-01 06:24:36 +00003753 /* We have a class with no __getinitargs__,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003754 so bypass usual construction */
3755 PyObject *inst;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003756
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003757 PyErr_Clear();
Tim Peterscba30e22003-02-01 06:24:36 +00003758 if (!( inst=PyInstance_NewRaw(cls, NULL)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003759 goto err;
3760 return inst;
3761 }
3762 Py_DECREF(__getinitargs__);
3763 }
Tim Peters84e87f32001-03-17 04:50:51 +00003764
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003765 if ((r=PyInstance_New(cls, args, NULL))) return r;
3766 else goto err;
3767 }
Tim Peters84e87f32001-03-17 04:50:51 +00003768
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003769 if ((r=PyObject_CallObject(cls, args))) return r;
Guido van Rossum142eeb81997-08-13 03:14:41 +00003770
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003771 err:
3772 {
Neal Norwitz0f8b31a2006-06-28 06:28:31 +00003773 PyObject *tp, *v, *tb, *tmp_value;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003774
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003775 PyErr_Fetch(&tp, &v, &tb);
Neal Norwitz0f8b31a2006-06-28 06:28:31 +00003776 tmp_value = v;
3777 /* NULL occurs when there was a KeyboardInterrupt */
3778 if (tmp_value == NULL)
3779 tmp_value = Py_None;
3780 if ((r = PyTuple_Pack(3, tmp_value, cls, args))) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003781 Py_XDECREF(v);
3782 v=r;
3783 }
3784 PyErr_Restore(tp,v,tb);
3785 }
3786 return NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003787}
Tim Peters84e87f32001-03-17 04:50:51 +00003788
Guido van Rossum60456fd1997-04-09 17:36:32 +00003789
3790static int
Tim Peterscba30e22003-02-01 06:24:36 +00003791load_obj(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003792{
3793 PyObject *class, *tup, *obj=0;
3794 int i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003795
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003796 if ((i = marker(self)) < 0) return -1;
3797 if (!( tup=Pdata_popTuple(self->stack, i+1))) return -1;
3798 PDATA_POP(self->stack, class);
3799 if (class) {
3800 obj = Instance_New(class, tup);
3801 Py_DECREF(class);
3802 }
3803 Py_DECREF(tup);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003804
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003805 if (! obj) return -1;
3806 PDATA_PUSH(self->stack, obj, -1);
3807 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003808}
3809
3810
3811static int
Tim Peterscba30e22003-02-01 06:24:36 +00003812load_inst(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003813{
3814 PyObject *tup, *class=0, *obj=0, *module_name, *class_name;
3815 int i, len;
3816 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003817
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003818 if ((i = marker(self)) < 0) return -1;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003819
Tim Peters0bc93f52003-02-02 18:29:33 +00003820 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003821 if (len < 2) return bad_readline();
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003822 module_name = PyString_FromStringAndSize(s, len - 1);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003823 if (!module_name) return -1;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003824
Tim Peters0bc93f52003-02-02 18:29:33 +00003825 if ((len = self->readline_func(self, &s)) >= 0) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003826 if (len < 2) return bad_readline();
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003827 if ((class_name = PyString_FromStringAndSize(s, len - 1))) {
Tim Peterscba30e22003-02-01 06:24:36 +00003828 class = find_class(module_name, class_name,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003829 self->find_class);
3830 Py_DECREF(class_name);
3831 }
3832 }
3833 Py_DECREF(module_name);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003834
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003835 if (! class) return -1;
Tim Peters84e87f32001-03-17 04:50:51 +00003836
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003837 if ((tup=Pdata_popTuple(self->stack, i))) {
3838 obj = Instance_New(class, tup);
3839 Py_DECREF(tup);
3840 }
3841 Py_DECREF(class);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003842
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003843 if (! obj) return -1;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003844
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003845 PDATA_PUSH(self->stack, obj, -1);
3846 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003847}
3848
Tim Peterseab7db32003-02-13 18:24:14 +00003849static int
3850load_newobj(Unpicklerobject *self)
3851{
3852 PyObject *args = NULL;
3853 PyObject *clsraw = NULL;
3854 PyTypeObject *cls; /* clsraw cast to its true type */
3855 PyObject *obj;
3856
3857 /* Stack is ... cls argtuple, and we want to call
3858 * cls.__new__(cls, *argtuple).
3859 */
3860 PDATA_POP(self->stack, args);
3861 if (args == NULL) goto Fail;
3862 if (! PyTuple_Check(args)) {
3863 PyErr_SetString(UnpicklingError, "NEWOBJ expected an arg "
3864 "tuple.");
3865 goto Fail;
3866 }
3867
3868 PDATA_POP(self->stack, clsraw);
3869 cls = (PyTypeObject *)clsraw;
3870 if (cls == NULL) goto Fail;
3871 if (! PyType_Check(cls)) {
3872 PyErr_SetString(UnpicklingError, "NEWOBJ class argument "
3873 "isn't a type object");
3874 goto Fail;
3875 }
3876 if (cls->tp_new == NULL) {
3877 PyErr_SetString(UnpicklingError, "NEWOBJ class argument "
3878 "has NULL tp_new");
3879 goto Fail;
3880 }
3881
3882 /* Call __new__. */
3883 obj = cls->tp_new(cls, args, NULL);
3884 if (obj == NULL) goto Fail;
3885
3886 Py_DECREF(args);
3887 Py_DECREF(clsraw);
3888 PDATA_PUSH(self->stack, obj, -1);
3889 return 0;
3890
3891 Fail:
3892 Py_XDECREF(args);
3893 Py_XDECREF(clsraw);
3894 return -1;
3895}
Guido van Rossum60456fd1997-04-09 17:36:32 +00003896
3897static int
Tim Peterscba30e22003-02-01 06:24:36 +00003898load_global(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003899{
3900 PyObject *class = 0, *module_name = 0, *class_name = 0;
3901 int len;
3902 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003903
Tim Peters0bc93f52003-02-02 18:29:33 +00003904 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003905 if (len < 2) return bad_readline();
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003906 module_name = PyString_FromStringAndSize(s, len - 1);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003907 if (!module_name) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003908
Tim Peters0bc93f52003-02-02 18:29:33 +00003909 if ((len = self->readline_func(self, &s)) >= 0) {
Tim Peters4e52ca82002-12-07 02:43:28 +00003910 if (len < 2) {
3911 Py_DECREF(module_name);
3912 return bad_readline();
3913 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003914 if ((class_name = PyString_FromStringAndSize(s, len - 1))) {
Tim Peterscba30e22003-02-01 06:24:36 +00003915 class = find_class(module_name, class_name,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003916 self->find_class);
3917 Py_DECREF(class_name);
3918 }
3919 }
3920 Py_DECREF(module_name);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003921
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003922 if (! class) return -1;
3923 PDATA_PUSH(self->stack, class, -1);
3924 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003925}
3926
3927
3928static int
Tim Peterscba30e22003-02-01 06:24:36 +00003929load_persid(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003930{
3931 PyObject *pid = 0;
3932 int len;
3933 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003934
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003935 if (self->pers_func) {
Tim Peters0bc93f52003-02-02 18:29:33 +00003936 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003937 if (len < 2) return bad_readline();
3938
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003939 pid = PyString_FromStringAndSize(s, len - 1);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003940 if (!pid) return -1;
3941
3942 if (PyList_Check(self->pers_func)) {
3943 if (PyList_Append(self->pers_func, pid) < 0) {
3944 Py_DECREF(pid);
3945 return -1;
3946 }
3947 }
3948 else {
3949 ARG_TUP(self, pid);
3950 if (self->arg) {
Tim Peterscba30e22003-02-01 06:24:36 +00003951 pid = PyObject_Call(self->pers_func, self->arg,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003952 NULL);
3953 FREE_ARG_TUP(self);
3954 }
3955 }
3956
3957 if (! pid) return -1;
3958
3959 PDATA_PUSH(self->stack, pid, -1);
3960 return 0;
3961 }
3962 else {
3963 PyErr_SetString(UnpicklingError,
3964 "A load persistent id instruction was encountered,\n"
3965 "but no persistent_load function was specified.");
3966 return -1;
3967 }
3968}
3969
3970static int
Tim Peterscba30e22003-02-01 06:24:36 +00003971load_binpersid(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003972{
3973 PyObject *pid = 0;
3974
3975 if (self->pers_func) {
3976 PDATA_POP(self->stack, pid);
3977 if (! pid) return -1;
3978
3979 if (PyList_Check(self->pers_func)) {
3980 if (PyList_Append(self->pers_func, pid) < 0) {
3981 Py_DECREF(pid);
3982 return -1;
3983 }
3984 }
3985 else {
3986 ARG_TUP(self, pid);
3987 if (self->arg) {
3988 pid = PyObject_Call(self->pers_func, self->arg,
3989 NULL);
3990 FREE_ARG_TUP(self);
3991 }
3992 if (! pid) return -1;
3993 }
3994
3995 PDATA_PUSH(self->stack, pid, -1);
3996 return 0;
3997 }
3998 else {
3999 PyErr_SetString(UnpicklingError,
4000 "A load persistent id instruction was encountered,\n"
4001 "but no persistent_load function was specified.");
4002 return -1;
4003 }
4004}
4005
4006
4007static int
Tim Peterscba30e22003-02-01 06:24:36 +00004008load_pop(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004009{
4010 int len;
4011
4012 if (!( (len=self->stack->length) > 0 )) return stackUnderflow();
4013
4014 /* Note that we split the (pickle.py) stack into two stacks,
4015 an object stack and a mark stack. We have to be clever and
4016 pop the right one. We do this by looking at the top of the
4017 mark stack.
4018 */
4019
4020 if ((self->num_marks > 0) &&
4021 (self->marks[self->num_marks - 1] == len))
4022 self->num_marks--;
4023 else {
4024 len--;
4025 Py_DECREF(self->stack->data[len]);
4026 self->stack->length=len;
4027 }
4028
4029 return 0;
4030}
4031
4032
4033static int
Tim Peterscba30e22003-02-01 06:24:36 +00004034load_pop_mark(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004035{
4036 int i;
4037
4038 if ((i = marker(self)) < 0)
4039 return -1;
4040
4041 Pdata_clear(self->stack, i);
4042
4043 return 0;
4044}
4045
4046
4047static int
Tim Peterscba30e22003-02-01 06:24:36 +00004048load_dup(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004049{
4050 PyObject *last;
4051 int len;
4052
4053 if ((len = self->stack->length) <= 0) return stackUnderflow();
4054 last=self->stack->data[len-1];
4055 Py_INCREF(last);
4056 PDATA_PUSH(self->stack, last, -1);
4057 return 0;
4058}
4059
4060
4061static int
Tim Peterscba30e22003-02-01 06:24:36 +00004062load_get(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004063{
4064 PyObject *py_str = 0, *value = 0;
4065 int len;
4066 char *s;
4067 int rc;
4068
Tim Peters0bc93f52003-02-02 18:29:33 +00004069 if ((len = self->readline_func(self, &s)) < 0) return -1;
Guido van Rossumf9ffb031999-02-04 14:54:04 +00004070 if (len < 2) return bad_readline();
Tim Peters84e87f32001-03-17 04:50:51 +00004071
Gregory P. Smithdd96db62008-06-09 04:58:54 +00004072 if (!( py_str = PyString_FromStringAndSize(s, len - 1))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004073
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004074 value = PyDict_GetItem(self->memo, py_str);
4075 if (! value) {
4076 PyErr_SetObject(BadPickleGet, py_str);
4077 rc = -1;
Tim Peters92c8bb32003-02-13 23:00:26 +00004078 }
4079 else {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004080 PDATA_APPEND(self->stack, value, -1);
4081 rc = 0;
4082 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00004083
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004084 Py_DECREF(py_str);
4085 return rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004086}
4087
4088
4089static int
Tim Peterscba30e22003-02-01 06:24:36 +00004090load_binget(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004091{
4092 PyObject *py_key = 0, *value = 0;
4093 unsigned char key;
4094 char *s;
4095 int rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004096
Tim Peters0bc93f52003-02-02 18:29:33 +00004097 if (self->read_func(self, &s, 1) < 0) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004098
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004099 key = (unsigned char)s[0];
4100 if (!( py_key = PyInt_FromLong((long)key))) return -1;
Guido van Rossumea2b7152000-05-09 18:14:50 +00004101
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004102 value = PyDict_GetItem(self->memo, py_key);
4103 if (! value) {
4104 PyErr_SetObject(BadPickleGet, py_key);
4105 rc = -1;
Tim Peters92c8bb32003-02-13 23:00:26 +00004106 }
4107 else {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004108 PDATA_APPEND(self->stack, value, -1);
4109 rc = 0;
4110 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00004111
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004112 Py_DECREF(py_key);
4113 return rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004114}
4115
4116
4117static int
Tim Peterscba30e22003-02-01 06:24:36 +00004118load_long_binget(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004119{
4120 PyObject *py_key = 0, *value = 0;
4121 unsigned char c;
4122 char *s;
4123 long key;
4124 int rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004125
Tim Peters0bc93f52003-02-02 18:29:33 +00004126 if (self->read_func(self, &s, 4) < 0) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004127
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004128 c = (unsigned char)s[0];
4129 key = (long)c;
4130 c = (unsigned char)s[1];
4131 key |= (long)c << 8;
4132 c = (unsigned char)s[2];
4133 key |= (long)c << 16;
4134 c = (unsigned char)s[3];
4135 key |= (long)c << 24;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004136
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004137 if (!( py_key = PyInt_FromLong((long)key))) return -1;
4138
4139 value = PyDict_GetItem(self->memo, py_key);
4140 if (! value) {
4141 PyErr_SetObject(BadPickleGet, py_key);
4142 rc = -1;
Tim Peters92c8bb32003-02-13 23:00:26 +00004143 }
4144 else {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004145 PDATA_APPEND(self->stack, value, -1);
4146 rc = 0;
4147 }
4148
4149 Py_DECREF(py_key);
4150 return rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004151}
4152
Tim Peters2d629652003-02-04 05:06:17 +00004153/* Push an object from the extension registry (EXT[124]). nbytes is
4154 * the number of bytes following the opcode, holding the index (code) value.
4155 */
4156static int
4157load_extension(Unpicklerobject *self, int nbytes)
4158{
4159 char *codebytes; /* the nbytes bytes after the opcode */
4160 long code; /* calc_binint returns long */
4161 PyObject *py_code; /* code as a Python int */
4162 PyObject *obj; /* the object to push */
4163 PyObject *pair; /* (module_name, class_name) */
4164 PyObject *module_name, *class_name;
4165
4166 assert(nbytes == 1 || nbytes == 2 || nbytes == 4);
4167 if (self->read_func(self, &codebytes, nbytes) < 0) return -1;
4168 code = calc_binint(codebytes, nbytes);
4169 if (code <= 0) { /* note that 0 is forbidden */
4170 /* Corrupt or hostile pickle. */
4171 PyErr_SetString(UnpicklingError, "EXT specifies code <= 0");
4172 return -1;
4173 }
4174
4175 /* Look for the code in the cache. */
4176 py_code = PyInt_FromLong(code);
4177 if (py_code == NULL) return -1;
4178 obj = PyDict_GetItem(extension_cache, py_code);
4179 if (obj != NULL) {
4180 /* Bingo. */
4181 Py_DECREF(py_code);
4182 PDATA_APPEND(self->stack, obj, -1);
4183 return 0;
4184 }
4185
4186 /* Look up the (module_name, class_name) pair. */
4187 pair = PyDict_GetItem(inverted_registry, py_code);
4188 if (pair == NULL) {
4189 Py_DECREF(py_code);
4190 PyErr_Format(PyExc_ValueError, "unregistered extension "
4191 "code %ld", code);
4192 return -1;
4193 }
4194 /* Since the extension registry is manipulable via Python code,
Tim Petersfa05ce32003-02-04 05:20:32 +00004195 * confirm that pair is really a 2-tuple of strings.
Tim Peters2d629652003-02-04 05:06:17 +00004196 */
4197 if (!PyTuple_Check(pair) || PyTuple_Size(pair) != 2 ||
Gregory P. Smithdd96db62008-06-09 04:58:54 +00004198 !PyString_Check(module_name = PyTuple_GET_ITEM(pair, 0)) ||
4199 !PyString_Check(class_name = PyTuple_GET_ITEM(pair, 1))) {
Tim Peters2d629652003-02-04 05:06:17 +00004200 Py_DECREF(py_code);
4201 PyErr_Format(PyExc_ValueError, "_inverted_registry[%ld] "
4202 "isn't a 2-tuple of strings", code);
4203 return -1;
4204 }
4205 /* Load the object. */
4206 obj = find_class(module_name, class_name, self->find_class);
4207 if (obj == NULL) {
4208 Py_DECREF(py_code);
4209 return -1;
4210 }
4211 /* Cache code -> obj. */
4212 code = PyDict_SetItem(extension_cache, py_code, obj);
4213 Py_DECREF(py_code);
4214 if (code < 0) {
4215 Py_DECREF(obj);
4216 return -1;
4217 }
4218 PDATA_PUSH(self->stack, obj, -1);
4219 return 0;
4220}
Guido van Rossum60456fd1997-04-09 17:36:32 +00004221
4222static int
Tim Peterscba30e22003-02-01 06:24:36 +00004223load_put(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004224{
4225 PyObject *py_str = 0, *value = 0;
4226 int len, l;
4227 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004228
Tim Peters0bc93f52003-02-02 18:29:33 +00004229 if ((l = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004230 if (l < 2) return bad_readline();
4231 if (!( len=self->stack->length )) return stackUnderflow();
Gregory P. Smithdd96db62008-06-09 04:58:54 +00004232 if (!( py_str = PyString_FromStringAndSize(s, l - 1))) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004233 value=self->stack->data[len-1];
4234 l=PyDict_SetItem(self->memo, py_str, value);
4235 Py_DECREF(py_str);
4236 return l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004237}
4238
4239
4240static int
Tim Peterscba30e22003-02-01 06:24:36 +00004241load_binput(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004242{
4243 PyObject *py_key = 0, *value = 0;
4244 unsigned char key;
4245 char *s;
4246 int len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004247
Tim Peters0bc93f52003-02-02 18:29:33 +00004248 if (self->read_func(self, &s, 1) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004249 if (!( (len=self->stack->length) > 0 )) return stackUnderflow();
Guido van Rossum60456fd1997-04-09 17:36:32 +00004250
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004251 key = (unsigned char)s[0];
Guido van Rossum053b8df1998-11-25 16:18:00 +00004252
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004253 if (!( py_key = PyInt_FromLong((long)key))) return -1;
4254 value=self->stack->data[len-1];
4255 len=PyDict_SetItem(self->memo, py_key, value);
4256 Py_DECREF(py_key);
4257 return len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004258}
4259
4260
4261static int
Tim Peterscba30e22003-02-01 06:24:36 +00004262load_long_binput(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004263{
4264 PyObject *py_key = 0, *value = 0;
4265 long key;
4266 unsigned char c;
4267 char *s;
4268 int len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004269
Tim Peters0bc93f52003-02-02 18:29:33 +00004270 if (self->read_func(self, &s, 4) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004271 if (!( len=self->stack->length )) return stackUnderflow();
Guido van Rossum60456fd1997-04-09 17:36:32 +00004272
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004273 c = (unsigned char)s[0];
4274 key = (long)c;
4275 c = (unsigned char)s[1];
4276 key |= (long)c << 8;
4277 c = (unsigned char)s[2];
4278 key |= (long)c << 16;
4279 c = (unsigned char)s[3];
4280 key |= (long)c << 24;
Tim Peters84e87f32001-03-17 04:50:51 +00004281
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004282 if (!( py_key = PyInt_FromLong(key))) return -1;
4283 value=self->stack->data[len-1];
4284 len=PyDict_SetItem(self->memo, py_key, value);
4285 Py_DECREF(py_key);
4286 return len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004287}
4288
4289
4290static int
Tim Peterscba30e22003-02-01 06:24:36 +00004291do_append(Unpicklerobject *self, int x)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004292{
4293 PyObject *value = 0, *list = 0, *append_method = 0;
4294 int len, i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004295
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004296 len=self->stack->length;
4297 if (!( len >= x && x > 0 )) return stackUnderflow();
4298 /* nothing to do */
Tim Peterscba30e22003-02-01 06:24:36 +00004299 if (len==x) return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004300
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004301 list=self->stack->data[x-1];
Guido van Rossum60456fd1997-04-09 17:36:32 +00004302
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004303 if (PyList_Check(list)) {
4304 PyObject *slice;
4305 int list_len;
Tim Peters84e87f32001-03-17 04:50:51 +00004306
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004307 slice=Pdata_popList(self->stack, x);
Neal Norwitzb59d08c2006-07-22 16:20:49 +00004308 if (! slice) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004309 list_len = PyList_GET_SIZE(list);
4310 i=PyList_SetSlice(list, list_len, list_len, slice);
4311 Py_DECREF(slice);
4312 return i;
4313 }
4314 else {
Guido van Rossum60456fd1997-04-09 17:36:32 +00004315
Tim Peterscba30e22003-02-01 06:24:36 +00004316 if (!( append_method = PyObject_GetAttr(list, append_str)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004317 return -1;
4318
4319 for (i = x; i < len; i++) {
4320 PyObject *junk;
4321
4322 value=self->stack->data[i];
4323 junk=0;
4324 ARG_TUP(self, value);
4325 if (self->arg) {
Tim Peterscba30e22003-02-01 06:24:36 +00004326 junk = PyObject_Call(append_method, self->arg,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004327 NULL);
4328 FREE_ARG_TUP(self);
4329 }
4330 if (! junk) {
4331 Pdata_clear(self->stack, i+1);
4332 self->stack->length=x;
4333 Py_DECREF(append_method);
4334 return -1;
4335 }
4336 Py_DECREF(junk);
4337 }
4338 self->stack->length=x;
4339 Py_DECREF(append_method);
4340 }
4341
4342 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004343}
4344
4345
4346static int
Tim Peterscba30e22003-02-01 06:24:36 +00004347load_append(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004348{
4349 return do_append(self, self->stack->length - 1);
Guido van Rossum60456fd1997-04-09 17:36:32 +00004350}
4351
4352
4353static int
Tim Peterscba30e22003-02-01 06:24:36 +00004354load_appends(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004355{
4356 return do_append(self, marker(self));
Guido van Rossum60456fd1997-04-09 17:36:32 +00004357}
4358
4359
4360static int
Tim Peterscba30e22003-02-01 06:24:36 +00004361do_setitems(Unpicklerobject *self, int x)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004362{
4363 PyObject *value = 0, *key = 0, *dict = 0;
4364 int len, i, r=0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004365
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004366 if (!( (len=self->stack->length) >= x
4367 && x > 0 )) return stackUnderflow();
Guido van Rossum60456fd1997-04-09 17:36:32 +00004368
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004369 dict=self->stack->data[x-1];
Guido van Rossum60456fd1997-04-09 17:36:32 +00004370
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004371 for (i = x+1; i < len; i += 2) {
4372 key =self->stack->data[i-1];
4373 value=self->stack->data[i ];
4374 if (PyObject_SetItem(dict, key, value) < 0) {
4375 r=-1;
4376 break;
4377 }
4378 }
4379
4380 Pdata_clear(self->stack, x);
4381
4382 return r;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004383}
4384
4385
Tim Peters84e87f32001-03-17 04:50:51 +00004386static int
Tim Peterscba30e22003-02-01 06:24:36 +00004387load_setitem(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004388{
4389 return do_setitems(self, self->stack->length - 2);
4390}
Guido van Rossum60456fd1997-04-09 17:36:32 +00004391
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004392static int
Tim Peterscba30e22003-02-01 06:24:36 +00004393load_setitems(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004394{
4395 return do_setitems(self, marker(self));
Guido van Rossum60456fd1997-04-09 17:36:32 +00004396}
4397
Tim Peters84e87f32001-03-17 04:50:51 +00004398
Guido van Rossum60456fd1997-04-09 17:36:32 +00004399static int
Tim Peterscba30e22003-02-01 06:24:36 +00004400load_build(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004401{
Tim Peters080c88b2003-02-15 03:01:11 +00004402 PyObject *state, *inst, *slotstate;
4403 PyObject *__setstate__;
4404 PyObject *d_key, *d_value;
Martin v. Löwis18e16552006-02-15 17:27:45 +00004405 Py_ssize_t i;
Tim Peters080c88b2003-02-15 03:01:11 +00004406 int res = -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004407
Tim Peters080c88b2003-02-15 03:01:11 +00004408 /* Stack is ... instance, state. We want to leave instance at
4409 * the stack top, possibly mutated via instance.__setstate__(state).
4410 */
4411 if (self->stack->length < 2)
4412 return stackUnderflow();
4413 PDATA_POP(self->stack, state);
4414 if (state == NULL)
4415 return -1;
4416 inst = self->stack->data[self->stack->length - 1];
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004417
Tim Peters080c88b2003-02-15 03:01:11 +00004418 __setstate__ = PyObject_GetAttr(inst, __setstate___str);
4419 if (__setstate__ != NULL) {
4420 PyObject *junk = NULL;
4421
4422 /* The explicit __setstate__ is responsible for everything. */
4423 ARG_TUP(self, state);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004424 if (self->arg) {
4425 junk = PyObject_Call(__setstate__, self->arg, NULL);
4426 FREE_ARG_TUP(self);
4427 }
4428 Py_DECREF(__setstate__);
Tim Peters080c88b2003-02-15 03:01:11 +00004429 if (junk == NULL)
4430 return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004431 Py_DECREF(junk);
4432 return 0;
4433 }
Jeremy Hyltonf00368f2003-06-16 20:19:49 +00004434 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4435 return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004436 PyErr_Clear();
Tim Peters080c88b2003-02-15 03:01:11 +00004437
4438 /* A default __setstate__. First see whether state embeds a
4439 * slot state dict too (a proto 2 addition).
4440 */
4441 if (PyTuple_Check(state) && PyTuple_Size(state) == 2) {
4442 PyObject *temp = state;
4443 state = PyTuple_GET_ITEM(temp, 0);
4444 slotstate = PyTuple_GET_ITEM(temp, 1);
4445 Py_INCREF(state);
4446 Py_INCREF(slotstate);
4447 Py_DECREF(temp);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004448 }
Tim Peters080c88b2003-02-15 03:01:11 +00004449 else
4450 slotstate = NULL;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004451
Tim Peters080c88b2003-02-15 03:01:11 +00004452 /* Set inst.__dict__ from the state dict (if any). */
4453 if (state != Py_None) {
4454 PyObject *dict;
4455 if (! PyDict_Check(state)) {
4456 PyErr_SetString(UnpicklingError, "state is not a "
4457 "dictionary");
4458 goto finally;
4459 }
4460 dict = PyObject_GetAttr(inst, __dict___str);
4461 if (dict == NULL)
4462 goto finally;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004463
Tim Peters080c88b2003-02-15 03:01:11 +00004464 i = 0;
4465 while (PyDict_Next(state, &i, &d_key, &d_value)) {
4466 if (PyObject_SetItem(dict, d_key, d_value) < 0)
4467 goto finally;
4468 }
4469 Py_DECREF(dict);
4470 }
4471
4472 /* Also set instance attributes from the slotstate dict (if any). */
4473 if (slotstate != NULL) {
4474 if (! PyDict_Check(slotstate)) {
4475 PyErr_SetString(UnpicklingError, "slot state is not "
4476 "a dictionary");
4477 goto finally;
4478 }
4479 i = 0;
4480 while (PyDict_Next(slotstate, &i, &d_key, &d_value)) {
4481 if (PyObject_SetAttr(inst, d_key, d_value) < 0)
4482 goto finally;
4483 }
4484 }
4485 res = 0;
4486
4487 finally:
4488 Py_DECREF(state);
4489 Py_XDECREF(slotstate);
4490 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004491}
4492
4493
4494static int
Tim Peterscba30e22003-02-01 06:24:36 +00004495load_mark(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004496{
4497 int s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004498
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004499 /* Note that we split the (pickle.py) stack into two stacks, an
4500 object stack and a mark stack. Here we push a mark onto the
4501 mark stack.
4502 */
Guido van Rossum60456fd1997-04-09 17:36:32 +00004503
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004504 if ((self->num_marks + 1) >= self->marks_size) {
Kristján Valur Jónsson17b8e972007-04-25 00:10:50 +00004505 int *marks;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004506 s=self->marks_size+20;
4507 if (s <= self->num_marks) s=self->num_marks + 1;
4508 if (self->marks == NULL)
Kristján Valur Jónsson17b8e972007-04-25 00:10:50 +00004509 marks=(int *)malloc(s * sizeof(int));
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004510 else
Kristján Valur Jónsson17b8e972007-04-25 00:10:50 +00004511 marks=(int *)realloc(self->marks,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004512 s * sizeof(int));
Kristján Valur Jónsson17b8e972007-04-25 00:10:50 +00004513 if (!marks) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004514 PyErr_NoMemory();
4515 return -1;
4516 }
Kristján Valur Jónsson17b8e972007-04-25 00:10:50 +00004517 self->marks = marks;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004518 self->marks_size = s;
4519 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00004520
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004521 self->marks[self->num_marks++] = self->stack->length;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004522
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004523 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004524}
4525
Guido van Rossum60456fd1997-04-09 17:36:32 +00004526static int
Tim Peterscba30e22003-02-01 06:24:36 +00004527load_reduce(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004528{
4529 PyObject *callable = 0, *arg_tup = 0, *ob = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004530
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004531 PDATA_POP(self->stack, arg_tup);
4532 if (! arg_tup) return -1;
4533 PDATA_POP(self->stack, callable);
4534 if (callable) {
4535 ob = Instance_New(callable, arg_tup);
4536 Py_DECREF(callable);
4537 }
4538 Py_DECREF(arg_tup);
Guido van Rossum60456fd1997-04-09 17:36:32 +00004539
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004540 if (! ob) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004541
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004542 PDATA_PUSH(self->stack, ob, -1);
4543 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004544}
Tim Peters84e87f32001-03-17 04:50:51 +00004545
Tim Peters4190fb82003-02-02 16:09:05 +00004546/* Just raises an error if we don't know the protocol specified. PROTO
4547 * is the first opcode for protocols >= 2.
4548 */
4549static int
4550load_proto(Unpicklerobject *self)
4551{
4552 int i;
4553 char *protobyte;
4554
4555 i = self->read_func(self, &protobyte, 1);
4556 if (i < 0)
4557 return -1;
4558
4559 i = calc_binint(protobyte, 1);
4560 /* No point checking for < 0, since calc_binint returns an unsigned
4561 * int when chewing on 1 byte.
4562 */
4563 assert(i >= 0);
Tim Peters8587b3c2003-02-13 15:44:41 +00004564 if (i <= HIGHEST_PROTOCOL)
Tim Peters4190fb82003-02-02 16:09:05 +00004565 return 0;
4566
4567 PyErr_Format(PyExc_ValueError, "unsupported pickle protocol: %d", i);
4568 return -1;
4569}
4570
Guido van Rossum60456fd1997-04-09 17:36:32 +00004571static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00004572load(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004573{
4574 PyObject *err = 0, *val = 0;
4575 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004576
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004577 self->num_marks = 0;
4578 if (self->stack->length) Pdata_clear(self->stack, 0);
Guido van Rossum60456fd1997-04-09 17:36:32 +00004579
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004580 while (1) {
Tim Peters0bc93f52003-02-02 18:29:33 +00004581 if (self->read_func(self, &s, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004582 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004583
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004584 switch (s[0]) {
4585 case NONE:
4586 if (load_none(self) < 0)
4587 break;
4588 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004589
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004590 case BININT:
4591 if (load_binint(self) < 0)
4592 break;
4593 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004594
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004595 case BININT1:
4596 if (load_binint1(self) < 0)
4597 break;
4598 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004599
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004600 case BININT2:
4601 if (load_binint2(self) < 0)
4602 break;
4603 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004604
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004605 case INT:
4606 if (load_int(self) < 0)
4607 break;
4608 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004609
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004610 case LONG:
4611 if (load_long(self) < 0)
4612 break;
4613 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004614
Tim Petersee1a53c2003-02-02 02:57:53 +00004615 case LONG1:
4616 if (load_counted_long(self, 1) < 0)
4617 break;
4618 continue;
4619
4620 case LONG4:
4621 if (load_counted_long(self, 4) < 0)
4622 break;
4623 continue;
4624
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004625 case FLOAT:
4626 if (load_float(self) < 0)
4627 break;
4628 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004629
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004630 case BINFLOAT:
4631 if (load_binfloat(self) < 0)
4632 break;
4633 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004634
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004635 case BINSTRING:
4636 if (load_binstring(self) < 0)
4637 break;
4638 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004639
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004640 case SHORT_BINSTRING:
4641 if (load_short_binstring(self) < 0)
4642 break;
4643 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004644
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004645 case STRING:
4646 if (load_string(self) < 0)
4647 break;
4648 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004649
Martin v. Löwis339d0f72001-08-17 18:39:25 +00004650#ifdef Py_USING_UNICODE
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004651 case UNICODE:
4652 if (load_unicode(self) < 0)
4653 break;
4654 continue;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00004655
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004656 case BINUNICODE:
4657 if (load_binunicode(self) < 0)
4658 break;
4659 continue;
Martin v. Löwis339d0f72001-08-17 18:39:25 +00004660#endif
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00004661
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004662 case EMPTY_TUPLE:
Tim Peters1d63c9f2003-02-02 20:29:39 +00004663 if (load_counted_tuple(self, 0) < 0)
4664 break;
4665 continue;
4666
4667 case TUPLE1:
4668 if (load_counted_tuple(self, 1) < 0)
4669 break;
4670 continue;
4671
4672 case TUPLE2:
4673 if (load_counted_tuple(self, 2) < 0)
4674 break;
4675 continue;
4676
4677 case TUPLE3:
4678 if (load_counted_tuple(self, 3) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004679 break;
4680 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004681
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004682 case TUPLE:
4683 if (load_tuple(self) < 0)
4684 break;
4685 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004686
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004687 case EMPTY_LIST:
4688 if (load_empty_list(self) < 0)
4689 break;
4690 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004691
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004692 case LIST:
4693 if (load_list(self) < 0)
4694 break;
4695 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004696
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004697 case EMPTY_DICT:
4698 if (load_empty_dict(self) < 0)
4699 break;
4700 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004701
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004702 case DICT:
4703 if (load_dict(self) < 0)
4704 break;
4705 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004706
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004707 case OBJ:
4708 if (load_obj(self) < 0)
4709 break;
4710 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004711
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004712 case INST:
4713 if (load_inst(self) < 0)
4714 break;
4715 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004716
Tim Peterseab7db32003-02-13 18:24:14 +00004717 case NEWOBJ:
4718 if (load_newobj(self) < 0)
4719 break;
4720 continue;
4721
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004722 case GLOBAL:
4723 if (load_global(self) < 0)
4724 break;
4725 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004726
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004727 case APPEND:
4728 if (load_append(self) < 0)
4729 break;
4730 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004731
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004732 case APPENDS:
4733 if (load_appends(self) < 0)
4734 break;
4735 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00004736
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004737 case BUILD:
4738 if (load_build(self) < 0)
4739 break;
4740 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00004741
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004742 case DUP:
4743 if (load_dup(self) < 0)
4744 break;
4745 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004746
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004747 case BINGET:
4748 if (load_binget(self) < 0)
4749 break;
4750 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004751
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004752 case LONG_BINGET:
4753 if (load_long_binget(self) < 0)
4754 break;
4755 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00004756
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004757 case GET:
4758 if (load_get(self) < 0)
4759 break;
4760 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004761
Tim Peters2d629652003-02-04 05:06:17 +00004762 case EXT1:
4763 if (load_extension(self, 1) < 0)
4764 break;
4765 continue;
4766
4767 case EXT2:
4768 if (load_extension(self, 2) < 0)
4769 break;
4770 continue;
4771
4772 case EXT4:
4773 if (load_extension(self, 4) < 0)
4774 break;
4775 continue;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004776 case MARK:
4777 if (load_mark(self) < 0)
4778 break;
4779 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004780
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004781 case BINPUT:
4782 if (load_binput(self) < 0)
4783 break;
4784 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004785
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004786 case LONG_BINPUT:
4787 if (load_long_binput(self) < 0)
4788 break;
4789 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00004790
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004791 case PUT:
4792 if (load_put(self) < 0)
4793 break;
4794 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004795
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004796 case POP:
4797 if (load_pop(self) < 0)
4798 break;
4799 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004800
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004801 case POP_MARK:
4802 if (load_pop_mark(self) < 0)
4803 break;
4804 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004805
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004806 case SETITEM:
4807 if (load_setitem(self) < 0)
4808 break;
4809 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004810
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004811 case SETITEMS:
4812 if (load_setitems(self) < 0)
4813 break;
4814 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004815
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004816 case STOP:
4817 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004818
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004819 case PERSID:
4820 if (load_persid(self) < 0)
4821 break;
4822 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004823
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004824 case BINPERSID:
4825 if (load_binpersid(self) < 0)
4826 break;
4827 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004828
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004829 case REDUCE:
4830 if (load_reduce(self) < 0)
4831 break;
4832 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004833
Tim Peters4190fb82003-02-02 16:09:05 +00004834 case PROTO:
4835 if (load_proto(self) < 0)
4836 break;
4837 continue;
4838
Tim Peters3c67d792003-02-02 17:59:11 +00004839 case NEWTRUE:
4840 if (load_bool(self, Py_True) < 0)
4841 break;
4842 continue;
4843
4844 case NEWFALSE:
4845 if (load_bool(self, Py_False) < 0)
4846 break;
4847 continue;
4848
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004849 case '\0':
4850 /* end of file */
4851 PyErr_SetNone(PyExc_EOFError);
4852 break;
Tim Peterscba30e22003-02-01 06:24:36 +00004853
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004854 default:
Tim Peterscba30e22003-02-01 06:24:36 +00004855 cPickle_ErrFormat(UnpicklingError,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004856 "invalid load key, '%s'.",
4857 "c", s[0]);
4858 return NULL;
4859 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00004860
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004861 break;
4862 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00004863
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004864 if ((err = PyErr_Occurred())) {
4865 if (err == PyExc_EOFError) {
4866 PyErr_SetNone(PyExc_EOFError);
4867 }
4868 return NULL;
4869 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00004870
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004871 PDATA_POP(self->stack, val);
4872 return val;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004873}
Tim Peters84e87f32001-03-17 04:50:51 +00004874
Guido van Rossum60456fd1997-04-09 17:36:32 +00004875
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004876/* No-load functions to support noload, which is used to
4877 find persistent references. */
4878
4879static int
Tim Peterscba30e22003-02-01 06:24:36 +00004880noload_obj(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004881{
4882 int i;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004883
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004884 if ((i = marker(self)) < 0) return -1;
4885 return Pdata_clear(self->stack, i+1);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004886}
4887
4888
4889static int
Tim Peterscba30e22003-02-01 06:24:36 +00004890noload_inst(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004891{
4892 int i;
4893 char *s;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004894
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004895 if ((i = marker(self)) < 0) return -1;
4896 Pdata_clear(self->stack, i);
Tim Peters0bc93f52003-02-02 18:29:33 +00004897 if (self->readline_func(self, &s) < 0) return -1;
4898 if (self->readline_func(self, &s) < 0) return -1;
Tim Peterseab7db32003-02-13 18:24:14 +00004899 PDATA_APPEND(self->stack, Py_None, -1);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004900 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004901}
4902
4903static int
Tim Peterseab7db32003-02-13 18:24:14 +00004904noload_newobj(Unpicklerobject *self)
4905{
4906 PyObject *obj;
4907
4908 PDATA_POP(self->stack, obj); /* pop argtuple */
4909 if (obj == NULL) return -1;
4910 Py_DECREF(obj);
4911
4912 PDATA_POP(self->stack, obj); /* pop cls */
4913 if (obj == NULL) return -1;
4914 Py_DECREF(obj);
4915
4916 PDATA_APPEND(self->stack, Py_None, -1);
4917 return 0;
4918}
4919
4920static int
Tim Peterscba30e22003-02-01 06:24:36 +00004921noload_global(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004922{
4923 char *s;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004924
Tim Peters0bc93f52003-02-02 18:29:33 +00004925 if (self->readline_func(self, &s) < 0) return -1;
4926 if (self->readline_func(self, &s) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004927 PDATA_APPEND(self->stack, Py_None,-1);
4928 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004929}
4930
4931static int
Tim Peterscba30e22003-02-01 06:24:36 +00004932noload_reduce(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004933{
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004934
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004935 if (self->stack->length < 2) return stackUnderflow();
4936 Pdata_clear(self->stack, self->stack->length-2);
4937 PDATA_APPEND(self->stack, Py_None,-1);
4938 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004939}
4940
4941static int
4942noload_build(Unpicklerobject *self) {
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004943
Guido van Rossum053b8df1998-11-25 16:18:00 +00004944 if (self->stack->length < 1) return stackUnderflow();
4945 Pdata_clear(self->stack, self->stack->length-1);
Guido van Rossum50f385c1998-12-04 18:48:44 +00004946 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004947}
4948
Tim Peters2d629652003-02-04 05:06:17 +00004949static int
4950noload_extension(Unpicklerobject *self, int nbytes)
4951{
4952 char *codebytes;
4953
4954 assert(nbytes == 1 || nbytes == 2 || nbytes == 4);
4955 if (self->read_func(self, &codebytes, nbytes) < 0) return -1;
4956 PDATA_APPEND(self->stack, Py_None, -1);
4957 return 0;
4958}
4959
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004960
4961static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00004962noload(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004963{
4964 PyObject *err = 0, *val = 0;
4965 char *s;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004966
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004967 self->num_marks = 0;
4968 Pdata_clear(self->stack, 0);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004969
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004970 while (1) {
Tim Peters0bc93f52003-02-02 18:29:33 +00004971 if (self->read_func(self, &s, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004972 break;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004973
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004974 switch (s[0]) {
4975 case NONE:
4976 if (load_none(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 BININT:
4981 if (load_binint(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 BININT1:
4986 if (load_binint1(self) < 0)
4987 break;
4988 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004989
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004990 case BININT2:
4991 if (load_binint2(self) < 0)
4992 break;
4993 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004994
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004995 case INT:
4996 if (load_int(self) < 0)
4997 break;
4998 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004999
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005000 case LONG:
5001 if (load_long(self) < 0)
5002 break;
5003 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005004
Tim Peters4190fb82003-02-02 16:09:05 +00005005 case LONG1:
5006 if (load_counted_long(self, 1) < 0)
5007 break;
5008 continue;
5009
5010 case LONG4:
5011 if (load_counted_long(self, 4) < 0)
5012 break;
5013 continue;
5014
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005015 case FLOAT:
5016 if (load_float(self) < 0)
5017 break;
5018 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005019
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005020 case BINFLOAT:
5021 if (load_binfloat(self) < 0)
5022 break;
5023 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005024
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005025 case BINSTRING:
5026 if (load_binstring(self) < 0)
5027 break;
5028 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005029
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005030 case SHORT_BINSTRING:
5031 if (load_short_binstring(self) < 0)
5032 break;
5033 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005034
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005035 case STRING:
5036 if (load_string(self) < 0)
5037 break;
5038 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005039
Martin v. Löwis339d0f72001-08-17 18:39:25 +00005040#ifdef Py_USING_UNICODE
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005041 case UNICODE:
5042 if (load_unicode(self) < 0)
5043 break;
5044 continue;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00005045
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005046 case BINUNICODE:
5047 if (load_binunicode(self) < 0)
5048 break;
5049 continue;
Martin v. Löwis339d0f72001-08-17 18:39:25 +00005050#endif
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00005051
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005052 case EMPTY_TUPLE:
Tim Peters1d63c9f2003-02-02 20:29:39 +00005053 if (load_counted_tuple(self, 0) < 0)
5054 break;
5055 continue;
5056
5057 case TUPLE1:
5058 if (load_counted_tuple(self, 1) < 0)
5059 break;
5060 continue;
5061
5062 case TUPLE2:
5063 if (load_counted_tuple(self, 2) < 0)
5064 break;
5065 continue;
5066
5067 case TUPLE3:
5068 if (load_counted_tuple(self, 3) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005069 break;
5070 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005071
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005072 case TUPLE:
5073 if (load_tuple(self) < 0)
5074 break;
5075 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005076
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005077 case EMPTY_LIST:
5078 if (load_empty_list(self) < 0)
5079 break;
5080 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005081
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005082 case LIST:
5083 if (load_list(self) < 0)
5084 break;
5085 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005086
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005087 case EMPTY_DICT:
5088 if (load_empty_dict(self) < 0)
5089 break;
5090 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005091
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005092 case DICT:
5093 if (load_dict(self) < 0)
5094 break;
5095 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005096
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005097 case OBJ:
5098 if (noload_obj(self) < 0)
5099 break;
5100 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005101
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005102 case INST:
5103 if (noload_inst(self) < 0)
5104 break;
5105 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005106
Tim Peterseab7db32003-02-13 18:24:14 +00005107 case NEWOBJ:
5108 if (noload_newobj(self) < 0)
5109 break;
5110 continue;
5111
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005112 case GLOBAL:
5113 if (noload_global(self) < 0)
5114 break;
5115 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005116
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005117 case APPEND:
5118 if (load_append(self) < 0)
5119 break;
5120 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005121
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005122 case APPENDS:
5123 if (load_appends(self) < 0)
5124 break;
5125 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00005126
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005127 case BUILD:
5128 if (noload_build(self) < 0)
5129 break;
5130 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00005131
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005132 case DUP:
5133 if (load_dup(self) < 0)
5134 break;
5135 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005136
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005137 case BINGET:
5138 if (load_binget(self) < 0)
5139 break;
5140 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005141
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005142 case LONG_BINGET:
5143 if (load_long_binget(self) < 0)
5144 break;
5145 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00005146
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005147 case GET:
5148 if (load_get(self) < 0)
5149 break;
5150 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005151
Tim Peters2d629652003-02-04 05:06:17 +00005152 case EXT1:
5153 if (noload_extension(self, 1) < 0)
5154 break;
5155 continue;
5156
5157 case EXT2:
5158 if (noload_extension(self, 2) < 0)
5159 break;
5160 continue;
5161
5162 case EXT4:
5163 if (noload_extension(self, 4) < 0)
5164 break;
5165 continue;
5166
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005167 case MARK:
5168 if (load_mark(self) < 0)
5169 break;
5170 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005171
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005172 case BINPUT:
5173 if (load_binput(self) < 0)
5174 break;
5175 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005176
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005177 case LONG_BINPUT:
5178 if (load_long_binput(self) < 0)
5179 break;
5180 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00005181
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005182 case PUT:
5183 if (load_put(self) < 0)
5184 break;
5185 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005186
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005187 case POP:
5188 if (load_pop(self) < 0)
5189 break;
5190 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005191
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005192 case POP_MARK:
5193 if (load_pop_mark(self) < 0)
5194 break;
5195 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005196
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005197 case SETITEM:
5198 if (load_setitem(self) < 0)
5199 break;
5200 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005201
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005202 case SETITEMS:
5203 if (load_setitems(self) < 0)
5204 break;
5205 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005206
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005207 case STOP:
5208 break;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005209
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005210 case PERSID:
5211 if (load_persid(self) < 0)
5212 break;
5213 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005214
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005215 case BINPERSID:
5216 if (load_binpersid(self) < 0)
5217 break;
5218 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005219
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005220 case REDUCE:
5221 if (noload_reduce(self) < 0)
5222 break;
5223 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005224
Tim Peters4190fb82003-02-02 16:09:05 +00005225 case PROTO:
5226 if (load_proto(self) < 0)
5227 break;
5228 continue;
5229
Tim Peters3c67d792003-02-02 17:59:11 +00005230 case NEWTRUE:
5231 if (load_bool(self, Py_True) < 0)
5232 break;
5233 continue;
5234
5235 case NEWFALSE:
5236 if (load_bool(self, Py_False) < 0)
5237 break;
5238 continue;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005239 default:
Tim Peterscba30e22003-02-01 06:24:36 +00005240 cPickle_ErrFormat(UnpicklingError,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005241 "invalid load key, '%s'.",
5242 "c", s[0]);
5243 return NULL;
5244 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005245
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005246 break;
5247 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005248
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005249 if ((err = PyErr_Occurred())) {
5250 if (err == PyExc_EOFError) {
5251 PyErr_SetNone(PyExc_EOFError);
5252 }
5253 return NULL;
5254 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005255
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005256 PDATA_POP(self->stack, val);
5257 return val;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005258}
Tim Peters84e87f32001-03-17 04:50:51 +00005259
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005260
Guido van Rossum60456fd1997-04-09 17:36:32 +00005261static PyObject *
Georg Brandl96a8c392006-05-29 21:04:52 +00005262Unpickler_load(Unpicklerobject *self, PyObject *unused)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005263{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005264 return load(self);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005265}
5266
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005267static PyObject *
Georg Brandl96a8c392006-05-29 21:04:52 +00005268Unpickler_noload(Unpicklerobject *self, PyObject *unused)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005269{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005270 return noload(self);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005271}
5272
Guido van Rossum60456fd1997-04-09 17:36:32 +00005273
5274static struct PyMethodDef Unpickler_methods[] = {
Georg Brandl96a8c392006-05-29 21:04:52 +00005275 {"load", (PyCFunction)Unpickler_load, METH_NOARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00005276 PyDoc_STR("load() -- Load a pickle")
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005277 },
Georg Brandl96a8c392006-05-29 21:04:52 +00005278 {"noload", (PyCFunction)Unpickler_noload, METH_NOARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00005279 PyDoc_STR(
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005280 "noload() -- not load a pickle, but go through most of the motions\n"
5281 "\n"
5282 "This function can be used to read past a pickle without instantiating\n"
5283 "any objects or importing any modules. It can also be used to find all\n"
5284 "persistent references without instantiating any objects or importing\n"
Neal Norwitz200788c2002-08-13 22:20:41 +00005285 "any modules.\n")
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005286 },
Guido van Rossum60456fd1997-04-09 17:36:32 +00005287 {NULL, NULL} /* sentinel */
5288};
5289
5290
5291static Unpicklerobject *
Tim Peterscba30e22003-02-01 06:24:36 +00005292newUnpicklerobject(PyObject *f)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005293{
5294 Unpicklerobject *self;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005295
Jeremy Hylton7b5ce7f2003-04-09 21:25:30 +00005296 if (!( self = PyObject_GC_New(Unpicklerobject, &Unpicklertype)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005297 return NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005298
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005299 self->file = NULL;
5300 self->arg = NULL;
5301 self->stack = (Pdata*)Pdata_New();
5302 self->pers_func = NULL;
5303 self->last_string = NULL;
5304 self->marks = NULL;
5305 self->num_marks = 0;
5306 self->marks_size = 0;
5307 self->buf_size = 0;
5308 self->read = NULL;
5309 self->readline = NULL;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005310 self->find_class = NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005311
Tim Peterscba30e22003-02-01 06:24:36 +00005312 if (!( self->memo = PyDict_New()))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005313 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005314
Neal Norwitzb59d08c2006-07-22 16:20:49 +00005315 if (!self->stack)
5316 goto err;
5317
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005318 Py_INCREF(f);
5319 self->file = f;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005320
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005321 /* Set read, readline based on type of f */
5322 if (PyFile_Check(f)) {
5323 self->fp = PyFile_AsFile(f);
5324 if (self->fp == NULL) {
Tim Peterscba30e22003-02-01 06:24:36 +00005325 PyErr_SetString(PyExc_ValueError,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005326 "I/O operation on closed file");
5327 goto err;
5328 }
5329 self->read_func = read_file;
5330 self->readline_func = readline_file;
Guido van Rossumc91fcaa1999-03-29 20:00:14 +00005331 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005332 else if (PycStringIO_InputCheck(f)) {
5333 self->fp = NULL;
5334 self->read_func = read_cStringIO;
5335 self->readline_func = readline_cStringIO;
5336 }
5337 else {
Guido van Rossum60456fd1997-04-09 17:36:32 +00005338
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005339 self->fp = NULL;
5340 self->read_func = read_other;
5341 self->readline_func = readline_other;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005342
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005343 if (!( (self->readline = PyObject_GetAttr(f, readline_str)) &&
5344 (self->read = PyObject_GetAttr(f, read_str)))) {
5345 PyErr_Clear();
Tim Peterscba30e22003-02-01 06:24:36 +00005346 PyErr_SetString( PyExc_TypeError,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005347 "argument must have 'read' and "
5348 "'readline' attributes" );
5349 goto err;
5350 }
5351 }
Jeremy Hylton7b5ce7f2003-04-09 21:25:30 +00005352 PyObject_GC_Track(self);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005353
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005354 return self;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005355
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005356 err:
5357 Py_DECREF((PyObject *)self);
5358 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005359}
5360
5361
5362static PyObject *
Georg Brandl96a8c392006-05-29 21:04:52 +00005363get_Unpickler(PyObject *self, PyObject *file)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005364{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005365 return (PyObject *)newUnpicklerobject(file);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005366}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005367
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005368
Guido van Rossum60456fd1997-04-09 17:36:32 +00005369static void
Tim Peterscba30e22003-02-01 06:24:36 +00005370Unpickler_dealloc(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005371{
Jeremy Hylton7b5ce7f2003-04-09 21:25:30 +00005372 PyObject_GC_UnTrack((PyObject *)self);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005373 Py_XDECREF(self->readline);
5374 Py_XDECREF(self->read);
5375 Py_XDECREF(self->file);
5376 Py_XDECREF(self->memo);
5377 Py_XDECREF(self->stack);
5378 Py_XDECREF(self->pers_func);
5379 Py_XDECREF(self->arg);
5380 Py_XDECREF(self->last_string);
Jeremy Hyltonfff093f2003-07-11 19:42:49 +00005381 Py_XDECREF(self->find_class);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005382
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005383 if (self->marks) {
5384 free(self->marks);
5385 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005386
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005387 if (self->buf_size) {
5388 free(self->buf);
5389 }
Tim Peters84e87f32001-03-17 04:50:51 +00005390
Christian Heimese93237d2007-12-19 02:37:44 +00005391 Py_TYPE(self)->tp_free((PyObject *)self);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005392}
5393
Jeremy Hylton7b5ce7f2003-04-09 21:25:30 +00005394static int
5395Unpickler_traverse(Unpicklerobject *self, visitproc visit, void *arg)
5396{
Thomas Woutersc6e55062006-04-15 21:47:09 +00005397 Py_VISIT(self->readline);
5398 Py_VISIT(self->read);
5399 Py_VISIT(self->file);
5400 Py_VISIT(self->memo);
5401 Py_VISIT(self->stack);
5402 Py_VISIT(self->pers_func);
5403 Py_VISIT(self->arg);
5404 Py_VISIT(self->last_string);
5405 Py_VISIT(self->find_class);
Jeremy Hylton7b5ce7f2003-04-09 21:25:30 +00005406 return 0;
5407}
5408
5409static int
5410Unpickler_clear(Unpicklerobject *self)
5411{
Thomas Woutersedf17d82006-04-15 17:28:34 +00005412 Py_CLEAR(self->readline);
5413 Py_CLEAR(self->read);
5414 Py_CLEAR(self->file);
5415 Py_CLEAR(self->memo);
5416 Py_CLEAR(self->stack);
5417 Py_CLEAR(self->pers_func);
5418 Py_CLEAR(self->arg);
5419 Py_CLEAR(self->last_string);
5420 Py_CLEAR(self->find_class);
Jeremy Hylton7b5ce7f2003-04-09 21:25:30 +00005421 return 0;
5422}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005423
5424static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00005425Unpickler_getattr(Unpicklerobject *self, char *name)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005426{
5427 if (!strcmp(name, "persistent_load")) {
5428 if (!self->pers_func) {
5429 PyErr_SetString(PyExc_AttributeError, name);
5430 return NULL;
5431 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005432
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005433 Py_INCREF(self->pers_func);
5434 return self->pers_func;
5435 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005436
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005437 if (!strcmp(name, "find_global")) {
5438 if (!self->find_class) {
5439 PyErr_SetString(PyExc_AttributeError, name);
5440 return NULL;
5441 }
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00005442
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005443 Py_INCREF(self->find_class);
5444 return self->find_class;
5445 }
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00005446
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005447 if (!strcmp(name, "memo")) {
5448 if (!self->memo) {
5449 PyErr_SetString(PyExc_AttributeError, name);
5450 return NULL;
5451 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005452
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005453 Py_INCREF(self->memo);
5454 return self->memo;
5455 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005456
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005457 if (!strcmp(name, "UnpicklingError")) {
5458 Py_INCREF(UnpicklingError);
5459 return UnpicklingError;
5460 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005461
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005462 return Py_FindMethod(Unpickler_methods, (PyObject *)self, name);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005463}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005464
Guido van Rossum60456fd1997-04-09 17:36:32 +00005465
5466static int
Tim Peterscba30e22003-02-01 06:24:36 +00005467Unpickler_setattr(Unpicklerobject *self, char *name, PyObject *value)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005468{
Jeremy Hyltonce616e41998-08-13 23:13:52 +00005469
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005470 if (!strcmp(name, "persistent_load")) {
5471 Py_XDECREF(self->pers_func);
5472 self->pers_func = value;
5473 Py_XINCREF(value);
5474 return 0;
5475 }
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00005476
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005477 if (!strcmp(name, "find_global")) {
5478 Py_XDECREF(self->find_class);
5479 self->find_class = value;
5480 Py_XINCREF(value);
5481 return 0;
5482 }
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00005483
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005484 if (! value) {
5485 PyErr_SetString(PyExc_TypeError,
5486 "attribute deletion is not supported");
5487 return -1;
5488 }
Jeremy Hyltonce616e41998-08-13 23:13:52 +00005489
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005490 if (strcmp(name, "memo") == 0) {
5491 if (!PyDict_Check(value)) {
Tim Peterscba30e22003-02-01 06:24:36 +00005492 PyErr_SetString(PyExc_TypeError,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005493 "memo must be a dictionary");
5494 return -1;
5495 }
5496 Py_XDECREF(self->memo);
5497 self->memo = value;
5498 Py_INCREF(value);
5499 return 0;
5500 }
Jeremy Hyltonce616e41998-08-13 23:13:52 +00005501
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005502 PyErr_SetString(PyExc_AttributeError, name);
5503 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005504}
5505
Tim Peters5bd2a792003-02-01 16:45:06 +00005506/* ---------------------------------------------------------------------------
5507 * Module-level functions.
5508 */
Guido van Rossum60456fd1997-04-09 17:36:32 +00005509
Martin v. Löwis544f1192004-07-27 05:22:33 +00005510/* dump(obj, file, protocol=0). */
Guido van Rossum60456fd1997-04-09 17:36:32 +00005511static PyObject *
Martin v. Löwis544f1192004-07-27 05:22:33 +00005512cpm_dump(PyObject *self, PyObject *args, PyObject *kwds)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005513{
Martin v. Löwis15e62742006-02-27 16:46:16 +00005514 static char *kwlist[] = {"obj", "file", "protocol", NULL};
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005515 PyObject *ob, *file, *res = NULL;
5516 Picklerobject *pickler = 0;
Tim Peters5bd2a792003-02-01 16:45:06 +00005517 int proto = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005518
Martin v. Löwis544f1192004-07-27 05:22:33 +00005519 if (!( PyArg_ParseTupleAndKeywords(args, kwds, "OO|i", kwlist,
5520 &ob, &file, &proto)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005521 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005522
Tim Peters5bd2a792003-02-01 16:45:06 +00005523 if (!( pickler = newPicklerobject(file, proto)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005524 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005525
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005526 if (dump(pickler, ob) < 0)
5527 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005528
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005529 Py_INCREF(Py_None);
5530 res = Py_None;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005531
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005532 finally:
5533 Py_XDECREF(pickler);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005534
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005535 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005536}
5537
5538
Martin v. Löwis544f1192004-07-27 05:22:33 +00005539/* dumps(obj, protocol=0). */
Guido van Rossum60456fd1997-04-09 17:36:32 +00005540static PyObject *
Martin v. Löwis544f1192004-07-27 05:22:33 +00005541cpm_dumps(PyObject *self, PyObject *args, PyObject *kwds)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005542{
Martin v. Löwis15e62742006-02-27 16:46:16 +00005543 static char *kwlist[] = {"obj", "protocol", NULL};
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005544 PyObject *ob, *file = 0, *res = NULL;
5545 Picklerobject *pickler = 0;
Tim Peters5bd2a792003-02-01 16:45:06 +00005546 int proto = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005547
Martin v. Löwis544f1192004-07-27 05:22:33 +00005548 if (!( PyArg_ParseTupleAndKeywords(args, kwds, "O|i:dumps", kwlist,
5549 &ob, &proto)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005550 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005551
Tim Peterscba30e22003-02-01 06:24:36 +00005552 if (!( file = PycStringIO->NewOutput(128)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005553 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005554
Tim Peters5bd2a792003-02-01 16:45:06 +00005555 if (!( pickler = newPicklerobject(file, proto)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005556 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005557
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005558 if (dump(pickler, ob) < 0)
5559 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005560
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005561 res = PycStringIO->cgetvalue(file);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005562
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005563 finally:
5564 Py_XDECREF(pickler);
5565 Py_XDECREF(file);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005566
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005567 return res;
Tim Peters84e87f32001-03-17 04:50:51 +00005568}
5569
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005570
Tim Peters5bd2a792003-02-01 16:45:06 +00005571/* load(fileobj). */
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005572static PyObject *
Georg Brandl96a8c392006-05-29 21:04:52 +00005573cpm_load(PyObject *self, PyObject *ob)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005574{
5575 Unpicklerobject *unpickler = 0;
Georg Brandl96a8c392006-05-29 21:04:52 +00005576 PyObject *res = NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005577
Tim Peterscba30e22003-02-01 06:24:36 +00005578 if (!( unpickler = newUnpicklerobject(ob)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005579 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005580
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005581 res = load(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005582
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005583 finally:
5584 Py_XDECREF(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005585
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005586 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005587}
5588
5589
Tim Peters5bd2a792003-02-01 16:45:06 +00005590/* loads(string) */
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005591static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00005592cpm_loads(PyObject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005593{
5594 PyObject *ob, *file = 0, *res = NULL;
5595 Unpicklerobject *unpickler = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005596
Tim Peterscba30e22003-02-01 06:24:36 +00005597 if (!( PyArg_ParseTuple(args, "S:loads", &ob)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005598 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005599
Tim Peterscba30e22003-02-01 06:24:36 +00005600 if (!( file = PycStringIO->NewInput(ob)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005601 goto finally;
Tim Peters84e87f32001-03-17 04:50:51 +00005602
Tim Peterscba30e22003-02-01 06:24:36 +00005603 if (!( unpickler = newUnpicklerobject(file)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005604 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005605
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005606 res = load(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005607
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005608 finally:
5609 Py_XDECREF(file);
5610 Py_XDECREF(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005611
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005612 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005613}
5614
5615
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005616PyDoc_STRVAR(Unpicklertype__doc__,
5617"Objects that know how to unpickle");
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005618
Guido van Rossum9716aaa1997-12-08 15:15:16 +00005619static PyTypeObject Unpicklertype = {
Martin v. Löwis68192102007-07-21 06:55:02 +00005620 PyVarObject_HEAD_INIT(NULL, 0)
Jeremy Hylton7b5ce7f2003-04-09 21:25:30 +00005621 "cPickle.Unpickler", /*tp_name*/
5622 sizeof(Unpicklerobject), /*tp_basicsize*/
5623 0,
5624 (destructor)Unpickler_dealloc, /* tp_dealloc */
5625 0, /* tp_print */
5626 (getattrfunc)Unpickler_getattr, /* tp_getattr */
5627 (setattrfunc)Unpickler_setattr, /* tp_setattr */
5628 0, /* tp_compare */
5629 0, /* tp_repr */
5630 0, /* tp_as_number */
5631 0, /* tp_as_sequence */
5632 0, /* tp_as_mapping */
5633 0, /* tp_hash */
5634 0, /* tp_call */
5635 0, /* tp_str */
5636 0, /* tp_getattro */
5637 0, /* tp_setattro */
5638 0, /* tp_as_buffer */
5639 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
5640 Unpicklertype__doc__, /* tp_doc */
5641 (traverseproc)Unpickler_traverse, /* tp_traverse */
5642 (inquiry)Unpickler_clear, /* tp_clear */
Guido van Rossum9716aaa1997-12-08 15:15:16 +00005643};
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005644
Guido van Rossum60456fd1997-04-09 17:36:32 +00005645static struct PyMethodDef cPickle_methods[] = {
Martin v. Löwis544f1192004-07-27 05:22:33 +00005646 {"dump", (PyCFunction)cpm_dump, METH_VARARGS | METH_KEYWORDS,
5647 PyDoc_STR("dump(obj, file, protocol=0) -- "
Tim Peters5bd2a792003-02-01 16:45:06 +00005648 "Write an object in pickle format to the given file.\n"
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005649 "\n"
Tim Peters5bd2a792003-02-01 16:45:06 +00005650 "See the Pickler docstring for the meaning of optional argument proto.")
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005651 },
Tim Peters5bd2a792003-02-01 16:45:06 +00005652
Martin v. Löwis544f1192004-07-27 05:22:33 +00005653 {"dumps", (PyCFunction)cpm_dumps, METH_VARARGS | METH_KEYWORDS,
5654 PyDoc_STR("dumps(obj, protocol=0) -- "
Tim Peters5bd2a792003-02-01 16:45:06 +00005655 "Return a string containing an object in pickle format.\n"
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005656 "\n"
Tim Peters5bd2a792003-02-01 16:45:06 +00005657 "See the Pickler docstring for the meaning of optional argument proto.")
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005658 },
Tim Peters5bd2a792003-02-01 16:45:06 +00005659
Georg Brandl96a8c392006-05-29 21:04:52 +00005660 {"load", (PyCFunction)cpm_load, METH_O,
Neal Norwitz200788c2002-08-13 22:20:41 +00005661 PyDoc_STR("load(file) -- Load a pickle from the given file")},
Tim Peters5bd2a792003-02-01 16:45:06 +00005662
Neal Norwitzb0493252002-03-31 14:44:22 +00005663 {"loads", (PyCFunction)cpm_loads, METH_VARARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00005664 PyDoc_STR("loads(string) -- Load a pickle from the given string")},
Tim Peters5bd2a792003-02-01 16:45:06 +00005665
Martin v. Löwis544f1192004-07-27 05:22:33 +00005666 {"Pickler", (PyCFunction)get_Pickler, METH_VARARGS | METH_KEYWORDS,
5667 PyDoc_STR("Pickler(file, protocol=0) -- Create a pickler.\n"
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005668 "\n"
Tim Peters5bd2a792003-02-01 16:45:06 +00005669 "This takes a file-like object for writing a pickle data stream.\n"
5670 "The optional proto argument tells the pickler to use the given\n"
5671 "protocol; supported protocols are 0, 1, 2. The default\n"
5672 "protocol is 0, to be backwards compatible. (Protocol 0 is the\n"
5673 "only protocol that can be written to a file opened in text\n"
5674 "mode and read back successfully. When using a protocol higher\n"
5675 "than 0, make sure the file is opened in binary mode, both when\n"
5676 "pickling and unpickling.)\n"
5677 "\n"
5678 "Protocol 1 is more efficient than protocol 0; protocol 2 is\n"
5679 "more efficient than protocol 1.\n"
5680 "\n"
5681 "Specifying a negative protocol version selects the highest\n"
5682 "protocol version supported. The higher the protocol used, the\n"
5683 "more recent the version of Python needed to read the pickle\n"
5684 "produced.\n"
5685 "\n"
5686 "The file parameter must have a write() method that accepts a single\n"
5687 "string argument. It can thus be an open file object, a StringIO\n"
5688 "object, or any other custom object that meets this interface.\n")
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005689 },
Tim Peters5bd2a792003-02-01 16:45:06 +00005690
Georg Brandl96a8c392006-05-29 21:04:52 +00005691 {"Unpickler", (PyCFunction)get_Unpickler, METH_O,
Tim Peters5bd2a792003-02-01 16:45:06 +00005692 PyDoc_STR("Unpickler(file) -- Create an unpickler.")},
5693
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005694 { NULL, NULL }
5695};
5696
Guido van Rossum60456fd1997-04-09 17:36:32 +00005697static int
Tim Peterscba30e22003-02-01 06:24:36 +00005698init_stuff(PyObject *module_dict)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005699{
Alexandre Vassalotti9510e4a2008-05-11 08:25:28 +00005700 PyObject *copyreg, *t, *r;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005701
Gregory P. Smithdd96db62008-06-09 04:58:54 +00005702#define INIT_STR(S) if (!( S ## _str=PyString_InternFromString(#S))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005703
Tim Peters3cfe7542003-05-21 21:29:48 +00005704 if (PyType_Ready(&Unpicklertype) < 0)
5705 return -1;
5706 if (PyType_Ready(&Picklertype) < 0)
5707 return -1;
5708
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005709 INIT_STR(__class__);
5710 INIT_STR(__getinitargs__);
5711 INIT_STR(__dict__);
5712 INIT_STR(__getstate__);
5713 INIT_STR(__setstate__);
5714 INIT_STR(__name__);
5715 INIT_STR(__main__);
5716 INIT_STR(__reduce__);
Guido van Rossumb289b872003-02-19 01:45:13 +00005717 INIT_STR(__reduce_ex__);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005718 INIT_STR(write);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005719 INIT_STR(append);
5720 INIT_STR(read);
5721 INIT_STR(readline);
Alexandre Vassalotti9510e4a2008-05-11 08:25:28 +00005722 INIT_STR(copyreg);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005723 INIT_STR(dispatch_table);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005724
Georg Brandldffbf5f2008-05-20 07:49:57 +00005725 if (!( copyreg = PyImport_ImportModule("copy_reg")))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005726 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005727
Tim Peters1f1b2d22003-02-01 02:16:37 +00005728 /* This is special because we want to use a different
5729 one in restricted mode. */
Alexandre Vassalotti9510e4a2008-05-11 08:25:28 +00005730 dispatch_table = PyObject_GetAttr(copyreg, dispatch_table_str);
Tim Peters5b7da392003-02-04 00:21:07 +00005731 if (!dispatch_table) return -1;
5732
Alexandre Vassalotti9510e4a2008-05-11 08:25:28 +00005733 extension_registry = PyObject_GetAttrString(copyreg,
Guido van Rossumd4b920c2003-02-04 01:54:49 +00005734 "_extension_registry");
Tim Peters5b7da392003-02-04 00:21:07 +00005735 if (!extension_registry) return -1;
5736
Alexandre Vassalotti9510e4a2008-05-11 08:25:28 +00005737 inverted_registry = PyObject_GetAttrString(copyreg,
Guido van Rossumd4b920c2003-02-04 01:54:49 +00005738 "_inverted_registry");
Tim Peters5b7da392003-02-04 00:21:07 +00005739 if (!inverted_registry) return -1;
5740
Alexandre Vassalotti9510e4a2008-05-11 08:25:28 +00005741 extension_cache = PyObject_GetAttrString(copyreg,
Guido van Rossumd4b920c2003-02-04 01:54:49 +00005742 "_extension_cache");
Tim Peters5b7da392003-02-04 00:21:07 +00005743 if (!extension_cache) return -1;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005744
Alexandre Vassalotti9510e4a2008-05-11 08:25:28 +00005745 Py_DECREF(copyreg);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005746
Tim Peters731098b2003-02-04 20:56:09 +00005747 if (!(empty_tuple = PyTuple_New(0)))
5748 return -1;
5749
5750 two_tuple = PyTuple_New(2);
5751 if (two_tuple == NULL)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005752 return -1;
Tim Peters3e667d52003-02-04 21:47:44 +00005753 /* We use this temp container with no regard to refcounts, or to
5754 * keeping containees alive. Exempt from GC, because we don't
5755 * want anything looking at two_tuple() by magic.
5756 */
5757 PyObject_GC_UnTrack(two_tuple);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005758
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005759 /* Ugh */
5760 if (!( t=PyImport_ImportModule("__builtin__"))) return -1;
5761 if (PyDict_SetItemString(module_dict, "__builtins__", t) < 0)
5762 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005763
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005764 if (!( t=PyDict_New())) return -1;
5765 if (!( r=PyRun_String(
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005766 "def __str__(self):\n"
5767 " return self.args and ('%s' % self.args[0]) or '(what)'\n",
5768 Py_file_input,
5769 module_dict, t) )) return -1;
5770 Py_DECREF(r);
Guido van Rossumc03158b1999-06-09 15:23:31 +00005771
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005772 PickleError = PyErr_NewException("cPickle.PickleError", NULL, t);
Tim Peterscba30e22003-02-01 06:24:36 +00005773 if (!PickleError)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005774 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00005775
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005776 Py_DECREF(t);
Guido van Rossumc03158b1999-06-09 15:23:31 +00005777
Tim Peterscba30e22003-02-01 06:24:36 +00005778 PicklingError = PyErr_NewException("cPickle.PicklingError",
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005779 PickleError, NULL);
Tim Peterscba30e22003-02-01 06:24:36 +00005780 if (!PicklingError)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005781 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00005782
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005783 if (!( t=PyDict_New())) return -1;
5784 if (!( r=PyRun_String(
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005785 "def __str__(self):\n"
5786 " a=self.args\n"
5787 " a=a and type(a[0]) or '(what)'\n"
5788 " return 'Cannot pickle %s objects' % a\n"
5789 , Py_file_input,
5790 module_dict, t) )) return -1;
5791 Py_DECREF(r);
Tim Peters84e87f32001-03-17 04:50:51 +00005792
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005793 if (!( UnpickleableError = PyErr_NewException(
Tim Peterscba30e22003-02-01 06:24:36 +00005794 "cPickle.UnpickleableError", PicklingError, t)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005795 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00005796
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005797 Py_DECREF(t);
Guido van Rossumc03158b1999-06-09 15:23:31 +00005798
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005799 if (!( UnpicklingError = PyErr_NewException("cPickle.UnpicklingError",
Tim Peterscba30e22003-02-01 06:24:36 +00005800 PickleError, NULL)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005801 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00005802
Martin v. Löwis658009a2002-09-16 17:26:24 +00005803 if (!( BadPickleGet = PyErr_NewException("cPickle.BadPickleGet",
5804 UnpicklingError, NULL)))
5805 return -1;
Tim Peterscba30e22003-02-01 06:24:36 +00005806
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005807 if (PyDict_SetItemString(module_dict, "PickleError",
5808 PickleError) < 0)
5809 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00005810
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005811 if (PyDict_SetItemString(module_dict, "PicklingError",
5812 PicklingError) < 0)
5813 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00005814
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005815 if (PyDict_SetItemString(module_dict, "UnpicklingError",
5816 UnpicklingError) < 0)
5817 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005818
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005819 if (PyDict_SetItemString(module_dict, "UnpickleableError",
5820 UnpickleableError) < 0)
5821 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005822
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005823 if (PyDict_SetItemString(module_dict, "BadPickleGet",
5824 BadPickleGet) < 0)
5825 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00005826
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005827 PycString_IMPORT;
Guido van Rossum053b8df1998-11-25 16:18:00 +00005828
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005829 return 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005830}
5831
Mark Hammondfe51c6d2002-08-02 02:27:13 +00005832#ifndef PyMODINIT_FUNC /* declarations for DLL import/export */
5833#define PyMODINIT_FUNC void
Guido van Rossumf9ffb031999-02-04 14:54:04 +00005834#endif
Mark Hammondfe51c6d2002-08-02 02:27:13 +00005835PyMODINIT_FUNC
Tim Peterscba30e22003-02-01 06:24:36 +00005836initcPickle(void)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005837{
5838 PyObject *m, *d, *di, *v, *k;
Martin v. Löwis18e16552006-02-15 17:27:45 +00005839 Py_ssize_t i;
Tim Peters5b7da392003-02-04 00:21:07 +00005840 char *rev = "1.71"; /* XXX when does this change? */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005841 PyObject *format_version;
5842 PyObject *compatible_formats;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005843
Alexandre Vassalotti3c4971c2008-05-16 19:14:31 +00005844 /* XXX: Should mention that the pickle module will include the C
5845 XXX: optimized implementation automatically. */
5846 if (PyErr_WarnPy3k("the cPickle module has been removed in "
5847 "Python 3.0", 2) < 0)
5848 return;
5849
Christian Heimese93237d2007-12-19 02:37:44 +00005850 Py_TYPE(&Picklertype) = &PyType_Type;
5851 Py_TYPE(&Unpicklertype) = &PyType_Type;
5852 Py_TYPE(&PdataType) = &PyType_Type;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005853
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005854 /* Initialize some pieces. We need to do this before module creation,
Tim Peters5b7da392003-02-04 00:21:07 +00005855 * so we're forced to use a temporary dictionary. :(
5856 */
5857 di = PyDict_New();
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005858 if (!di) return;
5859 if (init_stuff(di) < 0) return;
Guido van Rossumebba4202000-09-07 14:35:37 +00005860
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005861 /* Create the module and add the functions */
5862 m = Py_InitModule4("cPickle", cPickle_methods,
5863 cPickle_module_documentation,
5864 (PyObject*)NULL,PYTHON_API_VERSION);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00005865 if (m == NULL)
5866 return;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005867
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005868 /* Add some symbolic constants to the module */
5869 d = PyModule_GetDict(m);
Gregory P. Smithdd96db62008-06-09 04:58:54 +00005870 v = PyString_FromString(rev);
Tim Peters5b7da392003-02-04 00:21:07 +00005871 PyDict_SetItemString(d, "__version__", v);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005872 Py_XDECREF(v);
Barry Warsaw93d29b61997-01-14 17:45:08 +00005873
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005874 /* Copy data from di. Waaa. */
5875 for (i=0; PyDict_Next(di, &i, &k, &v); ) {
5876 if (PyObject_SetItem(d, k, v) < 0) {
5877 Py_DECREF(di);
5878 return;
5879 }
5880 }
5881 Py_DECREF(di);
Guido van Rossumebba4202000-09-07 14:35:37 +00005882
Tim Peters8587b3c2003-02-13 15:44:41 +00005883 i = PyModule_AddIntConstant(m, "HIGHEST_PROTOCOL", HIGHEST_PROTOCOL);
5884 if (i < 0)
5885 return;
5886
Tim Peters5b7da392003-02-04 00:21:07 +00005887 /* These are purely informational; no code uses them. */
5888 /* File format version we write. */
Gregory P. Smithdd96db62008-06-09 04:58:54 +00005889 format_version = PyString_FromString("2.0");
Tim Peters5b7da392003-02-04 00:21:07 +00005890 /* Format versions we can read. */
5891 compatible_formats = Py_BuildValue("[sssss]",
5892 "1.0", /* Original protocol 0 */
5893 "1.1", /* Protocol 0 + INST */
5894 "1.2", /* Original protocol 1 */
5895 "1.3", /* Protocol 1 + BINFLOAT */
Tim Peters0dd23aa2003-02-04 00:30:46 +00005896 "2.0"); /* Original protocol 2 */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005897 PyDict_SetItemString(d, "format_version", format_version);
5898 PyDict_SetItemString(d, "compatible_formats", compatible_formats);
5899 Py_XDECREF(format_version);
5900 Py_XDECREF(compatible_formats);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005901}