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