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