Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 1 | /*********************************************************** |
Guido van Rossum | 6d023c9 | 1995-01-04 19:12:13 +0000 | [diff] [blame] | 2 | Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam, |
| 3 | The Netherlands. |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 4 | |
| 5 | All Rights Reserved |
| 6 | |
| 7 | Permission to use, copy, modify, and distribute this software and its |
| 8 | documentation for any purpose and without fee is hereby granted, |
| 9 | provided that the above copyright notice appear in all copies and that |
| 10 | both that copyright notice and this permission notice appear in |
| 11 | supporting documentation, and that the names of Stichting Mathematisch |
| 12 | Centrum or CWI not be used in advertising or publicity pertaining to |
| 13 | distribution of the software without specific, written prior permission. |
| 14 | |
| 15 | STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO |
| 16 | THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND |
| 17 | FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE |
| 18 | FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 19 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 20 | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT |
| 21 | OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 22 | |
| 23 | ******************************************************************/ |
| 24 | |
| 25 | /* Write Python objects to files and read them back. |
| 26 | This is intended for writing and reading compiled Python code only; |
| 27 | a true persistent storage facility would be much harder, since |
| 28 | it would have to take circular links and sharing into account. */ |
| 29 | |
| 30 | #include "allobjects.h" |
Guido van Rossum | 2807d19 | 1992-03-27 17:23:17 +0000 | [diff] [blame] | 31 | #include "modsupport.h" |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 32 | #include "longintrepr.h" |
| 33 | #include "compile.h" |
| 34 | #include "marshal.h" |
| 35 | |
| 36 | #include <errno.h> |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 37 | |
| 38 | #define TYPE_NULL '0' |
| 39 | #define TYPE_NONE 'N' |
| 40 | #define TYPE_INT 'i' |
| 41 | #define TYPE_FLOAT 'f' |
Guido van Rossum | 8a5c5d2 | 1996-01-12 01:09:56 +0000 | [diff] [blame] | 42 | #define TYPE_COMPLEX 'x' |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 43 | #define TYPE_LONG 'l' |
| 44 | #define TYPE_STRING 's' |
| 45 | #define TYPE_TUPLE '(' |
| 46 | #define TYPE_LIST '[' |
| 47 | #define TYPE_DICT '{' |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 48 | #define TYPE_CODE 'c' |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 49 | #define TYPE_UNKNOWN '?' |
| 50 | |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 51 | typedef struct { |
| 52 | FILE *fp; |
| 53 | /* If fp == NULL, the following are valid: */ |
| 54 | object *str; |
| 55 | char *ptr; |
| 56 | char *end; |
| 57 | } WFILE; |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 58 | |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 59 | #define w_byte(c, p) if (((p)->fp)) putc((c), (p)->fp); \ |
| 60 | else if ((p)->ptr != (p)->end) *(p)->ptr++ = (c); \ |
| 61 | else w_more(c, p) |
| 62 | |
| 63 | static void |
| 64 | w_more(c, p) |
| 65 | char c; |
| 66 | WFILE *p; |
| 67 | { |
| 68 | int size, newsize; |
| 69 | if (p->str == NULL) |
| 70 | return; /* An error already occurred */ |
| 71 | size = getstringsize(p->str); |
| 72 | newsize = size + 1024; |
| 73 | if (resizestring(&p->str, newsize) != 0) { |
| 74 | p->ptr = p->end = NULL; |
| 75 | } |
| 76 | else { |
| 77 | p->ptr = GETSTRINGVALUE((stringobject *)p->str) + size; |
| 78 | p->end = GETSTRINGVALUE((stringobject *)p->str) + newsize; |
| 79 | *p->ptr++ = c; |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | static void |
| 84 | w_string(s, n, p) |
| 85 | char *s; |
| 86 | int n; |
| 87 | WFILE *p; |
| 88 | { |
| 89 | if (p->fp != NULL) { |
| 90 | fwrite(s, 1, n, p->fp); |
| 91 | } |
| 92 | else { |
| 93 | while (--n >= 0) { |
| 94 | w_byte(*s, p); |
| 95 | s++; |
| 96 | } |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | static void |
| 101 | w_short(x, p) |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 102 | int x; |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 103 | WFILE *p; |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 104 | { |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 105 | w_byte( x & 0xff, p); |
| 106 | w_byte((x>> 8) & 0xff, p); |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 107 | } |
| 108 | |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 109 | static void |
| 110 | w_long(x, p) |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 111 | long x; |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 112 | WFILE *p; |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 113 | { |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 114 | w_byte((int)( x & 0xff), p); |
| 115 | w_byte((int)((x>> 8) & 0xff), p); |
| 116 | w_byte((int)((x>>16) & 0xff), p); |
| 117 | w_byte((int)((x>>24) & 0xff), p); |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 118 | } |
| 119 | |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 120 | static void |
| 121 | w_object(v, p) |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 122 | object *v; |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 123 | WFILE *p; |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 124 | { |
Guido van Rossum | 3a205f7 | 1995-02-17 15:10:07 +0000 | [diff] [blame] | 125 | int i, n; |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 126 | |
| 127 | if (v == NULL) |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 128 | w_byte(TYPE_NULL, p); |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 129 | else if (v == None) |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 130 | w_byte(TYPE_NONE, p); |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 131 | else if (is_intobject(v)) { |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 132 | w_byte(TYPE_INT, p); |
| 133 | w_long(getintvalue(v), p); |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 134 | } |
| 135 | else if (is_longobject(v)) { |
| 136 | longobject *ob = (longobject *)v; |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 137 | w_byte(TYPE_LONG, p); |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 138 | n = ob->ob_size; |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 139 | w_long((long)n, p); |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 140 | if (n < 0) |
| 141 | n = -n; |
| 142 | for (i = 0; i < n; i++) |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 143 | w_short(ob->ob_digit[i], p); |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 144 | } |
| 145 | else if (is_floatobject(v)) { |
| 146 | extern void float_buf_repr PROTO((char *, floatobject *)); |
| 147 | char buf[256]; /* Plenty to format any double */ |
| 148 | float_buf_repr(buf, (floatobject *)v); |
| 149 | n = strlen(buf); |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 150 | w_byte(TYPE_FLOAT, p); |
Guido van Rossum | 3a205f7 | 1995-02-17 15:10:07 +0000 | [diff] [blame] | 151 | w_byte(n, p); |
| 152 | w_string(buf, n, p); |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 153 | } |
Guido van Rossum | 8a5c5d2 | 1996-01-12 01:09:56 +0000 | [diff] [blame] | 154 | #ifndef WITHOUT_COMPLEX |
| 155 | else if (is_complexobject(v)) { |
| 156 | extern void float_buf_repr PROTO((char *, floatobject *)); |
| 157 | char buf[256]; /* Plenty to format any double */ |
| 158 | floatobject *temp; |
| 159 | w_byte(TYPE_COMPLEX, p); |
| 160 | temp = (floatobject*)newfloatobject(PyComplex_RealAsDouble(v)); |
| 161 | float_buf_repr(buf, temp); |
| 162 | DECREF(temp); |
| 163 | n = strlen(buf); |
| 164 | w_byte(n, p); |
| 165 | w_string(buf, n, p); |
| 166 | temp = (floatobject*)newfloatobject(PyComplex_ImagAsDouble(v)); |
| 167 | float_buf_repr(buf, temp); |
| 168 | DECREF(temp); |
| 169 | n = strlen(buf); |
| 170 | w_byte(n, p); |
| 171 | w_string(buf, n, p); |
| 172 | } |
| 173 | #endif |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 174 | else if (is_stringobject(v)) { |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 175 | w_byte(TYPE_STRING, p); |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 176 | n = getstringsize(v); |
Guido van Rossum | 3a205f7 | 1995-02-17 15:10:07 +0000 | [diff] [blame] | 177 | w_long((long)n, p); |
| 178 | w_string(getstringvalue(v), n, p); |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 179 | } |
| 180 | else if (is_tupleobject(v)) { |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 181 | w_byte(TYPE_TUPLE, p); |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 182 | n = gettuplesize(v); |
Guido van Rossum | 3a205f7 | 1995-02-17 15:10:07 +0000 | [diff] [blame] | 183 | w_long((long)n, p); |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 184 | for (i = 0; i < n; i++) { |
Guido van Rossum | 8d617a6 | 1995-03-09 12:12:11 +0000 | [diff] [blame] | 185 | w_object(GETTUPLEITEM(v, i), p); |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 186 | } |
| 187 | } |
| 188 | else if (is_listobject(v)) { |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 189 | w_byte(TYPE_LIST, p); |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 190 | n = getlistsize(v); |
Guido van Rossum | 3a205f7 | 1995-02-17 15:10:07 +0000 | [diff] [blame] | 191 | w_long((long)n, p); |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 192 | for (i = 0; i < n; i++) { |
Guido van Rossum | 3a205f7 | 1995-02-17 15:10:07 +0000 | [diff] [blame] | 193 | w_object(getlistitem(v, i), p); |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 194 | } |
| 195 | } |
Guido van Rossum | 64b4552 | 1991-06-07 13:58:22 +0000 | [diff] [blame] | 196 | else if (is_dictobject(v)) { |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 197 | int pos; |
| 198 | object *key, *value; |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 199 | w_byte(TYPE_DICT, p); |
Guido van Rossum | 64b4552 | 1991-06-07 13:58:22 +0000 | [diff] [blame] | 200 | /* This one is NULL object terminated! */ |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 201 | pos = 0; |
| 202 | while (mappinggetnext(v, &pos, &key, &value)) { |
| 203 | w_object(key, p); |
| 204 | w_object(value, p); |
Guido van Rossum | 64b4552 | 1991-06-07 13:58:22 +0000 | [diff] [blame] | 205 | } |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 206 | w_object((object *)NULL, p); |
Guido van Rossum | 64b4552 | 1991-06-07 13:58:22 +0000 | [diff] [blame] | 207 | } |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 208 | else if (is_codeobject(v)) { |
| 209 | codeobject *co = (codeobject *)v; |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 210 | w_byte(TYPE_CODE, p); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 211 | w_short(co->co_argcount, p); |
| 212 | w_short(co->co_nlocals, p); |
| 213 | w_short(co->co_flags, p); |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 214 | w_object((object *)co->co_code, p); |
| 215 | w_object(co->co_consts, p); |
| 216 | w_object(co->co_names, p); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 217 | w_object(co->co_varnames, p); |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 218 | w_object(co->co_filename, p); |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 219 | w_object(co->co_name, p); |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 220 | } |
| 221 | else { |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 222 | w_byte(TYPE_UNKNOWN, p); |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 223 | } |
| 224 | } |
| 225 | |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 226 | void |
| 227 | wr_long(x, fp) |
| 228 | long x; |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 229 | FILE *fp; |
| 230 | { |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 231 | WFILE wf; |
| 232 | wf.fp = fp; |
| 233 | w_long(x, &wf); |
| 234 | } |
| 235 | |
| 236 | void |
| 237 | wr_object(x, fp) |
| 238 | object *x; |
| 239 | FILE *fp; |
| 240 | { |
| 241 | WFILE wf; |
| 242 | wf.fp = fp; |
| 243 | w_object(x, &wf); |
| 244 | } |
| 245 | |
| 246 | typedef WFILE RFILE; /* Same struct with different invariants */ |
| 247 | |
Guido van Rossum | 8d617a6 | 1995-03-09 12:12:11 +0000 | [diff] [blame] | 248 | #define rs_byte(p) (((p)->ptr != (p)->end) ? (unsigned char)*(p)->ptr++ : EOF) |
| 249 | |
| 250 | #define r_byte(p) ((p)->fp ? getc((p)->fp) : rs_byte(p)) |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 251 | |
| 252 | static int |
| 253 | r_string(s, n, p) |
| 254 | char *s; |
| 255 | int n; |
| 256 | RFILE *p; |
| 257 | { |
| 258 | if (p->fp != NULL) |
| 259 | return fread(s, 1, n, p->fp); |
| 260 | if (p->end - p->ptr < n) |
| 261 | n = p->end - p->ptr; |
| 262 | memcpy(s, p->ptr, n); |
| 263 | p->ptr += n; |
| 264 | return n; |
| 265 | } |
| 266 | |
| 267 | static int |
| 268 | r_short(p) |
| 269 | RFILE *p; |
| 270 | { |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 271 | register short x; |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 272 | x = r_byte(p); |
| 273 | x |= r_byte(p) << 8; |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 274 | /* XXX If your short is > 16 bits, add sign-extension here!!! */ |
| 275 | return x; |
| 276 | } |
| 277 | |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 278 | static long |
| 279 | r_long(p) |
| 280 | RFILE *p; |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 281 | { |
| 282 | register long x; |
Guido van Rossum | 8d617a6 | 1995-03-09 12:12:11 +0000 | [diff] [blame] | 283 | register FILE *fp = p->fp; |
| 284 | if (fp) { |
| 285 | x = getc(fp); |
| 286 | x |= (long)getc(fp) << 8; |
| 287 | x |= (long)getc(fp) << 16; |
| 288 | x |= (long)getc(fp) << 24; |
| 289 | } |
| 290 | else { |
| 291 | x = rs_byte(p); |
| 292 | x |= (long)rs_byte(p) << 8; |
| 293 | x |= (long)rs_byte(p) << 16; |
| 294 | x |= (long)rs_byte(p) << 24; |
| 295 | } |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 296 | /* XXX If your long is > 32 bits, add sign-extension here!!! */ |
| 297 | return x; |
| 298 | } |
| 299 | |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 300 | static object * |
| 301 | r_object(p) |
| 302 | RFILE *p; |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 303 | { |
Jack Jansen | 9513f2c | 1995-10-27 13:21:28 +0000 | [diff] [blame] | 304 | object *v, *v2; |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 305 | long i, n; |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 306 | int type = r_byte(p); |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 307 | |
| 308 | switch (type) { |
| 309 | |
| 310 | case EOF: |
Guido van Rossum | 87e7ea7 | 1991-12-10 14:00:03 +0000 | [diff] [blame] | 311 | err_setstr(EOFError, "EOF read where object expected"); |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 312 | return NULL; |
| 313 | |
| 314 | case TYPE_NULL: |
| 315 | return NULL; |
| 316 | |
| 317 | case TYPE_NONE: |
| 318 | INCREF(None); |
| 319 | return None; |
| 320 | |
| 321 | case TYPE_INT: |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 322 | return newintobject(r_long(p)); |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 323 | |
| 324 | case TYPE_LONG: |
| 325 | { |
| 326 | int size; |
| 327 | longobject *ob; |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 328 | n = r_long(p); |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 329 | size = n<0 ? -n : n; |
| 330 | ob = alloclongobject(size); |
| 331 | if (ob == NULL) |
| 332 | return NULL; |
| 333 | ob->ob_size = n; |
| 334 | for (i = 0; i < size; i++) |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 335 | ob->ob_digit[i] = r_short(p); |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 336 | return (object *)ob; |
| 337 | } |
| 338 | |
| 339 | case TYPE_FLOAT: |
| 340 | { |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 341 | extern double atof PROTO((const char *)); |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 342 | char buf[256]; |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 343 | n = r_byte(p); |
| 344 | if (r_string(buf, (int)n, p) != n) { |
Guido van Rossum | 87e7ea7 | 1991-12-10 14:00:03 +0000 | [diff] [blame] | 345 | err_setstr(EOFError, |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 346 | "EOF read where object expected"); |
| 347 | return NULL; |
| 348 | } |
| 349 | buf[n] = '\0'; |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 350 | return newfloatobject(atof(buf)); |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 351 | } |
| 352 | |
Guido van Rossum | 8a5c5d2 | 1996-01-12 01:09:56 +0000 | [diff] [blame] | 353 | #ifndef WITHOUT_COMPLEX |
| 354 | case TYPE_COMPLEX: |
| 355 | { |
| 356 | extern double atof PROTO((const char *)); |
| 357 | char buf[256]; |
| 358 | complex c; |
| 359 | n = r_byte(p); |
| 360 | if (r_string(buf, (int)n, p) != n) { |
| 361 | err_setstr(EOFError, |
| 362 | "EOF read where object expected"); |
| 363 | return NULL; |
| 364 | } |
| 365 | buf[n] = '\0'; |
| 366 | c.real = atof(buf); |
| 367 | n = r_byte(p); |
| 368 | if (r_string(buf, (int)n, p) != n) { |
| 369 | err_setstr(EOFError, |
| 370 | "EOF read where object expected"); |
| 371 | return NULL; |
| 372 | } |
| 373 | buf[n] = '\0'; |
| 374 | c.imag = atof(buf); |
| 375 | return newcomplexobject(c); |
| 376 | } |
| 377 | #endif |
| 378 | |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 379 | case TYPE_STRING: |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 380 | n = r_long(p); |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 381 | v = newsizedstringobject((char *)NULL, n); |
| 382 | if (v != NULL) { |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 383 | if (r_string(getstringvalue(v), (int)n, p) != n) { |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 384 | DECREF(v); |
| 385 | v = NULL; |
Guido van Rossum | 87e7ea7 | 1991-12-10 14:00:03 +0000 | [diff] [blame] | 386 | err_setstr(EOFError, |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 387 | "EOF read where object expected"); |
| 388 | } |
| 389 | } |
| 390 | return v; |
| 391 | |
| 392 | case TYPE_TUPLE: |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 393 | n = r_long(p); |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 394 | v = newtupleobject((int)n); |
| 395 | if (v == NULL) |
| 396 | return v; |
Jack Jansen | 9513f2c | 1995-10-27 13:21:28 +0000 | [diff] [blame] | 397 | for (i = 0; i < n; i++) { |
| 398 | v2 = r_object(p); |
| 399 | if ( v2 == NULL ) { |
| 400 | DECREF(v); |
| 401 | v = NULL; |
| 402 | break; |
| 403 | } |
| 404 | SETTUPLEITEM(v, (int)i, v2); |
| 405 | } |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 406 | return v; |
| 407 | |
| 408 | case TYPE_LIST: |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 409 | n = r_long(p); |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 410 | v = newlistobject((int)n); |
| 411 | if (v == NULL) |
| 412 | return v; |
Jack Jansen | 9513f2c | 1995-10-27 13:21:28 +0000 | [diff] [blame] | 413 | for (i = 0; i < n; i++) { |
| 414 | v2 = r_object(p); |
| 415 | if ( v2 == NULL ) { |
| 416 | DECREF(v); |
| 417 | v = NULL; |
| 418 | break; |
| 419 | } |
| 420 | setlistitem(v, (int)i, v2); |
| 421 | } |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 422 | return v; |
| 423 | |
Guido van Rossum | 64b4552 | 1991-06-07 13:58:22 +0000 | [diff] [blame] | 424 | case TYPE_DICT: |
| 425 | v = newdictobject(); |
| 426 | if (v == NULL) |
| 427 | return NULL; |
| 428 | for (;;) { |
| 429 | object *key, *val; |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 430 | key = r_object(p); |
Guido van Rossum | 64b4552 | 1991-06-07 13:58:22 +0000 | [diff] [blame] | 431 | if (key == NULL) |
Jack Jansen | 9513f2c | 1995-10-27 13:21:28 +0000 | [diff] [blame] | 432 | break; /* XXXX and how about memory errors? */ |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 433 | val = r_object(p); |
Jack Jansen | 9513f2c | 1995-10-27 13:21:28 +0000 | [diff] [blame] | 434 | /* XXXX error check? */ |
Guido van Rossum | 64b4552 | 1991-06-07 13:58:22 +0000 | [diff] [blame] | 435 | dict2insert(v, key, val); |
| 436 | DECREF(key); |
| 437 | XDECREF(val); |
| 438 | } |
| 439 | return v; |
| 440 | |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 441 | case TYPE_CODE: |
| 442 | { |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 443 | int argcount = r_short(p); |
| 444 | int nlocals = r_short(p); |
| 445 | int flags = r_short(p); |
Jack Jansen | 9513f2c | 1995-10-27 13:21:28 +0000 | [diff] [blame] | 446 | object *code = NULL; |
| 447 | object *consts = NULL; |
| 448 | object *names = NULL; |
| 449 | object *varnames = NULL; |
| 450 | object *filename = NULL; |
| 451 | object *name = NULL; |
| 452 | |
| 453 | code = r_object(p); |
| 454 | if (code) consts = r_object(p); |
| 455 | if (consts) names = r_object(p); |
| 456 | if (names) varnames = r_object(p); |
| 457 | if (varnames) filename = r_object(p); |
| 458 | if (filename) name = r_object(p); |
| 459 | |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 460 | if (!err_occurred()) { |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 461 | v = (object *) newcodeobject( |
| 462 | argcount, nlocals, flags, |
| 463 | code, consts, names, varnames, |
| 464 | filename, name); |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 465 | } |
| 466 | else |
| 467 | v = NULL; |
| 468 | XDECREF(code); |
| 469 | XDECREF(consts); |
| 470 | XDECREF(names); |
| 471 | XDECREF(filename); |
Guido van Rossum | a440310 | 1994-10-05 12:24:16 +0000 | [diff] [blame] | 472 | XDECREF(name); |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 473 | |
| 474 | } |
| 475 | return v; |
| 476 | |
| 477 | default: |
Guido van Rossum | 87e7ea7 | 1991-12-10 14:00:03 +0000 | [diff] [blame] | 478 | err_setstr(TypeError, "read unknown object"); |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 479 | return NULL; |
| 480 | |
| 481 | } |
| 482 | } |
| 483 | |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 484 | long |
| 485 | rd_long(fp) |
| 486 | FILE *fp; |
| 487 | { |
| 488 | RFILE rf; |
| 489 | rf.fp = fp; |
| 490 | return r_long(&rf); |
| 491 | } |
| 492 | |
| 493 | object * |
| 494 | rd_object(fp) |
| 495 | FILE *fp; |
| 496 | { |
| 497 | RFILE rf; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 498 | if (err_occurred()) { |
Guido van Rossum | 0587011 | 1995-08-28 02:56:20 +0000 | [diff] [blame] | 499 | fatal("XXX rd_object called with exception set"); /* tmp */ |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 500 | fprintf(stderr, "XXX rd_object called with exception set\n"); |
| 501 | return NULL; |
| 502 | } |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 503 | rf.fp = fp; |
| 504 | return r_object(&rf); |
| 505 | } |
| 506 | |
Guido van Rossum | f56e3db | 1993-04-01 20:59:32 +0000 | [diff] [blame] | 507 | object * |
| 508 | rds_object(str, len) |
| 509 | char *str; |
| 510 | int len; |
| 511 | { |
| 512 | RFILE rf; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 513 | if (err_occurred()) { |
| 514 | fprintf(stderr, "XXX rds_object called with exception set\n"); |
| 515 | return NULL; |
| 516 | } |
Guido van Rossum | f56e3db | 1993-04-01 20:59:32 +0000 | [diff] [blame] | 517 | rf.fp = NULL; |
| 518 | rf.str = NULL; |
| 519 | rf.ptr = str; |
| 520 | rf.end = str + len; |
| 521 | return r_object(&rf); |
| 522 | } |
| 523 | |
Guido van Rossum | 64b4552 | 1991-06-07 13:58:22 +0000 | [diff] [blame] | 524 | /* And an interface for Python programs... */ |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 525 | |
| 526 | static object * |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 527 | marshal_dump(self, args) |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 528 | object *self; |
| 529 | object *args; |
| 530 | { |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 531 | WFILE wf; |
| 532 | object *x; |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 533 | object *f; |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 534 | if (!getargs(args, "(OO)", &x, &f)) |
| 535 | return NULL; |
| 536 | if (!is_fileobject(f)) { |
| 537 | err_setstr(TypeError, "marshal.dump() 2nd arg must be file"); |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 538 | return NULL; |
| 539 | } |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 540 | wf.fp = getfilefile(f); |
| 541 | wf.str = NULL; |
| 542 | wf.ptr = wf.end = NULL; |
| 543 | w_object(x, &wf); |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 544 | INCREF(None); |
| 545 | return None; |
| 546 | } |
| 547 | |
| 548 | static object * |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 549 | marshal_load(self, args) |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 550 | object *self; |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 551 | object *args; |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 552 | { |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 553 | RFILE rf; |
| 554 | object *f; |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 555 | object *v; |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 556 | if (!getargs(args, "O", &f)) |
| 557 | return NULL; |
| 558 | if (!is_fileobject(f)) { |
| 559 | err_setstr(TypeError, "marshal.load() arg must be file"); |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 560 | return NULL; |
| 561 | } |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 562 | rf.fp = getfilefile(f); |
| 563 | rf.str = NULL; |
| 564 | rf.ptr = rf.end = NULL; |
| 565 | err_clear(); |
| 566 | v = r_object(&rf); |
| 567 | if (err_occurred()) { |
| 568 | XDECREF(v); |
| 569 | v = NULL; |
| 570 | } |
| 571 | return v; |
| 572 | } |
| 573 | |
| 574 | static object * |
| 575 | marshal_dumps(self, args) |
| 576 | object *self; |
| 577 | object *args; |
| 578 | { |
| 579 | WFILE wf; |
| 580 | object *x; |
| 581 | if (!getargs(args, "O", &x)) |
| 582 | return NULL; |
| 583 | wf.fp = NULL; |
| 584 | wf.str = newsizedstringobject((char *)NULL, 50); |
| 585 | if (wf.str == NULL) |
| 586 | return NULL; |
| 587 | wf.ptr = GETSTRINGVALUE((stringobject *)wf.str); |
| 588 | wf.end = wf.ptr + getstringsize(wf.str); |
| 589 | w_object(x, &wf); |
| 590 | if (wf.str != NULL) |
| 591 | resizestring(&wf.str, |
| 592 | (int) (wf.ptr - GETSTRINGVALUE((stringobject *)wf.str))); |
| 593 | return wf.str; |
| 594 | } |
| 595 | |
| 596 | static object * |
| 597 | marshal_loads(self, args) |
| 598 | object *self; |
| 599 | object *args; |
| 600 | { |
| 601 | RFILE rf; |
| 602 | object *v; |
| 603 | char *s; |
| 604 | int n; |
| 605 | if (!getargs(args, "s#", &s, &n)) |
| 606 | return NULL; |
| 607 | rf.fp = NULL; |
| 608 | rf.str = args; |
| 609 | rf.ptr = s; |
| 610 | rf.end = s + n; |
| 611 | err_clear(); |
| 612 | v = r_object(&rf); |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 613 | if (err_occurred()) { |
| 614 | XDECREF(v); |
| 615 | v = NULL; |
| 616 | } |
| 617 | return v; |
| 618 | } |
| 619 | |
| 620 | static struct methodlist marshal_methods[] = { |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 621 | {"dump", marshal_dump}, |
| 622 | {"load", marshal_load}, |
| 623 | {"dumps", marshal_dumps}, |
| 624 | {"loads", marshal_loads}, |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 625 | {NULL, NULL} /* sentinel */ |
| 626 | }; |
| 627 | |
| 628 | void |
| 629 | initmarshal() |
| 630 | { |
| 631 | (void) initmodule("marshal", marshal_methods); |
| 632 | } |