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