Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 1 | #include "Python.h" |
| 2 | #include "cStringIO.h" |
Jeremy Hylton | a0fb177 | 2001-10-12 04:11:06 +0000 | [diff] [blame] | 3 | #include "structmember.h" |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 4 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 5 | PyDoc_STRVAR(cPickle_module_documentation, |
| 6 | "C implementation and optimization of the Python pickle module\n" |
| 7 | "\n" |
| 8 | "cPickle.c,v 1.71 1999/07/11 13:30:34 jim Exp\n"); |
| 9 | |
Guido van Rossum | 142eeb8 | 1997-08-13 03:14:41 +0000 | [diff] [blame] | 10 | #ifndef Py_eval_input |
| 11 | #include <graminit.h> |
| 12 | #define Py_eval_input eval_input |
Guido van Rossum | c6ef204 | 1997-08-21 02:30:45 +0000 | [diff] [blame] | 13 | #endif /* Py_eval_input */ |
Guido van Rossum | 142eeb8 | 1997-08-13 03:14:41 +0000 | [diff] [blame] | 14 | |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 15 | #include <errno.h> |
| 16 | |
Martin v. Löwis | 43c9d8a | 2002-04-01 12:34:33 +0000 | [diff] [blame] | 17 | |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 18 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 19 | #define DEL_LIST_SLICE(list, from, to) (PyList_SetSlice(list, from, to, NULL)) |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 20 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 21 | #define WRITE_BUF_SIZE 256 |
| 22 | |
Tim Peters | 3906eb8 | 2001-04-10 04:22:00 +0000 | [diff] [blame] | 23 | /* -------------------------------------------------------------------------- |
| 24 | NOTES on format codes. |
| 25 | XXX much more is needed here |
| 26 | |
| 27 | Integer types |
Tim Peters | d8ae7c2 | 2001-04-10 04:35:28 +0000 | [diff] [blame] | 28 | BININT1 8-bit unsigned integer; followed by 1 byte. |
Tim Peters | 3906eb8 | 2001-04-10 04:22:00 +0000 | [diff] [blame] | 29 | BININT2 16-bit unsigned integer; followed by 2 bytes, little-endian. |
Tim Peters | d8ae7c2 | 2001-04-10 04:35:28 +0000 | [diff] [blame] | 30 | BININT 32-bit signed integer; followed by 4 bytes, little-endian. |
| 31 | INT Integer; natural decimal string conversion, then newline. |
Tim Peters | 3906eb8 | 2001-04-10 04:22:00 +0000 | [diff] [blame] | 32 | CAUTION: INT-reading code can't assume that what follows |
| 33 | fits in a Python int, because the size of Python ints varies |
| 34 | across platforms. |
Tim Peters | d8ae7c2 | 2001-04-10 04:35:28 +0000 | [diff] [blame] | 35 | LONG Long (unbounded) integer; repr(i), then newline. |
Tim Peters | 3906eb8 | 2001-04-10 04:22:00 +0000 | [diff] [blame] | 36 | -------------------------------------------------------------------------- */ |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 37 | |
| 38 | #define MARK '(' |
| 39 | #define STOP '.' |
| 40 | #define POP '0' |
| 41 | #define POP_MARK '1' |
| 42 | #define DUP '2' |
| 43 | #define FLOAT 'F' |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 44 | #define BINFLOAT 'G' |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 45 | #define INT 'I' |
| 46 | #define BININT 'J' |
| 47 | #define BININT1 'K' |
| 48 | #define LONG 'L' |
| 49 | #define BININT2 'M' |
| 50 | #define NONE 'N' |
| 51 | #define PERSID 'P' |
| 52 | #define BINPERSID 'Q' |
| 53 | #define REDUCE 'R' |
| 54 | #define STRING 'S' |
| 55 | #define BINSTRING 'T' |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 56 | #define SHORT_BINSTRING 'U' |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 57 | #define UNICODE 'V' |
| 58 | #define BINUNICODE 'X' |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 59 | #define APPEND 'a' |
| 60 | #define BUILD 'b' |
| 61 | #define GLOBAL 'c' |
| 62 | #define DICT 'd' |
| 63 | #define EMPTY_DICT '}' |
| 64 | #define APPENDS 'e' |
| 65 | #define GET 'g' |
| 66 | #define BINGET 'h' |
| 67 | #define INST 'i' |
| 68 | #define LONG_BINGET 'j' |
| 69 | #define LIST 'l' |
| 70 | #define EMPTY_LIST ']' |
| 71 | #define OBJ 'o' |
| 72 | #define PUT 'p' |
| 73 | #define BINPUT 'q' |
| 74 | #define LONG_BINPUT 'r' |
| 75 | #define SETITEM 's' |
| 76 | #define TUPLE 't' |
| 77 | #define EMPTY_TUPLE ')' |
| 78 | #define SETITEMS 'u' |
Jack Jansen | 3a96702 | 2002-06-26 20:40:42 +0000 | [diff] [blame] | 79 | #undef TRUE |
Guido van Rossum | e276339 | 2002-04-05 19:30:08 +0000 | [diff] [blame] | 80 | #define TRUE "I01\n" |
Jack Jansen | 3a96702 | 2002-06-26 20:40:42 +0000 | [diff] [blame] | 81 | #undef FALSE |
Guido van Rossum | e276339 | 2002-04-05 19:30:08 +0000 | [diff] [blame] | 82 | #define FALSE "I00\n" |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 83 | |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 84 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 85 | static char MARKv = MARK; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 86 | |
Guido van Rossum | c03158b | 1999-06-09 15:23:31 +0000 | [diff] [blame] | 87 | static PyObject *PickleError; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 88 | static PyObject *PicklingError; |
Guido van Rossum | c03158b | 1999-06-09 15:23:31 +0000 | [diff] [blame] | 89 | static PyObject *UnpickleableError; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 90 | static PyObject *UnpicklingError; |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 91 | static PyObject *BadPickleGet; |
| 92 | |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 93 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 94 | static PyObject *dispatch_table; |
| 95 | static PyObject *safe_constructors; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 96 | static PyObject *empty_tuple; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 97 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 98 | static PyObject *__class___str, *__getinitargs___str, *__dict___str, |
| 99 | *__getstate___str, *__setstate___str, *__name___str, *__reduce___str, |
| 100 | *write_str, *__safe_for_unpickling___str, *append_str, |
Guido van Rossum | 9716aaa | 1997-12-08 15:15:16 +0000 | [diff] [blame] | 101 | *read_str, *readline_str, *__main___str, *__basicnew___str, |
Fred Drake | 2c7a685 | 2001-07-17 18:34:03 +0000 | [diff] [blame] | 102 | *copy_reg_str, *dispatch_table_str, *safe_constructors_str; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 103 | |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 104 | /************************************************************************* |
| 105 | Internal Data type for pickle data. */ |
| 106 | |
| 107 | typedef struct { |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 108 | PyObject_HEAD |
| 109 | int length, size; |
| 110 | PyObject **data; |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 111 | } Pdata; |
| 112 | |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 113 | static void |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 114 | Pdata_dealloc(Pdata *self) |
| 115 | { |
| 116 | int i; |
| 117 | PyObject **p; |
| 118 | |
| 119 | for (i=self->length, p=self->data; --i >= 0; p++) Py_DECREF(*p); |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 120 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 121 | if (self->data) free(self->data); |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 122 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 123 | PyObject_Del(self); |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | static PyTypeObject PdataType = { |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 127 | PyObject_HEAD_INIT(NULL) 0, "cPickle.Pdata", sizeof(Pdata), 0, |
| 128 | (destructor)Pdata_dealloc, |
| 129 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0L,0L,0L,0L, "" |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 130 | }; |
| 131 | |
| 132 | #define Pdata_Check(O) ((O)->ob_type == &PdataType) |
| 133 | |
| 134 | static PyObject * |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 135 | Pdata_New(void) |
| 136 | { |
| 137 | Pdata *self; |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 138 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 139 | if (!( self = PyObject_New(Pdata, &PdataType))) return NULL; |
| 140 | self->size=8; |
| 141 | self->length=0; |
| 142 | self->data=malloc(self->size * sizeof(PyObject*)); |
| 143 | if (self->data) return (PyObject*)self; |
| 144 | Py_DECREF(self); |
| 145 | return PyErr_NoMemory(); |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 146 | } |
| 147 | |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 148 | static int |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 149 | stackUnderflow(void) |
| 150 | { |
| 151 | PyErr_SetString(UnpicklingError, "unpickling stack underflow"); |
| 152 | return -1; |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | static int |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 156 | Pdata_clear(Pdata *self, int clearto) |
| 157 | { |
| 158 | int i; |
| 159 | PyObject **p; |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 160 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 161 | if (clearto < 0) return stackUnderflow(); |
| 162 | if (clearto >= self->length) return 0; |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 163 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 164 | for (i=self->length, p=self->data+clearto; --i >= clearto; p++) |
| 165 | Py_DECREF(*p); |
| 166 | self->length=clearto; |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 167 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 168 | return 0; |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 172 | static int |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 173 | Pdata_grow(Pdata *self) |
| 174 | { |
| 175 | if (! self->size) { |
| 176 | PyErr_NoMemory(); |
| 177 | return -1; |
| 178 | } |
| 179 | self->size *= 2; |
| 180 | self->data = realloc(self->data, self->size*sizeof(PyObject*)); |
| 181 | if (! self->data) { |
| 182 | self->size = 0; |
| 183 | PyErr_NoMemory(); |
| 184 | return -1; |
| 185 | } |
| 186 | return 0; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 187 | } |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 188 | |
| 189 | #define PDATA_POP(D,V) { \ |
| 190 | if ((D)->length) V=D->data[--((D)->length)]; \ |
| 191 | else { \ |
| 192 | PyErr_SetString(UnpicklingError, "bad pickle data"); \ |
| 193 | V=NULL; \ |
| 194 | } \ |
| 195 | } |
| 196 | |
| 197 | |
| 198 | static PyObject * |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 199 | Pdata_popTuple(Pdata *self, int start) |
| 200 | { |
| 201 | PyObject *r; |
| 202 | int i, j, l; |
| 203 | |
| 204 | l=self->length-start; |
| 205 | if (!( r=PyTuple_New(l))) return NULL; |
| 206 | for (i=start, j=0 ; j < l; i++, j++) |
| 207 | PyTuple_SET_ITEM(r, j, self->data[i]); |
| 208 | |
| 209 | self->length=start; |
| 210 | return r; |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | static PyObject * |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 214 | Pdata_popList(Pdata *self, int start) |
| 215 | { |
| 216 | PyObject *r; |
| 217 | int i, j, l; |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 218 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 219 | l=self->length-start; |
| 220 | if (!( r=PyList_New(l))) return NULL; |
| 221 | for (i=start, j=0 ; j < l; i++, j++) |
| 222 | PyList_SET_ITEM(r, j, self->data[i]); |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 223 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 224 | self->length=start; |
| 225 | return r; |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 226 | } |
| 227 | |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 228 | #define PDATA_APPEND(D,O,ER) { \ |
| 229 | if (((Pdata*)(D))->length == ((Pdata*)(D))->size && \ |
| 230 | Pdata_grow((Pdata*)(D)) < 0) \ |
| 231 | return ER; \ |
| 232 | Py_INCREF(O); \ |
| 233 | ((Pdata*)(D))->data[((Pdata*)(D))->length++]=O; \ |
| 234 | } |
| 235 | |
| 236 | #define PDATA_PUSH(D,O,ER) { \ |
| 237 | if (((Pdata*)(D))->length == ((Pdata*)(D))->size && \ |
| 238 | Pdata_grow((Pdata*)(D)) < 0) { \ |
| 239 | Py_DECREF(O); \ |
| 240 | return ER; \ |
| 241 | } \ |
| 242 | ((Pdata*)(D))->data[((Pdata*)(D))->length++]=O; \ |
| 243 | } |
| 244 | |
| 245 | /*************************************************************************/ |
| 246 | |
| 247 | #define ARG_TUP(self, o) { \ |
| 248 | if (self->arg || (self->arg=PyTuple_New(1))) { \ |
| 249 | Py_XDECREF(PyTuple_GET_ITEM(self->arg,0)); \ |
| 250 | PyTuple_SET_ITEM(self->arg,0,o); \ |
| 251 | } \ |
| 252 | else { \ |
| 253 | Py_DECREF(o); \ |
| 254 | } \ |
| 255 | } |
| 256 | |
| 257 | #define FREE_ARG_TUP(self) { \ |
| 258 | if (self->arg->ob_refcnt > 1) { \ |
| 259 | Py_DECREF(self->arg); \ |
| 260 | self->arg=NULL; \ |
| 261 | } \ |
| 262 | } |
| 263 | |
Thomas Wouters | 3b6448f | 2000-07-22 23:56:07 +0000 | [diff] [blame] | 264 | typedef struct Picklerobject { |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 265 | PyObject_HEAD |
| 266 | FILE *fp; |
| 267 | PyObject *write; |
| 268 | PyObject *file; |
| 269 | PyObject *memo; |
| 270 | PyObject *arg; |
| 271 | PyObject *pers_func; |
| 272 | PyObject *inst_pers_func; |
| 273 | int bin; |
| 274 | int fast; /* Fast mode doesn't save in memo, don't use if circ ref */ |
Martin v. Löwis | 5a39530 | 2002-08-04 08:20:23 +0000 | [diff] [blame] | 275 | int nesting; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 276 | int (*write_func)(struct Picklerobject *, char *, int); |
| 277 | char *write_buf; |
| 278 | int buf_size; |
| 279 | PyObject *dispatch_table; |
| 280 | int fast_container; /* count nested container dumps */ |
| 281 | PyObject *fast_memo; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 282 | } Picklerobject; |
| 283 | |
Barry Warsaw | 52acb49 | 2001-12-21 20:04:22 +0000 | [diff] [blame] | 284 | #ifndef PY_CPICKLE_FAST_LIMIT |
| 285 | #define PY_CPICKLE_FAST_LIMIT 50 |
| 286 | #endif |
Jeremy Hylton | a0fb177 | 2001-10-12 04:11:06 +0000 | [diff] [blame] | 287 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 288 | static PyTypeObject Picklertype; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 289 | |
Thomas Wouters | 3b6448f | 2000-07-22 23:56:07 +0000 | [diff] [blame] | 290 | typedef struct Unpicklerobject { |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 291 | PyObject_HEAD |
| 292 | FILE *fp; |
| 293 | PyObject *file; |
| 294 | PyObject *readline; |
| 295 | PyObject *read; |
| 296 | PyObject *memo; |
| 297 | PyObject *arg; |
| 298 | Pdata *stack; |
| 299 | PyObject *mark; |
| 300 | PyObject *pers_func; |
| 301 | PyObject *last_string; |
| 302 | int *marks; |
| 303 | int num_marks; |
| 304 | int marks_size; |
| 305 | int (*read_func)(struct Unpicklerobject *, char **, int); |
| 306 | int (*readline_func)(struct Unpicklerobject *, char **); |
| 307 | int buf_size; |
| 308 | char *buf; |
| 309 | PyObject *safe_constructors; |
| 310 | PyObject *find_class; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 311 | } Unpicklerobject; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 312 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 313 | static PyTypeObject Unpicklertype; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 314 | |
Thomas Wouters | 4789b3a | 2000-07-24 11:36:47 +0000 | [diff] [blame] | 315 | /* Forward decls that need the above structs */ |
| 316 | static int save(Picklerobject *, PyObject *, int); |
| 317 | static int put2(Picklerobject *, PyObject *); |
| 318 | |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 319 | int |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 320 | cPickle_PyMapping_HasKey(PyObject *o, PyObject *key) |
| 321 | { |
| 322 | PyObject *v; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 323 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 324 | if ((v = PyObject_GetItem(o,key))) { |
| 325 | Py_DECREF(v); |
| 326 | return 1; |
| 327 | } |
| 328 | |
| 329 | PyErr_Clear(); |
| 330 | return 0; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 331 | } |
| 332 | |
Guido van Rossum | d385d59 | 1997-04-09 17:47:47 +0000 | [diff] [blame] | 333 | static |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 334 | PyObject * |
Thomas Wouters | 3b6448f | 2000-07-22 23:56:07 +0000 | [diff] [blame] | 335 | cPickle_ErrFormat(PyObject *ErrType, char *stringformat, char *format, ...) |
| 336 | { |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 337 | va_list va; |
| 338 | PyObject *args=0, *retval=0; |
| 339 | va_start(va, format); |
| 340 | |
| 341 | if (format) args = Py_VaBuildValue(format, va); |
| 342 | va_end(va); |
| 343 | if (format && ! args) return NULL; |
| 344 | if (stringformat && !(retval=PyString_FromString(stringformat))) |
| 345 | return NULL; |
| 346 | |
| 347 | if (retval) { |
| 348 | if (args) { |
| 349 | PyObject *v; |
| 350 | v=PyString_Format(retval, args); |
| 351 | Py_DECREF(retval); |
| 352 | Py_DECREF(args); |
| 353 | if (! v) return NULL; |
| 354 | retval=v; |
| 355 | } |
| 356 | } |
| 357 | else |
| 358 | if (args) retval=args; |
| 359 | else { |
| 360 | PyErr_SetObject(ErrType,Py_None); |
| 361 | return NULL; |
| 362 | } |
| 363 | PyErr_SetObject(ErrType,retval); |
| 364 | Py_DECREF(retval); |
| 365 | return NULL; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 366 | } |
| 367 | |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 368 | static int |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 369 | write_file(Picklerobject *self, char *s, int n) |
| 370 | { |
| 371 | size_t nbyteswritten; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 372 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 373 | if (s == NULL) { |
| 374 | return 0; |
| 375 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 376 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 377 | Py_BEGIN_ALLOW_THREADS |
Jeremy Hylton | 7fa4bfa | 2002-07-18 20:58:57 +0000 | [diff] [blame] | 378 | nbyteswritten = fwrite(s, sizeof(char), n, self->fp); |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 379 | Py_END_ALLOW_THREADS |
Jeremy Hylton | 7fa4bfa | 2002-07-18 20:58:57 +0000 | [diff] [blame] | 380 | if (nbyteswritten != (size_t)n) { |
| 381 | PyErr_SetFromErrno(PyExc_IOError); |
| 382 | return -1; |
| 383 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 384 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 385 | return n; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 386 | } |
| 387 | |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 388 | static int |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 389 | write_cStringIO(Picklerobject *self, char *s, int n) |
| 390 | { |
| 391 | if (s == NULL) { |
| 392 | return 0; |
| 393 | } |
| 394 | |
| 395 | if (PycStringIO->cwrite((PyObject *)self->file, s, n) != n) { |
| 396 | return -1; |
| 397 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 398 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 399 | return n; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 400 | } |
| 401 | |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 402 | static int |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 403 | write_none(Picklerobject *self, char *s, int n) |
| 404 | { |
| 405 | if (s == NULL) return 0; |
| 406 | return n; |
Guido van Rossum | 142eeb8 | 1997-08-13 03:14:41 +0000 | [diff] [blame] | 407 | } |
| 408 | |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 409 | static int |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 410 | write_other(Picklerobject *self, char *s, int n) |
| 411 | { |
| 412 | PyObject *py_str = 0, *junk = 0; |
| 413 | |
| 414 | if (s == NULL) { |
| 415 | if (!( self->buf_size )) return 0; |
| 416 | py_str = PyString_FromStringAndSize(self->write_buf, |
| 417 | self->buf_size); |
| 418 | if (!py_str) |
| 419 | return -1; |
| 420 | } |
| 421 | else { |
| 422 | if (self->buf_size && (n + self->buf_size) > WRITE_BUF_SIZE) { |
| 423 | if (write_other(self, NULL, 0) < 0) |
| 424 | return -1; |
| 425 | } |
| 426 | |
| 427 | if (n > WRITE_BUF_SIZE) { |
| 428 | if (!( py_str = |
| 429 | PyString_FromStringAndSize(s, n))) |
| 430 | return -1; |
| 431 | } |
| 432 | else { |
| 433 | memcpy(self->write_buf + self->buf_size, s, n); |
| 434 | self->buf_size += n; |
| 435 | return n; |
| 436 | } |
| 437 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 438 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 439 | if (self->write) { |
| 440 | /* object with write method */ |
| 441 | ARG_TUP(self, py_str); |
| 442 | if (self->arg) { |
| 443 | junk = PyObject_Call(self->write, self->arg, NULL); |
| 444 | FREE_ARG_TUP(self); |
| 445 | } |
| 446 | if (junk) Py_DECREF(junk); |
| 447 | else return -1; |
| 448 | } |
| 449 | else |
| 450 | PDATA_PUSH(self->file, py_str, -1); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 451 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 452 | self->buf_size = 0; |
| 453 | return n; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 454 | } |
| 455 | |
| 456 | |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 457 | static int |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 458 | read_file(Unpicklerobject *self, char **s, int n) |
| 459 | { |
| 460 | size_t nbytesread; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 461 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 462 | if (self->buf_size == 0) { |
| 463 | int size; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 464 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 465 | size = ((n < 32) ? 32 : n); |
| 466 | if (!( self->buf = (char *)malloc(size * sizeof(char)))) { |
| 467 | PyErr_NoMemory(); |
| 468 | return -1; |
| 469 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 470 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 471 | self->buf_size = size; |
| 472 | } |
| 473 | else if (n > self->buf_size) { |
| 474 | self->buf = (char *)realloc(self->buf, n * sizeof(char)); |
| 475 | if (!self->buf) { |
| 476 | PyErr_NoMemory(); |
| 477 | return -1; |
| 478 | } |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 479 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 480 | self->buf_size = n; |
| 481 | } |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 482 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 483 | Py_BEGIN_ALLOW_THREADS |
| 484 | nbytesread = fread(self->buf, sizeof(char), n, self->fp); |
| 485 | Py_END_ALLOW_THREADS |
| 486 | if (nbytesread != (size_t)n) { |
| 487 | if (feof(self->fp)) { |
| 488 | PyErr_SetNone(PyExc_EOFError); |
| 489 | return -1; |
| 490 | } |
| 491 | |
| 492 | PyErr_SetFromErrno(PyExc_IOError); |
| 493 | return -1; |
| 494 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 495 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 496 | *s = self->buf; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 497 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 498 | return n; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 499 | } |
| 500 | |
| 501 | |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 502 | static int |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 503 | readline_file(Unpicklerobject *self, char **s) |
| 504 | { |
| 505 | int i; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 506 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 507 | if (self->buf_size == 0) { |
| 508 | if (!( self->buf = (char *)malloc(40 * sizeof(char)))) { |
| 509 | PyErr_NoMemory(); |
| 510 | return -1; |
| 511 | } |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 512 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 513 | self->buf_size = 40; |
| 514 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 515 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 516 | i = 0; |
| 517 | while (1) { |
| 518 | for (; i < (self->buf_size - 1); i++) { |
| 519 | if (feof(self->fp) || |
| 520 | (self->buf[i] = getc(self->fp)) == '\n') { |
| 521 | self->buf[i + 1] = '\0'; |
| 522 | *s = self->buf; |
| 523 | return i + 1; |
| 524 | } |
| 525 | } |
| 526 | self->buf = (char *)realloc(self->buf, |
| 527 | (self->buf_size * 2) * sizeof(char)); |
| 528 | if (!self->buf) { |
| 529 | PyErr_NoMemory(); |
| 530 | return -1; |
| 531 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 532 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 533 | self->buf_size *= 2; |
| 534 | } |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 535 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 536 | |
| 537 | |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 538 | static int |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 539 | read_cStringIO(Unpicklerobject *self, char **s, int n) |
| 540 | { |
| 541 | char *ptr; |
| 542 | |
| 543 | if (PycStringIO->cread((PyObject *)self->file, &ptr, n) != n) { |
| 544 | PyErr_SetNone(PyExc_EOFError); |
| 545 | return -1; |
| 546 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 547 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 548 | *s = ptr; |
| 549 | |
| 550 | return n; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 551 | } |
| 552 | |
| 553 | |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 554 | static int |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 555 | readline_cStringIO(Unpicklerobject *self, char **s) |
| 556 | { |
| 557 | int n; |
| 558 | char *ptr; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 559 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 560 | if ((n = PycStringIO->creadline((PyObject *)self->file, &ptr)) < 0) { |
| 561 | return -1; |
| 562 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 563 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 564 | *s = ptr; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 565 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 566 | return n; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 567 | } |
| 568 | |
| 569 | |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 570 | static int |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 571 | read_other(Unpicklerobject *self, char **s, int n) |
| 572 | { |
| 573 | PyObject *bytes, *str=0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 574 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 575 | if (!( bytes = PyInt_FromLong(n))) return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 576 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 577 | ARG_TUP(self, bytes); |
| 578 | if (self->arg) { |
| 579 | str = PyObject_Call(self->read, self->arg, NULL); |
| 580 | FREE_ARG_TUP(self); |
| 581 | } |
| 582 | if (! str) return -1; |
| 583 | |
| 584 | Py_XDECREF(self->last_string); |
| 585 | self->last_string = str; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 586 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 587 | if (! (*s = PyString_AsString(str))) return -1; |
| 588 | return n; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 589 | } |
| 590 | |
| 591 | |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 592 | static int |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 593 | readline_other(Unpicklerobject *self, char **s) |
| 594 | { |
| 595 | PyObject *str; |
| 596 | int str_size; |
| 597 | |
| 598 | if (!( str = PyObject_CallObject(self->readline, empty_tuple))) { |
| 599 | return -1; |
| 600 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 601 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 602 | if ((str_size = PyString_Size(str)) < 0) |
| 603 | return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 604 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 605 | Py_XDECREF(self->last_string); |
| 606 | self->last_string = str; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 607 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 608 | if (! (*s = PyString_AsString(str))) |
| 609 | return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 610 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 611 | return str_size; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 612 | } |
| 613 | |
| 614 | |
| 615 | static char * |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 616 | pystrndup(char *s, int l) |
| 617 | { |
| 618 | char *r; |
| 619 | if (!( r=malloc((l+1)*sizeof(char)))) return (char*)PyErr_NoMemory(); |
| 620 | memcpy(r,s,l); |
| 621 | r[l]=0; |
| 622 | return r; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 623 | } |
| 624 | |
| 625 | |
| 626 | static int |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 627 | get(Picklerobject *self, PyObject *id) |
| 628 | { |
| 629 | PyObject *value, *mv; |
| 630 | long c_value; |
| 631 | char s[30]; |
| 632 | size_t len; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 633 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 634 | if (!( mv = PyDict_GetItem(self->memo, id))) { |
| 635 | PyErr_SetObject(PyExc_KeyError, id); |
| 636 | return -1; |
| 637 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 638 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 639 | if (!( value = PyTuple_GetItem(mv, 0))) |
| 640 | return -1; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 641 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 642 | if (!( PyInt_Check(value))) { |
| 643 | PyErr_SetString(PicklingError, "no int where int expected in memo"); |
| 644 | return -1; |
| 645 | } |
| 646 | c_value = PyInt_AS_LONG((PyIntObject*)value); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 647 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 648 | if (!self->bin) { |
| 649 | s[0] = GET; |
| 650 | PyOS_snprintf(s + 1, sizeof(s) - 1, "%ld\n", c_value); |
| 651 | len = strlen(s); |
| 652 | } |
| 653 | else if (Pdata_Check(self->file)) { |
| 654 | if (write_other(self, NULL, 0) < 0) return -1; |
| 655 | PDATA_APPEND(self->file, mv, -1); |
| 656 | return 0; |
| 657 | } |
| 658 | else { |
| 659 | if (c_value < 256) { |
| 660 | s[0] = BINGET; |
| 661 | s[1] = (int)(c_value & 0xff); |
| 662 | len = 2; |
| 663 | } |
| 664 | else { |
| 665 | s[0] = LONG_BINGET; |
| 666 | s[1] = (int)(c_value & 0xff); |
| 667 | s[2] = (int)((c_value >> 8) & 0xff); |
| 668 | s[3] = (int)((c_value >> 16) & 0xff); |
| 669 | s[4] = (int)((c_value >> 24) & 0xff); |
| 670 | len = 5; |
| 671 | } |
| 672 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 673 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 674 | if ((*self->write_func)(self, s, len) < 0) |
| 675 | return -1; |
| 676 | |
Jeremy Hylton | a0fb177 | 2001-10-12 04:11:06 +0000 | [diff] [blame] | 677 | return 0; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 678 | } |
Jeremy Hylton | ce616e4 | 1998-08-13 23:13:52 +0000 | [diff] [blame] | 679 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 680 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 681 | static int |
| 682 | put(Picklerobject *self, PyObject *ob) |
| 683 | { |
| 684 | if (ob->ob_refcnt < 2 || self->fast) |
| 685 | return 0; |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 686 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 687 | return put2(self, ob); |
| 688 | } |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 689 | |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 690 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 691 | static int |
| 692 | put2(Picklerobject *self, PyObject *ob) |
| 693 | { |
| 694 | char c_str[30]; |
| 695 | int p; |
| 696 | size_t len; |
| 697 | int res = -1; |
| 698 | PyObject *py_ob_id = 0, *memo_len = 0, *t = 0; |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 699 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 700 | if (self->fast) |
| 701 | return 0; |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 702 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 703 | if ((p = PyDict_Size(self->memo)) < 0) |
| 704 | goto finally; |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 705 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 706 | /* Make sure memo keys are positive! */ |
| 707 | p++; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 708 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 709 | if (!( py_ob_id = PyLong_FromVoidPtr(ob))) |
| 710 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 711 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 712 | if (!( memo_len = PyInt_FromLong(p))) |
| 713 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 714 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 715 | if (!( t = PyTuple_New(2))) |
| 716 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 717 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 718 | PyTuple_SET_ITEM(t, 0, memo_len); |
| 719 | Py_INCREF(memo_len); |
| 720 | PyTuple_SET_ITEM(t, 1, ob); |
| 721 | Py_INCREF(ob); |
| 722 | |
| 723 | if (PyDict_SetItem(self->memo, py_ob_id, t) < 0) |
| 724 | goto finally; |
| 725 | |
| 726 | if (!self->bin) { |
| 727 | c_str[0] = PUT; |
| 728 | PyOS_snprintf(c_str + 1, sizeof(c_str) - 1, "%d\n", p); |
| 729 | len = strlen(c_str); |
| 730 | } |
| 731 | else if (Pdata_Check(self->file)) { |
| 732 | if (write_other(self, NULL, 0) < 0) return -1; |
| 733 | PDATA_APPEND(self->file, memo_len, -1); |
| 734 | res=0; /* Job well done ;) */ |
| 735 | goto finally; |
| 736 | } |
| 737 | else { |
| 738 | if (p >= 256) { |
| 739 | c_str[0] = LONG_BINPUT; |
| 740 | c_str[1] = (int)(p & 0xff); |
| 741 | c_str[2] = (int)((p >> 8) & 0xff); |
| 742 | c_str[3] = (int)((p >> 16) & 0xff); |
| 743 | c_str[4] = (int)((p >> 24) & 0xff); |
| 744 | len = 5; |
| 745 | } |
| 746 | else { |
| 747 | c_str[0] = BINPUT; |
| 748 | c_str[1] = p; |
| 749 | len = 2; |
| 750 | } |
| 751 | } |
| 752 | |
| 753 | if ((*self->write_func)(self, c_str, len) < 0) |
| 754 | goto finally; |
| 755 | |
| 756 | res = 0; |
| 757 | |
| 758 | finally: |
| 759 | Py_XDECREF(py_ob_id); |
| 760 | Py_XDECREF(memo_len); |
| 761 | Py_XDECREF(t); |
| 762 | |
| 763 | return res; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 764 | } |
| 765 | |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 766 | #define PyImport_Import cPickle_Import |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 767 | |
| 768 | static PyObject * |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 769 | PyImport_Import(PyObject *module_name) |
| 770 | { |
| 771 | static PyObject *silly_list=0, *__builtins___str=0, *__import___str; |
| 772 | static PyObject *standard_builtins=0; |
| 773 | PyObject *globals=0, *__import__=0, *__builtins__=0, *r=0; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 774 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 775 | if (!( silly_list )) { |
| 776 | if (!( __import___str=PyString_FromString("__import__"))) |
| 777 | return NULL; |
| 778 | if (!( __builtins___str=PyString_FromString("__builtins__"))) |
| 779 | return NULL; |
| 780 | if (!( silly_list=Py_BuildValue("[s]","__doc__"))) |
| 781 | return NULL; |
| 782 | } |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 783 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 784 | if ((globals=PyEval_GetGlobals())) { |
| 785 | Py_INCREF(globals); |
| 786 | __builtins__=PyObject_GetItem(globals,__builtins___str); |
| 787 | if (!__builtins__) |
| 788 | goto err; |
| 789 | } |
| 790 | else { |
| 791 | PyErr_Clear(); |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 792 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 793 | if (!(standard_builtins || |
| 794 | (standard_builtins=PyImport_ImportModule("__builtin__")))) |
| 795 | return NULL; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 796 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 797 | __builtins__=standard_builtins; |
| 798 | Py_INCREF(__builtins__); |
| 799 | globals = Py_BuildValue("{sO}", "__builtins__", __builtins__); |
| 800 | if (!globals) |
| 801 | goto err; |
| 802 | } |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 803 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 804 | if (PyDict_Check(__builtins__)) { |
| 805 | __import__=PyObject_GetItem(__builtins__,__import___str); |
| 806 | if (!__import__) goto err; |
| 807 | } |
| 808 | else { |
| 809 | __import__=PyObject_GetAttr(__builtins__,__import___str); |
| 810 | if (!__import__) goto err; |
| 811 | } |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 812 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 813 | r=PyObject_CallFunction(__import__,"OOOO", |
| 814 | module_name, globals, globals, silly_list); |
| 815 | if (!r) |
| 816 | goto err; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 817 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 818 | Py_DECREF(globals); |
| 819 | Py_DECREF(__builtins__); |
| 820 | Py_DECREF(__import__); |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 821 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 822 | return r; |
| 823 | err: |
| 824 | Py_XDECREF(globals); |
| 825 | Py_XDECREF(__builtins__); |
| 826 | Py_XDECREF(__import__); |
| 827 | return NULL; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 828 | } |
| 829 | |
| 830 | static PyObject * |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 831 | whichmodule(PyObject *global, PyObject *global_name) |
| 832 | { |
| 833 | int i, j; |
| 834 | PyObject *module = 0, *modules_dict = 0, |
| 835 | *global_name_attr = 0, *name = 0; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 836 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 837 | module = PyObject_GetAttrString(global, "__module__"); |
| 838 | if (module) return module; |
| 839 | PyErr_Clear(); |
Guido van Rossum | 4518823 | 1997-09-28 05:38:51 +0000 | [diff] [blame] | 840 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 841 | if (!( modules_dict = PySys_GetObject("modules"))) |
| 842 | return NULL; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 843 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 844 | i = 0; |
| 845 | while ((j = PyDict_Next(modules_dict, &i, &name, &module))) { |
Guido van Rossum | 142eeb8 | 1997-08-13 03:14:41 +0000 | [diff] [blame] | 846 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 847 | if (PyObject_Compare(name, __main___str)==0) continue; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 848 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 849 | global_name_attr = PyObject_GetAttr(module, global_name); |
| 850 | if (!global_name_attr) { |
| 851 | PyErr_Clear(); |
| 852 | continue; |
| 853 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 854 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 855 | if (global_name_attr != global) { |
| 856 | Py_DECREF(global_name_attr); |
| 857 | continue; |
| 858 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 859 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 860 | Py_DECREF(global_name_attr); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 861 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 862 | break; |
| 863 | } |
Guido van Rossum | 142eeb8 | 1997-08-13 03:14:41 +0000 | [diff] [blame] | 864 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 865 | /* The following implements the rule in pickle.py added in 1.5 |
| 866 | that used __main__ if no module is found. I don't actually |
| 867 | like this rule. jlf |
| 868 | */ |
| 869 | if (!j) { |
| 870 | j=1; |
| 871 | name=__main___str; |
| 872 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 873 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 874 | Py_INCREF(name); |
| 875 | return name; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 876 | } |
| 877 | |
| 878 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 879 | static int |
Jeremy Hylton | 499ab6a | 2001-10-15 21:37:58 +0000 | [diff] [blame] | 880 | fast_save_enter(Picklerobject *self, PyObject *obj) |
| 881 | { |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 882 | /* if fast_container < 0, we're doing an error exit. */ |
| 883 | if (++self->fast_container >= PY_CPICKLE_FAST_LIMIT) { |
| 884 | PyObject *key = NULL; |
| 885 | if (self->fast_memo == NULL) { |
| 886 | self->fast_memo = PyDict_New(); |
| 887 | if (self->fast_memo == NULL) { |
| 888 | self->fast_container = -1; |
| 889 | return 0; |
| 890 | } |
| 891 | } |
| 892 | key = PyLong_FromVoidPtr(obj); |
| 893 | if (key == NULL) |
| 894 | return 0; |
| 895 | if (PyDict_GetItem(self->fast_memo, key)) { |
Tim Peters | 4e52ca8 | 2002-12-07 02:43:28 +0000 | [diff] [blame] | 896 | Py_DECREF(key); |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 897 | PyErr_Format(PyExc_ValueError, |
| 898 | "fast mode: can't pickle cyclic objects including object type %s at %p", |
| 899 | obj->ob_type->tp_name, obj); |
| 900 | self->fast_container = -1; |
| 901 | return 0; |
| 902 | } |
| 903 | if (PyDict_SetItem(self->fast_memo, key, Py_None) < 0) { |
Tim Peters | 4e52ca8 | 2002-12-07 02:43:28 +0000 | [diff] [blame] | 904 | Py_DECREF(key); |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 905 | self->fast_container = -1; |
| 906 | return 0; |
| 907 | } |
Tim Peters | 4e52ca8 | 2002-12-07 02:43:28 +0000 | [diff] [blame] | 908 | Py_DECREF(key); |
Jeremy Hylton | 499ab6a | 2001-10-15 21:37:58 +0000 | [diff] [blame] | 909 | } |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 910 | return 1; |
Jeremy Hylton | 499ab6a | 2001-10-15 21:37:58 +0000 | [diff] [blame] | 911 | } |
| 912 | |
| 913 | int |
| 914 | fast_save_leave(Picklerobject *self, PyObject *obj) |
| 915 | { |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 916 | if (self->fast_container-- >= PY_CPICKLE_FAST_LIMIT) { |
| 917 | PyObject *key = PyLong_FromVoidPtr(obj); |
| 918 | if (key == NULL) |
| 919 | return 0; |
| 920 | if (PyDict_DelItem(self->fast_memo, key) < 0) { |
Tim Peters | 4e52ca8 | 2002-12-07 02:43:28 +0000 | [diff] [blame] | 921 | Py_DECREF(key); |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 922 | return 0; |
| 923 | } |
Tim Peters | 4e52ca8 | 2002-12-07 02:43:28 +0000 | [diff] [blame] | 924 | Py_DECREF(key); |
Jeremy Hylton | 499ab6a | 2001-10-15 21:37:58 +0000 | [diff] [blame] | 925 | } |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 926 | return 1; |
Jeremy Hylton | 499ab6a | 2001-10-15 21:37:58 +0000 | [diff] [blame] | 927 | } |
| 928 | |
| 929 | static int |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 930 | save_none(Picklerobject *self, PyObject *args) |
| 931 | { |
| 932 | static char none = NONE; |
| 933 | if ((*self->write_func)(self, &none, 1) < 0) |
| 934 | return -1; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 935 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 936 | return 0; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 937 | } |
| 938 | |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 939 | static int |
| 940 | save_bool(Picklerobject *self, PyObject *args) |
| 941 | { |
Guido van Rossum | e276339 | 2002-04-05 19:30:08 +0000 | [diff] [blame] | 942 | static char *buf[2] = {FALSE, TRUE}; |
| 943 | static char len[2] = {sizeof(FALSE)-1, sizeof(TRUE)-1}; |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 944 | long l = PyInt_AS_LONG((PyIntObject *)args); |
| 945 | |
Guido van Rossum | e276339 | 2002-04-05 19:30:08 +0000 | [diff] [blame] | 946 | if ((*self->write_func)(self, buf[l], len[l]) < 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 947 | return -1; |
| 948 | |
| 949 | return 0; |
| 950 | } |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 951 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 952 | static int |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 953 | save_int(Picklerobject *self, PyObject *args) |
| 954 | { |
| 955 | char c_str[32]; |
| 956 | long l = PyInt_AS_LONG((PyIntObject *)args); |
| 957 | int len = 0; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 958 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 959 | if (!self->bin |
Guido van Rossum | de8d6d7 | 1997-05-13 18:00:44 +0000 | [diff] [blame] | 960 | #if SIZEOF_LONG > 4 |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 961 | || l > 0x7fffffffL |
| 962 | || l < -0x80000000L |
Guido van Rossum | de8d6d7 | 1997-05-13 18:00:44 +0000 | [diff] [blame] | 963 | #endif |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 964 | ) { |
| 965 | /* Text-mode pickle, or long too big to fit in the 4-byte |
| 966 | * signed BININT format: store as a string. |
| 967 | */ |
| 968 | c_str[0] = INT; |
| 969 | PyOS_snprintf(c_str + 1, sizeof(c_str) - 1, "%ld\n", l); |
| 970 | if ((*self->write_func)(self, c_str, strlen(c_str)) < 0) |
| 971 | return -1; |
| 972 | } |
| 973 | else { |
| 974 | /* Binary pickle and l fits in a signed 4-byte int. */ |
| 975 | c_str[1] = (int)( l & 0xff); |
| 976 | c_str[2] = (int)((l >> 8) & 0xff); |
| 977 | c_str[3] = (int)((l >> 16) & 0xff); |
| 978 | c_str[4] = (int)((l >> 24) & 0xff); |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 979 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 980 | if ((c_str[4] == 0) && (c_str[3] == 0)) { |
| 981 | if (c_str[2] == 0) { |
| 982 | c_str[0] = BININT1; |
| 983 | len = 2; |
| 984 | } |
| 985 | else { |
| 986 | c_str[0] = BININT2; |
| 987 | len = 3; |
| 988 | } |
| 989 | } |
| 990 | else { |
| 991 | c_str[0] = BININT; |
| 992 | len = 5; |
| 993 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 994 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 995 | if ((*self->write_func)(self, c_str, len) < 0) |
| 996 | return -1; |
| 997 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 998 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 999 | return 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1000 | } |
| 1001 | |
| 1002 | |
| 1003 | static int |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1004 | save_long(Picklerobject *self, PyObject *args) |
| 1005 | { |
| 1006 | int size, res = -1; |
| 1007 | PyObject *repr = 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1008 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1009 | static char l = LONG; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1010 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1011 | if (!( repr = PyObject_Repr(args))) |
| 1012 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1013 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1014 | if ((size = PyString_Size(repr)) < 0) |
| 1015 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1016 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1017 | if ((*self->write_func)(self, &l, 1) < 0) |
| 1018 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1019 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1020 | if ((*self->write_func)(self, |
| 1021 | PyString_AS_STRING((PyStringObject *)repr), |
| 1022 | size) < 0) |
| 1023 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1024 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1025 | if ((*self->write_func)(self, "\n", 1) < 0) |
| 1026 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1027 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1028 | res = 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1029 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1030 | finally: |
| 1031 | Py_XDECREF(repr); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1032 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1033 | return res; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1034 | } |
| 1035 | |
| 1036 | |
| 1037 | static int |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1038 | save_float(Picklerobject *self, PyObject *args) |
| 1039 | { |
| 1040 | double x = PyFloat_AS_DOUBLE((PyFloatObject *)args); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1041 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1042 | if (self->bin) { |
| 1043 | int s, e; |
| 1044 | double f; |
| 1045 | long fhi, flo; |
| 1046 | char str[9]; |
| 1047 | unsigned char *p = (unsigned char *)str; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1048 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1049 | *p = BINFLOAT; |
| 1050 | p++; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1051 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1052 | if (x < 0) { |
| 1053 | s = 1; |
| 1054 | x = -x; |
| 1055 | } |
| 1056 | else |
| 1057 | s = 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1058 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1059 | f = frexp(x, &e); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1060 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1061 | /* Normalize f to be in the range [1.0, 2.0) */ |
| 1062 | if (0.5 <= f && f < 1.0) { |
| 1063 | f *= 2.0; |
| 1064 | e--; |
| 1065 | } |
| 1066 | else if (f == 0.0) { |
| 1067 | e = 0; |
| 1068 | } |
| 1069 | else { |
| 1070 | PyErr_SetString(PyExc_SystemError, |
| 1071 | "frexp() result out of range"); |
| 1072 | return -1; |
| 1073 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1074 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1075 | if (e >= 1024) { |
| 1076 | /* XXX 1024 itself is reserved for Inf/NaN */ |
| 1077 | PyErr_SetString(PyExc_OverflowError, |
| 1078 | "float too large to pack with d format"); |
| 1079 | return -1; |
| 1080 | } |
| 1081 | else if (e < -1022) { |
| 1082 | /* Gradual underflow */ |
| 1083 | f = ldexp(f, 1022 + e); |
| 1084 | e = 0; |
| 1085 | } |
| 1086 | else if (!(e == 0 && f == 0.0)) { |
| 1087 | e += 1023; |
| 1088 | f -= 1.0; /* Get rid of leading 1 */ |
| 1089 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1090 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1091 | /* fhi receives the high 28 bits; |
| 1092 | flo the low 24 bits (== 52 bits) */ |
| 1093 | f *= 268435456.0; /* 2**28 */ |
| 1094 | fhi = (long) floor(f); /* Truncate */ |
| 1095 | f -= (double)fhi; |
| 1096 | f *= 16777216.0; /* 2**24 */ |
| 1097 | flo = (long) floor(f + 0.5); /* Round */ |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1098 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1099 | /* First byte */ |
| 1100 | *p = (s<<7) | (e>>4); |
| 1101 | p++; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1102 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1103 | /* Second byte */ |
| 1104 | *p = (unsigned char) (((e&0xF)<<4) | (fhi>>24)); |
| 1105 | p++; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1106 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1107 | /* Third byte */ |
| 1108 | *p = (unsigned char) ((fhi>>16) & 0xFF); |
| 1109 | p++; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1110 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1111 | /* Fourth byte */ |
| 1112 | *p = (unsigned char) ((fhi>>8) & 0xFF); |
| 1113 | p++; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1114 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1115 | /* Fifth byte */ |
| 1116 | *p = (unsigned char) (fhi & 0xFF); |
| 1117 | p++; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1118 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1119 | /* Sixth byte */ |
| 1120 | *p = (unsigned char) ((flo>>16) & 0xFF); |
| 1121 | p++; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1122 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1123 | /* Seventh byte */ |
| 1124 | *p = (unsigned char) ((flo>>8) & 0xFF); |
| 1125 | p++; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1126 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1127 | /* Eighth byte */ |
| 1128 | *p = (unsigned char) (flo & 0xFF); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1129 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1130 | if ((*self->write_func)(self, str, 9) < 0) |
| 1131 | return -1; |
| 1132 | } |
| 1133 | else { |
| 1134 | char c_str[250]; |
| 1135 | c_str[0] = FLOAT; |
| 1136 | PyOS_snprintf(c_str + 1, sizeof(c_str) - 1, "%.17g\n", x); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1137 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1138 | if ((*self->write_func)(self, c_str, strlen(c_str)) < 0) |
| 1139 | return -1; |
| 1140 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1141 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1142 | return 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1143 | } |
| 1144 | |
| 1145 | |
| 1146 | static int |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1147 | save_string(Picklerobject *self, PyObject *args, int doput) |
| 1148 | { |
| 1149 | int size, len; |
| 1150 | PyObject *repr=0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1151 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1152 | if ((size = PyString_Size(args)) < 0) |
| 1153 | return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1154 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1155 | if (!self->bin) { |
| 1156 | char *repr_str; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1157 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1158 | static char string = STRING; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1159 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1160 | if (!( repr = PyObject_Repr(args))) |
| 1161 | return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1162 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1163 | if ((len = PyString_Size(repr)) < 0) |
| 1164 | goto err; |
| 1165 | repr_str = PyString_AS_STRING((PyStringObject *)repr); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1166 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1167 | if ((*self->write_func)(self, &string, 1) < 0) |
| 1168 | goto err; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1169 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1170 | if ((*self->write_func)(self, repr_str, len) < 0) |
| 1171 | goto err; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1172 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1173 | if ((*self->write_func)(self, "\n", 1) < 0) |
| 1174 | goto err; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1175 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1176 | Py_XDECREF(repr); |
| 1177 | } |
| 1178 | else { |
| 1179 | int i; |
| 1180 | char c_str[5]; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1181 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1182 | if ((size = PyString_Size(args)) < 0) |
| 1183 | return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1184 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1185 | if (size < 256) { |
| 1186 | c_str[0] = SHORT_BINSTRING; |
| 1187 | c_str[1] = size; |
| 1188 | len = 2; |
| 1189 | } |
| 1190 | else { |
| 1191 | c_str[0] = BINSTRING; |
| 1192 | for (i = 1; i < 5; i++) |
| 1193 | c_str[i] = (int)(size >> ((i - 1) * 8)); |
| 1194 | len = 5; |
| 1195 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1196 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1197 | if ((*self->write_func)(self, c_str, len) < 0) |
| 1198 | return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1199 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1200 | if (size > 128 && Pdata_Check(self->file)) { |
| 1201 | if (write_other(self, NULL, 0) < 0) return -1; |
| 1202 | PDATA_APPEND(self->file, args, -1); |
| 1203 | } |
| 1204 | else { |
| 1205 | if ((*self->write_func)(self, |
| 1206 | PyString_AS_STRING((PyStringObject *)args), size) < 0) |
| 1207 | return -1; |
| 1208 | } |
| 1209 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1210 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1211 | if (doput) |
| 1212 | if (put(self, args) < 0) |
| 1213 | return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1214 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1215 | return 0; |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 1216 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1217 | err: |
| 1218 | Py_XDECREF(repr); |
| 1219 | return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1220 | } |
| 1221 | |
| 1222 | |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1223 | #ifdef Py_USING_UNICODE |
Guido van Rossum | fb10c3f | 2000-12-19 02:08:38 +0000 | [diff] [blame] | 1224 | /* A copy of PyUnicode_EncodeRawUnicodeEscape() that also translates |
| 1225 | backslash and newline characters to \uXXXX escapes. */ |
| 1226 | static PyObject * |
| 1227 | modified_EncodeRawUnicodeEscape(const Py_UNICODE *s, int size) |
| 1228 | { |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1229 | PyObject *repr; |
| 1230 | char *p; |
| 1231 | char *q; |
Guido van Rossum | fb10c3f | 2000-12-19 02:08:38 +0000 | [diff] [blame] | 1232 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1233 | static const char *hexdigit = "0123456789ABCDEF"; |
Guido van Rossum | fb10c3f | 2000-12-19 02:08:38 +0000 | [diff] [blame] | 1234 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1235 | repr = PyString_FromStringAndSize(NULL, 6 * size); |
| 1236 | if (repr == NULL) |
| 1237 | return NULL; |
| 1238 | if (size == 0) |
| 1239 | return repr; |
| 1240 | |
| 1241 | p = q = PyString_AS_STRING(repr); |
| 1242 | while (size-- > 0) { |
| 1243 | Py_UNICODE ch = *s++; |
| 1244 | /* Map 16-bit characters to '\uxxxx' */ |
| 1245 | if (ch >= 256 || ch == '\\' || ch == '\n') { |
| 1246 | *p++ = '\\'; |
| 1247 | *p++ = 'u'; |
| 1248 | *p++ = hexdigit[(ch >> 12) & 0xf]; |
| 1249 | *p++ = hexdigit[(ch >> 8) & 0xf]; |
| 1250 | *p++ = hexdigit[(ch >> 4) & 0xf]; |
| 1251 | *p++ = hexdigit[ch & 15]; |
| 1252 | } |
| 1253 | /* Copy everything else as-is */ |
| 1254 | else |
| 1255 | *p++ = (char) ch; |
| 1256 | } |
| 1257 | *p = '\0'; |
Tim Peters | 5de9842 | 2002-04-27 18:44:32 +0000 | [diff] [blame] | 1258 | _PyString_Resize(&repr, p - q); |
Guido van Rossum | fb10c3f | 2000-12-19 02:08:38 +0000 | [diff] [blame] | 1259 | return repr; |
Guido van Rossum | fb10c3f | 2000-12-19 02:08:38 +0000 | [diff] [blame] | 1260 | } |
| 1261 | |
| 1262 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1263 | static int |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1264 | save_unicode(Picklerobject *self, PyObject *args, int doput) |
| 1265 | { |
| 1266 | int size, len; |
| 1267 | PyObject *repr=0; |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 1268 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1269 | if (!PyUnicode_Check(args)) |
| 1270 | return -1; |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 1271 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1272 | if (!self->bin) { |
| 1273 | char *repr_str; |
| 1274 | static char string = UNICODE; |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 1275 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1276 | repr = modified_EncodeRawUnicodeEscape( |
| 1277 | PyUnicode_AS_UNICODE(args), PyUnicode_GET_SIZE(args)); |
| 1278 | if (!repr) |
| 1279 | return -1; |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 1280 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1281 | if ((len = PyString_Size(repr)) < 0) |
| 1282 | goto err; |
| 1283 | repr_str = PyString_AS_STRING((PyStringObject *)repr); |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 1284 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1285 | if ((*self->write_func)(self, &string, 1) < 0) |
| 1286 | goto err; |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 1287 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1288 | if ((*self->write_func)(self, repr_str, len) < 0) |
| 1289 | goto err; |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 1290 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1291 | if ((*self->write_func)(self, "\n", 1) < 0) |
| 1292 | goto err; |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 1293 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1294 | Py_XDECREF(repr); |
| 1295 | } |
| 1296 | else { |
| 1297 | int i; |
| 1298 | char c_str[5]; |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 1299 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1300 | if (!( repr = PyUnicode_AsUTF8String(args))) |
| 1301 | return -1; |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 1302 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1303 | if ((size = PyString_Size(repr)) < 0) |
| 1304 | goto err; |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 1305 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1306 | c_str[0] = BINUNICODE; |
| 1307 | for (i = 1; i < 5; i++) |
| 1308 | c_str[i] = (int)(size >> ((i - 1) * 8)); |
| 1309 | len = 5; |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 1310 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1311 | if ((*self->write_func)(self, c_str, len) < 0) |
| 1312 | goto err; |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 1313 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1314 | if (size > 128 && Pdata_Check(self->file)) { |
| 1315 | if (write_other(self, NULL, 0) < 0) |
| 1316 | goto err; |
| 1317 | PDATA_APPEND(self->file, repr, -1); |
| 1318 | } |
| 1319 | else { |
| 1320 | if ((*self->write_func)(self, PyString_AS_STRING(repr), |
| 1321 | size) < 0) |
| 1322 | goto err; |
| 1323 | } |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 1324 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1325 | Py_DECREF(repr); |
| 1326 | } |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 1327 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1328 | if (doput) |
| 1329 | if (put(self, args) < 0) |
| 1330 | return -1; |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 1331 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1332 | return 0; |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 1333 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1334 | err: |
| 1335 | Py_XDECREF(repr); |
| 1336 | return -1; |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 1337 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1338 | #endif |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 1339 | |
| 1340 | |
| 1341 | static int |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1342 | save_tuple(Picklerobject *self, PyObject *args) |
| 1343 | { |
| 1344 | PyObject *element = 0, *py_tuple_id = 0; |
| 1345 | int len, i, res = -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1346 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1347 | static char tuple = TUPLE; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1348 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1349 | if ((*self->write_func)(self, &MARKv, 1) < 0) |
| 1350 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1351 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1352 | if ((len = PyTuple_Size(args)) < 0) |
| 1353 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1354 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1355 | for (i = 0; i < len; i++) { |
| 1356 | if (!( element = PyTuple_GET_ITEM((PyTupleObject *)args, i))) |
| 1357 | goto finally; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 1358 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1359 | if (save(self, element, 0) < 0) |
| 1360 | goto finally; |
| 1361 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1362 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1363 | if (!( py_tuple_id = PyLong_FromVoidPtr(args))) |
| 1364 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1365 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1366 | if (len) { |
| 1367 | if (PyDict_GetItem(self->memo, py_tuple_id)) { |
| 1368 | if (self->bin) { |
| 1369 | static char pop_mark = POP_MARK; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 1370 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1371 | if ((*self->write_func)(self, &pop_mark, 1) < 0) |
| 1372 | goto finally; |
| 1373 | } |
| 1374 | else { |
| 1375 | static char pop = POP; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 1376 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1377 | for (i = 0; i <= len; i++) { |
| 1378 | if ((*self->write_func)(self, &pop, 1) < 0) |
| 1379 | goto finally; |
| 1380 | } |
| 1381 | } |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 1382 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1383 | if (get(self, py_tuple_id) < 0) |
| 1384 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1385 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1386 | res = 0; |
| 1387 | goto finally; |
| 1388 | } |
| 1389 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1390 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1391 | if ((*self->write_func)(self, &tuple, 1) < 0) { |
| 1392 | goto finally; |
| 1393 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1394 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1395 | if (put(self, args) < 0) |
| 1396 | goto finally; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 1397 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1398 | res = 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1399 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1400 | finally: |
| 1401 | Py_XDECREF(py_tuple_id); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1402 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1403 | return res; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1404 | } |
| 1405 | |
| 1406 | static int |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1407 | save_empty_tuple(Picklerobject *self, PyObject *args) |
| 1408 | { |
| 1409 | static char tuple = EMPTY_TUPLE; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1410 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1411 | return (*self->write_func)(self, &tuple, 1); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1412 | } |
| 1413 | |
| 1414 | |
| 1415 | static int |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1416 | save_list(Picklerobject *self, PyObject *args) |
| 1417 | { |
| 1418 | PyObject *element = 0; |
| 1419 | int s_len, len, i, using_appends, res = -1; |
| 1420 | char s[3]; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1421 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1422 | static char append = APPEND, appends = APPENDS; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1423 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1424 | if (self->fast && !fast_save_enter(self, args)) |
| 1425 | goto finally; |
Jeremy Hylton | a0fb177 | 2001-10-12 04:11:06 +0000 | [diff] [blame] | 1426 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1427 | if (self->bin) { |
| 1428 | s[0] = EMPTY_LIST; |
| 1429 | s_len = 1; |
| 1430 | } |
| 1431 | else { |
| 1432 | s[0] = MARK; |
| 1433 | s[1] = LIST; |
| 1434 | s_len = 2; |
| 1435 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1436 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1437 | if ((len = PyList_Size(args)) < 0) |
| 1438 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1439 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1440 | if ((*self->write_func)(self, s, s_len) < 0) |
| 1441 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1442 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1443 | if (len == 0) { |
| 1444 | if (put(self, args) < 0) |
| 1445 | goto finally; |
| 1446 | } |
| 1447 | else { |
| 1448 | if (put2(self, args) < 0) |
| 1449 | goto finally; |
| 1450 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 1451 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1452 | if ((using_appends = (self->bin && (len > 1)))) |
| 1453 | if ((*self->write_func)(self, &MARKv, 1) < 0) |
| 1454 | goto finally; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 1455 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1456 | for (i = 0; i < len; i++) { |
| 1457 | if (!( element = PyList_GET_ITEM((PyListObject *)args, i))) |
| 1458 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1459 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1460 | if (save(self, element, 0) < 0) |
| 1461 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1462 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1463 | if (!using_appends) { |
| 1464 | if ((*self->write_func)(self, &append, 1) < 0) |
| 1465 | goto finally; |
| 1466 | } |
| 1467 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1468 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1469 | if (using_appends) { |
| 1470 | if ((*self->write_func)(self, &appends, 1) < 0) |
| 1471 | goto finally; |
| 1472 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1473 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1474 | res = 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1475 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1476 | finally: |
| 1477 | if (self->fast && !fast_save_leave(self, args)) |
| 1478 | res = -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1479 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1480 | return res; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 1481 | } |
| 1482 | |
| 1483 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1484 | static int |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1485 | save_dict(Picklerobject *self, PyObject *args) |
| 1486 | { |
| 1487 | PyObject *key = 0, *value = 0; |
| 1488 | int i, len, res = -1, using_setitems; |
| 1489 | char s[3]; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 1490 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1491 | static char setitem = SETITEM, setitems = SETITEMS; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 1492 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1493 | if (self->fast && !fast_save_enter(self, args)) |
| 1494 | goto finally; |
Jeremy Hylton | a0fb177 | 2001-10-12 04:11:06 +0000 | [diff] [blame] | 1495 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1496 | if (self->bin) { |
| 1497 | s[0] = EMPTY_DICT; |
| 1498 | len = 1; |
| 1499 | } |
| 1500 | else { |
| 1501 | s[0] = MARK; |
| 1502 | s[1] = DICT; |
| 1503 | len = 2; |
| 1504 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 1505 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1506 | if ((*self->write_func)(self, s, len) < 0) |
| 1507 | goto finally; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 1508 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1509 | if ((len = PyDict_Size(args)) < 0) |
| 1510 | goto finally; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 1511 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1512 | if (len == 0) { |
| 1513 | if (put(self, args) < 0) |
| 1514 | goto finally; |
| 1515 | } |
| 1516 | else { |
| 1517 | if (put2(self, args) < 0) |
| 1518 | goto finally; |
| 1519 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 1520 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1521 | if ((using_setitems = (self->bin && (PyDict_Size(args) > 1)))) |
| 1522 | if ((*self->write_func)(self, &MARKv, 1) < 0) |
| 1523 | goto finally; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 1524 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1525 | i = 0; |
| 1526 | while (PyDict_Next(args, &i, &key, &value)) { |
| 1527 | if (save(self, key, 0) < 0) |
| 1528 | goto finally; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 1529 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1530 | if (save(self, value, 0) < 0) |
| 1531 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1532 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1533 | if (!using_setitems) { |
| 1534 | if ((*self->write_func)(self, &setitem, 1) < 0) |
| 1535 | goto finally; |
| 1536 | } |
| 1537 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1538 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1539 | if (using_setitems) { |
| 1540 | if ((*self->write_func)(self, &setitems, 1) < 0) |
| 1541 | goto finally; |
| 1542 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1543 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1544 | res = 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1545 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1546 | finally: |
| 1547 | if (self->fast && !fast_save_leave(self, args)) |
| 1548 | res = -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1549 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1550 | return res; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 1551 | } |
| 1552 | |
| 1553 | |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 1554 | static int |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1555 | save_inst(Picklerobject *self, PyObject *args) |
| 1556 | { |
| 1557 | PyObject *class = 0, *module = 0, *name = 0, *state = 0, |
| 1558 | *getinitargs_func = 0, *getstate_func = 0, *class_args = 0; |
| 1559 | char *module_str, *name_str; |
| 1560 | int module_size, name_size, res = -1; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 1561 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1562 | static char inst = INST, obj = OBJ, build = BUILD; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 1563 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1564 | if (self->fast && !fast_save_enter(self, args)) |
| 1565 | goto finally; |
Jeremy Hylton | a0fb177 | 2001-10-12 04:11:06 +0000 | [diff] [blame] | 1566 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1567 | if ((*self->write_func)(self, &MARKv, 1) < 0) |
| 1568 | goto finally; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 1569 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1570 | if (!( class = PyObject_GetAttr(args, __class___str))) |
| 1571 | goto finally; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 1572 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1573 | if (self->bin) { |
| 1574 | if (save(self, class, 0) < 0) |
| 1575 | goto finally; |
| 1576 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 1577 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1578 | if ((getinitargs_func = PyObject_GetAttr(args, __getinitargs___str))) { |
| 1579 | PyObject *element = 0; |
| 1580 | int i, len; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1581 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1582 | if (!( class_args = |
| 1583 | PyObject_Call(getinitargs_func, empty_tuple, NULL))) |
| 1584 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1585 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1586 | if ((len = PyObject_Size(class_args)) < 0) |
| 1587 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1588 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1589 | for (i = 0; i < len; i++) { |
| 1590 | if (!( element = PySequence_GetItem(class_args, i))) |
| 1591 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1592 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1593 | if (save(self, element, 0) < 0) { |
| 1594 | Py_DECREF(element); |
| 1595 | goto finally; |
| 1596 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1597 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1598 | Py_DECREF(element); |
| 1599 | } |
| 1600 | } |
| 1601 | else { |
| 1602 | PyErr_Clear(); |
| 1603 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 1604 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1605 | if (!self->bin) { |
| 1606 | if (!( name = ((PyClassObject *)class)->cl_name )) { |
| 1607 | PyErr_SetString(PicklingError, "class has no name"); |
| 1608 | goto finally; |
| 1609 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 1610 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1611 | if (!( module = whichmodule(class, name))) |
| 1612 | goto finally; |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 1613 | |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 1614 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1615 | if ((module_size = PyString_Size(module)) < 0 || |
| 1616 | (name_size = PyString_Size(name)) < 0) |
| 1617 | goto finally; |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 1618 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1619 | module_str = PyString_AS_STRING((PyStringObject *)module); |
| 1620 | name_str = PyString_AS_STRING((PyStringObject *)name); |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 1621 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1622 | if ((*self->write_func)(self, &inst, 1) < 0) |
| 1623 | goto finally; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 1624 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1625 | if ((*self->write_func)(self, module_str, module_size) < 0) |
| 1626 | goto finally; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 1627 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1628 | if ((*self->write_func)(self, "\n", 1) < 0) |
| 1629 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1630 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1631 | if ((*self->write_func)(self, name_str, name_size) < 0) |
| 1632 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1633 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1634 | if ((*self->write_func)(self, "\n", 1) < 0) |
| 1635 | goto finally; |
| 1636 | } |
| 1637 | else if ((*self->write_func)(self, &obj, 1) < 0) { |
| 1638 | goto finally; |
| 1639 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1640 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1641 | if ((getstate_func = PyObject_GetAttr(args, __getstate___str))) { |
| 1642 | state = PyObject_Call(getstate_func, empty_tuple, NULL); |
| 1643 | if (!state) |
| 1644 | goto finally; |
| 1645 | } |
| 1646 | else { |
| 1647 | PyErr_Clear(); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1648 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1649 | if (!( state = PyObject_GetAttr(args, __dict___str))) { |
| 1650 | PyErr_Clear(); |
| 1651 | res = 0; |
| 1652 | goto finally; |
| 1653 | } |
| 1654 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1655 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1656 | if (!PyDict_Check(state)) { |
| 1657 | if (put2(self, args) < 0) |
| 1658 | goto finally; |
| 1659 | } |
| 1660 | else { |
| 1661 | if (put(self, args) < 0) |
| 1662 | goto finally; |
| 1663 | } |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 1664 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1665 | if (save(self, state, 0) < 0) |
| 1666 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1667 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1668 | if ((*self->write_func)(self, &build, 1) < 0) |
| 1669 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1670 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1671 | res = 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1672 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1673 | finally: |
| 1674 | if (self->fast && !fast_save_leave(self, args)) |
| 1675 | res = -1; |
Jeremy Hylton | a0fb177 | 2001-10-12 04:11:06 +0000 | [diff] [blame] | 1676 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1677 | Py_XDECREF(module); |
| 1678 | Py_XDECREF(class); |
| 1679 | Py_XDECREF(state); |
| 1680 | Py_XDECREF(getinitargs_func); |
| 1681 | Py_XDECREF(getstate_func); |
| 1682 | Py_XDECREF(class_args); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1683 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1684 | return res; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 1685 | } |
| 1686 | |
| 1687 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1688 | static int |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1689 | save_global(Picklerobject *self, PyObject *args, PyObject *name) |
| 1690 | { |
Jeremy Hylton | 9ee91f1 | 2002-07-11 22:02:33 +0000 | [diff] [blame] | 1691 | PyObject *global_name = 0, *module = 0, *mod = 0, *klass = 0; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1692 | char *name_str, *module_str; |
| 1693 | int module_size, name_size, res = -1; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 1694 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1695 | static char global = GLOBAL; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 1696 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1697 | if (name) { |
| 1698 | global_name = name; |
| 1699 | Py_INCREF(global_name); |
| 1700 | } |
| 1701 | else { |
| 1702 | if (!( global_name = PyObject_GetAttr(args, __name___str))) |
| 1703 | goto finally; |
| 1704 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 1705 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1706 | if (!( module = whichmodule(args, global_name))) |
| 1707 | goto finally; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 1708 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1709 | if ((module_size = PyString_Size(module)) < 0 || |
| 1710 | (name_size = PyString_Size(global_name)) < 0) |
| 1711 | goto finally; |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 1712 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1713 | module_str = PyString_AS_STRING((PyStringObject *)module); |
| 1714 | name_str = PyString_AS_STRING((PyStringObject *)global_name); |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 1715 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1716 | mod = PyImport_ImportModule(module_str); |
| 1717 | if (mod == NULL) { |
| 1718 | /* Py_ErrClear(); ?? */ |
| 1719 | cPickle_ErrFormat(PicklingError, |
| 1720 | "Can't pickle %s: it's not found as %s.%s", |
| 1721 | "OSS", args, module, global_name); |
| 1722 | goto finally; |
| 1723 | } |
Jeremy Hylton | 0e1f7a8 | 2002-07-11 22:01:40 +0000 | [diff] [blame] | 1724 | klass = PyObject_GetAttrString(mod, name_str); |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1725 | if (klass == NULL) { |
| 1726 | cPickle_ErrFormat(PicklingError, |
| 1727 | "Can't pickle %s: it's not found as %s.%s", |
| 1728 | "OSS", args, module, global_name); |
| 1729 | goto finally; |
| 1730 | } |
| 1731 | if (klass != args) { |
Jeremy Hylton | 0e1f7a8 | 2002-07-11 22:01:40 +0000 | [diff] [blame] | 1732 | Py_DECREF(klass); |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1733 | cPickle_ErrFormat(PicklingError, |
| 1734 | "Can't pickle %s: it's not the same object as %s.%s", |
| 1735 | "OSS", args, module, global_name); |
| 1736 | goto finally; |
| 1737 | } |
Jeremy Hylton | 0e1f7a8 | 2002-07-11 22:01:40 +0000 | [diff] [blame] | 1738 | Py_DECREF(klass); |
Guido van Rossum | a92d16a | 2001-08-18 21:22:07 +0000 | [diff] [blame] | 1739 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1740 | if ((*self->write_func)(self, &global, 1) < 0) |
| 1741 | goto finally; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 1742 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1743 | if ((*self->write_func)(self, module_str, module_size) < 0) |
| 1744 | goto finally; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 1745 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1746 | if ((*self->write_func)(self, "\n", 1) < 0) |
| 1747 | goto finally; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 1748 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1749 | if ((*self->write_func)(self, name_str, name_size) < 0) |
| 1750 | goto finally; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 1751 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1752 | if ((*self->write_func)(self, "\n", 1) < 0) |
| 1753 | goto finally; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 1754 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1755 | if (put(self, args) < 0) |
| 1756 | goto finally; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 1757 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1758 | res = 0; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 1759 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1760 | finally: |
| 1761 | Py_XDECREF(module); |
| 1762 | Py_XDECREF(global_name); |
| 1763 | Py_XDECREF(mod); |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 1764 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1765 | return res; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 1766 | } |
| 1767 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1768 | static int |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1769 | save_pers(Picklerobject *self, PyObject *args, PyObject *f) |
| 1770 | { |
| 1771 | PyObject *pid = 0; |
| 1772 | int size, res = -1; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 1773 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1774 | static char persid = PERSID, binpersid = BINPERSID; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 1775 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1776 | Py_INCREF(args); |
| 1777 | ARG_TUP(self, args); |
| 1778 | if (self->arg) { |
| 1779 | pid = PyObject_Call(f, self->arg, NULL); |
| 1780 | FREE_ARG_TUP(self); |
| 1781 | } |
| 1782 | if (! pid) return -1; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 1783 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1784 | if (pid != Py_None) { |
| 1785 | if (!self->bin) { |
| 1786 | if (!PyString_Check(pid)) { |
| 1787 | PyErr_SetString(PicklingError, |
| 1788 | "persistent id must be string"); |
| 1789 | goto finally; |
| 1790 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 1791 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1792 | if ((*self->write_func)(self, &persid, 1) < 0) |
| 1793 | goto finally; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 1794 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1795 | if ((size = PyString_Size(pid)) < 0) |
| 1796 | goto finally; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 1797 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1798 | if ((*self->write_func)(self, |
| 1799 | PyString_AS_STRING((PyStringObject *)pid), size) < 0) |
| 1800 | goto finally; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 1801 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1802 | if ((*self->write_func)(self, "\n", 1) < 0) |
| 1803 | goto finally; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 1804 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1805 | res = 1; |
| 1806 | goto finally; |
| 1807 | } |
| 1808 | else if (save(self, pid, 1) >= 0) { |
| 1809 | if ((*self->write_func)(self, &binpersid, 1) < 0) |
| 1810 | res = -1; |
| 1811 | else |
| 1812 | res = 1; |
| 1813 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 1814 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1815 | goto finally; |
| 1816 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 1817 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1818 | res = 0; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 1819 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1820 | finally: |
| 1821 | Py_XDECREF(pid); |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 1822 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1823 | return res; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1824 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 1825 | |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 1826 | |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 1827 | static int |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1828 | save_reduce(Picklerobject *self, PyObject *callable, |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1829 | PyObject *tup, PyObject *state, PyObject *ob) |
| 1830 | { |
| 1831 | static char reduce = REDUCE, build = BUILD; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 1832 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1833 | if (save(self, callable, 0) < 0) |
| 1834 | return -1; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 1835 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1836 | if (save(self, tup, 0) < 0) |
| 1837 | return -1; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 1838 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1839 | if ((*self->write_func)(self, &reduce, 1) < 0) |
| 1840 | return -1; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 1841 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1842 | if (ob != NULL) { |
| 1843 | if (state && !PyDict_Check(state)) { |
| 1844 | if (put2(self, ob) < 0) |
| 1845 | return -1; |
| 1846 | } |
| 1847 | else { |
| 1848 | if (put(self, ob) < 0) |
| 1849 | return -1; |
| 1850 | } |
| 1851 | } |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 1852 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1853 | if (state) { |
| 1854 | if (save(self, state, 0) < 0) |
| 1855 | return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1856 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1857 | if ((*self->write_func)(self, &build, 1) < 0) |
| 1858 | return -1; |
| 1859 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 1860 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1861 | return 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1862 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 1863 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1864 | static int |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1865 | save(Picklerobject *self, PyObject *args, int pers_save) |
| 1866 | { |
| 1867 | PyTypeObject *type; |
| 1868 | PyObject *py_ob_id = 0, *__reduce__ = 0, *t = 0, *arg_tup = 0, |
| 1869 | *callable = 0, *state = 0; |
| 1870 | int res = -1, tmp, size; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 1871 | |
Martin v. Löwis | 5a39530 | 2002-08-04 08:20:23 +0000 | [diff] [blame] | 1872 | if (self->nesting++ > Py_GetRecursionLimit()){ |
| 1873 | PyErr_SetString(PyExc_RuntimeError, |
| 1874 | "maximum recursion depth exceeded"); |
| 1875 | goto finally; |
| 1876 | } |
| 1877 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1878 | if (!pers_save && self->pers_func) { |
| 1879 | if ((tmp = save_pers(self, args, self->pers_func)) != 0) { |
| 1880 | res = tmp; |
| 1881 | goto finally; |
| 1882 | } |
| 1883 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 1884 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1885 | if (args == Py_None) { |
| 1886 | res = save_none(self, args); |
| 1887 | goto finally; |
| 1888 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 1889 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1890 | type = args->ob_type; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 1891 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1892 | switch (type->tp_name[0]) { |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 1893 | case 'b': |
| 1894 | if (args == Py_False || args == Py_True) { |
| 1895 | res = save_bool(self, args); |
| 1896 | goto finally; |
| 1897 | } |
| 1898 | break; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1899 | case 'i': |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1900 | if (type == &PyInt_Type) { |
| 1901 | res = save_int(self, args); |
| 1902 | goto finally; |
| 1903 | } |
| 1904 | break; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 1905 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1906 | case 'l': |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1907 | if (type == &PyLong_Type) { |
| 1908 | res = save_long(self, args); |
| 1909 | goto finally; |
| 1910 | } |
| 1911 | break; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 1912 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1913 | case 'f': |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1914 | if (type == &PyFloat_Type) { |
| 1915 | res = save_float(self, args); |
| 1916 | goto finally; |
| 1917 | } |
| 1918 | break; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 1919 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1920 | case 't': |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1921 | if (type == &PyTuple_Type && PyTuple_Size(args)==0) { |
| 1922 | if (self->bin) res = save_empty_tuple(self, args); |
| 1923 | else res = save_tuple(self, args); |
| 1924 | goto finally; |
| 1925 | } |
| 1926 | break; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 1927 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1928 | case 's': |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1929 | if ((type == &PyString_Type) && (PyString_GET_SIZE(args) < 2)) { |
| 1930 | res = save_string(self, args, 0); |
| 1931 | goto finally; |
| 1932 | } |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 1933 | |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1934 | #ifdef Py_USING_UNICODE |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 1935 | case 'u': |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1936 | if ((type == &PyUnicode_Type) && (PyString_GET_SIZE(args) < 2)) { |
| 1937 | res = save_unicode(self, args, 0); |
| 1938 | goto finally; |
| 1939 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1940 | #endif |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1941 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 1942 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1943 | if (args->ob_refcnt > 1) { |
| 1944 | if (!( py_ob_id = PyLong_FromVoidPtr(args))) |
| 1945 | goto finally; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 1946 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1947 | if (PyDict_GetItem(self->memo, py_ob_id)) { |
| 1948 | if (get(self, py_ob_id) < 0) |
| 1949 | goto finally; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 1950 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1951 | res = 0; |
| 1952 | goto finally; |
| 1953 | } |
| 1954 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 1955 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1956 | switch (type->tp_name[0]) { |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1957 | case 's': |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1958 | if (type == &PyString_Type) { |
| 1959 | res = save_string(self, args, 1); |
| 1960 | goto finally; |
| 1961 | } |
| 1962 | break; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 1963 | |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1964 | #ifdef Py_USING_UNICODE |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 1965 | case 'u': |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1966 | if (type == &PyUnicode_Type) { |
| 1967 | res = save_unicode(self, args, 1); |
| 1968 | goto finally; |
| 1969 | } |
| 1970 | break; |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1971 | #endif |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 1972 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1973 | case 't': |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1974 | if (type == &PyTuple_Type) { |
| 1975 | res = save_tuple(self, args); |
| 1976 | goto finally; |
| 1977 | } |
| 1978 | if (type == &PyType_Type) { |
| 1979 | res = save_global(self, args, NULL); |
| 1980 | goto finally; |
| 1981 | } |
| 1982 | break; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 1983 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1984 | case 'l': |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1985 | if (type == &PyList_Type) { |
| 1986 | res = save_list(self, args); |
| 1987 | goto finally; |
| 1988 | } |
| 1989 | break; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1990 | |
| 1991 | case 'd': |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1992 | if (type == &PyDict_Type) { |
| 1993 | res = save_dict(self, args); |
| 1994 | goto finally; |
| 1995 | } |
| 1996 | break; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1997 | |
| 1998 | case 'i': |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 1999 | if (type == &PyInstance_Type) { |
| 2000 | res = save_inst(self, args); |
| 2001 | goto finally; |
| 2002 | } |
| 2003 | break; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2004 | |
| 2005 | case 'c': |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2006 | if (type == &PyClass_Type) { |
| 2007 | res = save_global(self, args, NULL); |
| 2008 | goto finally; |
| 2009 | } |
| 2010 | break; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2011 | |
| 2012 | case 'f': |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2013 | if (type == &PyFunction_Type) { |
| 2014 | res = save_global(self, args, NULL); |
| 2015 | goto finally; |
| 2016 | } |
| 2017 | break; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2018 | |
| 2019 | case 'b': |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2020 | if (type == &PyCFunction_Type) { |
| 2021 | res = save_global(self, args, NULL); |
| 2022 | goto finally; |
| 2023 | } |
| 2024 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2025 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2026 | if (!pers_save && self->inst_pers_func) { |
| 2027 | if ((tmp = save_pers(self, args, self->inst_pers_func)) != 0) { |
| 2028 | res = tmp; |
| 2029 | goto finally; |
| 2030 | } |
| 2031 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2032 | |
Jeremy Hylton | 39c6116 | 2002-07-16 19:47:43 +0000 | [diff] [blame] | 2033 | if (PyType_IsSubtype(type, &PyType_Type)) { |
| 2034 | res = save_global(self, args, NULL); |
| 2035 | goto finally; |
| 2036 | } |
| 2037 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2038 | if ((__reduce__ = PyDict_GetItem(dispatch_table, (PyObject *)type))) { |
| 2039 | Py_INCREF(__reduce__); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2040 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2041 | Py_INCREF(args); |
| 2042 | ARG_TUP(self, args); |
| 2043 | if (self->arg) { |
| 2044 | t = PyObject_Call(__reduce__, self->arg, NULL); |
| 2045 | FREE_ARG_TUP(self); |
| 2046 | } |
| 2047 | if (! t) goto finally; |
| 2048 | } |
| 2049 | else { |
| 2050 | PyErr_Clear(); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2051 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2052 | if ((__reduce__ = PyObject_GetAttr(args, __reduce___str))) { |
| 2053 | t = PyObject_Call(__reduce__, empty_tuple, NULL); |
| 2054 | if (!t) |
| 2055 | goto finally; |
| 2056 | } |
| 2057 | else { |
| 2058 | PyErr_Clear(); |
| 2059 | } |
| 2060 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2061 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2062 | if (t) { |
| 2063 | if (PyString_Check(t)) { |
| 2064 | res = save_global(self, args, t); |
| 2065 | goto finally; |
| 2066 | } |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 2067 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2068 | if (!PyTuple_Check(t)) { |
| 2069 | cPickle_ErrFormat(PicklingError, "Value returned by %s must " |
| 2070 | "be a tuple", "O", __reduce__); |
| 2071 | goto finally; |
| 2072 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2073 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2074 | size = PyTuple_Size(t); |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 2075 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2076 | if ((size != 3) && (size != 2)) { |
| 2077 | cPickle_ErrFormat(PicklingError, "tuple returned by %s must " |
| 2078 | "contain only two or three elements", "O", __reduce__); |
| 2079 | goto finally; |
| 2080 | } |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 2081 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2082 | callable = PyTuple_GET_ITEM(t, 0); |
Guido van Rossum | 142eeb8 | 1997-08-13 03:14:41 +0000 | [diff] [blame] | 2083 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2084 | arg_tup = PyTuple_GET_ITEM(t, 1); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2085 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2086 | if (size > 2) { |
| 2087 | state = PyTuple_GET_ITEM(t, 2); |
| 2088 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2089 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2090 | if (!( PyTuple_Check(arg_tup) || arg_tup==Py_None )) { |
| 2091 | cPickle_ErrFormat(PicklingError, "Second element of tuple " |
| 2092 | "returned by %s must be a tuple", "O", __reduce__); |
| 2093 | goto finally; |
| 2094 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2095 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2096 | res = save_reduce(self, callable, arg_tup, state, args); |
| 2097 | goto finally; |
| 2098 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2099 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2100 | PyErr_SetObject(UnpickleableError, args); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2101 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2102 | finally: |
Martin v. Löwis | 5a39530 | 2002-08-04 08:20:23 +0000 | [diff] [blame] | 2103 | self->nesting--; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2104 | Py_XDECREF(py_ob_id); |
| 2105 | Py_XDECREF(__reduce__); |
| 2106 | Py_XDECREF(t); |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 2107 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2108 | return res; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2109 | } |
| 2110 | |
| 2111 | |
| 2112 | static int |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2113 | dump(Picklerobject *self, PyObject *args) |
| 2114 | { |
| 2115 | static char stop = STOP; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2116 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2117 | if (save(self, args, 0) < 0) |
| 2118 | return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2119 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2120 | if ((*self->write_func)(self, &stop, 1) < 0) |
| 2121 | return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2122 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2123 | if ((*self->write_func)(self, NULL, 0) < 0) |
| 2124 | return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2125 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2126 | return 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2127 | } |
| 2128 | |
| 2129 | static PyObject * |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2130 | Pickle_clear_memo(Picklerobject *self, PyObject *args) |
| 2131 | { |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2132 | if (self->memo) |
| 2133 | PyDict_Clear(self->memo); |
| 2134 | Py_INCREF(Py_None); |
| 2135 | return Py_None; |
| 2136 | } |
| 2137 | |
| 2138 | static PyObject * |
| 2139 | Pickle_getvalue(Picklerobject *self, PyObject *args) |
| 2140 | { |
| 2141 | int l, i, rsize, ssize, clear=1, lm; |
| 2142 | long ik; |
| 2143 | PyObject *k, *r; |
| 2144 | char *s, *p, *have_get; |
| 2145 | Pdata *data; |
| 2146 | |
| 2147 | /* Can be called by Python code or C code */ |
| 2148 | if (args && !PyArg_ParseTuple(args, "|i:getvalue", &clear)) |
| 2149 | return NULL; |
| 2150 | |
| 2151 | /* Check to make sure we are based on a list */ |
| 2152 | if (! Pdata_Check(self->file)) { |
| 2153 | PyErr_SetString(PicklingError, |
| 2154 | "Attempt to getvalue() a non-list-based pickler"); |
| 2155 | return NULL; |
| 2156 | } |
| 2157 | |
| 2158 | /* flush write buffer */ |
| 2159 | if (write_other(self, NULL, 0) < 0) return NULL; |
| 2160 | |
| 2161 | data=(Pdata*)self->file; |
| 2162 | l=data->length; |
| 2163 | |
| 2164 | /* set up an array to hold get/put status */ |
| 2165 | if ((lm=PyDict_Size(self->memo)) < 0) return NULL; |
| 2166 | lm++; |
| 2167 | if (! (have_get=malloc((lm)*sizeof(char)))) return PyErr_NoMemory(); |
| 2168 | memset(have_get,0,lm); |
| 2169 | |
| 2170 | /* Scan for gets. */ |
| 2171 | for (rsize=0, i=l; --i >= 0; ) { |
| 2172 | k=data->data[i]; |
| 2173 | |
| 2174 | if (PyString_Check(k)) { |
| 2175 | rsize += PyString_GET_SIZE(k); |
| 2176 | } |
| 2177 | |
| 2178 | else if (PyInt_Check(k)) { /* put */ |
| 2179 | ik=PyInt_AS_LONG((PyIntObject*)k); |
| 2180 | if (ik >= lm || ik==0) { |
| 2181 | PyErr_SetString(PicklingError, |
| 2182 | "Invalid get data"); |
| 2183 | return NULL; |
| 2184 | } |
| 2185 | if (have_get[ik]) { /* with matching get */ |
| 2186 | if (ik < 256) rsize += 2; |
| 2187 | else rsize+=5; |
| 2188 | } |
| 2189 | } |
| 2190 | |
| 2191 | else if (! (PyTuple_Check(k) && |
| 2192 | PyTuple_GET_SIZE(k) == 2 && |
| 2193 | PyInt_Check((k=PyTuple_GET_ITEM(k,0)))) |
| 2194 | ) { |
| 2195 | PyErr_SetString(PicklingError, |
| 2196 | "Unexpected data in internal list"); |
| 2197 | return NULL; |
| 2198 | } |
| 2199 | |
| 2200 | else { /* put */ |
| 2201 | ik=PyInt_AS_LONG((PyIntObject*)k); |
| 2202 | if (ik >= lm || ik==0) { |
| 2203 | PyErr_SetString(PicklingError, |
| 2204 | "Invalid get data"); |
| 2205 | return NULL; |
| 2206 | } |
| 2207 | have_get[ik]=1; |
| 2208 | if (ik < 256) rsize += 2; |
| 2209 | else rsize+=5; |
| 2210 | } |
| 2211 | |
| 2212 | } |
| 2213 | |
| 2214 | /* Now generate the result */ |
| 2215 | if (!( r=PyString_FromStringAndSize(NULL,rsize))) goto err; |
| 2216 | s=PyString_AS_STRING((PyStringObject*)r); |
| 2217 | |
| 2218 | for (i=0; i<l; i++) { |
| 2219 | k=data->data[i]; |
| 2220 | |
| 2221 | if (PyString_Check(k)) { |
| 2222 | ssize=PyString_GET_SIZE(k); |
| 2223 | if (ssize) { |
| 2224 | p=PyString_AS_STRING((PyStringObject*)k); |
| 2225 | while (--ssize >= 0) *s++=*p++; |
| 2226 | } |
| 2227 | } |
| 2228 | |
| 2229 | else if (PyTuple_Check(k)) { /* get */ |
| 2230 | ik=PyInt_AS_LONG((PyIntObject*)PyTuple_GET_ITEM(k,0)); |
| 2231 | if (ik < 256) { |
| 2232 | *s++ = BINGET; |
| 2233 | *s++ = (int)(ik & 0xff); |
| 2234 | } |
| 2235 | else { |
| 2236 | *s++ = LONG_BINGET; |
| 2237 | *s++ = (int)(ik & 0xff); |
| 2238 | *s++ = (int)((ik >> 8) & 0xff); |
| 2239 | *s++ = (int)((ik >> 16) & 0xff); |
| 2240 | *s++ = (int)((ik >> 24) & 0xff); |
| 2241 | } |
| 2242 | } |
| 2243 | |
| 2244 | else { /* put */ |
| 2245 | ik=PyInt_AS_LONG((PyIntObject*)k); |
| 2246 | |
| 2247 | if (have_get[ik]) { /* with matching get */ |
| 2248 | if (ik < 256) { |
| 2249 | *s++ = BINPUT; |
| 2250 | *s++ = (int)(ik & 0xff); |
| 2251 | } |
| 2252 | else { |
| 2253 | *s++ = LONG_BINPUT; |
| 2254 | *s++ = (int)(ik & 0xff); |
| 2255 | *s++ = (int)((ik >> 8) & 0xff); |
| 2256 | *s++ = (int)((ik >> 16) & 0xff); |
| 2257 | *s++ = (int)((ik >> 24) & 0xff); |
| 2258 | } |
| 2259 | } |
| 2260 | } |
| 2261 | |
| 2262 | } |
| 2263 | |
| 2264 | if (clear) { |
| 2265 | PyDict_Clear(self->memo); |
| 2266 | Pdata_clear(data,0); |
| 2267 | } |
| 2268 | |
| 2269 | free(have_get); |
| 2270 | return r; |
| 2271 | err: |
| 2272 | free(have_get); |
Jeremy Hylton | a0fb177 | 2001-10-12 04:11:06 +0000 | [diff] [blame] | 2273 | return NULL; |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 2274 | } |
| 2275 | |
| 2276 | static PyObject * |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2277 | Pickler_dump(Picklerobject *self, PyObject *args) |
| 2278 | { |
| 2279 | PyObject *ob; |
| 2280 | int get=0; |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 2281 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2282 | if (!( PyArg_ParseTuple(args, "O|i:dump", &ob, &get))) |
| 2283 | return NULL; |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 2284 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2285 | if (dump(self, ob) < 0) |
| 2286 | return NULL; |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 2287 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2288 | if (get) return Pickle_getvalue(self, NULL); |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 2289 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2290 | /* XXX Why does dump() return self? */ |
| 2291 | Py_INCREF(self); |
| 2292 | return (PyObject*)self; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2293 | } |
| 2294 | |
| 2295 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2296 | static struct PyMethodDef Pickler_methods[] = |
| 2297 | { |
Neal Norwitz | b049325 | 2002-03-31 14:44:22 +0000 | [diff] [blame] | 2298 | {"dump", (PyCFunction)Pickler_dump, METH_VARARGS, |
Neal Norwitz | 200788c | 2002-08-13 22:20:41 +0000 | [diff] [blame] | 2299 | PyDoc_STR("dump(object) -- " |
| 2300 | "Write an object in pickle format to the object's pickle stream")}, |
Fred Drake | 0ebacc8 | 2002-05-01 20:36:39 +0000 | [diff] [blame] | 2301 | {"clear_memo", (PyCFunction)Pickle_clear_memo, METH_NOARGS, |
Neal Norwitz | 200788c | 2002-08-13 22:20:41 +0000 | [diff] [blame] | 2302 | PyDoc_STR("clear_memo() -- Clear the picklers memo")}, |
Neal Norwitz | b049325 | 2002-03-31 14:44:22 +0000 | [diff] [blame] | 2303 | {"getvalue", (PyCFunction)Pickle_getvalue, METH_VARARGS, |
Neal Norwitz | 200788c | 2002-08-13 22:20:41 +0000 | [diff] [blame] | 2304 | PyDoc_STR("getvalue() -- Finish picking a list-based pickle")}, |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2305 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2306 | }; |
| 2307 | |
| 2308 | |
| 2309 | static Picklerobject * |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2310 | newPicklerobject(PyObject *file, int bin) |
| 2311 | { |
| 2312 | Picklerobject *self; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2313 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2314 | if (!( self = PyObject_New(Picklerobject, &Picklertype))) |
| 2315 | return NULL; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2316 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2317 | self->fp = NULL; |
| 2318 | self->write = NULL; |
| 2319 | self->memo = NULL; |
| 2320 | self->arg = NULL; |
| 2321 | self->pers_func = NULL; |
| 2322 | self->inst_pers_func = NULL; |
| 2323 | self->write_buf = NULL; |
| 2324 | self->bin = bin; |
| 2325 | self->fast = 0; |
Martin v. Löwis | 5a39530 | 2002-08-04 08:20:23 +0000 | [diff] [blame] | 2326 | self->nesting = 0; |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2327 | self->fast_container = 0; |
| 2328 | self->fast_memo = NULL; |
| 2329 | self->buf_size = 0; |
| 2330 | self->dispatch_table = NULL; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2331 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2332 | if (file) |
| 2333 | Py_INCREF(file); |
| 2334 | else |
| 2335 | file=Pdata_New(); |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 2336 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2337 | if (!( self->file = file )) |
| 2338 | goto err; |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 2339 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2340 | if (!( self->memo = PyDict_New())) |
| 2341 | goto err; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2342 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2343 | if (PyFile_Check(file)) { |
| 2344 | self->fp = PyFile_AsFile(file); |
| 2345 | if (self->fp == NULL) { |
| 2346 | PyErr_SetString(PyExc_ValueError, "I/O operation on closed file"); |
| 2347 | goto err; |
| 2348 | } |
| 2349 | self->write_func = write_file; |
Guido van Rossum | c91fcaa | 1999-03-29 20:00:14 +0000 | [diff] [blame] | 2350 | } |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2351 | else if (PycStringIO_OutputCheck(file)) { |
| 2352 | self->write_func = write_cStringIO; |
| 2353 | } |
| 2354 | else if (file == Py_None) { |
| 2355 | self->write_func = write_none; |
| 2356 | } |
| 2357 | else { |
| 2358 | self->write_func = write_other; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2359 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2360 | if (! Pdata_Check(file)) { |
| 2361 | self->write = PyObject_GetAttr(file, write_str); |
| 2362 | if (!self->write) { |
| 2363 | PyErr_Clear(); |
| 2364 | PyErr_SetString(PyExc_TypeError, |
| 2365 | "argument must have 'write' " |
| 2366 | "attribute"); |
| 2367 | goto err; |
| 2368 | } |
| 2369 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2370 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2371 | if (!( self->write_buf = |
| 2372 | (char *)malloc(WRITE_BUF_SIZE * sizeof(char)))) { |
| 2373 | PyErr_NoMemory(); |
| 2374 | goto err; |
| 2375 | } |
| 2376 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2377 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2378 | if (PyEval_GetRestricted()) { |
| 2379 | /* Restricted execution, get private tables */ |
| 2380 | PyObject *m; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 2381 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2382 | if (!( m=PyImport_Import(copy_reg_str))) goto err; |
| 2383 | self->dispatch_table=PyObject_GetAttr(m, dispatch_table_str); |
| 2384 | Py_DECREF(m); |
| 2385 | if (!( self->dispatch_table )) goto err; |
| 2386 | } |
| 2387 | else { |
| 2388 | self->dispatch_table=dispatch_table; |
| 2389 | Py_INCREF(dispatch_table); |
| 2390 | } |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 2391 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2392 | return self; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 2393 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2394 | err: |
| 2395 | Py_DECREF((PyObject *)self); |
| 2396 | return NULL; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2397 | } |
| 2398 | |
| 2399 | |
| 2400 | static PyObject * |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2401 | get_Pickler(PyObject *self, PyObject *args) |
| 2402 | { |
| 2403 | PyObject *file = NULL; |
| 2404 | int bin = 1; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2405 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2406 | if (!PyArg_ParseTuple(args, "|i:Pickler", &bin)) { |
| 2407 | PyErr_Clear(); |
| 2408 | bin = 0; |
| 2409 | if (!PyArg_ParseTuple(args, "O|i:Pickler", &file, &bin)) |
| 2410 | return NULL; |
| 2411 | } |
| 2412 | return (PyObject *)newPicklerobject(file, bin); |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2413 | } |
| 2414 | |
| 2415 | |
| 2416 | static void |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2417 | Pickler_dealloc(Picklerobject *self) |
| 2418 | { |
| 2419 | Py_XDECREF(self->write); |
| 2420 | Py_XDECREF(self->memo); |
| 2421 | Py_XDECREF(self->fast_memo); |
| 2422 | Py_XDECREF(self->arg); |
| 2423 | Py_XDECREF(self->file); |
| 2424 | Py_XDECREF(self->pers_func); |
| 2425 | Py_XDECREF(self->inst_pers_func); |
| 2426 | Py_XDECREF(self->dispatch_table); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2427 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2428 | if (self->write_buf) { |
| 2429 | free(self->write_buf); |
| 2430 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2431 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2432 | PyObject_Del(self); |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2433 | } |
| 2434 | |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2435 | static PyObject * |
Jeremy Hylton | a0fb177 | 2001-10-12 04:11:06 +0000 | [diff] [blame] | 2436 | Pickler_get_pers_func(Picklerobject *p) |
| 2437 | { |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2438 | if (p->pers_func == NULL) |
| 2439 | PyErr_SetString(PyExc_AttributeError, "persistent_id"); |
| 2440 | else |
| 2441 | Py_INCREF(p->pers_func); |
| 2442 | return p->pers_func; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2443 | } |
| 2444 | |
Jeremy Hylton | a0fb177 | 2001-10-12 04:11:06 +0000 | [diff] [blame] | 2445 | static int |
| 2446 | Pickler_set_pers_func(Picklerobject *p, PyObject *v) |
| 2447 | { |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2448 | if (v == NULL) { |
| 2449 | PyErr_SetString(PyExc_TypeError, |
| 2450 | "attribute deletion is not supported"); |
| 2451 | return -1; |
| 2452 | } |
| 2453 | Py_XDECREF(p->pers_func); |
| 2454 | Py_INCREF(v); |
| 2455 | p->pers_func = v; |
| 2456 | return 0; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2457 | } |
| 2458 | |
Jeremy Hylton | a0fb177 | 2001-10-12 04:11:06 +0000 | [diff] [blame] | 2459 | static int |
| 2460 | Pickler_set_inst_pers_func(Picklerobject *p, PyObject *v) |
| 2461 | { |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2462 | if (v == NULL) { |
| 2463 | PyErr_SetString(PyExc_TypeError, |
| 2464 | "attribute deletion is not supported"); |
| 2465 | return -1; |
| 2466 | } |
| 2467 | Py_XDECREF(p->inst_pers_func); |
| 2468 | Py_INCREF(v); |
| 2469 | p->inst_pers_func = v; |
| 2470 | return 0; |
Jeremy Hylton | a0fb177 | 2001-10-12 04:11:06 +0000 | [diff] [blame] | 2471 | } |
| 2472 | |
| 2473 | static PyObject * |
| 2474 | Pickler_get_memo(Picklerobject *p) |
| 2475 | { |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2476 | if (p->memo == NULL) |
| 2477 | PyErr_SetString(PyExc_AttributeError, "memo"); |
| 2478 | else |
| 2479 | Py_INCREF(p->memo); |
| 2480 | return p->memo; |
Jeremy Hylton | a0fb177 | 2001-10-12 04:11:06 +0000 | [diff] [blame] | 2481 | } |
| 2482 | |
| 2483 | static int |
| 2484 | Pickler_set_memo(Picklerobject *p, PyObject *v) |
| 2485 | { |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2486 | if (v == NULL) { |
| 2487 | PyErr_SetString(PyExc_TypeError, |
| 2488 | "attribute deletion is not supported"); |
| 2489 | return -1; |
| 2490 | } |
| 2491 | if (!PyDict_Check(v)) { |
| 2492 | PyErr_SetString(PyExc_TypeError, "memo must be a dictionary"); |
| 2493 | return -1; |
| 2494 | } |
| 2495 | Py_XDECREF(p->memo); |
| 2496 | Py_INCREF(v); |
| 2497 | p->memo = v; |
| 2498 | return 0; |
Jeremy Hylton | a0fb177 | 2001-10-12 04:11:06 +0000 | [diff] [blame] | 2499 | } |
| 2500 | |
| 2501 | static PyObject * |
| 2502 | Pickler_get_error(Picklerobject *p) |
| 2503 | { |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2504 | /* why is this an attribute on the Pickler? */ |
| 2505 | Py_INCREF(PicklingError); |
| 2506 | return PicklingError; |
Jeremy Hylton | a0fb177 | 2001-10-12 04:11:06 +0000 | [diff] [blame] | 2507 | } |
| 2508 | |
| 2509 | static PyMemberDef Pickler_members[] = { |
| 2510 | {"binary", T_INT, offsetof(Picklerobject, bin)}, |
| 2511 | {"fast", T_INT, offsetof(Picklerobject, fast)}, |
Jeremy Hylton | 3eb46f3 | 2001-10-16 17:10:49 +0000 | [diff] [blame] | 2512 | {NULL} |
Jeremy Hylton | a0fb177 | 2001-10-12 04:11:06 +0000 | [diff] [blame] | 2513 | }; |
| 2514 | |
| 2515 | static PyGetSetDef Pickler_getsets[] = { |
| 2516 | {"persistent_id", (getter)Pickler_get_pers_func, |
| 2517 | (setter)Pickler_set_pers_func}, |
| 2518 | {"inst_persistent_id", NULL, (setter)Pickler_set_inst_pers_func}, |
| 2519 | {"memo", (getter)Pickler_get_memo, (setter)Pickler_set_memo}, |
Jeremy Hylton | 3eb46f3 | 2001-10-16 17:10:49 +0000 | [diff] [blame] | 2520 | {"PicklingError", (getter)Pickler_get_error, NULL}, |
| 2521 | {NULL} |
Jeremy Hylton | a0fb177 | 2001-10-12 04:11:06 +0000 | [diff] [blame] | 2522 | }; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2523 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2524 | PyDoc_STRVAR(Picklertype__doc__, |
| 2525 | "Objects that know how to pickle objects\n"); |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2526 | |
Guido van Rossum | 9716aaa | 1997-12-08 15:15:16 +0000 | [diff] [blame] | 2527 | static PyTypeObject Picklertype = { |
| 2528 | PyObject_HEAD_INIT(NULL) |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2529 | 0, /*ob_size*/ |
Guido van Rossum | 1464839 | 2001-12-08 18:02:58 +0000 | [diff] [blame] | 2530 | "cPickle.Pickler", /*tp_name*/ |
Jeremy Hylton | a0fb177 | 2001-10-12 04:11:06 +0000 | [diff] [blame] | 2531 | sizeof(Picklerobject), /*tp_basicsize*/ |
| 2532 | 0, |
| 2533 | (destructor)Pickler_dealloc, /* tp_dealloc */ |
| 2534 | 0, /* tp_print */ |
| 2535 | 0, /* tp_getattr */ |
| 2536 | 0, /* tp_setattr */ |
| 2537 | 0, /* tp_compare */ |
| 2538 | 0, /* tp_repr */ |
| 2539 | 0, /* tp_as_number */ |
| 2540 | 0, /* tp_as_sequence */ |
| 2541 | 0, /* tp_as_mapping */ |
| 2542 | 0, /* tp_hash */ |
| 2543 | 0, /* tp_call */ |
| 2544 | 0, /* tp_str */ |
Michael W. Hudson | 03f96bd | 2001-11-09 10:06:23 +0000 | [diff] [blame] | 2545 | 0, /* set below */ /* tp_getattro */ |
| 2546 | 0, /* set below */ /* tp_setattro */ |
Jeremy Hylton | a0fb177 | 2001-10-12 04:11:06 +0000 | [diff] [blame] | 2547 | 0, /* tp_as_buffer */ |
| 2548 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 2549 | Picklertype__doc__, /* tp_doc */ |
| 2550 | 0, /* tp_traverse */ |
| 2551 | 0, /* tp_clear */ |
| 2552 | 0, /* tp_richcompare */ |
| 2553 | 0, /* tp_weaklistoffset */ |
| 2554 | 0, /* tp_iter */ |
| 2555 | 0, /* tp_iternext */ |
| 2556 | Pickler_methods, /* tp_methods */ |
| 2557 | Pickler_members, /* tp_members */ |
| 2558 | Pickler_getsets, /* tp_getset */ |
Guido van Rossum | 9716aaa | 1997-12-08 15:15:16 +0000 | [diff] [blame] | 2559 | }; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2560 | |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2561 | static PyObject * |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2562 | find_class(PyObject *py_module_name, PyObject *py_global_name, PyObject *fc) |
| 2563 | { |
| 2564 | PyObject *global = 0, *module; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2565 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2566 | if (fc) { |
| 2567 | if (fc==Py_None) { |
| 2568 | PyErr_SetString(UnpicklingError, |
| 2569 | "Global and instance pickles are not supported."); |
| 2570 | return NULL; |
| 2571 | } |
| 2572 | return PyObject_CallFunction(fc, "OO", py_module_name, |
| 2573 | py_global_name); |
| 2574 | } |
Guido van Rossum | 1b9e0aa | 1999-04-19 17:58:18 +0000 | [diff] [blame] | 2575 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2576 | module = PySys_GetObject("modules"); |
| 2577 | if (module == NULL) |
| 2578 | return NULL; |
Jeremy Hylton | d105523 | 1998-08-11 19:52:51 +0000 | [diff] [blame] | 2579 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2580 | module = PyDict_GetItem(module, py_module_name); |
| 2581 | if (module == NULL) { |
| 2582 | module = PyImport_Import(py_module_name); |
| 2583 | if (!module) |
| 2584 | return NULL; |
| 2585 | global = PyObject_GetAttr(module, py_global_name); |
| 2586 | Py_DECREF(module); |
| 2587 | } |
| 2588 | else |
| 2589 | global = PyObject_GetAttr(module, py_global_name); |
| 2590 | return global; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2591 | } |
| 2592 | |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2593 | static int |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2594 | marker(Unpicklerobject *self) |
| 2595 | { |
| 2596 | if (self->num_marks < 1) { |
| 2597 | PyErr_SetString(UnpicklingError, "could not find MARK"); |
| 2598 | return -1; |
| 2599 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2600 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2601 | return self->marks[--self->num_marks]; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2602 | } |
| 2603 | |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 2604 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2605 | static int |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2606 | load_none(Unpicklerobject *self) |
| 2607 | { |
| 2608 | PDATA_APPEND(self->stack, Py_None, -1); |
| 2609 | return 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2610 | } |
| 2611 | |
Guido van Rossum | f9ffb03 | 1999-02-04 14:54:04 +0000 | [diff] [blame] | 2612 | static int |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2613 | bad_readline(void) |
| 2614 | { |
| 2615 | PyErr_SetString(UnpicklingError, "pickle data was truncated"); |
| 2616 | return -1; |
Guido van Rossum | f9ffb03 | 1999-02-04 14:54:04 +0000 | [diff] [blame] | 2617 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2618 | |
| 2619 | static int |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2620 | load_int(Unpicklerobject *self) |
| 2621 | { |
| 2622 | PyObject *py_int = 0; |
| 2623 | char *endptr, *s; |
| 2624 | int len, res = -1; |
| 2625 | long l; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2626 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2627 | if ((len = (*self->readline_func)(self, &s)) < 0) return -1; |
| 2628 | if (len < 2) return bad_readline(); |
| 2629 | if (!( s=pystrndup(s,len))) return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2630 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2631 | errno = 0; |
| 2632 | l = strtol(s, &endptr, 0); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2633 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2634 | if (errno || (*endptr != '\n') || (endptr[1] != '\0')) { |
| 2635 | /* Hm, maybe we've got something long. Let's try reading |
| 2636 | it as a Python long object. */ |
| 2637 | errno = 0; |
| 2638 | py_int = PyLong_FromString(s, NULL, 0); |
| 2639 | if (py_int == NULL) { |
| 2640 | PyErr_SetString(PyExc_ValueError, |
| 2641 | "could not convert string to int"); |
| 2642 | goto finally; |
| 2643 | } |
| 2644 | } |
| 2645 | else { |
Guido van Rossum | e276339 | 2002-04-05 19:30:08 +0000 | [diff] [blame] | 2646 | if (len == 3 && (l == 0 || l == 1)) { |
| 2647 | if (!( py_int = PyBool_FromLong(l))) goto finally; |
| 2648 | } |
| 2649 | else { |
| 2650 | if (!( py_int = PyInt_FromLong(l))) goto finally; |
| 2651 | } |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2652 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2653 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2654 | free(s); |
| 2655 | PDATA_PUSH(self->stack, py_int, -1); |
| 2656 | return 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2657 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2658 | finally: |
| 2659 | free(s); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2660 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2661 | return res; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2662 | } |
| 2663 | |
| 2664 | |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 2665 | static long |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2666 | calc_binint(char *s, int x) |
| 2667 | { |
| 2668 | unsigned char c; |
| 2669 | int i; |
| 2670 | long l; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2671 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2672 | for (i = 0, l = 0L; i < x; i++) { |
| 2673 | c = (unsigned char)s[i]; |
| 2674 | l |= (long)c << (i * 8); |
| 2675 | } |
Tim Peters | bfa18f7 | 2001-04-10 01:54:42 +0000 | [diff] [blame] | 2676 | #if SIZEOF_LONG > 4 |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2677 | /* Unlike BININT1 and BININT2, BININT (more accurately BININT4) |
| 2678 | * is signed, so on a box with longs bigger than 4 bytes we need |
| 2679 | * to extend a BININT's sign bit to the full width. |
| 2680 | */ |
| 2681 | if (x == 4 && l & (1L << 31)) |
| 2682 | l |= (~0L) << 32; |
Tim Peters | bfa18f7 | 2001-04-10 01:54:42 +0000 | [diff] [blame] | 2683 | #endif |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2684 | return l; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2685 | } |
| 2686 | |
| 2687 | |
| 2688 | static int |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2689 | load_binintx(Unpicklerobject *self, char *s, int x) |
| 2690 | { |
| 2691 | PyObject *py_int = 0; |
| 2692 | long l; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2693 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2694 | l = calc_binint(s, x); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2695 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2696 | if (!( py_int = PyInt_FromLong(l))) |
| 2697 | return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2698 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2699 | PDATA_PUSH(self->stack, py_int, -1); |
| 2700 | return 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2701 | } |
| 2702 | |
| 2703 | |
| 2704 | static int |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2705 | load_binint(Unpicklerobject *self) |
| 2706 | { |
| 2707 | char *s; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2708 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2709 | if ((*self->read_func)(self, &s, 4) < 0) |
| 2710 | return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2711 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2712 | return load_binintx(self, s, 4); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2713 | } |
| 2714 | |
| 2715 | |
| 2716 | static int |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2717 | load_binint1(Unpicklerobject *self) |
| 2718 | { |
| 2719 | char *s; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2720 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2721 | if ((*self->read_func)(self, &s, 1) < 0) |
| 2722 | return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2723 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2724 | return load_binintx(self, s, 1); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2725 | } |
| 2726 | |
| 2727 | |
| 2728 | static int |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2729 | load_binint2(Unpicklerobject *self) |
| 2730 | { |
| 2731 | char *s; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2732 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2733 | if ((*self->read_func)(self, &s, 2) < 0) |
| 2734 | return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2735 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2736 | return load_binintx(self, s, 2); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2737 | } |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 2738 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2739 | static int |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2740 | load_long(Unpicklerobject *self) |
| 2741 | { |
| 2742 | PyObject *l = 0; |
| 2743 | char *end, *s; |
| 2744 | int len, res = -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2745 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2746 | if ((len = (*self->readline_func)(self, &s)) < 0) return -1; |
| 2747 | if (len < 2) return bad_readline(); |
| 2748 | if (!( s=pystrndup(s,len))) return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2749 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2750 | if (!( l = PyLong_FromString(s, &end, 0))) |
| 2751 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2752 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2753 | free(s); |
| 2754 | PDATA_PUSH(self->stack, l, -1); |
| 2755 | return 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2756 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2757 | finally: |
| 2758 | free(s); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2759 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2760 | return res; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2761 | } |
| 2762 | |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 2763 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2764 | static int |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2765 | load_float(Unpicklerobject *self) |
| 2766 | { |
| 2767 | PyObject *py_float = 0; |
| 2768 | char *endptr, *s; |
| 2769 | int len, res = -1; |
| 2770 | double d; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2771 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2772 | if ((len = (*self->readline_func)(self, &s)) < 0) return -1; |
| 2773 | if (len < 2) return bad_readline(); |
| 2774 | if (!( s=pystrndup(s,len))) return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2775 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2776 | errno = 0; |
| 2777 | d = strtod(s, &endptr); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2778 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2779 | if (errno || (endptr[0] != '\n') || (endptr[1] != '\0')) { |
| 2780 | PyErr_SetString(PyExc_ValueError, |
| 2781 | "could not convert string to float"); |
| 2782 | goto finally; |
| 2783 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2784 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2785 | if (!( py_float = PyFloat_FromDouble(d))) |
| 2786 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2787 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2788 | free(s); |
| 2789 | PDATA_PUSH(self->stack, py_float, -1); |
| 2790 | return 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2791 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2792 | finally: |
| 2793 | free(s); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2794 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2795 | return res; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2796 | } |
| 2797 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2798 | static int |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2799 | load_binfloat(Unpicklerobject *self) |
| 2800 | { |
| 2801 | PyObject *py_float = 0; |
| 2802 | int s, e; |
| 2803 | long fhi, flo; |
| 2804 | double x; |
| 2805 | char *p; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2806 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2807 | if ((*self->read_func)(self, &p, 8) < 0) |
| 2808 | return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2809 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2810 | /* First byte */ |
| 2811 | s = (*p>>7) & 1; |
| 2812 | e = (*p & 0x7F) << 4; |
| 2813 | p++; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2814 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2815 | /* Second byte */ |
| 2816 | e |= (*p>>4) & 0xF; |
| 2817 | fhi = (*p & 0xF) << 24; |
| 2818 | p++; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2819 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2820 | /* Third byte */ |
| 2821 | fhi |= (*p & 0xFF) << 16; |
| 2822 | p++; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2823 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2824 | /* Fourth byte */ |
| 2825 | fhi |= (*p & 0xFF) << 8; |
| 2826 | p++; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2827 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2828 | /* Fifth byte */ |
| 2829 | fhi |= *p & 0xFF; |
| 2830 | p++; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2831 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2832 | /* Sixth byte */ |
| 2833 | flo = (*p & 0xFF) << 16; |
| 2834 | p++; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2835 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2836 | /* Seventh byte */ |
| 2837 | flo |= (*p & 0xFF) << 8; |
| 2838 | p++; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2839 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2840 | /* Eighth byte */ |
| 2841 | flo |= *p & 0xFF; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2842 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2843 | x = (double)fhi + (double)flo / 16777216.0; /* 2**24 */ |
| 2844 | x /= 268435456.0; /* 2**28 */ |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2845 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2846 | /* XXX This sadly ignores Inf/NaN */ |
| 2847 | if (e == 0) |
| 2848 | e = -1022; |
| 2849 | else { |
| 2850 | x += 1.0; |
| 2851 | e -= 1023; |
| 2852 | } |
| 2853 | x = ldexp(x, e); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2854 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2855 | if (s) |
| 2856 | x = -x; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2857 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2858 | if (!( py_float = PyFloat_FromDouble(x))) return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2859 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2860 | PDATA_PUSH(self->stack, py_float, -1); |
| 2861 | return 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2862 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2863 | |
| 2864 | static int |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2865 | load_string(Unpicklerobject *self) |
| 2866 | { |
| 2867 | PyObject *str = 0; |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 2868 | int len, res = -1; |
| 2869 | char *s, *p; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2870 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2871 | if ((len = (*self->readline_func)(self, &s)) < 0) return -1; |
| 2872 | if (len < 2) return bad_readline(); |
| 2873 | if (!( s=pystrndup(s,len))) return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2874 | |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 2875 | |
| 2876 | /* Strip outermost quotes */ |
| 2877 | while (s[len-1] <= ' ') |
| 2878 | len--; |
| 2879 | if(s[0]=='"' && s[len-1]=='"'){ |
| 2880 | s[len-1] = '\0'; |
| 2881 | p = s + 1 ; |
| 2882 | len -= 2; |
| 2883 | } else if(s[0]=='\'' && s[len-1]=='\''){ |
| 2884 | s[len-1] = '\0'; |
| 2885 | p = s + 1 ; |
| 2886 | len -= 2; |
| 2887 | } else |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2888 | goto insecure; |
| 2889 | /********************************************/ |
Guido van Rossum | 9716aaa | 1997-12-08 15:15:16 +0000 | [diff] [blame] | 2890 | |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 2891 | str = PyString_DecodeEscape(p, len, NULL, 0, NULL); |
| 2892 | if (str) { |
| 2893 | PDATA_PUSH(self->stack, str, -1); |
| 2894 | res = 0; |
| 2895 | } |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2896 | free(s); |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2897 | return res; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 2898 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2899 | insecure: |
| 2900 | free(s); |
| 2901 | PyErr_SetString(PyExc_ValueError,"insecure string pickle"); |
| 2902 | return -1; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 2903 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2904 | |
| 2905 | |
| 2906 | static int |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2907 | load_binstring(Unpicklerobject *self) |
| 2908 | { |
| 2909 | PyObject *py_string = 0; |
| 2910 | long l; |
| 2911 | char *s; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2912 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2913 | if ((*self->read_func)(self, &s, 4) < 0) return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2914 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2915 | l = calc_binint(s, 4); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2916 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2917 | if ((*self->read_func)(self, &s, l) < 0) |
| 2918 | return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2919 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2920 | if (!( py_string = PyString_FromStringAndSize(s, l))) |
| 2921 | return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2922 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2923 | PDATA_PUSH(self->stack, py_string, -1); |
| 2924 | return 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2925 | } |
| 2926 | |
| 2927 | |
| 2928 | static int |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2929 | load_short_binstring(Unpicklerobject *self) |
| 2930 | { |
| 2931 | PyObject *py_string = 0; |
| 2932 | unsigned char l; |
| 2933 | char *s; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2934 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2935 | if ((*self->read_func)(self, &s, 1) < 0) |
| 2936 | return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2937 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2938 | l = (unsigned char)s[0]; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2939 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2940 | if ((*self->read_func)(self, &s, l) < 0) return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2941 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2942 | if (!( py_string = PyString_FromStringAndSize(s, l))) return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2943 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2944 | PDATA_PUSH(self->stack, py_string, -1); |
| 2945 | return 0; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 2946 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2947 | |
| 2948 | |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 2949 | #ifdef Py_USING_UNICODE |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2950 | static int |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2951 | load_unicode(Unpicklerobject *self) |
| 2952 | { |
| 2953 | PyObject *str = 0; |
| 2954 | int len, res = -1; |
| 2955 | char *s; |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 2956 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2957 | if ((len = (*self->readline_func)(self, &s)) < 0) return -1; |
| 2958 | if (len < 1) return bad_readline(); |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 2959 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2960 | if (!( str = PyUnicode_DecodeRawUnicodeEscape(s, len - 1, NULL))) |
| 2961 | goto finally; |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 2962 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2963 | PDATA_PUSH(self->stack, str, -1); |
| 2964 | return 0; |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 2965 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2966 | finally: |
| 2967 | return res; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 2968 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 2969 | #endif |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 2970 | |
| 2971 | |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 2972 | #ifdef Py_USING_UNICODE |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 2973 | static int |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2974 | load_binunicode(Unpicklerobject *self) |
| 2975 | { |
| 2976 | PyObject *unicode; |
| 2977 | long l; |
| 2978 | char *s; |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 2979 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2980 | if ((*self->read_func)(self, &s, 4) < 0) return -1; |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 2981 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2982 | l = calc_binint(s, 4); |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 2983 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2984 | if ((*self->read_func)(self, &s, l) < 0) |
| 2985 | return -1; |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 2986 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2987 | if (!( unicode = PyUnicode_DecodeUTF8(s, l, NULL))) |
| 2988 | return -1; |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 2989 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2990 | PDATA_PUSH(self->stack, unicode, -1); |
| 2991 | return 0; |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 2992 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 2993 | #endif |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 2994 | |
| 2995 | |
| 2996 | static int |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 2997 | load_tuple(Unpicklerobject *self) |
| 2998 | { |
| 2999 | PyObject *tup; |
| 3000 | int i; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3001 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3002 | if ((i = marker(self)) < 0) return -1; |
| 3003 | if (!( tup=Pdata_popTuple(self->stack, i))) return -1; |
| 3004 | PDATA_PUSH(self->stack, tup, -1); |
| 3005 | return 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3006 | } |
| 3007 | |
| 3008 | static int |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3009 | load_empty_tuple(Unpicklerobject *self) |
| 3010 | { |
| 3011 | PyObject *tup; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3012 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3013 | if (!( tup=PyTuple_New(0))) return -1; |
| 3014 | PDATA_PUSH(self->stack, tup, -1); |
| 3015 | return 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3016 | } |
| 3017 | |
| 3018 | static int |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3019 | load_empty_list(Unpicklerobject *self) |
| 3020 | { |
| 3021 | PyObject *list; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3022 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3023 | if (!( list=PyList_New(0))) return -1; |
| 3024 | PDATA_PUSH(self->stack, list, -1); |
| 3025 | return 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3026 | } |
| 3027 | |
| 3028 | static int |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3029 | load_empty_dict(Unpicklerobject *self) |
| 3030 | { |
| 3031 | PyObject *dict; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3032 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3033 | if (!( dict=PyDict_New())) return -1; |
| 3034 | PDATA_PUSH(self->stack, dict, -1); |
| 3035 | return 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3036 | } |
| 3037 | |
| 3038 | |
| 3039 | static int |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3040 | load_list(Unpicklerobject *self) |
| 3041 | { |
| 3042 | PyObject *list = 0; |
| 3043 | int i; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3044 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3045 | if ((i = marker(self)) < 0) return -1; |
| 3046 | if (!( list=Pdata_popList(self->stack, i))) return -1; |
| 3047 | PDATA_PUSH(self->stack, list, -1); |
| 3048 | return 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3049 | } |
| 3050 | |
| 3051 | static int |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3052 | load_dict(Unpicklerobject *self) |
| 3053 | { |
| 3054 | PyObject *dict, *key, *value; |
| 3055 | int i, j, k; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3056 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3057 | if ((i = marker(self)) < 0) return -1; |
| 3058 | j=self->stack->length; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3059 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3060 | if (!( dict = PyDict_New())) return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3061 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3062 | for (k = i+1; k < j; k += 2) { |
| 3063 | key =self->stack->data[k-1]; |
| 3064 | value=self->stack->data[k ]; |
| 3065 | if (PyDict_SetItem(dict, key, value) < 0) { |
| 3066 | Py_DECREF(dict); |
| 3067 | return -1; |
| 3068 | } |
| 3069 | } |
| 3070 | Pdata_clear(self->stack, i); |
| 3071 | PDATA_PUSH(self->stack, dict, -1); |
| 3072 | return 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3073 | } |
| 3074 | |
| 3075 | static PyObject * |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3076 | Instance_New(PyObject *cls, PyObject *args) |
| 3077 | { |
| 3078 | int has_key; |
| 3079 | PyObject *safe=0, *r=0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3080 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3081 | if (PyClass_Check(cls)) { |
| 3082 | int l; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 3083 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3084 | if ((l=PyObject_Size(args)) < 0) goto err; |
| 3085 | if (!( l )) { |
| 3086 | PyObject *__getinitargs__; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 3087 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3088 | __getinitargs__ = PyObject_GetAttr(cls, |
| 3089 | __getinitargs___str); |
| 3090 | if (!__getinitargs__) { |
| 3091 | /* We have a class with no __getinitargs__, |
| 3092 | so bypass usual construction */ |
| 3093 | PyObject *inst; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 3094 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3095 | PyErr_Clear(); |
| 3096 | if (!( inst=PyInstance_NewRaw(cls, NULL))) |
| 3097 | goto err; |
| 3098 | return inst; |
| 3099 | } |
| 3100 | Py_DECREF(__getinitargs__); |
| 3101 | } |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 3102 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3103 | if ((r=PyInstance_New(cls, args, NULL))) return r; |
| 3104 | else goto err; |
| 3105 | } |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 3106 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3107 | /* Is safe_constructors always a dict? */ |
| 3108 | has_key = cPickle_PyMapping_HasKey(safe_constructors, cls); |
| 3109 | if (!has_key) { |
| 3110 | safe = PyObject_GetAttr(cls, __safe_for_unpickling___str); |
| 3111 | if (!safe || |
| 3112 | !PyObject_IsTrue(safe)) { |
| 3113 | cPickle_ErrFormat(UnpicklingError, |
| 3114 | "%s is not safe for unpickling", |
| 3115 | "O", cls); |
| 3116 | Py_XDECREF(safe); |
| 3117 | return NULL; |
| 3118 | } |
Tim Peters | 4e52ca8 | 2002-12-07 02:43:28 +0000 | [diff] [blame] | 3119 | Py_DECREF(safe); |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3120 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 3121 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3122 | if (args==Py_None) { |
| 3123 | /* Special case, call cls.__basicnew__() */ |
| 3124 | PyObject *basicnew; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 3125 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3126 | basicnew = PyObject_GetAttr(cls, __basicnew___str); |
| 3127 | if (!basicnew) return NULL; |
| 3128 | r=PyObject_CallObject(basicnew, NULL); |
| 3129 | Py_DECREF(basicnew); |
| 3130 | if (r) return r; |
| 3131 | } |
Guido van Rossum | 9716aaa | 1997-12-08 15:15:16 +0000 | [diff] [blame] | 3132 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3133 | if ((r=PyObject_CallObject(cls, args))) return r; |
Guido van Rossum | 142eeb8 | 1997-08-13 03:14:41 +0000 | [diff] [blame] | 3134 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3135 | err: |
| 3136 | { |
| 3137 | PyObject *tp, *v, *tb; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3138 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3139 | PyErr_Fetch(&tp, &v, &tb); |
| 3140 | if ((r=Py_BuildValue("OOO",v,cls,args))) { |
| 3141 | Py_XDECREF(v); |
| 3142 | v=r; |
| 3143 | } |
| 3144 | PyErr_Restore(tp,v,tb); |
| 3145 | } |
| 3146 | return NULL; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3147 | } |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 3148 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3149 | |
| 3150 | static int |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3151 | load_obj(Unpicklerobject *self) |
| 3152 | { |
| 3153 | PyObject *class, *tup, *obj=0; |
| 3154 | int i; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3155 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3156 | if ((i = marker(self)) < 0) return -1; |
| 3157 | if (!( tup=Pdata_popTuple(self->stack, i+1))) return -1; |
| 3158 | PDATA_POP(self->stack, class); |
| 3159 | if (class) { |
| 3160 | obj = Instance_New(class, tup); |
| 3161 | Py_DECREF(class); |
| 3162 | } |
| 3163 | Py_DECREF(tup); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3164 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3165 | if (! obj) return -1; |
| 3166 | PDATA_PUSH(self->stack, obj, -1); |
| 3167 | return 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3168 | } |
| 3169 | |
| 3170 | |
| 3171 | static int |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3172 | load_inst(Unpicklerobject *self) |
| 3173 | { |
| 3174 | PyObject *tup, *class=0, *obj=0, *module_name, *class_name; |
| 3175 | int i, len; |
| 3176 | char *s; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3177 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3178 | if ((i = marker(self)) < 0) return -1; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 3179 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3180 | if ((len = (*self->readline_func)(self, &s)) < 0) return -1; |
| 3181 | if (len < 2) return bad_readline(); |
| 3182 | module_name = PyString_FromStringAndSize(s, len - 1); |
| 3183 | if (!module_name) return -1; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 3184 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3185 | if ((len = (*self->readline_func)(self, &s)) >= 0) { |
| 3186 | if (len < 2) return bad_readline(); |
| 3187 | if ((class_name = PyString_FromStringAndSize(s, len - 1))) { |
| 3188 | class = find_class(module_name, class_name, |
| 3189 | self->find_class); |
| 3190 | Py_DECREF(class_name); |
| 3191 | } |
| 3192 | } |
| 3193 | Py_DECREF(module_name); |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 3194 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3195 | if (! class) return -1; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 3196 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3197 | if ((tup=Pdata_popTuple(self->stack, i))) { |
| 3198 | obj = Instance_New(class, tup); |
| 3199 | Py_DECREF(tup); |
| 3200 | } |
| 3201 | Py_DECREF(class); |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 3202 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3203 | if (! obj) return -1; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 3204 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3205 | PDATA_PUSH(self->stack, obj, -1); |
| 3206 | return 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3207 | } |
| 3208 | |
| 3209 | |
| 3210 | static int |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3211 | load_global(Unpicklerobject *self) |
| 3212 | { |
| 3213 | PyObject *class = 0, *module_name = 0, *class_name = 0; |
| 3214 | int len; |
| 3215 | char *s; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3216 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3217 | if ((len = (*self->readline_func)(self, &s)) < 0) return -1; |
| 3218 | if (len < 2) return bad_readline(); |
| 3219 | module_name = PyString_FromStringAndSize(s, len - 1); |
| 3220 | if (!module_name) return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3221 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3222 | if ((len = (*self->readline_func)(self, &s)) >= 0) { |
Tim Peters | 4e52ca8 | 2002-12-07 02:43:28 +0000 | [diff] [blame] | 3223 | if (len < 2) { |
| 3224 | Py_DECREF(module_name); |
| 3225 | return bad_readline(); |
| 3226 | } |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3227 | if ((class_name = PyString_FromStringAndSize(s, len - 1))) { |
| 3228 | class = find_class(module_name, class_name, |
| 3229 | self->find_class); |
| 3230 | Py_DECREF(class_name); |
| 3231 | } |
| 3232 | } |
| 3233 | Py_DECREF(module_name); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3234 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3235 | if (! class) return -1; |
| 3236 | PDATA_PUSH(self->stack, class, -1); |
| 3237 | return 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3238 | } |
| 3239 | |
| 3240 | |
| 3241 | static int |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3242 | load_persid(Unpicklerobject *self) |
| 3243 | { |
| 3244 | PyObject *pid = 0; |
| 3245 | int len; |
| 3246 | char *s; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3247 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3248 | if (self->pers_func) { |
| 3249 | if ((len = (*self->readline_func)(self, &s)) < 0) return -1; |
| 3250 | if (len < 2) return bad_readline(); |
| 3251 | |
| 3252 | pid = PyString_FromStringAndSize(s, len - 1); |
| 3253 | if (!pid) return -1; |
| 3254 | |
| 3255 | if (PyList_Check(self->pers_func)) { |
| 3256 | if (PyList_Append(self->pers_func, pid) < 0) { |
| 3257 | Py_DECREF(pid); |
| 3258 | return -1; |
| 3259 | } |
| 3260 | } |
| 3261 | else { |
| 3262 | ARG_TUP(self, pid); |
| 3263 | if (self->arg) { |
| 3264 | pid = PyObject_Call(self->pers_func, self->arg, |
| 3265 | NULL); |
| 3266 | FREE_ARG_TUP(self); |
| 3267 | } |
| 3268 | } |
| 3269 | |
| 3270 | if (! pid) return -1; |
| 3271 | |
| 3272 | PDATA_PUSH(self->stack, pid, -1); |
| 3273 | return 0; |
| 3274 | } |
| 3275 | else { |
| 3276 | PyErr_SetString(UnpicklingError, |
| 3277 | "A load persistent id instruction was encountered,\n" |
| 3278 | "but no persistent_load function was specified."); |
| 3279 | return -1; |
| 3280 | } |
| 3281 | } |
| 3282 | |
| 3283 | static int |
| 3284 | load_binpersid(Unpicklerobject *self) |
| 3285 | { |
| 3286 | PyObject *pid = 0; |
| 3287 | |
| 3288 | if (self->pers_func) { |
| 3289 | PDATA_POP(self->stack, pid); |
| 3290 | if (! pid) return -1; |
| 3291 | |
| 3292 | if (PyList_Check(self->pers_func)) { |
| 3293 | if (PyList_Append(self->pers_func, pid) < 0) { |
| 3294 | Py_DECREF(pid); |
| 3295 | return -1; |
| 3296 | } |
| 3297 | } |
| 3298 | else { |
| 3299 | ARG_TUP(self, pid); |
| 3300 | if (self->arg) { |
| 3301 | pid = PyObject_Call(self->pers_func, self->arg, |
| 3302 | NULL); |
| 3303 | FREE_ARG_TUP(self); |
| 3304 | } |
| 3305 | if (! pid) return -1; |
| 3306 | } |
| 3307 | |
| 3308 | PDATA_PUSH(self->stack, pid, -1); |
| 3309 | return 0; |
| 3310 | } |
| 3311 | else { |
| 3312 | PyErr_SetString(UnpicklingError, |
| 3313 | "A load persistent id instruction was encountered,\n" |
| 3314 | "but no persistent_load function was specified."); |
| 3315 | return -1; |
| 3316 | } |
| 3317 | } |
| 3318 | |
| 3319 | |
| 3320 | static int |
| 3321 | load_pop(Unpicklerobject *self) |
| 3322 | { |
| 3323 | int len; |
| 3324 | |
| 3325 | if (!( (len=self->stack->length) > 0 )) return stackUnderflow(); |
| 3326 | |
| 3327 | /* Note that we split the (pickle.py) stack into two stacks, |
| 3328 | an object stack and a mark stack. We have to be clever and |
| 3329 | pop the right one. We do this by looking at the top of the |
| 3330 | mark stack. |
| 3331 | */ |
| 3332 | |
| 3333 | if ((self->num_marks > 0) && |
| 3334 | (self->marks[self->num_marks - 1] == len)) |
| 3335 | self->num_marks--; |
| 3336 | else { |
| 3337 | len--; |
| 3338 | Py_DECREF(self->stack->data[len]); |
| 3339 | self->stack->length=len; |
| 3340 | } |
| 3341 | |
| 3342 | return 0; |
| 3343 | } |
| 3344 | |
| 3345 | |
| 3346 | static int |
| 3347 | load_pop_mark(Unpicklerobject *self) |
| 3348 | { |
| 3349 | int i; |
| 3350 | |
| 3351 | if ((i = marker(self)) < 0) |
| 3352 | return -1; |
| 3353 | |
| 3354 | Pdata_clear(self->stack, i); |
| 3355 | |
| 3356 | return 0; |
| 3357 | } |
| 3358 | |
| 3359 | |
| 3360 | static int |
| 3361 | load_dup(Unpicklerobject *self) |
| 3362 | { |
| 3363 | PyObject *last; |
| 3364 | int len; |
| 3365 | |
| 3366 | if ((len = self->stack->length) <= 0) return stackUnderflow(); |
| 3367 | last=self->stack->data[len-1]; |
| 3368 | Py_INCREF(last); |
| 3369 | PDATA_PUSH(self->stack, last, -1); |
| 3370 | return 0; |
| 3371 | } |
| 3372 | |
| 3373 | |
| 3374 | static int |
| 3375 | load_get(Unpicklerobject *self) |
| 3376 | { |
| 3377 | PyObject *py_str = 0, *value = 0; |
| 3378 | int len; |
| 3379 | char *s; |
| 3380 | int rc; |
| 3381 | |
| 3382 | if ((len = (*self->readline_func)(self, &s)) < 0) return -1; |
Guido van Rossum | f9ffb03 | 1999-02-04 14:54:04 +0000 | [diff] [blame] | 3383 | if (len < 2) return bad_readline(); |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 3384 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3385 | if (!( py_str = PyString_FromStringAndSize(s, len - 1))) return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3386 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3387 | value = PyDict_GetItem(self->memo, py_str); |
| 3388 | if (! value) { |
| 3389 | PyErr_SetObject(BadPickleGet, py_str); |
| 3390 | rc = -1; |
| 3391 | } else { |
| 3392 | PDATA_APPEND(self->stack, value, -1); |
| 3393 | rc = 0; |
| 3394 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3395 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3396 | Py_DECREF(py_str); |
| 3397 | return rc; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3398 | } |
| 3399 | |
| 3400 | |
| 3401 | static int |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3402 | load_binget(Unpicklerobject *self) |
| 3403 | { |
| 3404 | PyObject *py_key = 0, *value = 0; |
| 3405 | unsigned char key; |
| 3406 | char *s; |
| 3407 | int rc; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3408 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3409 | if ((*self->read_func)(self, &s, 1) < 0) return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3410 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3411 | key = (unsigned char)s[0]; |
| 3412 | if (!( py_key = PyInt_FromLong((long)key))) return -1; |
Guido van Rossum | ea2b715 | 2000-05-09 18:14:50 +0000 | [diff] [blame] | 3413 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3414 | value = PyDict_GetItem(self->memo, py_key); |
| 3415 | if (! value) { |
| 3416 | PyErr_SetObject(BadPickleGet, py_key); |
| 3417 | rc = -1; |
| 3418 | } else { |
| 3419 | PDATA_APPEND(self->stack, value, -1); |
| 3420 | rc = 0; |
| 3421 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3422 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3423 | Py_DECREF(py_key); |
| 3424 | return rc; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3425 | } |
| 3426 | |
| 3427 | |
| 3428 | static int |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3429 | load_long_binget(Unpicklerobject *self) |
| 3430 | { |
| 3431 | PyObject *py_key = 0, *value = 0; |
| 3432 | unsigned char c; |
| 3433 | char *s; |
| 3434 | long key; |
| 3435 | int rc; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3436 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3437 | if ((*self->read_func)(self, &s, 4) < 0) return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3438 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3439 | c = (unsigned char)s[0]; |
| 3440 | key = (long)c; |
| 3441 | c = (unsigned char)s[1]; |
| 3442 | key |= (long)c << 8; |
| 3443 | c = (unsigned char)s[2]; |
| 3444 | key |= (long)c << 16; |
| 3445 | c = (unsigned char)s[3]; |
| 3446 | key |= (long)c << 24; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3447 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3448 | if (!( py_key = PyInt_FromLong((long)key))) return -1; |
| 3449 | |
| 3450 | value = PyDict_GetItem(self->memo, py_key); |
| 3451 | if (! value) { |
| 3452 | PyErr_SetObject(BadPickleGet, py_key); |
| 3453 | rc = -1; |
| 3454 | } else { |
| 3455 | PDATA_APPEND(self->stack, value, -1); |
| 3456 | rc = 0; |
| 3457 | } |
| 3458 | |
| 3459 | Py_DECREF(py_key); |
| 3460 | return rc; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3461 | } |
| 3462 | |
| 3463 | |
| 3464 | static int |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3465 | load_put(Unpicklerobject *self) |
| 3466 | { |
| 3467 | PyObject *py_str = 0, *value = 0; |
| 3468 | int len, l; |
| 3469 | char *s; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3470 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3471 | if ((l = (*self->readline_func)(self, &s)) < 0) return -1; |
| 3472 | if (l < 2) return bad_readline(); |
| 3473 | if (!( len=self->stack->length )) return stackUnderflow(); |
| 3474 | if (!( py_str = PyString_FromStringAndSize(s, l - 1))) return -1; |
| 3475 | value=self->stack->data[len-1]; |
| 3476 | l=PyDict_SetItem(self->memo, py_str, value); |
| 3477 | Py_DECREF(py_str); |
| 3478 | return l; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3479 | } |
| 3480 | |
| 3481 | |
| 3482 | static int |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3483 | load_binput(Unpicklerobject *self) |
| 3484 | { |
| 3485 | PyObject *py_key = 0, *value = 0; |
| 3486 | unsigned char key; |
| 3487 | char *s; |
| 3488 | int len; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3489 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3490 | if ((*self->read_func)(self, &s, 1) < 0) return -1; |
| 3491 | if (!( (len=self->stack->length) > 0 )) return stackUnderflow(); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3492 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3493 | key = (unsigned char)s[0]; |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 3494 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3495 | if (!( py_key = PyInt_FromLong((long)key))) return -1; |
| 3496 | value=self->stack->data[len-1]; |
| 3497 | len=PyDict_SetItem(self->memo, py_key, value); |
| 3498 | Py_DECREF(py_key); |
| 3499 | return len; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3500 | } |
| 3501 | |
| 3502 | |
| 3503 | static int |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3504 | load_long_binput(Unpicklerobject *self) |
| 3505 | { |
| 3506 | PyObject *py_key = 0, *value = 0; |
| 3507 | long key; |
| 3508 | unsigned char c; |
| 3509 | char *s; |
| 3510 | int len; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3511 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3512 | if ((*self->read_func)(self, &s, 4) < 0) return -1; |
| 3513 | if (!( len=self->stack->length )) return stackUnderflow(); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3514 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3515 | c = (unsigned char)s[0]; |
| 3516 | key = (long)c; |
| 3517 | c = (unsigned char)s[1]; |
| 3518 | key |= (long)c << 8; |
| 3519 | c = (unsigned char)s[2]; |
| 3520 | key |= (long)c << 16; |
| 3521 | c = (unsigned char)s[3]; |
| 3522 | key |= (long)c << 24; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 3523 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3524 | if (!( py_key = PyInt_FromLong(key))) return -1; |
| 3525 | value=self->stack->data[len-1]; |
| 3526 | len=PyDict_SetItem(self->memo, py_key, value); |
| 3527 | Py_DECREF(py_key); |
| 3528 | return len; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3529 | } |
| 3530 | |
| 3531 | |
| 3532 | static int |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3533 | do_append(Unpicklerobject *self, int x) |
| 3534 | { |
| 3535 | PyObject *value = 0, *list = 0, *append_method = 0; |
| 3536 | int len, i; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3537 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3538 | len=self->stack->length; |
| 3539 | if (!( len >= x && x > 0 )) return stackUnderflow(); |
| 3540 | /* nothing to do */ |
| 3541 | if (len==x) return 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3542 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3543 | list=self->stack->data[x-1]; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3544 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3545 | if (PyList_Check(list)) { |
| 3546 | PyObject *slice; |
| 3547 | int list_len; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 3548 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3549 | slice=Pdata_popList(self->stack, x); |
| 3550 | list_len = PyList_GET_SIZE(list); |
| 3551 | i=PyList_SetSlice(list, list_len, list_len, slice); |
| 3552 | Py_DECREF(slice); |
| 3553 | return i; |
| 3554 | } |
| 3555 | else { |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3556 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3557 | if (!( append_method = PyObject_GetAttr(list, append_str))) |
| 3558 | return -1; |
| 3559 | |
| 3560 | for (i = x; i < len; i++) { |
| 3561 | PyObject *junk; |
| 3562 | |
| 3563 | value=self->stack->data[i]; |
| 3564 | junk=0; |
| 3565 | ARG_TUP(self, value); |
| 3566 | if (self->arg) { |
| 3567 | junk = PyObject_Call(append_method, self->arg, |
| 3568 | NULL); |
| 3569 | FREE_ARG_TUP(self); |
| 3570 | } |
| 3571 | if (! junk) { |
| 3572 | Pdata_clear(self->stack, i+1); |
| 3573 | self->stack->length=x; |
| 3574 | Py_DECREF(append_method); |
| 3575 | return -1; |
| 3576 | } |
| 3577 | Py_DECREF(junk); |
| 3578 | } |
| 3579 | self->stack->length=x; |
| 3580 | Py_DECREF(append_method); |
| 3581 | } |
| 3582 | |
| 3583 | return 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3584 | } |
| 3585 | |
| 3586 | |
| 3587 | static int |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3588 | load_append(Unpicklerobject *self) |
| 3589 | { |
| 3590 | return do_append(self, self->stack->length - 1); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3591 | } |
| 3592 | |
| 3593 | |
| 3594 | static int |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3595 | load_appends(Unpicklerobject *self) |
| 3596 | { |
| 3597 | return do_append(self, marker(self)); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3598 | } |
| 3599 | |
| 3600 | |
| 3601 | static int |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3602 | do_setitems(Unpicklerobject *self, int x) |
| 3603 | { |
| 3604 | PyObject *value = 0, *key = 0, *dict = 0; |
| 3605 | int len, i, r=0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3606 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3607 | if (!( (len=self->stack->length) >= x |
| 3608 | && x > 0 )) return stackUnderflow(); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3609 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3610 | dict=self->stack->data[x-1]; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3611 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3612 | for (i = x+1; i < len; i += 2) { |
| 3613 | key =self->stack->data[i-1]; |
| 3614 | value=self->stack->data[i ]; |
| 3615 | if (PyObject_SetItem(dict, key, value) < 0) { |
| 3616 | r=-1; |
| 3617 | break; |
| 3618 | } |
| 3619 | } |
| 3620 | |
| 3621 | Pdata_clear(self->stack, x); |
| 3622 | |
| 3623 | return r; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3624 | } |
| 3625 | |
| 3626 | |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 3627 | static int |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3628 | load_setitem(Unpicklerobject *self) |
| 3629 | { |
| 3630 | return do_setitems(self, self->stack->length - 2); |
| 3631 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3632 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3633 | static int |
| 3634 | load_setitems(Unpicklerobject *self) |
| 3635 | { |
| 3636 | return do_setitems(self, marker(self)); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3637 | } |
| 3638 | |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 3639 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3640 | static int |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3641 | load_build(Unpicklerobject *self) |
| 3642 | { |
| 3643 | PyObject *value = 0, *inst = 0, *instdict = 0, *d_key = 0, *d_value = 0, |
| 3644 | *junk = 0, *__setstate__ = 0; |
| 3645 | int i, r = 0; |
| 3646 | |
| 3647 | if (self->stack->length < 2) return stackUnderflow(); |
| 3648 | PDATA_POP(self->stack, value); |
| 3649 | if (! value) return -1; |
| 3650 | inst=self->stack->data[self->stack->length-1]; |
| 3651 | |
| 3652 | if ((__setstate__ = PyObject_GetAttr(inst, __setstate___str))) { |
| 3653 | ARG_TUP(self, value); |
| 3654 | if (self->arg) { |
| 3655 | junk = PyObject_Call(__setstate__, self->arg, NULL); |
| 3656 | FREE_ARG_TUP(self); |
| 3657 | } |
| 3658 | Py_DECREF(__setstate__); |
| 3659 | if (! junk) return -1; |
| 3660 | Py_DECREF(junk); |
| 3661 | return 0; |
| 3662 | } |
| 3663 | |
| 3664 | PyErr_Clear(); |
| 3665 | if ((instdict = PyObject_GetAttr(inst, __dict___str))) { |
| 3666 | i = 0; |
| 3667 | while (PyDict_Next(value, &i, &d_key, &d_value)) { |
| 3668 | if (PyObject_SetItem(instdict, d_key, d_value) < 0) { |
| 3669 | r=-1; |
| 3670 | break; |
| 3671 | } |
| 3672 | } |
| 3673 | Py_DECREF(instdict); |
| 3674 | } |
| 3675 | else r=-1; |
| 3676 | |
| 3677 | Py_XDECREF(value); |
| 3678 | |
| 3679 | return r; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3680 | } |
| 3681 | |
| 3682 | |
| 3683 | static int |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3684 | load_mark(Unpicklerobject *self) |
| 3685 | { |
| 3686 | int s; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3687 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3688 | /* Note that we split the (pickle.py) stack into two stacks, an |
| 3689 | object stack and a mark stack. Here we push a mark onto the |
| 3690 | mark stack. |
| 3691 | */ |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3692 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3693 | if ((self->num_marks + 1) >= self->marks_size) { |
| 3694 | s=self->marks_size+20; |
| 3695 | if (s <= self->num_marks) s=self->num_marks + 1; |
| 3696 | if (self->marks == NULL) |
| 3697 | self->marks=(int *)malloc(s * sizeof(int)); |
| 3698 | else |
| 3699 | self->marks=(int *)realloc(self->marks, |
| 3700 | s * sizeof(int)); |
| 3701 | if (! self->marks) { |
| 3702 | PyErr_NoMemory(); |
| 3703 | return -1; |
| 3704 | } |
| 3705 | self->marks_size = s; |
| 3706 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3707 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3708 | self->marks[self->num_marks++] = self->stack->length; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3709 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3710 | return 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3711 | } |
| 3712 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3713 | static int |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3714 | load_reduce(Unpicklerobject *self) |
| 3715 | { |
| 3716 | PyObject *callable = 0, *arg_tup = 0, *ob = 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3717 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3718 | PDATA_POP(self->stack, arg_tup); |
| 3719 | if (! arg_tup) return -1; |
| 3720 | PDATA_POP(self->stack, callable); |
| 3721 | if (callable) { |
| 3722 | ob = Instance_New(callable, arg_tup); |
| 3723 | Py_DECREF(callable); |
| 3724 | } |
| 3725 | Py_DECREF(arg_tup); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3726 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3727 | if (! ob) return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3728 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3729 | PDATA_PUSH(self->stack, ob, -1); |
| 3730 | return 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3731 | } |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 3732 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3733 | static PyObject * |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3734 | load(Unpicklerobject *self) |
| 3735 | { |
| 3736 | PyObject *err = 0, *val = 0; |
| 3737 | char *s; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3738 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3739 | self->num_marks = 0; |
| 3740 | if (self->stack->length) Pdata_clear(self->stack, 0); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3741 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3742 | while (1) { |
| 3743 | if ((*self->read_func)(self, &s, 1) < 0) |
| 3744 | break; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3745 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3746 | switch (s[0]) { |
| 3747 | case NONE: |
| 3748 | if (load_none(self) < 0) |
| 3749 | break; |
| 3750 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3751 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3752 | case BININT: |
| 3753 | if (load_binint(self) < 0) |
| 3754 | break; |
| 3755 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3756 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3757 | case BININT1: |
| 3758 | if (load_binint1(self) < 0) |
| 3759 | break; |
| 3760 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3761 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3762 | case BININT2: |
| 3763 | if (load_binint2(self) < 0) |
| 3764 | break; |
| 3765 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3766 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3767 | case INT: |
| 3768 | if (load_int(self) < 0) |
| 3769 | break; |
| 3770 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3771 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3772 | case LONG: |
| 3773 | if (load_long(self) < 0) |
| 3774 | break; |
| 3775 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3776 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3777 | case FLOAT: |
| 3778 | if (load_float(self) < 0) |
| 3779 | break; |
| 3780 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3781 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3782 | case BINFLOAT: |
| 3783 | if (load_binfloat(self) < 0) |
| 3784 | break; |
| 3785 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3786 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3787 | case BINSTRING: |
| 3788 | if (load_binstring(self) < 0) |
| 3789 | break; |
| 3790 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3791 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3792 | case SHORT_BINSTRING: |
| 3793 | if (load_short_binstring(self) < 0) |
| 3794 | break; |
| 3795 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3796 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3797 | case STRING: |
| 3798 | if (load_string(self) < 0) |
| 3799 | break; |
| 3800 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3801 | |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 3802 | #ifdef Py_USING_UNICODE |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3803 | case UNICODE: |
| 3804 | if (load_unicode(self) < 0) |
| 3805 | break; |
| 3806 | continue; |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 3807 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3808 | case BINUNICODE: |
| 3809 | if (load_binunicode(self) < 0) |
| 3810 | break; |
| 3811 | continue; |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 3812 | #endif |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 3813 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3814 | case EMPTY_TUPLE: |
| 3815 | if (load_empty_tuple(self) < 0) |
| 3816 | break; |
| 3817 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3818 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3819 | case TUPLE: |
| 3820 | if (load_tuple(self) < 0) |
| 3821 | break; |
| 3822 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3823 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3824 | case EMPTY_LIST: |
| 3825 | if (load_empty_list(self) < 0) |
| 3826 | break; |
| 3827 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3828 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3829 | case LIST: |
| 3830 | if (load_list(self) < 0) |
| 3831 | break; |
| 3832 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3833 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3834 | case EMPTY_DICT: |
| 3835 | if (load_empty_dict(self) < 0) |
| 3836 | break; |
| 3837 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3838 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3839 | case DICT: |
| 3840 | if (load_dict(self) < 0) |
| 3841 | break; |
| 3842 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3843 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3844 | case OBJ: |
| 3845 | if (load_obj(self) < 0) |
| 3846 | break; |
| 3847 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3848 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3849 | case INST: |
| 3850 | if (load_inst(self) < 0) |
| 3851 | break; |
| 3852 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3853 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3854 | case GLOBAL: |
| 3855 | if (load_global(self) < 0) |
| 3856 | break; |
| 3857 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3858 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3859 | case APPEND: |
| 3860 | if (load_append(self) < 0) |
| 3861 | break; |
| 3862 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3863 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3864 | case APPENDS: |
| 3865 | if (load_appends(self) < 0) |
| 3866 | break; |
| 3867 | continue; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 3868 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3869 | case BUILD: |
| 3870 | if (load_build(self) < 0) |
| 3871 | break; |
| 3872 | continue; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 3873 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3874 | case DUP: |
| 3875 | if (load_dup(self) < 0) |
| 3876 | break; |
| 3877 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3878 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3879 | case BINGET: |
| 3880 | if (load_binget(self) < 0) |
| 3881 | break; |
| 3882 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3883 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3884 | case LONG_BINGET: |
| 3885 | if (load_long_binget(self) < 0) |
| 3886 | break; |
| 3887 | continue; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 3888 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3889 | case GET: |
| 3890 | if (load_get(self) < 0) |
| 3891 | break; |
| 3892 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3893 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3894 | case MARK: |
| 3895 | if (load_mark(self) < 0) |
| 3896 | break; |
| 3897 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3898 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3899 | case BINPUT: |
| 3900 | if (load_binput(self) < 0) |
| 3901 | break; |
| 3902 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3903 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3904 | case LONG_BINPUT: |
| 3905 | if (load_long_binput(self) < 0) |
| 3906 | break; |
| 3907 | continue; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 3908 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3909 | case PUT: |
| 3910 | if (load_put(self) < 0) |
| 3911 | break; |
| 3912 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3913 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3914 | case POP: |
| 3915 | if (load_pop(self) < 0) |
| 3916 | break; |
| 3917 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3918 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3919 | case POP_MARK: |
| 3920 | if (load_pop_mark(self) < 0) |
| 3921 | break; |
| 3922 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3923 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3924 | case SETITEM: |
| 3925 | if (load_setitem(self) < 0) |
| 3926 | break; |
| 3927 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3928 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3929 | case SETITEMS: |
| 3930 | if (load_setitems(self) < 0) |
| 3931 | break; |
| 3932 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3933 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3934 | case STOP: |
| 3935 | break; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3936 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3937 | case PERSID: |
| 3938 | if (load_persid(self) < 0) |
| 3939 | break; |
| 3940 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3941 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3942 | case BINPERSID: |
| 3943 | if (load_binpersid(self) < 0) |
| 3944 | break; |
| 3945 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3946 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3947 | case REDUCE: |
| 3948 | if (load_reduce(self) < 0) |
| 3949 | break; |
| 3950 | continue; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3951 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3952 | case '\0': |
| 3953 | /* end of file */ |
| 3954 | PyErr_SetNone(PyExc_EOFError); |
| 3955 | break; |
Neil Schemenauer | fa79c65 | 2002-03-22 23:02:53 +0000 | [diff] [blame] | 3956 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3957 | default: |
| 3958 | cPickle_ErrFormat(UnpicklingError, |
| 3959 | "invalid load key, '%s'.", |
| 3960 | "c", s[0]); |
| 3961 | return NULL; |
| 3962 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3963 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3964 | break; |
| 3965 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3966 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3967 | if ((err = PyErr_Occurred())) { |
| 3968 | if (err == PyExc_EOFError) { |
| 3969 | PyErr_SetNone(PyExc_EOFError); |
| 3970 | } |
| 3971 | return NULL; |
| 3972 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3973 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3974 | PDATA_POP(self->stack, val); |
| 3975 | return val; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3976 | } |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 3977 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3978 | |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 3979 | /* No-load functions to support noload, which is used to |
| 3980 | find persistent references. */ |
| 3981 | |
| 3982 | static int |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3983 | noload_obj(Unpicklerobject *self) |
| 3984 | { |
| 3985 | int i; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 3986 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3987 | if ((i = marker(self)) < 0) return -1; |
| 3988 | return Pdata_clear(self->stack, i+1); |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 3989 | } |
| 3990 | |
| 3991 | |
| 3992 | static int |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3993 | noload_inst(Unpicklerobject *self) |
| 3994 | { |
| 3995 | int i; |
| 3996 | char *s; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 3997 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 3998 | if ((i = marker(self)) < 0) return -1; |
| 3999 | Pdata_clear(self->stack, i); |
| 4000 | if ((*self->readline_func)(self, &s) < 0) return -1; |
| 4001 | if ((*self->readline_func)(self, &s) < 0) return -1; |
| 4002 | PDATA_APPEND(self->stack, Py_None,-1); |
| 4003 | return 0; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 4004 | } |
| 4005 | |
| 4006 | static int |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4007 | noload_global(Unpicklerobject *self) |
| 4008 | { |
| 4009 | char *s; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 4010 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4011 | if ((*self->readline_func)(self, &s) < 0) return -1; |
| 4012 | if ((*self->readline_func)(self, &s) < 0) return -1; |
| 4013 | PDATA_APPEND(self->stack, Py_None,-1); |
| 4014 | return 0; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 4015 | } |
| 4016 | |
| 4017 | static int |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4018 | noload_reduce(Unpicklerobject *self) |
| 4019 | { |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 4020 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4021 | if (self->stack->length < 2) return stackUnderflow(); |
| 4022 | Pdata_clear(self->stack, self->stack->length-2); |
| 4023 | PDATA_APPEND(self->stack, Py_None,-1); |
| 4024 | return 0; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 4025 | } |
| 4026 | |
| 4027 | static int |
| 4028 | noload_build(Unpicklerobject *self) { |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 4029 | |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 4030 | if (self->stack->length < 1) return stackUnderflow(); |
| 4031 | Pdata_clear(self->stack, self->stack->length-1); |
Guido van Rossum | 50f385c | 1998-12-04 18:48:44 +0000 | [diff] [blame] | 4032 | return 0; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 4033 | } |
| 4034 | |
| 4035 | |
| 4036 | static PyObject * |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4037 | noload(Unpicklerobject *self) |
| 4038 | { |
| 4039 | PyObject *err = 0, *val = 0; |
| 4040 | char *s; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 4041 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4042 | self->num_marks = 0; |
| 4043 | Pdata_clear(self->stack, 0); |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 4044 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4045 | while (1) { |
| 4046 | if ((*self->read_func)(self, &s, 1) < 0) |
| 4047 | break; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 4048 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4049 | switch (s[0]) { |
| 4050 | case NONE: |
| 4051 | if (load_none(self) < 0) |
| 4052 | break; |
| 4053 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 4054 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4055 | case BININT: |
| 4056 | if (load_binint(self) < 0) |
| 4057 | break; |
| 4058 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 4059 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4060 | case BININT1: |
| 4061 | if (load_binint1(self) < 0) |
| 4062 | break; |
| 4063 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 4064 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4065 | case BININT2: |
| 4066 | if (load_binint2(self) < 0) |
| 4067 | break; |
| 4068 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 4069 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4070 | case INT: |
| 4071 | if (load_int(self) < 0) |
| 4072 | break; |
| 4073 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 4074 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4075 | case LONG: |
| 4076 | if (load_long(self) < 0) |
| 4077 | break; |
| 4078 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 4079 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4080 | case FLOAT: |
| 4081 | if (load_float(self) < 0) |
| 4082 | break; |
| 4083 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 4084 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4085 | case BINFLOAT: |
| 4086 | if (load_binfloat(self) < 0) |
| 4087 | break; |
| 4088 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 4089 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4090 | case BINSTRING: |
| 4091 | if (load_binstring(self) < 0) |
| 4092 | break; |
| 4093 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 4094 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4095 | case SHORT_BINSTRING: |
| 4096 | if (load_short_binstring(self) < 0) |
| 4097 | break; |
| 4098 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 4099 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4100 | case STRING: |
| 4101 | if (load_string(self) < 0) |
| 4102 | break; |
| 4103 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 4104 | |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 4105 | #ifdef Py_USING_UNICODE |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4106 | case UNICODE: |
| 4107 | if (load_unicode(self) < 0) |
| 4108 | break; |
| 4109 | continue; |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 4110 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4111 | case BINUNICODE: |
| 4112 | if (load_binunicode(self) < 0) |
| 4113 | break; |
| 4114 | continue; |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 4115 | #endif |
Guido van Rossum | 5fccb7c | 2000-03-10 23:11:40 +0000 | [diff] [blame] | 4116 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4117 | case EMPTY_TUPLE: |
| 4118 | if (load_empty_tuple(self) < 0) |
| 4119 | break; |
| 4120 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 4121 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4122 | case TUPLE: |
| 4123 | if (load_tuple(self) < 0) |
| 4124 | break; |
| 4125 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 4126 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4127 | case EMPTY_LIST: |
| 4128 | if (load_empty_list(self) < 0) |
| 4129 | break; |
| 4130 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 4131 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4132 | case LIST: |
| 4133 | if (load_list(self) < 0) |
| 4134 | break; |
| 4135 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 4136 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4137 | case EMPTY_DICT: |
| 4138 | if (load_empty_dict(self) < 0) |
| 4139 | break; |
| 4140 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 4141 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4142 | case DICT: |
| 4143 | if (load_dict(self) < 0) |
| 4144 | break; |
| 4145 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 4146 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4147 | case OBJ: |
| 4148 | if (noload_obj(self) < 0) |
| 4149 | break; |
| 4150 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 4151 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4152 | case INST: |
| 4153 | if (noload_inst(self) < 0) |
| 4154 | break; |
| 4155 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 4156 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4157 | case GLOBAL: |
| 4158 | if (noload_global(self) < 0) |
| 4159 | break; |
| 4160 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 4161 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4162 | case APPEND: |
| 4163 | if (load_append(self) < 0) |
| 4164 | break; |
| 4165 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 4166 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4167 | case APPENDS: |
| 4168 | if (load_appends(self) < 0) |
| 4169 | break; |
| 4170 | continue; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 4171 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4172 | case BUILD: |
| 4173 | if (noload_build(self) < 0) |
| 4174 | break; |
| 4175 | continue; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 4176 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4177 | case DUP: |
| 4178 | if (load_dup(self) < 0) |
| 4179 | break; |
| 4180 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 4181 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4182 | case BINGET: |
| 4183 | if (load_binget(self) < 0) |
| 4184 | break; |
| 4185 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 4186 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4187 | case LONG_BINGET: |
| 4188 | if (load_long_binget(self) < 0) |
| 4189 | break; |
| 4190 | continue; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 4191 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4192 | case GET: |
| 4193 | if (load_get(self) < 0) |
| 4194 | break; |
| 4195 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 4196 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4197 | case MARK: |
| 4198 | if (load_mark(self) < 0) |
| 4199 | break; |
| 4200 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 4201 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4202 | case BINPUT: |
| 4203 | if (load_binput(self) < 0) |
| 4204 | break; |
| 4205 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 4206 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4207 | case LONG_BINPUT: |
| 4208 | if (load_long_binput(self) < 0) |
| 4209 | break; |
| 4210 | continue; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 4211 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4212 | case PUT: |
| 4213 | if (load_put(self) < 0) |
| 4214 | break; |
| 4215 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 4216 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4217 | case POP: |
| 4218 | if (load_pop(self) < 0) |
| 4219 | break; |
| 4220 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 4221 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4222 | case POP_MARK: |
| 4223 | if (load_pop_mark(self) < 0) |
| 4224 | break; |
| 4225 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 4226 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4227 | case SETITEM: |
| 4228 | if (load_setitem(self) < 0) |
| 4229 | break; |
| 4230 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 4231 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4232 | case SETITEMS: |
| 4233 | if (load_setitems(self) < 0) |
| 4234 | break; |
| 4235 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 4236 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4237 | case STOP: |
| 4238 | break; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 4239 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4240 | case PERSID: |
| 4241 | if (load_persid(self) < 0) |
| 4242 | break; |
| 4243 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 4244 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4245 | case BINPERSID: |
| 4246 | if (load_binpersid(self) < 0) |
| 4247 | break; |
| 4248 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 4249 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4250 | case REDUCE: |
| 4251 | if (noload_reduce(self) < 0) |
| 4252 | break; |
| 4253 | continue; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 4254 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4255 | default: |
| 4256 | cPickle_ErrFormat(UnpicklingError, |
| 4257 | "invalid load key, '%s'.", |
| 4258 | "c", s[0]); |
| 4259 | return NULL; |
| 4260 | } |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 4261 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4262 | break; |
| 4263 | } |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 4264 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4265 | if ((err = PyErr_Occurred())) { |
| 4266 | if (err == PyExc_EOFError) { |
| 4267 | PyErr_SetNone(PyExc_EOFError); |
| 4268 | } |
| 4269 | return NULL; |
| 4270 | } |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 4271 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4272 | PDATA_POP(self->stack, val); |
| 4273 | return val; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 4274 | } |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 4275 | |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 4276 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4277 | static PyObject * |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4278 | Unpickler_load(Unpicklerobject *self, PyObject *args) |
| 4279 | { |
| 4280 | if (!( PyArg_ParseTuple(args, ":load"))) |
| 4281 | return NULL; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4282 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4283 | return load(self); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4284 | } |
| 4285 | |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 4286 | static PyObject * |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4287 | Unpickler_noload(Unpicklerobject *self, PyObject *args) |
| 4288 | { |
| 4289 | if (!( PyArg_ParseTuple(args, ":noload"))) |
| 4290 | return NULL; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 4291 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4292 | return noload(self); |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 4293 | } |
| 4294 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4295 | |
| 4296 | static struct PyMethodDef Unpickler_methods[] = { |
Neal Norwitz | b049325 | 2002-03-31 14:44:22 +0000 | [diff] [blame] | 4297 | {"load", (PyCFunction)Unpickler_load, METH_VARARGS, |
Neal Norwitz | 200788c | 2002-08-13 22:20:41 +0000 | [diff] [blame] | 4298 | PyDoc_STR("load() -- Load a pickle") |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 4299 | }, |
Neal Norwitz | b049325 | 2002-03-31 14:44:22 +0000 | [diff] [blame] | 4300 | {"noload", (PyCFunction)Unpickler_noload, METH_VARARGS, |
Neal Norwitz | 200788c | 2002-08-13 22:20:41 +0000 | [diff] [blame] | 4301 | PyDoc_STR( |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 4302 | "noload() -- not load a pickle, but go through most of the motions\n" |
| 4303 | "\n" |
| 4304 | "This function can be used to read past a pickle without instantiating\n" |
| 4305 | "any objects or importing any modules. It can also be used to find all\n" |
| 4306 | "persistent references without instantiating any objects or importing\n" |
Neal Norwitz | 200788c | 2002-08-13 22:20:41 +0000 | [diff] [blame] | 4307 | "any modules.\n") |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 4308 | }, |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4309 | {NULL, NULL} /* sentinel */ |
| 4310 | }; |
| 4311 | |
| 4312 | |
| 4313 | static Unpicklerobject * |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4314 | newUnpicklerobject(PyObject *f) |
| 4315 | { |
| 4316 | Unpicklerobject *self; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4317 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4318 | if (!( self = PyObject_New(Unpicklerobject, &Unpicklertype))) |
| 4319 | return NULL; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4320 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4321 | self->file = NULL; |
| 4322 | self->arg = NULL; |
| 4323 | self->stack = (Pdata*)Pdata_New(); |
| 4324 | self->pers_func = NULL; |
| 4325 | self->last_string = NULL; |
| 4326 | self->marks = NULL; |
| 4327 | self->num_marks = 0; |
| 4328 | self->marks_size = 0; |
| 4329 | self->buf_size = 0; |
| 4330 | self->read = NULL; |
| 4331 | self->readline = NULL; |
| 4332 | self->safe_constructors = NULL; |
| 4333 | self->find_class = NULL; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4334 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4335 | if (!( self->memo = PyDict_New())) |
| 4336 | goto err; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4337 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4338 | Py_INCREF(f); |
| 4339 | self->file = f; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4340 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4341 | /* Set read, readline based on type of f */ |
| 4342 | if (PyFile_Check(f)) { |
| 4343 | self->fp = PyFile_AsFile(f); |
| 4344 | if (self->fp == NULL) { |
| 4345 | PyErr_SetString(PyExc_ValueError, |
| 4346 | "I/O operation on closed file"); |
| 4347 | goto err; |
| 4348 | } |
| 4349 | self->read_func = read_file; |
| 4350 | self->readline_func = readline_file; |
Guido van Rossum | c91fcaa | 1999-03-29 20:00:14 +0000 | [diff] [blame] | 4351 | } |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4352 | else if (PycStringIO_InputCheck(f)) { |
| 4353 | self->fp = NULL; |
| 4354 | self->read_func = read_cStringIO; |
| 4355 | self->readline_func = readline_cStringIO; |
| 4356 | } |
| 4357 | else { |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4358 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4359 | self->fp = NULL; |
| 4360 | self->read_func = read_other; |
| 4361 | self->readline_func = readline_other; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4362 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4363 | if (!( (self->readline = PyObject_GetAttr(f, readline_str)) && |
| 4364 | (self->read = PyObject_GetAttr(f, read_str)))) { |
| 4365 | PyErr_Clear(); |
| 4366 | PyErr_SetString( PyExc_TypeError, |
| 4367 | "argument must have 'read' and " |
| 4368 | "'readline' attributes" ); |
| 4369 | goto err; |
| 4370 | } |
| 4371 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4372 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4373 | if (PyEval_GetRestricted()) { |
| 4374 | /* Restricted execution, get private tables */ |
| 4375 | PyObject *m; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 4376 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4377 | if (!( m=PyImport_Import(copy_reg_str))) goto err; |
| 4378 | self->safe_constructors=PyObject_GetAttr(m, |
| 4379 | safe_constructors_str); |
| 4380 | Py_DECREF(m); |
| 4381 | if (!( self->safe_constructors )) goto err; |
| 4382 | } |
| 4383 | else { |
| 4384 | self->safe_constructors=safe_constructors; |
| 4385 | Py_INCREF(safe_constructors); |
| 4386 | } |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 4387 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4388 | return self; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 4389 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4390 | err: |
| 4391 | Py_DECREF((PyObject *)self); |
| 4392 | return NULL; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 4393 | } |
| 4394 | |
| 4395 | |
| 4396 | static PyObject * |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4397 | get_Unpickler(PyObject *self, PyObject *args) |
| 4398 | { |
| 4399 | PyObject *file; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 4400 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4401 | if (!( PyArg_ParseTuple(args, "O:Unpickler", &file))) |
| 4402 | return NULL; |
| 4403 | return (PyObject *)newUnpicklerobject(file); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4404 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 4405 | |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 4406 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4407 | static void |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4408 | Unpickler_dealloc(Unpicklerobject *self) |
| 4409 | { |
| 4410 | Py_XDECREF(self->readline); |
| 4411 | Py_XDECREF(self->read); |
| 4412 | Py_XDECREF(self->file); |
| 4413 | Py_XDECREF(self->memo); |
| 4414 | Py_XDECREF(self->stack); |
| 4415 | Py_XDECREF(self->pers_func); |
| 4416 | Py_XDECREF(self->arg); |
| 4417 | Py_XDECREF(self->last_string); |
| 4418 | Py_XDECREF(self->safe_constructors); |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 4419 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4420 | if (self->marks) { |
| 4421 | free(self->marks); |
| 4422 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 4423 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4424 | if (self->buf_size) { |
| 4425 | free(self->buf); |
| 4426 | } |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 4427 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4428 | PyObject_Del(self); |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 4429 | } |
| 4430 | |
| 4431 | |
| 4432 | static PyObject * |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4433 | Unpickler_getattr(Unpicklerobject *self, char *name) |
| 4434 | { |
| 4435 | if (!strcmp(name, "persistent_load")) { |
| 4436 | if (!self->pers_func) { |
| 4437 | PyErr_SetString(PyExc_AttributeError, name); |
| 4438 | return NULL; |
| 4439 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 4440 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4441 | Py_INCREF(self->pers_func); |
| 4442 | return self->pers_func; |
| 4443 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 4444 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4445 | if (!strcmp(name, "find_global")) { |
| 4446 | if (!self->find_class) { |
| 4447 | PyErr_SetString(PyExc_AttributeError, name); |
| 4448 | return NULL; |
| 4449 | } |
Guido van Rossum | 1b9e0aa | 1999-04-19 17:58:18 +0000 | [diff] [blame] | 4450 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4451 | Py_INCREF(self->find_class); |
| 4452 | return self->find_class; |
| 4453 | } |
Guido van Rossum | 1b9e0aa | 1999-04-19 17:58:18 +0000 | [diff] [blame] | 4454 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4455 | if (!strcmp(name, "memo")) { |
| 4456 | if (!self->memo) { |
| 4457 | PyErr_SetString(PyExc_AttributeError, name); |
| 4458 | return NULL; |
| 4459 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 4460 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4461 | Py_INCREF(self->memo); |
| 4462 | return self->memo; |
| 4463 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 4464 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4465 | if (!strcmp(name, "UnpicklingError")) { |
| 4466 | Py_INCREF(UnpicklingError); |
| 4467 | return UnpicklingError; |
| 4468 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 4469 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4470 | return Py_FindMethod(Unpickler_methods, (PyObject *)self, name); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4471 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 4472 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4473 | |
| 4474 | static int |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4475 | Unpickler_setattr(Unpicklerobject *self, char *name, PyObject *value) |
| 4476 | { |
Jeremy Hylton | ce616e4 | 1998-08-13 23:13:52 +0000 | [diff] [blame] | 4477 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4478 | if (!strcmp(name, "persistent_load")) { |
| 4479 | Py_XDECREF(self->pers_func); |
| 4480 | self->pers_func = value; |
| 4481 | Py_XINCREF(value); |
| 4482 | return 0; |
| 4483 | } |
Guido van Rossum | 1b9e0aa | 1999-04-19 17:58:18 +0000 | [diff] [blame] | 4484 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4485 | if (!strcmp(name, "find_global")) { |
| 4486 | Py_XDECREF(self->find_class); |
| 4487 | self->find_class = value; |
| 4488 | Py_XINCREF(value); |
| 4489 | return 0; |
| 4490 | } |
Guido van Rossum | 1b9e0aa | 1999-04-19 17:58:18 +0000 | [diff] [blame] | 4491 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4492 | if (! value) { |
| 4493 | PyErr_SetString(PyExc_TypeError, |
| 4494 | "attribute deletion is not supported"); |
| 4495 | return -1; |
| 4496 | } |
Jeremy Hylton | ce616e4 | 1998-08-13 23:13:52 +0000 | [diff] [blame] | 4497 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4498 | if (strcmp(name, "memo") == 0) { |
| 4499 | if (!PyDict_Check(value)) { |
| 4500 | PyErr_SetString(PyExc_TypeError, |
| 4501 | "memo must be a dictionary"); |
| 4502 | return -1; |
| 4503 | } |
| 4504 | Py_XDECREF(self->memo); |
| 4505 | self->memo = value; |
| 4506 | Py_INCREF(value); |
| 4507 | return 0; |
| 4508 | } |
Jeremy Hylton | ce616e4 | 1998-08-13 23:13:52 +0000 | [diff] [blame] | 4509 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4510 | PyErr_SetString(PyExc_AttributeError, name); |
| 4511 | return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4512 | } |
| 4513 | |
| 4514 | |
| 4515 | static PyObject * |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4516 | cpm_dump(PyObject *self, PyObject *args) |
| 4517 | { |
| 4518 | PyObject *ob, *file, *res = NULL; |
| 4519 | Picklerobject *pickler = 0; |
| 4520 | int bin = 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4521 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4522 | if (!( PyArg_ParseTuple(args, "OO|i", &ob, &file, &bin))) |
| 4523 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4524 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4525 | if (!( pickler = newPicklerobject(file, bin))) |
| 4526 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4527 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4528 | if (dump(pickler, ob) < 0) |
| 4529 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4530 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4531 | Py_INCREF(Py_None); |
| 4532 | res = Py_None; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4533 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4534 | finally: |
| 4535 | Py_XDECREF(pickler); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4536 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4537 | return res; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4538 | } |
| 4539 | |
| 4540 | |
| 4541 | static PyObject * |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4542 | cpm_dumps(PyObject *self, PyObject *args) |
| 4543 | { |
| 4544 | PyObject *ob, *file = 0, *res = NULL; |
| 4545 | Picklerobject *pickler = 0; |
| 4546 | int bin = 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4547 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4548 | if (!( PyArg_ParseTuple(args, "O|i:dumps", &ob, &bin))) |
| 4549 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4550 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4551 | if (!( file = PycStringIO->NewOutput(128))) |
| 4552 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4553 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4554 | if (!( pickler = newPicklerobject(file, bin))) |
| 4555 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4556 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4557 | if (dump(pickler, ob) < 0) |
| 4558 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4559 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4560 | res = PycStringIO->cgetvalue(file); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4561 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4562 | finally: |
| 4563 | Py_XDECREF(pickler); |
| 4564 | Py_XDECREF(file); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4565 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4566 | return res; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 4567 | } |
| 4568 | |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 4569 | |
| 4570 | static PyObject * |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4571 | cpm_load(PyObject *self, PyObject *args) |
| 4572 | { |
| 4573 | Unpicklerobject *unpickler = 0; |
| 4574 | PyObject *ob, *res = NULL; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 4575 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4576 | if (!( PyArg_ParseTuple(args, "O:load", &ob))) |
| 4577 | goto finally; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 4578 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4579 | if (!( unpickler = newUnpicklerobject(ob))) |
| 4580 | goto finally; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 4581 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4582 | res = load(unpickler); |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 4583 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4584 | finally: |
| 4585 | Py_XDECREF(unpickler); |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 4586 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4587 | return res; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 4588 | } |
| 4589 | |
| 4590 | |
| 4591 | static PyObject * |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4592 | cpm_loads(PyObject *self, PyObject *args) |
| 4593 | { |
| 4594 | PyObject *ob, *file = 0, *res = NULL; |
| 4595 | Unpicklerobject *unpickler = 0; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 4596 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4597 | if (!( PyArg_ParseTuple(args, "S:loads", &ob))) |
| 4598 | goto finally; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 4599 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4600 | if (!( file = PycStringIO->NewInput(ob))) |
| 4601 | goto finally; |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 4602 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4603 | if (!( unpickler = newUnpicklerobject(file))) |
| 4604 | goto finally; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 4605 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4606 | res = load(unpickler); |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 4607 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4608 | finally: |
| 4609 | Py_XDECREF(file); |
| 4610 | Py_XDECREF(unpickler); |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 4611 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4612 | return res; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 4613 | } |
| 4614 | |
| 4615 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4616 | PyDoc_STRVAR(Unpicklertype__doc__, |
| 4617 | "Objects that know how to unpickle"); |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 4618 | |
Guido van Rossum | 9716aaa | 1997-12-08 15:15:16 +0000 | [diff] [blame] | 4619 | static PyTypeObject Unpicklertype = { |
| 4620 | PyObject_HEAD_INIT(NULL) |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4621 | 0, /*ob_size*/ |
Guido van Rossum | 1464839 | 2001-12-08 18:02:58 +0000 | [diff] [blame] | 4622 | "cPickle.Unpickler", /*tp_name*/ |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4623 | sizeof(Unpicklerobject), /*tp_basicsize*/ |
| 4624 | 0, /*tp_itemsize*/ |
| 4625 | /* methods */ |
| 4626 | (destructor)Unpickler_dealloc, /*tp_dealloc*/ |
| 4627 | (printfunc)0, /*tp_print*/ |
| 4628 | (getattrfunc)Unpickler_getattr, /*tp_getattr*/ |
| 4629 | (setattrfunc)Unpickler_setattr, /*tp_setattr*/ |
| 4630 | (cmpfunc)0, /*tp_compare*/ |
| 4631 | (reprfunc)0, /*tp_repr*/ |
| 4632 | 0, /*tp_as_number*/ |
| 4633 | 0, /*tp_as_sequence*/ |
| 4634 | 0, /*tp_as_mapping*/ |
| 4635 | (hashfunc)0, /*tp_hash*/ |
| 4636 | (ternaryfunc)0, /*tp_call*/ |
| 4637 | (reprfunc)0, /*tp_str*/ |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 4638 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4639 | /* Space for future expansion */ |
| 4640 | 0L,0L,0L,0L, |
| 4641 | Unpicklertype__doc__ /* Documentation string */ |
Guido van Rossum | 9716aaa | 1997-12-08 15:15:16 +0000 | [diff] [blame] | 4642 | }; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 4643 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4644 | static struct PyMethodDef cPickle_methods[] = { |
Neal Norwitz | b049325 | 2002-03-31 14:44:22 +0000 | [diff] [blame] | 4645 | {"dump", (PyCFunction)cpm_dump, METH_VARARGS, |
Neal Norwitz | 200788c | 2002-08-13 22:20:41 +0000 | [diff] [blame] | 4646 | PyDoc_STR("dump(object, file, [binary]) --" |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 4647 | "Write an object in pickle format to the given file\n" |
| 4648 | "\n" |
| 4649 | "If the optional argument, binary, is provided and is true, then the\n" |
| 4650 | "pickle will be written in binary format, which is more space and\n" |
Neal Norwitz | 200788c | 2002-08-13 22:20:41 +0000 | [diff] [blame] | 4651 | "computationally efficient. \n") |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 4652 | }, |
Neal Norwitz | b049325 | 2002-03-31 14:44:22 +0000 | [diff] [blame] | 4653 | {"dumps", (PyCFunction)cpm_dumps, METH_VARARGS, |
Neal Norwitz | 200788c | 2002-08-13 22:20:41 +0000 | [diff] [blame] | 4654 | PyDoc_STR("dumps(object, [binary]) --" |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 4655 | "Return a string containing an object in pickle format\n" |
| 4656 | "\n" |
| 4657 | "If the optional argument, binary, is provided and is true, then the\n" |
| 4658 | "pickle will be written in binary format, which is more space and\n" |
Neal Norwitz | 200788c | 2002-08-13 22:20:41 +0000 | [diff] [blame] | 4659 | "computationally efficient. \n") |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 4660 | }, |
Neal Norwitz | b049325 | 2002-03-31 14:44:22 +0000 | [diff] [blame] | 4661 | {"load", (PyCFunction)cpm_load, METH_VARARGS, |
Neal Norwitz | 200788c | 2002-08-13 22:20:41 +0000 | [diff] [blame] | 4662 | PyDoc_STR("load(file) -- Load a pickle from the given file")}, |
Neal Norwitz | b049325 | 2002-03-31 14:44:22 +0000 | [diff] [blame] | 4663 | {"loads", (PyCFunction)cpm_loads, METH_VARARGS, |
Neal Norwitz | 200788c | 2002-08-13 22:20:41 +0000 | [diff] [blame] | 4664 | PyDoc_STR("loads(string) -- Load a pickle from the given string")}, |
Neal Norwitz | b049325 | 2002-03-31 14:44:22 +0000 | [diff] [blame] | 4665 | {"Pickler", (PyCFunction)get_Pickler, METH_VARARGS, |
Neal Norwitz | 200788c | 2002-08-13 22:20:41 +0000 | [diff] [blame] | 4666 | PyDoc_STR("Pickler(file, [binary]) -- Create a pickler\n" |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 4667 | "\n" |
| 4668 | "If the optional argument, binary, is provided and is true, then\n" |
| 4669 | "pickles will be written in binary format, which is more space and\n" |
Neal Norwitz | 200788c | 2002-08-13 22:20:41 +0000 | [diff] [blame] | 4670 | "computationally efficient. \n") |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 4671 | }, |
Neal Norwitz | b049325 | 2002-03-31 14:44:22 +0000 | [diff] [blame] | 4672 | {"Unpickler", (PyCFunction)get_Unpickler, METH_VARARGS, |
Neal Norwitz | 200788c | 2002-08-13 22:20:41 +0000 | [diff] [blame] | 4673 | PyDoc_STR("Unpickler(file) -- Create an unpickler")}, |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 4674 | { NULL, NULL } |
| 4675 | }; |
| 4676 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4677 | static int |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4678 | init_stuff(PyObject *module_dict) |
| 4679 | { |
| 4680 | PyObject *copy_reg, *t, *r; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4681 | |
Martin v. Löwis | 43c9d8a | 2002-04-01 12:34:33 +0000 | [diff] [blame] | 4682 | #define INIT_STR(S) if (!( S ## _str=PyString_InternFromString(#S))) return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4683 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4684 | INIT_STR(__class__); |
| 4685 | INIT_STR(__getinitargs__); |
| 4686 | INIT_STR(__dict__); |
| 4687 | INIT_STR(__getstate__); |
| 4688 | INIT_STR(__setstate__); |
| 4689 | INIT_STR(__name__); |
| 4690 | INIT_STR(__main__); |
| 4691 | INIT_STR(__reduce__); |
| 4692 | INIT_STR(write); |
| 4693 | INIT_STR(__safe_for_unpickling__); |
| 4694 | INIT_STR(append); |
| 4695 | INIT_STR(read); |
| 4696 | INIT_STR(readline); |
| 4697 | INIT_STR(copy_reg); |
| 4698 | INIT_STR(dispatch_table); |
| 4699 | INIT_STR(safe_constructors); |
| 4700 | INIT_STR(__basicnew__); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4701 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4702 | if (!( copy_reg = PyImport_ImportModule("copy_reg"))) |
| 4703 | return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4704 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4705 | /* These next few are special because we want to use different |
| 4706 | ones in restricted mode. */ |
| 4707 | dispatch_table = PyObject_GetAttr(copy_reg, dispatch_table_str); |
| 4708 | if (!dispatch_table) |
| 4709 | return -1; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 4710 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4711 | if (!( safe_constructors = PyObject_GetAttr(copy_reg, |
| 4712 | safe_constructors_str))) |
| 4713 | return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4714 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4715 | Py_DECREF(copy_reg); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4716 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4717 | /* Down to here ********************************** */ |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4718 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4719 | if (!( empty_tuple = PyTuple_New(0))) |
| 4720 | return -1; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 4721 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4722 | /* Ugh */ |
| 4723 | if (!( t=PyImport_ImportModule("__builtin__"))) return -1; |
| 4724 | if (PyDict_SetItemString(module_dict, "__builtins__", t) < 0) |
| 4725 | return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4726 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4727 | if (!( t=PyDict_New())) return -1; |
| 4728 | if (!( r=PyRun_String( |
| 4729 | "def __init__(self, *args): self.args=args\n\n" |
| 4730 | "def __str__(self):\n" |
| 4731 | " return self.args and ('%s' % self.args[0]) or '(what)'\n", |
| 4732 | Py_file_input, |
| 4733 | module_dict, t) )) return -1; |
| 4734 | Py_DECREF(r); |
Guido van Rossum | c03158b | 1999-06-09 15:23:31 +0000 | [diff] [blame] | 4735 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4736 | PickleError = PyErr_NewException("cPickle.PickleError", NULL, t); |
| 4737 | if (!PickleError) |
| 4738 | return -1; |
Guido van Rossum | c03158b | 1999-06-09 15:23:31 +0000 | [diff] [blame] | 4739 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4740 | Py_DECREF(t); |
Guido van Rossum | c03158b | 1999-06-09 15:23:31 +0000 | [diff] [blame] | 4741 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4742 | PicklingError = PyErr_NewException("cPickle.PicklingError", |
| 4743 | PickleError, NULL); |
| 4744 | if (!PicklingError) |
| 4745 | return -1; |
Guido van Rossum | c03158b | 1999-06-09 15:23:31 +0000 | [diff] [blame] | 4746 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4747 | if (!( t=PyDict_New())) return -1; |
| 4748 | if (!( r=PyRun_String( |
| 4749 | "def __init__(self, *args): self.args=args\n\n" |
| 4750 | "def __str__(self):\n" |
| 4751 | " a=self.args\n" |
| 4752 | " a=a and type(a[0]) or '(what)'\n" |
| 4753 | " return 'Cannot pickle %s objects' % a\n" |
| 4754 | , Py_file_input, |
| 4755 | module_dict, t) )) return -1; |
| 4756 | Py_DECREF(r); |
Tim Peters | 84e87f3 | 2001-03-17 04:50:51 +0000 | [diff] [blame] | 4757 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4758 | if (!( UnpickleableError = PyErr_NewException( |
| 4759 | "cPickle.UnpickleableError", PicklingError, t))) |
| 4760 | return -1; |
Guido van Rossum | c03158b | 1999-06-09 15:23:31 +0000 | [diff] [blame] | 4761 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4762 | Py_DECREF(t); |
Guido van Rossum | c03158b | 1999-06-09 15:23:31 +0000 | [diff] [blame] | 4763 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4764 | if (!( UnpicklingError = PyErr_NewException("cPickle.UnpicklingError", |
| 4765 | PickleError, NULL))) |
| 4766 | return -1; |
Guido van Rossum | c03158b | 1999-06-09 15:23:31 +0000 | [diff] [blame] | 4767 | |
Martin v. Löwis | 658009a | 2002-09-16 17:26:24 +0000 | [diff] [blame] | 4768 | if (!( BadPickleGet = PyErr_NewException("cPickle.BadPickleGet", |
| 4769 | UnpicklingError, NULL))) |
| 4770 | return -1; |
| 4771 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4772 | if (PyDict_SetItemString(module_dict, "PickleError", |
| 4773 | PickleError) < 0) |
| 4774 | return -1; |
Guido van Rossum | c03158b | 1999-06-09 15:23:31 +0000 | [diff] [blame] | 4775 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4776 | if (PyDict_SetItemString(module_dict, "PicklingError", |
| 4777 | PicklingError) < 0) |
| 4778 | return -1; |
Guido van Rossum | c03158b | 1999-06-09 15:23:31 +0000 | [diff] [blame] | 4779 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4780 | if (PyDict_SetItemString(module_dict, "UnpicklingError", |
| 4781 | UnpicklingError) < 0) |
| 4782 | return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4783 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4784 | if (PyDict_SetItemString(module_dict, "UnpickleableError", |
| 4785 | UnpickleableError) < 0) |
| 4786 | return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4787 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4788 | if (PyDict_SetItemString(module_dict, "BadPickleGet", |
| 4789 | BadPickleGet) < 0) |
| 4790 | return -1; |
Guido van Rossum | c03158b | 1999-06-09 15:23:31 +0000 | [diff] [blame] | 4791 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4792 | PycString_IMPORT; |
Guido van Rossum | 053b8df | 1998-11-25 16:18:00 +0000 | [diff] [blame] | 4793 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4794 | return 0; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 4795 | } |
| 4796 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 4797 | #ifndef PyMODINIT_FUNC /* declarations for DLL import/export */ |
| 4798 | #define PyMODINIT_FUNC void |
Guido van Rossum | f9ffb03 | 1999-02-04 14:54:04 +0000 | [diff] [blame] | 4799 | #endif |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 4800 | PyMODINIT_FUNC |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4801 | initcPickle(void) |
| 4802 | { |
| 4803 | PyObject *m, *d, *di, *v, *k; |
| 4804 | int i; |
| 4805 | char *rev="1.71"; |
| 4806 | PyObject *format_version; |
| 4807 | PyObject *compatible_formats; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 4808 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4809 | Picklertype.ob_type = &PyType_Type; |
| 4810 | Picklertype.tp_getattro = PyObject_GenericGetAttr; |
| 4811 | Picklertype.tp_setattro = PyObject_GenericSetAttr; |
| 4812 | Unpicklertype.ob_type = &PyType_Type; |
| 4813 | PdataType.ob_type = &PyType_Type; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 4814 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4815 | /* Initialize some pieces. We need to do this before module creation, |
| 4816 | so we're forced to use a temporary dictionary. :( |
| 4817 | */ |
| 4818 | di=PyDict_New(); |
| 4819 | if (!di) return; |
| 4820 | if (init_stuff(di) < 0) return; |
Guido van Rossum | ebba420 | 2000-09-07 14:35:37 +0000 | [diff] [blame] | 4821 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4822 | /* Create the module and add the functions */ |
| 4823 | m = Py_InitModule4("cPickle", cPickle_methods, |
| 4824 | cPickle_module_documentation, |
| 4825 | (PyObject*)NULL,PYTHON_API_VERSION); |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 4826 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4827 | /* Add some symbolic constants to the module */ |
| 4828 | d = PyModule_GetDict(m); |
| 4829 | PyDict_SetItemString(d,"__version__", v = PyString_FromString(rev)); |
| 4830 | Py_XDECREF(v); |
Barry Warsaw | 93d29b6 | 1997-01-14 17:45:08 +0000 | [diff] [blame] | 4831 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4832 | /* Copy data from di. Waaa. */ |
| 4833 | for (i=0; PyDict_Next(di, &i, &k, &v); ) { |
| 4834 | if (PyObject_SetItem(d, k, v) < 0) { |
| 4835 | Py_DECREF(di); |
| 4836 | return; |
| 4837 | } |
| 4838 | } |
| 4839 | Py_DECREF(di); |
Guido van Rossum | ebba420 | 2000-09-07 14:35:37 +0000 | [diff] [blame] | 4840 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4841 | format_version = PyString_FromString("1.3"); |
| 4842 | compatible_formats = Py_BuildValue("[sss]", "1.0", "1.1", "1.2"); |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 4843 | |
Martin v. Löwis | 2f6ef4c | 2002-04-01 17:40:08 +0000 | [diff] [blame] | 4844 | PyDict_SetItemString(d, "format_version", format_version); |
| 4845 | PyDict_SetItemString(d, "compatible_formats", compatible_formats); |
| 4846 | Py_XDECREF(format_version); |
| 4847 | Py_XDECREF(compatible_formats); |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 4848 | } |