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