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