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