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