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