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 | |
Thomas Wouters | 695934a | 2006-03-01 23:49:13 +0000 | [diff] [blame] | 7 | #define PY_SSIZE_T_CLEAN |
| 8 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 9 | #include "Python.h" |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 10 | #include "longintrepr.h" |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 11 | #include "code.h" |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 12 | #include "marshal.h" |
| 13 | |
Mark Dickinson | efc82f7 | 2009-03-20 15:51:55 +0000 | [diff] [blame] | 14 | #define ABS(x) ((x) < 0 ? -(x) : (x)) |
| 15 | |
Fred Drake | 6da0b91 | 2000-06-28 18:47:56 +0000 | [diff] [blame] | 16 | /* High water mark to determine when the marshalled object is dangerously deep |
| 17 | * and risks coring the interpreter. When the object stack gets this deep, |
| 18 | * raise an exception instead of continuing. |
Benjamin Peterson | 64c8275 | 2016-07-06 23:37:02 -0700 | [diff] [blame] | 19 | * On Windows debug builds, reduce this value. |
Fred Drake | 6da0b91 | 2000-06-28 18:47:56 +0000 | [diff] [blame] | 20 | */ |
Benjamin Peterson | 64c8275 | 2016-07-06 23:37:02 -0700 | [diff] [blame] | 21 | #if defined(MS_WINDOWS) && defined(_DEBUG) |
| 22 | #define MAX_MARSHAL_STACK_DEPTH 1000 |
| 23 | #else |
Neal Norwitz | f6b0e4d | 2007-05-17 07:04:46 +0000 | [diff] [blame] | 24 | #define MAX_MARSHAL_STACK_DEPTH 2000 |
Benjamin Peterson | 64c8275 | 2016-07-06 23:37:02 -0700 | [diff] [blame] | 25 | #endif |
Fred Drake | 6da0b91 | 2000-06-28 18:47:56 +0000 | [diff] [blame] | 26 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 27 | #define TYPE_NULL '0' |
| 28 | #define TYPE_NONE 'N' |
| 29 | #define TYPE_FALSE 'F' |
| 30 | #define TYPE_TRUE 'T' |
| 31 | #define TYPE_STOPITER 'S' |
| 32 | #define TYPE_ELLIPSIS '.' |
| 33 | #define TYPE_INT 'i' |
| 34 | #define TYPE_INT64 'I' |
| 35 | #define TYPE_FLOAT 'f' |
| 36 | #define TYPE_BINARY_FLOAT 'g' |
| 37 | #define TYPE_COMPLEX 'x' |
| 38 | #define TYPE_BINARY_COMPLEX 'y' |
| 39 | #define TYPE_LONG 'l' |
| 40 | #define TYPE_STRING 's' |
| 41 | #define TYPE_INTERNED 't' |
| 42 | #define TYPE_STRINGREF 'R' |
| 43 | #define TYPE_TUPLE '(' |
| 44 | #define TYPE_LIST '[' |
| 45 | #define TYPE_DICT '{' |
| 46 | #define TYPE_CODE 'c' |
| 47 | #define TYPE_UNICODE 'u' |
| 48 | #define TYPE_UNKNOWN '?' |
| 49 | #define TYPE_SET '<' |
| 50 | #define TYPE_FROZENSET '>' |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 51 | |
Eric Smith | 1566927 | 2009-10-19 00:34:12 +0000 | [diff] [blame] | 52 | #define WFERR_OK 0 |
| 53 | #define WFERR_UNMARSHALLABLE 1 |
| 54 | #define WFERR_NESTEDTOODEEP 2 |
| 55 | #define WFERR_NOMEMORY 3 |
| 56 | |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 57 | typedef struct { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 58 | FILE *fp; |
| 59 | int error; /* see WFERR_* values */ |
| 60 | int depth; |
| 61 | /* If fp == NULL, the following are valid: */ |
| 62 | PyObject *str; |
| 63 | char *ptr; |
| 64 | char *end; |
| 65 | PyObject *strings; /* dict on marshal, list on unmarshal */ |
| 66 | int version; |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 67 | } WFILE; |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 68 | |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 69 | #define w_byte(c, p) if (((p)->fp)) putc((c), (p)->fp); \ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 70 | else if ((p)->ptr != (p)->end) *(p)->ptr++ = (c); \ |
| 71 | else w_more(c, p) |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 72 | |
| 73 | static void |
Fredrik Lundh | 1153438 | 2000-07-23 18:24:06 +0000 | [diff] [blame] | 74 | w_more(int c, WFILE *p) |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 75 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 76 | Py_ssize_t size, newsize; |
| 77 | if (p->str == NULL) |
| 78 | return; /* An error already occurred */ |
| 79 | size = PyString_Size(p->str); |
| 80 | newsize = size + size + 1024; |
| 81 | if (newsize > 32*1024*1024) { |
| 82 | newsize = size + (size >> 3); /* 12.5% overallocation */ |
| 83 | } |
| 84 | if (_PyString_Resize(&p->str, newsize) != 0) { |
| 85 | p->ptr = p->end = NULL; |
| 86 | } |
| 87 | else { |
| 88 | p->ptr = PyString_AS_STRING((PyStringObject *)p->str) + size; |
| 89 | p->end = |
| 90 | PyString_AS_STRING((PyStringObject *)p->str) + newsize; |
| 91 | *p->ptr++ = Py_SAFE_DOWNCAST(c, int, char); |
| 92 | } |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | static void |
Serhiy Storchaka | fe2e839 | 2013-07-11 19:14:07 +0300 | [diff] [blame] | 96 | w_string(const char *s, Py_ssize_t n, WFILE *p) |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 97 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 98 | if (p->fp != NULL) { |
| 99 | fwrite(s, 1, n, p->fp); |
| 100 | } |
| 101 | else { |
| 102 | while (--n >= 0) { |
| 103 | w_byte(*s, p); |
| 104 | s++; |
| 105 | } |
| 106 | } |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 110 | w_short(int x, WFILE *p) |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 111 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 112 | w_byte((char)( x & 0xff), p); |
| 113 | w_byte((char)((x>> 8) & 0xff), p); |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 114 | } |
| 115 | |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 116 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 117 | w_long(long x, WFILE *p) |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 118 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 119 | w_byte((char)( x & 0xff), p); |
| 120 | w_byte((char)((x>> 8) & 0xff), p); |
| 121 | w_byte((char)((x>>16) & 0xff), p); |
| 122 | w_byte((char)((x>>24) & 0xff), p); |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 123 | } |
| 124 | |
Guido van Rossum | c1547d9 | 1996-12-10 15:39:04 +0000 | [diff] [blame] | 125 | #if SIZEOF_LONG > 4 |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 126 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 127 | w_long64(long x, WFILE *p) |
Guido van Rossum | b0c168c | 1996-12-05 23:15:02 +0000 | [diff] [blame] | 128 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 129 | w_long(x, p); |
| 130 | w_long(x>>32, p); |
Guido van Rossum | b0c168c | 1996-12-05 23:15:02 +0000 | [diff] [blame] | 131 | } |
Guido van Rossum | c1547d9 | 1996-12-10 15:39:04 +0000 | [diff] [blame] | 132 | #endif |
Guido van Rossum | b0c168c | 1996-12-05 23:15:02 +0000 | [diff] [blame] | 133 | |
Serhiy Storchaka | 34fe1b7 | 2013-02-13 12:07:43 +0200 | [diff] [blame] | 134 | #define SIZE32_MAX 0x7FFFFFFF |
| 135 | |
| 136 | #if SIZEOF_SIZE_T > 4 |
| 137 | # define W_SIZE(n, p) do { \ |
| 138 | if ((n) > SIZE32_MAX) { \ |
| 139 | (p)->depth--; \ |
| 140 | (p)->error = WFERR_UNMARSHALLABLE; \ |
| 141 | return; \ |
| 142 | } \ |
| 143 | w_long((long)(n), p); \ |
| 144 | } while(0) |
| 145 | #else |
| 146 | # define W_SIZE w_long |
| 147 | #endif |
| 148 | |
Serhiy Storchaka | fe2e839 | 2013-07-11 19:14:07 +0300 | [diff] [blame] | 149 | static void |
| 150 | w_pstring(const char *s, Py_ssize_t n, WFILE *p) |
| 151 | { |
| 152 | W_SIZE(n, p); |
| 153 | w_string(s, n, p); |
| 154 | } |
| 155 | |
Mark Dickinson | efc82f7 | 2009-03-20 15:51:55 +0000 | [diff] [blame] | 156 | /* We assume that Python longs are stored internally in base some power of |
| 157 | 2**15; for the sake of portability we'll always read and write them in base |
| 158 | exactly 2**15. */ |
| 159 | |
| 160 | #define PyLong_MARSHAL_SHIFT 15 |
| 161 | #define PyLong_MARSHAL_BASE ((short)1 << PyLong_MARSHAL_SHIFT) |
| 162 | #define PyLong_MARSHAL_MASK (PyLong_MARSHAL_BASE - 1) |
| 163 | #if PyLong_SHIFT % PyLong_MARSHAL_SHIFT != 0 |
| 164 | #error "PyLong_SHIFT must be a multiple of PyLong_MARSHAL_SHIFT" |
| 165 | #endif |
| 166 | #define PyLong_MARSHAL_RATIO (PyLong_SHIFT / PyLong_MARSHAL_SHIFT) |
| 167 | |
| 168 | static void |
| 169 | w_PyLong(const PyLongObject *ob, WFILE *p) |
| 170 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 171 | Py_ssize_t i, j, n, l; |
| 172 | digit d; |
Mark Dickinson | efc82f7 | 2009-03-20 15:51:55 +0000 | [diff] [blame] | 173 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 174 | w_byte(TYPE_LONG, p); |
| 175 | if (Py_SIZE(ob) == 0) { |
| 176 | w_long((long)0, p); |
| 177 | return; |
| 178 | } |
Mark Dickinson | efc82f7 | 2009-03-20 15:51:55 +0000 | [diff] [blame] | 179 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 180 | /* set l to number of base PyLong_MARSHAL_BASE digits */ |
| 181 | n = ABS(Py_SIZE(ob)); |
| 182 | l = (n-1) * PyLong_MARSHAL_RATIO; |
| 183 | d = ob->ob_digit[n-1]; |
| 184 | assert(d != 0); /* a PyLong is always normalized */ |
| 185 | do { |
| 186 | d >>= PyLong_MARSHAL_SHIFT; |
| 187 | l++; |
| 188 | } while (d != 0); |
Serhiy Storchaka | 34fe1b7 | 2013-02-13 12:07:43 +0200 | [diff] [blame] | 189 | if (l > SIZE32_MAX) { |
| 190 | p->depth--; |
| 191 | p->error = WFERR_UNMARSHALLABLE; |
| 192 | return; |
| 193 | } |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 194 | w_long((long)(Py_SIZE(ob) > 0 ? l : -l), p); |
Mark Dickinson | efc82f7 | 2009-03-20 15:51:55 +0000 | [diff] [blame] | 195 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 196 | for (i=0; i < n-1; i++) { |
| 197 | d = ob->ob_digit[i]; |
| 198 | for (j=0; j < PyLong_MARSHAL_RATIO; j++) { |
| 199 | w_short(d & PyLong_MARSHAL_MASK, p); |
| 200 | d >>= PyLong_MARSHAL_SHIFT; |
| 201 | } |
| 202 | assert (d == 0); |
| 203 | } |
| 204 | d = ob->ob_digit[n-1]; |
| 205 | do { |
| 206 | w_short(d & PyLong_MARSHAL_MASK, p); |
| 207 | d >>= PyLong_MARSHAL_SHIFT; |
| 208 | } while (d != 0); |
Mark Dickinson | efc82f7 | 2009-03-20 15:51:55 +0000 | [diff] [blame] | 209 | } |
| 210 | |
Guido van Rossum | b0c168c | 1996-12-05 23:15:02 +0000 | [diff] [blame] | 211 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 212 | w_object(PyObject *v, WFILE *p) |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 213 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 214 | Py_ssize_t i, n; |
Fred Drake | 6da0b91 | 2000-06-28 18:47:56 +0000 | [diff] [blame] | 215 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 216 | p->depth++; |
Tim Peters | d9b9ac8 | 2001-01-28 00:27:39 +0000 | [diff] [blame] | 217 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 218 | if (p->depth > MAX_MARSHAL_STACK_DEPTH) { |
| 219 | p->error = WFERR_NESTEDTOODEEP; |
| 220 | } |
| 221 | else if (v == NULL) { |
| 222 | w_byte(TYPE_NULL, p); |
| 223 | } |
| 224 | else if (v == Py_None) { |
| 225 | w_byte(TYPE_NONE, p); |
| 226 | } |
| 227 | else if (v == PyExc_StopIteration) { |
| 228 | w_byte(TYPE_STOPITER, p); |
| 229 | } |
| 230 | else if (v == Py_Ellipsis) { |
| 231 | w_byte(TYPE_ELLIPSIS, p); |
| 232 | } |
| 233 | else if (v == Py_False) { |
| 234 | w_byte(TYPE_FALSE, p); |
| 235 | } |
| 236 | else if (v == Py_True) { |
| 237 | w_byte(TYPE_TRUE, p); |
| 238 | } |
| 239 | else if (PyInt_CheckExact(v)) { |
| 240 | long x = PyInt_AS_LONG((PyIntObject *)v); |
Guido van Rossum | c1547d9 | 1996-12-10 15:39:04 +0000 | [diff] [blame] | 241 | #if SIZEOF_LONG > 4 |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 242 | long y = Py_ARITHMETIC_RIGHT_SHIFT(long, x, 31); |
| 243 | if (y && y != -1) { |
| 244 | w_byte(TYPE_INT64, p); |
| 245 | w_long64(x, p); |
| 246 | } |
| 247 | else |
Guido van Rossum | c1547d9 | 1996-12-10 15:39:04 +0000 | [diff] [blame] | 248 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 249 | { |
| 250 | w_byte(TYPE_INT, p); |
| 251 | w_long(x, p); |
| 252 | } |
| 253 | } |
| 254 | else if (PyLong_CheckExact(v)) { |
| 255 | PyLongObject *ob = (PyLongObject *)v; |
| 256 | w_PyLong(ob, p); |
| 257 | } |
| 258 | else if (PyFloat_CheckExact(v)) { |
| 259 | if (p->version > 1) { |
| 260 | unsigned char buf[8]; |
| 261 | if (_PyFloat_Pack8(PyFloat_AsDouble(v), |
| 262 | buf, 1) < 0) { |
| 263 | p->error = WFERR_UNMARSHALLABLE; |
| 264 | return; |
| 265 | } |
| 266 | w_byte(TYPE_BINARY_FLOAT, p); |
| 267 | w_string((char*)buf, 8, p); |
| 268 | } |
| 269 | else { |
| 270 | char *buf = PyOS_double_to_string(PyFloat_AS_DOUBLE(v), |
| 271 | 'g', 17, 0, NULL); |
| 272 | if (!buf) { |
| 273 | p->error = WFERR_NOMEMORY; |
| 274 | return; |
| 275 | } |
| 276 | n = strlen(buf); |
| 277 | w_byte(TYPE_FLOAT, p); |
| 278 | w_byte((int)n, p); |
Serhiy Storchaka | 34fe1b7 | 2013-02-13 12:07:43 +0200 | [diff] [blame] | 279 | w_string(buf, n, p); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 280 | PyMem_Free(buf); |
| 281 | } |
| 282 | } |
Guido van Rossum | 8a5c5d2 | 1996-01-12 01:09:56 +0000 | [diff] [blame] | 283 | #ifndef WITHOUT_COMPLEX |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 284 | else if (PyComplex_CheckExact(v)) { |
| 285 | if (p->version > 1) { |
| 286 | unsigned char buf[8]; |
| 287 | if (_PyFloat_Pack8(PyComplex_RealAsDouble(v), |
| 288 | buf, 1) < 0) { |
| 289 | p->error = WFERR_UNMARSHALLABLE; |
| 290 | return; |
| 291 | } |
| 292 | w_byte(TYPE_BINARY_COMPLEX, p); |
| 293 | w_string((char*)buf, 8, p); |
| 294 | if (_PyFloat_Pack8(PyComplex_ImagAsDouble(v), |
| 295 | buf, 1) < 0) { |
| 296 | p->error = WFERR_UNMARSHALLABLE; |
| 297 | return; |
| 298 | } |
| 299 | w_string((char*)buf, 8, p); |
| 300 | } |
| 301 | else { |
| 302 | char *buf; |
| 303 | w_byte(TYPE_COMPLEX, p); |
| 304 | buf = PyOS_double_to_string(PyComplex_RealAsDouble(v), |
| 305 | 'g', 17, 0, NULL); |
| 306 | if (!buf) { |
| 307 | p->error = WFERR_NOMEMORY; |
| 308 | return; |
| 309 | } |
| 310 | n = strlen(buf); |
| 311 | w_byte((int)n, p); |
Serhiy Storchaka | 34fe1b7 | 2013-02-13 12:07:43 +0200 | [diff] [blame] | 312 | w_string(buf, n, p); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 313 | PyMem_Free(buf); |
| 314 | buf = PyOS_double_to_string(PyComplex_ImagAsDouble(v), |
| 315 | 'g', 17, 0, NULL); |
| 316 | if (!buf) { |
| 317 | p->error = WFERR_NOMEMORY; |
| 318 | return; |
| 319 | } |
| 320 | n = strlen(buf); |
| 321 | w_byte((int)n, p); |
Serhiy Storchaka | 34fe1b7 | 2013-02-13 12:07:43 +0200 | [diff] [blame] | 322 | w_string(buf, n, p); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 323 | PyMem_Free(buf); |
| 324 | } |
| 325 | } |
Guido van Rossum | 8a5c5d2 | 1996-01-12 01:09:56 +0000 | [diff] [blame] | 326 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 327 | else if (PyString_CheckExact(v)) { |
| 328 | if (p->strings && PyString_CHECK_INTERNED(v)) { |
| 329 | PyObject *o = PyDict_GetItem(p->strings, v); |
| 330 | if (o) { |
| 331 | long w = PyInt_AsLong(o); |
| 332 | w_byte(TYPE_STRINGREF, p); |
| 333 | w_long(w, p); |
| 334 | goto exit; |
| 335 | } |
| 336 | else { |
| 337 | int ok; |
| 338 | o = PyInt_FromSsize_t(PyDict_Size(p->strings)); |
| 339 | ok = o && |
| 340 | PyDict_SetItem(p->strings, v, o) >= 0; |
| 341 | Py_XDECREF(o); |
| 342 | if (!ok) { |
| 343 | p->depth--; |
| 344 | p->error = WFERR_UNMARSHALLABLE; |
| 345 | return; |
| 346 | } |
| 347 | w_byte(TYPE_INTERNED, p); |
| 348 | } |
| 349 | } |
| 350 | else { |
| 351 | w_byte(TYPE_STRING, p); |
| 352 | } |
Serhiy Storchaka | fe2e839 | 2013-07-11 19:14:07 +0300 | [diff] [blame] | 353 | w_pstring(PyBytes_AS_STRING(v), PyString_GET_SIZE(v), p); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 354 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 355 | #ifdef Py_USING_UNICODE |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 356 | else if (PyUnicode_CheckExact(v)) { |
| 357 | PyObject *utf8; |
| 358 | utf8 = PyUnicode_AsUTF8String(v); |
| 359 | if (utf8 == NULL) { |
| 360 | p->depth--; |
| 361 | p->error = WFERR_UNMARSHALLABLE; |
| 362 | return; |
| 363 | } |
| 364 | w_byte(TYPE_UNICODE, p); |
Serhiy Storchaka | fe2e839 | 2013-07-11 19:14:07 +0300 | [diff] [blame] | 365 | w_pstring(PyString_AS_STRING(utf8), PyString_GET_SIZE(utf8), p); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 366 | Py_DECREF(utf8); |
| 367 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 368 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 369 | else if (PyTuple_CheckExact(v)) { |
| 370 | w_byte(TYPE_TUPLE, p); |
| 371 | n = PyTuple_Size(v); |
Serhiy Storchaka | 34fe1b7 | 2013-02-13 12:07:43 +0200 | [diff] [blame] | 372 | W_SIZE(n, p); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 373 | for (i = 0; i < n; i++) { |
| 374 | w_object(PyTuple_GET_ITEM(v, i), p); |
| 375 | } |
| 376 | } |
| 377 | else if (PyList_CheckExact(v)) { |
| 378 | w_byte(TYPE_LIST, p); |
| 379 | n = PyList_GET_SIZE(v); |
Serhiy Storchaka | 34fe1b7 | 2013-02-13 12:07:43 +0200 | [diff] [blame] | 380 | W_SIZE(n, p); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 381 | for (i = 0; i < n; i++) { |
| 382 | w_object(PyList_GET_ITEM(v, i), p); |
| 383 | } |
| 384 | } |
| 385 | else if (PyDict_CheckExact(v)) { |
| 386 | Py_ssize_t pos; |
| 387 | PyObject *key, *value; |
| 388 | w_byte(TYPE_DICT, p); |
| 389 | /* This one is NULL object terminated! */ |
| 390 | pos = 0; |
| 391 | while (PyDict_Next(v, &pos, &key, &value)) { |
| 392 | w_object(key, p); |
| 393 | w_object(value, p); |
| 394 | } |
| 395 | w_object((PyObject *)NULL, p); |
| 396 | } |
| 397 | else if (PyAnySet_CheckExact(v)) { |
| 398 | PyObject *value, *it; |
Raymond Hettinger | a422c34 | 2005-01-11 03:03:27 +0000 | [diff] [blame] | 399 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 400 | if (PyObject_TypeCheck(v, &PySet_Type)) |
| 401 | w_byte(TYPE_SET, p); |
| 402 | else |
| 403 | w_byte(TYPE_FROZENSET, p); |
| 404 | n = PyObject_Size(v); |
| 405 | if (n == -1) { |
| 406 | p->depth--; |
| 407 | p->error = WFERR_UNMARSHALLABLE; |
| 408 | return; |
| 409 | } |
Serhiy Storchaka | 34fe1b7 | 2013-02-13 12:07:43 +0200 | [diff] [blame] | 410 | W_SIZE(n, p); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 411 | it = PyObject_GetIter(v); |
| 412 | if (it == NULL) { |
| 413 | p->depth--; |
| 414 | p->error = WFERR_UNMARSHALLABLE; |
| 415 | return; |
| 416 | } |
| 417 | while ((value = PyIter_Next(it)) != NULL) { |
| 418 | w_object(value, p); |
| 419 | Py_DECREF(value); |
| 420 | } |
| 421 | Py_DECREF(it); |
| 422 | if (PyErr_Occurred()) { |
| 423 | p->depth--; |
| 424 | p->error = WFERR_UNMARSHALLABLE; |
| 425 | return; |
| 426 | } |
| 427 | } |
| 428 | else if (PyCode_Check(v)) { |
| 429 | PyCodeObject *co = (PyCodeObject *)v; |
| 430 | w_byte(TYPE_CODE, p); |
| 431 | w_long(co->co_argcount, p); |
| 432 | w_long(co->co_nlocals, p); |
| 433 | w_long(co->co_stacksize, p); |
| 434 | w_long(co->co_flags, p); |
| 435 | w_object(co->co_code, p); |
| 436 | w_object(co->co_consts, p); |
| 437 | w_object(co->co_names, p); |
| 438 | w_object(co->co_varnames, p); |
| 439 | w_object(co->co_freevars, p); |
| 440 | w_object(co->co_cellvars, p); |
| 441 | w_object(co->co_filename, p); |
| 442 | w_object(co->co_name, p); |
| 443 | w_long(co->co_firstlineno, p); |
| 444 | w_object(co->co_lnotab, p); |
| 445 | } |
| 446 | else if (PyObject_CheckReadBuffer(v)) { |
| 447 | /* Write unknown buffer-style objects as a string */ |
| 448 | char *s; |
| 449 | PyBufferProcs *pb = v->ob_type->tp_as_buffer; |
| 450 | w_byte(TYPE_STRING, p); |
| 451 | n = (*pb->bf_getreadbuffer)(v, 0, (void **)&s); |
Serhiy Storchaka | fe2e839 | 2013-07-11 19:14:07 +0300 | [diff] [blame] | 452 | w_pstring(s, n, p); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 453 | } |
| 454 | else { |
| 455 | w_byte(TYPE_UNKNOWN, p); |
| 456 | p->error = WFERR_UNMARSHALLABLE; |
| 457 | } |
Martin v. Löwis | ef82d2f | 2004-06-27 16:51:46 +0000 | [diff] [blame] | 458 | exit: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 459 | p->depth--; |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 460 | } |
| 461 | |
Martin v. Löwis | ef82d2f | 2004-06-27 16:51:46 +0000 | [diff] [blame] | 462 | /* version currently has no effect for writing longs. */ |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 463 | void |
Martin v. Löwis | ef82d2f | 2004-06-27 16:51:46 +0000 | [diff] [blame] | 464 | PyMarshal_WriteLongToFile(long x, FILE *fp, int version) |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 465 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 466 | WFILE wf; |
| 467 | wf.fp = fp; |
Benjamin Peterson | 88d5e2c | 2017-10-24 23:09:55 -0700 | [diff] [blame] | 468 | wf.str = NULL; |
| 469 | wf.ptr = NULL; |
| 470 | wf.end = NULL; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 471 | wf.error = WFERR_OK; |
| 472 | wf.depth = 0; |
| 473 | wf.strings = NULL; |
| 474 | wf.version = version; |
| 475 | w_long(x, &wf); |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 476 | } |
| 477 | |
| 478 | void |
Martin v. Löwis | ef82d2f | 2004-06-27 16:51:46 +0000 | [diff] [blame] | 479 | PyMarshal_WriteObjectToFile(PyObject *x, FILE *fp, int version) |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 480 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 481 | WFILE wf; |
| 482 | wf.fp = fp; |
Benjamin Peterson | 88d5e2c | 2017-10-24 23:09:55 -0700 | [diff] [blame] | 483 | wf.str = NULL; |
| 484 | wf.ptr = NULL; |
| 485 | wf.end = NULL; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 486 | wf.error = WFERR_OK; |
| 487 | wf.depth = 0; |
| 488 | wf.strings = (version > 0) ? PyDict_New() : NULL; |
| 489 | wf.version = version; |
| 490 | w_object(x, &wf); |
| 491 | Py_XDECREF(wf.strings); |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 492 | } |
| 493 | |
| 494 | typedef WFILE RFILE; /* Same struct with different invariants */ |
| 495 | |
Neal Norwitz | b1a9b37 | 2007-05-16 20:05:11 +0000 | [diff] [blame] | 496 | #define rs_byte(p) (((p)->ptr < (p)->end) ? (unsigned char)*(p)->ptr++ : EOF) |
Guido van Rossum | 8d617a6 | 1995-03-09 12:12:11 +0000 | [diff] [blame] | 497 | |
| 498 | #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] | 499 | |
Serhiy Storchaka | 34fe1b7 | 2013-02-13 12:07:43 +0200 | [diff] [blame] | 500 | static Py_ssize_t |
| 501 | r_string(char *s, Py_ssize_t n, RFILE *p) |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 502 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 503 | if (p->fp != NULL) |
| 504 | /* The result fits into int because it must be <=n. */ |
Serhiy Storchaka | 34fe1b7 | 2013-02-13 12:07:43 +0200 | [diff] [blame] | 505 | return fread(s, 1, n, p->fp); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 506 | if (p->end - p->ptr < n) |
Serhiy Storchaka | 34fe1b7 | 2013-02-13 12:07:43 +0200 | [diff] [blame] | 507 | n = p->end - p->ptr; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 508 | memcpy(s, p->ptr, n); |
| 509 | p->ptr += n; |
| 510 | return n; |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 511 | } |
| 512 | |
| 513 | static int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 514 | r_short(RFILE *p) |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 515 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 516 | register short x; |
| 517 | x = r_byte(p); |
| 518 | x |= r_byte(p) << 8; |
| 519 | /* Sign-extension, in case short greater than 16 bits */ |
| 520 | x |= -(x & 0x8000); |
| 521 | return x; |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 522 | } |
| 523 | |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 524 | static long |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 525 | r_long(RFILE *p) |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 526 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 527 | register long x; |
| 528 | register FILE *fp = p->fp; |
| 529 | if (fp) { |
| 530 | x = getc(fp); |
| 531 | x |= (long)getc(fp) << 8; |
| 532 | x |= (long)getc(fp) << 16; |
| 533 | x |= (long)getc(fp) << 24; |
| 534 | } |
| 535 | else { |
| 536 | x = rs_byte(p); |
| 537 | x |= (long)rs_byte(p) << 8; |
| 538 | x |= (long)rs_byte(p) << 16; |
| 539 | x |= (long)rs_byte(p) << 24; |
| 540 | } |
Guido van Rossum | c1547d9 | 1996-12-10 15:39:04 +0000 | [diff] [blame] | 541 | #if SIZEOF_LONG > 4 |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 542 | /* Sign extension for 64-bit machines */ |
| 543 | x |= -(x & 0x80000000L); |
Guido van Rossum | c1547d9 | 1996-12-10 15:39:04 +0000 | [diff] [blame] | 544 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 545 | return x; |
Guido van Rossum | b0c168c | 1996-12-05 23:15:02 +0000 | [diff] [blame] | 546 | } |
| 547 | |
Tim Peters | 8211237 | 2001-08-29 02:28:42 +0000 | [diff] [blame] | 548 | /* r_long64 deals with the TYPE_INT64 code. On a machine with |
| 549 | sizeof(long) > 4, it returns a Python int object, else a Python long |
| 550 | object. Note that w_long64 writes out TYPE_INT if 32 bits is enough, |
| 551 | so there's no inefficiency here in returning a PyLong on 32-bit boxes |
| 552 | for everything written via TYPE_INT64 (i.e., if an int is written via |
| 553 | TYPE_INT64, it *needs* more than 32 bits). |
| 554 | */ |
| 555 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 556 | r_long64(RFILE *p) |
Guido van Rossum | b0c168c | 1996-12-05 23:15:02 +0000 | [diff] [blame] | 557 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 558 | long lo4 = r_long(p); |
| 559 | long hi4 = r_long(p); |
Guido van Rossum | c1547d9 | 1996-12-10 15:39:04 +0000 | [diff] [blame] | 560 | #if SIZEOF_LONG > 4 |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 561 | long x = (hi4 << 32) | (lo4 & 0xFFFFFFFFL); |
| 562 | return PyInt_FromLong(x); |
Guido van Rossum | c1547d9 | 1996-12-10 15:39:04 +0000 | [diff] [blame] | 563 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 564 | unsigned char buf[8]; |
| 565 | int one = 1; |
| 566 | int is_little_endian = (int)*(char*)&one; |
| 567 | if (is_little_endian) { |
| 568 | memcpy(buf, &lo4, 4); |
| 569 | memcpy(buf+4, &hi4, 4); |
| 570 | } |
| 571 | else { |
| 572 | memcpy(buf, &hi4, 4); |
| 573 | memcpy(buf+4, &lo4, 4); |
| 574 | } |
| 575 | return _PyLong_FromByteArray(buf, 8, is_little_endian, 1); |
Guido van Rossum | c1547d9 | 1996-12-10 15:39:04 +0000 | [diff] [blame] | 576 | #endif |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 577 | } |
| 578 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 579 | static PyObject * |
Mark Dickinson | efc82f7 | 2009-03-20 15:51:55 +0000 | [diff] [blame] | 580 | r_PyLong(RFILE *p) |
| 581 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 582 | PyLongObject *ob; |
Serhiy Storchaka | 34fe1b7 | 2013-02-13 12:07:43 +0200 | [diff] [blame] | 583 | long n, size, i; |
| 584 | int j, md, shorts_in_top_digit; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 585 | digit d; |
Mark Dickinson | efc82f7 | 2009-03-20 15:51:55 +0000 | [diff] [blame] | 586 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 587 | n = r_long(p); |
| 588 | if (n == 0) |
| 589 | return (PyObject *)_PyLong_New(0); |
Serhiy Storchaka | 34fe1b7 | 2013-02-13 12:07:43 +0200 | [diff] [blame] | 590 | if (n < -SIZE32_MAX || n > SIZE32_MAX) { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 591 | PyErr_SetString(PyExc_ValueError, |
| 592 | "bad marshal data (long size out of range)"); |
| 593 | return NULL; |
| 594 | } |
Mark Dickinson | efc82f7 | 2009-03-20 15:51:55 +0000 | [diff] [blame] | 595 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 596 | size = 1 + (ABS(n) - 1) / PyLong_MARSHAL_RATIO; |
| 597 | shorts_in_top_digit = 1 + (ABS(n) - 1) % PyLong_MARSHAL_RATIO; |
| 598 | ob = _PyLong_New(size); |
| 599 | if (ob == NULL) |
| 600 | return NULL; |
| 601 | Py_SIZE(ob) = n > 0 ? size : -size; |
Mark Dickinson | efc82f7 | 2009-03-20 15:51:55 +0000 | [diff] [blame] | 602 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 603 | for (i = 0; i < size-1; i++) { |
| 604 | d = 0; |
| 605 | for (j=0; j < PyLong_MARSHAL_RATIO; j++) { |
| 606 | md = r_short(p); |
| 607 | if (md < 0 || md > PyLong_MARSHAL_BASE) |
| 608 | goto bad_digit; |
| 609 | d += (digit)md << j*PyLong_MARSHAL_SHIFT; |
| 610 | } |
| 611 | ob->ob_digit[i] = d; |
| 612 | } |
| 613 | d = 0; |
| 614 | for (j=0; j < shorts_in_top_digit; j++) { |
| 615 | md = r_short(p); |
| 616 | if (md < 0 || md > PyLong_MARSHAL_BASE) |
| 617 | goto bad_digit; |
| 618 | /* topmost marshal digit should be nonzero */ |
| 619 | if (md == 0 && j == shorts_in_top_digit - 1) { |
| 620 | Py_DECREF(ob); |
| 621 | PyErr_SetString(PyExc_ValueError, |
| 622 | "bad marshal data (unnormalized long data)"); |
| 623 | return NULL; |
| 624 | } |
| 625 | d += (digit)md << j*PyLong_MARSHAL_SHIFT; |
| 626 | } |
| 627 | /* top digit should be nonzero, else the resulting PyLong won't be |
| 628 | normalized */ |
| 629 | ob->ob_digit[size-1] = d; |
| 630 | return (PyObject *)ob; |
Mark Dickinson | efc82f7 | 2009-03-20 15:51:55 +0000 | [diff] [blame] | 631 | bad_digit: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 632 | Py_DECREF(ob); |
| 633 | PyErr_SetString(PyExc_ValueError, |
| 634 | "bad marshal data (digit out of range in long)"); |
| 635 | return NULL; |
Mark Dickinson | efc82f7 | 2009-03-20 15:51:55 +0000 | [diff] [blame] | 636 | } |
| 637 | |
| 638 | |
| 639 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 640 | r_object(RFILE *p) |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 641 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 642 | /* NULL is a valid return value, it does not necessarily means that |
| 643 | an exception is set. */ |
| 644 | PyObject *v, *v2; |
| 645 | long i, n; |
| 646 | int type = r_byte(p); |
| 647 | PyObject *retval; |
Neal Norwitz | b1a9b37 | 2007-05-16 20:05:11 +0000 | [diff] [blame] | 648 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 649 | p->depth++; |
Neal Norwitz | b1a9b37 | 2007-05-16 20:05:11 +0000 | [diff] [blame] | 650 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 651 | if (p->depth > MAX_MARSHAL_STACK_DEPTH) { |
| 652 | p->depth--; |
| 653 | PyErr_SetString(PyExc_ValueError, "recursion limit exceeded"); |
| 654 | return NULL; |
| 655 | } |
Tim Peters | d9b9ac8 | 2001-01-28 00:27:39 +0000 | [diff] [blame] | 656 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 657 | switch (type) { |
Tim Peters | d9b9ac8 | 2001-01-28 00:27:39 +0000 | [diff] [blame] | 658 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 659 | case EOF: |
| 660 | PyErr_SetString(PyExc_EOFError, |
| 661 | "EOF read where object expected"); |
| 662 | retval = NULL; |
| 663 | break; |
Tim Peters | d9b9ac8 | 2001-01-28 00:27:39 +0000 | [diff] [blame] | 664 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 665 | case TYPE_NULL: |
| 666 | retval = NULL; |
| 667 | break; |
Tim Peters | d9b9ac8 | 2001-01-28 00:27:39 +0000 | [diff] [blame] | 668 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 669 | case TYPE_NONE: |
| 670 | Py_INCREF(Py_None); |
| 671 | retval = Py_None; |
| 672 | break; |
Tim Peters | d9b9ac8 | 2001-01-28 00:27:39 +0000 | [diff] [blame] | 673 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 674 | case TYPE_STOPITER: |
| 675 | Py_INCREF(PyExc_StopIteration); |
| 676 | retval = PyExc_StopIteration; |
| 677 | break; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 678 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 679 | case TYPE_ELLIPSIS: |
| 680 | Py_INCREF(Py_Ellipsis); |
| 681 | retval = Py_Ellipsis; |
| 682 | break; |
Tim Peters | d9b9ac8 | 2001-01-28 00:27:39 +0000 | [diff] [blame] | 683 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 684 | case TYPE_FALSE: |
| 685 | Py_INCREF(Py_False); |
| 686 | retval = Py_False; |
| 687 | break; |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 688 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 689 | case TYPE_TRUE: |
| 690 | Py_INCREF(Py_True); |
| 691 | retval = Py_True; |
| 692 | break; |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 693 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 694 | case TYPE_INT: |
| 695 | retval = PyInt_FromLong(r_long(p)); |
| 696 | break; |
Tim Peters | d9b9ac8 | 2001-01-28 00:27:39 +0000 | [diff] [blame] | 697 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 698 | case TYPE_INT64: |
| 699 | retval = r_long64(p); |
| 700 | break; |
Tim Peters | d9b9ac8 | 2001-01-28 00:27:39 +0000 | [diff] [blame] | 701 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 702 | case TYPE_LONG: |
| 703 | retval = r_PyLong(p); |
| 704 | break; |
Tim Peters | d9b9ac8 | 2001-01-28 00:27:39 +0000 | [diff] [blame] | 705 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 706 | case TYPE_FLOAT: |
| 707 | { |
| 708 | char buf[256]; |
| 709 | double dx; |
| 710 | n = r_byte(p); |
Serhiy Storchaka | 34fe1b7 | 2013-02-13 12:07:43 +0200 | [diff] [blame] | 711 | if (n == EOF || r_string(buf, n, p) != n) { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 712 | PyErr_SetString(PyExc_EOFError, |
| 713 | "EOF read where object expected"); |
| 714 | retval = NULL; |
| 715 | break; |
| 716 | } |
| 717 | buf[n] = '\0'; |
| 718 | dx = PyOS_string_to_double(buf, NULL, NULL); |
| 719 | if (dx == -1.0 && PyErr_Occurred()) { |
| 720 | retval = NULL; |
| 721 | break; |
| 722 | } |
| 723 | retval = PyFloat_FromDouble(dx); |
| 724 | break; |
| 725 | } |
Tim Peters | d9b9ac8 | 2001-01-28 00:27:39 +0000 | [diff] [blame] | 726 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 727 | case TYPE_BINARY_FLOAT: |
| 728 | { |
| 729 | unsigned char buf[8]; |
| 730 | double x; |
| 731 | if (r_string((char*)buf, 8, p) != 8) { |
| 732 | PyErr_SetString(PyExc_EOFError, |
| 733 | "EOF read where object expected"); |
| 734 | retval = NULL; |
| 735 | break; |
| 736 | } |
| 737 | x = _PyFloat_Unpack8(buf, 1); |
| 738 | if (x == -1.0 && PyErr_Occurred()) { |
| 739 | retval = NULL; |
| 740 | break; |
| 741 | } |
| 742 | retval = PyFloat_FromDouble(x); |
| 743 | break; |
| 744 | } |
Michael W. Hudson | df88846 | 2005-06-03 14:41:55 +0000 | [diff] [blame] | 745 | |
Guido van Rossum | 8a5c5d2 | 1996-01-12 01:09:56 +0000 | [diff] [blame] | 746 | #ifndef WITHOUT_COMPLEX |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 747 | case TYPE_COMPLEX: |
| 748 | { |
| 749 | char buf[256]; |
| 750 | Py_complex c; |
| 751 | n = r_byte(p); |
Serhiy Storchaka | 34fe1b7 | 2013-02-13 12:07:43 +0200 | [diff] [blame] | 752 | if (n == EOF || r_string(buf, n, p) != n) { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 753 | PyErr_SetString(PyExc_EOFError, |
| 754 | "EOF read where object expected"); |
| 755 | retval = NULL; |
| 756 | break; |
| 757 | } |
| 758 | buf[n] = '\0'; |
| 759 | c.real = PyOS_string_to_double(buf, NULL, NULL); |
| 760 | if (c.real == -1.0 && PyErr_Occurred()) { |
| 761 | retval = NULL; |
| 762 | break; |
| 763 | } |
| 764 | n = r_byte(p); |
Serhiy Storchaka | 34fe1b7 | 2013-02-13 12:07:43 +0200 | [diff] [blame] | 765 | if (n == EOF || r_string(buf, n, p) != n) { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 766 | PyErr_SetString(PyExc_EOFError, |
| 767 | "EOF read where object expected"); |
| 768 | retval = NULL; |
| 769 | break; |
| 770 | } |
| 771 | buf[n] = '\0'; |
| 772 | c.imag = PyOS_string_to_double(buf, NULL, NULL); |
| 773 | if (c.imag == -1.0 && PyErr_Occurred()) { |
| 774 | retval = NULL; |
| 775 | break; |
| 776 | } |
| 777 | retval = PyComplex_FromCComplex(c); |
| 778 | break; |
| 779 | } |
Michael W. Hudson | df88846 | 2005-06-03 14:41:55 +0000 | [diff] [blame] | 780 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 781 | case TYPE_BINARY_COMPLEX: |
| 782 | { |
| 783 | unsigned char buf[8]; |
| 784 | Py_complex c; |
| 785 | if (r_string((char*)buf, 8, p) != 8) { |
| 786 | PyErr_SetString(PyExc_EOFError, |
| 787 | "EOF read where object expected"); |
| 788 | retval = NULL; |
| 789 | break; |
| 790 | } |
| 791 | c.real = _PyFloat_Unpack8(buf, 1); |
| 792 | if (c.real == -1.0 && PyErr_Occurred()) { |
| 793 | retval = NULL; |
| 794 | break; |
| 795 | } |
| 796 | if (r_string((char*)buf, 8, p) != 8) { |
| 797 | PyErr_SetString(PyExc_EOFError, |
| 798 | "EOF read where object expected"); |
| 799 | retval = NULL; |
| 800 | break; |
| 801 | } |
| 802 | c.imag = _PyFloat_Unpack8(buf, 1); |
| 803 | if (c.imag == -1.0 && PyErr_Occurred()) { |
| 804 | retval = NULL; |
| 805 | break; |
| 806 | } |
| 807 | retval = PyComplex_FromCComplex(c); |
| 808 | break; |
| 809 | } |
Guido van Rossum | 8a5c5d2 | 1996-01-12 01:09:56 +0000 | [diff] [blame] | 810 | #endif |
Tim Peters | d9b9ac8 | 2001-01-28 00:27:39 +0000 | [diff] [blame] | 811 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 812 | case TYPE_INTERNED: |
| 813 | case TYPE_STRING: |
| 814 | n = r_long(p); |
Serhiy Storchaka | 34fe1b7 | 2013-02-13 12:07:43 +0200 | [diff] [blame] | 815 | if (n < 0 || n > SIZE32_MAX) { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 816 | PyErr_SetString(PyExc_ValueError, "bad marshal data (string size out of range)"); |
| 817 | retval = NULL; |
| 818 | break; |
| 819 | } |
| 820 | v = PyString_FromStringAndSize((char *)NULL, n); |
| 821 | if (v == NULL) { |
| 822 | retval = NULL; |
| 823 | break; |
| 824 | } |
Serhiy Storchaka | 34fe1b7 | 2013-02-13 12:07:43 +0200 | [diff] [blame] | 825 | if (r_string(PyString_AS_STRING(v), n, p) != n) { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 826 | Py_DECREF(v); |
| 827 | PyErr_SetString(PyExc_EOFError, |
| 828 | "EOF read where object expected"); |
| 829 | retval = NULL; |
| 830 | break; |
| 831 | } |
| 832 | if (type == TYPE_INTERNED) { |
| 833 | PyString_InternInPlace(&v); |
| 834 | if (PyList_Append(p->strings, v) < 0) { |
| 835 | retval = NULL; |
| 836 | break; |
| 837 | } |
| 838 | } |
| 839 | retval = v; |
| 840 | break; |
Martin v. Löwis | ef82d2f | 2004-06-27 16:51:46 +0000 | [diff] [blame] | 841 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 842 | case TYPE_STRINGREF: |
| 843 | n = r_long(p); |
| 844 | if (n < 0 || n >= PyList_GET_SIZE(p->strings)) { |
| 845 | PyErr_SetString(PyExc_ValueError, "bad marshal data (string ref out of range)"); |
| 846 | retval = NULL; |
| 847 | break; |
| 848 | } |
| 849 | v = PyList_GET_ITEM(p->strings, n); |
| 850 | Py_INCREF(v); |
| 851 | retval = v; |
| 852 | break; |
Tim Peters | d9b9ac8 | 2001-01-28 00:27:39 +0000 | [diff] [blame] | 853 | |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 854 | #ifdef Py_USING_UNICODE |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 855 | case TYPE_UNICODE: |
| 856 | { |
| 857 | char *buffer; |
Guido van Rossum | c279b53 | 2000-03-10 23:03:02 +0000 | [diff] [blame] | 858 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 859 | n = r_long(p); |
Serhiy Storchaka | 34fe1b7 | 2013-02-13 12:07:43 +0200 | [diff] [blame] | 860 | if (n < 0 || n > SIZE32_MAX) { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 861 | PyErr_SetString(PyExc_ValueError, "bad marshal data (unicode size out of range)"); |
| 862 | retval = NULL; |
| 863 | break; |
| 864 | } |
| 865 | buffer = PyMem_NEW(char, n); |
| 866 | if (buffer == NULL) { |
| 867 | retval = PyErr_NoMemory(); |
| 868 | break; |
| 869 | } |
Serhiy Storchaka | 34fe1b7 | 2013-02-13 12:07:43 +0200 | [diff] [blame] | 870 | if (r_string(buffer, n, p) != n) { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 871 | PyMem_DEL(buffer); |
| 872 | PyErr_SetString(PyExc_EOFError, |
| 873 | "EOF read where object expected"); |
| 874 | retval = NULL; |
| 875 | break; |
| 876 | } |
| 877 | v = PyUnicode_DecodeUTF8(buffer, n, NULL); |
| 878 | PyMem_DEL(buffer); |
| 879 | retval = v; |
| 880 | break; |
| 881 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 882 | #endif |
Tim Peters | d9b9ac8 | 2001-01-28 00:27:39 +0000 | [diff] [blame] | 883 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 884 | case TYPE_TUPLE: |
| 885 | n = r_long(p); |
Serhiy Storchaka | 34fe1b7 | 2013-02-13 12:07:43 +0200 | [diff] [blame] | 886 | if (n < 0 || n > SIZE32_MAX) { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 887 | PyErr_SetString(PyExc_ValueError, "bad marshal data (tuple size out of range)"); |
| 888 | retval = NULL; |
| 889 | break; |
| 890 | } |
Serhiy Storchaka | 34fe1b7 | 2013-02-13 12:07:43 +0200 | [diff] [blame] | 891 | v = PyTuple_New(n); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 892 | if (v == NULL) { |
| 893 | retval = NULL; |
| 894 | break; |
| 895 | } |
| 896 | for (i = 0; i < n; i++) { |
| 897 | v2 = r_object(p); |
| 898 | if ( v2 == NULL ) { |
| 899 | if (!PyErr_Occurred()) |
| 900 | PyErr_SetString(PyExc_TypeError, |
| 901 | "NULL object in marshal data for tuple"); |
| 902 | Py_DECREF(v); |
| 903 | v = NULL; |
| 904 | break; |
| 905 | } |
Serhiy Storchaka | 34fe1b7 | 2013-02-13 12:07:43 +0200 | [diff] [blame] | 906 | PyTuple_SET_ITEM(v, i, v2); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 907 | } |
| 908 | retval = v; |
| 909 | break; |
Tim Peters | d9b9ac8 | 2001-01-28 00:27:39 +0000 | [diff] [blame] | 910 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 911 | case TYPE_LIST: |
| 912 | n = r_long(p); |
Serhiy Storchaka | 34fe1b7 | 2013-02-13 12:07:43 +0200 | [diff] [blame] | 913 | if (n < 0 || n > SIZE32_MAX) { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 914 | PyErr_SetString(PyExc_ValueError, "bad marshal data (list size out of range)"); |
| 915 | retval = NULL; |
| 916 | break; |
| 917 | } |
Serhiy Storchaka | 34fe1b7 | 2013-02-13 12:07:43 +0200 | [diff] [blame] | 918 | v = PyList_New(n); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 919 | if (v == NULL) { |
| 920 | retval = NULL; |
| 921 | break; |
| 922 | } |
| 923 | for (i = 0; i < n; i++) { |
| 924 | v2 = r_object(p); |
| 925 | if ( v2 == NULL ) { |
| 926 | if (!PyErr_Occurred()) |
| 927 | PyErr_SetString(PyExc_TypeError, |
| 928 | "NULL object in marshal data for list"); |
| 929 | Py_DECREF(v); |
| 930 | v = NULL; |
| 931 | break; |
| 932 | } |
Serhiy Storchaka | 34fe1b7 | 2013-02-13 12:07:43 +0200 | [diff] [blame] | 933 | PyList_SET_ITEM(v, i, v2); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 934 | } |
| 935 | retval = v; |
| 936 | break; |
Tim Peters | d9b9ac8 | 2001-01-28 00:27:39 +0000 | [diff] [blame] | 937 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 938 | case TYPE_DICT: |
| 939 | v = PyDict_New(); |
| 940 | if (v == NULL) { |
| 941 | retval = NULL; |
| 942 | break; |
| 943 | } |
| 944 | for (;;) { |
| 945 | PyObject *key, *val; |
| 946 | key = r_object(p); |
| 947 | if (key == NULL) |
| 948 | break; |
| 949 | val = r_object(p); |
| 950 | if (val != NULL) |
| 951 | PyDict_SetItem(v, key, val); |
| 952 | Py_DECREF(key); |
| 953 | Py_XDECREF(val); |
| 954 | } |
| 955 | if (PyErr_Occurred()) { |
| 956 | Py_DECREF(v); |
| 957 | v = NULL; |
| 958 | } |
| 959 | retval = v; |
| 960 | break; |
Tim Peters | d9b9ac8 | 2001-01-28 00:27:39 +0000 | [diff] [blame] | 961 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 962 | case TYPE_SET: |
| 963 | case TYPE_FROZENSET: |
| 964 | n = r_long(p); |
Serhiy Storchaka | 34fe1b7 | 2013-02-13 12:07:43 +0200 | [diff] [blame] | 965 | if (n < 0 || n > SIZE32_MAX) { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 966 | PyErr_SetString(PyExc_ValueError, "bad marshal data (set size out of range)"); |
| 967 | retval = NULL; |
| 968 | break; |
| 969 | } |
| 970 | v = (type == TYPE_SET) ? PySet_New(NULL) : PyFrozenSet_New(NULL); |
| 971 | if (v == NULL) { |
| 972 | retval = NULL; |
| 973 | break; |
| 974 | } |
| 975 | for (i = 0; i < n; i++) { |
| 976 | v2 = r_object(p); |
| 977 | if ( v2 == NULL ) { |
| 978 | if (!PyErr_Occurred()) |
| 979 | PyErr_SetString(PyExc_TypeError, |
| 980 | "NULL object in marshal data for set"); |
| 981 | Py_DECREF(v); |
| 982 | v = NULL; |
| 983 | break; |
| 984 | } |
| 985 | if (PySet_Add(v, v2) == -1) { |
| 986 | Py_DECREF(v); |
| 987 | Py_DECREF(v2); |
| 988 | v = NULL; |
| 989 | break; |
| 990 | } |
| 991 | Py_DECREF(v2); |
| 992 | } |
| 993 | retval = v; |
| 994 | break; |
Raymond Hettinger | a422c34 | 2005-01-11 03:03:27 +0000 | [diff] [blame] | 995 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 996 | case TYPE_CODE: |
| 997 | if (PyEval_GetRestricted()) { |
| 998 | PyErr_SetString(PyExc_RuntimeError, |
| 999 | "cannot unmarshal code objects in " |
| 1000 | "restricted execution mode"); |
| 1001 | retval = NULL; |
| 1002 | break; |
| 1003 | } |
| 1004 | else { |
| 1005 | int argcount; |
| 1006 | int nlocals; |
| 1007 | int stacksize; |
| 1008 | int flags; |
| 1009 | PyObject *code = NULL; |
| 1010 | PyObject *consts = NULL; |
| 1011 | PyObject *names = NULL; |
| 1012 | PyObject *varnames = NULL; |
| 1013 | PyObject *freevars = NULL; |
| 1014 | PyObject *cellvars = NULL; |
| 1015 | PyObject *filename = NULL; |
| 1016 | PyObject *name = NULL; |
| 1017 | int firstlineno; |
| 1018 | PyObject *lnotab = NULL; |
Tim Peters | d9b9ac8 | 2001-01-28 00:27:39 +0000 | [diff] [blame] | 1019 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1020 | v = NULL; |
Michael W. Hudson | df88846 | 2005-06-03 14:41:55 +0000 | [diff] [blame] | 1021 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1022 | /* XXX ignore long->int overflows for now */ |
| 1023 | argcount = (int)r_long(p); |
| 1024 | nlocals = (int)r_long(p); |
| 1025 | stacksize = (int)r_long(p); |
| 1026 | flags = (int)r_long(p); |
| 1027 | code = r_object(p); |
| 1028 | if (code == NULL) |
| 1029 | goto code_error; |
| 1030 | consts = r_object(p); |
| 1031 | if (consts == NULL) |
| 1032 | goto code_error; |
| 1033 | names = r_object(p); |
| 1034 | if (names == NULL) |
| 1035 | goto code_error; |
| 1036 | varnames = r_object(p); |
| 1037 | if (varnames == NULL) |
| 1038 | goto code_error; |
| 1039 | freevars = r_object(p); |
| 1040 | if (freevars == NULL) |
| 1041 | goto code_error; |
| 1042 | cellvars = r_object(p); |
| 1043 | if (cellvars == NULL) |
| 1044 | goto code_error; |
| 1045 | filename = r_object(p); |
| 1046 | if (filename == NULL) |
| 1047 | goto code_error; |
| 1048 | name = r_object(p); |
| 1049 | if (name == NULL) |
| 1050 | goto code_error; |
| 1051 | firstlineno = (int)r_long(p); |
| 1052 | lnotab = r_object(p); |
| 1053 | if (lnotab == NULL) |
| 1054 | goto code_error; |
Michael W. Hudson | df88846 | 2005-06-03 14:41:55 +0000 | [diff] [blame] | 1055 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1056 | v = (PyObject *) PyCode_New( |
| 1057 | argcount, nlocals, stacksize, flags, |
| 1058 | code, consts, names, varnames, |
| 1059 | freevars, cellvars, filename, name, |
| 1060 | firstlineno, lnotab); |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 1061 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1062 | code_error: |
| 1063 | Py_XDECREF(code); |
| 1064 | Py_XDECREF(consts); |
| 1065 | Py_XDECREF(names); |
| 1066 | Py_XDECREF(varnames); |
| 1067 | Py_XDECREF(freevars); |
| 1068 | Py_XDECREF(cellvars); |
| 1069 | Py_XDECREF(filename); |
| 1070 | Py_XDECREF(name); |
| 1071 | Py_XDECREF(lnotab); |
Tim Peters | d9b9ac8 | 2001-01-28 00:27:39 +0000 | [diff] [blame] | 1072 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1073 | } |
| 1074 | retval = v; |
| 1075 | break; |
Tim Peters | d9b9ac8 | 2001-01-28 00:27:39 +0000 | [diff] [blame] | 1076 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1077 | default: |
| 1078 | /* Bogus data got written, which isn't ideal. |
| 1079 | This will let you keep working and recover. */ |
| 1080 | PyErr_SetString(PyExc_ValueError, "bad marshal data (unknown type code)"); |
| 1081 | retval = NULL; |
| 1082 | break; |
| 1083 | |
| 1084 | } |
| 1085 | p->depth--; |
| 1086 | return retval; |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 1087 | } |
| 1088 | |
Neal Norwitz | d85c452 | 2004-06-13 20:31:49 +0000 | [diff] [blame] | 1089 | static PyObject * |
Armin Rigo | 01ab279 | 2004-03-26 15:09:27 +0000 | [diff] [blame] | 1090 | read_object(RFILE *p) |
| 1091 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1092 | PyObject *v; |
| 1093 | if (PyErr_Occurred()) { |
| 1094 | fprintf(stderr, "XXX readobject called with exception set\n"); |
| 1095 | return NULL; |
| 1096 | } |
| 1097 | v = r_object(p); |
| 1098 | if (v == NULL && !PyErr_Occurred()) |
| 1099 | PyErr_SetString(PyExc_TypeError, "NULL object in marshal data for object"); |
| 1100 | return v; |
Armin Rigo | 01ab279 | 2004-03-26 15:09:27 +0000 | [diff] [blame] | 1101 | } |
| 1102 | |
Guido van Rossum | b8cf3e6 | 2001-10-19 01:46:21 +0000 | [diff] [blame] | 1103 | int |
| 1104 | PyMarshal_ReadShortFromFile(FILE *fp) |
| 1105 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1106 | RFILE rf; |
| 1107 | assert(fp); |
| 1108 | rf.fp = fp; |
| 1109 | rf.strings = NULL; |
| 1110 | rf.end = rf.ptr = NULL; |
| 1111 | return r_short(&rf); |
Guido van Rossum | b8cf3e6 | 2001-10-19 01:46:21 +0000 | [diff] [blame] | 1112 | } |
| 1113 | |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 1114 | long |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1115 | PyMarshal_ReadLongFromFile(FILE *fp) |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 1116 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1117 | RFILE rf; |
| 1118 | rf.fp = fp; |
| 1119 | rf.strings = NULL; |
| 1120 | rf.ptr = rf.end = NULL; |
| 1121 | return r_long(&rf); |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 1122 | } |
| 1123 | |
Tim Peters | 691e0e9 | 2001-01-18 04:39:16 +0000 | [diff] [blame] | 1124 | #ifdef HAVE_FSTAT |
| 1125 | /* Return size of file in bytes; < 0 if unknown. */ |
| 1126 | static off_t |
| 1127 | getfilesize(FILE *fp) |
| 1128 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1129 | struct stat st; |
| 1130 | if (fstat(fileno(fp), &st) != 0) |
| 1131 | return -1; |
| 1132 | else |
| 1133 | return st.st_size; |
Tim Peters | 691e0e9 | 2001-01-18 04:39:16 +0000 | [diff] [blame] | 1134 | } |
| 1135 | #endif |
Tim Peters | d9b9ac8 | 2001-01-28 00:27:39 +0000 | [diff] [blame] | 1136 | |
Tim Peters | 691e0e9 | 2001-01-18 04:39:16 +0000 | [diff] [blame] | 1137 | /* If we can get the size of the file up-front, and it's reasonably small, |
| 1138 | * read it in one gulp and delegate to ...FromString() instead. Much quicker |
| 1139 | * than reading a byte at a time from file; speeds .pyc imports. |
Tim Peters | d9b9ac8 | 2001-01-28 00:27:39 +0000 | [diff] [blame] | 1140 | * CAUTION: since this may read the entire remainder of the file, don't |
| 1141 | * call it unless you know you're done with the file. |
Tim Peters | 691e0e9 | 2001-01-18 04:39:16 +0000 | [diff] [blame] | 1142 | */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1143 | PyObject * |
Tim Peters | d9b9ac8 | 2001-01-28 00:27:39 +0000 | [diff] [blame] | 1144 | PyMarshal_ReadLastObjectFromFile(FILE *fp) |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 1145 | { |
Antoine Pitrou | 18e63fb | 2010-04-21 22:53:29 +0000 | [diff] [blame] | 1146 | /* REASONABLE_FILE_LIMIT is by defn something big enough for Tkinter.pyc. */ |
Tim Peters | 691e0e9 | 2001-01-18 04:39:16 +0000 | [diff] [blame] | 1147 | #define REASONABLE_FILE_LIMIT (1L << 18) |
Tim Peters | 691e0e9 | 2001-01-18 04:39:16 +0000 | [diff] [blame] | 1148 | #ifdef HAVE_FSTAT |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1149 | off_t filesize; |
| 1150 | filesize = getfilesize(fp); |
| 1151 | if (filesize > 0 && filesize <= REASONABLE_FILE_LIMIT) { |
| 1152 | char* pBuf = (char *)PyMem_MALLOC(filesize); |
| 1153 | if (pBuf != NULL) { |
Serhiy Storchaka | 34fe1b7 | 2013-02-13 12:07:43 +0200 | [diff] [blame] | 1154 | size_t n = fread(pBuf, 1, (size_t)filesize, fp); |
| 1155 | PyObject* v = PyMarshal_ReadObjectFromString(pBuf, n); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1156 | PyMem_FREE(pBuf); |
| 1157 | return v; |
| 1158 | } |
Tim Peters | d9b9ac8 | 2001-01-28 00:27:39 +0000 | [diff] [blame] | 1159 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1160 | } |
Tim Peters | 691e0e9 | 2001-01-18 04:39:16 +0000 | [diff] [blame] | 1161 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1162 | /* We don't have fstat, or we do but the file is larger than |
| 1163 | * REASONABLE_FILE_LIMIT or malloc failed -- read a byte at a time. |
| 1164 | */ |
| 1165 | return PyMarshal_ReadObjectFromFile(fp); |
Tim Peters | d9b9ac8 | 2001-01-28 00:27:39 +0000 | [diff] [blame] | 1166 | |
Tim Peters | 691e0e9 | 2001-01-18 04:39:16 +0000 | [diff] [blame] | 1167 | #undef REASONABLE_FILE_LIMIT |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 1168 | } |
| 1169 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1170 | PyObject * |
Tim Peters | d9b9ac8 | 2001-01-28 00:27:39 +0000 | [diff] [blame] | 1171 | PyMarshal_ReadObjectFromFile(FILE *fp) |
| 1172 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1173 | RFILE rf; |
| 1174 | PyObject *result; |
| 1175 | rf.fp = fp; |
| 1176 | rf.strings = PyList_New(0); |
| 1177 | rf.depth = 0; |
| 1178 | rf.ptr = rf.end = NULL; |
| 1179 | result = r_object(&rf); |
| 1180 | Py_DECREF(rf.strings); |
| 1181 | return result; |
Tim Peters | d9b9ac8 | 2001-01-28 00:27:39 +0000 | [diff] [blame] | 1182 | } |
| 1183 | |
| 1184 | PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1185 | PyMarshal_ReadObjectFromString(char *str, Py_ssize_t len) |
Guido van Rossum | f56e3db | 1993-04-01 20:59:32 +0000 | [diff] [blame] | 1186 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1187 | RFILE rf; |
| 1188 | PyObject *result; |
| 1189 | rf.fp = NULL; |
| 1190 | rf.ptr = str; |
| 1191 | rf.end = str + len; |
| 1192 | rf.strings = PyList_New(0); |
| 1193 | rf.depth = 0; |
| 1194 | result = r_object(&rf); |
| 1195 | Py_DECREF(rf.strings); |
| 1196 | return result; |
Guido van Rossum | f56e3db | 1993-04-01 20:59:32 +0000 | [diff] [blame] | 1197 | } |
| 1198 | |
Eric Smith | 1566927 | 2009-10-19 00:34:12 +0000 | [diff] [blame] | 1199 | static void |
| 1200 | set_error(int error) |
| 1201 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1202 | switch (error) { |
| 1203 | case WFERR_NOMEMORY: |
| 1204 | PyErr_NoMemory(); |
| 1205 | break; |
| 1206 | case WFERR_UNMARSHALLABLE: |
| 1207 | PyErr_SetString(PyExc_ValueError, "unmarshallable object"); |
| 1208 | break; |
| 1209 | case WFERR_NESTEDTOODEEP: |
| 1210 | default: |
| 1211 | PyErr_SetString(PyExc_ValueError, |
| 1212 | "object too deeply nested to marshal"); |
| 1213 | break; |
| 1214 | } |
Eric Smith | 1566927 | 2009-10-19 00:34:12 +0000 | [diff] [blame] | 1215 | } |
| 1216 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1217 | PyObject * |
Martin v. Löwis | ef82d2f | 2004-06-27 16:51:46 +0000 | [diff] [blame] | 1218 | PyMarshal_WriteObjectToString(PyObject *x, int version) |
Guido van Rossum | 3f3bb3d | 1996-08-19 22:07:17 +0000 | [diff] [blame] | 1219 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1220 | WFILE wf; |
| 1221 | wf.fp = NULL; |
| 1222 | wf.str = PyString_FromStringAndSize((char *)NULL, 50); |
| 1223 | if (wf.str == NULL) |
| 1224 | return NULL; |
| 1225 | wf.ptr = PyString_AS_STRING((PyStringObject *)wf.str); |
| 1226 | wf.end = wf.ptr + PyString_Size(wf.str); |
| 1227 | wf.error = WFERR_OK; |
| 1228 | wf.depth = 0; |
| 1229 | wf.version = version; |
| 1230 | wf.strings = (version > 0) ? PyDict_New() : NULL; |
| 1231 | w_object(x, &wf); |
| 1232 | Py_XDECREF(wf.strings); |
| 1233 | if (wf.str != NULL) { |
| 1234 | char *base = PyString_AS_STRING((PyStringObject *)wf.str); |
| 1235 | if (wf.ptr - base > PY_SSIZE_T_MAX) { |
| 1236 | Py_DECREF(wf.str); |
| 1237 | PyErr_SetString(PyExc_OverflowError, |
| 1238 | "too much marshall data for a string"); |
| 1239 | return NULL; |
| 1240 | } |
| 1241 | if (_PyString_Resize(&wf.str, (Py_ssize_t)(wf.ptr - base))) |
| 1242 | return NULL; |
| 1243 | } |
| 1244 | if (wf.error != WFERR_OK) { |
| 1245 | Py_XDECREF(wf.str); |
| 1246 | set_error(wf.error); |
| 1247 | return NULL; |
| 1248 | } |
| 1249 | return wf.str; |
Guido van Rossum | 3f3bb3d | 1996-08-19 22:07:17 +0000 | [diff] [blame] | 1250 | } |
| 1251 | |
Guido van Rossum | 64b4552 | 1991-06-07 13:58:22 +0000 | [diff] [blame] | 1252 | /* And an interface for Python programs... */ |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 1253 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1254 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1255 | marshal_dump(PyObject *self, PyObject *args) |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 1256 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1257 | WFILE wf; |
| 1258 | PyObject *x; |
| 1259 | PyObject *f; |
| 1260 | int version = Py_MARSHAL_VERSION; |
| 1261 | if (!PyArg_ParseTuple(args, "OO|i:dump", &x, &f, &version)) |
| 1262 | return NULL; |
| 1263 | if (!PyFile_Check(f)) { |
| 1264 | PyErr_SetString(PyExc_TypeError, |
| 1265 | "marshal.dump() 2nd arg must be file"); |
| 1266 | return NULL; |
| 1267 | } |
| 1268 | wf.fp = PyFile_AsFile(f); |
| 1269 | wf.str = NULL; |
| 1270 | wf.ptr = wf.end = NULL; |
| 1271 | wf.error = WFERR_OK; |
| 1272 | wf.depth = 0; |
| 1273 | wf.strings = (version > 0) ? PyDict_New() : 0; |
| 1274 | wf.version = version; |
| 1275 | w_object(x, &wf); |
| 1276 | Py_XDECREF(wf.strings); |
| 1277 | if (wf.error != WFERR_OK) { |
| 1278 | set_error(wf.error); |
| 1279 | return NULL; |
| 1280 | } |
| 1281 | Py_INCREF(Py_None); |
| 1282 | return Py_None; |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 1283 | } |
| 1284 | |
R. David Murray | a3ec697 | 2009-05-13 00:30:29 +0000 | [diff] [blame] | 1285 | PyDoc_STRVAR(dump_doc, |
| 1286 | "dump(value, file[, version])\n\ |
| 1287 | \n\ |
| 1288 | Write the value on the open file. The value must be a supported type.\n\ |
| 1289 | The file must be an open file object such as sys.stdout or returned by\n\ |
| 1290 | open() or os.popen(). It must be opened in binary mode ('wb' or 'w+b').\n\ |
| 1291 | \n\ |
| 1292 | If the value has (or contains an object that has) an unsupported type, a\n\ |
| 1293 | ValueError exception is raised — but garbage data will also be written\n\ |
| 1294 | to the file. The object will not be properly read back by load()\n\ |
| 1295 | \n\ |
| 1296 | New in version 2.4: The version argument indicates the data format that\n\ |
| 1297 | dump should use."); |
| 1298 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1299 | static PyObject * |
Georg Brandl | bf92f46 | 2006-05-29 21:58:42 +0000 | [diff] [blame] | 1300 | marshal_load(PyObject *self, PyObject *f) |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 1301 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1302 | RFILE rf; |
| 1303 | PyObject *result; |
| 1304 | if (!PyFile_Check(f)) { |
| 1305 | PyErr_SetString(PyExc_TypeError, |
| 1306 | "marshal.load() arg must be file"); |
| 1307 | return NULL; |
| 1308 | } |
| 1309 | rf.fp = PyFile_AsFile(f); |
| 1310 | rf.strings = PyList_New(0); |
| 1311 | rf.depth = 0; |
| 1312 | result = read_object(&rf); |
| 1313 | Py_DECREF(rf.strings); |
| 1314 | return result; |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 1315 | } |
| 1316 | |
R. David Murray | a3ec697 | 2009-05-13 00:30:29 +0000 | [diff] [blame] | 1317 | PyDoc_STRVAR(load_doc, |
| 1318 | "load(file)\n\ |
| 1319 | \n\ |
| 1320 | Read one value from the open file and return it. If no valid value is\n\ |
| 1321 | read (e.g. because the data has a different Python version’s\n\ |
| 1322 | incompatible marshal format), raise EOFError, ValueError or TypeError.\n\ |
| 1323 | The file must be an open file object opened in binary mode ('rb' or\n\ |
| 1324 | 'r+b').\n\ |
| 1325 | \n\ |
| 1326 | Note: If an object containing an unsupported type was marshalled with\n\ |
| 1327 | dump(), load() will substitute None for the unmarshallable type."); |
| 1328 | |
| 1329 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1330 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1331 | marshal_dumps(PyObject *self, PyObject *args) |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 1332 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1333 | PyObject *x; |
| 1334 | int version = Py_MARSHAL_VERSION; |
| 1335 | if (!PyArg_ParseTuple(args, "O|i:dumps", &x, &version)) |
| 1336 | return NULL; |
| 1337 | return PyMarshal_WriteObjectToString(x, version); |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 1338 | } |
| 1339 | |
R. David Murray | a3ec697 | 2009-05-13 00:30:29 +0000 | [diff] [blame] | 1340 | PyDoc_STRVAR(dumps_doc, |
| 1341 | "dumps(value[, version])\n\ |
| 1342 | \n\ |
| 1343 | Return the string that would be written to a file by dump(value, file).\n\ |
| 1344 | The value must be a supported type. Raise a ValueError exception if\n\ |
| 1345 | value has (or contains an object that has) an unsupported type.\n\ |
| 1346 | \n\ |
| 1347 | New in version 2.4: The version argument indicates the data format that\n\ |
R. David Murray | 525cffc | 2009-05-13 13:07:14 +0000 | [diff] [blame] | 1348 | dumps should use."); |
R. David Murray | a3ec697 | 2009-05-13 00:30:29 +0000 | [diff] [blame] | 1349 | |
| 1350 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1351 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1352 | marshal_loads(PyObject *self, PyObject *args) |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 1353 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1354 | RFILE rf; |
| 1355 | char *s; |
| 1356 | Py_ssize_t n; |
| 1357 | PyObject* result; |
| 1358 | if (!PyArg_ParseTuple(args, "s#:loads", &s, &n)) |
| 1359 | return NULL; |
| 1360 | rf.fp = NULL; |
| 1361 | rf.ptr = s; |
| 1362 | rf.end = s + n; |
| 1363 | rf.strings = PyList_New(0); |
| 1364 | rf.depth = 0; |
| 1365 | result = read_object(&rf); |
| 1366 | Py_DECREF(rf.strings); |
| 1367 | return result; |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 1368 | } |
| 1369 | |
R. David Murray | a3ec697 | 2009-05-13 00:30:29 +0000 | [diff] [blame] | 1370 | PyDoc_STRVAR(loads_doc, |
| 1371 | "loads(string)\n\ |
| 1372 | \n\ |
| 1373 | Convert the string to a value. If no valid value is found, raise\n\ |
| 1374 | EOFError, ValueError or TypeError. Extra characters in the string are\n\ |
| 1375 | ignored."); |
| 1376 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1377 | static PyMethodDef marshal_methods[] = { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1378 | {"dump", marshal_dump, METH_VARARGS, dump_doc}, |
| 1379 | {"load", marshal_load, METH_O, load_doc}, |
| 1380 | {"dumps", marshal_dumps, METH_VARARGS, dumps_doc}, |
| 1381 | {"loads", marshal_loads, METH_VARARGS, loads_doc}, |
| 1382 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 1383 | }; |
| 1384 | |
R. David Murray | a3ec697 | 2009-05-13 00:30:29 +0000 | [diff] [blame] | 1385 | PyDoc_STRVAR(marshal_doc, |
| 1386 | "This module contains functions that can read and write Python values in\n\ |
| 1387 | a binary format. The format is specific to Python, but independent of\n\ |
| 1388 | machine architecture issues.\n\ |
| 1389 | \n\ |
| 1390 | Not all Python object types are supported; in general, only objects\n\ |
| 1391 | whose value is independent from a particular invocation of Python can be\n\ |
| 1392 | written and read by this module. The following types are supported:\n\ |
| 1393 | None, integers, long integers, floating point numbers, strings, Unicode\n\ |
| 1394 | objects, tuples, lists, sets, dictionaries, and code objects, where it\n\ |
| 1395 | should be understood that tuples, lists and dictionaries are only\n\ |
| 1396 | supported as long as the values contained therein are themselves\n\ |
| 1397 | supported; and recursive lists and dictionaries should not be written\n\ |
| 1398 | (they will cause infinite loops).\n\ |
| 1399 | \n\ |
| 1400 | Variables:\n\ |
| 1401 | \n\ |
| 1402 | version -- indicates the format that the module uses. Version 0 is the\n\ |
| 1403 | historical format, version 1 (added in Python 2.4) shares interned\n\ |
| 1404 | strings and version 2 (added in Python 2.5) uses a binary format for\n\ |
| 1405 | floating point numbers. (New in version 2.4)\n\ |
| 1406 | \n\ |
| 1407 | Functions:\n\ |
| 1408 | \n\ |
| 1409 | dump() -- write value to a file\n\ |
| 1410 | load() -- read value from a file\n\ |
| 1411 | dumps() -- write value to a string\n\ |
| 1412 | loads() -- read value from a string"); |
| 1413 | |
| 1414 | |
Jason Tishler | 6bc06ec | 2003-09-04 11:59:50 +0000 | [diff] [blame] | 1415 | PyMODINIT_FUNC |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1416 | PyMarshal_Init(void) |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 1417 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1418 | PyObject *mod = Py_InitModule3("marshal", marshal_methods, |
| 1419 | marshal_doc); |
| 1420 | if (mod == NULL) |
| 1421 | return; |
| 1422 | PyModule_AddIntConstant(mod, "version", Py_MARSHAL_VERSION); |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 1423 | } |