Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 1 | /* |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame^] | 2 | cPickle.c,v 1.46 1997/12/04 00:08:07 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 | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame^] | 58 | "cPickle.c,v 1.46 1997/12/04 00:08:07 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 | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame^] | 135 | *read_str, *readline_str, *__main___str, |
| 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 | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 158 | static 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 | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 183 | static 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 | |
| 1662 | UNLESS(PyTuple_Check(arg_tup)) { |
| 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 | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1934 | static PyTypeObject Picklertype_value() { |
| 1935 | PyTypeObject Picklertype = { |
| 1936 | PyObject_HEAD_INIT(&PyType_Type) |
| 1937 | 0, /*ob_size*/ |
| 1938 | "Pickler", /*tp_name*/ |
| 1939 | sizeof(Picklerobject), /*tp_basicsize*/ |
| 1940 | 0, /*tp_itemsize*/ |
| 1941 | /* methods */ |
| 1942 | (destructor)Pickler_dealloc, /*tp_dealloc*/ |
| 1943 | (printfunc)0, /*tp_print*/ |
| 1944 | (getattrfunc)Pickler_getattr, /*tp_getattr*/ |
| 1945 | (setattrfunc)Pickler_setattr, /*tp_setattr*/ |
| 1946 | (cmpfunc)0, /*tp_compare*/ |
| 1947 | (reprfunc)0, /*tp_repr*/ |
| 1948 | 0, /*tp_as_number*/ |
| 1949 | 0, /*tp_as_sequence*/ |
| 1950 | 0, /*tp_as_mapping*/ |
| 1951 | (hashfunc)0, /*tp_hash*/ |
| 1952 | (ternaryfunc)0, /*tp_call*/ |
| 1953 | (reprfunc)0, /*tp_str*/ |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 1954 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1955 | /* Space for future expansion */ |
| 1956 | 0L,0L,0L,0L, |
| 1957 | Picklertype__doc__ /* Documentation string */ |
| 1958 | }; |
| 1959 | return Picklertype; |
| 1960 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 1961 | |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 1962 | static PyObject * |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame^] | 1963 | find_class(PyObject *class_map, |
| 1964 | PyObject *py_module_name, PyObject *py_global_name) { |
| 1965 | PyObject *global = 0, *t = 0, *module; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 1966 | |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame^] | 1967 | UNLESS(t = PyTuple_New(2)) return NULL; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 1968 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1969 | PyTuple_SET_ITEM((PyTupleObject *)t, 0, py_module_name); |
| 1970 | Py_INCREF(py_module_name); |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame^] | 1971 | PyTuple_SET_ITEM((PyTupleObject *)t, 1, py_global_name); |
| 1972 | Py_INCREF(py_global_name); |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 1973 | |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame^] | 1974 | global=PyDict_GetItem(class_map, t); |
| 1975 | |
| 1976 | if (global) { |
| 1977 | Py_DECREF(t); |
| 1978 | Py_INCREF(global); |
| 1979 | return global; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 1980 | } |
| 1981 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1982 | PyErr_Clear(); |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 1983 | |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame^] | 1984 | UNLESS(module=PyImport_Import(py_module_name)) return NULL; |
| 1985 | global=PyObject_GetAttr(module, py_global_name); |
| 1986 | Py_DECREF(module); |
| 1987 | UNLESS(global) return NULL; |
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 | if (PyDict_SetItem(class_map, t, global) < 0) global=NULL; |
| 1990 | Py_DECREF(t); |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 1991 | |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame^] | 1992 | return global; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 1993 | } |
| 1994 | |
| 1995 | |
| 1996 | static int |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1997 | marker(Unpicklerobject *self) { |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame^] | 1998 | if (self->num_marks < 1) { |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 1999 | PyErr_SetString(UnpicklingError, "could not find MARK"); |
| 2000 | return -1; |
| 2001 | } |
| 2002 | |
| 2003 | return self->marks[--self->num_marks]; |
| 2004 | } |
| 2005 | |
| 2006 | |
| 2007 | static int |
| 2008 | load_none(Unpicklerobject *self) { |
| 2009 | if (PyList_Append(self->stack, Py_None) < 0) |
| 2010 | return -1; |
| 2011 | |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2012 | return 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2013 | } |
| 2014 | |
| 2015 | |
| 2016 | static int |
| 2017 | load_int(Unpicklerobject *self) { |
| 2018 | PyObject *py_int = 0; |
| 2019 | char *endptr, *s; |
| 2020 | int len, res = -1; |
| 2021 | long l; |
| 2022 | |
| 2023 | if ((len = (*self->readline_func)(self, &s)) < 0) return -1; |
Guido van Rossum | 725d941 | 1997-08-20 23:38:57 +0000 | [diff] [blame] | 2024 | UNLESS(s=pystrndup(s,len)) return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2025 | |
| 2026 | errno = 0; |
| 2027 | l = strtol(s, &endptr, 0); |
| 2028 | |
| 2029 | if (errno || (*endptr != '\n') || (endptr[1] != '\0')) { |
| 2030 | /* Hm, maybe we've got something long. Let's try reading |
| 2031 | it as a Python long object. */ |
| 2032 | errno=0; |
| 2033 | UNLESS(py_int=PyLong_FromString(s,&endptr,0)) goto finally; |
| 2034 | |
| 2035 | if ((*endptr != '\n') || (endptr[1] != '\0')) { |
| 2036 | PyErr_SetString(PyExc_ValueError, |
| 2037 | "could not convert string to int"); |
| 2038 | goto finally; |
| 2039 | } |
| 2040 | } |
| 2041 | else { |
| 2042 | UNLESS(py_int = PyInt_FromLong(l)) goto finally; |
| 2043 | } |
| 2044 | |
| 2045 | if (PyList_Append(self->stack, py_int) < 0) goto finally; |
| 2046 | |
| 2047 | res = 0; |
| 2048 | |
| 2049 | finally: |
| 2050 | free(s); |
| 2051 | Py_XDECREF(py_int); |
| 2052 | |
| 2053 | return res; |
| 2054 | } |
| 2055 | |
| 2056 | |
| 2057 | static long |
| 2058 | calc_binint(char *s, int x) { |
| 2059 | unsigned char c; |
| 2060 | int i; |
| 2061 | long l; |
| 2062 | |
| 2063 | for (i = 0, l = 0L; i < x; i++) { |
| 2064 | c = (unsigned char)s[i]; |
| 2065 | l |= (long)c << (i * 8); |
| 2066 | } |
| 2067 | |
| 2068 | return l; |
| 2069 | } |
| 2070 | |
| 2071 | |
| 2072 | static int |
| 2073 | load_binintx(Unpicklerobject *self, char *s, int x) { |
| 2074 | PyObject *py_int = 0; |
| 2075 | long l; |
| 2076 | |
| 2077 | l = calc_binint(s, x); |
| 2078 | |
| 2079 | UNLESS(py_int = PyInt_FromLong(l)) |
| 2080 | return -1; |
| 2081 | |
| 2082 | if (PyList_Append(self->stack, py_int) < 0) { |
| 2083 | Py_DECREF(py_int); |
| 2084 | return -1; |
| 2085 | } |
| 2086 | |
| 2087 | Py_DECREF(py_int); |
| 2088 | |
| 2089 | return 0; |
| 2090 | } |
| 2091 | |
| 2092 | |
| 2093 | static int |
| 2094 | load_binint(Unpicklerobject *self) { |
| 2095 | char *s; |
| 2096 | |
| 2097 | if ((*self->read_func)(self, &s, 4) < 0) |
| 2098 | return -1; |
| 2099 | |
| 2100 | return load_binintx(self, s, 4); |
| 2101 | } |
| 2102 | |
| 2103 | |
| 2104 | static int |
| 2105 | load_binint1(Unpicklerobject *self) { |
| 2106 | char *s; |
| 2107 | |
| 2108 | if ((*self->read_func)(self, &s, 1) < 0) |
| 2109 | return -1; |
| 2110 | |
| 2111 | return load_binintx(self, s, 1); |
| 2112 | } |
| 2113 | |
| 2114 | |
| 2115 | static int |
| 2116 | load_binint2(Unpicklerobject *self) { |
| 2117 | char *s; |
| 2118 | |
| 2119 | if ((*self->read_func)(self, &s, 2) < 0) |
| 2120 | return -1; |
| 2121 | |
| 2122 | return load_binintx(self, s, 2); |
| 2123 | } |
| 2124 | |
| 2125 | static int |
| 2126 | load_long(Unpicklerobject *self) { |
| 2127 | PyObject *l = 0; |
| 2128 | char *end, *s; |
| 2129 | int len, res = -1; |
| 2130 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2131 | if ((len = (*self->readline_func)(self, &s)) < 0) return -1; |
Guido van Rossum | 725d941 | 1997-08-20 23:38:57 +0000 | [diff] [blame] | 2132 | UNLESS(s=pystrndup(s,len)) return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2133 | |
| 2134 | UNLESS(l = PyLong_FromString(s, &end, 0)) |
| 2135 | goto finally; |
| 2136 | |
| 2137 | if (PyList_Append(self->stack, l) < 0) |
| 2138 | goto finally; |
| 2139 | |
| 2140 | res = 0; |
| 2141 | |
| 2142 | finally: |
| 2143 | free(s); |
| 2144 | Py_XDECREF(l); |
| 2145 | |
| 2146 | return res; |
| 2147 | } |
| 2148 | |
| 2149 | |
| 2150 | static int |
| 2151 | load_float(Unpicklerobject *self) { |
| 2152 | PyObject *py_float = 0; |
| 2153 | char *endptr, *s; |
| 2154 | int len, res = -1; |
| 2155 | double d; |
| 2156 | |
| 2157 | if ((len = (*self->readline_func)(self, &s)) < 0) return -1; |
Guido van Rossum | 725d941 | 1997-08-20 23:38:57 +0000 | [diff] [blame] | 2158 | UNLESS(s=pystrndup(s,len)) return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2159 | |
| 2160 | errno = 0; |
| 2161 | d = strtod(s, &endptr); |
| 2162 | |
| 2163 | if (errno || (endptr[0] != '\n') || (endptr[1] != '\0')) { |
| 2164 | PyErr_SetString(PyExc_ValueError, |
| 2165 | "could not convert string to float"); |
| 2166 | goto finally; |
| 2167 | } |
| 2168 | |
| 2169 | UNLESS(py_float = PyFloat_FromDouble(d)) |
| 2170 | goto finally; |
| 2171 | |
| 2172 | if (PyList_Append(self->stack, py_float) < 0) |
| 2173 | goto finally; |
| 2174 | |
| 2175 | res = 0; |
| 2176 | |
| 2177 | finally: |
| 2178 | free(s); |
| 2179 | Py_XDECREF(py_float); |
| 2180 | |
| 2181 | return res; |
| 2182 | } |
| 2183 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2184 | static int |
| 2185 | load_binfloat(Unpicklerobject *self) { |
| 2186 | PyObject *py_float = 0; |
| 2187 | int s, e, res = -1; |
| 2188 | long fhi, flo; |
| 2189 | double x; |
| 2190 | char *p; |
| 2191 | |
| 2192 | if ((*self->read_func)(self, &p, 8) < 0) |
| 2193 | return -1; |
| 2194 | |
| 2195 | /* First byte */ |
| 2196 | s = (*p>>7) & 1; |
| 2197 | e = (*p & 0x7F) << 4; |
| 2198 | p++; |
| 2199 | |
| 2200 | /* Second byte */ |
| 2201 | e |= (*p>>4) & 0xF; |
| 2202 | fhi = (*p & 0xF) << 24; |
| 2203 | p++; |
| 2204 | |
| 2205 | /* Third byte */ |
| 2206 | fhi |= (*p & 0xFF) << 16; |
| 2207 | p++; |
| 2208 | |
| 2209 | /* Fourth byte */ |
| 2210 | fhi |= (*p & 0xFF) << 8; |
| 2211 | p++; |
| 2212 | |
| 2213 | /* Fifth byte */ |
| 2214 | fhi |= *p & 0xFF; |
| 2215 | p++; |
| 2216 | |
| 2217 | /* Sixth byte */ |
| 2218 | flo = (*p & 0xFF) << 16; |
| 2219 | p++; |
| 2220 | |
| 2221 | /* Seventh byte */ |
| 2222 | flo |= (*p & 0xFF) << 8; |
| 2223 | p++; |
| 2224 | |
| 2225 | /* Eighth byte */ |
| 2226 | flo |= *p & 0xFF; |
| 2227 | |
| 2228 | x = (double)fhi + (double)flo / 16777216.0; /* 2**24 */ |
| 2229 | x /= 268435456.0; /* 2**28 */ |
| 2230 | |
| 2231 | /* XXX This sadly ignores Inf/NaN */ |
| 2232 | if (e == 0) |
| 2233 | e = -1022; |
| 2234 | else { |
| 2235 | x += 1.0; |
| 2236 | e -= 1023; |
| 2237 | } |
| 2238 | x = ldexp(x, e); |
| 2239 | |
| 2240 | if (s) |
| 2241 | x = -x; |
| 2242 | |
| 2243 | UNLESS(py_float = PyFloat_FromDouble(x)) |
| 2244 | goto finally; |
| 2245 | |
| 2246 | if (PyList_Append(self->stack, py_float) < 0) |
| 2247 | goto finally; |
| 2248 | |
| 2249 | res = 0; |
| 2250 | |
| 2251 | finally: |
| 2252 | Py_XDECREF(py_float); |
| 2253 | |
| 2254 | return res; |
| 2255 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2256 | |
| 2257 | static int |
| 2258 | load_string(Unpicklerobject *self) { |
| 2259 | PyObject *str = 0; |
| 2260 | int len, res = -1; |
| 2261 | char *s; |
| 2262 | |
| 2263 | static PyObject *eval_dict = 0; |
| 2264 | |
| 2265 | if ((len = (*self->readline_func)(self, &s)) < 0) return -1; |
Guido van Rossum | 725d941 | 1997-08-20 23:38:57 +0000 | [diff] [blame] | 2266 | UNLESS(s=pystrndup(s,len)) return -1; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2267 | |
| 2268 | UNLESS(eval_dict) |
| 2269 | UNLESS(eval_dict = Py_BuildValue("{s{}}", "__builtins__")) |
| 2270 | goto finally; |
| 2271 | |
Guido van Rossum | b05a5c7 | 1997-05-07 17:46:13 +0000 | [diff] [blame] | 2272 | 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] | 2273 | goto finally; |
| 2274 | |
| 2275 | if (PyList_Append(self->stack, str) < 0) |
| 2276 | goto finally; |
| 2277 | |
| 2278 | res = 0; |
| 2279 | |
| 2280 | finally: |
| 2281 | free(s); |
| 2282 | Py_XDECREF(str); |
| 2283 | |
| 2284 | return res; |
| 2285 | } |
| 2286 | |
| 2287 | |
| 2288 | static int |
| 2289 | load_binstring(Unpicklerobject *self) { |
| 2290 | PyObject *py_string = 0; |
| 2291 | long l; |
| 2292 | int res = -1; |
| 2293 | char *s; |
| 2294 | |
| 2295 | if ((*self->read_func)(self, &s, 4) < 0) |
| 2296 | goto finally; |
| 2297 | |
| 2298 | l = calc_binint(s, 4); |
| 2299 | |
| 2300 | if ((*self->read_func)(self, &s, l) < 0) |
| 2301 | goto finally; |
| 2302 | |
| 2303 | UNLESS(py_string = PyString_FromStringAndSize(s, l)) |
| 2304 | goto finally; |
| 2305 | |
| 2306 | if (PyList_Append(self->stack, py_string) < 0) |
| 2307 | goto finally; |
| 2308 | |
| 2309 | res = 0; |
| 2310 | |
| 2311 | finally: |
| 2312 | Py_XDECREF(py_string); |
| 2313 | |
| 2314 | return res; |
| 2315 | } |
| 2316 | |
| 2317 | |
| 2318 | static int |
| 2319 | load_short_binstring(Unpicklerobject *self) { |
| 2320 | PyObject *py_string = 0; |
| 2321 | unsigned char l; |
| 2322 | int res = -1; |
| 2323 | char *s; |
| 2324 | |
| 2325 | if ((*self->read_func)(self, &s, 1) < 0) |
| 2326 | return -1; |
| 2327 | |
| 2328 | l = (unsigned char)s[0]; |
| 2329 | |
| 2330 | if ((*self->read_func)(self, &s, l) < 0) |
| 2331 | goto finally; |
| 2332 | |
| 2333 | UNLESS(py_string = PyString_FromStringAndSize(s, l)) |
| 2334 | goto finally; |
| 2335 | |
| 2336 | if (PyList_Append(self->stack, py_string) < 0) |
| 2337 | goto finally; |
| 2338 | |
| 2339 | res = 0; |
| 2340 | |
| 2341 | finally: |
| 2342 | Py_XDECREF(py_string); |
| 2343 | |
| 2344 | return res; |
| 2345 | } |
| 2346 | |
| 2347 | |
| 2348 | static int |
| 2349 | load_tuple(Unpicklerobject *self) { |
| 2350 | PyObject *tup = 0, *slice = 0, *list = 0; |
| 2351 | int i, j, res = -1; |
| 2352 | |
| 2353 | if ((i = marker(self)) < 0) |
| 2354 | goto finally; |
| 2355 | |
| 2356 | if ((j = PyList_Size(self->stack)) < 0) |
| 2357 | goto finally; |
| 2358 | |
| 2359 | UNLESS(slice = PyList_GetSlice(self->stack, i, j)) |
| 2360 | goto finally; |
| 2361 | |
| 2362 | UNLESS(tup = PySequence_Tuple(slice)) |
| 2363 | goto finally; |
| 2364 | |
| 2365 | UNLESS(list = PyList_New(1)) |
| 2366 | goto finally; |
| 2367 | |
| 2368 | Py_INCREF(tup); |
| 2369 | if (PyList_SetItem(list, 0, tup) < 0) |
| 2370 | goto finally; |
| 2371 | |
| 2372 | if (PyList_SetSlice(self->stack, i, j, list) < 0) |
| 2373 | goto finally; |
| 2374 | |
| 2375 | res = 0; |
| 2376 | |
| 2377 | finally: |
| 2378 | Py_XDECREF(tup); |
| 2379 | Py_XDECREF(list); |
| 2380 | Py_XDECREF(slice); |
| 2381 | |
| 2382 | return res; |
| 2383 | } |
| 2384 | |
| 2385 | static int |
| 2386 | load_empty_tuple(Unpicklerobject *self) { |
| 2387 | PyObject *tup = 0; |
| 2388 | int res; |
| 2389 | |
| 2390 | UNLESS(tup=PyTuple_New(0)) return -1; |
| 2391 | res=PyList_Append(self->stack, tup); |
| 2392 | Py_DECREF(tup); |
| 2393 | return res; |
| 2394 | } |
| 2395 | |
| 2396 | static int |
| 2397 | load_empty_list(Unpicklerobject *self) { |
| 2398 | PyObject *list = 0; |
| 2399 | int res; |
| 2400 | |
| 2401 | UNLESS(list=PyList_New(0)) return -1; |
| 2402 | res=PyList_Append(self->stack, list); |
| 2403 | Py_DECREF(list); |
| 2404 | return res; |
| 2405 | } |
| 2406 | |
| 2407 | static int |
| 2408 | load_empty_dict(Unpicklerobject *self) { |
| 2409 | PyObject *dict = 0; |
| 2410 | int res; |
| 2411 | |
| 2412 | UNLESS(dict=PyDict_New()) return -1; |
| 2413 | res=PyList_Append(self->stack, dict); |
| 2414 | Py_DECREF(dict); |
| 2415 | return res; |
| 2416 | } |
| 2417 | |
| 2418 | |
| 2419 | static int |
| 2420 | load_list(Unpicklerobject *self) { |
| 2421 | PyObject *list = 0, *slice = 0; |
| 2422 | int i, j, l, res = -1; |
| 2423 | |
| 2424 | if ((i = marker(self)) < 0) |
| 2425 | goto finally; |
| 2426 | |
| 2427 | if ((j = PyList_Size(self->stack)) < 0) |
| 2428 | goto finally; |
| 2429 | |
| 2430 | UNLESS(slice = PyList_GetSlice(self->stack, i, j)) |
| 2431 | goto finally; |
| 2432 | |
| 2433 | if((l=PyList_Size(slice)) < 0) |
| 2434 | goto finally; |
| 2435 | |
| 2436 | if(l) { |
| 2437 | UNLESS(list = PyList_New(1)) |
| 2438 | goto finally; |
| 2439 | |
| 2440 | Py_INCREF(slice); |
| 2441 | if (PyList_SetItem(list, 0, slice) < 0) |
| 2442 | goto finally; |
| 2443 | |
| 2444 | if (PyList_SetSlice(self->stack, i, j, list) < 0) |
| 2445 | goto finally; |
| 2446 | } else { |
| 2447 | if(PyList_Append(self->stack,slice) < 0) |
| 2448 | goto finally; |
| 2449 | } |
| 2450 | |
| 2451 | res = 0; |
| 2452 | |
| 2453 | finally: |
| 2454 | Py_XDECREF(list); |
| 2455 | Py_XDECREF(slice); |
| 2456 | |
| 2457 | return res; |
| 2458 | } |
| 2459 | |
| 2460 | static int |
| 2461 | load_dict(Unpicklerobject *self) { |
| 2462 | PyObject *list = 0, *dict = 0, *key = 0, *value = 0; |
| 2463 | int i, j, k, res = -1; |
| 2464 | |
| 2465 | if ((i = marker(self)) < 0) |
| 2466 | goto finally; |
| 2467 | |
| 2468 | if ((j = PyList_Size(self->stack)) < 0) |
| 2469 | goto finally; |
| 2470 | |
| 2471 | UNLESS(dict = PyDict_New()) |
| 2472 | goto finally; |
| 2473 | |
| 2474 | for (k = i; k < j; k += 2) { |
| 2475 | UNLESS(key = PyList_GET_ITEM((PyListObject *)self->stack, k)) |
| 2476 | goto finally; |
| 2477 | |
| 2478 | UNLESS(value = PyList_GET_ITEM((PyListObject *)self->stack, k + 1)) |
| 2479 | goto finally; |
| 2480 | |
| 2481 | if (PyDict_SetItem(dict, key, value) < 0) |
| 2482 | goto finally; |
| 2483 | } |
| 2484 | |
| 2485 | if(j) { |
| 2486 | |
| 2487 | UNLESS(list = PyList_New(1)) |
| 2488 | goto finally; |
| 2489 | |
| 2490 | Py_INCREF(dict); |
| 2491 | if (PyList_SetItem(list, 0, dict) < 0) |
| 2492 | goto finally; |
| 2493 | |
| 2494 | if (PyList_SetSlice(self->stack, i, j, list) < 0) |
| 2495 | goto finally; |
| 2496 | } |
| 2497 | else |
| 2498 | if(PyList_Append(self->stack, dict) < 0) |
| 2499 | goto finally; |
| 2500 | |
| 2501 | res = 0; |
| 2502 | |
| 2503 | finally: |
| 2504 | Py_XDECREF(dict); |
| 2505 | Py_XDECREF(list); |
| 2506 | |
| 2507 | return res; |
| 2508 | } |
| 2509 | |
| 2510 | static PyObject * |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame^] | 2511 | Instance_New(PyObject *cls, PyObject *args) { |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2512 | int has_key; |
| 2513 | PyObject *safe=0, *r=0; |
| 2514 | |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame^] | 2515 | if (PyClass_Check(cls)) { |
| 2516 | int l; |
| 2517 | |
| 2518 | if((l=PyObject_Length(args)) < 0) goto err; |
| 2519 | UNLESS(l) { |
| 2520 | PyObject *__getinitargs__; |
| 2521 | |
| 2522 | UNLESS(__getinitargs__=PyObject_GetAttr(cls, __getinitargs___str)) { |
| 2523 | /* We have a class with no __getinitargs__, so bypass usual |
| 2524 | construction */ |
| 2525 | PyInstanceObject *inst; |
| 2526 | |
| 2527 | PyErr_Clear(); |
| 2528 | UNLESS(inst=PyObject_NEW(PyInstanceObject, &PyInstance_Type)) |
| 2529 | goto err; |
| 2530 | inst->in_class=(PyClassObject*)cls; |
| 2531 | Py_INCREF(cls); |
| 2532 | UNLESS(inst->in_dict=PyDict_New()) { |
| 2533 | Py_DECREF(inst); |
| 2534 | goto err; |
| 2535 | } |
| 2536 | |
| 2537 | return (PyObject *)inst; |
| 2538 | } |
| 2539 | Py_DECREF(__getinitargs__); |
| 2540 | } |
| 2541 | |
| 2542 | if((r=PyInstance_New(cls, args, NULL))) return r; |
| 2543 | else goto err; |
| 2544 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2545 | |
| 2546 | |
| 2547 | if ((has_key = cPickle_PyMapping_HasKey(safe_constructors, cls)) < 0) |
| 2548 | goto err; |
| 2549 | |
| 2550 | if (!has_key) |
| 2551 | if(!(safe = PyObject_GetAttr(cls, __safe_for_unpickling___str)) || |
| 2552 | !PyObject_IsTrue(safe)) { |
| 2553 | PyErr_Format(UnpicklingError, "%s is not safe for unpickling", "O", cls); |
| 2554 | Py_XDECREF(safe); |
| 2555 | return NULL; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 2556 | } |
| 2557 | |
Guido van Rossum | 142eeb8 | 1997-08-13 03:14:41 +0000 | [diff] [blame] | 2558 | if((r=PyObject_CallObject(cls, args))) return r; |
| 2559 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2560 | err: |
| 2561 | { |
| 2562 | PyObject *tp, *v, *tb; |
| 2563 | |
| 2564 | PyErr_Fetch(&tp, &v, &tb); |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame^] | 2565 | if((r=Py_BuildValue("OOO",v,cls,args))) { |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2566 | Py_XDECREF(v); |
| 2567 | v=r; |
| 2568 | } |
| 2569 | PyErr_Restore(tp,v,tb); |
| 2570 | } |
| 2571 | return NULL; |
| 2572 | } |
| 2573 | |
| 2574 | |
| 2575 | static int |
| 2576 | load_obj(Unpicklerobject *self) { |
| 2577 | PyObject *class = 0, *slice = 0, *tup = 0, *obj = 0; |
| 2578 | int i, len, res = -1; |
| 2579 | |
| 2580 | if ((i = marker(self)) < 0) |
| 2581 | goto finally; |
| 2582 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2583 | if ((len = PyList_Size(self->stack)) < 0) |
| 2584 | goto finally; |
| 2585 | |
| 2586 | UNLESS(slice = PyList_GetSlice(self->stack, i + 1, len)) |
| 2587 | goto finally; |
| 2588 | |
| 2589 | UNLESS(tup = PySequence_Tuple(slice)) |
| 2590 | goto finally; |
| 2591 | |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame^] | 2592 | class = PyList_GET_ITEM((PyListObject *)self->stack, i); |
| 2593 | Py_INCREF(class); |
| 2594 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2595 | UNLESS(obj = Instance_New(class, tup)) |
| 2596 | goto finally; |
| 2597 | |
| 2598 | if (DEL_LIST_SLICE(self->stack, i, len) < 0) |
| 2599 | goto finally; |
| 2600 | |
| 2601 | if (PyList_Append(self->stack, obj) < 0) |
| 2602 | goto finally; |
| 2603 | |
| 2604 | res = 0; |
| 2605 | |
| 2606 | finally: |
| 2607 | |
| 2608 | Py_XDECREF(class); |
| 2609 | Py_XDECREF(slice); |
| 2610 | Py_XDECREF(tup); |
| 2611 | Py_XDECREF(obj); |
| 2612 | |
| 2613 | return res; |
| 2614 | } |
| 2615 | |
| 2616 | |
| 2617 | static int |
| 2618 | load_inst(Unpicklerobject *self) { |
| 2619 | PyObject *arg_tup = 0, *arg_slice = 0, *class = 0, *obj = 0, |
| 2620 | *module_name = 0, *class_name = 0; |
| 2621 | int i, j, len, res = -1; |
| 2622 | char *s; |
| 2623 | |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame^] | 2624 | if ((i = marker(self)) < 0) goto finally; |
| 2625 | |
| 2626 | if ((j = PyList_Size(self->stack)) < 0) goto finally; |
| 2627 | |
| 2628 | UNLESS(arg_slice = PyList_GetSlice(self->stack, i, j)) goto finally; |
| 2629 | |
| 2630 | UNLESS(arg_tup = PySequence_Tuple(arg_slice)) goto finally; |
| 2631 | |
| 2632 | if (DEL_LIST_SLICE(self->stack, i, j) < 0) goto finally; |
| 2633 | |
| 2634 | if ((len = (*self->readline_func)(self, &s)) < 0) goto finally; |
| 2635 | |
| 2636 | UNLESS(module_name = PyString_FromStringAndSize(s, len - 1)) goto finally; |
| 2637 | |
| 2638 | if ((len = (*self->readline_func)(self, &s)) < 0) goto finally; |
| 2639 | |
| 2640 | UNLESS(class_name = PyString_FromStringAndSize(s, len - 1)) goto finally; |
| 2641 | |
| 2642 | UNLESS(class = find_class(self->class_map, module_name, class_name)) |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2643 | goto finally; |
| 2644 | |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame^] | 2645 | UNLESS(obj = Instance_New(class, arg_tup)) goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2646 | |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame^] | 2647 | if (PyList_Append(self->stack, obj) < 0) goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2648 | |
| 2649 | res = 0; |
| 2650 | |
| 2651 | finally: |
| 2652 | Py_XDECREF(class); |
| 2653 | Py_XDECREF(arg_slice); |
| 2654 | Py_XDECREF(arg_tup); |
| 2655 | Py_XDECREF(obj); |
| 2656 | Py_XDECREF(module_name); |
| 2657 | Py_XDECREF(class_name); |
| 2658 | |
| 2659 | return res; |
| 2660 | } |
| 2661 | |
| 2662 | |
| 2663 | static int |
| 2664 | load_global(Unpicklerobject *self) { |
| 2665 | PyObject *class = 0, *module_name = 0, *class_name = 0; |
| 2666 | int res = -1, len; |
| 2667 | char *s; |
| 2668 | |
| 2669 | if ((len = (*self->readline_func)(self, &s)) < 0) |
| 2670 | goto finally; |
| 2671 | |
| 2672 | UNLESS(module_name = PyString_FromStringAndSize(s, len - 1)) |
| 2673 | goto finally; |
| 2674 | |
| 2675 | if ((len = (*self->readline_func)(self, &s)) < 0) |
| 2676 | goto finally; |
| 2677 | |
| 2678 | UNLESS(class_name = PyString_FromStringAndSize(s, len - 1)) |
| 2679 | goto finally; |
| 2680 | |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame^] | 2681 | UNLESS(class = find_class(self->class_map, module_name, class_name)) |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2682 | goto finally; |
| 2683 | |
| 2684 | if (PyList_Append(self->stack, class) < 0) |
| 2685 | goto finally; |
| 2686 | |
| 2687 | res = 0; |
| 2688 | |
| 2689 | finally: |
| 2690 | Py_XDECREF(class); |
| 2691 | Py_XDECREF(module_name); |
| 2692 | Py_XDECREF(class_name); |
| 2693 | |
| 2694 | return res; |
| 2695 | } |
| 2696 | |
| 2697 | |
| 2698 | static int |
| 2699 | load_persid(Unpicklerobject *self) { |
| 2700 | PyObject *pid = 0, *pers_load_val = 0; |
| 2701 | int len, res = -1; |
| 2702 | char *s; |
| 2703 | |
| 2704 | if (self->pers_func) { |
| 2705 | if ((len = (*self->readline_func)(self, &s)) < 0) |
| 2706 | goto finally; |
| 2707 | |
| 2708 | UNLESS(pid = PyString_FromStringAndSize(s, len - 1)) |
| 2709 | goto finally; |
| 2710 | |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame^] | 2711 | if(PyList_Check(self->pers_func)) { |
| 2712 | if(PyList_Append(self->pers_func, pid) < 0) goto finally; |
| 2713 | pers_load_val=pid; |
| 2714 | Py_INCREF(pid); |
| 2715 | } |
| 2716 | else { |
| 2717 | UNLESS(self->arg) |
| 2718 | UNLESS(self->arg = PyTuple_New(1)) |
| 2719 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2720 | |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame^] | 2721 | Py_INCREF(pid); |
| 2722 | if (PyTuple_SetItem(self->arg, 0, pid) < 0) |
| 2723 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2724 | |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame^] | 2725 | UNLESS(pers_load_val = |
| 2726 | PyObject_CallObject(self->pers_func, self->arg)) |
| 2727 | goto finally; |
| 2728 | } |
| 2729 | if (PyList_Append(self->stack, pers_load_val) < 0) |
| 2730 | goto finally; |
| 2731 | } |
| 2732 | else { |
| 2733 | PyErr_SetString(UnpicklingError, |
| 2734 | "A load persistent id instruction was encountered,\n" |
| 2735 | "but no persistent_load function was specified."); |
| 2736 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2737 | } |
| 2738 | |
| 2739 | res = 0; |
| 2740 | |
| 2741 | finally: |
| 2742 | Py_XDECREF(pid); |
| 2743 | Py_XDECREF(pers_load_val); |
| 2744 | |
| 2745 | return res; |
| 2746 | } |
| 2747 | |
| 2748 | |
| 2749 | static int |
| 2750 | load_binpersid(Unpicklerobject *self) { |
| 2751 | PyObject *pid = 0, *pers_load_val = 0; |
| 2752 | int len, res = -1; |
| 2753 | |
| 2754 | if (self->pers_func) { |
| 2755 | if ((len = PyList_Size(self->stack)) < 0) |
| 2756 | goto finally; |
| 2757 | |
| 2758 | pid = PyList_GET_ITEM((PyListObject *)self->stack, len - 1); |
| 2759 | Py_INCREF(pid); |
| 2760 | |
| 2761 | if (DEL_LIST_SLICE(self->stack, len - 1, len) < 0) |
| 2762 | goto finally; |
| 2763 | |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame^] | 2764 | if(PyList_Check(self->pers_func)) { |
| 2765 | if(PyList_Append(self->pers_func, pid) < 0) goto finally; |
| 2766 | pers_load_val=pid; |
| 2767 | Py_INCREF(pid); |
| 2768 | } |
| 2769 | else { |
| 2770 | UNLESS(self->arg) |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2771 | UNLESS(self->arg = PyTuple_New(1)) |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame^] | 2772 | goto finally; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2773 | |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame^] | 2774 | Py_INCREF(pid); |
| 2775 | if (PyTuple_SetItem(self->arg, 0, pid) < 0) |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2776 | goto finally; |
| 2777 | |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame^] | 2778 | UNLESS(pers_load_val = |
| 2779 | PyObject_CallObject(self->pers_func, self->arg)) |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2780 | goto finally; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame^] | 2781 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2782 | if (PyList_Append(self->stack, pers_load_val) < 0) |
| 2783 | goto finally; |
| 2784 | } |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame^] | 2785 | else { |
| 2786 | PyErr_SetString(UnpicklingError, |
| 2787 | "A load persistent id instruction was encountered,\n" |
| 2788 | "but no persistent_load function was specified."); |
| 2789 | goto finally; |
| 2790 | } |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 2791 | |
| 2792 | res = 0; |
| 2793 | |
| 2794 | finally: |
| 2795 | Py_XDECREF(pid); |
| 2796 | Py_XDECREF(pers_load_val); |
| 2797 | |
| 2798 | return res; |
| 2799 | } |
| 2800 | |
| 2801 | |
| 2802 | static int |
| 2803 | load_pop(Unpicklerobject *self) { |
| 2804 | int len; |
| 2805 | |
| 2806 | if ((len = PyList_Size(self->stack)) < 0) |
| 2807 | return -1; |
| 2808 | |
| 2809 | if ((self->num_marks > 0) && |
| 2810 | (self->marks[self->num_marks - 1] == len)) |
| 2811 | self->num_marks--; |
| 2812 | else if (DEL_LIST_SLICE(self->stack, len - 1, len) < 0) |
| 2813 | return -1; |
| 2814 | |
| 2815 | return 0; |
| 2816 | } |
| 2817 | |
| 2818 | |
| 2819 | static int |
| 2820 | load_pop_mark(Unpicklerobject *self) { |
| 2821 | int i, len; |
| 2822 | |
| 2823 | if ((i = marker(self)) < 0) |
| 2824 | return -1; |
| 2825 | |
| 2826 | if ((len = PyList_Size(self->stack)) < 0) |
| 2827 | return -1; |
| 2828 | |
| 2829 | if (DEL_LIST_SLICE(self->stack, i, len) < 0) |
| 2830 | return -1; |
| 2831 | |
| 2832 | return 0; |
| 2833 | } |
| 2834 | |
| 2835 | |
| 2836 | static int |
| 2837 | load_dup(Unpicklerobject *self) { |
| 2838 | PyObject *last; |
| 2839 | int len; |
| 2840 | |
| 2841 | if ((len = PyList_Size(self->stack)) < 0) |
| 2842 | return -1; |
| 2843 | |
| 2844 | UNLESS(last = PyList_GetItem(self->stack, len - 1)) |
| 2845 | return -1; |
| 2846 | |
| 2847 | if (PyList_Append(self->stack, last) < 0) |
| 2848 | return -1; |
| 2849 | |
| 2850 | return 0; |
| 2851 | } |
| 2852 | |
| 2853 | |
| 2854 | static int |
| 2855 | load_get(Unpicklerobject *self) { |
| 2856 | PyObject *py_str = 0, *value = 0; |
| 2857 | int len, res = -1; |
| 2858 | char *s; |
| 2859 | |
| 2860 | if ((len = (*self->readline_func)(self, &s)) < 0) |
| 2861 | goto finally; |
| 2862 | |
| 2863 | UNLESS(py_str = PyString_FromStringAndSize(s, len - 1)) |
| 2864 | goto finally; |
| 2865 | |
| 2866 | UNLESS(value = PyDict_GetItem(self->memo, py_str)) |
| 2867 | goto finally; |
| 2868 | |
| 2869 | if (PyList_Append(self->stack, value) < 0) |
| 2870 | goto finally; |
| 2871 | |
| 2872 | res = 0; |
| 2873 | |
| 2874 | finally: |
| 2875 | Py_XDECREF(py_str); |
| 2876 | |
| 2877 | return res; |
| 2878 | } |
| 2879 | |
| 2880 | |
| 2881 | static int |
| 2882 | load_binget(Unpicklerobject *self) { |
| 2883 | PyObject *py_key = 0, *value = 0; |
| 2884 | unsigned char key; |
| 2885 | int res = -1; |
| 2886 | char *s; |
| 2887 | |
| 2888 | if ((*self->read_func)(self, &s, 1) < 0) |
| 2889 | goto finally; |
| 2890 | |
| 2891 | key = (unsigned char)s[0]; |
| 2892 | |
| 2893 | UNLESS(py_key = PyInt_FromLong((long)key)) |
| 2894 | goto finally; |
| 2895 | |
| 2896 | UNLESS(value = PyDict_GetItem(self->memo, py_key)) |
| 2897 | goto finally; |
| 2898 | |
| 2899 | if (PyList_Append(self->stack, value) < 0) |
| 2900 | goto finally; |
| 2901 | |
| 2902 | res = 0; |
| 2903 | |
| 2904 | finally: |
| 2905 | Py_XDECREF(py_key); |
| 2906 | |
| 2907 | return res; |
| 2908 | } |
| 2909 | |
| 2910 | |
| 2911 | static int |
| 2912 | load_long_binget(Unpicklerobject *self) { |
| 2913 | PyObject *py_key = 0, *value = 0; |
| 2914 | unsigned char c, *s; |
| 2915 | long key; |
| 2916 | int res = -1; |
| 2917 | |
| 2918 | if ((*self->read_func)(self, &s, 4) < 0) |
| 2919 | goto finally; |
| 2920 | |
| 2921 | c = (unsigned char)s[0]; |
| 2922 | key = (long)c; |
| 2923 | c = (unsigned char)s[1]; |
| 2924 | key |= (long)c << 8; |
| 2925 | c = (unsigned char)s[2]; |
| 2926 | key |= (long)c << 16; |
| 2927 | c = (unsigned char)s[3]; |
| 2928 | key |= (long)c << 24; |
| 2929 | |
| 2930 | UNLESS(py_key = PyInt_FromLong(key)) |
| 2931 | goto finally; |
| 2932 | |
| 2933 | UNLESS(value = PyDict_GetItem(self->memo, py_key)) |
| 2934 | goto finally; |
| 2935 | |
| 2936 | if (PyList_Append(self->stack, value) < 0) |
| 2937 | goto finally; |
| 2938 | |
| 2939 | res = 0; |
| 2940 | |
| 2941 | finally: |
| 2942 | Py_XDECREF(py_key); |
| 2943 | |
| 2944 | return res; |
| 2945 | } |
| 2946 | |
| 2947 | |
| 2948 | static int |
| 2949 | load_put(Unpicklerobject *self) { |
| 2950 | PyObject *py_str = 0, *value = 0; |
| 2951 | int len, res = -1; |
| 2952 | char *s; |
| 2953 | |
| 2954 | if ((len = (*self->readline_func)(self, &s)) < 0) |
| 2955 | goto finally; |
| 2956 | |
| 2957 | UNLESS(py_str = PyString_FromStringAndSize(s, len - 1)) |
| 2958 | goto finally; |
| 2959 | |
| 2960 | if ((len = PyList_Size(self->stack)) < 0) |
| 2961 | goto finally; |
| 2962 | |
| 2963 | UNLESS(value = PyList_GetItem(self->stack, len - 1)) |
| 2964 | goto finally; |
| 2965 | |
| 2966 | if (PyDict_SetItem(self->memo, py_str, value) < 0) |
| 2967 | goto finally; |
| 2968 | |
| 2969 | res = 0; |
| 2970 | |
| 2971 | finally: |
| 2972 | Py_XDECREF(py_str); |
| 2973 | |
| 2974 | return res; |
| 2975 | } |
| 2976 | |
| 2977 | |
| 2978 | static int |
| 2979 | load_binput(Unpicklerobject *self) { |
| 2980 | PyObject *py_key = 0, *value = 0; |
| 2981 | unsigned char key, *s; |
| 2982 | int len, res = -1; |
| 2983 | |
| 2984 | if ((*self->read_func)(self, &s, 1) < 0) |
| 2985 | goto finally; |
| 2986 | |
| 2987 | key = (unsigned char)s[0]; |
| 2988 | |
| 2989 | UNLESS(py_key = PyInt_FromLong((long)key)) |
| 2990 | goto finally; |
| 2991 | |
| 2992 | if ((len = PyList_Size(self->stack)) < 0) |
| 2993 | goto finally; |
| 2994 | |
| 2995 | UNLESS(value = PyList_GetItem(self->stack, len - 1)) |
| 2996 | goto finally; |
| 2997 | |
| 2998 | if (PyDict_SetItem(self->memo, py_key, value) < 0) |
| 2999 | goto finally; |
| 3000 | |
| 3001 | res = 0; |
| 3002 | |
| 3003 | finally: |
| 3004 | Py_XDECREF(py_key); |
| 3005 | |
| 3006 | return res; |
| 3007 | } |
| 3008 | |
| 3009 | |
| 3010 | static int |
| 3011 | load_long_binput(Unpicklerobject *self) { |
| 3012 | PyObject *py_key = 0, *value = 0; |
| 3013 | long key; |
| 3014 | unsigned char c, *s; |
| 3015 | int len, res = -1; |
| 3016 | |
| 3017 | if ((*self->read_func)(self, &s, 4) < 0) |
| 3018 | goto finally; |
| 3019 | |
| 3020 | c = (unsigned char)s[0]; |
| 3021 | key = (long)c; |
| 3022 | c = (unsigned char)s[1]; |
| 3023 | key |= (long)c << 8; |
| 3024 | c = (unsigned char)s[2]; |
| 3025 | key |= (long)c << 16; |
| 3026 | c = (unsigned char)s[3]; |
| 3027 | key |= (long)c << 24; |
| 3028 | |
| 3029 | UNLESS(py_key = PyInt_FromLong(key)) |
| 3030 | goto finally; |
| 3031 | |
| 3032 | if ((len = PyList_Size(self->stack)) < 0) |
| 3033 | goto finally; |
| 3034 | |
| 3035 | UNLESS(value = PyList_GetItem(self->stack, len - 1)) |
| 3036 | goto finally; |
| 3037 | |
| 3038 | if (PyDict_SetItem(self->memo, py_key, value) < 0) |
| 3039 | goto finally; |
| 3040 | |
| 3041 | res = 0; |
| 3042 | |
| 3043 | finally: |
| 3044 | Py_XDECREF(py_key); |
| 3045 | |
| 3046 | return res; |
| 3047 | } |
| 3048 | |
| 3049 | |
| 3050 | static int |
| 3051 | do_append(Unpicklerobject *self, int x) { |
| 3052 | PyObject *value = 0, *list = 0, *append_method = 0; |
| 3053 | int len, i; |
| 3054 | |
| 3055 | if ((len = PyList_Size(self->stack)) < 0) |
| 3056 | return -1; |
| 3057 | |
| 3058 | UNLESS(list = PyList_GetItem(self->stack, x - 1)) |
| 3059 | goto err; |
| 3060 | |
| 3061 | if (PyList_Check(list)) { |
| 3062 | PyObject *slice = 0; |
| 3063 | int list_len; |
| 3064 | |
| 3065 | UNLESS(slice = PyList_GetSlice(self->stack, x, len)) |
| 3066 | return -1; |
| 3067 | |
| 3068 | list_len = PyList_Size(list); |
| 3069 | if (PyList_SetSlice(list, list_len, list_len, slice) < 0) { |
| 3070 | Py_DECREF(slice); |
| 3071 | return -1; |
| 3072 | } |
| 3073 | |
| 3074 | Py_DECREF(slice); |
| 3075 | } |
| 3076 | else { |
| 3077 | |
| 3078 | UNLESS(append_method = PyObject_GetAttr(list, append_str)) |
| 3079 | return -1; |
| 3080 | |
| 3081 | for (i = x; i < len; i++) { |
| 3082 | PyObject *junk; |
| 3083 | |
| 3084 | UNLESS(value = PyList_GetItem(self->stack, i)) |
| 3085 | return -1; |
| 3086 | |
| 3087 | UNLESS(self->arg) |
| 3088 | UNLESS(self->arg = PyTuple_New(1)) |
| 3089 | goto err; |
| 3090 | |
| 3091 | Py_INCREF(value); |
| 3092 | if (PyTuple_SetItem(self->arg, 0, value) < 0) |
| 3093 | goto err; |
| 3094 | |
| 3095 | UNLESS(junk = PyObject_CallObject(append_method, self->arg)) |
| 3096 | goto err; |
| 3097 | Py_DECREF(junk); |
| 3098 | } |
| 3099 | } |
| 3100 | |
| 3101 | if (DEL_LIST_SLICE(self->stack, x, len) < 0) |
| 3102 | goto err; |
| 3103 | |
| 3104 | Py_XDECREF(append_method); |
| 3105 | |
| 3106 | return 0; |
| 3107 | |
| 3108 | err: |
| 3109 | Py_XDECREF(append_method); |
| 3110 | |
| 3111 | return -1; |
| 3112 | } |
| 3113 | |
| 3114 | |
| 3115 | static int |
| 3116 | load_append(Unpicklerobject *self) { |
| 3117 | return do_append(self, PyList_Size(self->stack) - 1); |
| 3118 | } |
| 3119 | |
| 3120 | |
| 3121 | static int |
| 3122 | load_appends(Unpicklerobject *self) { |
| 3123 | return do_append(self, marker(self)); |
| 3124 | } |
| 3125 | |
| 3126 | |
| 3127 | static int |
| 3128 | do_setitems(Unpicklerobject *self, int x) { |
| 3129 | PyObject *value = 0, *key = 0, *dict = 0; |
| 3130 | int len, i, res = -1; |
| 3131 | |
| 3132 | if ((len = PyList_Size(self->stack)) < 0) |
| 3133 | goto finally; |
| 3134 | |
| 3135 | UNLESS(dict = PyList_GetItem(self->stack, x - 1)) |
| 3136 | goto finally; |
| 3137 | |
| 3138 | for (i = x; i < len; i += 2) { |
| 3139 | UNLESS(key = PyList_GetItem(self->stack, i)) |
| 3140 | goto finally; |
| 3141 | |
| 3142 | UNLESS(value = PyList_GetItem(self->stack, i + 1)) |
| 3143 | goto finally; |
| 3144 | |
| 3145 | if (PyObject_SetItem(dict, key, value) < 0) |
| 3146 | goto finally; |
| 3147 | } |
| 3148 | |
| 3149 | if (DEL_LIST_SLICE(self->stack, x, len) < 0) |
| 3150 | goto finally; |
| 3151 | |
| 3152 | res = 0; |
| 3153 | |
| 3154 | finally: |
| 3155 | |
| 3156 | return res; |
| 3157 | } |
| 3158 | |
| 3159 | |
| 3160 | static int |
| 3161 | load_setitem(Unpicklerobject *self) { |
| 3162 | return do_setitems(self, PyList_Size(self->stack) - 2); |
| 3163 | } |
| 3164 | |
| 3165 | |
| 3166 | static int |
| 3167 | load_setitems(Unpicklerobject *self) { |
| 3168 | return do_setitems(self, marker(self)); |
| 3169 | } |
| 3170 | |
| 3171 | |
| 3172 | static int |
| 3173 | load_build(Unpicklerobject *self) { |
| 3174 | PyObject *value = 0, *inst = 0, *instdict = 0, *d_key = 0, *d_value = 0, |
| 3175 | *junk = 0, *__setstate__ = 0; |
| 3176 | int len, i, res = -1; |
| 3177 | |
| 3178 | if ((len = PyList_Size(self->stack)) < 0) |
| 3179 | goto finally; |
| 3180 | |
| 3181 | UNLESS(value = PyList_GetItem(self->stack, len - 1)) |
| 3182 | goto finally; |
| 3183 | Py_INCREF(value); |
| 3184 | |
| 3185 | if (DEL_LIST_SLICE(self->stack, len - 1, len) < 0) |
| 3186 | goto finally; |
| 3187 | |
| 3188 | UNLESS(inst = PyList_GetItem(self->stack, len - 2)) |
| 3189 | goto finally; |
| 3190 | |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame^] | 3191 | UNLESS(__setstate__ = PyObject_GetAttr(inst, __setstate___str)) { |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3192 | PyErr_Clear(); |
| 3193 | |
| 3194 | UNLESS(instdict = PyObject_GetAttr(inst, __dict___str)) |
| 3195 | goto finally; |
| 3196 | |
| 3197 | i = 0; |
| 3198 | while (PyDict_Next(value, &i, &d_key, &d_value)) { |
| 3199 | if (PyObject_SetItem(instdict, d_key, d_value) < 0) |
| 3200 | goto finally; |
| 3201 | } |
| 3202 | } |
| 3203 | else { |
| 3204 | UNLESS(self->arg) |
| 3205 | UNLESS(self->arg = PyTuple_New(1)) |
| 3206 | goto finally; |
| 3207 | |
| 3208 | Py_INCREF(value); |
| 3209 | if (PyTuple_SetItem(self->arg, 0, value) < 0) |
| 3210 | goto finally; |
| 3211 | |
| 3212 | UNLESS(junk = PyObject_CallObject(__setstate__, self->arg)) |
| 3213 | goto finally; |
| 3214 | Py_DECREF(junk); |
| 3215 | } |
| 3216 | |
| 3217 | res = 0; |
| 3218 | |
| 3219 | finally: |
| 3220 | Py_XDECREF(value); |
| 3221 | Py_XDECREF(instdict); |
| 3222 | Py_XDECREF(__setstate__); |
| 3223 | |
| 3224 | return res; |
| 3225 | } |
| 3226 | |
| 3227 | |
| 3228 | static int |
| 3229 | load_mark(Unpicklerobject *self) { |
| 3230 | int len; |
| 3231 | |
| 3232 | if ((len = PyList_Size(self->stack)) < 0) |
| 3233 | return -1; |
| 3234 | |
| 3235 | if (!self->marks_size) { |
| 3236 | self->marks_size = 20; |
| 3237 | UNLESS(self->marks = (int *)malloc(self->marks_size * sizeof(int))) { |
| 3238 | PyErr_NoMemory(); |
| 3239 | return -1; |
| 3240 | } |
| 3241 | } |
| 3242 | else if ((self->num_marks + 1) >= self->marks_size) { |
| 3243 | UNLESS(self->marks = (int *)realloc(self->marks, |
| 3244 | (self->marks_size + 20) * sizeof(int))) { |
| 3245 | PyErr_NoMemory(); |
| 3246 | return -1; |
| 3247 | } |
| 3248 | |
| 3249 | self->marks_size += 20; |
| 3250 | } |
| 3251 | |
| 3252 | self->marks[self->num_marks++] = len; |
| 3253 | |
| 3254 | return 0; |
| 3255 | } |
| 3256 | |
| 3257 | static int |
| 3258 | load_reduce(Unpicklerobject *self) { |
| 3259 | PyObject *callable = 0, *arg_tup = 0, *ob = 0; |
| 3260 | int len, res = -1; |
| 3261 | |
| 3262 | if ((len = PyList_Size(self->stack)) < 0) |
| 3263 | goto finally; |
| 3264 | |
| 3265 | UNLESS(arg_tup = PyList_GetItem(self->stack, len - 1)) |
| 3266 | goto finally; |
| 3267 | |
| 3268 | UNLESS(callable = PyList_GetItem(self->stack, len - 2)) |
| 3269 | goto finally; |
| 3270 | |
| 3271 | UNLESS(ob = Instance_New(callable, arg_tup)) |
| 3272 | goto finally; |
| 3273 | |
| 3274 | if (PyList_Append(self->stack, ob) < 0) |
| 3275 | goto finally; |
| 3276 | |
| 3277 | if (DEL_LIST_SLICE(self->stack, len - 2, len) < 0) |
| 3278 | goto finally; |
| 3279 | |
| 3280 | res = 0; |
| 3281 | |
| 3282 | finally: |
| 3283 | Py_XDECREF(ob); |
| 3284 | |
| 3285 | return res; |
| 3286 | } |
| 3287 | |
| 3288 | static PyObject * |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame^] | 3289 | load(Unpicklerobject *self) { |
Guido van Rossum | 142eeb8 | 1997-08-13 03:14:41 +0000 | [diff] [blame] | 3290 | PyObject *stack = 0, *err = 0, *val = 0; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3291 | int len; |
| 3292 | char *s; |
| 3293 | |
| 3294 | UNLESS(stack = PyList_New(0)) |
| 3295 | goto err; |
| 3296 | |
| 3297 | self->stack = stack; |
| 3298 | self->num_marks = 0; |
| 3299 | |
| 3300 | while (1) { |
| 3301 | if ((*self->read_func)(self, &s, 1) < 0) |
| 3302 | break; |
| 3303 | |
| 3304 | switch (s[0]) { |
| 3305 | case NONE: |
| 3306 | if (load_none(self) < 0) |
| 3307 | break; |
| 3308 | continue; |
| 3309 | |
| 3310 | case BININT: |
| 3311 | if (load_binint(self) < 0) |
| 3312 | break; |
| 3313 | continue; |
| 3314 | |
| 3315 | case BININT1: |
| 3316 | if (load_binint1(self) < 0) |
| 3317 | break; |
| 3318 | continue; |
| 3319 | |
| 3320 | case BININT2: |
| 3321 | if (load_binint2(self) < 0) |
| 3322 | break; |
| 3323 | continue; |
| 3324 | |
| 3325 | case INT: |
| 3326 | if (load_int(self) < 0) |
| 3327 | break; |
| 3328 | continue; |
| 3329 | |
| 3330 | case LONG: |
| 3331 | if (load_long(self) < 0) |
| 3332 | break; |
| 3333 | continue; |
| 3334 | |
| 3335 | case FLOAT: |
| 3336 | if (load_float(self) < 0) |
| 3337 | break; |
| 3338 | continue; |
| 3339 | |
| 3340 | #ifdef FORMAT_1_3 |
| 3341 | case BINFLOAT: |
| 3342 | if (load_binfloat(self) < 0) |
| 3343 | break; |
| 3344 | continue; |
| 3345 | #endif |
| 3346 | |
| 3347 | case BINSTRING: |
| 3348 | if (load_binstring(self) < 0) |
| 3349 | break; |
| 3350 | continue; |
| 3351 | |
| 3352 | case SHORT_BINSTRING: |
| 3353 | if (load_short_binstring(self) < 0) |
| 3354 | break; |
| 3355 | continue; |
| 3356 | |
| 3357 | case STRING: |
| 3358 | if (load_string(self) < 0) |
| 3359 | break; |
| 3360 | continue; |
| 3361 | |
| 3362 | case EMPTY_TUPLE: |
| 3363 | if (load_empty_tuple(self) < 0) |
| 3364 | break; |
| 3365 | continue; |
| 3366 | |
| 3367 | case TUPLE: |
| 3368 | if (load_tuple(self) < 0) |
| 3369 | break; |
| 3370 | continue; |
| 3371 | |
| 3372 | case EMPTY_LIST: |
| 3373 | if (load_empty_list(self) < 0) |
| 3374 | break; |
| 3375 | continue; |
| 3376 | |
| 3377 | case LIST: |
| 3378 | if (load_list(self) < 0) |
| 3379 | break; |
| 3380 | continue; |
| 3381 | |
| 3382 | case EMPTY_DICT: |
| 3383 | if (load_empty_dict(self) < 0) |
| 3384 | break; |
| 3385 | continue; |
| 3386 | |
| 3387 | case DICT: |
| 3388 | if (load_dict(self) < 0) |
| 3389 | break; |
| 3390 | continue; |
| 3391 | |
| 3392 | case OBJ: |
| 3393 | if (load_obj(self) < 0) |
| 3394 | break; |
| 3395 | continue; |
| 3396 | |
| 3397 | case INST: |
| 3398 | if (load_inst(self) < 0) |
| 3399 | break; |
| 3400 | continue; |
| 3401 | |
| 3402 | case GLOBAL: |
| 3403 | if (load_global(self) < 0) |
| 3404 | break; |
| 3405 | continue; |
| 3406 | |
| 3407 | case APPEND: |
| 3408 | if (load_append(self) < 0) |
| 3409 | break; |
| 3410 | continue; |
| 3411 | |
| 3412 | case APPENDS: |
| 3413 | if (load_appends(self) < 0) |
| 3414 | break; |
| 3415 | continue; |
| 3416 | |
| 3417 | case BUILD: |
| 3418 | if (load_build(self) < 0) |
| 3419 | break; |
| 3420 | continue; |
| 3421 | |
| 3422 | case DUP: |
| 3423 | if (load_dup(self) < 0) |
| 3424 | break; |
| 3425 | continue; |
| 3426 | |
| 3427 | case BINGET: |
| 3428 | if (load_binget(self) < 0) |
| 3429 | break; |
| 3430 | continue; |
| 3431 | |
| 3432 | case LONG_BINGET: |
| 3433 | if (load_long_binget(self) < 0) |
| 3434 | break; |
| 3435 | continue; |
| 3436 | |
| 3437 | case GET: |
| 3438 | if (load_get(self) < 0) |
| 3439 | break; |
| 3440 | continue; |
| 3441 | |
| 3442 | case MARK: |
| 3443 | if (load_mark(self) < 0) |
| 3444 | break; |
| 3445 | continue; |
| 3446 | |
| 3447 | case BINPUT: |
| 3448 | if (load_binput(self) < 0) |
| 3449 | break; |
| 3450 | continue; |
| 3451 | |
| 3452 | case LONG_BINPUT: |
| 3453 | if (load_long_binput(self) < 0) |
| 3454 | break; |
| 3455 | continue; |
| 3456 | |
| 3457 | case PUT: |
| 3458 | if (load_put(self) < 0) |
| 3459 | break; |
| 3460 | continue; |
| 3461 | |
| 3462 | case POP: |
| 3463 | if (load_pop(self) < 0) |
| 3464 | break; |
| 3465 | continue; |
| 3466 | |
| 3467 | case POP_MARK: |
| 3468 | if (load_pop_mark(self) < 0) |
| 3469 | break; |
| 3470 | continue; |
| 3471 | |
| 3472 | case SETITEM: |
| 3473 | if (load_setitem(self) < 0) |
| 3474 | break; |
| 3475 | continue; |
| 3476 | |
| 3477 | case SETITEMS: |
| 3478 | if (load_setitems(self) < 0) |
| 3479 | break; |
| 3480 | continue; |
| 3481 | |
| 3482 | case STOP: |
| 3483 | break; |
| 3484 | |
| 3485 | case PERSID: |
| 3486 | if (load_persid(self) < 0) |
| 3487 | break; |
| 3488 | continue; |
| 3489 | |
| 3490 | case BINPERSID: |
| 3491 | if (load_binpersid(self) < 0) |
| 3492 | break; |
| 3493 | continue; |
| 3494 | |
| 3495 | case REDUCE: |
| 3496 | if (load_reduce(self) < 0) |
| 3497 | break; |
| 3498 | continue; |
| 3499 | |
| 3500 | default: |
| 3501 | PyErr_Format(UnpicklingError, "invalid load key, '%s'.", |
| 3502 | "c", s[0]); |
| 3503 | goto err; |
| 3504 | } |
| 3505 | |
| 3506 | break; |
| 3507 | } |
| 3508 | |
| 3509 | if ((err = PyErr_Occurred()) == PyExc_EOFError) { |
| 3510 | PyErr_SetNone(PyExc_EOFError); |
| 3511 | goto err; |
| 3512 | } |
| 3513 | |
| 3514 | if (err) goto err; |
| 3515 | |
| 3516 | if ((len = PyList_Size(stack)) < 0) goto err; |
| 3517 | |
| 3518 | UNLESS(val = PyList_GetItem(stack, len - 1)) goto err; |
| 3519 | Py_INCREF(val); |
| 3520 | |
| 3521 | Py_DECREF(stack); |
| 3522 | |
| 3523 | self->stack=NULL; |
| 3524 | return val; |
| 3525 | |
| 3526 | err: |
| 3527 | self->stack=NULL; |
| 3528 | Py_XDECREF(stack); |
| 3529 | |
| 3530 | return NULL; |
| 3531 | } |
| 3532 | |
| 3533 | |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame^] | 3534 | /* No-load functions to support noload, which is used to |
| 3535 | find persistent references. */ |
| 3536 | |
| 3537 | static int |
| 3538 | noload_obj(Unpicklerobject *self) { |
| 3539 | int i, len; |
| 3540 | |
| 3541 | if ((i = marker(self)) < 0) return -1; |
| 3542 | if ((len = PyList_Size(self->stack)) < 0) return -1; |
| 3543 | return DEL_LIST_SLICE(self->stack, i+1, len); |
| 3544 | } |
| 3545 | |
| 3546 | |
| 3547 | static int |
| 3548 | noload_inst(Unpicklerobject *self) { |
| 3549 | int i, j; |
| 3550 | char *s; |
| 3551 | |
| 3552 | if ((i = marker(self)) < 0) return -1; |
| 3553 | if ((j = PyList_Size(self->stack)) < 0) return -1; |
| 3554 | if (DEL_LIST_SLICE(self->stack, i, j) < 0) return -1; |
| 3555 | if ((*self->readline_func)(self, &s) < 0) return -1; |
| 3556 | if ((*self->readline_func)(self, &s) < 0) return -1; |
| 3557 | return PyList_Append(self->stack, Py_None); |
| 3558 | } |
| 3559 | |
| 3560 | static int |
| 3561 | noload_global(Unpicklerobject *self) { |
| 3562 | char *s; |
| 3563 | |
| 3564 | if ((*self->readline_func)(self, &s) < 0) return -1; |
| 3565 | if ((*self->readline_func)(self, &s) < 0) return -1; |
| 3566 | return PyList_Append(self->stack, Py_None); |
| 3567 | } |
| 3568 | |
| 3569 | static int |
| 3570 | noload_reduce(Unpicklerobject *self) { |
| 3571 | int len; |
| 3572 | |
| 3573 | if ((len = PyList_Size(self->stack)) < 0) return -1; |
| 3574 | if (DEL_LIST_SLICE(self->stack, len - 2, len) < 0) return -1; |
| 3575 | return PyList_Append(self->stack, Py_None); |
| 3576 | } |
| 3577 | |
| 3578 | static int |
| 3579 | noload_build(Unpicklerobject *self) { |
| 3580 | int len; |
| 3581 | |
| 3582 | if ((len = PyList_Size(self->stack)) < 0) return -1; |
| 3583 | return DEL_LIST_SLICE(self->stack, len - 1, len); |
| 3584 | } |
| 3585 | |
| 3586 | |
| 3587 | static PyObject * |
| 3588 | noload(Unpicklerobject *self) { |
| 3589 | PyObject *stack = 0, *err = 0, *val = 0; |
| 3590 | int len; |
| 3591 | char *s; |
| 3592 | |
| 3593 | UNLESS(stack = PyList_New(0)) |
| 3594 | goto err; |
| 3595 | |
| 3596 | self->stack = stack; |
| 3597 | self->num_marks = 0; |
| 3598 | |
| 3599 | while (1) { |
| 3600 | if ((*self->read_func)(self, &s, 1) < 0) |
| 3601 | break; |
| 3602 | |
| 3603 | switch (s[0]) { |
| 3604 | case NONE: |
| 3605 | if (load_none(self) < 0) |
| 3606 | break; |
| 3607 | continue; |
| 3608 | |
| 3609 | case BININT: |
| 3610 | if (load_binint(self) < 0) |
| 3611 | break; |
| 3612 | continue; |
| 3613 | |
| 3614 | case BININT1: |
| 3615 | if (load_binint1(self) < 0) |
| 3616 | break; |
| 3617 | continue; |
| 3618 | |
| 3619 | case BININT2: |
| 3620 | if (load_binint2(self) < 0) |
| 3621 | break; |
| 3622 | continue; |
| 3623 | |
| 3624 | case INT: |
| 3625 | if (load_int(self) < 0) |
| 3626 | break; |
| 3627 | continue; |
| 3628 | |
| 3629 | case LONG: |
| 3630 | if (load_long(self) < 0) |
| 3631 | break; |
| 3632 | continue; |
| 3633 | |
| 3634 | case FLOAT: |
| 3635 | if (load_float(self) < 0) |
| 3636 | break; |
| 3637 | continue; |
| 3638 | |
| 3639 | case BINFLOAT: |
| 3640 | if (load_binfloat(self) < 0) |
| 3641 | break; |
| 3642 | continue; |
| 3643 | |
| 3644 | case BINSTRING: |
| 3645 | if (load_binstring(self) < 0) |
| 3646 | break; |
| 3647 | continue; |
| 3648 | |
| 3649 | case SHORT_BINSTRING: |
| 3650 | if (load_short_binstring(self) < 0) |
| 3651 | break; |
| 3652 | continue; |
| 3653 | |
| 3654 | case STRING: |
| 3655 | if (load_string(self) < 0) |
| 3656 | break; |
| 3657 | continue; |
| 3658 | |
| 3659 | case EMPTY_TUPLE: |
| 3660 | if (load_empty_tuple(self) < 0) |
| 3661 | break; |
| 3662 | continue; |
| 3663 | |
| 3664 | case TUPLE: |
| 3665 | if (load_tuple(self) < 0) |
| 3666 | break; |
| 3667 | continue; |
| 3668 | |
| 3669 | case EMPTY_LIST: |
| 3670 | if (load_empty_list(self) < 0) |
| 3671 | break; |
| 3672 | continue; |
| 3673 | |
| 3674 | case LIST: |
| 3675 | if (load_list(self) < 0) |
| 3676 | break; |
| 3677 | continue; |
| 3678 | |
| 3679 | case EMPTY_DICT: |
| 3680 | if (load_empty_dict(self) < 0) |
| 3681 | break; |
| 3682 | continue; |
| 3683 | |
| 3684 | case DICT: |
| 3685 | if (load_dict(self) < 0) |
| 3686 | break; |
| 3687 | continue; |
| 3688 | |
| 3689 | case OBJ: |
| 3690 | if (noload_obj(self) < 0) |
| 3691 | break; |
| 3692 | continue; |
| 3693 | |
| 3694 | case INST: |
| 3695 | if (noload_inst(self) < 0) |
| 3696 | break; |
| 3697 | continue; |
| 3698 | |
| 3699 | case GLOBAL: |
| 3700 | if (noload_global(self) < 0) |
| 3701 | break; |
| 3702 | continue; |
| 3703 | |
| 3704 | case APPEND: |
| 3705 | if (load_append(self) < 0) |
| 3706 | break; |
| 3707 | continue; |
| 3708 | |
| 3709 | case APPENDS: |
| 3710 | if (load_appends(self) < 0) |
| 3711 | break; |
| 3712 | continue; |
| 3713 | |
| 3714 | case BUILD: |
| 3715 | if (noload_build(self) < 0) |
| 3716 | break; |
| 3717 | continue; |
| 3718 | |
| 3719 | case DUP: |
| 3720 | if (load_dup(self) < 0) |
| 3721 | break; |
| 3722 | continue; |
| 3723 | |
| 3724 | case BINGET: |
| 3725 | if (load_binget(self) < 0) |
| 3726 | break; |
| 3727 | continue; |
| 3728 | |
| 3729 | case LONG_BINGET: |
| 3730 | if (load_long_binget(self) < 0) |
| 3731 | break; |
| 3732 | continue; |
| 3733 | |
| 3734 | case GET: |
| 3735 | if (load_get(self) < 0) |
| 3736 | break; |
| 3737 | continue; |
| 3738 | |
| 3739 | case MARK: |
| 3740 | if (load_mark(self) < 0) |
| 3741 | break; |
| 3742 | continue; |
| 3743 | |
| 3744 | case BINPUT: |
| 3745 | if (load_binput(self) < 0) |
| 3746 | break; |
| 3747 | continue; |
| 3748 | |
| 3749 | case LONG_BINPUT: |
| 3750 | if (load_long_binput(self) < 0) |
| 3751 | break; |
| 3752 | continue; |
| 3753 | |
| 3754 | case PUT: |
| 3755 | if (load_put(self) < 0) |
| 3756 | break; |
| 3757 | continue; |
| 3758 | |
| 3759 | case POP: |
| 3760 | if (load_pop(self) < 0) |
| 3761 | break; |
| 3762 | continue; |
| 3763 | |
| 3764 | case POP_MARK: |
| 3765 | if (load_pop_mark(self) < 0) |
| 3766 | break; |
| 3767 | continue; |
| 3768 | |
| 3769 | case SETITEM: |
| 3770 | if (load_setitem(self) < 0) |
| 3771 | break; |
| 3772 | continue; |
| 3773 | |
| 3774 | case SETITEMS: |
| 3775 | if (load_setitems(self) < 0) |
| 3776 | break; |
| 3777 | continue; |
| 3778 | |
| 3779 | case STOP: |
| 3780 | break; |
| 3781 | |
| 3782 | case PERSID: |
| 3783 | if (load_persid(self) < 0) |
| 3784 | break; |
| 3785 | continue; |
| 3786 | |
| 3787 | case BINPERSID: |
| 3788 | if (load_binpersid(self) < 0) |
| 3789 | break; |
| 3790 | continue; |
| 3791 | |
| 3792 | case REDUCE: |
| 3793 | if (noload_reduce(self) < 0) |
| 3794 | break; |
| 3795 | continue; |
| 3796 | |
| 3797 | default: |
| 3798 | PyErr_Format(UnpicklingError, "invalid load key, '%s'.", |
| 3799 | "c", s[0]); |
| 3800 | goto err; |
| 3801 | } |
| 3802 | |
| 3803 | break; |
| 3804 | } |
| 3805 | |
| 3806 | if ((err = PyErr_Occurred()) == PyExc_EOFError) { |
| 3807 | PyErr_SetNone(PyExc_EOFError); |
| 3808 | goto err; |
| 3809 | } |
| 3810 | |
| 3811 | if (err) goto err; |
| 3812 | |
| 3813 | if ((len = PyList_Size(stack)) < 0) goto err; |
| 3814 | |
| 3815 | UNLESS(val = PyList_GetItem(stack, len - 1)) goto err; |
| 3816 | Py_INCREF(val); |
| 3817 | |
| 3818 | Py_DECREF(stack); |
| 3819 | |
| 3820 | self->stack=NULL; |
| 3821 | return val; |
| 3822 | |
| 3823 | err: |
| 3824 | self->stack=NULL; |
| 3825 | Py_XDECREF(stack); |
| 3826 | |
| 3827 | return NULL; |
| 3828 | } |
| 3829 | |
| 3830 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3831 | static PyObject * |
| 3832 | Unpickler_load(Unpicklerobject *self, PyObject *args) { |
| 3833 | UNLESS(PyArg_ParseTuple(args, "")) |
| 3834 | return NULL; |
| 3835 | |
| 3836 | return load(self); |
| 3837 | } |
| 3838 | |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame^] | 3839 | static PyObject * |
| 3840 | Unpickler_noload(Unpicklerobject *self, PyObject *args) { |
| 3841 | UNLESS(PyArg_ParseTuple(args, "")) |
| 3842 | return NULL; |
| 3843 | |
| 3844 | return noload(self); |
| 3845 | } |
| 3846 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3847 | |
| 3848 | static struct PyMethodDef Unpickler_methods[] = { |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame^] | 3849 | {"load", (PyCFunction)Unpickler_load, 1, |
| 3850 | "load() -- Load a pickle" |
| 3851 | }, |
| 3852 | {"noload", (PyCFunction)Unpickler_noload, 1, |
| 3853 | "noload() -- not load a pickle, but go through most of the motions\n" |
| 3854 | "\n" |
| 3855 | "This function can be used to read past a pickle without instantiating\n" |
| 3856 | "any objects or importing any modules. It can also be used to find all\n" |
| 3857 | "persistent references without instantiating any objects or importing\n" |
| 3858 | "any modules.\n" |
| 3859 | }, |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3860 | {NULL, NULL} /* sentinel */ |
| 3861 | }; |
| 3862 | |
| 3863 | |
| 3864 | static Unpicklerobject * |
| 3865 | newUnpicklerobject(PyObject *f) { |
| 3866 | Unpicklerobject *self; |
| 3867 | |
| 3868 | UNLESS(self = PyObject_NEW(Unpicklerobject, &Unpicklertype)) |
| 3869 | return NULL; |
| 3870 | |
| 3871 | self->file = NULL; |
| 3872 | self->arg = NULL; |
| 3873 | self->stack = NULL; |
| 3874 | self->pers_func = NULL; |
| 3875 | self->last_string = NULL; |
| 3876 | self->marks = NULL; |
| 3877 | self->num_marks = 0; |
| 3878 | self->marks_size = 0; |
| 3879 | self->buf_size = 0; |
| 3880 | self->read = NULL; |
| 3881 | self->readline = NULL; |
| 3882 | |
| 3883 | UNLESS(self->memo = PyDict_New()) { |
| 3884 | Py_XDECREF((PyObject *)self); |
| 3885 | return NULL; |
| 3886 | } |
| 3887 | |
| 3888 | Py_INCREF(f); |
| 3889 | self->file = f; |
| 3890 | |
| 3891 | /* Set read, readline based on type of f */ |
| 3892 | if (PyFile_Check(f)) { |
| 3893 | self->fp = PyFile_AsFile(f); |
| 3894 | self->read_func = read_file; |
| 3895 | self->readline_func = readline_file; |
| 3896 | } |
| 3897 | else if (PycStringIO_InputCheck(f)) { |
| 3898 | self->fp = NULL; |
| 3899 | self->read_func = read_cStringIO; |
| 3900 | self->readline_func = readline_cStringIO; |
| 3901 | } |
| 3902 | else { |
| 3903 | |
| 3904 | self->fp = NULL; |
| 3905 | self->read_func = read_other; |
| 3906 | self->readline_func = readline_other; |
| 3907 | |
| 3908 | UNLESS((self->readline = PyObject_GetAttr(f, readline_str)) && |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame^] | 3909 | (self->read = PyObject_GetAttr(f, read_str))) { |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3910 | PyErr_Clear(); |
| 3911 | PyErr_SetString( PyExc_TypeError, "argument must have 'read' and " |
| 3912 | "'readline' attributes" ); |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame^] | 3913 | goto err; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3914 | } |
| 3915 | } |
| 3916 | |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame^] | 3917 | if(PyEval_GetRestricted()) { |
| 3918 | /* Restricted execution, get private tables */ |
| 3919 | PyObject *m; |
| 3920 | |
| 3921 | UNLESS(self->class_map=PyDict_New()) goto err; |
| 3922 | UNLESS(m=PyImport_Import(copy_reg_str)) goto err; |
| 3923 | self->safe_constructors=PyObject_GetAttr(m, safe_constructors_str); |
| 3924 | Py_DECREF(m); |
| 3925 | UNLESS(self->safe_constructors) goto err; |
| 3926 | } |
| 3927 | else { |
| 3928 | self->class_map=class_map; |
| 3929 | Py_INCREF(class_map); |
| 3930 | self->safe_constructors=safe_constructors; |
| 3931 | Py_INCREF(safe_constructors); |
| 3932 | } |
| 3933 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3934 | return self; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame^] | 3935 | |
| 3936 | err: |
| 3937 | Py_DECREF((PyObject *)self); |
| 3938 | return NULL; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 3939 | } |
| 3940 | |
| 3941 | |
| 3942 | static PyObject * |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3943 | get_Unpickler(PyObject *self, PyObject *args) { |
| 3944 | PyObject *file; |
| 3945 | |
| 3946 | UNLESS(PyArg_ParseTuple(args, "O", &file)) |
| 3947 | return NULL; |
| 3948 | return (PyObject *)newUnpicklerobject(file); |
| 3949 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 3950 | |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 3951 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3952 | static void |
| 3953 | Unpickler_dealloc(Unpicklerobject *self) { |
| 3954 | Py_XDECREF(self->readline); |
| 3955 | Py_XDECREF(self->read); |
| 3956 | Py_XDECREF(self->file); |
| 3957 | Py_XDECREF(self->memo); |
| 3958 | Py_XDECREF(self->stack); |
| 3959 | Py_XDECREF(self->pers_func); |
| 3960 | Py_XDECREF(self->arg); |
| 3961 | Py_XDECREF(self->last_string); |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame^] | 3962 | Py_XDECREF(self->class_map); |
| 3963 | Py_XDECREF(self->safe_constructors); |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 3964 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3965 | if (self->marks) { |
| 3966 | free(self->marks); |
| 3967 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 3968 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3969 | if (self->buf_size) { |
| 3970 | free(self->buf); |
| 3971 | } |
| 3972 | |
| 3973 | PyMem_DEL(self); |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 3974 | } |
| 3975 | |
| 3976 | |
| 3977 | static PyObject * |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3978 | Unpickler_getattr(Unpicklerobject *self, char *name) { |
| 3979 | if (!strcmp(name, "persistent_load")) { |
| 3980 | if (!self->pers_func) { |
| 3981 | PyErr_SetString(PyExc_AttributeError, name); |
| 3982 | return NULL; |
| 3983 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 3984 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3985 | Py_INCREF(self->pers_func); |
| 3986 | return self->pers_func; |
| 3987 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 3988 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3989 | if (!strcmp(name, "memo")) { |
| 3990 | if (!self->memo) { |
| 3991 | PyErr_SetString(PyExc_AttributeError, name); |
| 3992 | return NULL; |
| 3993 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 3994 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3995 | Py_INCREF(self->memo); |
| 3996 | return self->memo; |
| 3997 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 3998 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 3999 | if (!strcmp(name, "stack")) { |
| 4000 | if (!self->stack) { |
| 4001 | PyErr_SetString(PyExc_AttributeError, name); |
| 4002 | return NULL; |
| 4003 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 4004 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4005 | Py_INCREF(self->stack); |
| 4006 | return self->stack; |
| 4007 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 4008 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4009 | if (!strcmp(name, "UnpicklingError")) { |
| 4010 | Py_INCREF(UnpicklingError); |
| 4011 | return UnpicklingError; |
| 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 | return Py_FindMethod(Unpickler_methods, (PyObject *)self, name); |
| 4015 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 4016 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4017 | |
| 4018 | static int |
| 4019 | Unpickler_setattr(Unpicklerobject *self, char *name, PyObject *value) { |
| 4020 | if (!strcmp(name, "persistent_load")) { |
| 4021 | Py_XDECREF(self->pers_func); |
| 4022 | self->pers_func = value; |
| 4023 | Py_INCREF(value); |
| 4024 | return 0; |
| 4025 | } |
| 4026 | |
| 4027 | PyErr_SetString(PyExc_AttributeError, name); |
| 4028 | return -1; |
| 4029 | } |
| 4030 | |
| 4031 | |
| 4032 | static PyObject * |
| 4033 | cpm_dump(PyObject *self, PyObject *args) { |
| 4034 | PyObject *ob, *file, *res = NULL; |
| 4035 | Picklerobject *pickler = 0; |
| 4036 | int bin = 0; |
| 4037 | |
| 4038 | UNLESS(PyArg_ParseTuple(args, "OO|i", &ob, &file, &bin)) |
| 4039 | goto finally; |
| 4040 | |
| 4041 | UNLESS(pickler = newPicklerobject(file, bin)) |
| 4042 | goto finally; |
| 4043 | |
| 4044 | if (dump(pickler, ob) < 0) |
| 4045 | goto finally; |
| 4046 | |
| 4047 | Py_INCREF(Py_None); |
| 4048 | res = Py_None; |
| 4049 | |
| 4050 | finally: |
| 4051 | Py_XDECREF(pickler); |
| 4052 | |
| 4053 | return res; |
| 4054 | } |
| 4055 | |
| 4056 | |
| 4057 | static PyObject * |
| 4058 | cpm_dumps(PyObject *self, PyObject *args) { |
| 4059 | PyObject *ob, *file = 0, *res = NULL; |
| 4060 | Picklerobject *pickler = 0; |
| 4061 | int bin = 0; |
| 4062 | |
| 4063 | UNLESS(PyArg_ParseTuple(args, "O|i", &ob, &bin)) |
| 4064 | goto finally; |
| 4065 | |
| 4066 | UNLESS(file = PycStringIO->NewOutput(128)) |
| 4067 | goto finally; |
| 4068 | |
| 4069 | UNLESS(pickler = newPicklerobject(file, bin)) |
| 4070 | goto finally; |
| 4071 | |
| 4072 | if (dump(pickler, ob) < 0) |
| 4073 | goto finally; |
| 4074 | |
| 4075 | res = PycStringIO->cgetvalue(file); |
| 4076 | |
| 4077 | finally: |
| 4078 | Py_XDECREF(pickler); |
| 4079 | Py_XDECREF(file); |
| 4080 | |
| 4081 | return res; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 4082 | } |
| 4083 | |
| 4084 | |
| 4085 | static PyObject * |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4086 | cpm_load(PyObject *self, PyObject *args) { |
| 4087 | Unpicklerobject *unpickler = 0; |
| 4088 | PyObject *ob, *res = NULL; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 4089 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4090 | UNLESS(PyArg_ParseTuple(args, "O", &ob)) |
| 4091 | goto finally; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 4092 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4093 | UNLESS(unpickler = newUnpicklerobject(ob)) |
| 4094 | goto finally; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 4095 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4096 | res = load(unpickler); |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 4097 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4098 | finally: |
| 4099 | Py_XDECREF(unpickler); |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 4100 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4101 | return res; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 4102 | } |
| 4103 | |
| 4104 | |
| 4105 | static PyObject * |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4106 | cpm_loads(PyObject *self, PyObject *args) { |
| 4107 | PyObject *ob, *file = 0, *res = NULL; |
| 4108 | Unpicklerobject *unpickler = 0; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 4109 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4110 | UNLESS(PyArg_ParseTuple(args, "O", &ob)) |
| 4111 | goto finally; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 4112 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4113 | UNLESS(file = PycStringIO->NewInput(ob)) |
| 4114 | goto finally; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 4115 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4116 | UNLESS(unpickler = newUnpicklerobject(file)) |
| 4117 | goto finally; |
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 | res = load(unpickler); |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 4120 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4121 | finally: |
| 4122 | Py_XDECREF(file); |
| 4123 | Py_XDECREF(unpickler); |
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 | return res; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 4126 | } |
| 4127 | |
| 4128 | |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame^] | 4129 | static char Unpicklertype__doc__[] = |
| 4130 | "Objects that know how to unpickle"; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 4131 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4132 | static PyTypeObject Unpicklertype_value() { |
| 4133 | PyTypeObject Unpicklertype = { |
| 4134 | PyObject_HEAD_INIT(&PyType_Type) |
| 4135 | 0, /*ob_size*/ |
| 4136 | "Unpickler", /*tp_name*/ |
| 4137 | sizeof(Unpicklerobject), /*tp_basicsize*/ |
| 4138 | 0, /*tp_itemsize*/ |
| 4139 | /* methods */ |
| 4140 | (destructor)Unpickler_dealloc, /*tp_dealloc*/ |
| 4141 | (printfunc)0, /*tp_print*/ |
| 4142 | (getattrfunc)Unpickler_getattr, /*tp_getattr*/ |
| 4143 | (setattrfunc)Unpickler_setattr, /*tp_setattr*/ |
| 4144 | (cmpfunc)0, /*tp_compare*/ |
| 4145 | (reprfunc)0, /*tp_repr*/ |
| 4146 | 0, /*tp_as_number*/ |
| 4147 | 0, /*tp_as_sequence*/ |
| 4148 | 0, /*tp_as_mapping*/ |
| 4149 | (hashfunc)0, /*tp_hash*/ |
| 4150 | (ternaryfunc)0, /*tp_call*/ |
| 4151 | (reprfunc)0, /*tp_str*/ |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 4152 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4153 | /* Space for future expansion */ |
| 4154 | 0L,0L,0L,0L, |
| 4155 | Unpicklertype__doc__ /* Documentation string */ |
| 4156 | }; |
| 4157 | return Unpicklertype; |
| 4158 | } |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 4159 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4160 | static struct PyMethodDef cPickle_methods[] = { |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame^] | 4161 | {"dump", (PyCFunction)cpm_dump, 1, |
| 4162 | "dump(object, file, [binary]) --" |
| 4163 | "Write an object in pickle format to the given file\n" |
| 4164 | "\n" |
| 4165 | "If the optional argument, binary, is provided and is true, then the\n" |
| 4166 | "pickle will be written in binary format, which is more space and\n" |
| 4167 | "computationally efficient. \n" |
| 4168 | }, |
| 4169 | {"dumps", (PyCFunction)cpm_dumps, 1, |
| 4170 | "dumps(object, [binary]) --" |
| 4171 | "Return a string containing an object in pickle format\n" |
| 4172 | "\n" |
| 4173 | "If the optional argument, binary, is provided and is true, then the\n" |
| 4174 | "pickle will be written in binary format, which is more space and\n" |
| 4175 | "computationally efficient. \n" |
| 4176 | }, |
| 4177 | {"load", (PyCFunction)cpm_load, 1, |
| 4178 | "load(file) -- Load a pickle from the given file"}, |
| 4179 | {"loads", (PyCFunction)cpm_loads, 1, |
| 4180 | "loads(string) -- Load a pickle from the given string"}, |
| 4181 | {"Pickler", (PyCFunction)get_Pickler, 1, |
| 4182 | "Pickler(file, [binary]) -- Create a pickler\n" |
| 4183 | "\n" |
| 4184 | "If the optional argument, binary, is provided and is true, then\n" |
| 4185 | "pickles will be written in binary format, which is more space and\n" |
| 4186 | "computationally efficient. \n" |
| 4187 | }, |
| 4188 | {"Unpickler", (PyCFunction)get_Unpickler, 1, |
| 4189 | "Unpickler(file) -- Create an unpickler"}, |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 4190 | { NULL, NULL } |
| 4191 | }; |
| 4192 | |
| 4193 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4194 | #define CHECK_FOR_ERRORS(MESS) \ |
| 4195 | if(PyErr_Occurred()) { \ |
| 4196 | PyObject *__sys_exc_type, *__sys_exc_value, *__sys_exc_traceback; \ |
| 4197 | PyErr_Fetch( &__sys_exc_type, &__sys_exc_value, &__sys_exc_traceback); \ |
| 4198 | fprintf(stderr, # MESS ":\n\t"); \ |
| 4199 | PyObject_Print(__sys_exc_type, stderr,0); \ |
| 4200 | fprintf(stderr,", "); \ |
| 4201 | PyObject_Print(__sys_exc_value, stderr,0); \ |
| 4202 | fprintf(stderr,"\n"); \ |
| 4203 | fflush(stderr); \ |
| 4204 | Py_FatalError(# MESS); \ |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 4205 | } |
| 4206 | |
| 4207 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4208 | static int |
| 4209 | init_stuff(PyObject *module, PyObject *module_dict) { |
| 4210 | PyObject *string, *copy_reg; |
| 4211 | |
| 4212 | #define INIT_STR(S) UNLESS(S ## _str=PyString_FromString(#S)) return -1; |
| 4213 | |
| 4214 | INIT_STR(__class__); |
| 4215 | INIT_STR(__getinitargs__); |
| 4216 | INIT_STR(__dict__); |
| 4217 | INIT_STR(__getstate__); |
| 4218 | INIT_STR(__setstate__); |
| 4219 | INIT_STR(__name__); |
Guido van Rossum | 142eeb8 | 1997-08-13 03:14:41 +0000 | [diff] [blame] | 4220 | INIT_STR(__main__); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4221 | INIT_STR(__reduce__); |
| 4222 | INIT_STR(write); |
| 4223 | INIT_STR(__safe_for_unpickling__); |
| 4224 | INIT_STR(append); |
| 4225 | INIT_STR(read); |
| 4226 | INIT_STR(readline); |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame^] | 4227 | INIT_STR(copy_reg); |
| 4228 | INIT_STR(dispatch_table); |
| 4229 | INIT_STR(safe_constructors); |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4230 | |
| 4231 | UNLESS(copy_reg = PyImport_ImportModule("copy_reg")) |
| 4232 | return -1; |
| 4233 | |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame^] | 4234 | /* These next few are special because we want to use different |
| 4235 | ones in restricted mode. */ |
| 4236 | |
| 4237 | UNLESS(dispatch_table = PyObject_GetAttr(copy_reg, dispatch_table_str)) |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4238 | return -1; |
| 4239 | |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame^] | 4240 | UNLESS(safe_constructors = PyObject_GetAttr(copy_reg, |
| 4241 | safe_constructors_str)) |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4242 | return -1; |
| 4243 | |
| 4244 | Py_DECREF(copy_reg); |
| 4245 | |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame^] | 4246 | UNLESS(class_map = PyDict_New()) return -1; |
| 4247 | |
| 4248 | /* Down to here ********************************** */ |
| 4249 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4250 | UNLESS(string = PyImport_ImportModule("string")) |
| 4251 | return -1; |
| 4252 | |
| 4253 | UNLESS(atol_func = PyObject_GetAttrString(string, "atol")) |
| 4254 | return -1; |
| 4255 | |
| 4256 | Py_DECREF(string); |
| 4257 | |
| 4258 | UNLESS(empty_tuple = PyTuple_New(0)) |
| 4259 | return -1; |
| 4260 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4261 | UNLESS(PicklingError = PyString_FromString("cPickle.PicklingError")) |
| 4262 | return -1; |
| 4263 | |
| 4264 | if (PyDict_SetItemString(module_dict, "PicklingError", |
| 4265 | PicklingError) < 0) |
| 4266 | return -1; |
| 4267 | |
| 4268 | UNLESS(UnpicklingError = PyString_FromString("cPickle.UnpicklingError")) |
| 4269 | return -1; |
| 4270 | |
| 4271 | if (PyDict_SetItemString(module_dict, "UnpicklingError", |
| 4272 | UnpicklingError) < 0) |
| 4273 | return -1; |
| 4274 | |
| 4275 | PycString_IMPORT; |
| 4276 | |
| 4277 | return 0; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 4278 | } |
| 4279 | |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 4280 | void |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4281 | initcPickle() { |
Guido van Rossum | 9efe8ef | 1997-09-03 18:19:40 +0000 | [diff] [blame] | 4282 | PyObject *m, *d, *v; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame^] | 4283 | char *rev="1.46"; |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4284 | PyObject *format_version; |
| 4285 | PyObject *compatible_formats; |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 4286 | |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 4287 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4288 | /* Create the module and add the functions */ |
| 4289 | m = Py_InitModule4("cPickle", cPickle_methods, |
| 4290 | cPickle_module_documentation, |
| 4291 | (PyObject*)NULL,PYTHON_API_VERSION); |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 4292 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4293 | Picklertype=Picklertype_value(); |
| 4294 | Unpicklertype=Unpicklertype_value(); |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 4295 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4296 | /* Add some symbolic constants to the module */ |
| 4297 | d = PyModule_GetDict(m); |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame^] | 4298 | PyDict_SetItemString(d,"__version__", v = PyString_FromString(rev)); |
Guido van Rossum | 9efe8ef | 1997-09-03 18:19:40 +0000 | [diff] [blame] | 4299 | Py_XDECREF(v); |
Barry Warsaw | 93d29b6 | 1997-01-14 17:45:08 +0000 | [diff] [blame] | 4300 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4301 | #ifdef FORMAT_1_3 |
| 4302 | format_version = PyString_FromString("1.3"); |
| 4303 | compatible_formats = Py_BuildValue("[sss]", "1.0", "1.1", "1.2"); |
| 4304 | #else |
| 4305 | format_version = PyString_FromString("1.2"); |
| 4306 | compatible_formats = Py_BuildValue("[ss]", "1.0", "1.1"); |
| 4307 | #endif |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 4308 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4309 | PyDict_SetItemString(d, "format_version", format_version); |
| 4310 | PyDict_SetItemString(d, "compatible_formats", compatible_formats); |
Guido van Rossum | 9efe8ef | 1997-09-03 18:19:40 +0000 | [diff] [blame] | 4311 | Py_XDECREF(format_version); |
| 4312 | Py_XDECREF(compatible_formats); |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 4313 | |
Guido van Rossum | 60456fd | 1997-04-09 17:36:32 +0000 | [diff] [blame] | 4314 | init_stuff(m, d); |
| 4315 | CHECK_FOR_ERRORS("can't initialize module cPickle"); |
Guido van Rossum | 2f4caa4 | 1997-01-06 22:59:08 +0000 | [diff] [blame] | 4316 | } |