blob: a0e443ed940e9aa208bd723958d77b7ece610972 [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/*
Martin v. Löwis9ac49272009-01-02 20:40:14 +000021 * Note: The UNICODE macro controls the TCHAR meaning of the win32 API. Since
22 * all headers have already been included here, we can safely redefine it.
23 */
24#ifdef UNICODE
25# undef UNICODE
26#endif
27
28/*
Tim Peters797ec242003-02-01 06:22:36 +000029 * Pickle opcodes. These must be kept in synch with pickle.py. Extensive
30 * docs are in pickletools.py.
31 */
Guido van Rossum60456fd1997-04-09 17:36:32 +000032#define MARK '('
33#define STOP '.'
34#define POP '0'
35#define POP_MARK '1'
36#define DUP '2'
37#define FLOAT 'F'
Guido van Rossum60456fd1997-04-09 17:36:32 +000038#define BINFLOAT 'G'
Guido van Rossum60456fd1997-04-09 17:36:32 +000039#define INT 'I'
40#define BININT 'J'
41#define BININT1 'K'
42#define LONG 'L'
43#define BININT2 'M'
44#define NONE 'N'
45#define PERSID 'P'
46#define BINPERSID 'Q'
47#define REDUCE 'R'
48#define STRING 'S'
49#define BINSTRING 'T'
Guido van Rossum2f4caa41997-01-06 22:59:08 +000050#define SHORT_BINSTRING 'U'
Guido van Rossum5fccb7c2000-03-10 23:11:40 +000051#define UNICODE 'V'
52#define BINUNICODE 'X'
Guido van Rossum60456fd1997-04-09 17:36:32 +000053#define APPEND 'a'
54#define BUILD 'b'
55#define GLOBAL 'c'
56#define DICT 'd'
57#define EMPTY_DICT '}'
58#define APPENDS 'e'
59#define GET 'g'
60#define BINGET 'h'
61#define INST 'i'
62#define LONG_BINGET 'j'
63#define LIST 'l'
64#define EMPTY_LIST ']'
65#define OBJ 'o'
66#define PUT 'p'
67#define BINPUT 'q'
68#define LONG_BINPUT 'r'
69#define SETITEM 's'
70#define TUPLE 't'
71#define EMPTY_TUPLE ')'
72#define SETITEMS 'u'
Tim Peters797ec242003-02-01 06:22:36 +000073
74/* Protocol 2. */
75#define PROTO '\x80' /* identify pickle protocol */
76#define NEWOBJ '\x81' /* build object by applying cls.__new__ to argtuple */
77#define EXT1 '\x82' /* push object from extension registry; 1-byte index */
78#define EXT2 '\x83' /* ditto, but 2-byte index */
79#define EXT4 '\x84' /* ditto, but 4-byte index */
80#define TUPLE1 '\x85' /* build 1-tuple from stack top */
81#define TUPLE2 '\x86' /* build 2-tuple from two topmost stack items */
82#define TUPLE3 '\x87' /* build 3-tuple from three topmost stack items */
83#define NEWTRUE '\x88' /* push True */
84#define NEWFALSE '\x89' /* push False */
85#define LONG1 '\x8a' /* push long from < 256 bytes */
86#define LONG4 '\x8b' /* push really big long */
87
88/* There aren't opcodes -- they're ways to pickle bools before protocol 2,
89 * so that unpicklers written before bools were introduced unpickle them
90 * as ints, but unpicklers after can recognize that bools were intended.
91 * Note that protocol 2 added direct ways to pickle bools.
92 */
Jack Jansen3a967022002-06-26 20:40:42 +000093#undef TRUE
Guido van Rossume2763392002-04-05 19:30:08 +000094#define TRUE "I01\n"
Jack Jansen3a967022002-06-26 20:40:42 +000095#undef FALSE
Guido van Rossume2763392002-04-05 19:30:08 +000096#define FALSE "I00\n"
Guido van Rossum77f6a652002-04-03 22:41:51 +000097
Tim Peters1092d642003-02-11 21:06:20 +000098/* Keep in synch with pickle.Pickler._BATCHSIZE. This is how many elements
Tim Peters42f08ac2003-02-11 22:43:24 +000099 * batch_list/dict() pumps out before doing APPENDS/SETITEMS. Nothing will
100 * break if this gets out of synch with pickle.py, but it's unclear that
101 * would help anything either.
Tim Peters1092d642003-02-11 21:06:20 +0000102 */
103#define BATCHSIZE 1000
104
Guido van Rossum60456fd1997-04-09 17:36:32 +0000105static char MARKv = MARK;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000106
Guido van Rossumc03158b1999-06-09 15:23:31 +0000107static PyObject *PickleError;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000108static PyObject *PicklingError;
Guido van Rossumc03158b1999-06-09 15:23:31 +0000109static PyObject *UnpickleableError;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000110static PyObject *UnpicklingError;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000111static PyObject *BadPickleGet;
112
Tim Peters5b7da392003-02-04 00:21:07 +0000113/* As the name says, an empty tuple. */
Guido van Rossum60456fd1997-04-09 17:36:32 +0000114static PyObject *empty_tuple;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000115
Georg Brandldffbf5f2008-05-20 07:49:57 +0000116/* copy_reg.dispatch_table, {type_object: pickling_function} */
Tim Peters5b7da392003-02-04 00:21:07 +0000117static PyObject *dispatch_table;
118
119/* For EXT[124] opcodes. */
Georg Brandldffbf5f2008-05-20 07:49:57 +0000120/* copy_reg._extension_registry, {(module_name, function_name): code} */
Tim Peters5b7da392003-02-04 00:21:07 +0000121static PyObject *extension_registry;
Georg Brandldffbf5f2008-05-20 07:49:57 +0000122/* copy_reg._inverted_registry, {code: (module_name, function_name)} */
Tim Peters5b7da392003-02-04 00:21:07 +0000123static PyObject *inverted_registry;
Georg Brandldffbf5f2008-05-20 07:49:57 +0000124/* copy_reg._extension_cache, {code: object} */
Tim Peters5b7da392003-02-04 00:21:07 +0000125static PyObject *extension_cache;
126
Georg Brandldffbf5f2008-05-20 07:49:57 +0000127/* For looking up name pairs in copy_reg._extension_registry. */
Tim Peters731098b2003-02-04 20:56:09 +0000128static PyObject *two_tuple;
129
Guido van Rossum60456fd1997-04-09 17:36:32 +0000130static PyObject *__class___str, *__getinitargs___str, *__dict___str,
131 *__getstate___str, *__setstate___str, *__name___str, *__reduce___str,
Guido van Rossumb289b872003-02-19 01:45:13 +0000132 *__reduce_ex___str,
Tim Peters1f1b2d22003-02-01 02:16:37 +0000133 *write_str, *append_str,
Neal Norwitzb183a252006-04-10 01:03:32 +0000134 *read_str, *readline_str, *__main___str,
Alexandre Vassalotti9510e4a2008-05-11 08:25:28 +0000135 *copyreg_str, *dispatch_table_str;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000136
Guido van Rossum053b8df1998-11-25 16:18:00 +0000137/*************************************************************************
138 Internal Data type for pickle data. */
139
140typedef struct {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000141 PyObject_HEAD
Tim Peters1d63c9f2003-02-02 20:29:39 +0000142 int length; /* number of initial slots in data currently used */
143 int size; /* number of slots in data allocated */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000144 PyObject **data;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000145} Pdata;
146
Tim Peters84e87f32001-03-17 04:50:51 +0000147static void
Tim Peterscba30e22003-02-01 06:24:36 +0000148Pdata_dealloc(Pdata *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000149{
150 int i;
151 PyObject **p;
Tim Peterscba30e22003-02-01 06:24:36 +0000152
Tim Peters1d63c9f2003-02-02 20:29:39 +0000153 for (i = self->length, p = self->data; --i >= 0; p++) {
154 Py_DECREF(*p);
155 }
156 if (self->data)
157 free(self->data);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000158 PyObject_Del(self);
Guido van Rossum053b8df1998-11-25 16:18:00 +0000159}
160
161static PyTypeObject PdataType = {
Martin v. Löwis68192102007-07-21 06:55:02 +0000162 PyVarObject_HEAD_INIT(NULL, 0) "cPickle.Pdata", sizeof(Pdata), 0,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000163 (destructor)Pdata_dealloc,
164 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0L,0L,0L,0L, ""
Guido van Rossum053b8df1998-11-25 16:18:00 +0000165};
166
Christian Heimese93237d2007-12-19 02:37:44 +0000167#define Pdata_Check(O) (Py_TYPE(O) == &PdataType)
Guido van Rossum053b8df1998-11-25 16:18:00 +0000168
169static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +0000170Pdata_New(void)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000171{
172 Pdata *self;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000173
Tim Peters1d63c9f2003-02-02 20:29:39 +0000174 if (!(self = PyObject_New(Pdata, &PdataType)))
175 return NULL;
176 self->size = 8;
177 self->length = 0;
178 self->data = malloc(self->size * sizeof(PyObject*));
179 if (self->data)
180 return (PyObject*)self;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000181 Py_DECREF(self);
182 return PyErr_NoMemory();
Guido van Rossum053b8df1998-11-25 16:18:00 +0000183}
184
Tim Peters84e87f32001-03-17 04:50:51 +0000185static int
Tim Peterscba30e22003-02-01 06:24:36 +0000186stackUnderflow(void)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000187{
188 PyErr_SetString(UnpicklingError, "unpickling stack underflow");
189 return -1;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000190}
191
Tim Peters1d63c9f2003-02-02 20:29:39 +0000192/* Retain only the initial clearto items. If clearto >= the current
193 * number of items, this is a (non-erroneous) NOP.
194 */
Guido van Rossum053b8df1998-11-25 16:18:00 +0000195static int
Tim Peterscba30e22003-02-01 06:24:36 +0000196Pdata_clear(Pdata *self, int clearto)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000197{
198 int i;
199 PyObject **p;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000200
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000201 if (clearto < 0) return stackUnderflow();
202 if (clearto >= self->length) return 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000203
Tim Peters1d63c9f2003-02-02 20:29:39 +0000204 for (i = self->length, p = self->data + clearto;
205 --i >= clearto;
206 p++) {
Neal Norwitzb59d08c2006-07-22 16:20:49 +0000207 Py_CLEAR(*p);
Tim Peters1d63c9f2003-02-02 20:29:39 +0000208 }
209 self->length = clearto;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000210
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000211 return 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000212}
213
Tim Peters84e87f32001-03-17 04:50:51 +0000214static int
Tim Peterscba30e22003-02-01 06:24:36 +0000215Pdata_grow(Pdata *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000216{
Tim Peters1d63c9f2003-02-02 20:29:39 +0000217 int bigger;
218 size_t nbytes;
Neal Norwitzb59d08c2006-07-22 16:20:49 +0000219 PyObject **tmp;
Tim Peters1d63c9f2003-02-02 20:29:39 +0000220
Tim Peters1d63c9f2003-02-02 20:29:39 +0000221 bigger = self->size << 1;
Tim Peterse0a39072003-02-03 15:45:56 +0000222 if (bigger <= 0) /* was 0, or new value overflows */
Tim Peters1d63c9f2003-02-02 20:29:39 +0000223 goto nomemory;
224 if ((int)(size_t)bigger != bigger)
225 goto nomemory;
226 nbytes = (size_t)bigger * sizeof(PyObject *);
227 if (nbytes / sizeof(PyObject *) != (size_t)bigger)
228 goto nomemory;
Neal Norwitzb59d08c2006-07-22 16:20:49 +0000229 tmp = realloc(self->data, nbytes);
230 if (tmp == NULL)
Tim Peters1d63c9f2003-02-02 20:29:39 +0000231 goto nomemory;
Neal Norwitzb59d08c2006-07-22 16:20:49 +0000232 self->data = tmp;
Tim Peters1d63c9f2003-02-02 20:29:39 +0000233 self->size = bigger;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000234 return 0;
Tim Peters1d63c9f2003-02-02 20:29:39 +0000235
236 nomemory:
Tim Peters1d63c9f2003-02-02 20:29:39 +0000237 PyErr_NoMemory();
238 return -1;
Tim Peters84e87f32001-03-17 04:50:51 +0000239}
Guido van Rossum053b8df1998-11-25 16:18:00 +0000240
Tim Peterse0a39072003-02-03 15:45:56 +0000241/* D is a Pdata*. Pop the topmost element and store it into V, which
242 * must be an lvalue holding PyObject*. On stack underflow, UnpicklingError
Tim Peters1d63c9f2003-02-02 20:29:39 +0000243 * is raised and V is set to NULL. D and V may be evaluated several times.
244 */
245#define PDATA_POP(D, V) { \
Tim Peterse0a39072003-02-03 15:45:56 +0000246 if ((D)->length) \
247 (V) = (D)->data[--((D)->length)]; \
248 else { \
249 PyErr_SetString(UnpicklingError, "bad pickle data"); \
250 (V) = NULL; \
251 } \
Guido van Rossum053b8df1998-11-25 16:18:00 +0000252}
253
Tim Peterse0a39072003-02-03 15:45:56 +0000254/* PDATA_PUSH and PDATA_APPEND both push rvalue PyObject* O on to Pdata*
255 * D. If the Pdata stack can't be grown to hold the new value, both
256 * raise MemoryError and execute "return ER". The difference is in ownership
257 * of O after: _PUSH transfers ownership of O from the caller to the stack
258 * (no incref of O is done, and in case of error O is decrefed), while
259 * _APPEND pushes a new reference.
260 */
261
262/* Push O on stack D, giving ownership of O to the stack. */
263#define PDATA_PUSH(D, O, ER) { \
264 if (((Pdata*)(D))->length == ((Pdata*)(D))->size && \
265 Pdata_grow((Pdata*)(D)) < 0) { \
266 Py_DECREF(O); \
267 return ER; \
268 } \
269 ((Pdata*)(D))->data[((Pdata*)(D))->length++] = (O); \
270}
271
272/* Push O on stack D, pushing a new reference. */
273#define PDATA_APPEND(D, O, ER) { \
274 if (((Pdata*)(D))->length == ((Pdata*)(D))->size && \
275 Pdata_grow((Pdata*)(D)) < 0) \
276 return ER; \
277 Py_INCREF(O); \
278 ((Pdata*)(D))->data[((Pdata*)(D))->length++] = (O); \
279}
280
281
Guido van Rossum053b8df1998-11-25 16:18:00 +0000282static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +0000283Pdata_popTuple(Pdata *self, int start)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000284{
285 PyObject *r;
286 int i, j, l;
Tim Peterscba30e22003-02-01 06:24:36 +0000287
Tim Peters1d63c9f2003-02-02 20:29:39 +0000288 l = self->length-start;
289 r = PyTuple_New(l);
290 if (r == NULL)
291 return NULL;
292 for (i = start, j = 0 ; j < l; i++, j++)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000293 PyTuple_SET_ITEM(r, j, self->data[i]);
Tim Peterscba30e22003-02-01 06:24:36 +0000294
Tim Peters1d63c9f2003-02-02 20:29:39 +0000295 self->length = start;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000296 return r;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000297}
298
299static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +0000300Pdata_popList(Pdata *self, int start)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000301{
302 PyObject *r;
303 int i, j, l;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000304
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000305 l=self->length-start;
306 if (!( r=PyList_New(l))) return NULL;
307 for (i=start, j=0 ; j < l; i++, j++)
308 PyList_SET_ITEM(r, j, self->data[i]);
Guido van Rossum053b8df1998-11-25 16:18:00 +0000309
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000310 self->length=start;
311 return r;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000312}
313
Guido van Rossum053b8df1998-11-25 16:18:00 +0000314/*************************************************************************/
315
316#define ARG_TUP(self, o) { \
317 if (self->arg || (self->arg=PyTuple_New(1))) { \
318 Py_XDECREF(PyTuple_GET_ITEM(self->arg,0)); \
319 PyTuple_SET_ITEM(self->arg,0,o); \
320 } \
321 else { \
322 Py_DECREF(o); \
323 } \
324}
325
326#define FREE_ARG_TUP(self) { \
Christian Heimese93237d2007-12-19 02:37:44 +0000327 if (Py_REFCNT(self->arg) > 1) { \
Guido van Rossum053b8df1998-11-25 16:18:00 +0000328 Py_DECREF(self->arg); \
329 self->arg=NULL; \
330 } \
331 }
332
Thomas Wouters3b6448f2000-07-22 23:56:07 +0000333typedef struct Picklerobject {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000334 PyObject_HEAD
335 FILE *fp;
336 PyObject *write;
337 PyObject *file;
338 PyObject *memo;
339 PyObject *arg;
340 PyObject *pers_func;
341 PyObject *inst_pers_func;
Tim Peters797ec242003-02-01 06:22:36 +0000342
343 /* pickle protocol number, >= 0 */
344 int proto;
345
346 /* bool, true if proto > 0 */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000347 int bin;
Tim Peters797ec242003-02-01 06:22:36 +0000348
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000349 int fast; /* Fast mode doesn't save in memo, don't use if circ ref */
Martin v. Löwis18e16552006-02-15 17:27:45 +0000350 int (*write_func)(struct Picklerobject *, const char *, Py_ssize_t);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000351 char *write_buf;
352 int buf_size;
353 PyObject *dispatch_table;
354 int fast_container; /* count nested container dumps */
355 PyObject *fast_memo;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000356} Picklerobject;
357
Barry Warsaw52acb492001-12-21 20:04:22 +0000358#ifndef PY_CPICKLE_FAST_LIMIT
359#define PY_CPICKLE_FAST_LIMIT 50
360#endif
Jeremy Hyltona0fb1772001-10-12 04:11:06 +0000361
Jeremy Hylton938ace62002-07-17 16:30:39 +0000362static PyTypeObject Picklertype;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000363
Thomas Wouters3b6448f2000-07-22 23:56:07 +0000364typedef struct Unpicklerobject {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000365 PyObject_HEAD
366 FILE *fp;
367 PyObject *file;
368 PyObject *readline;
369 PyObject *read;
370 PyObject *memo;
371 PyObject *arg;
372 Pdata *stack;
373 PyObject *mark;
374 PyObject *pers_func;
375 PyObject *last_string;
376 int *marks;
377 int num_marks;
378 int marks_size;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000379 Py_ssize_t (*read_func)(struct Unpicklerobject *, char **, Py_ssize_t);
380 Py_ssize_t (*readline_func)(struct Unpicklerobject *, char **);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000381 int buf_size;
382 char *buf;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000383 PyObject *find_class;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000384} Unpicklerobject;
Tim Peters84e87f32001-03-17 04:50:51 +0000385
Jeremy Hylton938ace62002-07-17 16:30:39 +0000386static PyTypeObject Unpicklertype;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000387
Thomas Wouters4789b3a2000-07-24 11:36:47 +0000388/* Forward decls that need the above structs */
389static int save(Picklerobject *, PyObject *, int);
390static int put2(Picklerobject *, PyObject *);
391
Guido van Rossumd385d591997-04-09 17:47:47 +0000392static
Guido van Rossum60456fd1997-04-09 17:36:32 +0000393PyObject *
Thomas Wouters3b6448f2000-07-22 23:56:07 +0000394cPickle_ErrFormat(PyObject *ErrType, char *stringformat, char *format, ...)
395{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000396 va_list va;
397 PyObject *args=0, *retval=0;
398 va_start(va, format);
Tim Peterscba30e22003-02-01 06:24:36 +0000399
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000400 if (format) args = Py_VaBuildValue(format, va);
401 va_end(va);
402 if (format && ! args) return NULL;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000403 if (stringformat && !(retval=PyString_FromString(stringformat)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000404 return NULL;
Tim Peterscba30e22003-02-01 06:24:36 +0000405
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000406 if (retval) {
407 if (args) {
408 PyObject *v;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000409 v=PyString_Format(retval, args);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000410 Py_DECREF(retval);
411 Py_DECREF(args);
412 if (! v) return NULL;
413 retval=v;
414 }
415 }
416 else
417 if (args) retval=args;
418 else {
419 PyErr_SetObject(ErrType,Py_None);
420 return NULL;
421 }
422 PyErr_SetObject(ErrType,retval);
423 Py_DECREF(retval);
424 return NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000425}
426
Tim Peters84e87f32001-03-17 04:50:51 +0000427static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000428write_file(Picklerobject *self, const char *s, Py_ssize_t n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000429{
430 size_t nbyteswritten;
Tim Peters84e87f32001-03-17 04:50:51 +0000431
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000432 if (s == NULL) {
433 return 0;
434 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000435
Martin v. Löwis18e16552006-02-15 17:27:45 +0000436 if (n > INT_MAX) {
437 /* String too large */
438 return -1;
439 }
440
Gregory P. Smithc20adf82008-04-07 06:33:21 +0000441 PyFile_IncUseCount((PyFileObject *)self->file);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000442 Py_BEGIN_ALLOW_THREADS
Jeremy Hylton7fa4bfa2002-07-18 20:58:57 +0000443 nbyteswritten = fwrite(s, sizeof(char), n, self->fp);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000444 Py_END_ALLOW_THREADS
Gregory P. Smithc20adf82008-04-07 06:33:21 +0000445 PyFile_DecUseCount((PyFileObject *)self->file);
Jeremy Hylton7fa4bfa2002-07-18 20:58:57 +0000446 if (nbyteswritten != (size_t)n) {
447 PyErr_SetFromErrno(PyExc_IOError);
448 return -1;
449 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000450
Martin v. Löwis18e16552006-02-15 17:27:45 +0000451 return (int)n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000452}
453
Tim Peters84e87f32001-03-17 04:50:51 +0000454static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000455write_cStringIO(Picklerobject *self, const char *s, Py_ssize_t n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000456{
457 if (s == NULL) {
458 return 0;
459 }
Tim Peterscba30e22003-02-01 06:24:36 +0000460
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000461 if (PycStringIO->cwrite((PyObject *)self->file, s, n) != n) {
462 return -1;
463 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000464
Martin v. Löwis18e16552006-02-15 17:27:45 +0000465 return (int)n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000466}
467
Tim Peters84e87f32001-03-17 04:50:51 +0000468static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000469write_none(Picklerobject *self, const char *s, Py_ssize_t n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000470{
471 if (s == NULL) return 0;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000472 if (n > INT_MAX) return -1;
473 return (int)n;
Guido van Rossum142eeb81997-08-13 03:14:41 +0000474}
475
Tim Peters84e87f32001-03-17 04:50:51 +0000476static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000477write_other(Picklerobject *self, const char *s, Py_ssize_t _n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000478{
479 PyObject *py_str = 0, *junk = 0;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000480 int n;
Tim Peterscba30e22003-02-01 06:24:36 +0000481
Martin v. Löwis18e16552006-02-15 17:27:45 +0000482 if (_n > INT_MAX)
483 return -1;
484 n = (int)_n;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000485 if (s == NULL) {
486 if (!( self->buf_size )) return 0;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000487 py_str = PyString_FromStringAndSize(self->write_buf,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000488 self->buf_size);
Tim Peterscba30e22003-02-01 06:24:36 +0000489 if (!py_str)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000490 return -1;
491 }
492 else {
493 if (self->buf_size && (n + self->buf_size) > WRITE_BUF_SIZE) {
494 if (write_other(self, NULL, 0) < 0)
495 return -1;
496 }
Tim Peterscba30e22003-02-01 06:24:36 +0000497
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000498 if (n > WRITE_BUF_SIZE) {
499 if (!( py_str =
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000500 PyString_FromStringAndSize(s, n)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000501 return -1;
502 }
503 else {
504 memcpy(self->write_buf + self->buf_size, s, n);
505 self->buf_size += n;
506 return n;
507 }
508 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000509
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000510 if (self->write) {
511 /* object with write method */
512 ARG_TUP(self, py_str);
513 if (self->arg) {
514 junk = PyObject_Call(self->write, self->arg, NULL);
515 FREE_ARG_TUP(self);
516 }
517 if (junk) Py_DECREF(junk);
518 else return -1;
519 }
520 else
521 PDATA_PUSH(self->file, py_str, -1);
Guido van Rossum60456fd1997-04-09 17:36:32 +0000522
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000523 self->buf_size = 0;
524 return n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000525}
526
527
Martin v. Löwis18e16552006-02-15 17:27:45 +0000528static Py_ssize_t
529read_file(Unpicklerobject *self, char **s, Py_ssize_t n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000530{
531 size_t nbytesread;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000532
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000533 if (self->buf_size == 0) {
534 int size;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000535
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000536 size = ((n < 32) ? 32 : n);
Tim Petersee1a53c2003-02-02 02:57:53 +0000537 if (!( self->buf = (char *)malloc(size))) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000538 PyErr_NoMemory();
539 return -1;
540 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000541
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000542 self->buf_size = size;
543 }
544 else if (n > self->buf_size) {
Kristján Valur Jónsson17b8e972007-04-25 00:10:50 +0000545 char *newbuf = (char *)realloc(self->buf, n);
546 if (!newbuf) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000547 PyErr_NoMemory();
548 return -1;
549 }
Kristján Valur Jónsson17b8e972007-04-25 00:10:50 +0000550 self->buf = newbuf;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000551 self->buf_size = n;
552 }
Tim Peters84e87f32001-03-17 04:50:51 +0000553
Gregory P. Smithc20adf82008-04-07 06:33:21 +0000554 PyFile_IncUseCount((PyFileObject *)self->file);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000555 Py_BEGIN_ALLOW_THREADS
556 nbytesread = fread(self->buf, sizeof(char), n, self->fp);
557 Py_END_ALLOW_THREADS
Gregory P. Smithc20adf82008-04-07 06:33:21 +0000558 PyFile_DecUseCount((PyFileObject *)self->file);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000559 if (nbytesread != (size_t)n) {
560 if (feof(self->fp)) {
561 PyErr_SetNone(PyExc_EOFError);
562 return -1;
563 }
Tim Peterscba30e22003-02-01 06:24:36 +0000564
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000565 PyErr_SetFromErrno(PyExc_IOError);
566 return -1;
567 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000568
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000569 *s = self->buf;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000570
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000571 return n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000572}
573
574
Martin v. Löwis18e16552006-02-15 17:27:45 +0000575static Py_ssize_t
Tim Peterscba30e22003-02-01 06:24:36 +0000576readline_file(Unpicklerobject *self, char **s)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000577{
578 int i;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000579
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000580 if (self->buf_size == 0) {
Tim Petersee1a53c2003-02-02 02:57:53 +0000581 if (!( self->buf = (char *)malloc(40))) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000582 PyErr_NoMemory();
583 return -1;
584 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000585 self->buf_size = 40;
586 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000587
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000588 i = 0;
589 while (1) {
Tim Petersee1a53c2003-02-02 02:57:53 +0000590 int bigger;
Kristján Valur Jónsson17b8e972007-04-25 00:10:50 +0000591 char *newbuf;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000592 for (; i < (self->buf_size - 1); i++) {
Tim Peterscba30e22003-02-01 06:24:36 +0000593 if (feof(self->fp) ||
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000594 (self->buf[i] = getc(self->fp)) == '\n') {
595 self->buf[i + 1] = '\0';
596 *s = self->buf;
597 return i + 1;
598 }
599 }
Tim Petersee1a53c2003-02-02 02:57:53 +0000600 bigger = self->buf_size << 1;
601 if (bigger <= 0) { /* overflow */
602 PyErr_NoMemory();
603 return -1;
604 }
Kristján Valur Jónsson17b8e972007-04-25 00:10:50 +0000605 newbuf = (char *)realloc(self->buf, bigger);
606 if (!newbuf) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000607 PyErr_NoMemory();
608 return -1;
609 }
Kristján Valur Jónsson17b8e972007-04-25 00:10:50 +0000610 self->buf = newbuf;
Tim Petersee1a53c2003-02-02 02:57:53 +0000611 self->buf_size = bigger;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000612 }
Tim Peters84e87f32001-03-17 04:50:51 +0000613}
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000614
615
Martin v. Löwis18e16552006-02-15 17:27:45 +0000616static Py_ssize_t
617read_cStringIO(Unpicklerobject *self, char **s, Py_ssize_t n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000618{
619 char *ptr;
Tim Peterscba30e22003-02-01 06:24:36 +0000620
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000621 if (PycStringIO->cread((PyObject *)self->file, &ptr, n) != n) {
622 PyErr_SetNone(PyExc_EOFError);
623 return -1;
624 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000625
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000626 *s = ptr;
Tim Peterscba30e22003-02-01 06:24:36 +0000627
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000628 return n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000629}
630
631
Martin v. Löwis18e16552006-02-15 17:27:45 +0000632static Py_ssize_t
Tim Peterscba30e22003-02-01 06:24:36 +0000633readline_cStringIO(Unpicklerobject *self, char **s)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000634{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000635 Py_ssize_t n;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000636 char *ptr;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000637
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000638 if ((n = PycStringIO->creadline((PyObject *)self->file, &ptr)) < 0) {
639 return -1;
640 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000641
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000642 *s = ptr;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000643
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000644 return n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000645}
646
647
Martin v. Löwis18e16552006-02-15 17:27:45 +0000648static Py_ssize_t
649read_other(Unpicklerobject *self, char **s, Py_ssize_t n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000650{
651 PyObject *bytes, *str=0;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000652
Martin v. Löwis18e16552006-02-15 17:27:45 +0000653 if (!( bytes = PyInt_FromSsize_t(n))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000654
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000655 ARG_TUP(self, bytes);
656 if (self->arg) {
657 str = PyObject_Call(self->read, self->arg, NULL);
658 FREE_ARG_TUP(self);
659 }
660 if (! str) return -1;
Tim Peterscba30e22003-02-01 06:24:36 +0000661
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000662 Py_XDECREF(self->last_string);
663 self->last_string = str;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000664
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000665 if (! (*s = PyString_AsString(str))) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000666 return n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000667}
668
669
Martin v. Löwis18e16552006-02-15 17:27:45 +0000670static Py_ssize_t
Tim Peterscba30e22003-02-01 06:24:36 +0000671readline_other(Unpicklerobject *self, char **s)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000672{
673 PyObject *str;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000674 Py_ssize_t str_size;
Tim Peterscba30e22003-02-01 06:24:36 +0000675
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000676 if (!( str = PyObject_CallObject(self->readline, empty_tuple))) {
677 return -1;
678 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000679
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000680 if ((str_size = PyString_Size(str)) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000681 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000682
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000683 Py_XDECREF(self->last_string);
684 self->last_string = str;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000685
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000686 if (! (*s = PyString_AsString(str)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000687 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000688
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000689 return str_size;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000690}
691
Tim Petersee1a53c2003-02-02 02:57:53 +0000692/* Copy the first n bytes from s into newly malloc'ed memory, plus a
693 * trailing 0 byte. Return a pointer to that, or NULL if out of memory.
694 * The caller is responsible for free()'ing the return value.
695 */
Guido van Rossum60456fd1997-04-09 17:36:32 +0000696static char *
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000697pystrndup(const char *s, int n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000698{
Tim Petersee1a53c2003-02-02 02:57:53 +0000699 char *r = (char *)malloc(n+1);
700 if (r == NULL)
701 return (char*)PyErr_NoMemory();
702 memcpy(r, s, n);
703 r[n] = 0;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000704 return r;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000705}
706
707
708static int
Tim Peterscba30e22003-02-01 06:24:36 +0000709get(Picklerobject *self, PyObject *id)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000710{
711 PyObject *value, *mv;
712 long c_value;
713 char s[30];
714 size_t len;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000715
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000716 if (!( mv = PyDict_GetItem(self->memo, id))) {
717 PyErr_SetObject(PyExc_KeyError, id);
718 return -1;
719 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000720
Tim Peterscba30e22003-02-01 06:24:36 +0000721 if (!( value = PyTuple_GetItem(mv, 0)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000722 return -1;
Tim Peters84e87f32001-03-17 04:50:51 +0000723
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000724 if (!( PyInt_Check(value))) {
725 PyErr_SetString(PicklingError, "no int where int expected in memo");
726 return -1;
727 }
728 c_value = PyInt_AS_LONG((PyIntObject*)value);
Guido van Rossum60456fd1997-04-09 17:36:32 +0000729
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000730 if (!self->bin) {
731 s[0] = GET;
732 PyOS_snprintf(s + 1, sizeof(s) - 1, "%ld\n", c_value);
733 len = strlen(s);
734 }
735 else if (Pdata_Check(self->file)) {
736 if (write_other(self, NULL, 0) < 0) return -1;
737 PDATA_APPEND(self->file, mv, -1);
738 return 0;
739 }
740 else {
741 if (c_value < 256) {
742 s[0] = BINGET;
743 s[1] = (int)(c_value & 0xff);
744 len = 2;
745 }
746 else {
747 s[0] = LONG_BINGET;
748 s[1] = (int)(c_value & 0xff);
749 s[2] = (int)((c_value >> 8) & 0xff);
750 s[3] = (int)((c_value >> 16) & 0xff);
751 s[4] = (int)((c_value >> 24) & 0xff);
752 len = 5;
753 }
754 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000755
Tim Peters0bc93f52003-02-02 18:29:33 +0000756 if (self->write_func(self, s, len) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000757 return -1;
Tim Peterscba30e22003-02-01 06:24:36 +0000758
Jeremy Hyltona0fb1772001-10-12 04:11:06 +0000759 return 0;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000760}
Jeremy Hyltonce616e41998-08-13 23:13:52 +0000761
Guido van Rossum60456fd1997-04-09 17:36:32 +0000762
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000763static int
Tim Peterscba30e22003-02-01 06:24:36 +0000764put(Picklerobject *self, PyObject *ob)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000765{
Christian Heimese93237d2007-12-19 02:37:44 +0000766 if (Py_REFCNT(ob) < 2 || self->fast)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000767 return 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000768
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000769 return put2(self, ob);
770}
Guido van Rossum053b8df1998-11-25 16:18:00 +0000771
Guido van Rossum053b8df1998-11-25 16:18:00 +0000772
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000773static int
Tim Peterscba30e22003-02-01 06:24:36 +0000774put2(Picklerobject *self, PyObject *ob)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000775{
776 char c_str[30];
777 int p;
778 size_t len;
779 int res = -1;
780 PyObject *py_ob_id = 0, *memo_len = 0, *t = 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000781
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000782 if (self->fast)
783 return 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000784
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000785 if ((p = PyDict_Size(self->memo)) < 0)
786 goto finally;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000787
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000788 /* Make sure memo keys are positive! */
Tim Peters70b02d72003-02-02 17:26:40 +0000789 /* XXX Why?
790 * XXX And does "positive" really mean non-negative?
791 * XXX pickle.py starts with PUT index 0, not 1. This makes for
792 * XXX gratuitous differences between the pickling modules.
793 */
Tim Peterscba30e22003-02-01 06:24:36 +0000794 p++;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000795
Tim Peterscba30e22003-02-01 06:24:36 +0000796 if (!( py_ob_id = PyLong_FromVoidPtr(ob)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000797 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000798
Tim Peterscba30e22003-02-01 06:24:36 +0000799 if (!( memo_len = PyInt_FromLong(p)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000800 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000801
Tim Peterscba30e22003-02-01 06:24:36 +0000802 if (!( t = PyTuple_New(2)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000803 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000804
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000805 PyTuple_SET_ITEM(t, 0, memo_len);
806 Py_INCREF(memo_len);
807 PyTuple_SET_ITEM(t, 1, ob);
808 Py_INCREF(ob);
809
810 if (PyDict_SetItem(self->memo, py_ob_id, t) < 0)
811 goto finally;
812
813 if (!self->bin) {
814 c_str[0] = PUT;
815 PyOS_snprintf(c_str + 1, sizeof(c_str) - 1, "%d\n", p);
816 len = strlen(c_str);
817 }
818 else if (Pdata_Check(self->file)) {
819 if (write_other(self, NULL, 0) < 0) return -1;
820 PDATA_APPEND(self->file, memo_len, -1);
821 res=0; /* Job well done ;) */
822 goto finally;
823 }
824 else {
825 if (p >= 256) {
826 c_str[0] = LONG_BINPUT;
827 c_str[1] = (int)(p & 0xff);
828 c_str[2] = (int)((p >> 8) & 0xff);
829 c_str[3] = (int)((p >> 16) & 0xff);
830 c_str[4] = (int)((p >> 24) & 0xff);
831 len = 5;
832 }
833 else {
834 c_str[0] = BINPUT;
835 c_str[1] = p;
836 len = 2;
837 }
838 }
839
Tim Peters0bc93f52003-02-02 18:29:33 +0000840 if (self->write_func(self, c_str, len) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000841 goto finally;
842
843 res = 0;
844
845 finally:
846 Py_XDECREF(py_ob_id);
847 Py_XDECREF(memo_len);
848 Py_XDECREF(t);
849
850 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000851}
852
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000853static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +0000854whichmodule(PyObject *global, PyObject *global_name)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000855{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000856 Py_ssize_t i, j;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000857 PyObject *module = 0, *modules_dict = 0,
858 *global_name_attr = 0, *name = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000859
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000860 module = PyObject_GetAttrString(global, "__module__");
Jeremy Hyltonf00368f2003-06-16 20:19:49 +0000861 if (module)
862 return module;
863 if (PyErr_ExceptionMatches(PyExc_AttributeError))
864 PyErr_Clear();
865 else
866 return NULL;
Guido van Rossum45188231997-09-28 05:38:51 +0000867
Tim Peterscba30e22003-02-01 06:24:36 +0000868 if (!( modules_dict = PySys_GetObject("modules")))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000869 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000870
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000871 i = 0;
872 while ((j = PyDict_Next(modules_dict, &i, &name, &module))) {
Guido van Rossum142eeb81997-08-13 03:14:41 +0000873
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000874 if (PyObject_Compare(name, __main___str)==0) continue;
Tim Peters84e87f32001-03-17 04:50:51 +0000875
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000876 global_name_attr = PyObject_GetAttr(module, global_name);
877 if (!global_name_attr) {
Jeremy Hyltonf00368f2003-06-16 20:19:49 +0000878 if (PyErr_ExceptionMatches(PyExc_AttributeError))
879 PyErr_Clear();
880 else
881 return NULL;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000882 continue;
883 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000884
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000885 if (global_name_attr != global) {
886 Py_DECREF(global_name_attr);
887 continue;
888 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000889
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000890 Py_DECREF(global_name_attr);
Guido van Rossum60456fd1997-04-09 17:36:32 +0000891
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000892 break;
893 }
Guido van Rossum142eeb81997-08-13 03:14:41 +0000894
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000895 /* The following implements the rule in pickle.py added in 1.5
896 that used __main__ if no module is found. I don't actually
897 like this rule. jlf
898 */
899 if (!j) {
900 j=1;
901 name=__main___str;
902 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000903
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000904 Py_INCREF(name);
905 return name;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000906}
907
908
Guido van Rossum60456fd1997-04-09 17:36:32 +0000909static int
Jeremy Hylton499ab6a2001-10-15 21:37:58 +0000910fast_save_enter(Picklerobject *self, PyObject *obj)
911{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000912 /* if fast_container < 0, we're doing an error exit. */
913 if (++self->fast_container >= PY_CPICKLE_FAST_LIMIT) {
914 PyObject *key = NULL;
915 if (self->fast_memo == NULL) {
916 self->fast_memo = PyDict_New();
917 if (self->fast_memo == NULL) {
918 self->fast_container = -1;
919 return 0;
920 }
921 }
922 key = PyLong_FromVoidPtr(obj);
923 if (key == NULL)
924 return 0;
925 if (PyDict_GetItem(self->fast_memo, key)) {
Tim Peters4e52ca82002-12-07 02:43:28 +0000926 Py_DECREF(key);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000927 PyErr_Format(PyExc_ValueError,
Tim Peters92c8bb32003-02-13 23:00:26 +0000928 "fast mode: can't pickle cyclic objects "
929 "including object type %s at %p",
Christian Heimese93237d2007-12-19 02:37:44 +0000930 Py_TYPE(obj)->tp_name, obj);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000931 self->fast_container = -1;
932 return 0;
933 }
934 if (PyDict_SetItem(self->fast_memo, key, Py_None) < 0) {
Tim Peters4e52ca82002-12-07 02:43:28 +0000935 Py_DECREF(key);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000936 self->fast_container = -1;
937 return 0;
938 }
Tim Peters4e52ca82002-12-07 02:43:28 +0000939 Py_DECREF(key);
Jeremy Hylton499ab6a2001-10-15 21:37:58 +0000940 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000941 return 1;
Jeremy Hylton499ab6a2001-10-15 21:37:58 +0000942}
943
Tim Peterscba30e22003-02-01 06:24:36 +0000944int
Jeremy Hylton499ab6a2001-10-15 21:37:58 +0000945fast_save_leave(Picklerobject *self, PyObject *obj)
946{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000947 if (self->fast_container-- >= PY_CPICKLE_FAST_LIMIT) {
948 PyObject *key = PyLong_FromVoidPtr(obj);
949 if (key == NULL)
950 return 0;
951 if (PyDict_DelItem(self->fast_memo, key) < 0) {
Tim Peters4e52ca82002-12-07 02:43:28 +0000952 Py_DECREF(key);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000953 return 0;
954 }
Tim Peters4e52ca82002-12-07 02:43:28 +0000955 Py_DECREF(key);
Jeremy Hylton499ab6a2001-10-15 21:37:58 +0000956 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000957 return 1;
Jeremy Hylton499ab6a2001-10-15 21:37:58 +0000958}
959
960static int
Tim Peterscba30e22003-02-01 06:24:36 +0000961save_none(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000962{
963 static char none = NONE;
Tim Peters0bc93f52003-02-02 18:29:33 +0000964 if (self->write_func(self, &none, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000965 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000966
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000967 return 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000968}
969
Guido van Rossum77f6a652002-04-03 22:41:51 +0000970static int
Tim Peterscba30e22003-02-01 06:24:36 +0000971save_bool(Picklerobject *self, PyObject *args)
Guido van Rossum77f6a652002-04-03 22:41:51 +0000972{
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000973 static const char *buf[2] = {FALSE, TRUE};
Guido van Rossume2763392002-04-05 19:30:08 +0000974 static char len[2] = {sizeof(FALSE)-1, sizeof(TRUE)-1};
Guido van Rossum77f6a652002-04-03 22:41:51 +0000975 long l = PyInt_AS_LONG((PyIntObject *)args);
976
Tim Peters3c67d792003-02-02 17:59:11 +0000977 if (self->proto >= 2) {
978 char opcode = l ? NEWTRUE : NEWFALSE;
979 if (self->write_func(self, &opcode, 1) < 0)
980 return -1;
981 }
982 else if (self->write_func(self, buf[l], len[l]) < 0)
Guido van Rossum77f6a652002-04-03 22:41:51 +0000983 return -1;
Guido van Rossum77f6a652002-04-03 22:41:51 +0000984 return 0;
985}
Tim Peters84e87f32001-03-17 04:50:51 +0000986
Guido van Rossum60456fd1997-04-09 17:36:32 +0000987static int
Tim Peterscba30e22003-02-01 06:24:36 +0000988save_int(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000989{
990 char c_str[32];
991 long l = PyInt_AS_LONG((PyIntObject *)args);
992 int len = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000993
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000994 if (!self->bin
Guido van Rossumde8d6d71997-05-13 18:00:44 +0000995#if SIZEOF_LONG > 4
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000996 || l > 0x7fffffffL
997 || l < -0x80000000L
Guido van Rossumde8d6d71997-05-13 18:00:44 +0000998#endif
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000999 ) {
1000 /* Text-mode pickle, or long too big to fit in the 4-byte
1001 * signed BININT format: store as a string.
1002 */
1003 c_str[0] = INT;
1004 PyOS_snprintf(c_str + 1, sizeof(c_str) - 1, "%ld\n", l);
Tim Peters0bc93f52003-02-02 18:29:33 +00001005 if (self->write_func(self, c_str, strlen(c_str)) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001006 return -1;
1007 }
1008 else {
1009 /* Binary pickle and l fits in a signed 4-byte int. */
1010 c_str[1] = (int)( l & 0xff);
1011 c_str[2] = (int)((l >> 8) & 0xff);
1012 c_str[3] = (int)((l >> 16) & 0xff);
1013 c_str[4] = (int)((l >> 24) & 0xff);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001014
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001015 if ((c_str[4] == 0) && (c_str[3] == 0)) {
1016 if (c_str[2] == 0) {
1017 c_str[0] = BININT1;
1018 len = 2;
1019 }
1020 else {
1021 c_str[0] = BININT2;
1022 len = 3;
1023 }
1024 }
1025 else {
1026 c_str[0] = BININT;
1027 len = 5;
1028 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001029
Tim Peters0bc93f52003-02-02 18:29:33 +00001030 if (self->write_func(self, c_str, len) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001031 return -1;
1032 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001033
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001034 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001035}
1036
1037
1038static int
Tim Peterscba30e22003-02-01 06:24:36 +00001039save_long(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001040{
Armin Rigo7ccbca92006-10-04 12:17:45 +00001041 Py_ssize_t size;
Tim Petersee1a53c2003-02-02 02:57:53 +00001042 int res = -1;
1043 PyObject *repr = NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001044
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001045 static char l = LONG;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001046
Tim Petersee1a53c2003-02-02 02:57:53 +00001047 if (self->proto >= 2) {
1048 /* Linear-time pickling. */
1049 size_t nbits;
1050 size_t nbytes;
1051 unsigned char *pdata;
1052 char c_str[5];
1053 int i;
1054 int sign = _PyLong_Sign(args);
1055
1056 if (sign == 0) {
1057 /* It's 0 -- an empty bytestring. */
1058 c_str[0] = LONG1;
1059 c_str[1] = 0;
1060 i = self->write_func(self, c_str, 2);
1061 if (i < 0) goto finally;
1062 res = 0;
1063 goto finally;
1064 }
1065 nbits = _PyLong_NumBits(args);
1066 if (nbits == (size_t)-1 && PyErr_Occurred())
1067 goto finally;
1068 /* How many bytes do we need? There are nbits >> 3 full
1069 * bytes of data, and nbits & 7 leftover bits. If there
1070 * are any leftover bits, then we clearly need another
1071 * byte. Wnat's not so obvious is that we *probably*
1072 * need another byte even if there aren't any leftovers:
1073 * the most-significant bit of the most-significant byte
1074 * acts like a sign bit, and it's usually got a sense
1075 * opposite of the one we need. The exception is longs
1076 * of the form -(2**(8*j-1)) for j > 0. Such a long is
1077 * its own 256's-complement, so has the right sign bit
1078 * even without the extra byte. That's a pain to check
1079 * for in advance, though, so we always grab an extra
1080 * byte at the start, and cut it back later if possible.
1081 */
1082 nbytes = (nbits >> 3) + 1;
Armin Rigo7ccbca92006-10-04 12:17:45 +00001083 if (nbytes > INT_MAX) {
Tim Petersee1a53c2003-02-02 02:57:53 +00001084 PyErr_SetString(PyExc_OverflowError, "long too large "
1085 "to pickle");
1086 goto finally;
1087 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001088 repr = PyString_FromStringAndSize(NULL, (int)nbytes);
Tim Petersee1a53c2003-02-02 02:57:53 +00001089 if (repr == NULL) goto finally;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001090 pdata = (unsigned char *)PyString_AS_STRING(repr);
Tim Petersee1a53c2003-02-02 02:57:53 +00001091 i = _PyLong_AsByteArray((PyLongObject *)args,
1092 pdata, nbytes,
1093 1 /* little endian */, 1 /* signed */);
1094 if (i < 0) goto finally;
1095 /* If the long is negative, this may be a byte more than
1096 * needed. This is so iff the MSB is all redundant sign
1097 * bits.
1098 */
1099 if (sign < 0 && nbytes > 1 && pdata[nbytes - 1] == 0xff &&
1100 (pdata[nbytes - 2] & 0x80) != 0)
1101 --nbytes;
1102
1103 if (nbytes < 256) {
1104 c_str[0] = LONG1;
1105 c_str[1] = (char)nbytes;
1106 size = 2;
1107 }
1108 else {
1109 c_str[0] = LONG4;
1110 size = (int)nbytes;
1111 for (i = 1; i < 5; i++) {
1112 c_str[i] = (char)(size & 0xff);
1113 size >>= 8;
1114 }
1115 size = 5;
1116 }
1117 i = self->write_func(self, c_str, size);
1118 if (i < 0) goto finally;
1119 i = self->write_func(self, (char *)pdata, (int)nbytes);
1120 if (i < 0) goto finally;
1121 res = 0;
1122 goto finally;
1123 }
1124
1125 /* proto < 2: write the repr and newline. This is quadratic-time
1126 * (in the number of digits), in both directions.
1127 */
Tim Peterscba30e22003-02-01 06:24:36 +00001128 if (!( repr = PyObject_Repr(args)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001129 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001130
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001131 if ((size = PyString_Size(repr)) < 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, &l, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001135 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001136
Tim Peters0bc93f52003-02-02 18:29:33 +00001137 if (self->write_func(self,
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001138 PyString_AS_STRING((PyStringObject *)repr),
Tim Peters0bc93f52003-02-02 18:29:33 +00001139 size) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001140 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001141
Tim Peters0bc93f52003-02-02 18:29:33 +00001142 if (self->write_func(self, "\n", 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001143 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001144
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001145 res = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001146
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001147 finally:
1148 Py_XDECREF(repr);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001149 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001150}
1151
1152
1153static int
Tim Peterscba30e22003-02-01 06:24:36 +00001154save_float(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001155{
1156 double x = PyFloat_AS_DOUBLE((PyFloatObject *)args);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001157
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001158 if (self->bin) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001159 char str[9];
Tim Peters9905b942003-03-20 20:53:32 +00001160 str[0] = BINFLOAT;
1161 if (_PyFloat_Pack8(x, (unsigned char *)&str[1], 0) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001162 return -1;
Tim Peters0bc93f52003-02-02 18:29:33 +00001163 if (self->write_func(self, str, 9) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001164 return -1;
1165 }
1166 else {
1167 char c_str[250];
1168 c_str[0] = FLOAT;
Eric Smith068f0652009-04-25 21:40:15 +00001169 _PyOS_double_to_string(c_str + 1, sizeof(c_str) - 2, x, 'g',
1170 17, 0, NULL);
Georg Brandlde9b6242006-04-30 11:13:56 +00001171 /* Extend the formatted string with a newline character */
1172 strcat(c_str, "\n");
Guido van Rossum60456fd1997-04-09 17:36:32 +00001173
Tim Peters0bc93f52003-02-02 18:29:33 +00001174 if (self->write_func(self, c_str, strlen(c_str)) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001175 return -1;
1176 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001177
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001178 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001179}
1180
1181
1182static int
Tim Peterscba30e22003-02-01 06:24:36 +00001183save_string(Picklerobject *self, PyObject *args, int doput)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001184{
1185 int size, len;
1186 PyObject *repr=0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001187
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001188 if ((size = PyString_Size(args)) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001189 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001190
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001191 if (!self->bin) {
1192 char *repr_str;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001193
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001194 static char string = STRING;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001195
Tim Peterscba30e22003-02-01 06:24:36 +00001196 if (!( repr = PyObject_Repr(args)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001197 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001198
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001199 if ((len = PyString_Size(repr)) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001200 goto err;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001201 repr_str = PyString_AS_STRING((PyStringObject *)repr);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001202
Tim Peters0bc93f52003-02-02 18:29:33 +00001203 if (self->write_func(self, &string, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001204 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001205
Tim Peters0bc93f52003-02-02 18:29:33 +00001206 if (self->write_func(self, repr_str, len) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001207 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001208
Tim Peters0bc93f52003-02-02 18:29:33 +00001209 if (self->write_func(self, "\n", 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001210 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001211
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001212 Py_XDECREF(repr);
1213 }
1214 else {
1215 int i;
1216 char c_str[5];
Guido van Rossum60456fd1997-04-09 17:36:32 +00001217
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001218 if ((size = PyString_Size(args)) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001219 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001220
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001221 if (size < 256) {
1222 c_str[0] = SHORT_BINSTRING;
1223 c_str[1] = size;
1224 len = 2;
1225 }
Armin Rigo7ccbca92006-10-04 12:17:45 +00001226 else if (size <= INT_MAX) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001227 c_str[0] = BINSTRING;
1228 for (i = 1; i < 5; i++)
1229 c_str[i] = (int)(size >> ((i - 1) * 8));
1230 len = 5;
1231 }
Armin Rigo7ccbca92006-10-04 12:17:45 +00001232 else
1233 return -1; /* string too large */
Guido van Rossum60456fd1997-04-09 17:36:32 +00001234
Tim Peters0bc93f52003-02-02 18:29:33 +00001235 if (self->write_func(self, c_str, len) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001236 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001237
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001238 if (size > 128 && Pdata_Check(self->file)) {
1239 if (write_other(self, NULL, 0) < 0) return -1;
1240 PDATA_APPEND(self->file, args, -1);
1241 }
1242 else {
Tim Peters0bc93f52003-02-02 18:29:33 +00001243 if (self->write_func(self,
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001244 PyString_AS_STRING(
1245 (PyStringObject *)args),
Tim Peters0bc93f52003-02-02 18:29:33 +00001246 size) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001247 return -1;
1248 }
1249 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001250
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001251 if (doput)
1252 if (put(self, args) < 0)
1253 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001254
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001255 return 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +00001256
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001257 err:
1258 Py_XDECREF(repr);
1259 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001260}
1261
1262
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001263#ifdef Py_USING_UNICODE
Guido van Rossumfb10c3f2000-12-19 02:08:38 +00001264/* A copy of PyUnicode_EncodeRawUnicodeEscape() that also translates
1265 backslash and newline characters to \uXXXX escapes. */
1266static PyObject *
Alexandre Vassalottif852bf92008-12-27 07:08:47 +00001267modified_EncodeRawUnicodeEscape(const Py_UNICODE *s, Py_ssize_t size)
Guido van Rossumfb10c3f2000-12-19 02:08:38 +00001268{
Alexandre Vassalottif852bf92008-12-27 07:08:47 +00001269 PyObject *repr;
1270 char *p;
1271 char *q;
Guido van Rossumfb10c3f2000-12-19 02:08:38 +00001272
Alexandre Vassalottif852bf92008-12-27 07:08:47 +00001273 static const char *hexdigit = "0123456789abcdef";
1274#ifdef Py_UNICODE_WIDE
1275 const Py_ssize_t expandsize = 10;
1276#else
1277 const Py_ssize_t expandsize = 6;
1278#endif
Guido van Rossumfb10c3f2000-12-19 02:08:38 +00001279
Alexandre Vassalottif852bf92008-12-27 07:08:47 +00001280 if (size > PY_SSIZE_T_MAX / expandsize)
1281 return PyErr_NoMemory();
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001282
Alexandre Vassalottif852bf92008-12-27 07:08:47 +00001283 repr = PyString_FromStringAndSize(NULL, expandsize * size);
1284 if (repr == NULL)
1285 return NULL;
1286 if (size == 0)
Guido van Rossumfb10c3f2000-12-19 02:08:38 +00001287 return repr;
Guido van Rossumfb10c3f2000-12-19 02:08:38 +00001288
Alexandre Vassalottif852bf92008-12-27 07:08:47 +00001289 p = q = PyString_AS_STRING(repr);
1290 while (size-- > 0) {
1291 Py_UNICODE ch = *s++;
1292#ifdef Py_UNICODE_WIDE
1293 /* Map 32-bit characters to '\Uxxxxxxxx' */
1294 if (ch >= 0x10000) {
1295 *p++ = '\\';
1296 *p++ = 'U';
1297 *p++ = hexdigit[(ch >> 28) & 0xf];
1298 *p++ = hexdigit[(ch >> 24) & 0xf];
1299 *p++ = hexdigit[(ch >> 20) & 0xf];
1300 *p++ = hexdigit[(ch >> 16) & 0xf];
1301 *p++ = hexdigit[(ch >> 12) & 0xf];
1302 *p++ = hexdigit[(ch >> 8) & 0xf];
1303 *p++ = hexdigit[(ch >> 4) & 0xf];
1304 *p++ = hexdigit[ch & 15];
1305 }
1306 else
1307#else
1308 /* Map UTF-16 surrogate pairs to '\U00xxxxxx' */
1309 if (ch >= 0xD800 && ch < 0xDC00) {
1310 Py_UNICODE ch2;
1311 Py_UCS4 ucs;
1312
1313 ch2 = *s++;
1314 size--;
1315 if (ch2 >= 0xDC00 && ch2 <= 0xDFFF) {
1316 ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) + 0x00010000;
1317 *p++ = '\\';
1318 *p++ = 'U';
1319 *p++ = hexdigit[(ucs >> 28) & 0xf];
1320 *p++ = hexdigit[(ucs >> 24) & 0xf];
1321 *p++ = hexdigit[(ucs >> 20) & 0xf];
1322 *p++ = hexdigit[(ucs >> 16) & 0xf];
1323 *p++ = hexdigit[(ucs >> 12) & 0xf];
1324 *p++ = hexdigit[(ucs >> 8) & 0xf];
1325 *p++ = hexdigit[(ucs >> 4) & 0xf];
1326 *p++ = hexdigit[ucs & 0xf];
1327 continue;
1328 }
1329 /* Fall through: isolated surrogates are copied as-is */
1330 s--;
1331 size++;
1332 }
1333#endif
1334 /* Map 16-bit characters to '\uxxxx' */
1335 if (ch >= 256 || ch == '\\' || ch == '\n') {
1336 *p++ = '\\';
1337 *p++ = 'u';
1338 *p++ = hexdigit[(ch >> 12) & 0xf];
1339 *p++ = hexdigit[(ch >> 8) & 0xf];
1340 *p++ = hexdigit[(ch >> 4) & 0xf];
1341 *p++ = hexdigit[ch & 15];
1342 }
1343 /* Copy everything else as-is */
1344 else
1345 *p++ = (char) ch;
1346 }
1347 *p = '\0';
1348 _PyString_Resize(&repr, p - q);
1349 return repr;
1350}
Guido van Rossumfb10c3f2000-12-19 02:08:38 +00001351
Guido van Rossum60456fd1997-04-09 17:36:32 +00001352static int
Tim Peterscba30e22003-02-01 06:24:36 +00001353save_unicode(Picklerobject *self, PyObject *args, int doput)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001354{
Armin Rigo7ccbca92006-10-04 12:17:45 +00001355 Py_ssize_t size, len;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001356 PyObject *repr=0;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001357
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001358 if (!PyUnicode_Check(args))
1359 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001360
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001361 if (!self->bin) {
1362 char *repr_str;
1363 static char string = UNICODE;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001364
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001365 repr = modified_EncodeRawUnicodeEscape(
1366 PyUnicode_AS_UNICODE(args), PyUnicode_GET_SIZE(args));
Tim Peterscba30e22003-02-01 06:24:36 +00001367 if (!repr)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001368 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001369
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001370 if ((len = PyString_Size(repr)) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001371 goto err;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001372 repr_str = PyString_AS_STRING((PyStringObject *)repr);
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001373
Tim Peters0bc93f52003-02-02 18:29:33 +00001374 if (self->write_func(self, &string, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001375 goto err;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001376
Tim Peters0bc93f52003-02-02 18:29:33 +00001377 if (self->write_func(self, repr_str, len) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001378 goto err;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001379
Tim Peters0bc93f52003-02-02 18:29:33 +00001380 if (self->write_func(self, "\n", 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001381 goto err;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001382
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001383 Py_XDECREF(repr);
1384 }
1385 else {
1386 int i;
1387 char c_str[5];
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001388
Tim Peterscba30e22003-02-01 06:24:36 +00001389 if (!( repr = PyUnicode_AsUTF8String(args)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001390 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001391
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001392 if ((size = PyString_Size(repr)) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001393 goto err;
Armin Rigo7ccbca92006-10-04 12:17:45 +00001394 if (size > INT_MAX)
1395 return -1; /* string too large */
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001396
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001397 c_str[0] = BINUNICODE;
1398 for (i = 1; i < 5; i++)
1399 c_str[i] = (int)(size >> ((i - 1) * 8));
1400 len = 5;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001401
Tim Peters0bc93f52003-02-02 18:29:33 +00001402 if (self->write_func(self, c_str, len) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001403 goto err;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001404
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001405 if (size > 128 && Pdata_Check(self->file)) {
1406 if (write_other(self, NULL, 0) < 0)
1407 goto err;
1408 PDATA_APPEND(self->file, repr, -1);
1409 }
1410 else {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001411 if (self->write_func(self, PyString_AS_STRING(repr),
Tim Peters0bc93f52003-02-02 18:29:33 +00001412 size) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001413 goto err;
1414 }
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001415
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001416 Py_DECREF(repr);
1417 }
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001418
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001419 if (doput)
1420 if (put(self, args) < 0)
1421 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001422
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001423 return 0;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001424
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001425 err:
1426 Py_XDECREF(repr);
1427 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001428}
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001429#endif
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001430
Tim Peters1d63c9f2003-02-02 20:29:39 +00001431/* A helper for save_tuple. Push the len elements in tuple t on the stack. */
1432static int
Tim Peters67920142003-02-05 03:46:17 +00001433store_tuple_elements(Picklerobject *self, PyObject *t, int len)
Tim Peters1d63c9f2003-02-02 20:29:39 +00001434{
1435 int i;
1436 int res = -1; /* guilty until proved innocent */
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001437
Tim Peters1d63c9f2003-02-02 20:29:39 +00001438 assert(PyTuple_Size(t) == len);
1439
1440 for (i = 0; i < len; i++) {
1441 PyObject *element = PyTuple_GET_ITEM(t, i);
1442
1443 if (element == NULL)
1444 goto finally;
1445 if (save(self, element, 0) < 0)
1446 goto finally;
1447 }
1448 res = 0;
1449
1450 finally:
1451 return res;
1452}
1453
1454/* Tuples are ubiquitous in the pickle protocols, so many techniques are
1455 * used across protocols to minimize the space needed to pickle them.
Tim Peters67920142003-02-05 03:46:17 +00001456 * Tuples are also the only builtin immutable type that can be recursive
Tim Peters1d63c9f2003-02-02 20:29:39 +00001457 * (a tuple can be reached from itself), and that requires some subtle
1458 * magic so that it works in all cases. IOW, this is a long routine.
1459 */
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001460static int
Tim Peterscba30e22003-02-01 06:24:36 +00001461save_tuple(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001462{
Tim Peters1d63c9f2003-02-02 20:29:39 +00001463 PyObject *py_tuple_id = NULL;
1464 int len, i;
1465 int res = -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001466
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001467 static char tuple = TUPLE;
Tim Peters1d63c9f2003-02-02 20:29:39 +00001468 static char pop = POP;
1469 static char pop_mark = POP_MARK;
1470 static char len2opcode[] = {EMPTY_TUPLE, TUPLE1, TUPLE2, TUPLE3};
Guido van Rossum60456fd1997-04-09 17:36:32 +00001471
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001472 if ((len = PyTuple_Size(args)) < 0)
1473 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001474
Tim Peters1d63c9f2003-02-02 20:29:39 +00001475 if (len == 0) {
1476 char c_str[2];
Tim Peters84e87f32001-03-17 04:50:51 +00001477
Tim Peters1d63c9f2003-02-02 20:29:39 +00001478 if (self->proto) {
1479 c_str[0] = EMPTY_TUPLE;
1480 len = 1;
1481 }
1482 else {
1483 c_str[0] = MARK;
1484 c_str[1] = TUPLE;
1485 len = 2;
1486 }
1487 if (self->write_func(self, c_str, len) >= 0)
1488 res = 0;
1489 /* Don't memoize an empty tuple. */
1490 goto finally;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001491 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001492
Tim Peters1d63c9f2003-02-02 20:29:39 +00001493 /* A non-empty tuple. */
1494
1495 /* id(tuple) isn't in the memo now. If it shows up there after
1496 * saving the tuple elements, the tuple must be recursive, in
1497 * which case we'll pop everything we put on the stack, and fetch
1498 * its value from the memo.
1499 */
1500 py_tuple_id = PyLong_FromVoidPtr(args);
1501 if (py_tuple_id == NULL)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001502 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001503
Tim Peters1d63c9f2003-02-02 20:29:39 +00001504 if (len <= 3 && self->proto >= 2) {
1505 /* Use TUPLE{1,2,3} opcodes. */
Tim Peters67920142003-02-05 03:46:17 +00001506 if (store_tuple_elements(self, args, len) < 0)
Tim Peters1d63c9f2003-02-02 20:29:39 +00001507 goto finally;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001508 if (PyDict_GetItem(self->memo, py_tuple_id)) {
Tim Peters1d63c9f2003-02-02 20:29:39 +00001509 /* pop the len elements */
1510 for (i = 0; i < len; ++i)
1511 if (self->write_func(self, &pop, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001512 goto finally;
Tim Peters1d63c9f2003-02-02 20:29:39 +00001513 /* fetch from memo */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001514 if (get(self, py_tuple_id) < 0)
1515 goto finally;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001516 res = 0;
1517 goto finally;
1518 }
Tim Peters1d63c9f2003-02-02 20:29:39 +00001519 /* Not recursive. */
1520 if (self->write_func(self, len2opcode + len, 1) < 0)
1521 goto finally;
1522 goto memoize;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001523 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001524
Tim Peters1d63c9f2003-02-02 20:29:39 +00001525 /* proto < 2 and len > 0, or proto >= 2 and len > 3.
1526 * Generate MARK elt1 elt2 ... TUPLE
1527 */
1528 if (self->write_func(self, &MARKv, 1) < 0)
1529 goto finally;
1530
Tim Peters67920142003-02-05 03:46:17 +00001531 if (store_tuple_elements(self, args, len) < 0)
Tim Peters1d63c9f2003-02-02 20:29:39 +00001532 goto finally;
1533
1534 if (PyDict_GetItem(self->memo, py_tuple_id)) {
1535 /* pop the stack stuff we pushed */
1536 if (self->bin) {
1537 if (self->write_func(self, &pop_mark, 1) < 0)
1538 goto finally;
1539 }
1540 else {
1541 /* Note that we pop one more than len, to remove
1542 * the MARK too.
1543 */
1544 for (i = 0; i <= len; i++)
1545 if (self->write_func(self, &pop, 1) < 0)
1546 goto finally;
1547 }
1548 /* fetch from memo */
1549 if (get(self, py_tuple_id) >= 0)
1550 res = 0;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001551 goto finally;
1552 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001553
Tim Peters1d63c9f2003-02-02 20:29:39 +00001554 /* Not recursive. */
1555 if (self->write_func(self, &tuple, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001556 goto finally;
Tim Peters84e87f32001-03-17 04:50:51 +00001557
Tim Peters1d63c9f2003-02-02 20:29:39 +00001558 memoize:
1559 if (put(self, args) >= 0)
1560 res = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001561
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001562 finally:
1563 Py_XDECREF(py_tuple_id);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001564 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001565}
1566
Tim Peters1092d642003-02-11 21:06:20 +00001567/* iter is an iterator giving items, and we batch up chunks of
1568 * MARK item item ... item APPENDS
1569 * opcode sequences. Calling code should have arranged to first create an
1570 * empty list, or list-like object, for the APPENDS to operate on.
1571 * Returns 0 on success, <0 on error.
1572 */
1573static int
1574batch_list(Picklerobject *self, PyObject *iter)
1575{
Amaury Forgeot d'Arc24cb3822008-09-11 20:56:13 +00001576 PyObject *obj = NULL;
1577 PyObject *firstitem = NULL;
Tim Peters1092d642003-02-11 21:06:20 +00001578 int i, n;
1579
1580 static char append = APPEND;
1581 static char appends = APPENDS;
1582
1583 assert(iter != NULL);
1584
1585 if (self->proto == 0) {
1586 /* APPENDS isn't available; do one at a time. */
1587 for (;;) {
1588 obj = PyIter_Next(iter);
1589 if (obj == NULL) {
1590 if (PyErr_Occurred())
1591 return -1;
1592 break;
1593 }
1594 i = save(self, obj, 0);
1595 Py_DECREF(obj);
1596 if (i < 0)
1597 return -1;
1598 if (self->write_func(self, &append, 1) < 0)
1599 return -1;
Tim Peters1092d642003-02-11 21:06:20 +00001600 }
1601 return 0;
1602 }
1603
1604 /* proto > 0: write in batches of BATCHSIZE. */
1605 do {
Amaury Forgeot d'Arc24cb3822008-09-11 20:56:13 +00001606 /* Get first item */
1607 firstitem = PyIter_Next(iter);
1608 if (firstitem == NULL) {
1609 if (PyErr_Occurred())
1610 goto BatchFailed;
1611
1612 /* nothing more to add */
1613 break;
1614 }
1615
1616 /* Try to get a second item */
1617 obj = PyIter_Next(iter);
1618 if (obj == NULL) {
1619 if (PyErr_Occurred())
1620 goto BatchFailed;
1621
1622 /* Only one item to write */
1623 if (save(self, firstitem, 0) < 0)
1624 goto BatchFailed;
1625 if (self->write_func(self, &append, 1) < 0)
1626 goto BatchFailed;
1627 Py_CLEAR(firstitem);
1628 break;
1629 }
1630
1631 /* More than one item to write */
1632
1633 /* Pump out MARK, items, APPENDS. */
1634 if (self->write_func(self, &MARKv, 1) < 0)
1635 goto BatchFailed;
1636
1637 if (save(self, firstitem, 0) < 0)
1638 goto BatchFailed;
1639 Py_CLEAR(firstitem);
1640 n = 1;
1641
1642 /* Fetch and save up to BATCHSIZE items */
1643 while (obj) {
1644 if (save(self, obj, 0) < 0)
1645 goto BatchFailed;
1646 Py_CLEAR(obj);
1647 n += 1;
1648
1649 if (n == BATCHSIZE)
1650 break;
1651
Tim Peters1092d642003-02-11 21:06:20 +00001652 obj = PyIter_Next(iter);
1653 if (obj == NULL) {
1654 if (PyErr_Occurred())
1655 goto BatchFailed;
1656 break;
1657 }
Tim Peters1092d642003-02-11 21:06:20 +00001658 }
1659
Amaury Forgeot d'Arc24cb3822008-09-11 20:56:13 +00001660 if (self->write_func(self, &appends, 1) < 0)
1661 goto BatchFailed;
Tim Peters1092d642003-02-11 21:06:20 +00001662
Tim Peters90975f12003-02-12 05:28:58 +00001663 } while (n == BATCHSIZE);
Tim Peters1092d642003-02-11 21:06:20 +00001664 return 0;
1665
1666BatchFailed:
Amaury Forgeot d'Arc24cb3822008-09-11 20:56:13 +00001667 Py_XDECREF(firstitem);
1668 Py_XDECREF(obj);
Tim Peters1092d642003-02-11 21:06:20 +00001669 return -1;
1670}
1671
Guido van Rossum60456fd1997-04-09 17:36:32 +00001672static int
Tim Peterscba30e22003-02-01 06:24:36 +00001673save_list(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001674{
Tim Peters1092d642003-02-11 21:06:20 +00001675 int res = -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001676 char s[3];
Tim Peters1092d642003-02-11 21:06:20 +00001677 int len;
1678 PyObject *iter;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001679
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001680 if (self->fast && !fast_save_enter(self, args))
1681 goto finally;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00001682
Tim Peters1092d642003-02-11 21:06:20 +00001683 /* Create an empty list. */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001684 if (self->bin) {
1685 s[0] = EMPTY_LIST;
Tim Peters1092d642003-02-11 21:06:20 +00001686 len = 1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001687 }
1688 else {
1689 s[0] = MARK;
1690 s[1] = LIST;
Tim Peters1092d642003-02-11 21:06:20 +00001691 len = 2;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001692 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001693
Tim Peters1092d642003-02-11 21:06:20 +00001694 if (self->write_func(self, s, len) < 0)
1695 goto finally;
1696
1697 /* Get list length, and bow out early if empty. */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001698 if ((len = PyList_Size(args)) < 0)
1699 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001700
Tim Peters1092d642003-02-11 21:06:20 +00001701 /* Memoize. */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001702 if (len == 0) {
Tim Peters90975f12003-02-12 05:28:58 +00001703 if (put(self, args) >= 0)
1704 res = 0;
1705 goto finally;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001706 }
Tim Peters90975f12003-02-12 05:28:58 +00001707 if (put2(self, args) < 0)
1708 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001709
Tim Peters1092d642003-02-11 21:06:20 +00001710 /* Materialize the list elements. */
1711 iter = PyObject_GetIter(args);
1712 if (iter == NULL)
1713 goto finally;
Facundo Batista763d3092008-06-30 01:10:55 +00001714
1715 if (Py_EnterRecursiveCall(" while pickling an object") == 0)
1716 {
1717 res = batch_list(self, iter);
1718 Py_LeaveRecursiveCall();
1719 }
Tim Peters1092d642003-02-11 21:06:20 +00001720 Py_DECREF(iter);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001721
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001722 finally:
1723 if (self->fast && !fast_save_leave(self, args))
1724 res = -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001725
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001726 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001727}
1728
1729
Tim Peters42f08ac2003-02-11 22:43:24 +00001730/* iter is an iterator giving (key, value) pairs, and we batch up chunks of
1731 * MARK key value ... key value SETITEMS
1732 * opcode sequences. Calling code should have arranged to first create an
1733 * empty dict, or dict-like object, for the SETITEMS to operate on.
1734 * Returns 0 on success, <0 on error.
1735 *
1736 * This is very much like batch_list(). The difference between saving
1737 * elements directly, and picking apart two-tuples, is so long-winded at
1738 * the C level, though, that attempts to combine these routines were too
1739 * ugly to bear.
1740 */
1741static int
1742batch_dict(Picklerobject *self, PyObject *iter)
1743{
Amaury Forgeot d'Arc24cb3822008-09-11 20:56:13 +00001744 PyObject *p = NULL;
1745 PyObject *firstitem = NULL;
Tim Peters42f08ac2003-02-11 22:43:24 +00001746 int i, n;
1747
1748 static char setitem = SETITEM;
1749 static char setitems = SETITEMS;
1750
1751 assert(iter != NULL);
1752
1753 if (self->proto == 0) {
1754 /* SETITEMS isn't available; do one at a time. */
1755 for (;;) {
1756 p = PyIter_Next(iter);
1757 if (p == NULL) {
1758 if (PyErr_Occurred())
1759 return -1;
1760 break;
1761 }
1762 if (!PyTuple_Check(p) || PyTuple_Size(p) != 2) {
1763 PyErr_SetString(PyExc_TypeError, "dict items "
1764 "iterator must return 2-tuples");
1765 return -1;
1766 }
1767 i = save(self, PyTuple_GET_ITEM(p, 0), 0);
1768 if (i >= 0)
1769 i = save(self, PyTuple_GET_ITEM(p, 1), 0);
1770 Py_DECREF(p);
1771 if (i < 0)
1772 return -1;
1773 if (self->write_func(self, &setitem, 1) < 0)
1774 return -1;
Tim Peters42f08ac2003-02-11 22:43:24 +00001775 }
1776 return 0;
1777 }
1778
1779 /* proto > 0: write in batches of BATCHSIZE. */
1780 do {
Amaury Forgeot d'Arc24cb3822008-09-11 20:56:13 +00001781 /* Get first item */
1782 firstitem = PyIter_Next(iter);
1783 if (firstitem == NULL) {
1784 if (PyErr_Occurred())
1785 goto BatchFailed;
1786
1787 /* nothing more to add */
1788 break;
1789 }
1790 if (!PyTuple_Check(firstitem) || PyTuple_Size(firstitem) != 2) {
1791 PyErr_SetString(PyExc_TypeError, "dict items "
1792 "iterator must return 2-tuples");
1793 goto BatchFailed;
1794 }
1795
1796 /* Try to get a second item */
1797 p = PyIter_Next(iter);
1798 if (p == NULL) {
1799 if (PyErr_Occurred())
1800 goto BatchFailed;
1801
1802 /* Only one item to write */
1803 if (save(self, PyTuple_GET_ITEM(firstitem, 0), 0) < 0)
1804 goto BatchFailed;
1805 if (save(self, PyTuple_GET_ITEM(firstitem, 1), 0) < 0)
1806 goto BatchFailed;
1807 if (self->write_func(self, &setitem, 1) < 0)
1808 goto BatchFailed;
1809 Py_CLEAR(firstitem);
1810 break;
1811 }
1812
1813 /* More than one item to write */
1814
1815 /* Pump out MARK, items, SETITEMS. */
1816 if (self->write_func(self, &MARKv, 1) < 0)
1817 goto BatchFailed;
1818
1819 if (save(self, PyTuple_GET_ITEM(firstitem, 0), 0) < 0)
1820 goto BatchFailed;
1821 if (save(self, PyTuple_GET_ITEM(firstitem, 1), 0) < 0)
1822 goto BatchFailed;
1823 Py_CLEAR(firstitem);
1824 n = 1;
1825
1826 /* Fetch and save up to BATCHSIZE items */
1827 while (p) {
1828 if (!PyTuple_Check(p) || PyTuple_Size(p) != 2) {
1829 PyErr_SetString(PyExc_TypeError, "dict items "
1830 "iterator must return 2-tuples");
1831 goto BatchFailed;
1832 }
1833 if (save(self, PyTuple_GET_ITEM(p, 0), 0) < 0)
1834 goto BatchFailed;
1835 if (save(self, PyTuple_GET_ITEM(p, 1), 0) < 0)
1836 goto BatchFailed;
1837 Py_CLEAR(p);
1838 n += 1;
1839
1840 if (n == BATCHSIZE)
1841 break;
1842
Tim Peters42f08ac2003-02-11 22:43:24 +00001843 p = PyIter_Next(iter);
1844 if (p == NULL) {
1845 if (PyErr_Occurred())
1846 goto BatchFailed;
1847 break;
1848 }
Tim Peters42f08ac2003-02-11 22:43:24 +00001849 }
1850
Amaury Forgeot d'Arc24cb3822008-09-11 20:56:13 +00001851 if (self->write_func(self, &setitems, 1) < 0)
1852 goto BatchFailed;
Tim Peters42f08ac2003-02-11 22:43:24 +00001853
Tim Peters90975f12003-02-12 05:28:58 +00001854 } while (n == BATCHSIZE);
Tim Peters42f08ac2003-02-11 22:43:24 +00001855 return 0;
1856
1857BatchFailed:
Amaury Forgeot d'Arc24cb3822008-09-11 20:56:13 +00001858 Py_XDECREF(firstitem);
1859 Py_XDECREF(p);
Tim Peters42f08ac2003-02-11 22:43:24 +00001860 return -1;
1861}
1862
Guido van Rossum60456fd1997-04-09 17:36:32 +00001863static int
Tim Peterscba30e22003-02-01 06:24:36 +00001864save_dict(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001865{
Tim Peters42f08ac2003-02-11 22:43:24 +00001866 int res = -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001867 char s[3];
Tim Peters42f08ac2003-02-11 22:43:24 +00001868 int len;
1869 PyObject *iter;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001870
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001871 if (self->fast && !fast_save_enter(self, args))
1872 goto finally;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00001873
Tim Peters42f08ac2003-02-11 22:43:24 +00001874 /* Create an empty dict. */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001875 if (self->bin) {
1876 s[0] = EMPTY_DICT;
1877 len = 1;
1878 }
1879 else {
1880 s[0] = MARK;
1881 s[1] = DICT;
1882 len = 2;
1883 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001884
Tim Peters0bc93f52003-02-02 18:29:33 +00001885 if (self->write_func(self, s, len) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001886 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001887
Tim Peters42f08ac2003-02-11 22:43:24 +00001888 /* Get dict size, and bow out early if empty. */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001889 if ((len = PyDict_Size(args)) < 0)
1890 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001891
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001892 if (len == 0) {
Tim Peters90975f12003-02-12 05:28:58 +00001893 if (put(self, args) >= 0)
1894 res = 0;
1895 goto finally;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001896 }
Tim Peters90975f12003-02-12 05:28:58 +00001897 if (put2(self, args) < 0)
1898 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001899
Tim Peters42f08ac2003-02-11 22:43:24 +00001900 /* Materialize the dict items. */
1901 iter = PyObject_CallMethod(args, "iteritems", "()");
1902 if (iter == NULL)
1903 goto finally;
Facundo Batista763d3092008-06-30 01:10:55 +00001904 if (Py_EnterRecursiveCall(" while pickling an object") == 0)
1905 {
1906 res = batch_dict(self, iter);
1907 Py_LeaveRecursiveCall();
1908 }
Tim Peters42f08ac2003-02-11 22:43:24 +00001909 Py_DECREF(iter);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001910
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001911 finally:
1912 if (self->fast && !fast_save_leave(self, args))
1913 res = -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001914
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001915 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001916}
1917
1918
Tim Peters84e87f32001-03-17 04:50:51 +00001919static int
Tim Peterscba30e22003-02-01 06:24:36 +00001920save_inst(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001921{
1922 PyObject *class = 0, *module = 0, *name = 0, *state = 0,
1923 *getinitargs_func = 0, *getstate_func = 0, *class_args = 0;
1924 char *module_str, *name_str;
1925 int module_size, name_size, res = -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001926
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001927 static char inst = INST, obj = OBJ, build = BUILD;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001928
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001929 if (self->fast && !fast_save_enter(self, args))
1930 goto finally;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00001931
Tim Peters0bc93f52003-02-02 18:29:33 +00001932 if (self->write_func(self, &MARKv, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001933 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001934
Tim Peterscba30e22003-02-01 06:24:36 +00001935 if (!( class = PyObject_GetAttr(args, __class___str)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001936 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001937
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001938 if (self->bin) {
1939 if (save(self, class, 0) < 0)
1940 goto finally;
1941 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001942
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001943 if ((getinitargs_func = PyObject_GetAttr(args, __getinitargs___str))) {
1944 PyObject *element = 0;
1945 int i, len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001946
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001947 if (!( class_args =
Tim Peterscba30e22003-02-01 06:24:36 +00001948 PyObject_Call(getinitargs_func, empty_tuple, NULL)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001949 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001950
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001951 if ((len = PyObject_Size(class_args)) < 0)
1952 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001953
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001954 for (i = 0; i < len; i++) {
Tim Peterscba30e22003-02-01 06:24:36 +00001955 if (!( element = PySequence_GetItem(class_args, i)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001956 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001957
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001958 if (save(self, element, 0) < 0) {
1959 Py_DECREF(element);
1960 goto finally;
1961 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001962
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001963 Py_DECREF(element);
1964 }
1965 }
1966 else {
Jeremy Hyltonf00368f2003-06-16 20:19:49 +00001967 if (PyErr_ExceptionMatches(PyExc_AttributeError))
1968 PyErr_Clear();
1969 else
1970 goto finally;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001971 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001972
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001973 if (!self->bin) {
1974 if (!( name = ((PyClassObject *)class)->cl_name )) {
1975 PyErr_SetString(PicklingError, "class has no name");
1976 goto finally;
1977 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001978
Tim Peterscba30e22003-02-01 06:24:36 +00001979 if (!( module = whichmodule(class, name)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001980 goto finally;
Guido van Rossum053b8df1998-11-25 16:18:00 +00001981
Tim Peters84e87f32001-03-17 04:50:51 +00001982
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001983 if ((module_size = PyString_Size(module)) < 0 ||
1984 (name_size = PyString_Size(name)) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001985 goto finally;
Guido van Rossum053b8df1998-11-25 16:18:00 +00001986
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001987 module_str = PyString_AS_STRING((PyStringObject *)module);
1988 name_str = PyString_AS_STRING((PyStringObject *)name);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001989
Tim Peters0bc93f52003-02-02 18:29:33 +00001990 if (self->write_func(self, &inst, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001991 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001992
Tim Peters0bc93f52003-02-02 18:29:33 +00001993 if (self->write_func(self, module_str, module_size) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001994 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001995
Tim Peters0bc93f52003-02-02 18:29:33 +00001996 if (self->write_func(self, "\n", 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001997 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001998
Tim Peters0bc93f52003-02-02 18:29:33 +00001999 if (self->write_func(self, name_str, name_size) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002000 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002001
Tim Peters0bc93f52003-02-02 18:29:33 +00002002 if (self->write_func(self, "\n", 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002003 goto finally;
2004 }
Tim Peters0bc93f52003-02-02 18:29:33 +00002005 else if (self->write_func(self, &obj, 1) < 0) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002006 goto finally;
2007 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002008
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002009 if ((getstate_func = PyObject_GetAttr(args, __getstate___str))) {
2010 state = PyObject_Call(getstate_func, empty_tuple, NULL);
Tim Peterscba30e22003-02-01 06:24:36 +00002011 if (!state)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002012 goto finally;
2013 }
2014 else {
Jeremy Hyltonf00368f2003-06-16 20:19:49 +00002015 if (PyErr_ExceptionMatches(PyExc_AttributeError))
2016 PyErr_Clear();
2017 else
2018 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002019
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002020 if (!( state = PyObject_GetAttr(args, __dict___str))) {
Jeremy Hyltonf00368f2003-06-16 20:19:49 +00002021 if (PyErr_ExceptionMatches(PyExc_AttributeError))
2022 PyErr_Clear();
2023 else
2024 goto finally;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002025 res = 0;
2026 goto finally;
2027 }
2028 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002029
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002030 if (!PyDict_Check(state)) {
2031 if (put2(self, args) < 0)
2032 goto finally;
2033 }
2034 else {
2035 if (put(self, args) < 0)
2036 goto finally;
2037 }
Tim Peters84e87f32001-03-17 04:50:51 +00002038
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002039 if (save(self, state, 0) < 0)
2040 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002041
Tim Peters0bc93f52003-02-02 18:29:33 +00002042 if (self->write_func(self, &build, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002043 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002044
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002045 res = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002046
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002047 finally:
2048 if (self->fast && !fast_save_leave(self, args))
2049 res = -1;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00002050
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002051 Py_XDECREF(module);
2052 Py_XDECREF(class);
2053 Py_XDECREF(state);
2054 Py_XDECREF(getinitargs_func);
2055 Py_XDECREF(getstate_func);
2056 Py_XDECREF(class_args);
Guido van Rossum60456fd1997-04-09 17:36:32 +00002057
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002058 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002059}
2060
2061
Guido van Rossum60456fd1997-04-09 17:36:32 +00002062static int
Tim Peterscba30e22003-02-01 06:24:36 +00002063save_global(Picklerobject *self, PyObject *args, PyObject *name)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002064{
Jeremy Hylton9ee91f12002-07-11 22:02:33 +00002065 PyObject *global_name = 0, *module = 0, *mod = 0, *klass = 0;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002066 char *name_str, *module_str;
2067 int module_size, name_size, res = -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002068
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002069 static char global = GLOBAL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002070
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002071 if (name) {
2072 global_name = name;
2073 Py_INCREF(global_name);
2074 }
2075 else {
Tim Peterscba30e22003-02-01 06:24:36 +00002076 if (!( global_name = PyObject_GetAttr(args, __name___str)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002077 goto finally;
2078 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002079
Tim Peterscba30e22003-02-01 06:24:36 +00002080 if (!( module = whichmodule(args, global_name)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002081 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002082
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002083 if ((module_size = PyString_Size(module)) < 0 ||
2084 (name_size = PyString_Size(global_name)) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002085 goto finally;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002086
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002087 module_str = PyString_AS_STRING((PyStringObject *)module);
2088 name_str = PyString_AS_STRING((PyStringObject *)global_name);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002089
Guido van Rossum75bfd052002-12-24 18:10:07 +00002090 /* XXX This can be doing a relative import. Clearly it shouldn't,
2091 but I don't know how to stop it. :-( */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002092 mod = PyImport_ImportModule(module_str);
2093 if (mod == NULL) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002094 cPickle_ErrFormat(PicklingError,
Tim Petersb9ce7cd2003-02-18 20:50:45 +00002095 "Can't pickle %s: import of module %s "
2096 "failed",
2097 "OS", args, module);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002098 goto finally;
2099 }
Jeremy Hylton0e1f7a82002-07-11 22:01:40 +00002100 klass = PyObject_GetAttrString(mod, name_str);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002101 if (klass == NULL) {
2102 cPickle_ErrFormat(PicklingError,
Tim Petersb9ce7cd2003-02-18 20:50:45 +00002103 "Can't pickle %s: attribute lookup %s.%s "
2104 "failed",
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002105 "OSS", args, module, global_name);
2106 goto finally;
2107 }
2108 if (klass != args) {
Jeremy Hylton0e1f7a82002-07-11 22:01:40 +00002109 Py_DECREF(klass);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002110 cPickle_ErrFormat(PicklingError,
Tim Peters731098b2003-02-04 20:56:09 +00002111 "Can't pickle %s: it's not the same object "
2112 "as %s.%s",
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002113 "OSS", args, module, global_name);
2114 goto finally;
2115 }
Jeremy Hylton0e1f7a82002-07-11 22:01:40 +00002116 Py_DECREF(klass);
Guido van Rossuma92d16a2001-08-18 21:22:07 +00002117
Tim Peters731098b2003-02-04 20:56:09 +00002118 if (self->proto >= 2) {
2119 /* See whether this is in the extension registry, and if
2120 * so generate an EXT opcode.
2121 */
2122 PyObject *py_code; /* extension code as Python object */
Tim Peters3e667d52003-02-04 21:47:44 +00002123 long code; /* extension code as C value */
Tim Peters731098b2003-02-04 20:56:09 +00002124 char c_str[5];
2125 int n;
2126
2127 PyTuple_SET_ITEM(two_tuple, 0, module);
2128 PyTuple_SET_ITEM(two_tuple, 1, global_name);
2129 py_code = PyDict_GetItem(extension_registry, two_tuple);
2130 if (py_code == NULL)
2131 goto gen_global; /* not registered */
2132
2133 /* Verify py_code has the right type and value. */
2134 if (!PyInt_Check(py_code)) {
2135 cPickle_ErrFormat(PicklingError, "Can't pickle %s: "
Tim Peters6288e232003-02-05 03:53:10 +00002136 "extension code %s isn't an integer",
Tim Peters731098b2003-02-04 20:56:09 +00002137 "OO", args, py_code);
2138 goto finally;
2139 }
2140 code = PyInt_AS_LONG(py_code);
2141 if (code <= 0 || code > 0x7fffffffL) {
2142 cPickle_ErrFormat(PicklingError, "Can't pickle %s: "
2143 "extension code %ld is out of range",
2144 "Ol", args, code);
2145 goto finally;
2146 }
2147
2148 /* Generate an EXT opcode. */
2149 if (code <= 0xff) {
2150 c_str[0] = EXT1;
2151 c_str[1] = (char)code;
2152 n = 2;
2153 }
2154 else if (code <= 0xffff) {
2155 c_str[0] = EXT2;
2156 c_str[1] = (char)(code & 0xff);
2157 c_str[2] = (char)((code >> 8) & 0xff);
2158 n = 3;
2159 }
2160 else {
2161 c_str[0] = EXT4;
2162 c_str[1] = (char)(code & 0xff);
2163 c_str[2] = (char)((code >> 8) & 0xff);
2164 c_str[3] = (char)((code >> 16) & 0xff);
2165 c_str[4] = (char)((code >> 24) & 0xff);
2166 n = 5;
2167 }
2168
2169 if (self->write_func(self, c_str, n) >= 0)
2170 res = 0;
2171 goto finally; /* and don't memoize */
2172 }
2173
2174 gen_global:
Tim Peters0bc93f52003-02-02 18:29:33 +00002175 if (self->write_func(self, &global, 1) < 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, module_str, module_size) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002179 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002180
Tim Peters0bc93f52003-02-02 18:29:33 +00002181 if (self->write_func(self, "\n", 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002182 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002183
Tim Peters0bc93f52003-02-02 18:29:33 +00002184 if (self->write_func(self, name_str, name_size) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002185 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002186
Tim Peters0bc93f52003-02-02 18:29:33 +00002187 if (self->write_func(self, "\n", 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002188 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002189
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002190 if (put(self, args) < 0)
2191 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002192
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002193 res = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002194
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002195 finally:
2196 Py_XDECREF(module);
2197 Py_XDECREF(global_name);
2198 Py_XDECREF(mod);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002199
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002200 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002201}
2202
Guido van Rossum60456fd1997-04-09 17:36:32 +00002203static int
Tim Peterscba30e22003-02-01 06:24:36 +00002204save_pers(Picklerobject *self, PyObject *args, PyObject *f)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002205{
2206 PyObject *pid = 0;
2207 int size, res = -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002208
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002209 static char persid = PERSID, binpersid = BINPERSID;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002210
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002211 Py_INCREF(args);
2212 ARG_TUP(self, args);
2213 if (self->arg) {
2214 pid = PyObject_Call(f, self->arg, NULL);
2215 FREE_ARG_TUP(self);
2216 }
2217 if (! pid) return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002218
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002219 if (pid != Py_None) {
2220 if (!self->bin) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002221 if (!PyString_Check(pid)) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002222 PyErr_SetString(PicklingError,
2223 "persistent id must be string");
2224 goto finally;
2225 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002226
Tim Peters0bc93f52003-02-02 18:29:33 +00002227 if (self->write_func(self, &persid, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002228 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002229
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002230 if ((size = PyString_Size(pid)) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002231 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002232
Tim Peters0bc93f52003-02-02 18:29:33 +00002233 if (self->write_func(self,
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002234 PyString_AS_STRING(
2235 (PyStringObject *)pid),
Tim Peters0bc93f52003-02-02 18:29:33 +00002236 size) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002237 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002238
Tim Peters0bc93f52003-02-02 18:29:33 +00002239 if (self->write_func(self, "\n", 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002240 goto finally;
Tim Peters84e87f32001-03-17 04:50:51 +00002241
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002242 res = 1;
2243 goto finally;
2244 }
2245 else if (save(self, pid, 1) >= 0) {
Tim Peters0bc93f52003-02-02 18:29:33 +00002246 if (self->write_func(self, &binpersid, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002247 res = -1;
2248 else
2249 res = 1;
2250 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002251
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002252 goto finally;
2253 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002254
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002255 res = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002256
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002257 finally:
2258 Py_XDECREF(pid);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002259
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002260 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002261}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002262
Tim Peters71fcda52003-02-14 23:05:28 +00002263/* We're saving ob, and args is the 2-thru-5 tuple returned by the
2264 * appropriate __reduce__ method for ob.
2265 */
Tim Peters84e87f32001-03-17 04:50:51 +00002266static int
Amaury Forgeot d'Arc69a9c5b2008-10-30 21:18:34 +00002267save_reduce(Picklerobject *self, PyObject *args, PyObject *fn, PyObject *ob)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002268{
Tim Peters71fcda52003-02-14 23:05:28 +00002269 PyObject *callable;
2270 PyObject *argtup;
Amaury Forgeot d'Arc69a9c5b2008-10-30 21:18:34 +00002271 PyObject *state = NULL;
2272 PyObject *listitems = Py_None;
2273 PyObject *dictitems = Py_None;
2274 Py_ssize_t size;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002275
Tim Peters71fcda52003-02-14 23:05:28 +00002276 int use_newobj = self->proto >= 2;
2277
2278 static char reduce = REDUCE;
2279 static char build = BUILD;
2280 static char newobj = NEWOBJ;
2281
Amaury Forgeot d'Arc69a9c5b2008-10-30 21:18:34 +00002282 size = PyTuple_Size(args);
2283 if (size < 2 || size > 5) {
2284 cPickle_ErrFormat(PicklingError, "tuple returned by "
2285 "%s must contain 2 through 5 elements",
2286 "O", fn);
2287 return -1;
2288 }
2289
Tim Peters71fcda52003-02-14 23:05:28 +00002290 if (! PyArg_UnpackTuple(args, "save_reduce", 2, 5,
2291 &callable,
2292 &argtup,
2293 &state,
2294 &listitems,
2295 &dictitems))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002296 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002297
Raymond Hettingera6b45cc2004-12-07 07:05:57 +00002298 if (!PyTuple_Check(argtup)) {
Amaury Forgeot d'Arc69a9c5b2008-10-30 21:18:34 +00002299 cPickle_ErrFormat(PicklingError, "Second element of "
2300 "tuple returned by %s must be a tuple",
2301 "O", fn);
Raymond Hettingera6b45cc2004-12-07 07:05:57 +00002302 return -1;
2303 }
2304
Tim Peters71fcda52003-02-14 23:05:28 +00002305 if (state == Py_None)
2306 state = NULL;
Amaury Forgeot d'Arc69a9c5b2008-10-30 21:18:34 +00002307
Tim Peters71fcda52003-02-14 23:05:28 +00002308 if (listitems == Py_None)
2309 listitems = NULL;
Amaury Forgeot d'Arc69a9c5b2008-10-30 21:18:34 +00002310 else if (!PyIter_Check(listitems)) {
2311 cPickle_ErrFormat(PicklingError, "Fourth element of "
2312 "tuple returned by %s must be an iterator, not %s",
2313 "Os", fn, Py_TYPE(listitems)->tp_name);
2314 return -1;
2315 }
2316
Tim Peters71fcda52003-02-14 23:05:28 +00002317 if (dictitems == Py_None)
2318 dictitems = NULL;
Amaury Forgeot d'Arc69a9c5b2008-10-30 21:18:34 +00002319 else if (!PyIter_Check(dictitems)) {
2320 cPickle_ErrFormat(PicklingError, "Fifth element of "
2321 "tuple returned by %s must be an iterator, not %s",
2322 "Os", fn, Py_TYPE(dictitems)->tp_name);
2323 return -1;
2324 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002325
Tim Peters71fcda52003-02-14 23:05:28 +00002326 /* Protocol 2 special case: if callable's name is __newobj__, use
2327 * NEWOBJ. This consumes a lot of code.
2328 */
2329 if (use_newobj) {
2330 PyObject *temp = PyObject_GetAttr(callable, __name___str);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002331
Tim Peters71fcda52003-02-14 23:05:28 +00002332 if (temp == NULL) {
Jeremy Hyltonf00368f2003-06-16 20:19:49 +00002333 if (PyErr_ExceptionMatches(PyExc_AttributeError))
2334 PyErr_Clear();
2335 else
2336 return -1;
Tim Peters71fcda52003-02-14 23:05:28 +00002337 use_newobj = 0;
2338 }
2339 else {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002340 use_newobj = PyString_Check(temp) &&
2341 strcmp(PyString_AS_STRING(temp),
Tim Peters71fcda52003-02-14 23:05:28 +00002342 "__newobj__") == 0;
2343 Py_DECREF(temp);
2344 }
2345 }
2346 if (use_newobj) {
2347 PyObject *cls;
2348 PyObject *newargtup;
2349 int n, i;
2350
2351 /* Sanity checks. */
2352 n = PyTuple_Size(argtup);
2353 if (n < 1) {
2354 PyErr_SetString(PicklingError, "__newobj__ arglist "
2355 "is empty");
2356 return -1;
2357 }
2358
2359 cls = PyTuple_GET_ITEM(argtup, 0);
2360 if (! PyObject_HasAttrString(cls, "__new__")) {
2361 PyErr_SetString(PicklingError, "args[0] from "
2362 "__newobj__ args has no __new__");
2363 return -1;
2364 }
2365
2366 /* XXX How could ob be NULL? */
2367 if (ob != NULL) {
2368 PyObject *ob_dot_class;
2369
2370 ob_dot_class = PyObject_GetAttr(ob, __class___str);
Jeremy Hyltonf00368f2003-06-16 20:19:49 +00002371 if (ob_dot_class == NULL) {
2372 if (PyErr_ExceptionMatches(
2373 PyExc_AttributeError))
2374 PyErr_Clear();
2375 else
2376 return -1;
2377 }
Tim Peters71fcda52003-02-14 23:05:28 +00002378 i = ob_dot_class != cls; /* true iff a problem */
2379 Py_XDECREF(ob_dot_class);
2380 if (i) {
2381 PyErr_SetString(PicklingError, "args[0] from "
2382 "__newobj__ args has the wrong class");
2383 return -1;
2384 }
2385 }
2386
2387 /* Save the class and its __new__ arguments. */
2388 if (save(self, cls, 0) < 0)
2389 return -1;
2390
2391 newargtup = PyTuple_New(n-1); /* argtup[1:] */
2392 if (newargtup == NULL)
2393 return -1;
2394 for (i = 1; i < n; ++i) {
2395 PyObject *temp = PyTuple_GET_ITEM(argtup, i);
2396 Py_INCREF(temp);
2397 PyTuple_SET_ITEM(newargtup, i-1, temp);
2398 }
Neal Norwitz5a29dd32007-10-05 05:01:38 +00002399 i = save(self, newargtup, 0);
Tim Peters71fcda52003-02-14 23:05:28 +00002400 Py_DECREF(newargtup);
2401 if (i < 0)
2402 return -1;
2403
2404 /* Add NEWOBJ opcode. */
2405 if (self->write_func(self, &newobj, 1) < 0)
2406 return -1;
2407 }
2408 else {
2409 /* Not using NEWOBJ. */
2410 if (save(self, callable, 0) < 0 ||
2411 save(self, argtup, 0) < 0 ||
2412 self->write_func(self, &reduce, 1) < 0)
2413 return -1;
2414 }
2415
2416 /* Memoize. */
2417 /* XXX How can ob be NULL? */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002418 if (ob != NULL) {
2419 if (state && !PyDict_Check(state)) {
2420 if (put2(self, ob) < 0)
2421 return -1;
2422 }
Tim Peters71fcda52003-02-14 23:05:28 +00002423 else if (put(self, ob) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002424 return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002425 }
Tim Peters84e87f32001-03-17 04:50:51 +00002426
Guido van Rossum60456fd1997-04-09 17:36:32 +00002427
Tim Peters71fcda52003-02-14 23:05:28 +00002428 if (listitems && batch_list(self, listitems) < 0)
2429 return -1;
2430
2431 if (dictitems && batch_dict(self, dictitems) < 0)
2432 return -1;
2433
2434 if (state) {
2435 if (save(self, state, 0) < 0 ||
2436 self->write_func(self, &build, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002437 return -1;
2438 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002439
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002440 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002441}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002442
Guido van Rossum60456fd1997-04-09 17:36:32 +00002443static int
Tim Peters1d63c9f2003-02-02 20:29:39 +00002444save(Picklerobject *self, PyObject *args, int pers_save)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002445{
2446 PyTypeObject *type;
Tim Peters71fcda52003-02-14 23:05:28 +00002447 PyObject *py_ob_id = 0, *__reduce__ = 0, *t = 0;
Tim Peters71fcda52003-02-14 23:05:28 +00002448 int res = -1;
Amaury Forgeot d'Arc69a9c5b2008-10-30 21:18:34 +00002449 int tmp;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002450
Facundo Batista763d3092008-06-30 01:10:55 +00002451 if (Py_EnterRecursiveCall(" while pickling an object"))
2452 return -1;
Martin v. Löwis5a395302002-08-04 08:20:23 +00002453
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002454 if (!pers_save && self->pers_func) {
2455 if ((tmp = save_pers(self, args, self->pers_func)) != 0) {
2456 res = tmp;
2457 goto finally;
2458 }
2459 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002460
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002461 if (args == Py_None) {
2462 res = save_none(self, args);
2463 goto finally;
2464 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002465
Christian Heimese93237d2007-12-19 02:37:44 +00002466 type = Py_TYPE(args);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002467
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002468 switch (type->tp_name[0]) {
Guido van Rossum77f6a652002-04-03 22:41:51 +00002469 case 'b':
2470 if (args == Py_False || args == Py_True) {
2471 res = save_bool(self, args);
2472 goto finally;
2473 }
2474 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002475 case 'i':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002476 if (type == &PyInt_Type) {
2477 res = save_int(self, args);
2478 goto finally;
2479 }
2480 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002481
Guido van Rossum60456fd1997-04-09 17:36:32 +00002482 case 'l':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002483 if (type == &PyLong_Type) {
2484 res = save_long(self, args);
2485 goto finally;
2486 }
2487 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002488
Guido van Rossum60456fd1997-04-09 17:36:32 +00002489 case 'f':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002490 if (type == &PyFloat_Type) {
2491 res = save_float(self, args);
2492 goto finally;
2493 }
2494 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002495
Guido van Rossum60456fd1997-04-09 17:36:32 +00002496 case 't':
Tim Peters1d63c9f2003-02-02 20:29:39 +00002497 if (type == &PyTuple_Type && PyTuple_Size(args) == 0) {
2498 res = save_tuple(self, args);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002499 goto finally;
2500 }
2501 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002502
Guido van Rossum60456fd1997-04-09 17:36:32 +00002503 case 's':
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002504 if ((type == &PyString_Type) && (PyString_GET_SIZE(args) < 2)) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002505 res = save_string(self, args, 0);
2506 goto finally;
2507 }
Facundo Batista14618862008-06-22 15:27:10 +00002508 break;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00002509
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002510#ifdef Py_USING_UNICODE
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00002511 case 'u':
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002512 if ((type == &PyUnicode_Type) && (PyString_GET_SIZE(args) < 2)) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002513 res = save_unicode(self, args, 0);
2514 goto finally;
2515 }
Facundo Batista14618862008-06-22 15:27:10 +00002516 break;
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002517#endif
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002518 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002519
Christian Heimese93237d2007-12-19 02:37:44 +00002520 if (Py_REFCNT(args) > 1) {
Tim Peterscba30e22003-02-01 06:24:36 +00002521 if (!( py_ob_id = PyLong_FromVoidPtr(args)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002522 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002523
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002524 if (PyDict_GetItem(self->memo, py_ob_id)) {
2525 if (get(self, py_ob_id) < 0)
2526 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002527
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002528 res = 0;
2529 goto finally;
2530 }
2531 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002532
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002533 switch (type->tp_name[0]) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00002534 case 's':
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002535 if (type == &PyString_Type) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002536 res = save_string(self, args, 1);
2537 goto finally;
2538 }
2539 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002540
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002541#ifdef Py_USING_UNICODE
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00002542 case 'u':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002543 if (type == &PyUnicode_Type) {
2544 res = save_unicode(self, args, 1);
2545 goto finally;
2546 }
2547 break;
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002548#endif
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00002549
Guido van Rossum60456fd1997-04-09 17:36:32 +00002550 case 't':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002551 if (type == &PyTuple_Type) {
2552 res = save_tuple(self, args);
2553 goto finally;
2554 }
2555 if (type == &PyType_Type) {
2556 res = save_global(self, args, NULL);
2557 goto finally;
2558 }
2559 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002560
Guido van Rossum60456fd1997-04-09 17:36:32 +00002561 case 'l':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002562 if (type == &PyList_Type) {
2563 res = save_list(self, args);
2564 goto finally;
2565 }
2566 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002567
2568 case 'd':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002569 if (type == &PyDict_Type) {
2570 res = save_dict(self, args);
2571 goto finally;
2572 }
2573 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002574
2575 case 'i':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002576 if (type == &PyInstance_Type) {
2577 res = save_inst(self, args);
2578 goto finally;
2579 }
2580 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002581
2582 case 'c':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002583 if (type == &PyClass_Type) {
2584 res = save_global(self, args, NULL);
2585 goto finally;
2586 }
2587 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002588
2589 case 'f':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002590 if (type == &PyFunction_Type) {
2591 res = save_global(self, args, NULL);
Christian Tismer2460c622004-02-26 16:21:45 +00002592 if (res && PyErr_ExceptionMatches(PickleError)) {
2593 /* fall back to reduce */
2594 PyErr_Clear();
2595 break;
2596 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002597 goto finally;
2598 }
2599 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002600
2601 case 'b':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002602 if (type == &PyCFunction_Type) {
2603 res = save_global(self, args, NULL);
2604 goto finally;
2605 }
2606 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002607
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002608 if (!pers_save && self->inst_pers_func) {
2609 if ((tmp = save_pers(self, args, self->inst_pers_func)) != 0) {
2610 res = tmp;
2611 goto finally;
2612 }
2613 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002614
Jeremy Hylton39c61162002-07-16 19:47:43 +00002615 if (PyType_IsSubtype(type, &PyType_Type)) {
2616 res = save_global(self, args, NULL);
2617 goto finally;
2618 }
2619
Guido van Rossumb289b872003-02-19 01:45:13 +00002620 /* Get a reduction callable, and call it. This may come from
Georg Brandldffbf5f2008-05-20 07:49:57 +00002621 * copy_reg.dispatch_table, the object's __reduce_ex__ method,
Guido van Rossumb289b872003-02-19 01:45:13 +00002622 * or the object's __reduce__ method.
Tim Peters71fcda52003-02-14 23:05:28 +00002623 */
Tim Peters5aa3da62003-02-13 21:03:57 +00002624 __reduce__ = PyDict_GetItem(dispatch_table, (PyObject *)type);
2625 if (__reduce__ != NULL) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002626 Py_INCREF(__reduce__);
Guido van Rossumb289b872003-02-19 01:45:13 +00002627 Py_INCREF(args);
2628 ARG_TUP(self, args);
2629 if (self->arg) {
2630 t = PyObject_Call(__reduce__, self->arg, NULL);
2631 FREE_ARG_TUP(self);
2632 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002633 }
2634 else {
Guido van Rossumb289b872003-02-19 01:45:13 +00002635 /* Check for a __reduce_ex__ method. */
2636 __reduce__ = PyObject_GetAttr(args, __reduce_ex___str);
2637 if (__reduce__ != NULL) {
2638 t = PyInt_FromLong(self->proto);
2639 if (t != NULL) {
2640 ARG_TUP(self, t);
2641 t = NULL;
2642 if (self->arg) {
2643 t = PyObject_Call(__reduce__,
2644 self->arg, NULL);
2645 FREE_ARG_TUP(self);
2646 }
2647 }
2648 }
2649 else {
Jeremy Hyltonf00368f2003-06-16 20:19:49 +00002650 if (PyErr_ExceptionMatches(PyExc_AttributeError))
2651 PyErr_Clear();
2652 else
2653 goto finally;
Guido van Rossumb289b872003-02-19 01:45:13 +00002654 /* Check for a __reduce__ method. */
2655 __reduce__ = PyObject_GetAttr(args, __reduce___str);
2656 if (__reduce__ != NULL) {
2657 t = PyObject_Call(__reduce__,
2658 empty_tuple, NULL);
2659 }
2660 else {
2661 PyErr_SetObject(UnpickleableError, args);
2662 goto finally;
2663 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002664 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002665 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002666
Tim Peters71fcda52003-02-14 23:05:28 +00002667 if (t == NULL)
2668 goto finally;
Tim Peters84e87f32001-03-17 04:50:51 +00002669
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002670 if (PyString_Check(t)) {
Tim Peters71fcda52003-02-14 23:05:28 +00002671 res = save_global(self, args, t);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002672 goto finally;
2673 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002674
Amaury Forgeot d'Arc69a9c5b2008-10-30 21:18:34 +00002675 if (!PyTuple_Check(t)) {
Tim Peters71fcda52003-02-14 23:05:28 +00002676 cPickle_ErrFormat(PicklingError, "Value returned by "
2677 "%s must be string or tuple",
2678 "O", __reduce__);
2679 goto finally;
2680 }
2681
Amaury Forgeot d'Arc69a9c5b2008-10-30 21:18:34 +00002682 res = save_reduce(self, t, __reduce__, args);
Guido van Rossum60456fd1997-04-09 17:36:32 +00002683
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002684 finally:
Facundo Batista763d3092008-06-30 01:10:55 +00002685 Py_LeaveRecursiveCall();
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002686 Py_XDECREF(py_ob_id);
2687 Py_XDECREF(__reduce__);
2688 Py_XDECREF(t);
Tim Peters84e87f32001-03-17 04:50:51 +00002689
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002690 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002691}
2692
2693
2694static int
Tim Peterscba30e22003-02-01 06:24:36 +00002695dump(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002696{
2697 static char stop = STOP;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002698
Tim Peters4190fb82003-02-02 16:09:05 +00002699 if (self->proto >= 2) {
2700 char bytes[2];
2701
2702 bytes[0] = PROTO;
Tim Peters87482ea2003-02-02 16:16:30 +00002703 assert(self->proto >= 0 && self->proto < 256);
2704 bytes[1] = (char)self->proto;
Tim Peters4190fb82003-02-02 16:09:05 +00002705 if (self->write_func(self, bytes, 2) < 0)
2706 return -1;
2707 }
2708
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002709 if (save(self, args, 0) < 0)
2710 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002711
Tim Peters4190fb82003-02-02 16:09:05 +00002712 if (self->write_func(self, &stop, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002713 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002714
Tim Peters4190fb82003-02-02 16:09:05 +00002715 if (self->write_func(self, NULL, 0) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002716 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002717
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002718 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002719}
2720
2721static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00002722Pickle_clear_memo(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002723{
Tim Peterscba30e22003-02-01 06:24:36 +00002724 if (self->memo)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002725 PyDict_Clear(self->memo);
2726 Py_INCREF(Py_None);
2727 return Py_None;
2728}
2729
2730static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00002731Pickle_getvalue(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002732{
2733 int l, i, rsize, ssize, clear=1, lm;
2734 long ik;
2735 PyObject *k, *r;
2736 char *s, *p, *have_get;
2737 Pdata *data;
2738
2739 /* Can be called by Python code or C code */
Tim Peterscba30e22003-02-01 06:24:36 +00002740 if (args && !PyArg_ParseTuple(args, "|i:getvalue", &clear))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002741 return NULL;
2742
2743 /* Check to make sure we are based on a list */
2744 if (! Pdata_Check(self->file)) {
2745 PyErr_SetString(PicklingError,
2746 "Attempt to getvalue() a non-list-based pickler");
2747 return NULL;
2748 }
2749
2750 /* flush write buffer */
2751 if (write_other(self, NULL, 0) < 0) return NULL;
2752
2753 data=(Pdata*)self->file;
2754 l=data->length;
2755
2756 /* set up an array to hold get/put status */
Tim Petersac5687a2003-02-02 18:08:34 +00002757 lm = PyDict_Size(self->memo);
2758 if (lm < 0) return NULL;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002759 lm++;
Tim Petersac5687a2003-02-02 18:08:34 +00002760 have_get = malloc(lm);
2761 if (have_get == NULL) return PyErr_NoMemory();
2762 memset(have_get, 0, lm);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002763
2764 /* Scan for gets. */
Tim Petersac5687a2003-02-02 18:08:34 +00002765 for (rsize = 0, i = l; --i >= 0; ) {
2766 k = data->data[i];
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002767
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002768 if (PyString_Check(k))
2769 rsize += PyString_GET_SIZE(k);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002770
2771 else if (PyInt_Check(k)) { /* put */
Tim Petersac5687a2003-02-02 18:08:34 +00002772 ik = PyInt_AS_LONG((PyIntObject*)k);
2773 if (ik >= lm || ik == 0) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002774 PyErr_SetString(PicklingError,
2775 "Invalid get data");
Neal Norwitz98a96002006-07-23 07:57:11 +00002776 goto err;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002777 }
Tim Petersac5687a2003-02-02 18:08:34 +00002778 if (have_get[ik]) /* with matching get */
2779 rsize += ik < 256 ? 2 : 5;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002780 }
2781
2782 else if (! (PyTuple_Check(k) &&
2783 PyTuple_GET_SIZE(k) == 2 &&
Tim Petersac5687a2003-02-02 18:08:34 +00002784 PyInt_Check((k = PyTuple_GET_ITEM(k, 0))))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002785 ) {
2786 PyErr_SetString(PicklingError,
2787 "Unexpected data in internal list");
Neal Norwitz98a96002006-07-23 07:57:11 +00002788 goto err;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002789 }
2790
2791 else { /* put */
Tim Petersac5687a2003-02-02 18:08:34 +00002792 ik = PyInt_AS_LONG((PyIntObject *)k);
2793 if (ik >= lm || ik == 0) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002794 PyErr_SetString(PicklingError,
2795 "Invalid get data");
2796 return NULL;
2797 }
Tim Petersac5687a2003-02-02 18:08:34 +00002798 have_get[ik] = 1;
2799 rsize += ik < 256 ? 2 : 5;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002800 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002801 }
2802
2803 /* Now generate the result */
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002804 r = PyString_FromStringAndSize(NULL, rsize);
Tim Petersac5687a2003-02-02 18:08:34 +00002805 if (r == NULL) goto err;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002806 s = PyString_AS_STRING((PyStringObject *)r);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002807
Tim Petersac5687a2003-02-02 18:08:34 +00002808 for (i = 0; i < l; i++) {
2809 k = data->data[i];
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002810
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002811 if (PyString_Check(k)) {
2812 ssize = PyString_GET_SIZE(k);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002813 if (ssize) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002814 p=PyString_AS_STRING((PyStringObject *)k);
Tim Petersac5687a2003-02-02 18:08:34 +00002815 while (--ssize >= 0)
2816 *s++ = *p++;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002817 }
2818 }
2819
2820 else if (PyTuple_Check(k)) { /* get */
Tim Petersac5687a2003-02-02 18:08:34 +00002821 ik = PyInt_AS_LONG((PyIntObject *)
2822 PyTuple_GET_ITEM(k, 0));
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002823 if (ik < 256) {
2824 *s++ = BINGET;
2825 *s++ = (int)(ik & 0xff);
2826 }
2827 else {
2828 *s++ = LONG_BINGET;
2829 *s++ = (int)(ik & 0xff);
2830 *s++ = (int)((ik >> 8) & 0xff);
2831 *s++ = (int)((ik >> 16) & 0xff);
2832 *s++ = (int)((ik >> 24) & 0xff);
2833 }
2834 }
2835
2836 else { /* put */
Tim Petersac5687a2003-02-02 18:08:34 +00002837 ik = PyInt_AS_LONG((PyIntObject*)k);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002838
2839 if (have_get[ik]) { /* with matching get */
2840 if (ik < 256) {
2841 *s++ = BINPUT;
2842 *s++ = (int)(ik & 0xff);
2843 }
2844 else {
2845 *s++ = LONG_BINPUT;
2846 *s++ = (int)(ik & 0xff);
2847 *s++ = (int)((ik >> 8) & 0xff);
2848 *s++ = (int)((ik >> 16) & 0xff);
2849 *s++ = (int)((ik >> 24) & 0xff);
2850 }
2851 }
2852 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002853 }
2854
2855 if (clear) {
2856 PyDict_Clear(self->memo);
Tim Petersac5687a2003-02-02 18:08:34 +00002857 Pdata_clear(data, 0);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002858 }
2859
2860 free(have_get);
2861 return r;
2862 err:
2863 free(have_get);
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00002864 return NULL;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002865}
2866
2867static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00002868Pickler_dump(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002869{
2870 PyObject *ob;
2871 int get=0;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002872
Tim Peterscba30e22003-02-01 06:24:36 +00002873 if (!( PyArg_ParseTuple(args, "O|i:dump", &ob, &get)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002874 return NULL;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002875
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002876 if (dump(self, ob) < 0)
2877 return NULL;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002878
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002879 if (get) return Pickle_getvalue(self, NULL);
Guido van Rossum053b8df1998-11-25 16:18:00 +00002880
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002881 /* XXX Why does dump() return self? */
2882 Py_INCREF(self);
2883 return (PyObject*)self;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002884}
2885
2886
Tim Peterscba30e22003-02-01 06:24:36 +00002887static struct PyMethodDef Pickler_methods[] =
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002888{
Neal Norwitzb0493252002-03-31 14:44:22 +00002889 {"dump", (PyCFunction)Pickler_dump, METH_VARARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00002890 PyDoc_STR("dump(object) -- "
2891 "Write an object in pickle format to the object's pickle stream")},
Fred Drake0ebacc82002-05-01 20:36:39 +00002892 {"clear_memo", (PyCFunction)Pickle_clear_memo, METH_NOARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00002893 PyDoc_STR("clear_memo() -- Clear the picklers memo")},
Neal Norwitzb0493252002-03-31 14:44:22 +00002894 {"getvalue", (PyCFunction)Pickle_getvalue, METH_VARARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00002895 PyDoc_STR("getvalue() -- Finish picking a list-based pickle")},
Guido van Rossum60456fd1997-04-09 17:36:32 +00002896 {NULL, NULL} /* sentinel */
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002897};
2898
2899
2900static Picklerobject *
Tim Peters5bd2a792003-02-01 16:45:06 +00002901newPicklerobject(PyObject *file, int proto)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002902{
2903 Picklerobject *self;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002904
Tim Peters5bd2a792003-02-01 16:45:06 +00002905 if (proto < 0)
Tim Peters8587b3c2003-02-13 15:44:41 +00002906 proto = HIGHEST_PROTOCOL;
2907 if (proto > HIGHEST_PROTOCOL) {
Tim Peters5bd2a792003-02-01 16:45:06 +00002908 PyErr_Format(PyExc_ValueError, "pickle protocol %d asked for; "
2909 "the highest available protocol is %d",
Tim Peters8587b3c2003-02-13 15:44:41 +00002910 proto, HIGHEST_PROTOCOL);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002911 return NULL;
Tim Peters5bd2a792003-02-01 16:45:06 +00002912 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002913
Jeremy Hylton4cf63192003-04-09 21:05:12 +00002914 self = PyObject_GC_New(Picklerobject, &Picklertype);
Tim Peters5bd2a792003-02-01 16:45:06 +00002915 if (self == NULL)
2916 return NULL;
2917 self->proto = proto;
2918 self->bin = proto > 0;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002919 self->fp = NULL;
2920 self->write = NULL;
2921 self->memo = NULL;
2922 self->arg = NULL;
2923 self->pers_func = NULL;
2924 self->inst_pers_func = NULL;
2925 self->write_buf = NULL;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002926 self->fast = 0;
2927 self->fast_container = 0;
2928 self->fast_memo = NULL;
2929 self->buf_size = 0;
2930 self->dispatch_table = NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002931
Tim Peters5bd2a792003-02-01 16:45:06 +00002932 self->file = NULL;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002933 if (file)
2934 Py_INCREF(file);
Tim Peters5bd2a792003-02-01 16:45:06 +00002935 else {
2936 file = Pdata_New();
2937 if (file == NULL)
2938 goto err;
2939 }
2940 self->file = file;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002941
Tim Peterscba30e22003-02-01 06:24:36 +00002942 if (!( self->memo = PyDict_New()))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002943 goto err;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002944
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002945 if (PyFile_Check(file)) {
2946 self->fp = PyFile_AsFile(file);
2947 if (self->fp == NULL) {
Tim Peters5bd2a792003-02-01 16:45:06 +00002948 PyErr_SetString(PyExc_ValueError,
2949 "I/O operation on closed file");
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002950 goto err;
2951 }
2952 self->write_func = write_file;
Guido van Rossumc91fcaa1999-03-29 20:00:14 +00002953 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002954 else if (PycStringIO_OutputCheck(file)) {
2955 self->write_func = write_cStringIO;
2956 }
2957 else if (file == Py_None) {
2958 self->write_func = write_none;
2959 }
2960 else {
2961 self->write_func = write_other;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002962
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002963 if (! Pdata_Check(file)) {
2964 self->write = PyObject_GetAttr(file, write_str);
2965 if (!self->write) {
2966 PyErr_Clear();
Tim Peterscba30e22003-02-01 06:24:36 +00002967 PyErr_SetString(PyExc_TypeError,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002968 "argument must have 'write' "
2969 "attribute");
2970 goto err;
2971 }
2972 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002973
Tim Peters5bd2a792003-02-01 16:45:06 +00002974 self->write_buf = (char *)PyMem_Malloc(WRITE_BUF_SIZE);
2975 if (self->write_buf == NULL) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002976 PyErr_NoMemory();
2977 goto err;
2978 }
2979 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002980
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002981 if (PyEval_GetRestricted()) {
2982 /* Restricted execution, get private tables */
Alexandre Vassalotti9510e4a2008-05-11 08:25:28 +00002983 PyObject *m = PyImport_Import(copyreg_str);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002984
Tim Peters5b7da392003-02-04 00:21:07 +00002985 if (m == NULL)
2986 goto err;
2987 self->dispatch_table = PyObject_GetAttr(m, dispatch_table_str);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002988 Py_DECREF(m);
Tim Peters5b7da392003-02-04 00:21:07 +00002989 if (self->dispatch_table == NULL)
2990 goto err;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002991 }
2992 else {
Tim Peters5b7da392003-02-04 00:21:07 +00002993 self->dispatch_table = dispatch_table;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002994 Py_INCREF(dispatch_table);
2995 }
Jeremy Hylton4cf63192003-04-09 21:05:12 +00002996 PyObject_GC_Track(self);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002997
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002998 return self;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002999
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003000 err:
Tim Peters5bd2a792003-02-01 16:45:06 +00003001 Py_DECREF(self);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003002 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003003}
3004
3005
3006static PyObject *
Martin v. Löwis544f1192004-07-27 05:22:33 +00003007get_Pickler(PyObject *self, PyObject *args, PyObject *kwds)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003008{
Martin v. Löwis15e62742006-02-27 16:46:16 +00003009 static char *kwlist[] = {"file", "protocol", NULL};
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003010 PyObject *file = NULL;
Tim Peters5bd2a792003-02-01 16:45:06 +00003011 int proto = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003012
Tim Peters92c8bb32003-02-13 23:00:26 +00003013 /* XXX
Martin v. Löwis544f1192004-07-27 05:22:33 +00003014 * The documented signature is Pickler(file, protocol=0), but this
Tim Peters92c8bb32003-02-13 23:00:26 +00003015 * accepts Pickler() and Pickler(integer) too. The meaning then
3016 * is clear as mud, undocumented, and not supported by pickle.py.
3017 * I'm told Zope uses this, but I haven't traced into this code
3018 * far enough to figure out what it means.
Tim Peters5bd2a792003-02-01 16:45:06 +00003019 */
3020 if (!PyArg_ParseTuple(args, "|i:Pickler", &proto)) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003021 PyErr_Clear();
Tim Peters5bd2a792003-02-01 16:45:06 +00003022 proto = 0;
Martin v. Löwis544f1192004-07-27 05:22:33 +00003023 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|i:Pickler",
3024 kwlist, &file, &proto))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003025 return NULL;
3026 }
Tim Peters5bd2a792003-02-01 16:45:06 +00003027 return (PyObject *)newPicklerobject(file, proto);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003028}
3029
3030
3031static void
Tim Peterscba30e22003-02-01 06:24:36 +00003032Pickler_dealloc(Picklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003033{
Jeremy Hylton4cf63192003-04-09 21:05:12 +00003034 PyObject_GC_UnTrack(self);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003035 Py_XDECREF(self->write);
3036 Py_XDECREF(self->memo);
3037 Py_XDECREF(self->fast_memo);
3038 Py_XDECREF(self->arg);
3039 Py_XDECREF(self->file);
3040 Py_XDECREF(self->pers_func);
3041 Py_XDECREF(self->inst_pers_func);
3042 Py_XDECREF(self->dispatch_table);
Tim Peters5bd2a792003-02-01 16:45:06 +00003043 PyMem_Free(self->write_buf);
Christian Heimese93237d2007-12-19 02:37:44 +00003044 Py_TYPE(self)->tp_free((PyObject *)self);
Jeremy Hylton4cf63192003-04-09 21:05:12 +00003045}
3046
3047static int
3048Pickler_traverse(Picklerobject *self, visitproc visit, void *arg)
3049{
Thomas Woutersc6e55062006-04-15 21:47:09 +00003050 Py_VISIT(self->write);
3051 Py_VISIT(self->memo);
3052 Py_VISIT(self->fast_memo);
3053 Py_VISIT(self->arg);
3054 Py_VISIT(self->file);
3055 Py_VISIT(self->pers_func);
3056 Py_VISIT(self->inst_pers_func);
3057 Py_VISIT(self->dispatch_table);
Jeremy Hylton4cf63192003-04-09 21:05:12 +00003058 return 0;
3059}
3060
3061static int
3062Pickler_clear(Picklerobject *self)
3063{
Thomas Woutersedf17d82006-04-15 17:28:34 +00003064 Py_CLEAR(self->write);
3065 Py_CLEAR(self->memo);
3066 Py_CLEAR(self->fast_memo);
3067 Py_CLEAR(self->arg);
3068 Py_CLEAR(self->file);
3069 Py_CLEAR(self->pers_func);
3070 Py_CLEAR(self->inst_pers_func);
3071 Py_CLEAR(self->dispatch_table);
Jeremy Hylton4cf63192003-04-09 21:05:12 +00003072 return 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003073}
3074
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003075static PyObject *
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003076Pickler_get_pers_func(Picklerobject *p)
3077{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003078 if (p->pers_func == NULL)
3079 PyErr_SetString(PyExc_AttributeError, "persistent_id");
3080 else
3081 Py_INCREF(p->pers_func);
3082 return p->pers_func;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003083}
3084
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003085static int
3086Pickler_set_pers_func(Picklerobject *p, PyObject *v)
3087{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003088 if (v == NULL) {
3089 PyErr_SetString(PyExc_TypeError,
3090 "attribute deletion is not supported");
3091 return -1;
3092 }
3093 Py_XDECREF(p->pers_func);
3094 Py_INCREF(v);
3095 p->pers_func = v;
3096 return 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003097}
3098
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003099static int
3100Pickler_set_inst_pers_func(Picklerobject *p, PyObject *v)
3101{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003102 if (v == NULL) {
3103 PyErr_SetString(PyExc_TypeError,
3104 "attribute deletion is not supported");
3105 return -1;
3106 }
3107 Py_XDECREF(p->inst_pers_func);
3108 Py_INCREF(v);
3109 p->inst_pers_func = v;
3110 return 0;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003111}
3112
3113static PyObject *
3114Pickler_get_memo(Picklerobject *p)
3115{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003116 if (p->memo == NULL)
3117 PyErr_SetString(PyExc_AttributeError, "memo");
3118 else
3119 Py_INCREF(p->memo);
3120 return p->memo;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003121}
3122
3123static int
3124Pickler_set_memo(Picklerobject *p, PyObject *v)
3125{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003126 if (v == NULL) {
3127 PyErr_SetString(PyExc_TypeError,
3128 "attribute deletion is not supported");
3129 return -1;
3130 }
3131 if (!PyDict_Check(v)) {
3132 PyErr_SetString(PyExc_TypeError, "memo must be a dictionary");
3133 return -1;
3134 }
3135 Py_XDECREF(p->memo);
3136 Py_INCREF(v);
3137 p->memo = v;
3138 return 0;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003139}
3140
3141static PyObject *
3142Pickler_get_error(Picklerobject *p)
3143{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003144 /* why is this an attribute on the Pickler? */
3145 Py_INCREF(PicklingError);
3146 return PicklingError;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003147}
3148
3149static PyMemberDef Pickler_members[] = {
3150 {"binary", T_INT, offsetof(Picklerobject, bin)},
3151 {"fast", T_INT, offsetof(Picklerobject, fast)},
Jeremy Hylton3eb46f32001-10-16 17:10:49 +00003152 {NULL}
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003153};
3154
3155static PyGetSetDef Pickler_getsets[] = {
Tim Peterscba30e22003-02-01 06:24:36 +00003156 {"persistent_id", (getter)Pickler_get_pers_func,
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003157 (setter)Pickler_set_pers_func},
3158 {"inst_persistent_id", NULL, (setter)Pickler_set_inst_pers_func},
3159 {"memo", (getter)Pickler_get_memo, (setter)Pickler_set_memo},
Jeremy Hylton3eb46f32001-10-16 17:10:49 +00003160 {"PicklingError", (getter)Pickler_get_error, NULL},
3161 {NULL}
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003162};
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003163
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003164PyDoc_STRVAR(Picklertype__doc__,
3165"Objects that know how to pickle objects\n");
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003166
Guido van Rossum9716aaa1997-12-08 15:15:16 +00003167static PyTypeObject Picklertype = {
Martin v. Löwis68192102007-07-21 06:55:02 +00003168 PyVarObject_HEAD_INIT(NULL, 0)
Guido van Rossum14648392001-12-08 18:02:58 +00003169 "cPickle.Pickler", /*tp_name*/
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003170 sizeof(Picklerobject), /*tp_basicsize*/
3171 0,
3172 (destructor)Pickler_dealloc, /* tp_dealloc */
3173 0, /* tp_print */
3174 0, /* tp_getattr */
3175 0, /* tp_setattr */
3176 0, /* tp_compare */
3177 0, /* tp_repr */
3178 0, /* tp_as_number */
3179 0, /* tp_as_sequence */
3180 0, /* tp_as_mapping */
3181 0, /* tp_hash */
3182 0, /* tp_call */
3183 0, /* tp_str */
Jason Tishlerfb8595d2003-01-06 12:41:26 +00003184 PyObject_GenericGetAttr, /* tp_getattro */
3185 PyObject_GenericSetAttr, /* tp_setattro */
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003186 0, /* tp_as_buffer */
Jeremy Hylton4cf63192003-04-09 21:05:12 +00003187 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003188 Picklertype__doc__, /* tp_doc */
Jeremy Hylton4cf63192003-04-09 21:05:12 +00003189 (traverseproc)Pickler_traverse, /* tp_traverse */
3190 (inquiry)Pickler_clear, /* tp_clear */
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003191 0, /* tp_richcompare */
3192 0, /* tp_weaklistoffset */
3193 0, /* tp_iter */
3194 0, /* tp_iternext */
3195 Pickler_methods, /* tp_methods */
3196 Pickler_members, /* tp_members */
3197 Pickler_getsets, /* tp_getset */
Guido van Rossum9716aaa1997-12-08 15:15:16 +00003198};
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003199
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003200static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00003201find_class(PyObject *py_module_name, PyObject *py_global_name, PyObject *fc)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003202{
3203 PyObject *global = 0, *module;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003204
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003205 if (fc) {
3206 if (fc==Py_None) {
Tim Peters92c8bb32003-02-13 23:00:26 +00003207 PyErr_SetString(UnpicklingError, "Global and instance "
3208 "pickles are not supported.");
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003209 return NULL;
3210 }
Georg Brandl684fd0c2006-05-25 19:15:31 +00003211 return PyObject_CallFunctionObjArgs(fc, py_module_name,
3212 py_global_name, NULL);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003213 }
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00003214
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003215 module = PySys_GetObject("modules");
3216 if (module == NULL)
3217 return NULL;
Jeremy Hyltond1055231998-08-11 19:52:51 +00003218
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003219 module = PyDict_GetItem(module, py_module_name);
3220 if (module == NULL) {
3221 module = PyImport_Import(py_module_name);
3222 if (!module)
3223 return NULL;
3224 global = PyObject_GetAttr(module, py_global_name);
3225 Py_DECREF(module);
3226 }
3227 else
3228 global = PyObject_GetAttr(module, py_global_name);
3229 return global;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003230}
3231
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003232static int
Tim Peterscba30e22003-02-01 06:24:36 +00003233marker(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003234{
3235 if (self->num_marks < 1) {
3236 PyErr_SetString(UnpicklingError, "could not find MARK");
3237 return -1;
3238 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003239
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003240 return self->marks[--self->num_marks];
Guido van Rossum60456fd1997-04-09 17:36:32 +00003241}
3242
Tim Peters84e87f32001-03-17 04:50:51 +00003243
Guido van Rossum60456fd1997-04-09 17:36:32 +00003244static int
Tim Peterscba30e22003-02-01 06:24:36 +00003245load_none(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003246{
3247 PDATA_APPEND(self->stack, Py_None, -1);
3248 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003249}
3250
Guido van Rossumf9ffb031999-02-04 14:54:04 +00003251static int
Tim Peterscba30e22003-02-01 06:24:36 +00003252bad_readline(void)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003253{
3254 PyErr_SetString(UnpicklingError, "pickle data was truncated");
3255 return -1;
Guido van Rossumf9ffb031999-02-04 14:54:04 +00003256}
Guido van Rossum60456fd1997-04-09 17:36:32 +00003257
3258static int
Tim Peterscba30e22003-02-01 06:24:36 +00003259load_int(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003260{
3261 PyObject *py_int = 0;
3262 char *endptr, *s;
3263 int len, res = -1;
3264 long l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003265
Tim Peters0bc93f52003-02-02 18:29:33 +00003266 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003267 if (len < 2) return bad_readline();
3268 if (!( s=pystrndup(s,len))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003269
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003270 errno = 0;
3271 l = strtol(s, &endptr, 0);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003272
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003273 if (errno || (*endptr != '\n') || (endptr[1] != '\0')) {
3274 /* Hm, maybe we've got something long. Let's try reading
3275 it as a Python long object. */
3276 errno = 0;
3277 py_int = PyLong_FromString(s, NULL, 0);
3278 if (py_int == NULL) {
3279 PyErr_SetString(PyExc_ValueError,
3280 "could not convert string to int");
3281 goto finally;
3282 }
3283 }
3284 else {
Guido van Rossume2763392002-04-05 19:30:08 +00003285 if (len == 3 && (l == 0 || l == 1)) {
3286 if (!( py_int = PyBool_FromLong(l))) goto finally;
3287 }
3288 else {
3289 if (!( py_int = PyInt_FromLong(l))) goto finally;
3290 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003291 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003292
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003293 free(s);
3294 PDATA_PUSH(self->stack, py_int, -1);
3295 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003296
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003297 finally:
3298 free(s);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003299
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003300 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003301}
3302
Tim Peters3c67d792003-02-02 17:59:11 +00003303static int
3304load_bool(Unpicklerobject *self, PyObject *boolean)
3305{
3306 assert(boolean == Py_True || boolean == Py_False);
Tim Peterse0a39072003-02-03 15:45:56 +00003307 PDATA_APPEND(self->stack, boolean, -1);
Tim Peters3c67d792003-02-02 17:59:11 +00003308 return 0;
3309}
3310
Tim Petersee1a53c2003-02-02 02:57:53 +00003311/* s contains x bytes of a little-endian integer. Return its value as a
3312 * C int. Obscure: when x is 1 or 2, this is an unsigned little-endian
3313 * int, but when x is 4 it's a signed one. This is an historical source
3314 * of x-platform bugs.
3315 */
Tim Peters84e87f32001-03-17 04:50:51 +00003316static long
Tim Petersee1a53c2003-02-02 02:57:53 +00003317calc_binint(char *s, int x)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003318{
3319 unsigned char c;
3320 int i;
3321 long l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003322
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003323 for (i = 0, l = 0L; i < x; i++) {
3324 c = (unsigned char)s[i];
3325 l |= (long)c << (i * 8);
3326 }
Tim Petersbfa18f72001-04-10 01:54:42 +00003327#if SIZEOF_LONG > 4
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003328 /* Unlike BININT1 and BININT2, BININT (more accurately BININT4)
3329 * is signed, so on a box with longs bigger than 4 bytes we need
3330 * to extend a BININT's sign bit to the full width.
3331 */
3332 if (x == 4 && l & (1L << 31))
3333 l |= (~0L) << 32;
Tim Petersbfa18f72001-04-10 01:54:42 +00003334#endif
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003335 return l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003336}
3337
3338
3339static int
Tim Peterscba30e22003-02-01 06:24:36 +00003340load_binintx(Unpicklerobject *self, char *s, int x)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003341{
3342 PyObject *py_int = 0;
3343 long l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003344
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003345 l = calc_binint(s, x);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003346
Tim Peterscba30e22003-02-01 06:24:36 +00003347 if (!( py_int = PyInt_FromLong(l)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003348 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003349
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003350 PDATA_PUSH(self->stack, py_int, -1);
3351 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003352}
3353
3354
3355static int
Tim Peterscba30e22003-02-01 06:24:36 +00003356load_binint(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003357{
3358 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003359
Tim Peters0bc93f52003-02-02 18:29:33 +00003360 if (self->read_func(self, &s, 4) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003361 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003362
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003363 return load_binintx(self, s, 4);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003364}
3365
3366
3367static int
Tim Peterscba30e22003-02-01 06:24:36 +00003368load_binint1(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003369{
3370 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003371
Tim Peters0bc93f52003-02-02 18:29:33 +00003372 if (self->read_func(self, &s, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003373 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003374
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003375 return load_binintx(self, s, 1);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003376}
3377
3378
3379static int
Tim Peterscba30e22003-02-01 06:24:36 +00003380load_binint2(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003381{
3382 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003383
Tim Peters0bc93f52003-02-02 18:29:33 +00003384 if (self->read_func(self, &s, 2) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003385 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003386
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003387 return load_binintx(self, s, 2);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003388}
Tim Peters84e87f32001-03-17 04:50:51 +00003389
Guido van Rossum60456fd1997-04-09 17:36:32 +00003390static int
Tim Peterscba30e22003-02-01 06:24:36 +00003391load_long(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003392{
3393 PyObject *l = 0;
3394 char *end, *s;
3395 int len, res = -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003396
Tim Peters0bc93f52003-02-02 18:29:33 +00003397 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003398 if (len < 2) return bad_readline();
3399 if (!( s=pystrndup(s,len))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003400
Tim Peterscba30e22003-02-01 06:24:36 +00003401 if (!( l = PyLong_FromString(s, &end, 0)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003402 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003403
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003404 free(s);
3405 PDATA_PUSH(self->stack, l, -1);
3406 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003407
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003408 finally:
3409 free(s);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003410
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003411 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003412}
3413
Tim Petersee1a53c2003-02-02 02:57:53 +00003414/* 'size' bytes contain the # of bytes of little-endian 256's-complement
3415 * data following.
3416 */
3417static int
3418load_counted_long(Unpicklerobject *self, int size)
3419{
Martin v. Löwis18e16552006-02-15 17:27:45 +00003420 Py_ssize_t i;
Tim Petersee1a53c2003-02-02 02:57:53 +00003421 char *nbytes;
3422 unsigned char *pdata;
3423 PyObject *along;
3424
3425 assert(size == 1 || size == 4);
3426 i = self->read_func(self, &nbytes, size);
3427 if (i < 0) return -1;
3428
3429 size = calc_binint(nbytes, size);
3430 if (size < 0) {
3431 /* Corrupt or hostile pickle -- we never write one like
3432 * this.
3433 */
Tim Peters0c7c48e2003-02-03 22:07:24 +00003434 PyErr_SetString(UnpicklingError, "LONG pickle has negative "
Tim Petersee1a53c2003-02-02 02:57:53 +00003435 "byte count");
3436 return -1;
3437 }
3438
3439 if (size == 0)
3440 along = PyLong_FromLong(0L);
3441 else {
3442 /* Read the raw little-endian bytes & convert. */
Neal Norwitz529baf22003-02-02 17:08:33 +00003443 i = self->read_func(self, (char **)&pdata, size);
Tim Petersee1a53c2003-02-02 02:57:53 +00003444 if (i < 0) return -1;
3445 along = _PyLong_FromByteArray(pdata, (size_t)size,
3446 1 /* little endian */, 1 /* signed */);
3447 }
3448 if (along == NULL)
3449 return -1;
3450 PDATA_PUSH(self->stack, along, -1);
3451 return 0;
3452}
Tim Peters84e87f32001-03-17 04:50:51 +00003453
Guido van Rossum60456fd1997-04-09 17:36:32 +00003454static int
Tim Peterscba30e22003-02-01 06:24:36 +00003455load_float(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003456{
3457 PyObject *py_float = 0;
3458 char *endptr, *s;
3459 int len, res = -1;
3460 double d;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003461
Tim Peters0bc93f52003-02-02 18:29:33 +00003462 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003463 if (len < 2) return bad_readline();
3464 if (!( s=pystrndup(s,len))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003465
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003466 errno = 0;
Martin v. Löwis737ea822004-06-08 18:52:54 +00003467 d = PyOS_ascii_strtod(s, &endptr);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003468
Mark Dickinsona3ecd2c2009-01-24 16:40:29 +00003469 if ((errno == ERANGE && !(fabs(d) <= 1.0)) ||
3470 (endptr[0] != '\n') || (endptr[1] != '\0')) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003471 PyErr_SetString(PyExc_ValueError,
3472 "could not convert string to float");
3473 goto finally;
3474 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003475
Tim Peterscba30e22003-02-01 06:24:36 +00003476 if (!( py_float = PyFloat_FromDouble(d)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003477 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003478
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003479 free(s);
3480 PDATA_PUSH(self->stack, py_float, -1);
3481 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003482
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003483 finally:
3484 free(s);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003485
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003486 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003487}
3488
Guido van Rossum60456fd1997-04-09 17:36:32 +00003489static int
Tim Peterscba30e22003-02-01 06:24:36 +00003490load_binfloat(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003491{
Tim Peters9905b942003-03-20 20:53:32 +00003492 PyObject *py_float;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003493 double x;
3494 char *p;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003495
Tim Peters0bc93f52003-02-02 18:29:33 +00003496 if (self->read_func(self, &p, 8) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003497 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003498
Tim Peters9905b942003-03-20 20:53:32 +00003499 x = _PyFloat_Unpack8((unsigned char *)p, 0);
3500 if (x == -1.0 && PyErr_Occurred())
3501 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003502
Tim Peters9905b942003-03-20 20:53:32 +00003503 py_float = PyFloat_FromDouble(x);
3504 if (py_float == NULL)
3505 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003506
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003507 PDATA_PUSH(self->stack, py_float, -1);
3508 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003509}
Guido van Rossum60456fd1997-04-09 17:36:32 +00003510
3511static int
Tim Peterscba30e22003-02-01 06:24:36 +00003512load_string(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003513{
3514 PyObject *str = 0;
Martin v. Löwis8a8da792002-08-14 07:46:28 +00003515 int len, res = -1;
3516 char *s, *p;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003517
Tim Peters0bc93f52003-02-02 18:29:33 +00003518 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003519 if (len < 2) return bad_readline();
3520 if (!( s=pystrndup(s,len))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003521
Martin v. Löwis8a8da792002-08-14 07:46:28 +00003522
3523 /* Strip outermost quotes */
3524 while (s[len-1] <= ' ')
3525 len--;
3526 if(s[0]=='"' && s[len-1]=='"'){
3527 s[len-1] = '\0';
3528 p = s + 1 ;
3529 len -= 2;
3530 } else if(s[0]=='\'' && s[len-1]=='\''){
3531 s[len-1] = '\0';
3532 p = s + 1 ;
3533 len -= 2;
3534 } else
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003535 goto insecure;
3536 /********************************************/
Guido van Rossum9716aaa1997-12-08 15:15:16 +00003537
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003538 str = PyString_DecodeEscape(p, len, NULL, 0, NULL);
Neal Norwitz99dfe3c2006-08-02 04:27:11 +00003539 free(s);
Martin v. Löwis8a8da792002-08-14 07:46:28 +00003540 if (str) {
3541 PDATA_PUSH(self->stack, str, -1);
3542 res = 0;
3543 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003544 return res;
Tim Peters84e87f32001-03-17 04:50:51 +00003545
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003546 insecure:
3547 free(s);
3548 PyErr_SetString(PyExc_ValueError,"insecure string pickle");
3549 return -1;
Tim Peters84e87f32001-03-17 04:50:51 +00003550}
Guido van Rossum60456fd1997-04-09 17:36:32 +00003551
3552
3553static int
Tim Peterscba30e22003-02-01 06:24:36 +00003554load_binstring(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003555{
3556 PyObject *py_string = 0;
3557 long l;
3558 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003559
Tim Peters0bc93f52003-02-02 18:29:33 +00003560 if (self->read_func(self, &s, 4) < 0) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003561
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003562 l = calc_binint(s, 4);
Gregory P. Smith9d534572008-06-11 07:41:16 +00003563 if (l < 0) {
3564 /* Corrupt or hostile pickle -- we never write one like
3565 * this.
3566 */
3567 PyErr_SetString(UnpicklingError,
3568 "BINSTRING pickle has negative byte count");
3569 return -1;
3570 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003571
Tim Peters0bc93f52003-02-02 18:29:33 +00003572 if (self->read_func(self, &s, l) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003573 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003574
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003575 if (!( py_string = PyString_FromStringAndSize(s, l)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003576 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003577
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003578 PDATA_PUSH(self->stack, py_string, -1);
3579 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003580}
3581
3582
3583static int
Tim Peterscba30e22003-02-01 06:24:36 +00003584load_short_binstring(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003585{
3586 PyObject *py_string = 0;
3587 unsigned char l;
3588 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003589
Tim Peters0bc93f52003-02-02 18:29:33 +00003590 if (self->read_func(self, &s, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003591 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003592
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003593 l = (unsigned char)s[0];
Guido van Rossum60456fd1997-04-09 17:36:32 +00003594
Tim Peters0bc93f52003-02-02 18:29:33 +00003595 if (self->read_func(self, &s, l) < 0) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003596
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003597 if (!( py_string = PyString_FromStringAndSize(s, l))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003598
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003599 PDATA_PUSH(self->stack, py_string, -1);
3600 return 0;
Tim Peters84e87f32001-03-17 04:50:51 +00003601}
Guido van Rossum60456fd1997-04-09 17:36:32 +00003602
3603
Martin v. Löwis339d0f72001-08-17 18:39:25 +00003604#ifdef Py_USING_UNICODE
Guido van Rossum60456fd1997-04-09 17:36:32 +00003605static int
Tim Peterscba30e22003-02-01 06:24:36 +00003606load_unicode(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003607{
3608 PyObject *str = 0;
3609 int len, res = -1;
3610 char *s;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003611
Tim Peters0bc93f52003-02-02 18:29:33 +00003612 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003613 if (len < 1) return bad_readline();
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003614
Tim Peterscba30e22003-02-01 06:24:36 +00003615 if (!( str = PyUnicode_DecodeRawUnicodeEscape(s, len - 1, NULL)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003616 goto finally;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003617
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003618 PDATA_PUSH(self->stack, str, -1);
3619 return 0;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003620
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003621 finally:
3622 return res;
Tim Peters84e87f32001-03-17 04:50:51 +00003623}
Martin v. Löwis339d0f72001-08-17 18:39:25 +00003624#endif
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003625
3626
Martin v. Löwis339d0f72001-08-17 18:39:25 +00003627#ifdef Py_USING_UNICODE
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003628static int
Tim Peterscba30e22003-02-01 06:24:36 +00003629load_binunicode(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003630{
3631 PyObject *unicode;
3632 long l;
3633 char *s;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003634
Tim Peters0bc93f52003-02-02 18:29:33 +00003635 if (self->read_func(self, &s, 4) < 0) return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003636
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003637 l = calc_binint(s, 4);
Gregory P. Smith9d534572008-06-11 07:41:16 +00003638 if (l < 0) {
3639 /* Corrupt or hostile pickle -- we never write one like
3640 * this.
3641 */
3642 PyErr_SetString(UnpicklingError,
3643 "BINUNICODE pickle has negative byte count");
3644 return -1;
3645 }
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003646
Tim Peters0bc93f52003-02-02 18:29:33 +00003647 if (self->read_func(self, &s, l) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003648 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003649
Tim Peterscba30e22003-02-01 06:24:36 +00003650 if (!( unicode = PyUnicode_DecodeUTF8(s, l, NULL)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003651 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003652
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003653 PDATA_PUSH(self->stack, unicode, -1);
3654 return 0;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003655}
Martin v. Löwis339d0f72001-08-17 18:39:25 +00003656#endif
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003657
3658
3659static int
Tim Peterscba30e22003-02-01 06:24:36 +00003660load_tuple(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003661{
3662 PyObject *tup;
3663 int i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003664
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003665 if ((i = marker(self)) < 0) return -1;
3666 if (!( tup=Pdata_popTuple(self->stack, i))) return -1;
3667 PDATA_PUSH(self->stack, tup, -1);
3668 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003669}
3670
3671static int
Tim Peters1d63c9f2003-02-02 20:29:39 +00003672load_counted_tuple(Unpicklerobject *self, int len)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003673{
Tim Peters1d63c9f2003-02-02 20:29:39 +00003674 PyObject *tup = PyTuple_New(len);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003675
Tim Peters1d63c9f2003-02-02 20:29:39 +00003676 if (tup == NULL)
3677 return -1;
3678
3679 while (--len >= 0) {
3680 PyObject *element;
3681
3682 PDATA_POP(self->stack, element);
3683 if (element == NULL)
3684 return -1;
3685 PyTuple_SET_ITEM(tup, len, element);
3686 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003687 PDATA_PUSH(self->stack, tup, -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_list(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003693{
3694 PyObject *list;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003695
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003696 if (!( list=PyList_New(0))) return -1;
3697 PDATA_PUSH(self->stack, list, -1);
3698 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003699}
3700
3701static int
Tim Peterscba30e22003-02-01 06:24:36 +00003702load_empty_dict(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003703{
3704 PyObject *dict;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003705
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003706 if (!( dict=PyDict_New())) return -1;
3707 PDATA_PUSH(self->stack, dict, -1);
3708 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003709}
3710
3711
3712static int
Tim Peterscba30e22003-02-01 06:24:36 +00003713load_list(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003714{
3715 PyObject *list = 0;
3716 int i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003717
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003718 if ((i = marker(self)) < 0) return -1;
3719 if (!( list=Pdata_popList(self->stack, i))) return -1;
3720 PDATA_PUSH(self->stack, list, -1);
3721 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003722}
3723
3724static int
Tim Peterscba30e22003-02-01 06:24:36 +00003725load_dict(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003726{
3727 PyObject *dict, *key, *value;
3728 int i, j, k;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003729
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003730 if ((i = marker(self)) < 0) return -1;
3731 j=self->stack->length;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003732
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003733 if (!( dict = PyDict_New())) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003734
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003735 for (k = i+1; k < j; k += 2) {
3736 key =self->stack->data[k-1];
3737 value=self->stack->data[k ];
3738 if (PyDict_SetItem(dict, key, value) < 0) {
3739 Py_DECREF(dict);
3740 return -1;
3741 }
3742 }
3743 Pdata_clear(self->stack, i);
3744 PDATA_PUSH(self->stack, dict, -1);
3745 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003746}
3747
3748static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00003749Instance_New(PyObject *cls, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003750{
Tim Peters1f1b2d22003-02-01 02:16:37 +00003751 PyObject *r = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003752
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003753 if (PyClass_Check(cls)) {
3754 int l;
Tim Peters84e87f32001-03-17 04:50:51 +00003755
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003756 if ((l=PyObject_Size(args)) < 0) goto err;
3757 if (!( l )) {
3758 PyObject *__getinitargs__;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003759
Tim Peterscba30e22003-02-01 06:24:36 +00003760 __getinitargs__ = PyObject_GetAttr(cls,
Jeremy Hyltonf00368f2003-06-16 20:19:49 +00003761 __getinitargs___str);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003762 if (!__getinitargs__) {
Tim Peterscba30e22003-02-01 06:24:36 +00003763 /* We have a class with no __getinitargs__,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003764 so bypass usual construction */
3765 PyObject *inst;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003766
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003767 PyErr_Clear();
Tim Peterscba30e22003-02-01 06:24:36 +00003768 if (!( inst=PyInstance_NewRaw(cls, NULL)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003769 goto err;
3770 return inst;
3771 }
3772 Py_DECREF(__getinitargs__);
3773 }
Tim Peters84e87f32001-03-17 04:50:51 +00003774
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003775 if ((r=PyInstance_New(cls, args, NULL))) return r;
3776 else goto err;
3777 }
Tim Peters84e87f32001-03-17 04:50:51 +00003778
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003779 if ((r=PyObject_CallObject(cls, args))) return r;
Guido van Rossum142eeb81997-08-13 03:14:41 +00003780
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003781 err:
3782 {
Neal Norwitz0f8b31a2006-06-28 06:28:31 +00003783 PyObject *tp, *v, *tb, *tmp_value;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003784
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003785 PyErr_Fetch(&tp, &v, &tb);
Neal Norwitz0f8b31a2006-06-28 06:28:31 +00003786 tmp_value = v;
3787 /* NULL occurs when there was a KeyboardInterrupt */
3788 if (tmp_value == NULL)
3789 tmp_value = Py_None;
3790 if ((r = PyTuple_Pack(3, tmp_value, cls, args))) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003791 Py_XDECREF(v);
3792 v=r;
3793 }
3794 PyErr_Restore(tp,v,tb);
3795 }
3796 return NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003797}
Tim Peters84e87f32001-03-17 04:50:51 +00003798
Guido van Rossum60456fd1997-04-09 17:36:32 +00003799
3800static int
Tim Peterscba30e22003-02-01 06:24:36 +00003801load_obj(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003802{
3803 PyObject *class, *tup, *obj=0;
3804 int i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003805
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003806 if ((i = marker(self)) < 0) return -1;
3807 if (!( tup=Pdata_popTuple(self->stack, i+1))) return -1;
3808 PDATA_POP(self->stack, class);
3809 if (class) {
3810 obj = Instance_New(class, tup);
3811 Py_DECREF(class);
3812 }
3813 Py_DECREF(tup);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003814
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003815 if (! obj) return -1;
3816 PDATA_PUSH(self->stack, obj, -1);
3817 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003818}
3819
3820
3821static int
Tim Peterscba30e22003-02-01 06:24:36 +00003822load_inst(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003823{
3824 PyObject *tup, *class=0, *obj=0, *module_name, *class_name;
3825 int i, len;
3826 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003827
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003828 if ((i = marker(self)) < 0) return -1;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003829
Tim Peters0bc93f52003-02-02 18:29:33 +00003830 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003831 if (len < 2) return bad_readline();
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003832 module_name = PyString_FromStringAndSize(s, len - 1);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003833 if (!module_name) return -1;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003834
Tim Peters0bc93f52003-02-02 18:29:33 +00003835 if ((len = self->readline_func(self, &s)) >= 0) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003836 if (len < 2) return bad_readline();
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003837 if ((class_name = PyString_FromStringAndSize(s, len - 1))) {
Tim Peterscba30e22003-02-01 06:24:36 +00003838 class = find_class(module_name, class_name,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003839 self->find_class);
3840 Py_DECREF(class_name);
3841 }
3842 }
3843 Py_DECREF(module_name);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003844
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003845 if (! class) return -1;
Tim Peters84e87f32001-03-17 04:50:51 +00003846
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003847 if ((tup=Pdata_popTuple(self->stack, i))) {
3848 obj = Instance_New(class, tup);
3849 Py_DECREF(tup);
3850 }
3851 Py_DECREF(class);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003852
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003853 if (! obj) return -1;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003854
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003855 PDATA_PUSH(self->stack, obj, -1);
3856 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003857}
3858
Tim Peterseab7db32003-02-13 18:24:14 +00003859static int
3860load_newobj(Unpicklerobject *self)
3861{
3862 PyObject *args = NULL;
3863 PyObject *clsraw = NULL;
3864 PyTypeObject *cls; /* clsraw cast to its true type */
3865 PyObject *obj;
3866
3867 /* Stack is ... cls argtuple, and we want to call
3868 * cls.__new__(cls, *argtuple).
3869 */
3870 PDATA_POP(self->stack, args);
3871 if (args == NULL) goto Fail;
3872 if (! PyTuple_Check(args)) {
3873 PyErr_SetString(UnpicklingError, "NEWOBJ expected an arg "
3874 "tuple.");
3875 goto Fail;
3876 }
3877
3878 PDATA_POP(self->stack, clsraw);
3879 cls = (PyTypeObject *)clsraw;
3880 if (cls == NULL) goto Fail;
3881 if (! PyType_Check(cls)) {
3882 PyErr_SetString(UnpicklingError, "NEWOBJ class argument "
3883 "isn't a type object");
3884 goto Fail;
3885 }
3886 if (cls->tp_new == NULL) {
3887 PyErr_SetString(UnpicklingError, "NEWOBJ class argument "
3888 "has NULL tp_new");
3889 goto Fail;
3890 }
3891
3892 /* Call __new__. */
3893 obj = cls->tp_new(cls, args, NULL);
3894 if (obj == NULL) goto Fail;
3895
3896 Py_DECREF(args);
3897 Py_DECREF(clsraw);
3898 PDATA_PUSH(self->stack, obj, -1);
3899 return 0;
3900
3901 Fail:
3902 Py_XDECREF(args);
3903 Py_XDECREF(clsraw);
3904 return -1;
3905}
Guido van Rossum60456fd1997-04-09 17:36:32 +00003906
3907static int
Tim Peterscba30e22003-02-01 06:24:36 +00003908load_global(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003909{
3910 PyObject *class = 0, *module_name = 0, *class_name = 0;
3911 int len;
3912 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003913
Tim Peters0bc93f52003-02-02 18:29:33 +00003914 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003915 if (len < 2) return bad_readline();
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003916 module_name = PyString_FromStringAndSize(s, len - 1);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003917 if (!module_name) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003918
Tim Peters0bc93f52003-02-02 18:29:33 +00003919 if ((len = self->readline_func(self, &s)) >= 0) {
Tim Peters4e52ca82002-12-07 02:43:28 +00003920 if (len < 2) {
3921 Py_DECREF(module_name);
3922 return bad_readline();
3923 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003924 if ((class_name = PyString_FromStringAndSize(s, len - 1))) {
Tim Peterscba30e22003-02-01 06:24:36 +00003925 class = find_class(module_name, class_name,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003926 self->find_class);
3927 Py_DECREF(class_name);
3928 }
3929 }
3930 Py_DECREF(module_name);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003931
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003932 if (! class) return -1;
3933 PDATA_PUSH(self->stack, class, -1);
3934 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003935}
3936
3937
3938static int
Tim Peterscba30e22003-02-01 06:24:36 +00003939load_persid(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003940{
3941 PyObject *pid = 0;
3942 int len;
3943 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003944
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003945 if (self->pers_func) {
Tim Peters0bc93f52003-02-02 18:29:33 +00003946 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003947 if (len < 2) return bad_readline();
3948
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003949 pid = PyString_FromStringAndSize(s, len - 1);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003950 if (!pid) return -1;
3951
3952 if (PyList_Check(self->pers_func)) {
3953 if (PyList_Append(self->pers_func, pid) < 0) {
3954 Py_DECREF(pid);
3955 return -1;
3956 }
3957 }
3958 else {
3959 ARG_TUP(self, pid);
3960 if (self->arg) {
Tim Peterscba30e22003-02-01 06:24:36 +00003961 pid = PyObject_Call(self->pers_func, self->arg,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003962 NULL);
3963 FREE_ARG_TUP(self);
3964 }
3965 }
3966
3967 if (! pid) return -1;
3968
3969 PDATA_PUSH(self->stack, pid, -1);
3970 return 0;
3971 }
3972 else {
3973 PyErr_SetString(UnpicklingError,
3974 "A load persistent id instruction was encountered,\n"
3975 "but no persistent_load function was specified.");
3976 return -1;
3977 }
3978}
3979
3980static int
Tim Peterscba30e22003-02-01 06:24:36 +00003981load_binpersid(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003982{
3983 PyObject *pid = 0;
3984
3985 if (self->pers_func) {
3986 PDATA_POP(self->stack, pid);
3987 if (! pid) return -1;
3988
3989 if (PyList_Check(self->pers_func)) {
3990 if (PyList_Append(self->pers_func, pid) < 0) {
3991 Py_DECREF(pid);
3992 return -1;
3993 }
3994 }
3995 else {
3996 ARG_TUP(self, pid);
3997 if (self->arg) {
3998 pid = PyObject_Call(self->pers_func, self->arg,
3999 NULL);
4000 FREE_ARG_TUP(self);
4001 }
4002 if (! pid) return -1;
4003 }
4004
4005 PDATA_PUSH(self->stack, pid, -1);
4006 return 0;
4007 }
4008 else {
4009 PyErr_SetString(UnpicklingError,
4010 "A load persistent id instruction was encountered,\n"
4011 "but no persistent_load function was specified.");
4012 return -1;
4013 }
4014}
4015
4016
4017static int
Tim Peterscba30e22003-02-01 06:24:36 +00004018load_pop(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004019{
4020 int len;
4021
4022 if (!( (len=self->stack->length) > 0 )) return stackUnderflow();
4023
4024 /* Note that we split the (pickle.py) stack into two stacks,
4025 an object stack and a mark stack. We have to be clever and
4026 pop the right one. We do this by looking at the top of the
4027 mark stack.
4028 */
4029
4030 if ((self->num_marks > 0) &&
4031 (self->marks[self->num_marks - 1] == len))
4032 self->num_marks--;
4033 else {
4034 len--;
4035 Py_DECREF(self->stack->data[len]);
4036 self->stack->length=len;
4037 }
4038
4039 return 0;
4040}
4041
4042
4043static int
Tim Peterscba30e22003-02-01 06:24:36 +00004044load_pop_mark(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004045{
4046 int i;
4047
4048 if ((i = marker(self)) < 0)
4049 return -1;
4050
4051 Pdata_clear(self->stack, i);
4052
4053 return 0;
4054}
4055
4056
4057static int
Tim Peterscba30e22003-02-01 06:24:36 +00004058load_dup(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004059{
4060 PyObject *last;
4061 int len;
4062
4063 if ((len = self->stack->length) <= 0) return stackUnderflow();
4064 last=self->stack->data[len-1];
4065 Py_INCREF(last);
4066 PDATA_PUSH(self->stack, last, -1);
4067 return 0;
4068}
4069
4070
4071static int
Tim Peterscba30e22003-02-01 06:24:36 +00004072load_get(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004073{
4074 PyObject *py_str = 0, *value = 0;
4075 int len;
4076 char *s;
4077 int rc;
4078
Tim Peters0bc93f52003-02-02 18:29:33 +00004079 if ((len = self->readline_func(self, &s)) < 0) return -1;
Guido van Rossumf9ffb031999-02-04 14:54:04 +00004080 if (len < 2) return bad_readline();
Tim Peters84e87f32001-03-17 04:50:51 +00004081
Gregory P. Smithdd96db62008-06-09 04:58:54 +00004082 if (!( py_str = PyString_FromStringAndSize(s, len - 1))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004083
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004084 value = PyDict_GetItem(self->memo, py_str);
4085 if (! value) {
4086 PyErr_SetObject(BadPickleGet, py_str);
4087 rc = -1;
Tim Peters92c8bb32003-02-13 23:00:26 +00004088 }
4089 else {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004090 PDATA_APPEND(self->stack, value, -1);
4091 rc = 0;
4092 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00004093
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004094 Py_DECREF(py_str);
4095 return rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004096}
4097
4098
4099static int
Tim Peterscba30e22003-02-01 06:24:36 +00004100load_binget(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004101{
4102 PyObject *py_key = 0, *value = 0;
4103 unsigned char key;
4104 char *s;
4105 int rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004106
Tim Peters0bc93f52003-02-02 18:29:33 +00004107 if (self->read_func(self, &s, 1) < 0) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004108
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004109 key = (unsigned char)s[0];
4110 if (!( py_key = PyInt_FromLong((long)key))) return -1;
Guido van Rossumea2b7152000-05-09 18:14:50 +00004111
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004112 value = PyDict_GetItem(self->memo, py_key);
4113 if (! value) {
4114 PyErr_SetObject(BadPickleGet, py_key);
4115 rc = -1;
Tim Peters92c8bb32003-02-13 23:00:26 +00004116 }
4117 else {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004118 PDATA_APPEND(self->stack, value, -1);
4119 rc = 0;
4120 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00004121
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004122 Py_DECREF(py_key);
4123 return rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004124}
4125
4126
4127static int
Tim Peterscba30e22003-02-01 06:24:36 +00004128load_long_binget(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004129{
4130 PyObject *py_key = 0, *value = 0;
4131 unsigned char c;
4132 char *s;
4133 long key;
4134 int rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004135
Tim Peters0bc93f52003-02-02 18:29:33 +00004136 if (self->read_func(self, &s, 4) < 0) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004137
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004138 c = (unsigned char)s[0];
4139 key = (long)c;
4140 c = (unsigned char)s[1];
4141 key |= (long)c << 8;
4142 c = (unsigned char)s[2];
4143 key |= (long)c << 16;
4144 c = (unsigned char)s[3];
4145 key |= (long)c << 24;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004146
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004147 if (!( py_key = PyInt_FromLong((long)key))) return -1;
4148
4149 value = PyDict_GetItem(self->memo, py_key);
4150 if (! value) {
4151 PyErr_SetObject(BadPickleGet, py_key);
4152 rc = -1;
Tim Peters92c8bb32003-02-13 23:00:26 +00004153 }
4154 else {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004155 PDATA_APPEND(self->stack, value, -1);
4156 rc = 0;
4157 }
4158
4159 Py_DECREF(py_key);
4160 return rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004161}
4162
Tim Peters2d629652003-02-04 05:06:17 +00004163/* Push an object from the extension registry (EXT[124]). nbytes is
4164 * the number of bytes following the opcode, holding the index (code) value.
4165 */
4166static int
4167load_extension(Unpicklerobject *self, int nbytes)
4168{
4169 char *codebytes; /* the nbytes bytes after the opcode */
4170 long code; /* calc_binint returns long */
4171 PyObject *py_code; /* code as a Python int */
4172 PyObject *obj; /* the object to push */
4173 PyObject *pair; /* (module_name, class_name) */
4174 PyObject *module_name, *class_name;
4175
4176 assert(nbytes == 1 || nbytes == 2 || nbytes == 4);
4177 if (self->read_func(self, &codebytes, nbytes) < 0) return -1;
4178 code = calc_binint(codebytes, nbytes);
4179 if (code <= 0) { /* note that 0 is forbidden */
4180 /* Corrupt or hostile pickle. */
4181 PyErr_SetString(UnpicklingError, "EXT specifies code <= 0");
4182 return -1;
4183 }
4184
4185 /* Look for the code in the cache. */
4186 py_code = PyInt_FromLong(code);
4187 if (py_code == NULL) return -1;
4188 obj = PyDict_GetItem(extension_cache, py_code);
4189 if (obj != NULL) {
4190 /* Bingo. */
4191 Py_DECREF(py_code);
4192 PDATA_APPEND(self->stack, obj, -1);
4193 return 0;
4194 }
4195
4196 /* Look up the (module_name, class_name) pair. */
4197 pair = PyDict_GetItem(inverted_registry, py_code);
4198 if (pair == NULL) {
4199 Py_DECREF(py_code);
4200 PyErr_Format(PyExc_ValueError, "unregistered extension "
4201 "code %ld", code);
4202 return -1;
4203 }
4204 /* Since the extension registry is manipulable via Python code,
Tim Petersfa05ce32003-02-04 05:20:32 +00004205 * confirm that pair is really a 2-tuple of strings.
Tim Peters2d629652003-02-04 05:06:17 +00004206 */
4207 if (!PyTuple_Check(pair) || PyTuple_Size(pair) != 2 ||
Gregory P. Smithdd96db62008-06-09 04:58:54 +00004208 !PyString_Check(module_name = PyTuple_GET_ITEM(pair, 0)) ||
4209 !PyString_Check(class_name = PyTuple_GET_ITEM(pair, 1))) {
Tim Peters2d629652003-02-04 05:06:17 +00004210 Py_DECREF(py_code);
4211 PyErr_Format(PyExc_ValueError, "_inverted_registry[%ld] "
4212 "isn't a 2-tuple of strings", code);
4213 return -1;
4214 }
4215 /* Load the object. */
4216 obj = find_class(module_name, class_name, self->find_class);
4217 if (obj == NULL) {
4218 Py_DECREF(py_code);
4219 return -1;
4220 }
4221 /* Cache code -> obj. */
4222 code = PyDict_SetItem(extension_cache, py_code, obj);
4223 Py_DECREF(py_code);
4224 if (code < 0) {
4225 Py_DECREF(obj);
4226 return -1;
4227 }
4228 PDATA_PUSH(self->stack, obj, -1);
4229 return 0;
4230}
Guido van Rossum60456fd1997-04-09 17:36:32 +00004231
4232static int
Tim Peterscba30e22003-02-01 06:24:36 +00004233load_put(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004234{
4235 PyObject *py_str = 0, *value = 0;
4236 int len, l;
4237 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004238
Tim Peters0bc93f52003-02-02 18:29:33 +00004239 if ((l = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004240 if (l < 2) return bad_readline();
4241 if (!( len=self->stack->length )) return stackUnderflow();
Gregory P. Smithdd96db62008-06-09 04:58:54 +00004242 if (!( py_str = PyString_FromStringAndSize(s, l - 1))) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004243 value=self->stack->data[len-1];
4244 l=PyDict_SetItem(self->memo, py_str, value);
4245 Py_DECREF(py_str);
4246 return l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004247}
4248
4249
4250static int
Tim Peterscba30e22003-02-01 06:24:36 +00004251load_binput(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004252{
4253 PyObject *py_key = 0, *value = 0;
4254 unsigned char key;
4255 char *s;
4256 int len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004257
Tim Peters0bc93f52003-02-02 18:29:33 +00004258 if (self->read_func(self, &s, 1) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004259 if (!( (len=self->stack->length) > 0 )) return stackUnderflow();
Guido van Rossum60456fd1997-04-09 17:36:32 +00004260
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004261 key = (unsigned char)s[0];
Guido van Rossum053b8df1998-11-25 16:18:00 +00004262
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004263 if (!( py_key = PyInt_FromLong((long)key))) return -1;
4264 value=self->stack->data[len-1];
4265 len=PyDict_SetItem(self->memo, py_key, value);
4266 Py_DECREF(py_key);
4267 return len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004268}
4269
4270
4271static int
Tim Peterscba30e22003-02-01 06:24:36 +00004272load_long_binput(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004273{
4274 PyObject *py_key = 0, *value = 0;
4275 long key;
4276 unsigned char c;
4277 char *s;
4278 int len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004279
Tim Peters0bc93f52003-02-02 18:29:33 +00004280 if (self->read_func(self, &s, 4) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004281 if (!( len=self->stack->length )) return stackUnderflow();
Guido van Rossum60456fd1997-04-09 17:36:32 +00004282
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004283 c = (unsigned char)s[0];
4284 key = (long)c;
4285 c = (unsigned char)s[1];
4286 key |= (long)c << 8;
4287 c = (unsigned char)s[2];
4288 key |= (long)c << 16;
4289 c = (unsigned char)s[3];
4290 key |= (long)c << 24;
Tim Peters84e87f32001-03-17 04:50:51 +00004291
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004292 if (!( py_key = PyInt_FromLong(key))) return -1;
4293 value=self->stack->data[len-1];
4294 len=PyDict_SetItem(self->memo, py_key, value);
4295 Py_DECREF(py_key);
4296 return len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004297}
4298
4299
4300static int
Tim Peterscba30e22003-02-01 06:24:36 +00004301do_append(Unpicklerobject *self, int x)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004302{
4303 PyObject *value = 0, *list = 0, *append_method = 0;
4304 int len, i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004305
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004306 len=self->stack->length;
4307 if (!( len >= x && x > 0 )) return stackUnderflow();
4308 /* nothing to do */
Tim Peterscba30e22003-02-01 06:24:36 +00004309 if (len==x) return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004310
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004311 list=self->stack->data[x-1];
Guido van Rossum60456fd1997-04-09 17:36:32 +00004312
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004313 if (PyList_Check(list)) {
4314 PyObject *slice;
4315 int list_len;
Tim Peters84e87f32001-03-17 04:50:51 +00004316
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004317 slice=Pdata_popList(self->stack, x);
Neal Norwitzb59d08c2006-07-22 16:20:49 +00004318 if (! slice) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004319 list_len = PyList_GET_SIZE(list);
4320 i=PyList_SetSlice(list, list_len, list_len, slice);
4321 Py_DECREF(slice);
4322 return i;
4323 }
4324 else {
Guido van Rossum60456fd1997-04-09 17:36:32 +00004325
Tim Peterscba30e22003-02-01 06:24:36 +00004326 if (!( append_method = PyObject_GetAttr(list, append_str)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004327 return -1;
4328
4329 for (i = x; i < len; i++) {
4330 PyObject *junk;
4331
4332 value=self->stack->data[i];
4333 junk=0;
4334 ARG_TUP(self, value);
4335 if (self->arg) {
Tim Peterscba30e22003-02-01 06:24:36 +00004336 junk = PyObject_Call(append_method, self->arg,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004337 NULL);
4338 FREE_ARG_TUP(self);
4339 }
4340 if (! junk) {
4341 Pdata_clear(self->stack, i+1);
4342 self->stack->length=x;
4343 Py_DECREF(append_method);
4344 return -1;
4345 }
4346 Py_DECREF(junk);
4347 }
4348 self->stack->length=x;
4349 Py_DECREF(append_method);
4350 }
4351
4352 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004353}
4354
4355
4356static int
Tim Peterscba30e22003-02-01 06:24:36 +00004357load_append(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004358{
4359 return do_append(self, self->stack->length - 1);
Guido van Rossum60456fd1997-04-09 17:36:32 +00004360}
4361
4362
4363static int
Tim Peterscba30e22003-02-01 06:24:36 +00004364load_appends(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004365{
4366 return do_append(self, marker(self));
Guido van Rossum60456fd1997-04-09 17:36:32 +00004367}
4368
4369
4370static int
Tim Peterscba30e22003-02-01 06:24:36 +00004371do_setitems(Unpicklerobject *self, int x)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004372{
4373 PyObject *value = 0, *key = 0, *dict = 0;
4374 int len, i, r=0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004375
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004376 if (!( (len=self->stack->length) >= x
4377 && x > 0 )) return stackUnderflow();
Guido van Rossum60456fd1997-04-09 17:36:32 +00004378
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004379 dict=self->stack->data[x-1];
Guido van Rossum60456fd1997-04-09 17:36:32 +00004380
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004381 for (i = x+1; i < len; i += 2) {
4382 key =self->stack->data[i-1];
4383 value=self->stack->data[i ];
4384 if (PyObject_SetItem(dict, key, value) < 0) {
4385 r=-1;
4386 break;
4387 }
4388 }
4389
4390 Pdata_clear(self->stack, x);
4391
4392 return r;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004393}
4394
4395
Tim Peters84e87f32001-03-17 04:50:51 +00004396static int
Tim Peterscba30e22003-02-01 06:24:36 +00004397load_setitem(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004398{
4399 return do_setitems(self, self->stack->length - 2);
4400}
Guido van Rossum60456fd1997-04-09 17:36:32 +00004401
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004402static int
Tim Peterscba30e22003-02-01 06:24:36 +00004403load_setitems(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004404{
4405 return do_setitems(self, marker(self));
Guido van Rossum60456fd1997-04-09 17:36:32 +00004406}
4407
Tim Peters84e87f32001-03-17 04:50:51 +00004408
Guido van Rossum60456fd1997-04-09 17:36:32 +00004409static int
Tim Peterscba30e22003-02-01 06:24:36 +00004410load_build(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004411{
Tim Peters080c88b2003-02-15 03:01:11 +00004412 PyObject *state, *inst, *slotstate;
4413 PyObject *__setstate__;
4414 PyObject *d_key, *d_value;
Martin v. Löwis18e16552006-02-15 17:27:45 +00004415 Py_ssize_t i;
Tim Peters080c88b2003-02-15 03:01:11 +00004416 int res = -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004417
Tim Peters080c88b2003-02-15 03:01:11 +00004418 /* Stack is ... instance, state. We want to leave instance at
4419 * the stack top, possibly mutated via instance.__setstate__(state).
4420 */
4421 if (self->stack->length < 2)
4422 return stackUnderflow();
4423 PDATA_POP(self->stack, state);
4424 if (state == NULL)
4425 return -1;
4426 inst = self->stack->data[self->stack->length - 1];
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004427
Tim Peters080c88b2003-02-15 03:01:11 +00004428 __setstate__ = PyObject_GetAttr(inst, __setstate___str);
4429 if (__setstate__ != NULL) {
4430 PyObject *junk = NULL;
4431
4432 /* The explicit __setstate__ is responsible for everything. */
4433 ARG_TUP(self, state);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004434 if (self->arg) {
4435 junk = PyObject_Call(__setstate__, self->arg, NULL);
4436 FREE_ARG_TUP(self);
4437 }
4438 Py_DECREF(__setstate__);
Tim Peters080c88b2003-02-15 03:01:11 +00004439 if (junk == NULL)
4440 return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004441 Py_DECREF(junk);
4442 return 0;
4443 }
Jeremy Hyltonf00368f2003-06-16 20:19:49 +00004444 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4445 return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004446 PyErr_Clear();
Tim Peters080c88b2003-02-15 03:01:11 +00004447
4448 /* A default __setstate__. First see whether state embeds a
4449 * slot state dict too (a proto 2 addition).
4450 */
4451 if (PyTuple_Check(state) && PyTuple_Size(state) == 2) {
4452 PyObject *temp = state;
4453 state = PyTuple_GET_ITEM(temp, 0);
4454 slotstate = PyTuple_GET_ITEM(temp, 1);
4455 Py_INCREF(state);
4456 Py_INCREF(slotstate);
4457 Py_DECREF(temp);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004458 }
Tim Peters080c88b2003-02-15 03:01:11 +00004459 else
4460 slotstate = NULL;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004461
Tim Peters080c88b2003-02-15 03:01:11 +00004462 /* Set inst.__dict__ from the state dict (if any). */
4463 if (state != Py_None) {
4464 PyObject *dict;
4465 if (! PyDict_Check(state)) {
4466 PyErr_SetString(UnpicklingError, "state is not a "
4467 "dictionary");
4468 goto finally;
4469 }
4470 dict = PyObject_GetAttr(inst, __dict___str);
4471 if (dict == NULL)
4472 goto finally;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004473
Tim Peters080c88b2003-02-15 03:01:11 +00004474 i = 0;
4475 while (PyDict_Next(state, &i, &d_key, &d_value)) {
Antoine Pitrou74309892009-05-02 21:13:23 +00004476 /* normally the keys for instance attributes are
4477 interned. we should try to do that here. */
4478 Py_INCREF(d_key);
4479 if (PyString_CheckExact(d_key))
4480 PyString_InternInPlace(&d_key);
4481 if (PyObject_SetItem(dict, d_key, d_value) < 0) {
4482 Py_DECREF(d_key);
Tim Peters080c88b2003-02-15 03:01:11 +00004483 goto finally;
Antoine Pitrou74309892009-05-02 21:13:23 +00004484 }
4485 Py_DECREF(d_key);
Tim Peters080c88b2003-02-15 03:01:11 +00004486 }
4487 Py_DECREF(dict);
4488 }
4489
4490 /* Also set instance attributes from the slotstate dict (if any). */
4491 if (slotstate != NULL) {
4492 if (! PyDict_Check(slotstate)) {
4493 PyErr_SetString(UnpicklingError, "slot state is not "
4494 "a dictionary");
4495 goto finally;
4496 }
4497 i = 0;
4498 while (PyDict_Next(slotstate, &i, &d_key, &d_value)) {
4499 if (PyObject_SetAttr(inst, d_key, d_value) < 0)
4500 goto finally;
4501 }
4502 }
4503 res = 0;
4504
4505 finally:
4506 Py_DECREF(state);
4507 Py_XDECREF(slotstate);
4508 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004509}
4510
4511
4512static int
Tim Peterscba30e22003-02-01 06:24:36 +00004513load_mark(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004514{
4515 int s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004516
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004517 /* Note that we split the (pickle.py) stack into two stacks, an
4518 object stack and a mark stack. Here we push a mark onto the
4519 mark stack.
4520 */
Guido van Rossum60456fd1997-04-09 17:36:32 +00004521
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004522 if ((self->num_marks + 1) >= self->marks_size) {
Kristján Valur Jónsson17b8e972007-04-25 00:10:50 +00004523 int *marks;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004524 s=self->marks_size+20;
4525 if (s <= self->num_marks) s=self->num_marks + 1;
4526 if (self->marks == NULL)
Kristján Valur Jónsson17b8e972007-04-25 00:10:50 +00004527 marks=(int *)malloc(s * sizeof(int));
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004528 else
Kristján Valur Jónsson17b8e972007-04-25 00:10:50 +00004529 marks=(int *)realloc(self->marks,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004530 s * sizeof(int));
Kristján Valur Jónsson17b8e972007-04-25 00:10:50 +00004531 if (!marks) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004532 PyErr_NoMemory();
4533 return -1;
4534 }
Kristján Valur Jónsson17b8e972007-04-25 00:10:50 +00004535 self->marks = marks;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004536 self->marks_size = s;
4537 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00004538
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004539 self->marks[self->num_marks++] = self->stack->length;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004540
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004541 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004542}
4543
Guido van Rossum60456fd1997-04-09 17:36:32 +00004544static int
Tim Peterscba30e22003-02-01 06:24:36 +00004545load_reduce(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004546{
4547 PyObject *callable = 0, *arg_tup = 0, *ob = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004548
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004549 PDATA_POP(self->stack, arg_tup);
4550 if (! arg_tup) return -1;
4551 PDATA_POP(self->stack, callable);
4552 if (callable) {
4553 ob = Instance_New(callable, arg_tup);
4554 Py_DECREF(callable);
4555 }
4556 Py_DECREF(arg_tup);
Guido van Rossum60456fd1997-04-09 17:36:32 +00004557
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004558 if (! ob) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004559
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004560 PDATA_PUSH(self->stack, ob, -1);
4561 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004562}
Tim Peters84e87f32001-03-17 04:50:51 +00004563
Tim Peters4190fb82003-02-02 16:09:05 +00004564/* Just raises an error if we don't know the protocol specified. PROTO
4565 * is the first opcode for protocols >= 2.
4566 */
4567static int
4568load_proto(Unpicklerobject *self)
4569{
4570 int i;
4571 char *protobyte;
4572
4573 i = self->read_func(self, &protobyte, 1);
4574 if (i < 0)
4575 return -1;
4576
4577 i = calc_binint(protobyte, 1);
4578 /* No point checking for < 0, since calc_binint returns an unsigned
4579 * int when chewing on 1 byte.
4580 */
4581 assert(i >= 0);
Tim Peters8587b3c2003-02-13 15:44:41 +00004582 if (i <= HIGHEST_PROTOCOL)
Tim Peters4190fb82003-02-02 16:09:05 +00004583 return 0;
4584
4585 PyErr_Format(PyExc_ValueError, "unsupported pickle protocol: %d", i);
4586 return -1;
4587}
4588
Guido van Rossum60456fd1997-04-09 17:36:32 +00004589static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00004590load(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004591{
4592 PyObject *err = 0, *val = 0;
4593 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004594
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004595 self->num_marks = 0;
4596 if (self->stack->length) Pdata_clear(self->stack, 0);
Guido van Rossum60456fd1997-04-09 17:36:32 +00004597
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004598 while (1) {
Tim Peters0bc93f52003-02-02 18:29:33 +00004599 if (self->read_func(self, &s, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004600 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004601
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004602 switch (s[0]) {
4603 case NONE:
4604 if (load_none(self) < 0)
4605 break;
4606 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004607
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004608 case BININT:
4609 if (load_binint(self) < 0)
4610 break;
4611 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004612
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004613 case BININT1:
4614 if (load_binint1(self) < 0)
4615 break;
4616 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004617
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004618 case BININT2:
4619 if (load_binint2(self) < 0)
4620 break;
4621 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004622
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004623 case INT:
4624 if (load_int(self) < 0)
4625 break;
4626 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004627
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004628 case LONG:
4629 if (load_long(self) < 0)
4630 break;
4631 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004632
Tim Petersee1a53c2003-02-02 02:57:53 +00004633 case LONG1:
4634 if (load_counted_long(self, 1) < 0)
4635 break;
4636 continue;
4637
4638 case LONG4:
4639 if (load_counted_long(self, 4) < 0)
4640 break;
4641 continue;
4642
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004643 case FLOAT:
4644 if (load_float(self) < 0)
4645 break;
4646 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004647
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004648 case BINFLOAT:
4649 if (load_binfloat(self) < 0)
4650 break;
4651 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004652
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004653 case BINSTRING:
4654 if (load_binstring(self) < 0)
4655 break;
4656 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004657
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004658 case SHORT_BINSTRING:
4659 if (load_short_binstring(self) < 0)
4660 break;
4661 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004662
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004663 case STRING:
4664 if (load_string(self) < 0)
4665 break;
4666 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004667
Martin v. Löwis339d0f72001-08-17 18:39:25 +00004668#ifdef Py_USING_UNICODE
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004669 case UNICODE:
4670 if (load_unicode(self) < 0)
4671 break;
4672 continue;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00004673
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004674 case BINUNICODE:
4675 if (load_binunicode(self) < 0)
4676 break;
4677 continue;
Martin v. Löwis339d0f72001-08-17 18:39:25 +00004678#endif
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00004679
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004680 case EMPTY_TUPLE:
Tim Peters1d63c9f2003-02-02 20:29:39 +00004681 if (load_counted_tuple(self, 0) < 0)
4682 break;
4683 continue;
4684
4685 case TUPLE1:
4686 if (load_counted_tuple(self, 1) < 0)
4687 break;
4688 continue;
4689
4690 case TUPLE2:
4691 if (load_counted_tuple(self, 2) < 0)
4692 break;
4693 continue;
4694
4695 case TUPLE3:
4696 if (load_counted_tuple(self, 3) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004697 break;
4698 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004699
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004700 case TUPLE:
4701 if (load_tuple(self) < 0)
4702 break;
4703 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004704
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004705 case EMPTY_LIST:
4706 if (load_empty_list(self) < 0)
4707 break;
4708 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004709
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004710 case LIST:
4711 if (load_list(self) < 0)
4712 break;
4713 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004714
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004715 case EMPTY_DICT:
4716 if (load_empty_dict(self) < 0)
4717 break;
4718 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004719
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004720 case DICT:
4721 if (load_dict(self) < 0)
4722 break;
4723 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004724
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004725 case OBJ:
4726 if (load_obj(self) < 0)
4727 break;
4728 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004729
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004730 case INST:
4731 if (load_inst(self) < 0)
4732 break;
4733 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004734
Tim Peterseab7db32003-02-13 18:24:14 +00004735 case NEWOBJ:
4736 if (load_newobj(self) < 0)
4737 break;
4738 continue;
4739
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004740 case GLOBAL:
4741 if (load_global(self) < 0)
4742 break;
4743 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004744
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004745 case APPEND:
4746 if (load_append(self) < 0)
4747 break;
4748 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004749
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004750 case APPENDS:
4751 if (load_appends(self) < 0)
4752 break;
4753 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00004754
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004755 case BUILD:
4756 if (load_build(self) < 0)
4757 break;
4758 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00004759
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004760 case DUP:
4761 if (load_dup(self) < 0)
4762 break;
4763 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004764
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004765 case BINGET:
4766 if (load_binget(self) < 0)
4767 break;
4768 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004769
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004770 case LONG_BINGET:
4771 if (load_long_binget(self) < 0)
4772 break;
4773 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00004774
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004775 case GET:
4776 if (load_get(self) < 0)
4777 break;
4778 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004779
Tim Peters2d629652003-02-04 05:06:17 +00004780 case EXT1:
4781 if (load_extension(self, 1) < 0)
4782 break;
4783 continue;
4784
4785 case EXT2:
4786 if (load_extension(self, 2) < 0)
4787 break;
4788 continue;
4789
4790 case EXT4:
4791 if (load_extension(self, 4) < 0)
4792 break;
4793 continue;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004794 case MARK:
4795 if (load_mark(self) < 0)
4796 break;
4797 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004798
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004799 case BINPUT:
4800 if (load_binput(self) < 0)
4801 break;
4802 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004803
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004804 case LONG_BINPUT:
4805 if (load_long_binput(self) < 0)
4806 break;
4807 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00004808
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004809 case PUT:
4810 if (load_put(self) < 0)
4811 break;
4812 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004813
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004814 case POP:
4815 if (load_pop(self) < 0)
4816 break;
4817 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004818
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004819 case POP_MARK:
4820 if (load_pop_mark(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 SETITEM:
4825 if (load_setitem(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 SETITEMS:
4830 if (load_setitems(self) < 0)
4831 break;
4832 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004833
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004834 case STOP:
4835 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004836
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004837 case PERSID:
4838 if (load_persid(self) < 0)
4839 break;
4840 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004841
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004842 case BINPERSID:
4843 if (load_binpersid(self) < 0)
4844 break;
4845 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004846
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004847 case REDUCE:
4848 if (load_reduce(self) < 0)
4849 break;
4850 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004851
Tim Peters4190fb82003-02-02 16:09:05 +00004852 case PROTO:
4853 if (load_proto(self) < 0)
4854 break;
4855 continue;
4856
Tim Peters3c67d792003-02-02 17:59:11 +00004857 case NEWTRUE:
4858 if (load_bool(self, Py_True) < 0)
4859 break;
4860 continue;
4861
4862 case NEWFALSE:
4863 if (load_bool(self, Py_False) < 0)
4864 break;
4865 continue;
4866
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004867 case '\0':
4868 /* end of file */
4869 PyErr_SetNone(PyExc_EOFError);
4870 break;
Tim Peterscba30e22003-02-01 06:24:36 +00004871
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004872 default:
Tim Peterscba30e22003-02-01 06:24:36 +00004873 cPickle_ErrFormat(UnpicklingError,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004874 "invalid load key, '%s'.",
4875 "c", s[0]);
4876 return NULL;
4877 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00004878
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004879 break;
4880 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00004881
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004882 if ((err = PyErr_Occurred())) {
4883 if (err == PyExc_EOFError) {
4884 PyErr_SetNone(PyExc_EOFError);
4885 }
4886 return NULL;
4887 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00004888
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004889 PDATA_POP(self->stack, val);
4890 return val;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004891}
Tim Peters84e87f32001-03-17 04:50:51 +00004892
Guido van Rossum60456fd1997-04-09 17:36:32 +00004893
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004894/* No-load functions to support noload, which is used to
4895 find persistent references. */
4896
4897static int
Tim Peterscba30e22003-02-01 06:24:36 +00004898noload_obj(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004899{
4900 int i;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004901
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004902 if ((i = marker(self)) < 0) return -1;
4903 return Pdata_clear(self->stack, i+1);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004904}
4905
4906
4907static int
Tim Peterscba30e22003-02-01 06:24:36 +00004908noload_inst(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004909{
4910 int i;
4911 char *s;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004912
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004913 if ((i = marker(self)) < 0) return -1;
4914 Pdata_clear(self->stack, i);
Tim Peters0bc93f52003-02-02 18:29:33 +00004915 if (self->readline_func(self, &s) < 0) return -1;
4916 if (self->readline_func(self, &s) < 0) return -1;
Tim Peterseab7db32003-02-13 18:24:14 +00004917 PDATA_APPEND(self->stack, Py_None, -1);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004918 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004919}
4920
4921static int
Tim Peterseab7db32003-02-13 18:24:14 +00004922noload_newobj(Unpicklerobject *self)
4923{
4924 PyObject *obj;
4925
4926 PDATA_POP(self->stack, obj); /* pop argtuple */
4927 if (obj == NULL) return -1;
4928 Py_DECREF(obj);
4929
4930 PDATA_POP(self->stack, obj); /* pop cls */
4931 if (obj == NULL) return -1;
4932 Py_DECREF(obj);
4933
4934 PDATA_APPEND(self->stack, Py_None, -1);
4935 return 0;
4936}
4937
4938static int
Tim Peterscba30e22003-02-01 06:24:36 +00004939noload_global(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004940{
4941 char *s;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004942
Tim Peters0bc93f52003-02-02 18:29:33 +00004943 if (self->readline_func(self, &s) < 0) return -1;
4944 if (self->readline_func(self, &s) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004945 PDATA_APPEND(self->stack, Py_None,-1);
4946 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004947}
4948
4949static int
Tim Peterscba30e22003-02-01 06:24:36 +00004950noload_reduce(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004951{
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004952
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004953 if (self->stack->length < 2) return stackUnderflow();
4954 Pdata_clear(self->stack, self->stack->length-2);
4955 PDATA_APPEND(self->stack, Py_None,-1);
4956 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004957}
4958
4959static int
4960noload_build(Unpicklerobject *self) {
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004961
Guido van Rossum053b8df1998-11-25 16:18:00 +00004962 if (self->stack->length < 1) return stackUnderflow();
4963 Pdata_clear(self->stack, self->stack->length-1);
Guido van Rossum50f385c1998-12-04 18:48:44 +00004964 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004965}
4966
Tim Peters2d629652003-02-04 05:06:17 +00004967static int
4968noload_extension(Unpicklerobject *self, int nbytes)
4969{
4970 char *codebytes;
4971
4972 assert(nbytes == 1 || nbytes == 2 || nbytes == 4);
4973 if (self->read_func(self, &codebytes, nbytes) < 0) return -1;
4974 PDATA_APPEND(self->stack, Py_None, -1);
4975 return 0;
4976}
4977
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004978
4979static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00004980noload(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004981{
4982 PyObject *err = 0, *val = 0;
4983 char *s;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004984
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004985 self->num_marks = 0;
4986 Pdata_clear(self->stack, 0);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004987
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004988 while (1) {
Tim Peters0bc93f52003-02-02 18:29:33 +00004989 if (self->read_func(self, &s, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004990 break;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004991
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004992 switch (s[0]) {
4993 case NONE:
4994 if (load_none(self) < 0)
4995 break;
4996 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004997
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004998 case BININT:
4999 if (load_binint(self) < 0)
5000 break;
5001 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005002
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005003 case BININT1:
5004 if (load_binint1(self) < 0)
5005 break;
5006 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005007
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005008 case BININT2:
5009 if (load_binint2(self) < 0)
5010 break;
5011 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005012
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005013 case INT:
5014 if (load_int(self) < 0)
5015 break;
5016 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005017
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005018 case LONG:
5019 if (load_long(self) < 0)
5020 break;
5021 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005022
Tim Peters4190fb82003-02-02 16:09:05 +00005023 case LONG1:
5024 if (load_counted_long(self, 1) < 0)
5025 break;
5026 continue;
5027
5028 case LONG4:
5029 if (load_counted_long(self, 4) < 0)
5030 break;
5031 continue;
5032
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005033 case FLOAT:
5034 if (load_float(self) < 0)
5035 break;
5036 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005037
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005038 case BINFLOAT:
5039 if (load_binfloat(self) < 0)
5040 break;
5041 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005042
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005043 case BINSTRING:
5044 if (load_binstring(self) < 0)
5045 break;
5046 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005047
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005048 case SHORT_BINSTRING:
5049 if (load_short_binstring(self) < 0)
5050 break;
5051 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005052
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005053 case STRING:
5054 if (load_string(self) < 0)
5055 break;
5056 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005057
Martin v. Löwis339d0f72001-08-17 18:39:25 +00005058#ifdef Py_USING_UNICODE
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005059 case UNICODE:
5060 if (load_unicode(self) < 0)
5061 break;
5062 continue;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00005063
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005064 case BINUNICODE:
5065 if (load_binunicode(self) < 0)
5066 break;
5067 continue;
Martin v. Löwis339d0f72001-08-17 18:39:25 +00005068#endif
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00005069
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005070 case EMPTY_TUPLE:
Tim Peters1d63c9f2003-02-02 20:29:39 +00005071 if (load_counted_tuple(self, 0) < 0)
5072 break;
5073 continue;
5074
5075 case TUPLE1:
5076 if (load_counted_tuple(self, 1) < 0)
5077 break;
5078 continue;
5079
5080 case TUPLE2:
5081 if (load_counted_tuple(self, 2) < 0)
5082 break;
5083 continue;
5084
5085 case TUPLE3:
5086 if (load_counted_tuple(self, 3) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005087 break;
5088 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005089
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005090 case TUPLE:
5091 if (load_tuple(self) < 0)
5092 break;
5093 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005094
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005095 case EMPTY_LIST:
5096 if (load_empty_list(self) < 0)
5097 break;
5098 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005099
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005100 case LIST:
5101 if (load_list(self) < 0)
5102 break;
5103 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005104
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005105 case EMPTY_DICT:
5106 if (load_empty_dict(self) < 0)
5107 break;
5108 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005109
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005110 case DICT:
5111 if (load_dict(self) < 0)
5112 break;
5113 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005114
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005115 case OBJ:
5116 if (noload_obj(self) < 0)
5117 break;
5118 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005119
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005120 case INST:
5121 if (noload_inst(self) < 0)
5122 break;
5123 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005124
Tim Peterseab7db32003-02-13 18:24:14 +00005125 case NEWOBJ:
5126 if (noload_newobj(self) < 0)
5127 break;
5128 continue;
5129
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005130 case GLOBAL:
5131 if (noload_global(self) < 0)
5132 break;
5133 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005134
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005135 case APPEND:
5136 if (load_append(self) < 0)
5137 break;
5138 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005139
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005140 case APPENDS:
5141 if (load_appends(self) < 0)
5142 break;
5143 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00005144
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005145 case BUILD:
5146 if (noload_build(self) < 0)
5147 break;
5148 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00005149
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005150 case DUP:
5151 if (load_dup(self) < 0)
5152 break;
5153 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005154
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005155 case BINGET:
5156 if (load_binget(self) < 0)
5157 break;
5158 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005159
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005160 case LONG_BINGET:
5161 if (load_long_binget(self) < 0)
5162 break;
5163 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00005164
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005165 case GET:
5166 if (load_get(self) < 0)
5167 break;
5168 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005169
Tim Peters2d629652003-02-04 05:06:17 +00005170 case EXT1:
5171 if (noload_extension(self, 1) < 0)
5172 break;
5173 continue;
5174
5175 case EXT2:
5176 if (noload_extension(self, 2) < 0)
5177 break;
5178 continue;
5179
5180 case EXT4:
5181 if (noload_extension(self, 4) < 0)
5182 break;
5183 continue;
5184
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005185 case MARK:
5186 if (load_mark(self) < 0)
5187 break;
5188 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005189
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005190 case BINPUT:
5191 if (load_binput(self) < 0)
5192 break;
5193 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005194
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005195 case LONG_BINPUT:
5196 if (load_long_binput(self) < 0)
5197 break;
5198 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00005199
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005200 case PUT:
5201 if (load_put(self) < 0)
5202 break;
5203 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005204
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005205 case POP:
5206 if (load_pop(self) < 0)
5207 break;
5208 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005209
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005210 case POP_MARK:
5211 if (load_pop_mark(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 SETITEM:
5216 if (load_setitem(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 SETITEMS:
5221 if (load_setitems(self) < 0)
5222 break;
5223 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005224
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005225 case STOP:
5226 break;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005227
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005228 case PERSID:
5229 if (load_persid(self) < 0)
5230 break;
5231 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005232
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005233 case BINPERSID:
5234 if (load_binpersid(self) < 0)
5235 break;
5236 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005237
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005238 case REDUCE:
5239 if (noload_reduce(self) < 0)
5240 break;
5241 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005242
Tim Peters4190fb82003-02-02 16:09:05 +00005243 case PROTO:
5244 if (load_proto(self) < 0)
5245 break;
5246 continue;
5247
Tim Peters3c67d792003-02-02 17:59:11 +00005248 case NEWTRUE:
5249 if (load_bool(self, Py_True) < 0)
5250 break;
5251 continue;
5252
5253 case NEWFALSE:
5254 if (load_bool(self, Py_False) < 0)
5255 break;
5256 continue;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005257 default:
Tim Peterscba30e22003-02-01 06:24:36 +00005258 cPickle_ErrFormat(UnpicklingError,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005259 "invalid load key, '%s'.",
5260 "c", s[0]);
5261 return NULL;
5262 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005263
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005264 break;
5265 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005266
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005267 if ((err = PyErr_Occurred())) {
5268 if (err == PyExc_EOFError) {
5269 PyErr_SetNone(PyExc_EOFError);
5270 }
5271 return NULL;
5272 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005273
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005274 PDATA_POP(self->stack, val);
5275 return val;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005276}
Tim Peters84e87f32001-03-17 04:50:51 +00005277
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005278
Guido van Rossum60456fd1997-04-09 17:36:32 +00005279static PyObject *
Georg Brandl96a8c392006-05-29 21:04:52 +00005280Unpickler_load(Unpicklerobject *self, PyObject *unused)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005281{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005282 return load(self);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005283}
5284
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005285static PyObject *
Georg Brandl96a8c392006-05-29 21:04:52 +00005286Unpickler_noload(Unpicklerobject *self, PyObject *unused)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005287{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005288 return noload(self);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005289}
5290
Guido van Rossum60456fd1997-04-09 17:36:32 +00005291
5292static struct PyMethodDef Unpickler_methods[] = {
Georg Brandl96a8c392006-05-29 21:04:52 +00005293 {"load", (PyCFunction)Unpickler_load, METH_NOARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00005294 PyDoc_STR("load() -- Load a pickle")
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005295 },
Georg Brandl96a8c392006-05-29 21:04:52 +00005296 {"noload", (PyCFunction)Unpickler_noload, METH_NOARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00005297 PyDoc_STR(
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005298 "noload() -- not load a pickle, but go through most of the motions\n"
5299 "\n"
5300 "This function can be used to read past a pickle without instantiating\n"
5301 "any objects or importing any modules. It can also be used to find all\n"
5302 "persistent references without instantiating any objects or importing\n"
Neal Norwitz200788c2002-08-13 22:20:41 +00005303 "any modules.\n")
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005304 },
Guido van Rossum60456fd1997-04-09 17:36:32 +00005305 {NULL, NULL} /* sentinel */
5306};
5307
5308
5309static Unpicklerobject *
Tim Peterscba30e22003-02-01 06:24:36 +00005310newUnpicklerobject(PyObject *f)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005311{
5312 Unpicklerobject *self;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005313
Jeremy Hylton7b5ce7f2003-04-09 21:25:30 +00005314 if (!( self = PyObject_GC_New(Unpicklerobject, &Unpicklertype)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005315 return NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005316
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005317 self->file = NULL;
5318 self->arg = NULL;
5319 self->stack = (Pdata*)Pdata_New();
5320 self->pers_func = NULL;
5321 self->last_string = NULL;
5322 self->marks = NULL;
5323 self->num_marks = 0;
5324 self->marks_size = 0;
5325 self->buf_size = 0;
5326 self->read = NULL;
5327 self->readline = NULL;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005328 self->find_class = NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005329
Tim Peterscba30e22003-02-01 06:24:36 +00005330 if (!( self->memo = PyDict_New()))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005331 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005332
Neal Norwitzb59d08c2006-07-22 16:20:49 +00005333 if (!self->stack)
5334 goto err;
5335
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005336 Py_INCREF(f);
5337 self->file = f;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005338
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005339 /* Set read, readline based on type of f */
5340 if (PyFile_Check(f)) {
5341 self->fp = PyFile_AsFile(f);
5342 if (self->fp == NULL) {
Tim Peterscba30e22003-02-01 06:24:36 +00005343 PyErr_SetString(PyExc_ValueError,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005344 "I/O operation on closed file");
5345 goto err;
5346 }
5347 self->read_func = read_file;
5348 self->readline_func = readline_file;
Guido van Rossumc91fcaa1999-03-29 20:00:14 +00005349 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005350 else if (PycStringIO_InputCheck(f)) {
5351 self->fp = NULL;
5352 self->read_func = read_cStringIO;
5353 self->readline_func = readline_cStringIO;
5354 }
5355 else {
Guido van Rossum60456fd1997-04-09 17:36:32 +00005356
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005357 self->fp = NULL;
5358 self->read_func = read_other;
5359 self->readline_func = readline_other;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005360
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005361 if (!( (self->readline = PyObject_GetAttr(f, readline_str)) &&
5362 (self->read = PyObject_GetAttr(f, read_str)))) {
5363 PyErr_Clear();
Tim Peterscba30e22003-02-01 06:24:36 +00005364 PyErr_SetString( PyExc_TypeError,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005365 "argument must have 'read' and "
5366 "'readline' attributes" );
5367 goto err;
5368 }
5369 }
Jeremy Hylton7b5ce7f2003-04-09 21:25:30 +00005370 PyObject_GC_Track(self);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005371
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005372 return self;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005373
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005374 err:
5375 Py_DECREF((PyObject *)self);
5376 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005377}
5378
5379
5380static PyObject *
Georg Brandl96a8c392006-05-29 21:04:52 +00005381get_Unpickler(PyObject *self, PyObject *file)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005382{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005383 return (PyObject *)newUnpicklerobject(file);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005384}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005385
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005386
Guido van Rossum60456fd1997-04-09 17:36:32 +00005387static void
Tim Peterscba30e22003-02-01 06:24:36 +00005388Unpickler_dealloc(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005389{
Jeremy Hylton7b5ce7f2003-04-09 21:25:30 +00005390 PyObject_GC_UnTrack((PyObject *)self);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005391 Py_XDECREF(self->readline);
5392 Py_XDECREF(self->read);
5393 Py_XDECREF(self->file);
5394 Py_XDECREF(self->memo);
5395 Py_XDECREF(self->stack);
5396 Py_XDECREF(self->pers_func);
5397 Py_XDECREF(self->arg);
5398 Py_XDECREF(self->last_string);
Jeremy Hyltonfff093f2003-07-11 19:42:49 +00005399 Py_XDECREF(self->find_class);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005400
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005401 if (self->marks) {
5402 free(self->marks);
5403 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005404
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005405 if (self->buf_size) {
5406 free(self->buf);
5407 }
Tim Peters84e87f32001-03-17 04:50:51 +00005408
Christian Heimese93237d2007-12-19 02:37:44 +00005409 Py_TYPE(self)->tp_free((PyObject *)self);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005410}
5411
Jeremy Hylton7b5ce7f2003-04-09 21:25:30 +00005412static int
5413Unpickler_traverse(Unpicklerobject *self, visitproc visit, void *arg)
5414{
Thomas Woutersc6e55062006-04-15 21:47:09 +00005415 Py_VISIT(self->readline);
5416 Py_VISIT(self->read);
5417 Py_VISIT(self->file);
5418 Py_VISIT(self->memo);
5419 Py_VISIT(self->stack);
5420 Py_VISIT(self->pers_func);
5421 Py_VISIT(self->arg);
5422 Py_VISIT(self->last_string);
5423 Py_VISIT(self->find_class);
Jeremy Hylton7b5ce7f2003-04-09 21:25:30 +00005424 return 0;
5425}
5426
5427static int
5428Unpickler_clear(Unpicklerobject *self)
5429{
Thomas Woutersedf17d82006-04-15 17:28:34 +00005430 Py_CLEAR(self->readline);
5431 Py_CLEAR(self->read);
5432 Py_CLEAR(self->file);
5433 Py_CLEAR(self->memo);
5434 Py_CLEAR(self->stack);
5435 Py_CLEAR(self->pers_func);
5436 Py_CLEAR(self->arg);
5437 Py_CLEAR(self->last_string);
5438 Py_CLEAR(self->find_class);
Jeremy Hylton7b5ce7f2003-04-09 21:25:30 +00005439 return 0;
5440}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005441
5442static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00005443Unpickler_getattr(Unpicklerobject *self, char *name)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005444{
5445 if (!strcmp(name, "persistent_load")) {
5446 if (!self->pers_func) {
5447 PyErr_SetString(PyExc_AttributeError, name);
5448 return NULL;
5449 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005450
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005451 Py_INCREF(self->pers_func);
5452 return self->pers_func;
5453 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005454
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005455 if (!strcmp(name, "find_global")) {
5456 if (!self->find_class) {
5457 PyErr_SetString(PyExc_AttributeError, name);
5458 return NULL;
5459 }
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00005460
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005461 Py_INCREF(self->find_class);
5462 return self->find_class;
5463 }
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00005464
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005465 if (!strcmp(name, "memo")) {
5466 if (!self->memo) {
5467 PyErr_SetString(PyExc_AttributeError, name);
5468 return NULL;
5469 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005470
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005471 Py_INCREF(self->memo);
5472 return self->memo;
5473 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005474
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005475 if (!strcmp(name, "UnpicklingError")) {
5476 Py_INCREF(UnpicklingError);
5477 return UnpicklingError;
5478 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005479
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005480 return Py_FindMethod(Unpickler_methods, (PyObject *)self, name);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005481}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005482
Guido van Rossum60456fd1997-04-09 17:36:32 +00005483
5484static int
Tim Peterscba30e22003-02-01 06:24:36 +00005485Unpickler_setattr(Unpicklerobject *self, char *name, PyObject *value)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005486{
Jeremy Hyltonce616e41998-08-13 23:13:52 +00005487
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005488 if (!strcmp(name, "persistent_load")) {
5489 Py_XDECREF(self->pers_func);
5490 self->pers_func = value;
5491 Py_XINCREF(value);
5492 return 0;
5493 }
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00005494
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005495 if (!strcmp(name, "find_global")) {
5496 Py_XDECREF(self->find_class);
5497 self->find_class = value;
5498 Py_XINCREF(value);
5499 return 0;
5500 }
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00005501
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005502 if (! value) {
5503 PyErr_SetString(PyExc_TypeError,
5504 "attribute deletion is not supported");
5505 return -1;
5506 }
Jeremy Hyltonce616e41998-08-13 23:13:52 +00005507
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005508 if (strcmp(name, "memo") == 0) {
5509 if (!PyDict_Check(value)) {
Tim Peterscba30e22003-02-01 06:24:36 +00005510 PyErr_SetString(PyExc_TypeError,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005511 "memo must be a dictionary");
5512 return -1;
5513 }
5514 Py_XDECREF(self->memo);
5515 self->memo = value;
5516 Py_INCREF(value);
5517 return 0;
5518 }
Jeremy Hyltonce616e41998-08-13 23:13:52 +00005519
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005520 PyErr_SetString(PyExc_AttributeError, name);
5521 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005522}
5523
Tim Peters5bd2a792003-02-01 16:45:06 +00005524/* ---------------------------------------------------------------------------
5525 * Module-level functions.
5526 */
Guido van Rossum60456fd1997-04-09 17:36:32 +00005527
Martin v. Löwis544f1192004-07-27 05:22:33 +00005528/* dump(obj, file, protocol=0). */
Guido van Rossum60456fd1997-04-09 17:36:32 +00005529static PyObject *
Martin v. Löwis544f1192004-07-27 05:22:33 +00005530cpm_dump(PyObject *self, PyObject *args, PyObject *kwds)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005531{
Martin v. Löwis15e62742006-02-27 16:46:16 +00005532 static char *kwlist[] = {"obj", "file", "protocol", NULL};
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005533 PyObject *ob, *file, *res = NULL;
5534 Picklerobject *pickler = 0;
Tim Peters5bd2a792003-02-01 16:45:06 +00005535 int proto = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005536
Martin v. Löwis544f1192004-07-27 05:22:33 +00005537 if (!( PyArg_ParseTupleAndKeywords(args, kwds, "OO|i", kwlist,
5538 &ob, &file, &proto)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005539 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005540
Tim Peters5bd2a792003-02-01 16:45:06 +00005541 if (!( pickler = newPicklerobject(file, proto)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005542 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005543
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005544 if (dump(pickler, ob) < 0)
5545 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005546
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005547 Py_INCREF(Py_None);
5548 res = Py_None;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005549
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005550 finally:
5551 Py_XDECREF(pickler);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005552
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005553 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005554}
5555
5556
Martin v. Löwis544f1192004-07-27 05:22:33 +00005557/* dumps(obj, protocol=0). */
Guido van Rossum60456fd1997-04-09 17:36:32 +00005558static PyObject *
Martin v. Löwis544f1192004-07-27 05:22:33 +00005559cpm_dumps(PyObject *self, PyObject *args, PyObject *kwds)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005560{
Martin v. Löwis15e62742006-02-27 16:46:16 +00005561 static char *kwlist[] = {"obj", "protocol", NULL};
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005562 PyObject *ob, *file = 0, *res = NULL;
5563 Picklerobject *pickler = 0;
Tim Peters5bd2a792003-02-01 16:45:06 +00005564 int proto = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005565
Martin v. Löwis544f1192004-07-27 05:22:33 +00005566 if (!( PyArg_ParseTupleAndKeywords(args, kwds, "O|i:dumps", kwlist,
5567 &ob, &proto)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005568 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005569
Tim Peterscba30e22003-02-01 06:24:36 +00005570 if (!( file = PycStringIO->NewOutput(128)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005571 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005572
Tim Peters5bd2a792003-02-01 16:45:06 +00005573 if (!( pickler = newPicklerobject(file, proto)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005574 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005575
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005576 if (dump(pickler, ob) < 0)
5577 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005578
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005579 res = PycStringIO->cgetvalue(file);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005580
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005581 finally:
5582 Py_XDECREF(pickler);
5583 Py_XDECREF(file);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005584
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005585 return res;
Tim Peters84e87f32001-03-17 04:50:51 +00005586}
5587
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005588
Tim Peters5bd2a792003-02-01 16:45:06 +00005589/* load(fileobj). */
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005590static PyObject *
Georg Brandl96a8c392006-05-29 21:04:52 +00005591cpm_load(PyObject *self, PyObject *ob)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005592{
5593 Unpicklerobject *unpickler = 0;
Georg Brandl96a8c392006-05-29 21:04:52 +00005594 PyObject *res = NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005595
Tim Peterscba30e22003-02-01 06:24:36 +00005596 if (!( unpickler = newUnpicklerobject(ob)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005597 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005598
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005599 res = load(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005600
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005601 finally:
5602 Py_XDECREF(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005603
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005604 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005605}
5606
5607
Tim Peters5bd2a792003-02-01 16:45:06 +00005608/* loads(string) */
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005609static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00005610cpm_loads(PyObject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005611{
5612 PyObject *ob, *file = 0, *res = NULL;
5613 Unpicklerobject *unpickler = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005614
Tim Peterscba30e22003-02-01 06:24:36 +00005615 if (!( PyArg_ParseTuple(args, "S:loads", &ob)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005616 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005617
Tim Peterscba30e22003-02-01 06:24:36 +00005618 if (!( file = PycStringIO->NewInput(ob)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005619 goto finally;
Tim Peters84e87f32001-03-17 04:50:51 +00005620
Tim Peterscba30e22003-02-01 06:24:36 +00005621 if (!( unpickler = newUnpicklerobject(file)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005622 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005623
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005624 res = load(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005625
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005626 finally:
5627 Py_XDECREF(file);
5628 Py_XDECREF(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005629
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005630 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005631}
5632
5633
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005634PyDoc_STRVAR(Unpicklertype__doc__,
5635"Objects that know how to unpickle");
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005636
Guido van Rossum9716aaa1997-12-08 15:15:16 +00005637static PyTypeObject Unpicklertype = {
Martin v. Löwis68192102007-07-21 06:55:02 +00005638 PyVarObject_HEAD_INIT(NULL, 0)
Jeremy Hylton7b5ce7f2003-04-09 21:25:30 +00005639 "cPickle.Unpickler", /*tp_name*/
5640 sizeof(Unpicklerobject), /*tp_basicsize*/
5641 0,
5642 (destructor)Unpickler_dealloc, /* tp_dealloc */
5643 0, /* tp_print */
5644 (getattrfunc)Unpickler_getattr, /* tp_getattr */
5645 (setattrfunc)Unpickler_setattr, /* tp_setattr */
5646 0, /* tp_compare */
5647 0, /* tp_repr */
5648 0, /* tp_as_number */
5649 0, /* tp_as_sequence */
5650 0, /* tp_as_mapping */
5651 0, /* tp_hash */
5652 0, /* tp_call */
5653 0, /* tp_str */
5654 0, /* tp_getattro */
5655 0, /* tp_setattro */
5656 0, /* tp_as_buffer */
5657 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
5658 Unpicklertype__doc__, /* tp_doc */
5659 (traverseproc)Unpickler_traverse, /* tp_traverse */
5660 (inquiry)Unpickler_clear, /* tp_clear */
Guido van Rossum9716aaa1997-12-08 15:15:16 +00005661};
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005662
Guido van Rossum60456fd1997-04-09 17:36:32 +00005663static struct PyMethodDef cPickle_methods[] = {
Martin v. Löwis544f1192004-07-27 05:22:33 +00005664 {"dump", (PyCFunction)cpm_dump, METH_VARARGS | METH_KEYWORDS,
5665 PyDoc_STR("dump(obj, file, protocol=0) -- "
Tim Peters5bd2a792003-02-01 16:45:06 +00005666 "Write an object in pickle format to the given file.\n"
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005667 "\n"
Tim Peters5bd2a792003-02-01 16:45:06 +00005668 "See the Pickler docstring for the meaning of optional argument proto.")
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005669 },
Tim Peters5bd2a792003-02-01 16:45:06 +00005670
Martin v. Löwis544f1192004-07-27 05:22:33 +00005671 {"dumps", (PyCFunction)cpm_dumps, METH_VARARGS | METH_KEYWORDS,
5672 PyDoc_STR("dumps(obj, protocol=0) -- "
Tim Peters5bd2a792003-02-01 16:45:06 +00005673 "Return a string containing an object in pickle format.\n"
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005674 "\n"
Tim Peters5bd2a792003-02-01 16:45:06 +00005675 "See the Pickler docstring for the meaning of optional argument proto.")
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005676 },
Tim Peters5bd2a792003-02-01 16:45:06 +00005677
Georg Brandl96a8c392006-05-29 21:04:52 +00005678 {"load", (PyCFunction)cpm_load, METH_O,
Neal Norwitz200788c2002-08-13 22:20:41 +00005679 PyDoc_STR("load(file) -- Load a pickle from the given file")},
Tim Peters5bd2a792003-02-01 16:45:06 +00005680
Neal Norwitzb0493252002-03-31 14:44:22 +00005681 {"loads", (PyCFunction)cpm_loads, METH_VARARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00005682 PyDoc_STR("loads(string) -- Load a pickle from the given string")},
Tim Peters5bd2a792003-02-01 16:45:06 +00005683
Martin v. Löwis544f1192004-07-27 05:22:33 +00005684 {"Pickler", (PyCFunction)get_Pickler, METH_VARARGS | METH_KEYWORDS,
5685 PyDoc_STR("Pickler(file, protocol=0) -- Create a pickler.\n"
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005686 "\n"
Tim Peters5bd2a792003-02-01 16:45:06 +00005687 "This takes a file-like object for writing a pickle data stream.\n"
5688 "The optional proto argument tells the pickler to use the given\n"
5689 "protocol; supported protocols are 0, 1, 2. The default\n"
5690 "protocol is 0, to be backwards compatible. (Protocol 0 is the\n"
5691 "only protocol that can be written to a file opened in text\n"
5692 "mode and read back successfully. When using a protocol higher\n"
5693 "than 0, make sure the file is opened in binary mode, both when\n"
5694 "pickling and unpickling.)\n"
5695 "\n"
5696 "Protocol 1 is more efficient than protocol 0; protocol 2 is\n"
5697 "more efficient than protocol 1.\n"
5698 "\n"
5699 "Specifying a negative protocol version selects the highest\n"
5700 "protocol version supported. The higher the protocol used, the\n"
5701 "more recent the version of Python needed to read the pickle\n"
5702 "produced.\n"
5703 "\n"
5704 "The file parameter must have a write() method that accepts a single\n"
5705 "string argument. It can thus be an open file object, a StringIO\n"
5706 "object, or any other custom object that meets this interface.\n")
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005707 },
Tim Peters5bd2a792003-02-01 16:45:06 +00005708
Georg Brandl96a8c392006-05-29 21:04:52 +00005709 {"Unpickler", (PyCFunction)get_Unpickler, METH_O,
Tim Peters5bd2a792003-02-01 16:45:06 +00005710 PyDoc_STR("Unpickler(file) -- Create an unpickler.")},
5711
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005712 { NULL, NULL }
5713};
5714
Guido van Rossum60456fd1997-04-09 17:36:32 +00005715static int
Tim Peterscba30e22003-02-01 06:24:36 +00005716init_stuff(PyObject *module_dict)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005717{
Alexandre Vassalotti9510e4a2008-05-11 08:25:28 +00005718 PyObject *copyreg, *t, *r;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005719
Gregory P. Smithdd96db62008-06-09 04:58:54 +00005720#define INIT_STR(S) if (!( S ## _str=PyString_InternFromString(#S))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005721
Tim Peters3cfe7542003-05-21 21:29:48 +00005722 if (PyType_Ready(&Unpicklertype) < 0)
5723 return -1;
5724 if (PyType_Ready(&Picklertype) < 0)
5725 return -1;
5726
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005727 INIT_STR(__class__);
5728 INIT_STR(__getinitargs__);
5729 INIT_STR(__dict__);
5730 INIT_STR(__getstate__);
5731 INIT_STR(__setstate__);
5732 INIT_STR(__name__);
5733 INIT_STR(__main__);
5734 INIT_STR(__reduce__);
Guido van Rossumb289b872003-02-19 01:45:13 +00005735 INIT_STR(__reduce_ex__);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005736 INIT_STR(write);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005737 INIT_STR(append);
5738 INIT_STR(read);
5739 INIT_STR(readline);
Alexandre Vassalotti9510e4a2008-05-11 08:25:28 +00005740 INIT_STR(copyreg);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005741 INIT_STR(dispatch_table);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005742
Georg Brandldffbf5f2008-05-20 07:49:57 +00005743 if (!( copyreg = PyImport_ImportModule("copy_reg")))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005744 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005745
Tim Peters1f1b2d22003-02-01 02:16:37 +00005746 /* This is special because we want to use a different
5747 one in restricted mode. */
Alexandre Vassalotti9510e4a2008-05-11 08:25:28 +00005748 dispatch_table = PyObject_GetAttr(copyreg, dispatch_table_str);
Tim Peters5b7da392003-02-04 00:21:07 +00005749 if (!dispatch_table) return -1;
5750
Alexandre Vassalotti9510e4a2008-05-11 08:25:28 +00005751 extension_registry = PyObject_GetAttrString(copyreg,
Guido van Rossumd4b920c2003-02-04 01:54:49 +00005752 "_extension_registry");
Tim Peters5b7da392003-02-04 00:21:07 +00005753 if (!extension_registry) return -1;
5754
Alexandre Vassalotti9510e4a2008-05-11 08:25:28 +00005755 inverted_registry = PyObject_GetAttrString(copyreg,
Guido van Rossumd4b920c2003-02-04 01:54:49 +00005756 "_inverted_registry");
Tim Peters5b7da392003-02-04 00:21:07 +00005757 if (!inverted_registry) return -1;
5758
Alexandre Vassalotti9510e4a2008-05-11 08:25:28 +00005759 extension_cache = PyObject_GetAttrString(copyreg,
Guido van Rossumd4b920c2003-02-04 01:54:49 +00005760 "_extension_cache");
Tim Peters5b7da392003-02-04 00:21:07 +00005761 if (!extension_cache) return -1;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005762
Alexandre Vassalotti9510e4a2008-05-11 08:25:28 +00005763 Py_DECREF(copyreg);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005764
Tim Peters731098b2003-02-04 20:56:09 +00005765 if (!(empty_tuple = PyTuple_New(0)))
5766 return -1;
5767
5768 two_tuple = PyTuple_New(2);
5769 if (two_tuple == NULL)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005770 return -1;
Tim Peters3e667d52003-02-04 21:47:44 +00005771 /* We use this temp container with no regard to refcounts, or to
5772 * keeping containees alive. Exempt from GC, because we don't
5773 * want anything looking at two_tuple() by magic.
5774 */
5775 PyObject_GC_UnTrack(two_tuple);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005776
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005777 /* Ugh */
5778 if (!( t=PyImport_ImportModule("__builtin__"))) return -1;
5779 if (PyDict_SetItemString(module_dict, "__builtins__", t) < 0)
5780 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005781
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005782 if (!( t=PyDict_New())) return -1;
5783 if (!( r=PyRun_String(
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005784 "def __str__(self):\n"
5785 " return self.args and ('%s' % self.args[0]) or '(what)'\n",
5786 Py_file_input,
5787 module_dict, t) )) return -1;
5788 Py_DECREF(r);
Guido van Rossumc03158b1999-06-09 15:23:31 +00005789
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005790 PickleError = PyErr_NewException("cPickle.PickleError", NULL, t);
Tim Peterscba30e22003-02-01 06:24:36 +00005791 if (!PickleError)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005792 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00005793
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005794 Py_DECREF(t);
Guido van Rossumc03158b1999-06-09 15:23:31 +00005795
Tim Peterscba30e22003-02-01 06:24:36 +00005796 PicklingError = PyErr_NewException("cPickle.PicklingError",
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005797 PickleError, NULL);
Tim Peterscba30e22003-02-01 06:24:36 +00005798 if (!PicklingError)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005799 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00005800
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005801 if (!( t=PyDict_New())) return -1;
5802 if (!( r=PyRun_String(
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005803 "def __str__(self):\n"
5804 " a=self.args\n"
5805 " a=a and type(a[0]) or '(what)'\n"
5806 " return 'Cannot pickle %s objects' % a\n"
5807 , Py_file_input,
5808 module_dict, t) )) return -1;
5809 Py_DECREF(r);
Tim Peters84e87f32001-03-17 04:50:51 +00005810
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005811 if (!( UnpickleableError = PyErr_NewException(
Tim Peterscba30e22003-02-01 06:24:36 +00005812 "cPickle.UnpickleableError", PicklingError, t)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005813 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00005814
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005815 Py_DECREF(t);
Guido van Rossumc03158b1999-06-09 15:23:31 +00005816
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005817 if (!( UnpicklingError = PyErr_NewException("cPickle.UnpicklingError",
Tim Peterscba30e22003-02-01 06:24:36 +00005818 PickleError, NULL)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005819 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00005820
Martin v. Löwis658009a2002-09-16 17:26:24 +00005821 if (!( BadPickleGet = PyErr_NewException("cPickle.BadPickleGet",
5822 UnpicklingError, NULL)))
5823 return -1;
Tim Peterscba30e22003-02-01 06:24:36 +00005824
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005825 if (PyDict_SetItemString(module_dict, "PickleError",
5826 PickleError) < 0)
5827 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00005828
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005829 if (PyDict_SetItemString(module_dict, "PicklingError",
5830 PicklingError) < 0)
5831 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00005832
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005833 if (PyDict_SetItemString(module_dict, "UnpicklingError",
5834 UnpicklingError) < 0)
5835 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005836
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005837 if (PyDict_SetItemString(module_dict, "UnpickleableError",
5838 UnpickleableError) < 0)
5839 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005840
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005841 if (PyDict_SetItemString(module_dict, "BadPickleGet",
5842 BadPickleGet) < 0)
5843 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00005844
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005845 PycString_IMPORT;
Guido van Rossum053b8df1998-11-25 16:18:00 +00005846
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005847 return 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005848}
5849
Mark Hammondfe51c6d2002-08-02 02:27:13 +00005850#ifndef PyMODINIT_FUNC /* declarations for DLL import/export */
5851#define PyMODINIT_FUNC void
Guido van Rossumf9ffb031999-02-04 14:54:04 +00005852#endif
Mark Hammondfe51c6d2002-08-02 02:27:13 +00005853PyMODINIT_FUNC
Tim Peterscba30e22003-02-01 06:24:36 +00005854initcPickle(void)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005855{
5856 PyObject *m, *d, *di, *v, *k;
Martin v. Löwis18e16552006-02-15 17:27:45 +00005857 Py_ssize_t i;
Tim Peters5b7da392003-02-04 00:21:07 +00005858 char *rev = "1.71"; /* XXX when does this change? */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005859 PyObject *format_version;
5860 PyObject *compatible_formats;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005861
Alexandre Vassalotti3c4971c2008-05-16 19:14:31 +00005862 /* XXX: Should mention that the pickle module will include the C
5863 XXX: optimized implementation automatically. */
5864 if (PyErr_WarnPy3k("the cPickle module has been removed in "
5865 "Python 3.0", 2) < 0)
5866 return;
5867
Christian Heimese93237d2007-12-19 02:37:44 +00005868 Py_TYPE(&Picklertype) = &PyType_Type;
5869 Py_TYPE(&Unpicklertype) = &PyType_Type;
5870 Py_TYPE(&PdataType) = &PyType_Type;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005871
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005872 /* Initialize some pieces. We need to do this before module creation,
Tim Peters5b7da392003-02-04 00:21:07 +00005873 * so we're forced to use a temporary dictionary. :(
5874 */
5875 di = PyDict_New();
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005876 if (!di) return;
5877 if (init_stuff(di) < 0) return;
Guido van Rossumebba4202000-09-07 14:35:37 +00005878
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005879 /* Create the module and add the functions */
5880 m = Py_InitModule4("cPickle", cPickle_methods,
5881 cPickle_module_documentation,
5882 (PyObject*)NULL,PYTHON_API_VERSION);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00005883 if (m == NULL)
5884 return;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005885
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005886 /* Add some symbolic constants to the module */
5887 d = PyModule_GetDict(m);
Gregory P. Smithdd96db62008-06-09 04:58:54 +00005888 v = PyString_FromString(rev);
Tim Peters5b7da392003-02-04 00:21:07 +00005889 PyDict_SetItemString(d, "__version__", v);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005890 Py_XDECREF(v);
Barry Warsaw93d29b61997-01-14 17:45:08 +00005891
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005892 /* Copy data from di. Waaa. */
5893 for (i=0; PyDict_Next(di, &i, &k, &v); ) {
5894 if (PyObject_SetItem(d, k, v) < 0) {
5895 Py_DECREF(di);
5896 return;
5897 }
5898 }
5899 Py_DECREF(di);
Guido van Rossumebba4202000-09-07 14:35:37 +00005900
Tim Peters8587b3c2003-02-13 15:44:41 +00005901 i = PyModule_AddIntConstant(m, "HIGHEST_PROTOCOL", HIGHEST_PROTOCOL);
5902 if (i < 0)
5903 return;
5904
Tim Peters5b7da392003-02-04 00:21:07 +00005905 /* These are purely informational; no code uses them. */
5906 /* File format version we write. */
Gregory P. Smithdd96db62008-06-09 04:58:54 +00005907 format_version = PyString_FromString("2.0");
Tim Peters5b7da392003-02-04 00:21:07 +00005908 /* Format versions we can read. */
5909 compatible_formats = Py_BuildValue("[sssss]",
5910 "1.0", /* Original protocol 0 */
5911 "1.1", /* Protocol 0 + INST */
5912 "1.2", /* Original protocol 1 */
5913 "1.3", /* Protocol 1 + BINFLOAT */
Tim Peters0dd23aa2003-02-04 00:30:46 +00005914 "2.0"); /* Original protocol 2 */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005915 PyDict_SetItemString(d, "format_version", format_version);
5916 PyDict_SetItemString(d, "compatible_formats", compatible_formats);
5917 Py_XDECREF(format_version);
5918 Py_XDECREF(compatible_formats);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005919}