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