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