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