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