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