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