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