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