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