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 | |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 7 | Permission to use, copy, modify, and distribute this software and its |
| 8 | documentation for any purpose and without fee is hereby granted, |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 9 | provided that the above copyright notice appear in all copies and that |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 10 | both that copyright notice and this permission notice appear in |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 11 | supporting documentation, and that the names of Stichting Mathematisch |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 12 | Centrum or CWI or Corporation for National Research Initiatives or |
| 13 | CNRI not be used in advertising or publicity pertaining to |
| 14 | distribution of the software without specific, written prior |
| 15 | permission. |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 16 | |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 17 | While CWI is the initial source for this software, a modified version |
| 18 | is made available by the Corporation for National Research Initiatives |
| 19 | (CNRI) at the Internet address ftp://ftp.python.org. |
| 20 | |
| 21 | STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH |
| 22 | REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF |
| 23 | MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH |
| 24 | CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL |
| 25 | DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR |
| 26 | PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER |
| 27 | TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR |
| 28 | PERFORMANCE OF THIS SOFTWARE. |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 29 | |
| 30 | ******************************************************************/ |
| 31 | |
| 32 | /* Write Python objects to files and read them back. |
| 33 | This is intended for writing and reading compiled Python code only; |
| 34 | a true persistent storage facility would be much harder, since |
| 35 | it would have to take circular links and sharing into account. */ |
| 36 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 37 | #include "Python.h" |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 38 | #include "longintrepr.h" |
| 39 | #include "compile.h" |
| 40 | #include "marshal.h" |
| 41 | |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 42 | #define TYPE_NULL '0' |
| 43 | #define TYPE_NONE 'N' |
Guido van Rossum | e449af7 | 1996-10-11 16:25:41 +0000 | [diff] [blame] | 44 | #define TYPE_ELLIPSIS '.' |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 45 | #define TYPE_INT 'i' |
Guido van Rossum | b0c168c | 1996-12-05 23:15:02 +0000 | [diff] [blame] | 46 | #define TYPE_INT64 'I' |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 47 | #define TYPE_FLOAT 'f' |
Guido van Rossum | 8a5c5d2 | 1996-01-12 01:09:56 +0000 | [diff] [blame] | 48 | #define TYPE_COMPLEX 'x' |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 49 | #define TYPE_LONG 'l' |
| 50 | #define TYPE_STRING 's' |
| 51 | #define TYPE_TUPLE '(' |
| 52 | #define TYPE_LIST '[' |
| 53 | #define TYPE_DICT '{' |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 54 | #define TYPE_CODE 'c' |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 55 | #define TYPE_UNKNOWN '?' |
| 56 | |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 57 | typedef struct { |
| 58 | FILE *fp; |
Guido van Rossum | f215060 | 1996-06-26 20:41:23 +0000 | [diff] [blame] | 59 | int error; |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 60 | /* If fp == NULL, the following are valid: */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 61 | PyObject *str; |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 62 | char *ptr; |
| 63 | char *end; |
| 64 | } WFILE; |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 65 | |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 66 | #define w_byte(c, p) if (((p)->fp)) putc((c), (p)->fp); \ |
| 67 | else if ((p)->ptr != (p)->end) *(p)->ptr++ = (c); \ |
| 68 | else w_more(c, p) |
| 69 | |
| 70 | static void |
| 71 | w_more(c, p) |
| 72 | char c; |
| 73 | WFILE *p; |
| 74 | { |
| 75 | int size, newsize; |
| 76 | if (p->str == NULL) |
| 77 | return; /* An error already occurred */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 78 | size = PyString_Size(p->str); |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 79 | newsize = size + 1024; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 80 | if (_PyString_Resize(&p->str, newsize) != 0) { |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 81 | p->ptr = p->end = NULL; |
| 82 | } |
| 83 | else { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 84 | p->ptr = PyString_AS_STRING((PyStringObject *)p->str) + size; |
| 85 | p->end = |
| 86 | PyString_AS_STRING((PyStringObject *)p->str) + newsize; |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 87 | *p->ptr++ = c; |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | static void |
| 92 | w_string(s, n, p) |
| 93 | char *s; |
| 94 | int n; |
| 95 | WFILE *p; |
| 96 | { |
| 97 | if (p->fp != NULL) { |
| 98 | fwrite(s, 1, n, p->fp); |
| 99 | } |
| 100 | else { |
| 101 | while (--n >= 0) { |
| 102 | w_byte(*s, p); |
| 103 | s++; |
| 104 | } |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | static void |
| 109 | w_short(x, p) |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 110 | int x; |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 111 | WFILE *p; |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 112 | { |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 113 | w_byte( x & 0xff, p); |
| 114 | w_byte((x>> 8) & 0xff, p); |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 115 | } |
| 116 | |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 117 | static void |
| 118 | w_long(x, p) |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 119 | long x; |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 120 | WFILE *p; |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 121 | { |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 122 | w_byte((int)( x & 0xff), p); |
| 123 | w_byte((int)((x>> 8) & 0xff), p); |
| 124 | w_byte((int)((x>>16) & 0xff), p); |
| 125 | w_byte((int)((x>>24) & 0xff), p); |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 126 | } |
| 127 | |
Guido van Rossum | c1547d9 | 1996-12-10 15:39:04 +0000 | [diff] [blame] | 128 | #if SIZEOF_LONG > 4 |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 129 | static void |
Guido van Rossum | b0c168c | 1996-12-05 23:15:02 +0000 | [diff] [blame] | 130 | w_long64(x, p) |
| 131 | long x; |
| 132 | WFILE *p; |
| 133 | { |
| 134 | w_long(x, p); |
Guido van Rossum | c1547d9 | 1996-12-10 15:39:04 +0000 | [diff] [blame] | 135 | w_long(x>>32, p); |
Guido van Rossum | b0c168c | 1996-12-05 23:15:02 +0000 | [diff] [blame] | 136 | } |
Guido van Rossum | c1547d9 | 1996-12-10 15:39:04 +0000 | [diff] [blame] | 137 | #endif |
Guido van Rossum | b0c168c | 1996-12-05 23:15:02 +0000 | [diff] [blame] | 138 | |
| 139 | static void |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 140 | w_object(v, p) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 141 | PyObject *v; |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 142 | WFILE *p; |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 143 | { |
Guido van Rossum | 3a205f7 | 1995-02-17 15:10:07 +0000 | [diff] [blame] | 144 | int i, n; |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 145 | |
Guido van Rossum | 730806d | 1998-04-10 22:27:42 +0000 | [diff] [blame] | 146 | if (v == NULL) { |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 147 | w_byte(TYPE_NULL, p); |
Guido van Rossum | 730806d | 1998-04-10 22:27:42 +0000 | [diff] [blame] | 148 | } |
| 149 | else if (v == Py_None) { |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 150 | w_byte(TYPE_NONE, p); |
Guido van Rossum | 730806d | 1998-04-10 22:27:42 +0000 | [diff] [blame] | 151 | } |
| 152 | else if (v == Py_Ellipsis) { |
| 153 | w_byte(TYPE_ELLIPSIS, p); |
| 154 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 155 | else if (PyInt_Check(v)) { |
| 156 | long x = PyInt_AS_LONG((PyIntObject *)v); |
Guido van Rossum | c1547d9 | 1996-12-10 15:39:04 +0000 | [diff] [blame] | 157 | #if SIZEOF_LONG > 4 |
Guido van Rossum | b0c168c | 1996-12-05 23:15:02 +0000 | [diff] [blame] | 158 | long y = x>>31; |
| 159 | if (y && y != -1) { |
| 160 | w_byte(TYPE_INT64, p); |
| 161 | w_long64(x, p); |
| 162 | } |
Guido van Rossum | c1547d9 | 1996-12-10 15:39:04 +0000 | [diff] [blame] | 163 | else |
| 164 | #endif |
| 165 | { |
Guido van Rossum | b0c168c | 1996-12-05 23:15:02 +0000 | [diff] [blame] | 166 | w_byte(TYPE_INT, p); |
| 167 | w_long(x, p); |
| 168 | } |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 169 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 170 | else if (PyLong_Check(v)) { |
| 171 | PyLongObject *ob = (PyLongObject *)v; |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 172 | w_byte(TYPE_LONG, p); |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 173 | n = ob->ob_size; |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 174 | w_long((long)n, p); |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 175 | if (n < 0) |
| 176 | n = -n; |
| 177 | for (i = 0; i < n; i++) |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 178 | w_short(ob->ob_digit[i], p); |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 179 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 180 | else if (PyFloat_Check(v)) { |
| 181 | extern void PyFloat_AsString |
| 182 | Py_PROTO((char *, PyFloatObject *)); |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 183 | char buf[256]; /* Plenty to format any double */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 184 | PyFloat_AsString(buf, (PyFloatObject *)v); |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 185 | n = strlen(buf); |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 186 | w_byte(TYPE_FLOAT, p); |
Guido van Rossum | 3a205f7 | 1995-02-17 15:10:07 +0000 | [diff] [blame] | 187 | w_byte(n, p); |
| 188 | w_string(buf, n, p); |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 189 | } |
Guido van Rossum | 8a5c5d2 | 1996-01-12 01:09:56 +0000 | [diff] [blame] | 190 | #ifndef WITHOUT_COMPLEX |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 191 | else if (PyComplex_Check(v)) { |
| 192 | extern void PyFloat_AsString |
| 193 | Py_PROTO((char *, PyFloatObject *)); |
Guido van Rossum | 8a5c5d2 | 1996-01-12 01:09:56 +0000 | [diff] [blame] | 194 | char buf[256]; /* Plenty to format any double */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 195 | PyFloatObject *temp; |
Guido van Rossum | 8a5c5d2 | 1996-01-12 01:09:56 +0000 | [diff] [blame] | 196 | w_byte(TYPE_COMPLEX, p); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 197 | temp = (PyFloatObject*)PyFloat_FromDouble( |
| 198 | PyComplex_RealAsDouble(v)); |
| 199 | PyFloat_AsString(buf, temp); |
| 200 | Py_DECREF(temp); |
Guido van Rossum | 8a5c5d2 | 1996-01-12 01:09:56 +0000 | [diff] [blame] | 201 | n = strlen(buf); |
| 202 | w_byte(n, p); |
| 203 | w_string(buf, n, p); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 204 | temp = (PyFloatObject*)PyFloat_FromDouble( |
| 205 | PyComplex_ImagAsDouble(v)); |
| 206 | PyFloat_AsString(buf, temp); |
| 207 | Py_DECREF(temp); |
Guido van Rossum | 8a5c5d2 | 1996-01-12 01:09:56 +0000 | [diff] [blame] | 208 | n = strlen(buf); |
| 209 | w_byte(n, p); |
| 210 | w_string(buf, n, p); |
| 211 | } |
| 212 | #endif |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 213 | else if (PyString_Check(v)) { |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 214 | w_byte(TYPE_STRING, p); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 215 | n = PyString_Size(v); |
Guido van Rossum | 3a205f7 | 1995-02-17 15:10:07 +0000 | [diff] [blame] | 216 | w_long((long)n, p); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 217 | w_string(PyString_AsString(v), n, p); |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 218 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 219 | else if (PyTuple_Check(v)) { |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 220 | w_byte(TYPE_TUPLE, p); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 221 | n = PyTuple_Size(v); |
Guido van Rossum | 3a205f7 | 1995-02-17 15:10:07 +0000 | [diff] [blame] | 222 | w_long((long)n, p); |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 223 | for (i = 0; i < n; i++) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 224 | w_object(PyTuple_GET_ITEM(v, i), p); |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 225 | } |
| 226 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 227 | else if (PyList_Check(v)) { |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 228 | w_byte(TYPE_LIST, p); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 229 | n = PyList_Size(v); |
Guido van Rossum | 3a205f7 | 1995-02-17 15:10:07 +0000 | [diff] [blame] | 230 | w_long((long)n, p); |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 231 | for (i = 0; i < n; i++) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 232 | w_object(PyList_GetItem(v, i), p); |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 233 | } |
| 234 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 235 | else if (PyDict_Check(v)) { |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 236 | int pos; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 237 | PyObject *key, *value; |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 238 | w_byte(TYPE_DICT, p); |
Guido van Rossum | 64b4552 | 1991-06-07 13:58:22 +0000 | [diff] [blame] | 239 | /* This one is NULL object terminated! */ |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 240 | pos = 0; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 241 | while (PyDict_Next(v, &pos, &key, &value)) { |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 242 | w_object(key, p); |
| 243 | w_object(value, p); |
Guido van Rossum | 64b4552 | 1991-06-07 13:58:22 +0000 | [diff] [blame] | 244 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 245 | w_object((PyObject *)NULL, p); |
Guido van Rossum | 64b4552 | 1991-06-07 13:58:22 +0000 | [diff] [blame] | 246 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 247 | else if (PyCode_Check(v)) { |
| 248 | PyCodeObject *co = (PyCodeObject *)v; |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 249 | w_byte(TYPE_CODE, p); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 250 | w_short(co->co_argcount, p); |
| 251 | w_short(co->co_nlocals, p); |
Guido van Rossum | 98a9b31 | 1997-01-17 21:07:08 +0000 | [diff] [blame] | 252 | w_short(co->co_stacksize, p); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 253 | w_short(co->co_flags, p); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 254 | w_object((PyObject *)co->co_code, p); |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 255 | w_object(co->co_consts, p); |
| 256 | w_object(co->co_names, p); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 257 | w_object(co->co_varnames, p); |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 258 | w_object(co->co_filename, p); |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 259 | w_object(co->co_name, p); |
Guido van Rossum | d031c89 | 1997-01-24 03:44:17 +0000 | [diff] [blame] | 260 | w_short(co->co_firstlineno, p); |
| 261 | w_object(co->co_lnotab, p); |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 262 | } |
| 263 | else { |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 264 | w_byte(TYPE_UNKNOWN, p); |
Guido van Rossum | f215060 | 1996-06-26 20:41:23 +0000 | [diff] [blame] | 265 | p->error = 1; |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 266 | } |
| 267 | } |
| 268 | |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 269 | void |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 270 | PyMarshal_WriteLongToFile(x, fp) |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 271 | long x; |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 272 | FILE *fp; |
| 273 | { |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 274 | WFILE wf; |
| 275 | wf.fp = fp; |
Guido van Rossum | f215060 | 1996-06-26 20:41:23 +0000 | [diff] [blame] | 276 | wf.error = 0; |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 277 | w_long(x, &wf); |
| 278 | } |
| 279 | |
| 280 | void |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 281 | PyMarshal_WriteObjectToFile(x, fp) |
| 282 | PyObject *x; |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 283 | FILE *fp; |
| 284 | { |
| 285 | WFILE wf; |
| 286 | wf.fp = fp; |
Guido van Rossum | f215060 | 1996-06-26 20:41:23 +0000 | [diff] [blame] | 287 | wf.error = 0; |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 288 | w_object(x, &wf); |
| 289 | } |
| 290 | |
| 291 | typedef WFILE RFILE; /* Same struct with different invariants */ |
| 292 | |
Guido van Rossum | 8d617a6 | 1995-03-09 12:12:11 +0000 | [diff] [blame] | 293 | #define rs_byte(p) (((p)->ptr != (p)->end) ? (unsigned char)*(p)->ptr++ : EOF) |
| 294 | |
| 295 | #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] | 296 | |
| 297 | static int |
| 298 | r_string(s, n, p) |
| 299 | char *s; |
| 300 | int n; |
| 301 | RFILE *p; |
| 302 | { |
| 303 | if (p->fp != NULL) |
| 304 | return fread(s, 1, n, p->fp); |
| 305 | if (p->end - p->ptr < n) |
| 306 | n = p->end - p->ptr; |
| 307 | memcpy(s, p->ptr, n); |
| 308 | p->ptr += n; |
| 309 | return n; |
| 310 | } |
| 311 | |
| 312 | static int |
| 313 | r_short(p) |
| 314 | RFILE *p; |
| 315 | { |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 316 | register short x; |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 317 | x = r_byte(p); |
| 318 | x |= r_byte(p) << 8; |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 319 | /* XXX If your short is > 16 bits, add sign-extension here!!! */ |
| 320 | return x; |
| 321 | } |
| 322 | |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 323 | static long |
| 324 | r_long(p) |
| 325 | RFILE *p; |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 326 | { |
| 327 | register long x; |
Guido van Rossum | 8d617a6 | 1995-03-09 12:12:11 +0000 | [diff] [blame] | 328 | register FILE *fp = p->fp; |
| 329 | if (fp) { |
| 330 | x = getc(fp); |
| 331 | x |= (long)getc(fp) << 8; |
| 332 | x |= (long)getc(fp) << 16; |
| 333 | x |= (long)getc(fp) << 24; |
| 334 | } |
| 335 | else { |
| 336 | x = rs_byte(p); |
| 337 | x |= (long)rs_byte(p) << 8; |
| 338 | x |= (long)rs_byte(p) << 16; |
| 339 | x |= (long)rs_byte(p) << 24; |
| 340 | } |
Guido van Rossum | c1547d9 | 1996-12-10 15:39:04 +0000 | [diff] [blame] | 341 | #if SIZEOF_LONG > 4 |
Guido van Rossum | b0c168c | 1996-12-05 23:15:02 +0000 | [diff] [blame] | 342 | /* Sign extension for 64-bit machines */ |
| 343 | x <<= (8*sizeof(long) - 32); |
| 344 | x >>= (8*sizeof(long) - 32); |
Guido van Rossum | c1547d9 | 1996-12-10 15:39:04 +0000 | [diff] [blame] | 345 | #endif |
Guido van Rossum | b0c168c | 1996-12-05 23:15:02 +0000 | [diff] [blame] | 346 | return x; |
| 347 | } |
| 348 | |
| 349 | static long |
| 350 | r_long64(p) |
| 351 | RFILE *p; |
| 352 | { |
| 353 | register long x; |
Guido van Rossum | c1547d9 | 1996-12-10 15:39:04 +0000 | [diff] [blame] | 354 | x = r_long(p); |
| 355 | #if SIZEOF_LONG > 4 |
| 356 | x = (x & 0xFFFFFFFF) | (r_long(p) << 32); |
| 357 | #else |
| 358 | if (r_long(p) != 0) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 359 | PyObject *f = PySys_GetObject("stderr"); |
Guido van Rossum | c1547d9 | 1996-12-10 15:39:04 +0000 | [diff] [blame] | 360 | if (f != NULL) |
Guido van Rossum | 7e8d26d | 1997-05-22 22:35:47 +0000 | [diff] [blame] | 361 | (void) PyFile_WriteString( |
Guido van Rossum | c1547d9 | 1996-12-10 15:39:04 +0000 | [diff] [blame] | 362 | "Warning: un-marshal 64-bit int in 32-bit mode\n", |
| 363 | f); |
Guido van Rossum | 7e8d26d | 1997-05-22 22:35:47 +0000 | [diff] [blame] | 364 | PyErr_Clear(); |
Guido van Rossum | b0c168c | 1996-12-05 23:15:02 +0000 | [diff] [blame] | 365 | } |
Guido van Rossum | c1547d9 | 1996-12-10 15:39:04 +0000 | [diff] [blame] | 366 | #endif |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 367 | return x; |
| 368 | } |
| 369 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 370 | static PyObject * |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 371 | r_object(p) |
| 372 | RFILE *p; |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 373 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 374 | PyObject *v, *v2; |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 375 | long i, n; |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 376 | int type = r_byte(p); |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 377 | |
| 378 | switch (type) { |
| 379 | |
| 380 | case EOF: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 381 | PyErr_SetString(PyExc_EOFError, |
| 382 | "EOF read where object expected"); |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 383 | return NULL; |
| 384 | |
| 385 | case TYPE_NULL: |
| 386 | return NULL; |
| 387 | |
| 388 | case TYPE_NONE: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 389 | Py_INCREF(Py_None); |
| 390 | return Py_None; |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 391 | |
Guido van Rossum | e449af7 | 1996-10-11 16:25:41 +0000 | [diff] [blame] | 392 | case TYPE_ELLIPSIS: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 393 | Py_INCREF(Py_Ellipsis); |
Guido van Rossum | e449af7 | 1996-10-11 16:25:41 +0000 | [diff] [blame] | 394 | return Py_Ellipsis; |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 395 | |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 396 | case TYPE_INT: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 397 | return PyInt_FromLong(r_long(p)); |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 398 | |
Guido van Rossum | b0c168c | 1996-12-05 23:15:02 +0000 | [diff] [blame] | 399 | case TYPE_INT64: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 400 | return PyInt_FromLong(r_long64(p)); |
Guido van Rossum | b0c168c | 1996-12-05 23:15:02 +0000 | [diff] [blame] | 401 | |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 402 | case TYPE_LONG: |
| 403 | { |
| 404 | int size; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 405 | PyLongObject *ob; |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 406 | n = r_long(p); |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 407 | size = n<0 ? -n : n; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 408 | ob = _PyLong_New(size); |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 409 | if (ob == NULL) |
| 410 | return NULL; |
| 411 | ob->ob_size = n; |
| 412 | for (i = 0; i < size; i++) |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 413 | ob->ob_digit[i] = r_short(p); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 414 | return (PyObject *)ob; |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 415 | } |
| 416 | |
| 417 | case TYPE_FLOAT: |
| 418 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 419 | extern double atof Py_PROTO((const char *)); |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 420 | char buf[256]; |
Guido van Rossum | 0ae748d | 1997-02-14 22:58:07 +0000 | [diff] [blame] | 421 | double dx; |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 422 | n = r_byte(p); |
| 423 | if (r_string(buf, (int)n, p) != n) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 424 | PyErr_SetString(PyExc_EOFError, |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 425 | "EOF read where object expected"); |
| 426 | return NULL; |
| 427 | } |
| 428 | buf[n] = '\0'; |
Guido van Rossum | 0ae748d | 1997-02-14 22:58:07 +0000 | [diff] [blame] | 429 | PyFPE_START_PROTECT("atof", return 0) |
| 430 | dx = atof(buf); |
Guido van Rossum | 45b8391 | 1997-03-14 04:32:50 +0000 | [diff] [blame] | 431 | PyFPE_END_PROTECT(dx) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 432 | return PyFloat_FromDouble(dx); |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 433 | } |
| 434 | |
Guido van Rossum | 8a5c5d2 | 1996-01-12 01:09:56 +0000 | [diff] [blame] | 435 | #ifndef WITHOUT_COMPLEX |
| 436 | case TYPE_COMPLEX: |
| 437 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 438 | extern double atof Py_PROTO((const char *)); |
Guido van Rossum | 8a5c5d2 | 1996-01-12 01:09:56 +0000 | [diff] [blame] | 439 | char buf[256]; |
Guido van Rossum | 530956d | 1996-07-21 02:27:43 +0000 | [diff] [blame] | 440 | Py_complex c; |
Guido van Rossum | 8a5c5d2 | 1996-01-12 01:09:56 +0000 | [diff] [blame] | 441 | n = r_byte(p); |
| 442 | if (r_string(buf, (int)n, p) != n) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 443 | PyErr_SetString(PyExc_EOFError, |
Guido van Rossum | 8a5c5d2 | 1996-01-12 01:09:56 +0000 | [diff] [blame] | 444 | "EOF read where object expected"); |
| 445 | return NULL; |
| 446 | } |
| 447 | buf[n] = '\0'; |
Guido van Rossum | 0ae748d | 1997-02-14 22:58:07 +0000 | [diff] [blame] | 448 | PyFPE_START_PROTECT("atof", return 0) |
Guido van Rossum | 8a5c5d2 | 1996-01-12 01:09:56 +0000 | [diff] [blame] | 449 | c.real = atof(buf); |
Guido van Rossum | 45b8391 | 1997-03-14 04:32:50 +0000 | [diff] [blame] | 450 | PyFPE_END_PROTECT(c) |
Guido van Rossum | 8a5c5d2 | 1996-01-12 01:09:56 +0000 | [diff] [blame] | 451 | n = r_byte(p); |
| 452 | if (r_string(buf, (int)n, p) != n) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 453 | PyErr_SetString(PyExc_EOFError, |
Guido van Rossum | 8a5c5d2 | 1996-01-12 01:09:56 +0000 | [diff] [blame] | 454 | "EOF read where object expected"); |
| 455 | return NULL; |
| 456 | } |
| 457 | buf[n] = '\0'; |
Guido van Rossum | 0ae748d | 1997-02-14 22:58:07 +0000 | [diff] [blame] | 458 | PyFPE_START_PROTECT("atof", return 0) |
Guido van Rossum | 8a5c5d2 | 1996-01-12 01:09:56 +0000 | [diff] [blame] | 459 | c.imag = atof(buf); |
Guido van Rossum | 45b8391 | 1997-03-14 04:32:50 +0000 | [diff] [blame] | 460 | PyFPE_END_PROTECT(c) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 461 | return PyComplex_FromCComplex(c); |
Guido van Rossum | 8a5c5d2 | 1996-01-12 01:09:56 +0000 | [diff] [blame] | 462 | } |
| 463 | #endif |
| 464 | |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 465 | case TYPE_STRING: |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 466 | n = r_long(p); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 467 | v = PyString_FromStringAndSize((char *)NULL, n); |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 468 | if (v != NULL) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 469 | if (r_string(PyString_AsString(v), (int)n, p) != n) { |
| 470 | Py_DECREF(v); |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 471 | v = NULL; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 472 | PyErr_SetString(PyExc_EOFError, |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 473 | "EOF read where object expected"); |
| 474 | } |
| 475 | } |
| 476 | return v; |
| 477 | |
| 478 | case TYPE_TUPLE: |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 479 | n = r_long(p); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 480 | v = PyTuple_New((int)n); |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 481 | if (v == NULL) |
| 482 | return v; |
Jack Jansen | 9513f2c | 1995-10-27 13:21:28 +0000 | [diff] [blame] | 483 | for (i = 0; i < n; i++) { |
| 484 | v2 = r_object(p); |
| 485 | if ( v2 == NULL ) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 486 | Py_DECREF(v); |
Jack Jansen | 9513f2c | 1995-10-27 13:21:28 +0000 | [diff] [blame] | 487 | v = NULL; |
| 488 | break; |
| 489 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 490 | PyTuple_SET_ITEM(v, (int)i, v2); |
Jack Jansen | 9513f2c | 1995-10-27 13:21:28 +0000 | [diff] [blame] | 491 | } |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 492 | return v; |
| 493 | |
| 494 | case TYPE_LIST: |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 495 | n = r_long(p); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 496 | v = PyList_New((int)n); |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 497 | if (v == NULL) |
| 498 | return v; |
Jack Jansen | 9513f2c | 1995-10-27 13:21:28 +0000 | [diff] [blame] | 499 | for (i = 0; i < n; i++) { |
| 500 | v2 = r_object(p); |
| 501 | if ( v2 == NULL ) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 502 | Py_DECREF(v); |
Jack Jansen | 9513f2c | 1995-10-27 13:21:28 +0000 | [diff] [blame] | 503 | v = NULL; |
| 504 | break; |
| 505 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 506 | PyList_SetItem(v, (int)i, v2); |
Jack Jansen | 9513f2c | 1995-10-27 13:21:28 +0000 | [diff] [blame] | 507 | } |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 508 | return v; |
| 509 | |
Guido van Rossum | 64b4552 | 1991-06-07 13:58:22 +0000 | [diff] [blame] | 510 | case TYPE_DICT: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 511 | v = PyDict_New(); |
Guido van Rossum | 64b4552 | 1991-06-07 13:58:22 +0000 | [diff] [blame] | 512 | if (v == NULL) |
| 513 | return NULL; |
| 514 | for (;;) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 515 | PyObject *key, *val; |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 516 | key = r_object(p); |
Guido van Rossum | 64b4552 | 1991-06-07 13:58:22 +0000 | [diff] [blame] | 517 | if (key == NULL) |
Guido van Rossum | f215060 | 1996-06-26 20:41:23 +0000 | [diff] [blame] | 518 | break; /* XXX Assume TYPE_NULL, not an error */ |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 519 | val = r_object(p); |
Guido van Rossum | f215060 | 1996-06-26 20:41:23 +0000 | [diff] [blame] | 520 | if (val != NULL) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 521 | PyDict_SetItem(v, key, val); |
| 522 | Py_DECREF(key); |
| 523 | Py_XDECREF(val); |
Guido van Rossum | 64b4552 | 1991-06-07 13:58:22 +0000 | [diff] [blame] | 524 | } |
| 525 | return v; |
| 526 | |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 527 | case TYPE_CODE: |
| 528 | { |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 529 | int argcount = r_short(p); |
| 530 | int nlocals = r_short(p); |
Guido van Rossum | 98a9b31 | 1997-01-17 21:07:08 +0000 | [diff] [blame] | 531 | int stacksize = r_short(p); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 532 | int flags = r_short(p); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 533 | PyObject *code = NULL; |
| 534 | PyObject *consts = NULL; |
| 535 | PyObject *names = NULL; |
| 536 | PyObject *varnames = NULL; |
| 537 | PyObject *filename = NULL; |
| 538 | PyObject *name = NULL; |
Guido van Rossum | 0f4bbd2 | 1997-02-14 21:12:56 +0000 | [diff] [blame] | 539 | int firstlineno = 0; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 540 | PyObject *lnotab = NULL; |
Jack Jansen | 9513f2c | 1995-10-27 13:21:28 +0000 | [diff] [blame] | 541 | |
| 542 | code = r_object(p); |
| 543 | if (code) consts = r_object(p); |
| 544 | if (consts) names = r_object(p); |
| 545 | if (names) varnames = r_object(p); |
| 546 | if (varnames) filename = r_object(p); |
| 547 | if (filename) name = r_object(p); |
Guido van Rossum | d031c89 | 1997-01-24 03:44:17 +0000 | [diff] [blame] | 548 | if (name) { |
| 549 | firstlineno = r_short(p); |
| 550 | lnotab = r_object(p); |
| 551 | } |
Jack Jansen | 9513f2c | 1995-10-27 13:21:28 +0000 | [diff] [blame] | 552 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 553 | if (!PyErr_Occurred()) { |
| 554 | v = (PyObject *) PyCode_New( |
Guido van Rossum | 98a9b31 | 1997-01-17 21:07:08 +0000 | [diff] [blame] | 555 | argcount, nlocals, stacksize, flags, |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 556 | code, consts, names, varnames, |
Guido van Rossum | d031c89 | 1997-01-24 03:44:17 +0000 | [diff] [blame] | 557 | filename, name, firstlineno, lnotab); |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 558 | } |
| 559 | else |
| 560 | v = NULL; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 561 | Py_XDECREF(code); |
| 562 | Py_XDECREF(consts); |
| 563 | Py_XDECREF(names); |
| 564 | Py_XDECREF(varnames); |
| 565 | Py_XDECREF(filename); |
| 566 | Py_XDECREF(name); |
Guido van Rossum | 6fc06e7 | 1997-07-26 23:30:18 +0000 | [diff] [blame] | 567 | Py_XDECREF(lnotab); |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 568 | |
| 569 | } |
| 570 | return v; |
| 571 | |
| 572 | default: |
Guido van Rossum | f215060 | 1996-06-26 20:41:23 +0000 | [diff] [blame] | 573 | /* Bogus data got written, which isn't ideal. |
| 574 | This will let you keep working and recover. */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 575 | Py_INCREF(Py_None); |
| 576 | return Py_None; |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 577 | |
| 578 | } |
| 579 | } |
| 580 | |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 581 | long |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 582 | PyMarshal_ReadLongFromFile(fp) |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 583 | FILE *fp; |
| 584 | { |
| 585 | RFILE rf; |
| 586 | rf.fp = fp; |
| 587 | return r_long(&rf); |
| 588 | } |
| 589 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 590 | PyObject * |
| 591 | PyMarshal_ReadObjectFromFile(fp) |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 592 | FILE *fp; |
| 593 | { |
| 594 | RFILE rf; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 595 | if (PyErr_Occurred()) { |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 596 | fprintf(stderr, "XXX rd_object called with exception set\n"); |
| 597 | return NULL; |
| 598 | } |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 599 | rf.fp = fp; |
| 600 | return r_object(&rf); |
| 601 | } |
| 602 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 603 | PyObject * |
| 604 | PyMarshal_ReadObjectFromString(str, len) |
Guido van Rossum | f56e3db | 1993-04-01 20:59:32 +0000 | [diff] [blame] | 605 | char *str; |
| 606 | int len; |
| 607 | { |
| 608 | RFILE rf; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 609 | if (PyErr_Occurred()) { |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 610 | fprintf(stderr, "XXX rds_object called with exception set\n"); |
| 611 | return NULL; |
| 612 | } |
Guido van Rossum | f56e3db | 1993-04-01 20:59:32 +0000 | [diff] [blame] | 613 | rf.fp = NULL; |
| 614 | rf.str = NULL; |
| 615 | rf.ptr = str; |
| 616 | rf.end = str + len; |
| 617 | return r_object(&rf); |
| 618 | } |
| 619 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 620 | PyObject * |
Guido van Rossum | 3f3bb3d | 1996-08-19 22:07:17 +0000 | [diff] [blame] | 621 | PyMarshal_WriteObjectToString(x) /* wrs_object() */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 622 | PyObject *x; |
Guido van Rossum | 3f3bb3d | 1996-08-19 22:07:17 +0000 | [diff] [blame] | 623 | { |
| 624 | WFILE wf; |
| 625 | wf.fp = NULL; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 626 | wf.str = PyString_FromStringAndSize((char *)NULL, 50); |
Guido van Rossum | 3f3bb3d | 1996-08-19 22:07:17 +0000 | [diff] [blame] | 627 | if (wf.str == NULL) |
| 628 | return NULL; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 629 | wf.ptr = PyString_AS_STRING((PyStringObject *)wf.str); |
| 630 | wf.end = wf.ptr + PyString_Size(wf.str); |
Guido van Rossum | 3f3bb3d | 1996-08-19 22:07:17 +0000 | [diff] [blame] | 631 | wf.error = 0; |
| 632 | w_object(x, &wf); |
| 633 | if (wf.str != NULL) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 634 | _PyString_Resize(&wf.str, |
| 635 | (int) (wf.ptr - |
| 636 | PyString_AS_STRING((PyStringObject *)wf.str))); |
Guido van Rossum | 3f3bb3d | 1996-08-19 22:07:17 +0000 | [diff] [blame] | 637 | if (wf.error) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 638 | Py_XDECREF(wf.str); |
| 639 | PyErr_SetString(PyExc_ValueError, "unmarshallable object"); |
Guido van Rossum | 3f3bb3d | 1996-08-19 22:07:17 +0000 | [diff] [blame] | 640 | return NULL; |
| 641 | } |
| 642 | return wf.str; |
| 643 | } |
| 644 | |
Guido van Rossum | 64b4552 | 1991-06-07 13:58:22 +0000 | [diff] [blame] | 645 | /* And an interface for Python programs... */ |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 646 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 647 | static PyObject * |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 648 | marshal_dump(self, args) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 649 | PyObject *self; |
| 650 | PyObject *args; |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 651 | { |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 652 | WFILE wf; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 653 | PyObject *x; |
| 654 | PyObject *f; |
| 655 | if (!PyArg_Parse(args, "(OO)", &x, &f)) |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 656 | return NULL; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 657 | if (!PyFile_Check(f)) { |
| 658 | PyErr_SetString(PyExc_TypeError, |
| 659 | "marshal.dump() 2nd arg must be file"); |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 660 | return NULL; |
| 661 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 662 | wf.fp = PyFile_AsFile(f); |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 663 | wf.str = NULL; |
| 664 | wf.ptr = wf.end = NULL; |
Guido van Rossum | f215060 | 1996-06-26 20:41:23 +0000 | [diff] [blame] | 665 | wf.error = 0; |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 666 | w_object(x, &wf); |
Guido van Rossum | f215060 | 1996-06-26 20:41:23 +0000 | [diff] [blame] | 667 | if (wf.error) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 668 | PyErr_SetString(PyExc_ValueError, "unmarshallable object"); |
Guido van Rossum | f215060 | 1996-06-26 20:41:23 +0000 | [diff] [blame] | 669 | return NULL; |
| 670 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 671 | Py_INCREF(Py_None); |
| 672 | return Py_None; |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 673 | } |
| 674 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 675 | static PyObject * |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 676 | marshal_load(self, args) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 677 | PyObject *self; |
| 678 | PyObject *args; |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 679 | { |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 680 | RFILE rf; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 681 | PyObject *f; |
| 682 | PyObject *v; |
| 683 | if (!PyArg_Parse(args, "O", &f)) |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 684 | return NULL; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 685 | if (!PyFile_Check(f)) { |
| 686 | PyErr_SetString(PyExc_TypeError, |
| 687 | "marshal.load() arg must be file"); |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 688 | return NULL; |
| 689 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 690 | rf.fp = PyFile_AsFile(f); |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 691 | rf.str = NULL; |
| 692 | rf.ptr = rf.end = NULL; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 693 | PyErr_Clear(); |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 694 | v = r_object(&rf); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 695 | if (PyErr_Occurred()) { |
| 696 | Py_XDECREF(v); |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 697 | v = NULL; |
| 698 | } |
| 699 | return v; |
| 700 | } |
| 701 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 702 | static PyObject * |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 703 | marshal_dumps(self, args) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 704 | PyObject *self; |
| 705 | PyObject *args; |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 706 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 707 | PyObject *x; |
| 708 | if (!PyArg_Parse(args, "O", &x)) |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 709 | return NULL; |
Guido van Rossum | 3f3bb3d | 1996-08-19 22:07:17 +0000 | [diff] [blame] | 710 | return PyMarshal_WriteObjectToString(x); |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 711 | } |
| 712 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 713 | static PyObject * |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 714 | marshal_loads(self, args) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 715 | PyObject *self; |
| 716 | PyObject *args; |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 717 | { |
| 718 | RFILE rf; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 719 | PyObject *v; |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 720 | char *s; |
| 721 | int n; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 722 | if (!PyArg_Parse(args, "s#", &s, &n)) |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 723 | return NULL; |
| 724 | rf.fp = NULL; |
| 725 | rf.str = args; |
| 726 | rf.ptr = s; |
| 727 | rf.end = s + n; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 728 | PyErr_Clear(); |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 729 | v = r_object(&rf); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 730 | if (PyErr_Occurred()) { |
| 731 | Py_XDECREF(v); |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 732 | v = NULL; |
| 733 | } |
| 734 | return v; |
| 735 | } |
| 736 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 737 | static PyMethodDef marshal_methods[] = { |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 738 | {"dump", marshal_dump}, |
| 739 | {"load", marshal_load}, |
| 740 | {"dumps", marshal_dumps}, |
| 741 | {"loads", marshal_loads}, |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 742 | {NULL, NULL} /* sentinel */ |
| 743 | }; |
| 744 | |
| 745 | void |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 746 | PyMarshal_Init() |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 747 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 748 | (void) Py_InitModule("marshal", marshal_methods); |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 749 | } |