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 | b0c168c | 1996-12-05 23:15:02 +0000 | [diff] [blame] | 364 | } |
Guido van Rossum | c1547d9 | 1996-12-10 15:39:04 +0000 | [diff] [blame] | 365 | #endif |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 366 | return x; |
| 367 | } |
| 368 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 369 | static PyObject * |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 370 | r_object(p) |
| 371 | RFILE *p; |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 372 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 373 | PyObject *v, *v2; |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 374 | long i, n; |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 375 | int type = r_byte(p); |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 376 | |
| 377 | switch (type) { |
| 378 | |
| 379 | case EOF: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 380 | PyErr_SetString(PyExc_EOFError, |
| 381 | "EOF read where object expected"); |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 382 | return NULL; |
| 383 | |
| 384 | case TYPE_NULL: |
| 385 | return NULL; |
| 386 | |
| 387 | case TYPE_NONE: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 388 | Py_INCREF(Py_None); |
| 389 | return Py_None; |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 390 | |
Guido van Rossum | e449af7 | 1996-10-11 16:25:41 +0000 | [diff] [blame] | 391 | case TYPE_ELLIPSIS: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 392 | Py_INCREF(Py_Ellipsis); |
Guido van Rossum | e449af7 | 1996-10-11 16:25:41 +0000 | [diff] [blame] | 393 | return Py_Ellipsis; |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 394 | |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 395 | case TYPE_INT: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 396 | return PyInt_FromLong(r_long(p)); |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 397 | |
Guido van Rossum | b0c168c | 1996-12-05 23:15:02 +0000 | [diff] [blame] | 398 | case TYPE_INT64: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 399 | return PyInt_FromLong(r_long64(p)); |
Guido van Rossum | b0c168c | 1996-12-05 23:15:02 +0000 | [diff] [blame] | 400 | |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 401 | case TYPE_LONG: |
| 402 | { |
| 403 | int size; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 404 | PyLongObject *ob; |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 405 | n = r_long(p); |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 406 | size = n<0 ? -n : n; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 407 | ob = _PyLong_New(size); |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 408 | if (ob == NULL) |
| 409 | return NULL; |
| 410 | ob->ob_size = n; |
| 411 | for (i = 0; i < size; i++) |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 412 | ob->ob_digit[i] = r_short(p); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 413 | return (PyObject *)ob; |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 414 | } |
| 415 | |
| 416 | case TYPE_FLOAT: |
| 417 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 418 | extern double atof Py_PROTO((const char *)); |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 419 | char buf[256]; |
Guido van Rossum | 0ae748d | 1997-02-14 22:58:07 +0000 | [diff] [blame] | 420 | double dx; |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 421 | n = r_byte(p); |
| 422 | if (r_string(buf, (int)n, p) != n) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 423 | PyErr_SetString(PyExc_EOFError, |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 424 | "EOF read where object expected"); |
| 425 | return NULL; |
| 426 | } |
| 427 | buf[n] = '\0'; |
Guido van Rossum | 0ae748d | 1997-02-14 22:58:07 +0000 | [diff] [blame] | 428 | PyFPE_START_PROTECT("atof", return 0) |
| 429 | dx = atof(buf); |
Guido van Rossum | 45b8391 | 1997-03-14 04:32:50 +0000 | [diff] [blame] | 430 | PyFPE_END_PROTECT(dx) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 431 | return PyFloat_FromDouble(dx); |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 432 | } |
| 433 | |
Guido van Rossum | 8a5c5d2 | 1996-01-12 01:09:56 +0000 | [diff] [blame] | 434 | #ifndef WITHOUT_COMPLEX |
| 435 | case TYPE_COMPLEX: |
| 436 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 437 | extern double atof Py_PROTO((const char *)); |
Guido van Rossum | 8a5c5d2 | 1996-01-12 01:09:56 +0000 | [diff] [blame] | 438 | char buf[256]; |
Guido van Rossum | 530956d | 1996-07-21 02:27:43 +0000 | [diff] [blame] | 439 | Py_complex c; |
Guido van Rossum | 8a5c5d2 | 1996-01-12 01:09:56 +0000 | [diff] [blame] | 440 | n = r_byte(p); |
| 441 | if (r_string(buf, (int)n, p) != n) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 442 | PyErr_SetString(PyExc_EOFError, |
Guido van Rossum | 8a5c5d2 | 1996-01-12 01:09:56 +0000 | [diff] [blame] | 443 | "EOF read where object expected"); |
| 444 | return NULL; |
| 445 | } |
| 446 | buf[n] = '\0'; |
Guido van Rossum | 0ae748d | 1997-02-14 22:58:07 +0000 | [diff] [blame] | 447 | PyFPE_START_PROTECT("atof", return 0) |
Guido van Rossum | 8a5c5d2 | 1996-01-12 01:09:56 +0000 | [diff] [blame] | 448 | c.real = atof(buf); |
Guido van Rossum | 45b8391 | 1997-03-14 04:32:50 +0000 | [diff] [blame] | 449 | PyFPE_END_PROTECT(c) |
Guido van Rossum | 8a5c5d2 | 1996-01-12 01:09:56 +0000 | [diff] [blame] | 450 | n = r_byte(p); |
| 451 | if (r_string(buf, (int)n, p) != n) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 452 | PyErr_SetString(PyExc_EOFError, |
Guido van Rossum | 8a5c5d2 | 1996-01-12 01:09:56 +0000 | [diff] [blame] | 453 | "EOF read where object expected"); |
| 454 | return NULL; |
| 455 | } |
| 456 | buf[n] = '\0'; |
Guido van Rossum | 0ae748d | 1997-02-14 22:58:07 +0000 | [diff] [blame] | 457 | PyFPE_START_PROTECT("atof", return 0) |
Guido van Rossum | 8a5c5d2 | 1996-01-12 01:09:56 +0000 | [diff] [blame] | 458 | c.imag = atof(buf); |
Guido van Rossum | 45b8391 | 1997-03-14 04:32:50 +0000 | [diff] [blame] | 459 | PyFPE_END_PROTECT(c) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 460 | return PyComplex_FromCComplex(c); |
Guido van Rossum | 8a5c5d2 | 1996-01-12 01:09:56 +0000 | [diff] [blame] | 461 | } |
| 462 | #endif |
| 463 | |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 464 | case TYPE_STRING: |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 465 | n = r_long(p); |
Guido van Rossum | a45cb45 | 1998-06-08 20:27:29 +0000 | [diff] [blame] | 466 | if (n < 0) { |
| 467 | PyErr_SetString(PyExc_ValueError, "bad marshal data"); |
| 468 | return NULL; |
| 469 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 470 | v = PyString_FromStringAndSize((char *)NULL, n); |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 471 | if (v != NULL) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 472 | if (r_string(PyString_AsString(v), (int)n, p) != n) { |
| 473 | Py_DECREF(v); |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 474 | v = NULL; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 475 | PyErr_SetString(PyExc_EOFError, |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 476 | "EOF read where object expected"); |
| 477 | } |
| 478 | } |
| 479 | return v; |
| 480 | |
| 481 | case TYPE_TUPLE: |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 482 | n = r_long(p); |
Guido van Rossum | a45cb45 | 1998-06-08 20:27:29 +0000 | [diff] [blame] | 483 | if (n < 0) { |
| 484 | PyErr_SetString(PyExc_ValueError, "bad marshal data"); |
| 485 | return NULL; |
| 486 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 487 | v = PyTuple_New((int)n); |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 488 | if (v == NULL) |
| 489 | return v; |
Jack Jansen | 9513f2c | 1995-10-27 13:21:28 +0000 | [diff] [blame] | 490 | for (i = 0; i < n; i++) { |
| 491 | v2 = r_object(p); |
| 492 | if ( v2 == NULL ) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 493 | Py_DECREF(v); |
Jack Jansen | 9513f2c | 1995-10-27 13:21:28 +0000 | [diff] [blame] | 494 | v = NULL; |
| 495 | break; |
| 496 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 497 | PyTuple_SET_ITEM(v, (int)i, v2); |
Jack Jansen | 9513f2c | 1995-10-27 13:21:28 +0000 | [diff] [blame] | 498 | } |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 499 | return v; |
| 500 | |
| 501 | case TYPE_LIST: |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 502 | n = r_long(p); |
Guido van Rossum | a45cb45 | 1998-06-08 20:27:29 +0000 | [diff] [blame] | 503 | if (n < 0) { |
| 504 | PyErr_SetString(PyExc_ValueError, "bad marshal data"); |
| 505 | return NULL; |
| 506 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 507 | v = PyList_New((int)n); |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 508 | if (v == NULL) |
| 509 | return v; |
Jack Jansen | 9513f2c | 1995-10-27 13:21:28 +0000 | [diff] [blame] | 510 | for (i = 0; i < n; i++) { |
| 511 | v2 = r_object(p); |
| 512 | if ( v2 == NULL ) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 513 | Py_DECREF(v); |
Jack Jansen | 9513f2c | 1995-10-27 13:21:28 +0000 | [diff] [blame] | 514 | v = NULL; |
| 515 | break; |
| 516 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 517 | PyList_SetItem(v, (int)i, v2); |
Jack Jansen | 9513f2c | 1995-10-27 13:21:28 +0000 | [diff] [blame] | 518 | } |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 519 | return v; |
| 520 | |
Guido van Rossum | 64b4552 | 1991-06-07 13:58:22 +0000 | [diff] [blame] | 521 | case TYPE_DICT: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 522 | v = PyDict_New(); |
Guido van Rossum | 64b4552 | 1991-06-07 13:58:22 +0000 | [diff] [blame] | 523 | if (v == NULL) |
| 524 | return NULL; |
| 525 | for (;;) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 526 | PyObject *key, *val; |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 527 | key = r_object(p); |
Guido van Rossum | 64b4552 | 1991-06-07 13:58:22 +0000 | [diff] [blame] | 528 | if (key == NULL) |
Guido van Rossum | f215060 | 1996-06-26 20:41:23 +0000 | [diff] [blame] | 529 | break; /* XXX Assume TYPE_NULL, not an error */ |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 530 | val = r_object(p); |
Guido van Rossum | f215060 | 1996-06-26 20:41:23 +0000 | [diff] [blame] | 531 | if (val != NULL) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 532 | PyDict_SetItem(v, key, val); |
| 533 | Py_DECREF(key); |
| 534 | Py_XDECREF(val); |
Guido van Rossum | 64b4552 | 1991-06-07 13:58:22 +0000 | [diff] [blame] | 535 | } |
| 536 | return v; |
| 537 | |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 538 | case TYPE_CODE: |
| 539 | { |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 540 | int argcount = r_short(p); |
| 541 | int nlocals = r_short(p); |
Guido van Rossum | 98a9b31 | 1997-01-17 21:07:08 +0000 | [diff] [blame] | 542 | int stacksize = r_short(p); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 543 | int flags = r_short(p); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 544 | PyObject *code = NULL; |
| 545 | PyObject *consts = NULL; |
| 546 | PyObject *names = NULL; |
| 547 | PyObject *varnames = NULL; |
| 548 | PyObject *filename = NULL; |
| 549 | PyObject *name = NULL; |
Guido van Rossum | 0f4bbd2 | 1997-02-14 21:12:56 +0000 | [diff] [blame] | 550 | int firstlineno = 0; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 551 | PyObject *lnotab = NULL; |
Jack Jansen | 9513f2c | 1995-10-27 13:21:28 +0000 | [diff] [blame] | 552 | |
| 553 | code = r_object(p); |
| 554 | if (code) consts = r_object(p); |
| 555 | if (consts) names = r_object(p); |
| 556 | if (names) varnames = r_object(p); |
| 557 | if (varnames) filename = r_object(p); |
| 558 | if (filename) name = r_object(p); |
Guido van Rossum | d031c89 | 1997-01-24 03:44:17 +0000 | [diff] [blame] | 559 | if (name) { |
| 560 | firstlineno = r_short(p); |
| 561 | lnotab = r_object(p); |
| 562 | } |
Jack Jansen | 9513f2c | 1995-10-27 13:21:28 +0000 | [diff] [blame] | 563 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 564 | if (!PyErr_Occurred()) { |
| 565 | v = (PyObject *) PyCode_New( |
Guido van Rossum | 98a9b31 | 1997-01-17 21:07:08 +0000 | [diff] [blame] | 566 | argcount, nlocals, stacksize, flags, |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 567 | code, consts, names, varnames, |
Guido van Rossum | d031c89 | 1997-01-24 03:44:17 +0000 | [diff] [blame] | 568 | filename, name, firstlineno, lnotab); |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 569 | } |
| 570 | else |
| 571 | v = NULL; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 572 | Py_XDECREF(code); |
| 573 | Py_XDECREF(consts); |
| 574 | Py_XDECREF(names); |
| 575 | Py_XDECREF(varnames); |
| 576 | Py_XDECREF(filename); |
| 577 | Py_XDECREF(name); |
Guido van Rossum | 6fc06e7 | 1997-07-26 23:30:18 +0000 | [diff] [blame] | 578 | Py_XDECREF(lnotab); |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 579 | |
| 580 | } |
| 581 | return v; |
| 582 | |
| 583 | default: |
Guido van Rossum | f215060 | 1996-06-26 20:41:23 +0000 | [diff] [blame] | 584 | /* Bogus data got written, which isn't ideal. |
| 585 | This will let you keep working and recover. */ |
Guido van Rossum | a45cb45 | 1998-06-08 20:27:29 +0000 | [diff] [blame] | 586 | PyErr_SetString(PyExc_ValueError, "bad marshal data"); |
| 587 | return NULL; |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 588 | |
| 589 | } |
| 590 | } |
| 591 | |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 592 | long |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 593 | PyMarshal_ReadLongFromFile(fp) |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 594 | FILE *fp; |
| 595 | { |
| 596 | RFILE rf; |
| 597 | rf.fp = fp; |
| 598 | return r_long(&rf); |
| 599 | } |
| 600 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 601 | PyObject * |
| 602 | PyMarshal_ReadObjectFromFile(fp) |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 603 | FILE *fp; |
| 604 | { |
| 605 | RFILE rf; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 606 | if (PyErr_Occurred()) { |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 607 | fprintf(stderr, "XXX rd_object called with exception set\n"); |
| 608 | return NULL; |
| 609 | } |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 610 | rf.fp = fp; |
| 611 | return r_object(&rf); |
| 612 | } |
| 613 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 614 | PyObject * |
| 615 | PyMarshal_ReadObjectFromString(str, len) |
Guido van Rossum | f56e3db | 1993-04-01 20:59:32 +0000 | [diff] [blame] | 616 | char *str; |
| 617 | int len; |
| 618 | { |
| 619 | RFILE rf; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 620 | if (PyErr_Occurred()) { |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 621 | fprintf(stderr, "XXX rds_object called with exception set\n"); |
| 622 | return NULL; |
| 623 | } |
Guido van Rossum | f56e3db | 1993-04-01 20:59:32 +0000 | [diff] [blame] | 624 | rf.fp = NULL; |
| 625 | rf.str = NULL; |
| 626 | rf.ptr = str; |
| 627 | rf.end = str + len; |
| 628 | return r_object(&rf); |
| 629 | } |
| 630 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 631 | PyObject * |
Guido van Rossum | 3f3bb3d | 1996-08-19 22:07:17 +0000 | [diff] [blame] | 632 | PyMarshal_WriteObjectToString(x) /* wrs_object() */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 633 | PyObject *x; |
Guido van Rossum | 3f3bb3d | 1996-08-19 22:07:17 +0000 | [diff] [blame] | 634 | { |
| 635 | WFILE wf; |
| 636 | wf.fp = NULL; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 637 | wf.str = PyString_FromStringAndSize((char *)NULL, 50); |
Guido van Rossum | 3f3bb3d | 1996-08-19 22:07:17 +0000 | [diff] [blame] | 638 | if (wf.str == NULL) |
| 639 | return NULL; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 640 | wf.ptr = PyString_AS_STRING((PyStringObject *)wf.str); |
| 641 | wf.end = wf.ptr + PyString_Size(wf.str); |
Guido van Rossum | 3f3bb3d | 1996-08-19 22:07:17 +0000 | [diff] [blame] | 642 | wf.error = 0; |
| 643 | w_object(x, &wf); |
| 644 | if (wf.str != NULL) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 645 | _PyString_Resize(&wf.str, |
| 646 | (int) (wf.ptr - |
| 647 | PyString_AS_STRING((PyStringObject *)wf.str))); |
Guido van Rossum | 3f3bb3d | 1996-08-19 22:07:17 +0000 | [diff] [blame] | 648 | if (wf.error) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 649 | Py_XDECREF(wf.str); |
| 650 | PyErr_SetString(PyExc_ValueError, "unmarshallable object"); |
Guido van Rossum | 3f3bb3d | 1996-08-19 22:07:17 +0000 | [diff] [blame] | 651 | return NULL; |
| 652 | } |
| 653 | return wf.str; |
| 654 | } |
| 655 | |
Guido van Rossum | 64b4552 | 1991-06-07 13:58:22 +0000 | [diff] [blame] | 656 | /* And an interface for Python programs... */ |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 657 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 658 | static PyObject * |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 659 | marshal_dump(self, args) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 660 | PyObject *self; |
| 661 | PyObject *args; |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 662 | { |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 663 | WFILE wf; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 664 | PyObject *x; |
| 665 | PyObject *f; |
| 666 | if (!PyArg_Parse(args, "(OO)", &x, &f)) |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 667 | return NULL; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 668 | if (!PyFile_Check(f)) { |
| 669 | PyErr_SetString(PyExc_TypeError, |
| 670 | "marshal.dump() 2nd arg must be file"); |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 671 | return NULL; |
| 672 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 673 | wf.fp = PyFile_AsFile(f); |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 674 | wf.str = NULL; |
| 675 | wf.ptr = wf.end = NULL; |
Guido van Rossum | f215060 | 1996-06-26 20:41:23 +0000 | [diff] [blame] | 676 | wf.error = 0; |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 677 | w_object(x, &wf); |
Guido van Rossum | f215060 | 1996-06-26 20:41:23 +0000 | [diff] [blame] | 678 | if (wf.error) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 679 | PyErr_SetString(PyExc_ValueError, "unmarshallable object"); |
Guido van Rossum | f215060 | 1996-06-26 20:41:23 +0000 | [diff] [blame] | 680 | return NULL; |
| 681 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 682 | Py_INCREF(Py_None); |
| 683 | return Py_None; |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 684 | } |
| 685 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 686 | static PyObject * |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 687 | marshal_load(self, args) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 688 | PyObject *self; |
| 689 | PyObject *args; |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 690 | { |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 691 | RFILE rf; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 692 | PyObject *f; |
| 693 | PyObject *v; |
| 694 | if (!PyArg_Parse(args, "O", &f)) |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 695 | return NULL; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 696 | if (!PyFile_Check(f)) { |
| 697 | PyErr_SetString(PyExc_TypeError, |
| 698 | "marshal.load() arg must be file"); |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 699 | return NULL; |
| 700 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 701 | rf.fp = PyFile_AsFile(f); |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 702 | rf.str = NULL; |
| 703 | rf.ptr = rf.end = NULL; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 704 | PyErr_Clear(); |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 705 | v = r_object(&rf); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 706 | if (PyErr_Occurred()) { |
| 707 | Py_XDECREF(v); |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 708 | v = NULL; |
| 709 | } |
| 710 | return v; |
| 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_dumps(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 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 718 | PyObject *x; |
| 719 | if (!PyArg_Parse(args, "O", &x)) |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 720 | return NULL; |
Guido van Rossum | 3f3bb3d | 1996-08-19 22:07:17 +0000 | [diff] [blame] | 721 | return PyMarshal_WriteObjectToString(x); |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 722 | } |
| 723 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 724 | static PyObject * |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 725 | marshal_loads(self, args) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 726 | PyObject *self; |
| 727 | PyObject *args; |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 728 | { |
| 729 | RFILE rf; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 730 | PyObject *v; |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 731 | char *s; |
| 732 | int n; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 733 | if (!PyArg_Parse(args, "s#", &s, &n)) |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 734 | return NULL; |
| 735 | rf.fp = NULL; |
| 736 | rf.str = args; |
| 737 | rf.ptr = s; |
| 738 | rf.end = s + n; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 739 | PyErr_Clear(); |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 740 | v = r_object(&rf); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 741 | if (PyErr_Occurred()) { |
| 742 | Py_XDECREF(v); |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 743 | v = NULL; |
| 744 | } |
| 745 | return v; |
| 746 | } |
| 747 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 748 | static PyMethodDef marshal_methods[] = { |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 749 | {"dump", marshal_dump}, |
| 750 | {"load", marshal_load}, |
| 751 | {"dumps", marshal_dumps}, |
| 752 | {"loads", marshal_loads}, |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 753 | {NULL, NULL} /* sentinel */ |
| 754 | }; |
| 755 | |
| 756 | void |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 757 | PyMarshal_Init() |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 758 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 759 | (void) Py_InitModule("marshal", marshal_methods); |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 760 | } |