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 | d076c73 | 1998-10-07 19:42:25 +0000 | [diff] [blame] | 145 | PyBufferProcs *pb; |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 146 | |
Guido van Rossum | 730806d | 1998-04-10 22:27:42 +0000 | [diff] [blame] | 147 | if (v == NULL) { |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 148 | w_byte(TYPE_NULL, p); |
Guido van Rossum | 730806d | 1998-04-10 22:27:42 +0000 | [diff] [blame] | 149 | } |
| 150 | else if (v == Py_None) { |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 151 | w_byte(TYPE_NONE, p); |
Guido van Rossum | 730806d | 1998-04-10 22:27:42 +0000 | [diff] [blame] | 152 | } |
| 153 | else if (v == Py_Ellipsis) { |
| 154 | w_byte(TYPE_ELLIPSIS, p); |
| 155 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 156 | else if (PyInt_Check(v)) { |
| 157 | long x = PyInt_AS_LONG((PyIntObject *)v); |
Guido van Rossum | c1547d9 | 1996-12-10 15:39:04 +0000 | [diff] [blame] | 158 | #if SIZEOF_LONG > 4 |
Guido van Rossum | b0c168c | 1996-12-05 23:15:02 +0000 | [diff] [blame] | 159 | long y = x>>31; |
| 160 | if (y && y != -1) { |
| 161 | w_byte(TYPE_INT64, p); |
| 162 | w_long64(x, p); |
| 163 | } |
Guido van Rossum | c1547d9 | 1996-12-10 15:39:04 +0000 | [diff] [blame] | 164 | else |
| 165 | #endif |
| 166 | { |
Guido van Rossum | b0c168c | 1996-12-05 23:15:02 +0000 | [diff] [blame] | 167 | w_byte(TYPE_INT, p); |
| 168 | w_long(x, p); |
| 169 | } |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 170 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 171 | else if (PyLong_Check(v)) { |
| 172 | PyLongObject *ob = (PyLongObject *)v; |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 173 | w_byte(TYPE_LONG, p); |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 174 | n = ob->ob_size; |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 175 | w_long((long)n, p); |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 176 | if (n < 0) |
| 177 | n = -n; |
| 178 | for (i = 0; i < n; i++) |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 179 | w_short(ob->ob_digit[i], p); |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 180 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 181 | else if (PyFloat_Check(v)) { |
| 182 | extern void PyFloat_AsString |
| 183 | Py_PROTO((char *, PyFloatObject *)); |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 184 | char buf[256]; /* Plenty to format any double */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 185 | PyFloat_AsString(buf, (PyFloatObject *)v); |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 186 | n = strlen(buf); |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 187 | w_byte(TYPE_FLOAT, p); |
Guido van Rossum | 3a205f7 | 1995-02-17 15:10:07 +0000 | [diff] [blame] | 188 | w_byte(n, p); |
| 189 | w_string(buf, n, p); |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 190 | } |
Guido van Rossum | 8a5c5d2 | 1996-01-12 01:09:56 +0000 | [diff] [blame] | 191 | #ifndef WITHOUT_COMPLEX |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 192 | else if (PyComplex_Check(v)) { |
| 193 | extern void PyFloat_AsString |
| 194 | Py_PROTO((char *, PyFloatObject *)); |
Guido van Rossum | 8a5c5d2 | 1996-01-12 01:09:56 +0000 | [diff] [blame] | 195 | char buf[256]; /* Plenty to format any double */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 196 | PyFloatObject *temp; |
Guido van Rossum | 8a5c5d2 | 1996-01-12 01:09:56 +0000 | [diff] [blame] | 197 | w_byte(TYPE_COMPLEX, p); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 198 | temp = (PyFloatObject*)PyFloat_FromDouble( |
| 199 | PyComplex_RealAsDouble(v)); |
| 200 | PyFloat_AsString(buf, temp); |
| 201 | Py_DECREF(temp); |
Guido van Rossum | 8a5c5d2 | 1996-01-12 01:09:56 +0000 | [diff] [blame] | 202 | n = strlen(buf); |
| 203 | w_byte(n, p); |
| 204 | w_string(buf, n, p); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 205 | temp = (PyFloatObject*)PyFloat_FromDouble( |
| 206 | PyComplex_ImagAsDouble(v)); |
| 207 | PyFloat_AsString(buf, temp); |
| 208 | Py_DECREF(temp); |
Guido van Rossum | 8a5c5d2 | 1996-01-12 01:09:56 +0000 | [diff] [blame] | 209 | n = strlen(buf); |
| 210 | w_byte(n, p); |
| 211 | w_string(buf, n, p); |
| 212 | } |
| 213 | #endif |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 214 | else if (PyString_Check(v)) { |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 215 | w_byte(TYPE_STRING, p); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 216 | n = PyString_Size(v); |
Guido van Rossum | 3a205f7 | 1995-02-17 15:10:07 +0000 | [diff] [blame] | 217 | w_long((long)n, p); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 218 | w_string(PyString_AsString(v), n, p); |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 219 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 220 | else if (PyTuple_Check(v)) { |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 221 | w_byte(TYPE_TUPLE, p); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 222 | n = PyTuple_Size(v); |
Guido van Rossum | 3a205f7 | 1995-02-17 15:10:07 +0000 | [diff] [blame] | 223 | w_long((long)n, p); |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 224 | for (i = 0; i < n; i++) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 225 | w_object(PyTuple_GET_ITEM(v, i), p); |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 226 | } |
| 227 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 228 | else if (PyList_Check(v)) { |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 229 | w_byte(TYPE_LIST, p); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 230 | n = PyList_Size(v); |
Guido van Rossum | 3a205f7 | 1995-02-17 15:10:07 +0000 | [diff] [blame] | 231 | w_long((long)n, p); |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 232 | for (i = 0; i < n; i++) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 233 | w_object(PyList_GetItem(v, i), p); |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 234 | } |
| 235 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 236 | else if (PyDict_Check(v)) { |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 237 | int pos; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 238 | PyObject *key, *value; |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 239 | w_byte(TYPE_DICT, p); |
Guido van Rossum | 64b4552 | 1991-06-07 13:58:22 +0000 | [diff] [blame] | 240 | /* This one is NULL object terminated! */ |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 241 | pos = 0; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 242 | while (PyDict_Next(v, &pos, &key, &value)) { |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 243 | w_object(key, p); |
| 244 | w_object(value, p); |
Guido van Rossum | 64b4552 | 1991-06-07 13:58:22 +0000 | [diff] [blame] | 245 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 246 | w_object((PyObject *)NULL, p); |
Guido van Rossum | 64b4552 | 1991-06-07 13:58:22 +0000 | [diff] [blame] | 247 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 248 | else if (PyCode_Check(v)) { |
| 249 | PyCodeObject *co = (PyCodeObject *)v; |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 250 | w_byte(TYPE_CODE, p); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 251 | w_short(co->co_argcount, p); |
| 252 | w_short(co->co_nlocals, p); |
Guido van Rossum | 98a9b31 | 1997-01-17 21:07:08 +0000 | [diff] [blame] | 253 | w_short(co->co_stacksize, p); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 254 | w_short(co->co_flags, p); |
Guido van Rossum | d076c73 | 1998-10-07 19:42:25 +0000 | [diff] [blame] | 255 | w_object(co->co_code, p); |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 256 | w_object(co->co_consts, p); |
| 257 | w_object(co->co_names, p); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 258 | w_object(co->co_varnames, p); |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 259 | w_object(co->co_filename, p); |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 260 | w_object(co->co_name, p); |
Guido van Rossum | d031c89 | 1997-01-24 03:44:17 +0000 | [diff] [blame] | 261 | w_short(co->co_firstlineno, p); |
| 262 | w_object(co->co_lnotab, p); |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 263 | } |
Guido van Rossum | d076c73 | 1998-10-07 19:42:25 +0000 | [diff] [blame] | 264 | else if ((pb = v->ob_type->tp_as_buffer) != NULL && |
| 265 | pb->bf_getsegcount != NULL && |
| 266 | pb->bf_getreadbuffer != NULL && |
| 267 | (*pb->bf_getsegcount)(v, NULL) == 1) |
| 268 | { |
| 269 | /* Write unknown buffer-style objects as a string */ |
| 270 | char *s; |
| 271 | w_byte(TYPE_STRING, p); |
| 272 | n = (*pb->bf_getreadbuffer)(v, 0, (void **)&s); |
| 273 | w_long((long)n, p); |
| 274 | w_string(s, n, p); |
| 275 | } |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 276 | else { |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 277 | w_byte(TYPE_UNKNOWN, p); |
Guido van Rossum | f215060 | 1996-06-26 20:41:23 +0000 | [diff] [blame] | 278 | p->error = 1; |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 279 | } |
| 280 | } |
| 281 | |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 282 | void |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 283 | PyMarshal_WriteLongToFile(x, fp) |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 284 | long x; |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 285 | FILE *fp; |
| 286 | { |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 287 | WFILE wf; |
| 288 | wf.fp = fp; |
Guido van Rossum | f215060 | 1996-06-26 20:41:23 +0000 | [diff] [blame] | 289 | wf.error = 0; |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 290 | w_long(x, &wf); |
| 291 | } |
| 292 | |
| 293 | void |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 294 | PyMarshal_WriteObjectToFile(x, fp) |
| 295 | PyObject *x; |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 296 | FILE *fp; |
| 297 | { |
| 298 | WFILE wf; |
| 299 | wf.fp = fp; |
Guido van Rossum | f215060 | 1996-06-26 20:41:23 +0000 | [diff] [blame] | 300 | wf.error = 0; |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 301 | w_object(x, &wf); |
| 302 | } |
| 303 | |
| 304 | typedef WFILE RFILE; /* Same struct with different invariants */ |
| 305 | |
Guido van Rossum | 8d617a6 | 1995-03-09 12:12:11 +0000 | [diff] [blame] | 306 | #define rs_byte(p) (((p)->ptr != (p)->end) ? (unsigned char)*(p)->ptr++ : EOF) |
| 307 | |
| 308 | #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] | 309 | |
| 310 | static int |
| 311 | r_string(s, n, p) |
| 312 | char *s; |
| 313 | int n; |
| 314 | RFILE *p; |
| 315 | { |
| 316 | if (p->fp != NULL) |
| 317 | return fread(s, 1, n, p->fp); |
| 318 | if (p->end - p->ptr < n) |
| 319 | n = p->end - p->ptr; |
| 320 | memcpy(s, p->ptr, n); |
| 321 | p->ptr += n; |
| 322 | return n; |
| 323 | } |
| 324 | |
| 325 | static int |
| 326 | r_short(p) |
| 327 | RFILE *p; |
| 328 | { |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 329 | register short x; |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 330 | x = r_byte(p); |
| 331 | x |= r_byte(p) << 8; |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 332 | /* XXX If your short is > 16 bits, add sign-extension here!!! */ |
| 333 | return x; |
| 334 | } |
| 335 | |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 336 | static long |
| 337 | r_long(p) |
| 338 | RFILE *p; |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 339 | { |
| 340 | register long x; |
Guido van Rossum | 8d617a6 | 1995-03-09 12:12:11 +0000 | [diff] [blame] | 341 | register FILE *fp = p->fp; |
| 342 | if (fp) { |
| 343 | x = getc(fp); |
| 344 | x |= (long)getc(fp) << 8; |
| 345 | x |= (long)getc(fp) << 16; |
| 346 | x |= (long)getc(fp) << 24; |
| 347 | } |
| 348 | else { |
| 349 | x = rs_byte(p); |
| 350 | x |= (long)rs_byte(p) << 8; |
| 351 | x |= (long)rs_byte(p) << 16; |
| 352 | x |= (long)rs_byte(p) << 24; |
| 353 | } |
Guido van Rossum | c1547d9 | 1996-12-10 15:39:04 +0000 | [diff] [blame] | 354 | #if SIZEOF_LONG > 4 |
Guido van Rossum | b0c168c | 1996-12-05 23:15:02 +0000 | [diff] [blame] | 355 | /* Sign extension for 64-bit machines */ |
| 356 | x <<= (8*sizeof(long) - 32); |
| 357 | x >>= (8*sizeof(long) - 32); |
Guido van Rossum | c1547d9 | 1996-12-10 15:39:04 +0000 | [diff] [blame] | 358 | #endif |
Guido van Rossum | b0c168c | 1996-12-05 23:15:02 +0000 | [diff] [blame] | 359 | return x; |
| 360 | } |
| 361 | |
| 362 | static long |
| 363 | r_long64(p) |
| 364 | RFILE *p; |
| 365 | { |
| 366 | register long x; |
Guido van Rossum | c1547d9 | 1996-12-10 15:39:04 +0000 | [diff] [blame] | 367 | x = r_long(p); |
| 368 | #if SIZEOF_LONG > 4 |
| 369 | x = (x & 0xFFFFFFFF) | (r_long(p) << 32); |
| 370 | #else |
| 371 | if (r_long(p) != 0) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 372 | PyObject *f = PySys_GetObject("stderr"); |
Guido van Rossum | c1547d9 | 1996-12-10 15:39:04 +0000 | [diff] [blame] | 373 | if (f != NULL) |
Guido van Rossum | 7e8d26d | 1997-05-22 22:35:47 +0000 | [diff] [blame] | 374 | (void) PyFile_WriteString( |
Guido van Rossum | c1547d9 | 1996-12-10 15:39:04 +0000 | [diff] [blame] | 375 | "Warning: un-marshal 64-bit int in 32-bit mode\n", |
| 376 | f); |
Guido van Rossum | b0c168c | 1996-12-05 23:15:02 +0000 | [diff] [blame] | 377 | } |
Guido van Rossum | c1547d9 | 1996-12-10 15:39:04 +0000 | [diff] [blame] | 378 | #endif |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 379 | return x; |
| 380 | } |
| 381 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 382 | static PyObject * |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 383 | r_object(p) |
| 384 | RFILE *p; |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 385 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 386 | PyObject *v, *v2; |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 387 | long i, n; |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 388 | int type = r_byte(p); |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 389 | |
| 390 | switch (type) { |
| 391 | |
| 392 | case EOF: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 393 | PyErr_SetString(PyExc_EOFError, |
| 394 | "EOF read where object expected"); |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 395 | return NULL; |
| 396 | |
| 397 | case TYPE_NULL: |
| 398 | return NULL; |
| 399 | |
| 400 | case TYPE_NONE: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 401 | Py_INCREF(Py_None); |
| 402 | return Py_None; |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 403 | |
Guido van Rossum | e449af7 | 1996-10-11 16:25:41 +0000 | [diff] [blame] | 404 | case TYPE_ELLIPSIS: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 405 | Py_INCREF(Py_Ellipsis); |
Guido van Rossum | e449af7 | 1996-10-11 16:25:41 +0000 | [diff] [blame] | 406 | return Py_Ellipsis; |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 407 | |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 408 | case TYPE_INT: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 409 | return PyInt_FromLong(r_long(p)); |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 410 | |
Guido van Rossum | b0c168c | 1996-12-05 23:15:02 +0000 | [diff] [blame] | 411 | case TYPE_INT64: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 412 | return PyInt_FromLong(r_long64(p)); |
Guido van Rossum | b0c168c | 1996-12-05 23:15:02 +0000 | [diff] [blame] | 413 | |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 414 | case TYPE_LONG: |
| 415 | { |
| 416 | int size; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 417 | PyLongObject *ob; |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 418 | n = r_long(p); |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 419 | size = n<0 ? -n : n; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 420 | ob = _PyLong_New(size); |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 421 | if (ob == NULL) |
| 422 | return NULL; |
| 423 | ob->ob_size = n; |
| 424 | for (i = 0; i < size; i++) |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 425 | ob->ob_digit[i] = r_short(p); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 426 | return (PyObject *)ob; |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 427 | } |
| 428 | |
| 429 | case TYPE_FLOAT: |
| 430 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 431 | extern double atof Py_PROTO((const char *)); |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 432 | char buf[256]; |
Guido van Rossum | 0ae748d | 1997-02-14 22:58:07 +0000 | [diff] [blame] | 433 | double dx; |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 434 | n = r_byte(p); |
| 435 | if (r_string(buf, (int)n, p) != n) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 436 | PyErr_SetString(PyExc_EOFError, |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 437 | "EOF read where object expected"); |
| 438 | return NULL; |
| 439 | } |
| 440 | buf[n] = '\0'; |
Guido van Rossum | 0ae748d | 1997-02-14 22:58:07 +0000 | [diff] [blame] | 441 | PyFPE_START_PROTECT("atof", return 0) |
| 442 | dx = atof(buf); |
Guido van Rossum | 45b8391 | 1997-03-14 04:32:50 +0000 | [diff] [blame] | 443 | PyFPE_END_PROTECT(dx) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 444 | return PyFloat_FromDouble(dx); |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 445 | } |
| 446 | |
Guido van Rossum | 8a5c5d2 | 1996-01-12 01:09:56 +0000 | [diff] [blame] | 447 | #ifndef WITHOUT_COMPLEX |
| 448 | case TYPE_COMPLEX: |
| 449 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 450 | extern double atof Py_PROTO((const char *)); |
Guido van Rossum | 8a5c5d2 | 1996-01-12 01:09:56 +0000 | [diff] [blame] | 451 | char buf[256]; |
Guido van Rossum | 530956d | 1996-07-21 02:27:43 +0000 | [diff] [blame] | 452 | Py_complex c; |
Guido van Rossum | 8a5c5d2 | 1996-01-12 01:09:56 +0000 | [diff] [blame] | 453 | n = r_byte(p); |
| 454 | if (r_string(buf, (int)n, p) != n) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 455 | PyErr_SetString(PyExc_EOFError, |
Guido van Rossum | 8a5c5d2 | 1996-01-12 01:09:56 +0000 | [diff] [blame] | 456 | "EOF read where object expected"); |
| 457 | return NULL; |
| 458 | } |
| 459 | buf[n] = '\0'; |
Guido van Rossum | 0ae748d | 1997-02-14 22:58:07 +0000 | [diff] [blame] | 460 | PyFPE_START_PROTECT("atof", return 0) |
Guido van Rossum | 8a5c5d2 | 1996-01-12 01:09:56 +0000 | [diff] [blame] | 461 | c.real = atof(buf); |
Guido van Rossum | 45b8391 | 1997-03-14 04:32:50 +0000 | [diff] [blame] | 462 | PyFPE_END_PROTECT(c) |
Guido van Rossum | 8a5c5d2 | 1996-01-12 01:09:56 +0000 | [diff] [blame] | 463 | n = r_byte(p); |
| 464 | if (r_string(buf, (int)n, p) != n) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 465 | PyErr_SetString(PyExc_EOFError, |
Guido van Rossum | 8a5c5d2 | 1996-01-12 01:09:56 +0000 | [diff] [blame] | 466 | "EOF read where object expected"); |
| 467 | return NULL; |
| 468 | } |
| 469 | buf[n] = '\0'; |
Guido van Rossum | 0ae748d | 1997-02-14 22:58:07 +0000 | [diff] [blame] | 470 | PyFPE_START_PROTECT("atof", return 0) |
Guido van Rossum | 8a5c5d2 | 1996-01-12 01:09:56 +0000 | [diff] [blame] | 471 | c.imag = atof(buf); |
Guido van Rossum | 45b8391 | 1997-03-14 04:32:50 +0000 | [diff] [blame] | 472 | PyFPE_END_PROTECT(c) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 473 | return PyComplex_FromCComplex(c); |
Guido van Rossum | 8a5c5d2 | 1996-01-12 01:09:56 +0000 | [diff] [blame] | 474 | } |
| 475 | #endif |
| 476 | |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 477 | case TYPE_STRING: |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 478 | n = r_long(p); |
Guido van Rossum | a45cb45 | 1998-06-08 20:27:29 +0000 | [diff] [blame] | 479 | if (n < 0) { |
| 480 | PyErr_SetString(PyExc_ValueError, "bad marshal data"); |
| 481 | return NULL; |
| 482 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 483 | v = PyString_FromStringAndSize((char *)NULL, n); |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 484 | if (v != NULL) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 485 | if (r_string(PyString_AsString(v), (int)n, p) != n) { |
| 486 | Py_DECREF(v); |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 487 | v = NULL; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 488 | PyErr_SetString(PyExc_EOFError, |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 489 | "EOF read where object expected"); |
| 490 | } |
| 491 | } |
| 492 | return v; |
| 493 | |
| 494 | case TYPE_TUPLE: |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 495 | n = r_long(p); |
Guido van Rossum | a45cb45 | 1998-06-08 20:27:29 +0000 | [diff] [blame] | 496 | if (n < 0) { |
| 497 | PyErr_SetString(PyExc_ValueError, "bad marshal data"); |
| 498 | return NULL; |
| 499 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 500 | v = PyTuple_New((int)n); |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 501 | if (v == NULL) |
| 502 | return v; |
Jack Jansen | 9513f2c | 1995-10-27 13:21:28 +0000 | [diff] [blame] | 503 | for (i = 0; i < n; i++) { |
| 504 | v2 = r_object(p); |
| 505 | if ( v2 == NULL ) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 506 | Py_DECREF(v); |
Jack Jansen | 9513f2c | 1995-10-27 13:21:28 +0000 | [diff] [blame] | 507 | v = NULL; |
| 508 | break; |
| 509 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 510 | PyTuple_SET_ITEM(v, (int)i, v2); |
Jack Jansen | 9513f2c | 1995-10-27 13:21:28 +0000 | [diff] [blame] | 511 | } |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 512 | return v; |
| 513 | |
| 514 | case TYPE_LIST: |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 515 | n = r_long(p); |
Guido van Rossum | a45cb45 | 1998-06-08 20:27:29 +0000 | [diff] [blame] | 516 | if (n < 0) { |
| 517 | PyErr_SetString(PyExc_ValueError, "bad marshal data"); |
| 518 | return NULL; |
| 519 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 520 | v = PyList_New((int)n); |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 521 | if (v == NULL) |
| 522 | return v; |
Jack Jansen | 9513f2c | 1995-10-27 13:21:28 +0000 | [diff] [blame] | 523 | for (i = 0; i < n; i++) { |
| 524 | v2 = r_object(p); |
| 525 | if ( v2 == NULL ) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 526 | Py_DECREF(v); |
Jack Jansen | 9513f2c | 1995-10-27 13:21:28 +0000 | [diff] [blame] | 527 | v = NULL; |
| 528 | break; |
| 529 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 530 | PyList_SetItem(v, (int)i, v2); |
Jack Jansen | 9513f2c | 1995-10-27 13:21:28 +0000 | [diff] [blame] | 531 | } |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 532 | return v; |
| 533 | |
Guido van Rossum | 64b4552 | 1991-06-07 13:58:22 +0000 | [diff] [blame] | 534 | case TYPE_DICT: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 535 | v = PyDict_New(); |
Guido van Rossum | 64b4552 | 1991-06-07 13:58:22 +0000 | [diff] [blame] | 536 | if (v == NULL) |
| 537 | return NULL; |
| 538 | for (;;) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 539 | PyObject *key, *val; |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 540 | key = r_object(p); |
Guido van Rossum | 64b4552 | 1991-06-07 13:58:22 +0000 | [diff] [blame] | 541 | if (key == NULL) |
Guido van Rossum | f215060 | 1996-06-26 20:41:23 +0000 | [diff] [blame] | 542 | break; /* XXX Assume TYPE_NULL, not an error */ |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 543 | val = r_object(p); |
Guido van Rossum | f215060 | 1996-06-26 20:41:23 +0000 | [diff] [blame] | 544 | if (val != NULL) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 545 | PyDict_SetItem(v, key, val); |
| 546 | Py_DECREF(key); |
| 547 | Py_XDECREF(val); |
Guido van Rossum | 64b4552 | 1991-06-07 13:58:22 +0000 | [diff] [blame] | 548 | } |
| 549 | return v; |
| 550 | |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 551 | case TYPE_CODE: |
| 552 | { |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 553 | int argcount = r_short(p); |
| 554 | int nlocals = r_short(p); |
Guido van Rossum | 98a9b31 | 1997-01-17 21:07:08 +0000 | [diff] [blame] | 555 | int stacksize = r_short(p); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 556 | int flags = r_short(p); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 557 | PyObject *code = NULL; |
| 558 | PyObject *consts = NULL; |
| 559 | PyObject *names = NULL; |
| 560 | PyObject *varnames = NULL; |
| 561 | PyObject *filename = NULL; |
| 562 | PyObject *name = NULL; |
Guido van Rossum | 0f4bbd2 | 1997-02-14 21:12:56 +0000 | [diff] [blame] | 563 | int firstlineno = 0; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 564 | PyObject *lnotab = NULL; |
Jack Jansen | 9513f2c | 1995-10-27 13:21:28 +0000 | [diff] [blame] | 565 | |
| 566 | code = r_object(p); |
| 567 | if (code) consts = r_object(p); |
| 568 | if (consts) names = r_object(p); |
| 569 | if (names) varnames = r_object(p); |
| 570 | if (varnames) filename = r_object(p); |
| 571 | if (filename) name = r_object(p); |
Guido van Rossum | d031c89 | 1997-01-24 03:44:17 +0000 | [diff] [blame] | 572 | if (name) { |
| 573 | firstlineno = r_short(p); |
| 574 | lnotab = r_object(p); |
| 575 | } |
Jack Jansen | 9513f2c | 1995-10-27 13:21:28 +0000 | [diff] [blame] | 576 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 577 | if (!PyErr_Occurred()) { |
| 578 | v = (PyObject *) PyCode_New( |
Guido van Rossum | 98a9b31 | 1997-01-17 21:07:08 +0000 | [diff] [blame] | 579 | argcount, nlocals, stacksize, flags, |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 580 | code, consts, names, varnames, |
Guido van Rossum | d031c89 | 1997-01-24 03:44:17 +0000 | [diff] [blame] | 581 | filename, name, firstlineno, lnotab); |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 582 | } |
| 583 | else |
| 584 | v = NULL; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 585 | Py_XDECREF(code); |
| 586 | Py_XDECREF(consts); |
| 587 | Py_XDECREF(names); |
| 588 | Py_XDECREF(varnames); |
| 589 | Py_XDECREF(filename); |
| 590 | Py_XDECREF(name); |
Guido van Rossum | 6fc06e7 | 1997-07-26 23:30:18 +0000 | [diff] [blame] | 591 | Py_XDECREF(lnotab); |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 592 | |
| 593 | } |
| 594 | return v; |
| 595 | |
| 596 | default: |
Guido van Rossum | f215060 | 1996-06-26 20:41:23 +0000 | [diff] [blame] | 597 | /* Bogus data got written, which isn't ideal. |
| 598 | This will let you keep working and recover. */ |
Guido van Rossum | a45cb45 | 1998-06-08 20:27:29 +0000 | [diff] [blame] | 599 | PyErr_SetString(PyExc_ValueError, "bad marshal data"); |
| 600 | return NULL; |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 601 | |
| 602 | } |
| 603 | } |
| 604 | |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 605 | long |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 606 | PyMarshal_ReadLongFromFile(fp) |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 607 | FILE *fp; |
| 608 | { |
| 609 | RFILE rf; |
| 610 | rf.fp = fp; |
| 611 | return r_long(&rf); |
| 612 | } |
| 613 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 614 | PyObject * |
| 615 | PyMarshal_ReadObjectFromFile(fp) |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 616 | FILE *fp; |
| 617 | { |
| 618 | RFILE rf; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 619 | if (PyErr_Occurred()) { |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 620 | fprintf(stderr, "XXX rd_object called with exception set\n"); |
| 621 | return NULL; |
| 622 | } |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 623 | rf.fp = fp; |
| 624 | return r_object(&rf); |
| 625 | } |
| 626 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 627 | PyObject * |
| 628 | PyMarshal_ReadObjectFromString(str, len) |
Guido van Rossum | f56e3db | 1993-04-01 20:59:32 +0000 | [diff] [blame] | 629 | char *str; |
| 630 | int len; |
| 631 | { |
| 632 | RFILE rf; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 633 | if (PyErr_Occurred()) { |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 634 | fprintf(stderr, "XXX rds_object called with exception set\n"); |
| 635 | return NULL; |
| 636 | } |
Guido van Rossum | f56e3db | 1993-04-01 20:59:32 +0000 | [diff] [blame] | 637 | rf.fp = NULL; |
| 638 | rf.str = NULL; |
| 639 | rf.ptr = str; |
| 640 | rf.end = str + len; |
| 641 | return r_object(&rf); |
| 642 | } |
| 643 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 644 | PyObject * |
Guido van Rossum | 3f3bb3d | 1996-08-19 22:07:17 +0000 | [diff] [blame] | 645 | PyMarshal_WriteObjectToString(x) /* wrs_object() */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 646 | PyObject *x; |
Guido van Rossum | 3f3bb3d | 1996-08-19 22:07:17 +0000 | [diff] [blame] | 647 | { |
| 648 | WFILE wf; |
| 649 | wf.fp = NULL; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 650 | wf.str = PyString_FromStringAndSize((char *)NULL, 50); |
Guido van Rossum | 3f3bb3d | 1996-08-19 22:07:17 +0000 | [diff] [blame] | 651 | if (wf.str == NULL) |
| 652 | return NULL; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 653 | wf.ptr = PyString_AS_STRING((PyStringObject *)wf.str); |
| 654 | wf.end = wf.ptr + PyString_Size(wf.str); |
Guido van Rossum | 3f3bb3d | 1996-08-19 22:07:17 +0000 | [diff] [blame] | 655 | wf.error = 0; |
| 656 | w_object(x, &wf); |
| 657 | if (wf.str != NULL) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 658 | _PyString_Resize(&wf.str, |
| 659 | (int) (wf.ptr - |
| 660 | PyString_AS_STRING((PyStringObject *)wf.str))); |
Guido van Rossum | 3f3bb3d | 1996-08-19 22:07:17 +0000 | [diff] [blame] | 661 | if (wf.error) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 662 | Py_XDECREF(wf.str); |
| 663 | PyErr_SetString(PyExc_ValueError, "unmarshallable object"); |
Guido van Rossum | 3f3bb3d | 1996-08-19 22:07:17 +0000 | [diff] [blame] | 664 | return NULL; |
| 665 | } |
| 666 | return wf.str; |
| 667 | } |
| 668 | |
Guido van Rossum | 64b4552 | 1991-06-07 13:58:22 +0000 | [diff] [blame] | 669 | /* And an interface for Python programs... */ |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 670 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 671 | static PyObject * |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 672 | marshal_dump(self, args) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 673 | PyObject *self; |
| 674 | PyObject *args; |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 675 | { |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 676 | WFILE wf; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 677 | PyObject *x; |
| 678 | PyObject *f; |
| 679 | if (!PyArg_Parse(args, "(OO)", &x, &f)) |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 680 | return NULL; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 681 | if (!PyFile_Check(f)) { |
| 682 | PyErr_SetString(PyExc_TypeError, |
| 683 | "marshal.dump() 2nd arg must be file"); |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 684 | return NULL; |
| 685 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 686 | wf.fp = PyFile_AsFile(f); |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 687 | wf.str = NULL; |
| 688 | wf.ptr = wf.end = NULL; |
Guido van Rossum | f215060 | 1996-06-26 20:41:23 +0000 | [diff] [blame] | 689 | wf.error = 0; |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 690 | w_object(x, &wf); |
Guido van Rossum | f215060 | 1996-06-26 20:41:23 +0000 | [diff] [blame] | 691 | if (wf.error) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 692 | PyErr_SetString(PyExc_ValueError, "unmarshallable object"); |
Guido van Rossum | f215060 | 1996-06-26 20:41:23 +0000 | [diff] [blame] | 693 | return NULL; |
| 694 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 695 | Py_INCREF(Py_None); |
| 696 | return Py_None; |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 697 | } |
| 698 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 699 | static PyObject * |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 700 | marshal_load(self, args) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 701 | PyObject *self; |
| 702 | PyObject *args; |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 703 | { |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 704 | RFILE rf; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 705 | PyObject *f; |
| 706 | PyObject *v; |
| 707 | if (!PyArg_Parse(args, "O", &f)) |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 708 | return NULL; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 709 | if (!PyFile_Check(f)) { |
| 710 | PyErr_SetString(PyExc_TypeError, |
| 711 | "marshal.load() arg must be file"); |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 712 | return NULL; |
| 713 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 714 | rf.fp = PyFile_AsFile(f); |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 715 | rf.str = NULL; |
| 716 | rf.ptr = rf.end = NULL; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 717 | PyErr_Clear(); |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 718 | v = r_object(&rf); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 719 | if (PyErr_Occurred()) { |
| 720 | Py_XDECREF(v); |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 721 | v = NULL; |
| 722 | } |
| 723 | return v; |
| 724 | } |
| 725 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 726 | static PyObject * |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 727 | marshal_dumps(self, args) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 728 | PyObject *self; |
| 729 | PyObject *args; |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 730 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 731 | PyObject *x; |
| 732 | if (!PyArg_Parse(args, "O", &x)) |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 733 | return NULL; |
Guido van Rossum | 3f3bb3d | 1996-08-19 22:07:17 +0000 | [diff] [blame] | 734 | return PyMarshal_WriteObjectToString(x); |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 735 | } |
| 736 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 737 | static PyObject * |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 738 | marshal_loads(self, args) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 739 | PyObject *self; |
| 740 | PyObject *args; |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 741 | { |
| 742 | RFILE rf; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 743 | PyObject *v; |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 744 | char *s; |
| 745 | int n; |
Guido van Rossum | 5bd893b | 1998-10-08 01:45:47 +0000 | [diff] [blame^] | 746 | if (!PyArg_Parse(args, "s#", &s, &n)) |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 747 | return NULL; |
| 748 | rf.fp = NULL; |
| 749 | rf.str = args; |
| 750 | rf.ptr = s; |
| 751 | rf.end = s + n; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 752 | PyErr_Clear(); |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 753 | v = r_object(&rf); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 754 | if (PyErr_Occurred()) { |
| 755 | Py_XDECREF(v); |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 756 | v = NULL; |
| 757 | } |
| 758 | return v; |
| 759 | } |
| 760 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 761 | static PyMethodDef marshal_methods[] = { |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 762 | {"dump", marshal_dump}, |
| 763 | {"load", marshal_load}, |
| 764 | {"dumps", marshal_dumps}, |
| 765 | {"loads", marshal_loads}, |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 766 | {NULL, NULL} /* sentinel */ |
| 767 | }; |
| 768 | |
| 769 | void |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 770 | PyMarshal_Init() |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 771 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 772 | (void) Py_InitModule("marshal", marshal_methods); |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 773 | } |