Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 1 | |
| 2 | /* Write Python objects to files and read them back. |
| 3 | This is intended for writing and reading compiled Python code only; |
| 4 | a true persistent storage facility would be much harder, since |
| 5 | it would have to take circular links and sharing into account. */ |
| 6 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 7 | #include "Python.h" |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 8 | #include "longintrepr.h" |
| 9 | #include "compile.h" |
| 10 | #include "marshal.h" |
| 11 | |
Fred Drake | 6da0b91 | 2000-06-28 18:47:56 +0000 | [diff] [blame] | 12 | /* High water mark to determine when the marshalled object is dangerously deep |
| 13 | * and risks coring the interpreter. When the object stack gets this deep, |
| 14 | * raise an exception instead of continuing. |
| 15 | */ |
| 16 | #define MAX_MARSHAL_STACK_DEPTH 5000 |
| 17 | |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 18 | #define TYPE_NULL '0' |
| 19 | #define TYPE_NONE 'N' |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 20 | #define TYPE_FALSE 'F' |
| 21 | #define TYPE_TRUE 'T' |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 22 | #define TYPE_STOPITER 'S' |
Guido van Rossum | e449af7 | 1996-10-11 16:25:41 +0000 | [diff] [blame] | 23 | #define TYPE_ELLIPSIS '.' |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 24 | #define TYPE_INT 'i' |
Guido van Rossum | b0c168c | 1996-12-05 23:15:02 +0000 | [diff] [blame] | 25 | #define TYPE_INT64 'I' |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 26 | #define TYPE_FLOAT 'f' |
Guido van Rossum | 8a5c5d2 | 1996-01-12 01:09:56 +0000 | [diff] [blame] | 27 | #define TYPE_COMPLEX 'x' |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 28 | #define TYPE_LONG 'l' |
| 29 | #define TYPE_STRING 's' |
| 30 | #define TYPE_TUPLE '(' |
| 31 | #define TYPE_LIST '[' |
| 32 | #define TYPE_DICT '{' |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 33 | #define TYPE_CODE 'c' |
Guido van Rossum | c279b53 | 2000-03-10 23:03:02 +0000 | [diff] [blame] | 34 | #define TYPE_UNICODE 'u' |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 35 | #define TYPE_UNKNOWN '?' |
| 36 | |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 37 | typedef struct { |
| 38 | FILE *fp; |
Guido van Rossum | f215060 | 1996-06-26 20:41:23 +0000 | [diff] [blame] | 39 | int error; |
Fred Drake | 6da0b91 | 2000-06-28 18:47:56 +0000 | [diff] [blame] | 40 | int depth; |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 41 | /* If fp == NULL, the following are valid: */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 42 | PyObject *str; |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 43 | char *ptr; |
| 44 | char *end; |
| 45 | } WFILE; |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 46 | |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 47 | #define w_byte(c, p) if (((p)->fp)) putc((c), (p)->fp); \ |
| 48 | else if ((p)->ptr != (p)->end) *(p)->ptr++ = (c); \ |
| 49 | else w_more(c, p) |
| 50 | |
| 51 | static void |
Fredrik Lundh | 1153438 | 2000-07-23 18:24:06 +0000 | [diff] [blame] | 52 | w_more(int c, WFILE *p) |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 53 | { |
| 54 | int size, newsize; |
| 55 | if (p->str == NULL) |
| 56 | return; /* An error already occurred */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 57 | size = PyString_Size(p->str); |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 58 | newsize = size + 1024; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 59 | if (_PyString_Resize(&p->str, newsize) != 0) { |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 60 | p->ptr = p->end = NULL; |
| 61 | } |
| 62 | else { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 63 | p->ptr = PyString_AS_STRING((PyStringObject *)p->str) + size; |
| 64 | p->end = |
| 65 | PyString_AS_STRING((PyStringObject *)p->str) + newsize; |
Tim Peters | 8315ea5 | 2000-07-23 19:28:35 +0000 | [diff] [blame] | 66 | *p->ptr++ = Py_SAFE_DOWNCAST(c, int, char); |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 67 | } |
| 68 | } |
| 69 | |
| 70 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 71 | w_string(char *s, int n, WFILE *p) |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 72 | { |
| 73 | if (p->fp != NULL) { |
| 74 | fwrite(s, 1, n, p->fp); |
| 75 | } |
| 76 | else { |
| 77 | while (--n >= 0) { |
| 78 | w_byte(*s, p); |
| 79 | s++; |
| 80 | } |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 85 | w_short(int x, WFILE *p) |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 86 | { |
Thomas Heller | 3e1c18a | 2002-07-30 11:40:57 +0000 | [diff] [blame] | 87 | w_byte((char)( x & 0xff), p); |
| 88 | w_byte((char)((x>> 8) & 0xff), p); |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 89 | } |
| 90 | |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 91 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 92 | w_long(long x, WFILE *p) |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 93 | { |
Thomas Heller | 37d5a15 | 2002-07-30 11:44:44 +0000 | [diff] [blame] | 94 | w_byte((char)( x & 0xff), p); |
| 95 | w_byte((char)((x>> 8) & 0xff), p); |
| 96 | w_byte((char)((x>>16) & 0xff), p); |
| 97 | w_byte((char)((x>>24) & 0xff), p); |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 98 | } |
| 99 | |
Guido van Rossum | c1547d9 | 1996-12-10 15:39:04 +0000 | [diff] [blame] | 100 | #if SIZEOF_LONG > 4 |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 101 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 102 | w_long64(long x, WFILE *p) |
Guido van Rossum | b0c168c | 1996-12-05 23:15:02 +0000 | [diff] [blame] | 103 | { |
| 104 | w_long(x, p); |
Guido van Rossum | c1547d9 | 1996-12-10 15:39:04 +0000 | [diff] [blame] | 105 | w_long(x>>32, p); |
Guido van Rossum | b0c168c | 1996-12-05 23:15:02 +0000 | [diff] [blame] | 106 | } |
Guido van Rossum | c1547d9 | 1996-12-10 15:39:04 +0000 | [diff] [blame] | 107 | #endif |
Guido van Rossum | b0c168c | 1996-12-05 23:15:02 +0000 | [diff] [blame] | 108 | |
| 109 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 110 | w_object(PyObject *v, WFILE *p) |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 111 | { |
Guido van Rossum | 3a205f7 | 1995-02-17 15:10:07 +0000 | [diff] [blame] | 112 | int i, n; |
Fred Drake | 6da0b91 | 2000-06-28 18:47:56 +0000 | [diff] [blame] | 113 | |
| 114 | p->depth++; |
Tim Peters | d9b9ac8 | 2001-01-28 00:27:39 +0000 | [diff] [blame] | 115 | |
Fred Drake | 6da0b91 | 2000-06-28 18:47:56 +0000 | [diff] [blame] | 116 | if (p->depth > MAX_MARSHAL_STACK_DEPTH) { |
| 117 | p->error = 2; |
Tim Peters | d9b9ac8 | 2001-01-28 00:27:39 +0000 | [diff] [blame] | 118 | } |
Fred Drake | 6da0b91 | 2000-06-28 18:47:56 +0000 | [diff] [blame] | 119 | else if (v == NULL) { |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 120 | w_byte(TYPE_NULL, p); |
Guido van Rossum | 730806d | 1998-04-10 22:27:42 +0000 | [diff] [blame] | 121 | } |
| 122 | else if (v == Py_None) { |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 123 | w_byte(TYPE_NONE, p); |
Guido van Rossum | 730806d | 1998-04-10 22:27:42 +0000 | [diff] [blame] | 124 | } |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 125 | else if (v == PyExc_StopIteration) { |
| 126 | w_byte(TYPE_STOPITER, p); |
| 127 | } |
Guido van Rossum | 730806d | 1998-04-10 22:27:42 +0000 | [diff] [blame] | 128 | else if (v == Py_Ellipsis) { |
| 129 | w_byte(TYPE_ELLIPSIS, p); |
| 130 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 131 | else if (v == Py_False) { |
| 132 | w_byte(TYPE_FALSE, p); |
| 133 | } |
| 134 | else if (v == Py_True) { |
| 135 | w_byte(TYPE_TRUE, p); |
| 136 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 137 | else if (PyInt_Check(v)) { |
| 138 | long x = PyInt_AS_LONG((PyIntObject *)v); |
Guido van Rossum | c1547d9 | 1996-12-10 15:39:04 +0000 | [diff] [blame] | 139 | #if SIZEOF_LONG > 4 |
Tim Peters | 4471400 | 2001-04-10 05:02:52 +0000 | [diff] [blame] | 140 | long y = Py_ARITHMETIC_RIGHT_SHIFT(long, x, 31); |
Guido van Rossum | b0c168c | 1996-12-05 23:15:02 +0000 | [diff] [blame] | 141 | if (y && y != -1) { |
| 142 | w_byte(TYPE_INT64, p); |
| 143 | w_long64(x, p); |
| 144 | } |
Guido van Rossum | c1547d9 | 1996-12-10 15:39:04 +0000 | [diff] [blame] | 145 | else |
| 146 | #endif |
| 147 | { |
Guido van Rossum | b0c168c | 1996-12-05 23:15:02 +0000 | [diff] [blame] | 148 | w_byte(TYPE_INT, p); |
| 149 | w_long(x, p); |
| 150 | } |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 151 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 152 | else if (PyLong_Check(v)) { |
| 153 | PyLongObject *ob = (PyLongObject *)v; |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 154 | w_byte(TYPE_LONG, p); |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 155 | n = ob->ob_size; |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 156 | w_long((long)n, p); |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 157 | if (n < 0) |
| 158 | n = -n; |
| 159 | for (i = 0; i < n; i++) |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 160 | w_short(ob->ob_digit[i], p); |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 161 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 162 | else if (PyFloat_Check(v)) { |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 163 | char buf[256]; /* Plenty to format any double */ |
Tim Peters | 72f98e9 | 2001-05-08 15:19:57 +0000 | [diff] [blame] | 164 | PyFloat_AsReprString(buf, (PyFloatObject *)v); |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 165 | n = strlen(buf); |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 166 | w_byte(TYPE_FLOAT, p); |
Guido van Rossum | 3a205f7 | 1995-02-17 15:10:07 +0000 | [diff] [blame] | 167 | w_byte(n, p); |
| 168 | w_string(buf, n, p); |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 169 | } |
Guido van Rossum | 8a5c5d2 | 1996-01-12 01:09:56 +0000 | [diff] [blame] | 170 | #ifndef WITHOUT_COMPLEX |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 171 | else if (PyComplex_Check(v)) { |
Guido van Rossum | 8a5c5d2 | 1996-01-12 01:09:56 +0000 | [diff] [blame] | 172 | char buf[256]; /* Plenty to format any double */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 173 | PyFloatObject *temp; |
Guido van Rossum | 8a5c5d2 | 1996-01-12 01:09:56 +0000 | [diff] [blame] | 174 | w_byte(TYPE_COMPLEX, p); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 175 | temp = (PyFloatObject*)PyFloat_FromDouble( |
| 176 | PyComplex_RealAsDouble(v)); |
Tim Peters | 72f98e9 | 2001-05-08 15:19:57 +0000 | [diff] [blame] | 177 | PyFloat_AsReprString(buf, temp); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 178 | Py_DECREF(temp); |
Guido van Rossum | 8a5c5d2 | 1996-01-12 01:09:56 +0000 | [diff] [blame] | 179 | n = strlen(buf); |
| 180 | w_byte(n, p); |
| 181 | w_string(buf, n, p); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 182 | temp = (PyFloatObject*)PyFloat_FromDouble( |
| 183 | PyComplex_ImagAsDouble(v)); |
Tim Peters | 72f98e9 | 2001-05-08 15:19:57 +0000 | [diff] [blame] | 184 | PyFloat_AsReprString(buf, temp); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 185 | Py_DECREF(temp); |
Guido van Rossum | 8a5c5d2 | 1996-01-12 01:09:56 +0000 | [diff] [blame] | 186 | n = strlen(buf); |
| 187 | w_byte(n, p); |
| 188 | w_string(buf, n, p); |
| 189 | } |
| 190 | #endif |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 191 | else if (PyString_Check(v)) { |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 192 | w_byte(TYPE_STRING, p); |
Guido van Rossum | c279b53 | 2000-03-10 23:03:02 +0000 | [diff] [blame] | 193 | n = PyString_GET_SIZE(v); |
Guido van Rossum | 3a205f7 | 1995-02-17 15:10:07 +0000 | [diff] [blame] | 194 | w_long((long)n, p); |
Guido van Rossum | c279b53 | 2000-03-10 23:03:02 +0000 | [diff] [blame] | 195 | w_string(PyString_AS_STRING(v), n, p); |
| 196 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 197 | #ifdef Py_USING_UNICODE |
Guido van Rossum | c279b53 | 2000-03-10 23:03:02 +0000 | [diff] [blame] | 198 | else if (PyUnicode_Check(v)) { |
| 199 | PyObject *utf8; |
| 200 | utf8 = PyUnicode_AsUTF8String(v); |
| 201 | if (utf8 == NULL) { |
Guido van Rossum | 98626cd | 2000-06-28 23:24:19 +0000 | [diff] [blame] | 202 | p->depth--; |
| 203 | p->error = 1; |
| 204 | return; |
Guido van Rossum | c279b53 | 2000-03-10 23:03:02 +0000 | [diff] [blame] | 205 | } |
| 206 | w_byte(TYPE_UNICODE, p); |
| 207 | n = PyString_GET_SIZE(utf8); |
| 208 | w_long((long)n, p); |
| 209 | w_string(PyString_AS_STRING(utf8), n, p); |
| 210 | Py_DECREF(utf8); |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 211 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 212 | #endif |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 213 | else if (PyTuple_Check(v)) { |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 214 | w_byte(TYPE_TUPLE, p); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 215 | n = PyTuple_Size(v); |
Guido van Rossum | 3a205f7 | 1995-02-17 15:10:07 +0000 | [diff] [blame] | 216 | w_long((long)n, p); |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 217 | for (i = 0; i < n; i++) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 218 | w_object(PyTuple_GET_ITEM(v, i), p); |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 219 | } |
| 220 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 221 | else if (PyList_Check(v)) { |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 222 | w_byte(TYPE_LIST, p); |
Guido van Rossum | c279b53 | 2000-03-10 23:03:02 +0000 | [diff] [blame] | 223 | n = PyList_GET_SIZE(v); |
Guido van Rossum | 3a205f7 | 1995-02-17 15:10:07 +0000 | [diff] [blame] | 224 | w_long((long)n, p); |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 225 | for (i = 0; i < n; i++) { |
Guido van Rossum | c279b53 | 2000-03-10 23:03:02 +0000 | [diff] [blame] | 226 | w_object(PyList_GET_ITEM(v, i), p); |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 227 | } |
| 228 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 229 | else if (PyDict_Check(v)) { |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 230 | int pos; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 231 | PyObject *key, *value; |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 232 | w_byte(TYPE_DICT, p); |
Guido van Rossum | 64b4552 | 1991-06-07 13:58:22 +0000 | [diff] [blame] | 233 | /* This one is NULL object terminated! */ |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 234 | pos = 0; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 235 | while (PyDict_Next(v, &pos, &key, &value)) { |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 236 | w_object(key, p); |
| 237 | w_object(value, p); |
Guido van Rossum | 64b4552 | 1991-06-07 13:58:22 +0000 | [diff] [blame] | 238 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 239 | w_object((PyObject *)NULL, p); |
Guido van Rossum | 64b4552 | 1991-06-07 13:58:22 +0000 | [diff] [blame] | 240 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 241 | else if (PyCode_Check(v)) { |
| 242 | PyCodeObject *co = (PyCodeObject *)v; |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 243 | w_byte(TYPE_CODE, p); |
Neal Norwitz | 7fdcb41 | 2002-06-14 01:07:39 +0000 | [diff] [blame] | 244 | w_long(co->co_argcount, p); |
| 245 | w_long(co->co_nlocals, p); |
| 246 | w_long(co->co_stacksize, p); |
| 247 | w_long(co->co_flags, p); |
Guido van Rossum | d076c73 | 1998-10-07 19:42:25 +0000 | [diff] [blame] | 248 | w_object(co->co_code, p); |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 249 | w_object(co->co_consts, p); |
| 250 | w_object(co->co_names, p); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 251 | w_object(co->co_varnames, p); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 252 | w_object(co->co_freevars, p); |
| 253 | w_object(co->co_cellvars, p); |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 254 | w_object(co->co_filename, p); |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 255 | w_object(co->co_name, p); |
Neal Norwitz | 7fdcb41 | 2002-06-14 01:07:39 +0000 | [diff] [blame] | 256 | w_long(co->co_firstlineno, p); |
Guido van Rossum | d031c89 | 1997-01-24 03:44:17 +0000 | [diff] [blame] | 257 | w_object(co->co_lnotab, p); |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 258 | } |
Jeremy Hylton | 9f64caa | 2001-11-09 22:02:48 +0000 | [diff] [blame] | 259 | else if (PyObject_CheckReadBuffer(v)) { |
Guido van Rossum | d076c73 | 1998-10-07 19:42:25 +0000 | [diff] [blame] | 260 | /* Write unknown buffer-style objects as a string */ |
| 261 | char *s; |
Jeremy Hylton | 9f64caa | 2001-11-09 22:02:48 +0000 | [diff] [blame] | 262 | PyBufferProcs *pb = v->ob_type->tp_as_buffer; |
Guido van Rossum | d076c73 | 1998-10-07 19:42:25 +0000 | [diff] [blame] | 263 | w_byte(TYPE_STRING, p); |
| 264 | n = (*pb->bf_getreadbuffer)(v, 0, (void **)&s); |
| 265 | w_long((long)n, p); |
| 266 | w_string(s, n, p); |
| 267 | } |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 268 | else { |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 269 | w_byte(TYPE_UNKNOWN, p); |
Guido van Rossum | f215060 | 1996-06-26 20:41:23 +0000 | [diff] [blame] | 270 | p->error = 1; |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 271 | } |
Guido van Rossum | 98626cd | 2000-06-28 23:24:19 +0000 | [diff] [blame] | 272 | |
| 273 | p->depth--; |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 274 | } |
| 275 | |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 276 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 277 | PyMarshal_WriteLongToFile(long x, FILE *fp) |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 278 | { |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 279 | WFILE wf; |
| 280 | wf.fp = fp; |
Guido van Rossum | f215060 | 1996-06-26 20:41:23 +0000 | [diff] [blame] | 281 | wf.error = 0; |
Fred Drake | 6da0b91 | 2000-06-28 18:47:56 +0000 | [diff] [blame] | 282 | wf.depth = 0; |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 283 | w_long(x, &wf); |
| 284 | } |
| 285 | |
| 286 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 287 | PyMarshal_WriteObjectToFile(PyObject *x, FILE *fp) |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 288 | { |
| 289 | WFILE wf; |
| 290 | wf.fp = fp; |
Guido van Rossum | f215060 | 1996-06-26 20:41:23 +0000 | [diff] [blame] | 291 | wf.error = 0; |
Guido van Rossum | 98626cd | 2000-06-28 23:24:19 +0000 | [diff] [blame] | 292 | wf.depth = 0; |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 293 | w_object(x, &wf); |
| 294 | } |
| 295 | |
| 296 | typedef WFILE RFILE; /* Same struct with different invariants */ |
| 297 | |
Guido van Rossum | 8d617a6 | 1995-03-09 12:12:11 +0000 | [diff] [blame] | 298 | #define rs_byte(p) (((p)->ptr != (p)->end) ? (unsigned char)*(p)->ptr++ : EOF) |
| 299 | |
| 300 | #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] | 301 | |
| 302 | static int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 303 | r_string(char *s, int n, RFILE *p) |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 304 | { |
| 305 | if (p->fp != NULL) |
| 306 | return fread(s, 1, n, p->fp); |
| 307 | if (p->end - p->ptr < n) |
| 308 | n = p->end - p->ptr; |
| 309 | memcpy(s, p->ptr, n); |
| 310 | p->ptr += n; |
| 311 | return n; |
| 312 | } |
| 313 | |
| 314 | static int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 315 | r_short(RFILE *p) |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 316 | { |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 317 | register short x; |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 318 | x = r_byte(p); |
| 319 | x |= r_byte(p) << 8; |
Tim Peters | e84b740 | 2000-09-19 08:54:13 +0000 | [diff] [blame] | 320 | /* Sign-extension, in case short greater than 16 bits */ |
| 321 | x |= -(x & 0x8000); |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 322 | return x; |
| 323 | } |
| 324 | |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 325 | static long |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 326 | r_long(RFILE *p) |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 327 | { |
| 328 | register long x; |
Guido van Rossum | 8d617a6 | 1995-03-09 12:12:11 +0000 | [diff] [blame] | 329 | register FILE *fp = p->fp; |
| 330 | if (fp) { |
| 331 | x = getc(fp); |
| 332 | x |= (long)getc(fp) << 8; |
| 333 | x |= (long)getc(fp) << 16; |
| 334 | x |= (long)getc(fp) << 24; |
| 335 | } |
| 336 | else { |
| 337 | x = rs_byte(p); |
| 338 | x |= (long)rs_byte(p) << 8; |
| 339 | x |= (long)rs_byte(p) << 16; |
| 340 | x |= (long)rs_byte(p) << 24; |
| 341 | } |
Guido van Rossum | c1547d9 | 1996-12-10 15:39:04 +0000 | [diff] [blame] | 342 | #if SIZEOF_LONG > 4 |
Guido van Rossum | b0c168c | 1996-12-05 23:15:02 +0000 | [diff] [blame] | 343 | /* Sign extension for 64-bit machines */ |
Tim Peters | e84b740 | 2000-09-19 08:54:13 +0000 | [diff] [blame] | 344 | x |= -(x & 0x80000000L); |
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 | |
Tim Peters | 8211237 | 2001-08-29 02:28:42 +0000 | [diff] [blame] | 349 | /* r_long64 deals with the TYPE_INT64 code. On a machine with |
| 350 | sizeof(long) > 4, it returns a Python int object, else a Python long |
| 351 | object. Note that w_long64 writes out TYPE_INT if 32 bits is enough, |
| 352 | so there's no inefficiency here in returning a PyLong on 32-bit boxes |
| 353 | for everything written via TYPE_INT64 (i.e., if an int is written via |
| 354 | TYPE_INT64, it *needs* more than 32 bits). |
| 355 | */ |
| 356 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 357 | r_long64(RFILE *p) |
Guido van Rossum | b0c168c | 1996-12-05 23:15:02 +0000 | [diff] [blame] | 358 | { |
Tim Peters | 8211237 | 2001-08-29 02:28:42 +0000 | [diff] [blame] | 359 | long lo4 = r_long(p); |
| 360 | long hi4 = r_long(p); |
Guido van Rossum | c1547d9 | 1996-12-10 15:39:04 +0000 | [diff] [blame] | 361 | #if SIZEOF_LONG > 4 |
Tim Peters | 8211237 | 2001-08-29 02:28:42 +0000 | [diff] [blame] | 362 | long x = (hi4 << 32) | (lo4 & 0xFFFFFFFFL); |
| 363 | return PyInt_FromLong(x); |
Guido van Rossum | c1547d9 | 1996-12-10 15:39:04 +0000 | [diff] [blame] | 364 | #else |
Tim Peters | 8211237 | 2001-08-29 02:28:42 +0000 | [diff] [blame] | 365 | unsigned char buf[8]; |
| 366 | int one = 1; |
| 367 | int is_little_endian = (int)*(char*)&one; |
| 368 | if (is_little_endian) { |
| 369 | memcpy(buf, &lo4, 4); |
| 370 | memcpy(buf+4, &hi4, 4); |
Guido van Rossum | b0c168c | 1996-12-05 23:15:02 +0000 | [diff] [blame] | 371 | } |
Tim Peters | 8211237 | 2001-08-29 02:28:42 +0000 | [diff] [blame] | 372 | else { |
| 373 | memcpy(buf, &hi4, 4); |
| 374 | memcpy(buf+4, &lo4, 4); |
| 375 | } |
| 376 | return _PyLong_FromByteArray(buf, 8, is_little_endian, 1); |
Guido van Rossum | c1547d9 | 1996-12-10 15:39:04 +0000 | [diff] [blame] | 377 | #endif |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 378 | } |
| 379 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 380 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 381 | r_object(RFILE *p) |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 382 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 383 | PyObject *v, *v2; |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 384 | long i, n; |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 385 | int type = r_byte(p); |
Tim Peters | d9b9ac8 | 2001-01-28 00:27:39 +0000 | [diff] [blame] | 386 | |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 387 | switch (type) { |
Tim Peters | d9b9ac8 | 2001-01-28 00:27:39 +0000 | [diff] [blame] | 388 | |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 389 | case EOF: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 390 | PyErr_SetString(PyExc_EOFError, |
| 391 | "EOF read where object expected"); |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 392 | return NULL; |
Tim Peters | d9b9ac8 | 2001-01-28 00:27:39 +0000 | [diff] [blame] | 393 | |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 394 | case TYPE_NULL: |
| 395 | return NULL; |
Tim Peters | d9b9ac8 | 2001-01-28 00:27:39 +0000 | [diff] [blame] | 396 | |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 397 | case TYPE_NONE: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 398 | Py_INCREF(Py_None); |
| 399 | return Py_None; |
Tim Peters | d9b9ac8 | 2001-01-28 00:27:39 +0000 | [diff] [blame] | 400 | |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 401 | case TYPE_STOPITER: |
| 402 | Py_INCREF(PyExc_StopIteration); |
| 403 | return PyExc_StopIteration; |
| 404 | |
Guido van Rossum | e449af7 | 1996-10-11 16:25:41 +0000 | [diff] [blame] | 405 | case TYPE_ELLIPSIS: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 406 | Py_INCREF(Py_Ellipsis); |
Guido van Rossum | e449af7 | 1996-10-11 16:25:41 +0000 | [diff] [blame] | 407 | return Py_Ellipsis; |
Tim Peters | d9b9ac8 | 2001-01-28 00:27:39 +0000 | [diff] [blame] | 408 | |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 409 | case TYPE_FALSE: |
| 410 | Py_INCREF(Py_False); |
| 411 | return Py_False; |
| 412 | |
| 413 | case TYPE_TRUE: |
| 414 | Py_INCREF(Py_True); |
| 415 | return Py_True; |
| 416 | |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 417 | case TYPE_INT: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 418 | return PyInt_FromLong(r_long(p)); |
Tim Peters | d9b9ac8 | 2001-01-28 00:27:39 +0000 | [diff] [blame] | 419 | |
Guido van Rossum | b0c168c | 1996-12-05 23:15:02 +0000 | [diff] [blame] | 420 | case TYPE_INT64: |
Tim Peters | 8211237 | 2001-08-29 02:28:42 +0000 | [diff] [blame] | 421 | return r_long64(p); |
Tim Peters | d9b9ac8 | 2001-01-28 00:27:39 +0000 | [diff] [blame] | 422 | |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 423 | case TYPE_LONG: |
| 424 | { |
| 425 | int size; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 426 | PyLongObject *ob; |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 427 | n = r_long(p); |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 428 | size = n<0 ? -n : n; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 429 | ob = _PyLong_New(size); |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 430 | if (ob == NULL) |
| 431 | return NULL; |
| 432 | ob->ob_size = n; |
| 433 | for (i = 0; i < size; i++) |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 434 | ob->ob_digit[i] = r_short(p); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 435 | return (PyObject *)ob; |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 436 | } |
Tim Peters | d9b9ac8 | 2001-01-28 00:27:39 +0000 | [diff] [blame] | 437 | |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 438 | case TYPE_FLOAT: |
| 439 | { |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 440 | char buf[256]; |
Guido van Rossum | 0ae748d | 1997-02-14 22:58:07 +0000 | [diff] [blame] | 441 | double dx; |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 442 | n = r_byte(p); |
| 443 | if (r_string(buf, (int)n, p) != n) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 444 | PyErr_SetString(PyExc_EOFError, |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 445 | "EOF read where object expected"); |
| 446 | return NULL; |
| 447 | } |
| 448 | buf[n] = '\0'; |
Guido van Rossum | 0ae748d | 1997-02-14 22:58:07 +0000 | [diff] [blame] | 449 | PyFPE_START_PROTECT("atof", return 0) |
| 450 | dx = atof(buf); |
Guido van Rossum | 45b8391 | 1997-03-14 04:32:50 +0000 | [diff] [blame] | 451 | PyFPE_END_PROTECT(dx) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 452 | return PyFloat_FromDouble(dx); |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 453 | } |
Tim Peters | d9b9ac8 | 2001-01-28 00:27:39 +0000 | [diff] [blame] | 454 | |
Guido van Rossum | 8a5c5d2 | 1996-01-12 01:09:56 +0000 | [diff] [blame] | 455 | #ifndef WITHOUT_COMPLEX |
| 456 | case TYPE_COMPLEX: |
| 457 | { |
Guido van Rossum | 8a5c5d2 | 1996-01-12 01:09:56 +0000 | [diff] [blame] | 458 | char buf[256]; |
Guido van Rossum | 530956d | 1996-07-21 02:27:43 +0000 | [diff] [blame] | 459 | Py_complex c; |
Guido van Rossum | 8a5c5d2 | 1996-01-12 01:09:56 +0000 | [diff] [blame] | 460 | n = r_byte(p); |
| 461 | if (r_string(buf, (int)n, p) != n) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 462 | PyErr_SetString(PyExc_EOFError, |
Guido van Rossum | 8a5c5d2 | 1996-01-12 01:09:56 +0000 | [diff] [blame] | 463 | "EOF read where object expected"); |
| 464 | return NULL; |
| 465 | } |
| 466 | buf[n] = '\0'; |
Guido van Rossum | 0ae748d | 1997-02-14 22:58:07 +0000 | [diff] [blame] | 467 | PyFPE_START_PROTECT("atof", return 0) |
Guido van Rossum | 8a5c5d2 | 1996-01-12 01:09:56 +0000 | [diff] [blame] | 468 | c.real = atof(buf); |
Guido van Rossum | 45b8391 | 1997-03-14 04:32:50 +0000 | [diff] [blame] | 469 | PyFPE_END_PROTECT(c) |
Guido van Rossum | 8a5c5d2 | 1996-01-12 01:09:56 +0000 | [diff] [blame] | 470 | n = r_byte(p); |
| 471 | if (r_string(buf, (int)n, p) != n) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 472 | PyErr_SetString(PyExc_EOFError, |
Guido van Rossum | 8a5c5d2 | 1996-01-12 01:09:56 +0000 | [diff] [blame] | 473 | "EOF read where object expected"); |
| 474 | return NULL; |
| 475 | } |
| 476 | buf[n] = '\0'; |
Guido van Rossum | 0ae748d | 1997-02-14 22:58:07 +0000 | [diff] [blame] | 477 | PyFPE_START_PROTECT("atof", return 0) |
Guido van Rossum | 8a5c5d2 | 1996-01-12 01:09:56 +0000 | [diff] [blame] | 478 | c.imag = atof(buf); |
Guido van Rossum | 45b8391 | 1997-03-14 04:32:50 +0000 | [diff] [blame] | 479 | PyFPE_END_PROTECT(c) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 480 | return PyComplex_FromCComplex(c); |
Guido van Rossum | 8a5c5d2 | 1996-01-12 01:09:56 +0000 | [diff] [blame] | 481 | } |
| 482 | #endif |
Tim Peters | d9b9ac8 | 2001-01-28 00:27:39 +0000 | [diff] [blame] | 483 | |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 484 | case TYPE_STRING: |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 485 | n = r_long(p); |
Guido van Rossum | a45cb45 | 1998-06-08 20:27:29 +0000 | [diff] [blame] | 486 | if (n < 0) { |
| 487 | PyErr_SetString(PyExc_ValueError, "bad marshal data"); |
| 488 | return NULL; |
| 489 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 490 | v = PyString_FromStringAndSize((char *)NULL, n); |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 491 | if (v != NULL) { |
Guido van Rossum | c279b53 | 2000-03-10 23:03:02 +0000 | [diff] [blame] | 492 | if (r_string(PyString_AS_STRING(v), (int)n, p) != n) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 493 | Py_DECREF(v); |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 494 | v = NULL; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 495 | PyErr_SetString(PyExc_EOFError, |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 496 | "EOF read where object expected"); |
| 497 | } |
| 498 | } |
| 499 | return v; |
Tim Peters | d9b9ac8 | 2001-01-28 00:27:39 +0000 | [diff] [blame] | 500 | |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 501 | #ifdef Py_USING_UNICODE |
Guido van Rossum | c279b53 | 2000-03-10 23:03:02 +0000 | [diff] [blame] | 502 | case TYPE_UNICODE: |
| 503 | { |
| 504 | char *buffer; |
| 505 | |
| 506 | n = r_long(p); |
| 507 | if (n < 0) { |
| 508 | PyErr_SetString(PyExc_ValueError, "bad marshal data"); |
| 509 | return NULL; |
| 510 | } |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 511 | buffer = PyMem_NEW(char, n); |
Guido van Rossum | c279b53 | 2000-03-10 23:03:02 +0000 | [diff] [blame] | 512 | if (buffer == NULL) |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 513 | return PyErr_NoMemory(); |
Guido van Rossum | c279b53 | 2000-03-10 23:03:02 +0000 | [diff] [blame] | 514 | if (r_string(buffer, (int)n, p) != n) { |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 515 | PyMem_DEL(buffer); |
Guido van Rossum | c279b53 | 2000-03-10 23:03:02 +0000 | [diff] [blame] | 516 | PyErr_SetString(PyExc_EOFError, |
| 517 | "EOF read where object expected"); |
| 518 | return NULL; |
| 519 | } |
| 520 | v = PyUnicode_DecodeUTF8(buffer, n, NULL); |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 521 | PyMem_DEL(buffer); |
Guido van Rossum | c279b53 | 2000-03-10 23:03:02 +0000 | [diff] [blame] | 522 | return v; |
| 523 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 524 | #endif |
Tim Peters | d9b9ac8 | 2001-01-28 00:27:39 +0000 | [diff] [blame] | 525 | |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 526 | case TYPE_TUPLE: |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 527 | n = r_long(p); |
Guido van Rossum | a45cb45 | 1998-06-08 20:27:29 +0000 | [diff] [blame] | 528 | if (n < 0) { |
| 529 | PyErr_SetString(PyExc_ValueError, "bad marshal data"); |
| 530 | return NULL; |
| 531 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 532 | v = PyTuple_New((int)n); |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 533 | if (v == NULL) |
| 534 | return v; |
Jack Jansen | 9513f2c | 1995-10-27 13:21:28 +0000 | [diff] [blame] | 535 | for (i = 0; i < n; i++) { |
| 536 | v2 = r_object(p); |
| 537 | if ( v2 == NULL ) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 538 | Py_DECREF(v); |
Jack Jansen | 9513f2c | 1995-10-27 13:21:28 +0000 | [diff] [blame] | 539 | v = NULL; |
| 540 | break; |
| 541 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 542 | PyTuple_SET_ITEM(v, (int)i, v2); |
Jack Jansen | 9513f2c | 1995-10-27 13:21:28 +0000 | [diff] [blame] | 543 | } |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 544 | return v; |
Tim Peters | d9b9ac8 | 2001-01-28 00:27:39 +0000 | [diff] [blame] | 545 | |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 546 | case TYPE_LIST: |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 547 | n = r_long(p); |
Guido van Rossum | a45cb45 | 1998-06-08 20:27:29 +0000 | [diff] [blame] | 548 | if (n < 0) { |
| 549 | PyErr_SetString(PyExc_ValueError, "bad marshal data"); |
| 550 | return NULL; |
| 551 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 552 | v = PyList_New((int)n); |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 553 | if (v == NULL) |
| 554 | return v; |
Jack Jansen | 9513f2c | 1995-10-27 13:21:28 +0000 | [diff] [blame] | 555 | for (i = 0; i < n; i++) { |
| 556 | v2 = r_object(p); |
| 557 | if ( v2 == NULL ) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 558 | Py_DECREF(v); |
Jack Jansen | 9513f2c | 1995-10-27 13:21:28 +0000 | [diff] [blame] | 559 | v = NULL; |
| 560 | break; |
| 561 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 562 | PyList_SetItem(v, (int)i, v2); |
Jack Jansen | 9513f2c | 1995-10-27 13:21:28 +0000 | [diff] [blame] | 563 | } |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 564 | return v; |
Tim Peters | d9b9ac8 | 2001-01-28 00:27:39 +0000 | [diff] [blame] | 565 | |
Guido van Rossum | 64b4552 | 1991-06-07 13:58:22 +0000 | [diff] [blame] | 566 | case TYPE_DICT: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 567 | v = PyDict_New(); |
Guido van Rossum | 64b4552 | 1991-06-07 13:58:22 +0000 | [diff] [blame] | 568 | if (v == NULL) |
| 569 | return NULL; |
| 570 | for (;;) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 571 | PyObject *key, *val; |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 572 | key = r_object(p); |
Guido van Rossum | 64b4552 | 1991-06-07 13:58:22 +0000 | [diff] [blame] | 573 | if (key == NULL) |
Guido van Rossum | f215060 | 1996-06-26 20:41:23 +0000 | [diff] [blame] | 574 | break; /* XXX Assume TYPE_NULL, not an error */ |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 575 | val = r_object(p); |
Guido van Rossum | f215060 | 1996-06-26 20:41:23 +0000 | [diff] [blame] | 576 | if (val != NULL) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 577 | PyDict_SetItem(v, key, val); |
| 578 | Py_DECREF(key); |
| 579 | Py_XDECREF(val); |
Guido van Rossum | 64b4552 | 1991-06-07 13:58:22 +0000 | [diff] [blame] | 580 | } |
| 581 | return v; |
Tim Peters | d9b9ac8 | 2001-01-28 00:27:39 +0000 | [diff] [blame] | 582 | |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 583 | case TYPE_CODE: |
Michael W. Hudson | 8019913 | 2001-08-30 14:50:20 +0000 | [diff] [blame] | 584 | if (PyEval_GetRestricted()) { |
| 585 | PyErr_SetString(PyExc_RuntimeError, |
| 586 | "cannot unmarshal code objects in " |
| 587 | "restricted execution mode"); |
| 588 | return NULL; |
| 589 | } |
| 590 | else { |
Neal Norwitz | 7fdcb41 | 2002-06-14 01:07:39 +0000 | [diff] [blame] | 591 | int argcount = r_long(p); |
| 592 | int nlocals = r_long(p); |
| 593 | int stacksize = r_long(p); |
| 594 | int flags = r_long(p); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 595 | PyObject *code = NULL; |
| 596 | PyObject *consts = NULL; |
| 597 | PyObject *names = NULL; |
| 598 | PyObject *varnames = NULL; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 599 | PyObject *freevars = NULL; |
| 600 | PyObject *cellvars = NULL; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 601 | PyObject *filename = NULL; |
| 602 | PyObject *name = NULL; |
Guido van Rossum | 0f4bbd2 | 1997-02-14 21:12:56 +0000 | [diff] [blame] | 603 | int firstlineno = 0; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 604 | PyObject *lnotab = NULL; |
Tim Peters | d9b9ac8 | 2001-01-28 00:27:39 +0000 | [diff] [blame] | 605 | |
Jack Jansen | 9513f2c | 1995-10-27 13:21:28 +0000 | [diff] [blame] | 606 | code = r_object(p); |
| 607 | if (code) consts = r_object(p); |
| 608 | if (consts) names = r_object(p); |
| 609 | if (names) varnames = r_object(p); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 610 | if (varnames) freevars = r_object(p); |
| 611 | if (freevars) cellvars = r_object(p); |
| 612 | if (cellvars) filename = r_object(p); |
Jack Jansen | 9513f2c | 1995-10-27 13:21:28 +0000 | [diff] [blame] | 613 | if (filename) name = r_object(p); |
Guido van Rossum | d031c89 | 1997-01-24 03:44:17 +0000 | [diff] [blame] | 614 | if (name) { |
Neal Norwitz | 7fdcb41 | 2002-06-14 01:07:39 +0000 | [diff] [blame] | 615 | firstlineno = r_long(p); |
Guido van Rossum | d031c89 | 1997-01-24 03:44:17 +0000 | [diff] [blame] | 616 | lnotab = r_object(p); |
| 617 | } |
Tim Peters | d9b9ac8 | 2001-01-28 00:27:39 +0000 | [diff] [blame] | 618 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 619 | if (!PyErr_Occurred()) { |
| 620 | v = (PyObject *) PyCode_New( |
Tim Peters | d9b9ac8 | 2001-01-28 00:27:39 +0000 | [diff] [blame] | 621 | argcount, nlocals, stacksize, flags, |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 622 | code, consts, names, varnames, |
Tim Peters | d9b9ac8 | 2001-01-28 00:27:39 +0000 | [diff] [blame] | 623 | freevars, cellvars, filename, name, |
| 624 | firstlineno, lnotab); |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 625 | } |
| 626 | else |
| 627 | v = NULL; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 628 | Py_XDECREF(code); |
| 629 | Py_XDECREF(consts); |
| 630 | Py_XDECREF(names); |
| 631 | Py_XDECREF(varnames); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 632 | Py_XDECREF(freevars); |
| 633 | Py_XDECREF(cellvars); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 634 | Py_XDECREF(filename); |
| 635 | Py_XDECREF(name); |
Guido van Rossum | 6fc06e7 | 1997-07-26 23:30:18 +0000 | [diff] [blame] | 636 | Py_XDECREF(lnotab); |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 637 | |
| 638 | } |
| 639 | return v; |
Tim Peters | d9b9ac8 | 2001-01-28 00:27:39 +0000 | [diff] [blame] | 640 | |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 641 | default: |
Guido van Rossum | f215060 | 1996-06-26 20:41:23 +0000 | [diff] [blame] | 642 | /* Bogus data got written, which isn't ideal. |
| 643 | This will let you keep working and recover. */ |
Guido van Rossum | a45cb45 | 1998-06-08 20:27:29 +0000 | [diff] [blame] | 644 | PyErr_SetString(PyExc_ValueError, "bad marshal data"); |
| 645 | return NULL; |
Tim Peters | d9b9ac8 | 2001-01-28 00:27:39 +0000 | [diff] [blame] | 646 | |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 647 | } |
| 648 | } |
| 649 | |
Guido van Rossum | b8cf3e6 | 2001-10-19 01:46:21 +0000 | [diff] [blame] | 650 | int |
| 651 | PyMarshal_ReadShortFromFile(FILE *fp) |
| 652 | { |
| 653 | RFILE rf; |
| 654 | rf.fp = fp; |
| 655 | return r_short(&rf); |
| 656 | } |
| 657 | |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 658 | long |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 659 | PyMarshal_ReadLongFromFile(FILE *fp) |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 660 | { |
| 661 | RFILE rf; |
| 662 | rf.fp = fp; |
| 663 | return r_long(&rf); |
| 664 | } |
| 665 | |
Tim Peters | 691e0e9 | 2001-01-18 04:39:16 +0000 | [diff] [blame] | 666 | #ifdef HAVE_FSTAT |
| 667 | /* Return size of file in bytes; < 0 if unknown. */ |
| 668 | static off_t |
| 669 | getfilesize(FILE *fp) |
| 670 | { |
| 671 | struct stat st; |
| 672 | if (fstat(fileno(fp), &st) != 0) |
| 673 | return -1; |
| 674 | else |
| 675 | return st.st_size; |
| 676 | } |
| 677 | #endif |
Tim Peters | d9b9ac8 | 2001-01-28 00:27:39 +0000 | [diff] [blame] | 678 | |
Tim Peters | 691e0e9 | 2001-01-18 04:39:16 +0000 | [diff] [blame] | 679 | /* If we can get the size of the file up-front, and it's reasonably small, |
| 680 | * read it in one gulp and delegate to ...FromString() instead. Much quicker |
| 681 | * than reading a byte at a time from file; speeds .pyc imports. |
Tim Peters | d9b9ac8 | 2001-01-28 00:27:39 +0000 | [diff] [blame] | 682 | * CAUTION: since this may read the entire remainder of the file, don't |
| 683 | * call it unless you know you're done with the file. |
Tim Peters | 691e0e9 | 2001-01-18 04:39:16 +0000 | [diff] [blame] | 684 | */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 685 | PyObject * |
Tim Peters | d9b9ac8 | 2001-01-28 00:27:39 +0000 | [diff] [blame] | 686 | PyMarshal_ReadLastObjectFromFile(FILE *fp) |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 687 | { |
Tim Peters | 691e0e9 | 2001-01-18 04:39:16 +0000 | [diff] [blame] | 688 | /* 75% of 2.1's .pyc files can exploit SMALL_FILE_LIMIT. |
| 689 | * REASONABLE_FILE_LIMIT is by defn something big enough for Tkinter.pyc. |
| 690 | */ |
| 691 | #define SMALL_FILE_LIMIT (1L << 14) |
| 692 | #define REASONABLE_FILE_LIMIT (1L << 18) |
Tim Peters | 691e0e9 | 2001-01-18 04:39:16 +0000 | [diff] [blame] | 693 | #ifdef HAVE_FSTAT |
| 694 | off_t filesize; |
| 695 | #endif |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 696 | if (PyErr_Occurred()) { |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 697 | fprintf(stderr, "XXX rd_object called with exception set\n"); |
| 698 | return NULL; |
| 699 | } |
Tim Peters | 691e0e9 | 2001-01-18 04:39:16 +0000 | [diff] [blame] | 700 | #ifdef HAVE_FSTAT |
| 701 | filesize = getfilesize(fp); |
| 702 | if (filesize > 0) { |
| 703 | char buf[SMALL_FILE_LIMIT]; |
| 704 | char* pBuf = NULL; |
| 705 | if (filesize <= SMALL_FILE_LIMIT) |
| 706 | pBuf = buf; |
| 707 | else if (filesize <= REASONABLE_FILE_LIMIT) |
| 708 | pBuf = (char *)PyMem_MALLOC(filesize); |
| 709 | if (pBuf != NULL) { |
| 710 | PyObject* v; |
| 711 | size_t n = fread(pBuf, 1, filesize, fp); |
| 712 | v = PyMarshal_ReadObjectFromString(pBuf, n); |
| 713 | if (pBuf != buf) |
| 714 | PyMem_FREE(pBuf); |
| 715 | return v; |
| 716 | } |
Tim Peters | d9b9ac8 | 2001-01-28 00:27:39 +0000 | [diff] [blame] | 717 | |
Tim Peters | 691e0e9 | 2001-01-18 04:39:16 +0000 | [diff] [blame] | 718 | } |
| 719 | #endif |
Tim Peters | d9b9ac8 | 2001-01-28 00:27:39 +0000 | [diff] [blame] | 720 | /* We don't have fstat, or we do but the file is larger than |
| 721 | * REASONABLE_FILE_LIMIT or malloc failed -- read a byte at a time. |
| 722 | */ |
| 723 | return PyMarshal_ReadObjectFromFile(fp); |
| 724 | |
Tim Peters | 691e0e9 | 2001-01-18 04:39:16 +0000 | [diff] [blame] | 725 | #undef SMALL_FILE_LIMIT |
| 726 | #undef REASONABLE_FILE_LIMIT |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 727 | } |
| 728 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 729 | PyObject * |
Tim Peters | d9b9ac8 | 2001-01-28 00:27:39 +0000 | [diff] [blame] | 730 | PyMarshal_ReadObjectFromFile(FILE *fp) |
| 731 | { |
| 732 | RFILE rf; |
| 733 | if (PyErr_Occurred()) { |
| 734 | fprintf(stderr, "XXX rd_object called with exception set\n"); |
| 735 | return NULL; |
| 736 | } |
| 737 | rf.fp = fp; |
| 738 | return r_object(&rf); |
| 739 | } |
| 740 | |
| 741 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 742 | PyMarshal_ReadObjectFromString(char *str, int len) |
Guido van Rossum | f56e3db | 1993-04-01 20:59:32 +0000 | [diff] [blame] | 743 | { |
| 744 | RFILE rf; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 745 | if (PyErr_Occurred()) { |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 746 | fprintf(stderr, "XXX rds_object called with exception set\n"); |
| 747 | return NULL; |
| 748 | } |
Guido van Rossum | f56e3db | 1993-04-01 20:59:32 +0000 | [diff] [blame] | 749 | rf.fp = NULL; |
| 750 | rf.str = NULL; |
| 751 | rf.ptr = str; |
| 752 | rf.end = str + len; |
| 753 | return r_object(&rf); |
| 754 | } |
| 755 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 756 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 757 | PyMarshal_WriteObjectToString(PyObject *x) /* wrs_object() */ |
Guido van Rossum | 3f3bb3d | 1996-08-19 22:07:17 +0000 | [diff] [blame] | 758 | { |
| 759 | WFILE wf; |
| 760 | wf.fp = NULL; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 761 | wf.str = PyString_FromStringAndSize((char *)NULL, 50); |
Guido van Rossum | 3f3bb3d | 1996-08-19 22:07:17 +0000 | [diff] [blame] | 762 | if (wf.str == NULL) |
| 763 | return NULL; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 764 | wf.ptr = PyString_AS_STRING((PyStringObject *)wf.str); |
| 765 | wf.end = wf.ptr + PyString_Size(wf.str); |
Guido van Rossum | 3f3bb3d | 1996-08-19 22:07:17 +0000 | [diff] [blame] | 766 | wf.error = 0; |
Fred Drake | 6da0b91 | 2000-06-28 18:47:56 +0000 | [diff] [blame] | 767 | wf.depth = 0; |
Guido van Rossum | 3f3bb3d | 1996-08-19 22:07:17 +0000 | [diff] [blame] | 768 | w_object(x, &wf); |
| 769 | if (wf.str != NULL) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 770 | _PyString_Resize(&wf.str, |
| 771 | (int) (wf.ptr - |
| 772 | PyString_AS_STRING((PyStringObject *)wf.str))); |
Guido van Rossum | 3f3bb3d | 1996-08-19 22:07:17 +0000 | [diff] [blame] | 773 | if (wf.error) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 774 | Py_XDECREF(wf.str); |
Tim Peters | d9b9ac8 | 2001-01-28 00:27:39 +0000 | [diff] [blame] | 775 | PyErr_SetString(PyExc_ValueError, |
Fred Drake | 6da0b91 | 2000-06-28 18:47:56 +0000 | [diff] [blame] | 776 | (wf.error==1)?"unmarshallable object" |
| 777 | :"object too deeply nested to marshal"); |
Guido van Rossum | 3f3bb3d | 1996-08-19 22:07:17 +0000 | [diff] [blame] | 778 | return NULL; |
| 779 | } |
| 780 | return wf.str; |
| 781 | } |
| 782 | |
Guido van Rossum | 64b4552 | 1991-06-07 13:58:22 +0000 | [diff] [blame] | 783 | /* And an interface for Python programs... */ |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 784 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 785 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 786 | marshal_dump(PyObject *self, PyObject *args) |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 787 | { |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 788 | WFILE wf; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 789 | PyObject *x; |
| 790 | PyObject *f; |
Guido van Rossum | 2efa369 | 2000-03-31 00:37:41 +0000 | [diff] [blame] | 791 | if (!PyArg_ParseTuple(args, "OO:dump", &x, &f)) |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 792 | return NULL; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 793 | if (!PyFile_Check(f)) { |
| 794 | PyErr_SetString(PyExc_TypeError, |
| 795 | "marshal.dump() 2nd arg must be file"); |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 796 | return NULL; |
| 797 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 798 | wf.fp = PyFile_AsFile(f); |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 799 | wf.str = NULL; |
| 800 | wf.ptr = wf.end = NULL; |
Guido van Rossum | f215060 | 1996-06-26 20:41:23 +0000 | [diff] [blame] | 801 | wf.error = 0; |
Fred Drake | 6da0b91 | 2000-06-28 18:47:56 +0000 | [diff] [blame] | 802 | wf.depth = 0; |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 803 | w_object(x, &wf); |
Guido van Rossum | f215060 | 1996-06-26 20:41:23 +0000 | [diff] [blame] | 804 | if (wf.error) { |
Tim Peters | d9b9ac8 | 2001-01-28 00:27:39 +0000 | [diff] [blame] | 805 | PyErr_SetString(PyExc_ValueError, |
Fred Drake | 6da0b91 | 2000-06-28 18:47:56 +0000 | [diff] [blame] | 806 | (wf.error==1)?"unmarshallable object" |
| 807 | :"object too deeply nested to marshal"); |
Guido van Rossum | f215060 | 1996-06-26 20:41:23 +0000 | [diff] [blame] | 808 | return NULL; |
| 809 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 810 | Py_INCREF(Py_None); |
| 811 | return Py_None; |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 812 | } |
| 813 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 814 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 815 | marshal_load(PyObject *self, PyObject *args) |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 816 | { |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 817 | RFILE rf; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 818 | PyObject *f; |
| 819 | PyObject *v; |
Guido van Rossum | 2efa369 | 2000-03-31 00:37:41 +0000 | [diff] [blame] | 820 | if (!PyArg_ParseTuple(args, "O:load", &f)) |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 821 | return NULL; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 822 | if (!PyFile_Check(f)) { |
| 823 | PyErr_SetString(PyExc_TypeError, |
| 824 | "marshal.load() arg must be file"); |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 825 | return NULL; |
| 826 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 827 | rf.fp = PyFile_AsFile(f); |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 828 | rf.str = NULL; |
| 829 | rf.ptr = rf.end = NULL; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 830 | PyErr_Clear(); |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 831 | v = r_object(&rf); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 832 | if (PyErr_Occurred()) { |
| 833 | Py_XDECREF(v); |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 834 | v = NULL; |
| 835 | } |
| 836 | return v; |
| 837 | } |
| 838 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 839 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 840 | marshal_dumps(PyObject *self, PyObject *args) |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 841 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 842 | PyObject *x; |
Guido van Rossum | 2efa369 | 2000-03-31 00:37:41 +0000 | [diff] [blame] | 843 | if (!PyArg_ParseTuple(args, "O:dumps", &x)) |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 844 | return NULL; |
Guido van Rossum | 3f3bb3d | 1996-08-19 22:07:17 +0000 | [diff] [blame] | 845 | return PyMarshal_WriteObjectToString(x); |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 846 | } |
| 847 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 848 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 849 | marshal_loads(PyObject *self, PyObject *args) |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 850 | { |
| 851 | RFILE rf; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 852 | PyObject *v; |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 853 | char *s; |
| 854 | int n; |
Guido van Rossum | 2efa369 | 2000-03-31 00:37:41 +0000 | [diff] [blame] | 855 | if (!PyArg_ParseTuple(args, "s#:loads", &s, &n)) |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 856 | return NULL; |
| 857 | rf.fp = NULL; |
| 858 | rf.str = args; |
| 859 | rf.ptr = s; |
| 860 | rf.end = s + n; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 861 | PyErr_Clear(); |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 862 | v = r_object(&rf); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 863 | if (PyErr_Occurred()) { |
| 864 | Py_XDECREF(v); |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 865 | v = NULL; |
| 866 | } |
| 867 | return v; |
| 868 | } |
| 869 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 870 | static PyMethodDef marshal_methods[] = { |
Neal Norwitz | 031829d | 2002-03-31 14:37:44 +0000 | [diff] [blame] | 871 | {"dump", marshal_dump, METH_VARARGS}, |
| 872 | {"load", marshal_load, METH_VARARGS}, |
| 873 | {"dumps", marshal_dumps, METH_VARARGS}, |
| 874 | {"loads", marshal_loads, METH_VARARGS}, |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 875 | {NULL, NULL} /* sentinel */ |
| 876 | }; |
| 877 | |
Jason Tishler | 6bc06ec | 2003-09-04 11:59:50 +0000 | [diff] [blame] | 878 | PyMODINIT_FUNC |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 879 | PyMarshal_Init(void) |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 880 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 881 | (void) Py_InitModule("marshal", marshal_methods); |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 882 | } |