Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 1 | |
| 2 | /* Write Python objects to files and read them back. |
Kristján Valur Jónsson | d7009c6 | 2013-03-19 18:02:10 -0700 | [diff] [blame] | 3 | This is primarily intended for writing and reading compiled Python code, |
| 4 | even though dicts, lists, sets and frozensets, not commonly seen in |
| 5 | code objects, are supported. |
| 6 | Version 3 of this protocol properly supports circular links |
| 7 | and sharing. */ |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 8 | |
Thomas Wouters | 695934a | 2006-03-01 23:49:13 +0000 | [diff] [blame] | 9 | #define PY_SSIZE_T_CLEAN |
| 10 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 11 | #include "Python.h" |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 12 | #include "longintrepr.h" |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 13 | #include "code.h" |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 14 | #include "marshal.h" |
Serhiy Storchaka | ce921c62 | 2015-02-11 15:53:31 +0200 | [diff] [blame] | 15 | #include "../Modules/hashtable.h" |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 16 | |
Serhiy Storchaka | 0767ad4 | 2017-03-12 09:20:15 +0200 | [diff] [blame] | 17 | /*[clinic input] |
| 18 | module marshal |
| 19 | [clinic start generated code]*/ |
| 20 | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=c982b7930dee17db]*/ |
| 21 | |
| 22 | #include "clinic/marshal.c.h" |
| 23 | |
Fred Drake | 6da0b91 | 2000-06-28 18:47:56 +0000 | [diff] [blame] | 24 | /* High water mark to determine when the marshalled object is dangerously deep |
| 25 | * and risks coring the interpreter. When the object stack gets this deep, |
| 26 | * raise an exception instead of continuing. |
Guido van Rossum | 63175a1 | 2007-08-29 20:39:13 +0000 | [diff] [blame] | 27 | * On Windows debug builds, reduce this value. |
Fred Drake | 6da0b91 | 2000-06-28 18:47:56 +0000 | [diff] [blame] | 28 | */ |
Guido van Rossum | 63175a1 | 2007-08-29 20:39:13 +0000 | [diff] [blame] | 29 | #if defined(MS_WINDOWS) && defined(_DEBUG) |
Steve Dower | f6c69e6 | 2014-11-01 15:15:16 -0700 | [diff] [blame] | 30 | #define MAX_MARSHAL_STACK_DEPTH 1000 |
Guido van Rossum | 63175a1 | 2007-08-29 20:39:13 +0000 | [diff] [blame] | 31 | #else |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 32 | #define MAX_MARSHAL_STACK_DEPTH 2000 |
Guido van Rossum | 63175a1 | 2007-08-29 20:39:13 +0000 | [diff] [blame] | 33 | #endif |
Fred Drake | 6da0b91 | 2000-06-28 18:47:56 +0000 | [diff] [blame] | 34 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 35 | #define TYPE_NULL '0' |
| 36 | #define TYPE_NONE 'N' |
| 37 | #define TYPE_FALSE 'F' |
| 38 | #define TYPE_TRUE 'T' |
| 39 | #define TYPE_STOPITER 'S' |
| 40 | #define TYPE_ELLIPSIS '.' |
| 41 | #define TYPE_INT 'i' |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 42 | #define TYPE_FLOAT 'f' |
| 43 | #define TYPE_BINARY_FLOAT 'g' |
| 44 | #define TYPE_COMPLEX 'x' |
| 45 | #define TYPE_BINARY_COMPLEX 'y' |
| 46 | #define TYPE_LONG 'l' |
| 47 | #define TYPE_STRING 's' |
Kristján Valur Jónsson | d7009c6 | 2013-03-19 18:02:10 -0700 | [diff] [blame] | 48 | #define TYPE_INTERNED 't' |
| 49 | #define TYPE_REF 'r' |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 50 | #define TYPE_TUPLE '(' |
| 51 | #define TYPE_LIST '[' |
| 52 | #define TYPE_DICT '{' |
| 53 | #define TYPE_CODE 'c' |
| 54 | #define TYPE_UNICODE 'u' |
| 55 | #define TYPE_UNKNOWN '?' |
| 56 | #define TYPE_SET '<' |
| 57 | #define TYPE_FROZENSET '>' |
Kristján Valur Jónsson | d7009c6 | 2013-03-19 18:02:10 -0700 | [diff] [blame] | 58 | #define FLAG_REF '\x80' /* with a type, add obj to index */ |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 59 | |
Antoine Pitrou | 1164dfc | 2013-10-12 22:25:39 +0200 | [diff] [blame] | 60 | #define TYPE_ASCII 'a' |
| 61 | #define TYPE_ASCII_INTERNED 'A' |
| 62 | #define TYPE_SMALL_TUPLE ')' |
| 63 | #define TYPE_SHORT_ASCII 'z' |
| 64 | #define TYPE_SHORT_ASCII_INTERNED 'Z' |
| 65 | |
Eric Smith | b1a03cf | 2009-04-21 11:57:38 +0000 | [diff] [blame] | 66 | #define WFERR_OK 0 |
| 67 | #define WFERR_UNMARSHALLABLE 1 |
| 68 | #define WFERR_NESTEDTOODEEP 2 |
| 69 | #define WFERR_NOMEMORY 3 |
| 70 | |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 71 | typedef struct { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 72 | FILE *fp; |
| 73 | int error; /* see WFERR_* values */ |
| 74 | int depth; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 75 | PyObject *str; |
| 76 | char *ptr; |
| 77 | char *end; |
Antoine Pitrou | 1164dfc | 2013-10-12 22:25:39 +0200 | [diff] [blame] | 78 | char *buf; |
Serhiy Storchaka | ce921c62 | 2015-02-11 15:53:31 +0200 | [diff] [blame] | 79 | _Py_hashtable_t *hashtable; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 80 | int version; |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 81 | } WFILE; |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 82 | |
Serhiy Storchaka | c1efe5f | 2015-02-11 15:54:54 +0200 | [diff] [blame] | 83 | #define w_byte(c, p) do { \ |
| 84 | if ((p)->ptr != (p)->end || w_reserve((p), 1)) \ |
| 85 | *(p)->ptr++ = (c); \ |
| 86 | } while(0) |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 87 | |
| 88 | static void |
Serhiy Storchaka | c1efe5f | 2015-02-11 15:54:54 +0200 | [diff] [blame] | 89 | w_flush(WFILE *p) |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 90 | { |
Serhiy Storchaka | c1efe5f | 2015-02-11 15:54:54 +0200 | [diff] [blame] | 91 | assert(p->fp != NULL); |
| 92 | fwrite(p->buf, 1, p->ptr - p->buf, p->fp); |
| 93 | p->ptr = p->buf; |
| 94 | } |
| 95 | |
| 96 | static int |
| 97 | w_reserve(WFILE *p, Py_ssize_t needed) |
| 98 | { |
| 99 | Py_ssize_t pos, size, delta; |
| 100 | if (p->ptr == NULL) |
| 101 | return 0; /* An error already occurred */ |
| 102 | if (p->fp != NULL) { |
| 103 | w_flush(p); |
| 104 | return needed <= p->end - p->ptr; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 105 | } |
Serhiy Storchaka | c1efe5f | 2015-02-11 15:54:54 +0200 | [diff] [blame] | 106 | assert(p->str != NULL); |
| 107 | pos = p->ptr - p->buf; |
| 108 | size = PyBytes_Size(p->str); |
| 109 | if (size > 16*1024*1024) |
| 110 | delta = (size >> 3); /* 12.5% overallocation */ |
| 111 | else |
| 112 | delta = size + 1024; |
| 113 | delta = Py_MAX(delta, needed); |
| 114 | if (delta > PY_SSIZE_T_MAX - size) { |
| 115 | p->error = WFERR_NOMEMORY; |
| 116 | return 0; |
| 117 | } |
| 118 | size += delta; |
| 119 | if (_PyBytes_Resize(&p->str, size) != 0) { |
| 120 | p->ptr = p->buf = p->end = NULL; |
| 121 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 122 | } |
| 123 | else { |
Serhiy Storchaka | c1efe5f | 2015-02-11 15:54:54 +0200 | [diff] [blame] | 124 | p->buf = PyBytes_AS_STRING(p->str); |
| 125 | p->ptr = p->buf + pos; |
| 126 | p->end = p->buf + size; |
| 127 | return 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 128 | } |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | static void |
Serhiy Storchaka | dfde215 | 2013-07-11 19:14:26 +0300 | [diff] [blame] | 132 | w_string(const char *s, Py_ssize_t n, WFILE *p) |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 133 | { |
Serhiy Storchaka | c1efe5f | 2015-02-11 15:54:54 +0200 | [diff] [blame] | 134 | Py_ssize_t m; |
| 135 | if (!n || p->ptr == NULL) |
| 136 | return; |
| 137 | m = p->end - p->ptr; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 138 | if (p->fp != NULL) { |
Serhiy Storchaka | c1efe5f | 2015-02-11 15:54:54 +0200 | [diff] [blame] | 139 | if (n <= m) { |
Christian Heimes | f051e43 | 2016-09-13 20:22:02 +0200 | [diff] [blame] | 140 | memcpy(p->ptr, s, n); |
Serhiy Storchaka | c1efe5f | 2015-02-11 15:54:54 +0200 | [diff] [blame] | 141 | p->ptr += n; |
| 142 | } |
| 143 | else { |
| 144 | w_flush(p); |
| 145 | fwrite(s, 1, n, p->fp); |
| 146 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 147 | } |
| 148 | else { |
Serhiy Storchaka | c1efe5f | 2015-02-11 15:54:54 +0200 | [diff] [blame] | 149 | if (n <= m || w_reserve(p, n - m)) { |
Christian Heimes | f051e43 | 2016-09-13 20:22:02 +0200 | [diff] [blame] | 150 | memcpy(p->ptr, s, n); |
Serhiy Storchaka | c1efe5f | 2015-02-11 15:54:54 +0200 | [diff] [blame] | 151 | p->ptr += n; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 152 | } |
| 153 | } |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 157 | w_short(int x, WFILE *p) |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 158 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 159 | w_byte((char)( x & 0xff), p); |
| 160 | w_byte((char)((x>> 8) & 0xff), p); |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 161 | } |
| 162 | |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 163 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 164 | w_long(long x, WFILE *p) |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 165 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 166 | w_byte((char)( x & 0xff), p); |
| 167 | w_byte((char)((x>> 8) & 0xff), p); |
| 168 | w_byte((char)((x>>16) & 0xff), p); |
| 169 | w_byte((char)((x>>24) & 0xff), p); |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 170 | } |
| 171 | |
Serhiy Storchaka | 7e01911 | 2013-02-13 12:08:15 +0200 | [diff] [blame] | 172 | #define SIZE32_MAX 0x7FFFFFFF |
| 173 | |
| 174 | #if SIZEOF_SIZE_T > 4 |
| 175 | # define W_SIZE(n, p) do { \ |
| 176 | if ((n) > SIZE32_MAX) { \ |
| 177 | (p)->depth--; \ |
| 178 | (p)->error = WFERR_UNMARSHALLABLE; \ |
| 179 | return; \ |
| 180 | } \ |
| 181 | w_long((long)(n), p); \ |
| 182 | } while(0) |
| 183 | #else |
| 184 | # define W_SIZE w_long |
| 185 | #endif |
| 186 | |
Serhiy Storchaka | dfde215 | 2013-07-11 19:14:26 +0300 | [diff] [blame] | 187 | static void |
| 188 | w_pstring(const char *s, Py_ssize_t n, WFILE *p) |
| 189 | { |
| 190 | W_SIZE(n, p); |
| 191 | w_string(s, n, p); |
| 192 | } |
| 193 | |
Antoine Pitrou | 1164dfc | 2013-10-12 22:25:39 +0200 | [diff] [blame] | 194 | static void |
| 195 | w_short_pstring(const char *s, Py_ssize_t n, WFILE *p) |
| 196 | { |
Antoine Pitrou | b30f271 | 2013-10-12 23:14:47 +0200 | [diff] [blame] | 197 | w_byte(Py_SAFE_DOWNCAST(n, Py_ssize_t, unsigned char), p); |
Antoine Pitrou | 1164dfc | 2013-10-12 22:25:39 +0200 | [diff] [blame] | 198 | w_string(s, n, p); |
| 199 | } |
| 200 | |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 201 | /* We assume that Python ints are stored internally in base some power of |
Mark Dickinson | bd79264 | 2009-03-18 20:06:12 +0000 | [diff] [blame] | 202 | 2**15; for the sake of portability we'll always read and write them in base |
| 203 | exactly 2**15. */ |
| 204 | |
| 205 | #define PyLong_MARSHAL_SHIFT 15 |
| 206 | #define PyLong_MARSHAL_BASE ((short)1 << PyLong_MARSHAL_SHIFT) |
| 207 | #define PyLong_MARSHAL_MASK (PyLong_MARSHAL_BASE - 1) |
| 208 | #if PyLong_SHIFT % PyLong_MARSHAL_SHIFT != 0 |
| 209 | #error "PyLong_SHIFT must be a multiple of PyLong_MARSHAL_SHIFT" |
| 210 | #endif |
| 211 | #define PyLong_MARSHAL_RATIO (PyLong_SHIFT / PyLong_MARSHAL_SHIFT) |
| 212 | |
Kristján Valur Jónsson | d7009c6 | 2013-03-19 18:02:10 -0700 | [diff] [blame] | 213 | #define W_TYPE(t, p) do { \ |
| 214 | w_byte((t) | flag, (p)); \ |
| 215 | } while(0) |
| 216 | |
Mark Dickinson | bd79264 | 2009-03-18 20:06:12 +0000 | [diff] [blame] | 217 | static void |
Kristján Valur Jónsson | d7009c6 | 2013-03-19 18:02:10 -0700 | [diff] [blame] | 218 | w_PyLong(const PyLongObject *ob, char flag, WFILE *p) |
Mark Dickinson | bd79264 | 2009-03-18 20:06:12 +0000 | [diff] [blame] | 219 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 220 | Py_ssize_t i, j, n, l; |
| 221 | digit d; |
Mark Dickinson | bd79264 | 2009-03-18 20:06:12 +0000 | [diff] [blame] | 222 | |
Kristján Valur Jónsson | d7009c6 | 2013-03-19 18:02:10 -0700 | [diff] [blame] | 223 | W_TYPE(TYPE_LONG, p); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 224 | if (Py_SIZE(ob) == 0) { |
| 225 | w_long((long)0, p); |
| 226 | return; |
| 227 | } |
Mark Dickinson | bd79264 | 2009-03-18 20:06:12 +0000 | [diff] [blame] | 228 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 229 | /* set l to number of base PyLong_MARSHAL_BASE digits */ |
Victor Stinner | 45e8e2f | 2014-05-14 17:24:35 +0200 | [diff] [blame] | 230 | n = Py_ABS(Py_SIZE(ob)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 231 | l = (n-1) * PyLong_MARSHAL_RATIO; |
| 232 | d = ob->ob_digit[n-1]; |
| 233 | assert(d != 0); /* a PyLong is always normalized */ |
| 234 | do { |
| 235 | d >>= PyLong_MARSHAL_SHIFT; |
| 236 | l++; |
| 237 | } while (d != 0); |
Serhiy Storchaka | 7e01911 | 2013-02-13 12:08:15 +0200 | [diff] [blame] | 238 | if (l > SIZE32_MAX) { |
| 239 | p->depth--; |
| 240 | p->error = WFERR_UNMARSHALLABLE; |
| 241 | return; |
| 242 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 243 | w_long((long)(Py_SIZE(ob) > 0 ? l : -l), p); |
Mark Dickinson | bd79264 | 2009-03-18 20:06:12 +0000 | [diff] [blame] | 244 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 245 | for (i=0; i < n-1; i++) { |
| 246 | d = ob->ob_digit[i]; |
| 247 | for (j=0; j < PyLong_MARSHAL_RATIO; j++) { |
| 248 | w_short(d & PyLong_MARSHAL_MASK, p); |
| 249 | d >>= PyLong_MARSHAL_SHIFT; |
| 250 | } |
| 251 | assert (d == 0); |
| 252 | } |
| 253 | d = ob->ob_digit[n-1]; |
| 254 | do { |
| 255 | w_short(d & PyLong_MARSHAL_MASK, p); |
| 256 | d >>= PyLong_MARSHAL_SHIFT; |
| 257 | } while (d != 0); |
Mark Dickinson | bd79264 | 2009-03-18 20:06:12 +0000 | [diff] [blame] | 258 | } |
| 259 | |
Kristján Valur Jónsson | d7009c6 | 2013-03-19 18:02:10 -0700 | [diff] [blame] | 260 | static int |
| 261 | w_ref(PyObject *v, char *flag, WFILE *p) |
| 262 | { |
Serhiy Storchaka | ce921c62 | 2015-02-11 15:53:31 +0200 | [diff] [blame] | 263 | _Py_hashtable_entry_t *entry; |
| 264 | int w; |
Kristján Valur Jónsson | d7009c6 | 2013-03-19 18:02:10 -0700 | [diff] [blame] | 265 | |
Serhiy Storchaka | ce921c62 | 2015-02-11 15:53:31 +0200 | [diff] [blame] | 266 | if (p->version < 3 || p->hashtable == NULL) |
Kristján Valur Jónsson | d7009c6 | 2013-03-19 18:02:10 -0700 | [diff] [blame] | 267 | return 0; /* not writing object references */ |
| 268 | |
| 269 | /* if it has only one reference, it definitely isn't shared */ |
| 270 | if (Py_REFCNT(v) == 1) |
| 271 | return 0; |
| 272 | |
Victor Stinner | 285cf0a | 2016-03-21 22:00:58 +0100 | [diff] [blame] | 273 | entry = _Py_HASHTABLE_GET_ENTRY(p->hashtable, v); |
Serhiy Storchaka | ce921c62 | 2015-02-11 15:53:31 +0200 | [diff] [blame] | 274 | if (entry != NULL) { |
Kristján Valur Jónsson | d7009c6 | 2013-03-19 18:02:10 -0700 | [diff] [blame] | 275 | /* write the reference index to the stream */ |
Victor Stinner | e8c6b2f | 2016-03-23 09:25:01 +0100 | [diff] [blame] | 276 | _Py_HASHTABLE_ENTRY_READ_DATA(p->hashtable, entry, w); |
Kristján Valur Jónsson | d7009c6 | 2013-03-19 18:02:10 -0700 | [diff] [blame] | 277 | /* we don't store "long" indices in the dict */ |
| 278 | assert(0 <= w && w <= 0x7fffffff); |
| 279 | w_byte(TYPE_REF, p); |
| 280 | w_long(w, p); |
| 281 | return 1; |
| 282 | } else { |
Serhiy Storchaka | ce921c62 | 2015-02-11 15:53:31 +0200 | [diff] [blame] | 283 | size_t s = p->hashtable->entries; |
Kristján Valur Jónsson | d7009c6 | 2013-03-19 18:02:10 -0700 | [diff] [blame] | 284 | /* we don't support long indices */ |
| 285 | if (s >= 0x7fffffff) { |
| 286 | PyErr_SetString(PyExc_ValueError, "too many objects"); |
| 287 | goto err; |
| 288 | } |
Serhiy Storchaka | 26861b0 | 2015-02-16 20:52:17 +0200 | [diff] [blame] | 289 | w = (int)s; |
Serhiy Storchaka | ce921c62 | 2015-02-11 15:53:31 +0200 | [diff] [blame] | 290 | Py_INCREF(v); |
| 291 | if (_Py_HASHTABLE_SET(p->hashtable, v, w) < 0) { |
| 292 | Py_DECREF(v); |
Kristján Valur Jónsson | d7009c6 | 2013-03-19 18:02:10 -0700 | [diff] [blame] | 293 | goto err; |
Serhiy Storchaka | ce921c62 | 2015-02-11 15:53:31 +0200 | [diff] [blame] | 294 | } |
Kristján Valur Jónsson | d7009c6 | 2013-03-19 18:02:10 -0700 | [diff] [blame] | 295 | *flag |= FLAG_REF; |
| 296 | return 0; |
| 297 | } |
| 298 | err: |
| 299 | p->error = WFERR_UNMARSHALLABLE; |
| 300 | return 1; |
| 301 | } |
| 302 | |
| 303 | static void |
| 304 | w_complex_object(PyObject *v, char flag, WFILE *p); |
| 305 | |
Guido van Rossum | b0c168c | 1996-12-05 23:15:02 +0000 | [diff] [blame] | 306 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 307 | w_object(PyObject *v, WFILE *p) |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 308 | { |
Kristján Valur Jónsson | d7009c6 | 2013-03-19 18:02:10 -0700 | [diff] [blame] | 309 | char flag = '\0'; |
Fred Drake | 6da0b91 | 2000-06-28 18:47:56 +0000 | [diff] [blame] | 310 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 311 | p->depth++; |
Tim Peters | d9b9ac8 | 2001-01-28 00:27:39 +0000 | [diff] [blame] | 312 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 313 | if (p->depth > MAX_MARSHAL_STACK_DEPTH) { |
| 314 | p->error = WFERR_NESTEDTOODEEP; |
| 315 | } |
| 316 | else if (v == NULL) { |
| 317 | w_byte(TYPE_NULL, p); |
| 318 | } |
| 319 | else if (v == Py_None) { |
| 320 | w_byte(TYPE_NONE, p); |
| 321 | } |
| 322 | else if (v == PyExc_StopIteration) { |
| 323 | w_byte(TYPE_STOPITER, p); |
| 324 | } |
| 325 | else if (v == Py_Ellipsis) { |
| 326 | w_byte(TYPE_ELLIPSIS, p); |
| 327 | } |
| 328 | else if (v == Py_False) { |
| 329 | w_byte(TYPE_FALSE, p); |
| 330 | } |
| 331 | else if (v == Py_True) { |
| 332 | w_byte(TYPE_TRUE, p); |
| 333 | } |
Kristján Valur Jónsson | d7009c6 | 2013-03-19 18:02:10 -0700 | [diff] [blame] | 334 | else if (!w_ref(v, &flag, p)) |
| 335 | w_complex_object(v, flag, p); |
| 336 | |
| 337 | p->depth--; |
| 338 | } |
| 339 | |
| 340 | static void |
| 341 | w_complex_object(PyObject *v, char flag, WFILE *p) |
| 342 | { |
| 343 | Py_ssize_t i, n; |
| 344 | |
| 345 | if (PyLong_CheckExact(v)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 346 | long x = PyLong_AsLong(v); |
| 347 | if ((x == -1) && PyErr_Occurred()) { |
| 348 | PyLongObject *ob = (PyLongObject *)v; |
| 349 | PyErr_Clear(); |
Kristján Valur Jónsson | d7009c6 | 2013-03-19 18:02:10 -0700 | [diff] [blame] | 350 | w_PyLong(ob, flag, p); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 351 | } |
| 352 | else { |
Guido van Rossum | c1547d9 | 1996-12-10 15:39:04 +0000 | [diff] [blame] | 353 | #if SIZEOF_LONG > 4 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 354 | long y = Py_ARITHMETIC_RIGHT_SHIFT(long, x, 31); |
| 355 | if (y && y != -1) { |
Martin v. Löwis | 7e39572 | 2012-07-28 19:44:05 +0200 | [diff] [blame] | 356 | /* Too large for TYPE_INT */ |
Kristján Valur Jónsson | d7009c6 | 2013-03-19 18:02:10 -0700 | [diff] [blame] | 357 | w_PyLong((PyLongObject*)v, flag, p); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 358 | } |
| 359 | else |
Guido van Rossum | c1547d9 | 1996-12-10 15:39:04 +0000 | [diff] [blame] | 360 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 361 | { |
Kristján Valur Jónsson | d7009c6 | 2013-03-19 18:02:10 -0700 | [diff] [blame] | 362 | W_TYPE(TYPE_INT, p); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 363 | w_long(x, p); |
| 364 | } |
| 365 | } |
| 366 | } |
| 367 | else if (PyFloat_CheckExact(v)) { |
| 368 | if (p->version > 1) { |
| 369 | unsigned char buf[8]; |
| 370 | if (_PyFloat_Pack8(PyFloat_AsDouble(v), |
| 371 | buf, 1) < 0) { |
| 372 | p->error = WFERR_UNMARSHALLABLE; |
| 373 | return; |
| 374 | } |
Kristján Valur Jónsson | d7009c6 | 2013-03-19 18:02:10 -0700 | [diff] [blame] | 375 | W_TYPE(TYPE_BINARY_FLOAT, p); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 376 | w_string((char*)buf, 8, p); |
| 377 | } |
| 378 | else { |
| 379 | char *buf = PyOS_double_to_string(PyFloat_AS_DOUBLE(v), |
| 380 | 'g', 17, 0, NULL); |
| 381 | if (!buf) { |
| 382 | p->error = WFERR_NOMEMORY; |
| 383 | return; |
| 384 | } |
| 385 | n = strlen(buf); |
Kristján Valur Jónsson | d7009c6 | 2013-03-19 18:02:10 -0700 | [diff] [blame] | 386 | W_TYPE(TYPE_FLOAT, p); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 387 | w_byte((int)n, p); |
Serhiy Storchaka | 7e01911 | 2013-02-13 12:08:15 +0200 | [diff] [blame] | 388 | w_string(buf, n, p); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 389 | PyMem_Free(buf); |
| 390 | } |
| 391 | } |
| 392 | else if (PyComplex_CheckExact(v)) { |
| 393 | if (p->version > 1) { |
| 394 | unsigned char buf[8]; |
| 395 | if (_PyFloat_Pack8(PyComplex_RealAsDouble(v), |
| 396 | buf, 1) < 0) { |
| 397 | p->error = WFERR_UNMARSHALLABLE; |
| 398 | return; |
| 399 | } |
Kristján Valur Jónsson | d7009c6 | 2013-03-19 18:02:10 -0700 | [diff] [blame] | 400 | W_TYPE(TYPE_BINARY_COMPLEX, p); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 401 | w_string((char*)buf, 8, p); |
| 402 | if (_PyFloat_Pack8(PyComplex_ImagAsDouble(v), |
| 403 | buf, 1) < 0) { |
| 404 | p->error = WFERR_UNMARSHALLABLE; |
| 405 | return; |
| 406 | } |
| 407 | w_string((char*)buf, 8, p); |
| 408 | } |
| 409 | else { |
| 410 | char *buf; |
Kristján Valur Jónsson | d7009c6 | 2013-03-19 18:02:10 -0700 | [diff] [blame] | 411 | W_TYPE(TYPE_COMPLEX, p); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 412 | buf = PyOS_double_to_string(PyComplex_RealAsDouble(v), |
| 413 | 'g', 17, 0, NULL); |
| 414 | if (!buf) { |
| 415 | p->error = WFERR_NOMEMORY; |
| 416 | return; |
| 417 | } |
| 418 | n = strlen(buf); |
| 419 | w_byte((int)n, p); |
Serhiy Storchaka | 7e01911 | 2013-02-13 12:08:15 +0200 | [diff] [blame] | 420 | w_string(buf, n, p); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 421 | PyMem_Free(buf); |
| 422 | buf = PyOS_double_to_string(PyComplex_ImagAsDouble(v), |
| 423 | 'g', 17, 0, NULL); |
| 424 | if (!buf) { |
| 425 | p->error = WFERR_NOMEMORY; |
| 426 | return; |
| 427 | } |
| 428 | n = strlen(buf); |
| 429 | w_byte((int)n, p); |
Serhiy Storchaka | 7e01911 | 2013-02-13 12:08:15 +0200 | [diff] [blame] | 430 | w_string(buf, n, p); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 431 | PyMem_Free(buf); |
| 432 | } |
| 433 | } |
| 434 | else if (PyBytes_CheckExact(v)) { |
Kristján Valur Jónsson | d7009c6 | 2013-03-19 18:02:10 -0700 | [diff] [blame] | 435 | W_TYPE(TYPE_STRING, p); |
Serhiy Storchaka | dfde215 | 2013-07-11 19:14:26 +0300 | [diff] [blame] | 436 | w_pstring(PyBytes_AS_STRING(v), PyBytes_GET_SIZE(v), p); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 437 | } |
| 438 | else if (PyUnicode_CheckExact(v)) { |
Antoine Pitrou | 1164dfc | 2013-10-12 22:25:39 +0200 | [diff] [blame] | 439 | if (p->version >= 4 && PyUnicode_IS_ASCII(v)) { |
| 440 | int is_short = PyUnicode_GET_LENGTH(v) < 256; |
| 441 | if (is_short) { |
| 442 | if (PyUnicode_CHECK_INTERNED(v)) |
| 443 | W_TYPE(TYPE_SHORT_ASCII_INTERNED, p); |
| 444 | else |
| 445 | W_TYPE(TYPE_SHORT_ASCII, p); |
| 446 | w_short_pstring((char *) PyUnicode_1BYTE_DATA(v), |
| 447 | PyUnicode_GET_LENGTH(v), p); |
| 448 | } |
| 449 | else { |
| 450 | if (PyUnicode_CHECK_INTERNED(v)) |
| 451 | W_TYPE(TYPE_ASCII_INTERNED, p); |
| 452 | else |
| 453 | W_TYPE(TYPE_ASCII, p); |
| 454 | w_pstring((char *) PyUnicode_1BYTE_DATA(v), |
| 455 | PyUnicode_GET_LENGTH(v), p); |
| 456 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 457 | } |
Antoine Pitrou | 1164dfc | 2013-10-12 22:25:39 +0200 | [diff] [blame] | 458 | else { |
| 459 | PyObject *utf8; |
| 460 | utf8 = PyUnicode_AsEncodedString(v, "utf8", "surrogatepass"); |
| 461 | if (utf8 == NULL) { |
| 462 | p->depth--; |
| 463 | p->error = WFERR_UNMARSHALLABLE; |
| 464 | return; |
| 465 | } |
| 466 | if (p->version >= 3 && PyUnicode_CHECK_INTERNED(v)) |
| 467 | W_TYPE(TYPE_INTERNED, p); |
| 468 | else |
| 469 | W_TYPE(TYPE_UNICODE, p); |
| 470 | w_pstring(PyBytes_AS_STRING(utf8), PyBytes_GET_SIZE(utf8), p); |
| 471 | Py_DECREF(utf8); |
| 472 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 473 | } |
| 474 | else if (PyTuple_CheckExact(v)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 475 | n = PyTuple_Size(v); |
Antoine Pitrou | 1164dfc | 2013-10-12 22:25:39 +0200 | [diff] [blame] | 476 | if (p->version >= 4 && n < 256) { |
| 477 | W_TYPE(TYPE_SMALL_TUPLE, p); |
Victor Stinner | da06255 | 2013-11-16 00:13:29 +0100 | [diff] [blame] | 478 | w_byte((unsigned char)n, p); |
Antoine Pitrou | 1164dfc | 2013-10-12 22:25:39 +0200 | [diff] [blame] | 479 | } |
| 480 | else { |
| 481 | W_TYPE(TYPE_TUPLE, p); |
| 482 | W_SIZE(n, p); |
| 483 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 484 | for (i = 0; i < n; i++) { |
| 485 | w_object(PyTuple_GET_ITEM(v, i), p); |
| 486 | } |
| 487 | } |
| 488 | else if (PyList_CheckExact(v)) { |
Kristján Valur Jónsson | d7009c6 | 2013-03-19 18:02:10 -0700 | [diff] [blame] | 489 | W_TYPE(TYPE_LIST, p); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 490 | n = PyList_GET_SIZE(v); |
Serhiy Storchaka | 7e01911 | 2013-02-13 12:08:15 +0200 | [diff] [blame] | 491 | W_SIZE(n, p); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 492 | for (i = 0; i < n; i++) { |
| 493 | w_object(PyList_GET_ITEM(v, i), p); |
| 494 | } |
| 495 | } |
| 496 | else if (PyDict_CheckExact(v)) { |
| 497 | Py_ssize_t pos; |
| 498 | PyObject *key, *value; |
Kristján Valur Jónsson | d7009c6 | 2013-03-19 18:02:10 -0700 | [diff] [blame] | 499 | W_TYPE(TYPE_DICT, p); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 500 | /* This one is NULL object terminated! */ |
| 501 | pos = 0; |
| 502 | while (PyDict_Next(v, &pos, &key, &value)) { |
| 503 | w_object(key, p); |
| 504 | w_object(value, p); |
| 505 | } |
| 506 | w_object((PyObject *)NULL, p); |
| 507 | } |
| 508 | else if (PyAnySet_CheckExact(v)) { |
| 509 | PyObject *value, *it; |
Raymond Hettinger | a422c34 | 2005-01-11 03:03:27 +0000 | [diff] [blame] | 510 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 511 | if (PyObject_TypeCheck(v, &PySet_Type)) |
Kristján Valur Jónsson | d7009c6 | 2013-03-19 18:02:10 -0700 | [diff] [blame] | 512 | W_TYPE(TYPE_SET, p); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 513 | else |
Kristján Valur Jónsson | d7009c6 | 2013-03-19 18:02:10 -0700 | [diff] [blame] | 514 | W_TYPE(TYPE_FROZENSET, p); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 515 | n = PyObject_Size(v); |
| 516 | if (n == -1) { |
| 517 | p->depth--; |
| 518 | p->error = WFERR_UNMARSHALLABLE; |
| 519 | return; |
| 520 | } |
Serhiy Storchaka | 7e01911 | 2013-02-13 12:08:15 +0200 | [diff] [blame] | 521 | W_SIZE(n, p); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 522 | it = PyObject_GetIter(v); |
| 523 | if (it == NULL) { |
| 524 | p->depth--; |
| 525 | p->error = WFERR_UNMARSHALLABLE; |
| 526 | return; |
| 527 | } |
| 528 | while ((value = PyIter_Next(it)) != NULL) { |
| 529 | w_object(value, p); |
| 530 | Py_DECREF(value); |
| 531 | } |
| 532 | Py_DECREF(it); |
| 533 | if (PyErr_Occurred()) { |
| 534 | p->depth--; |
| 535 | p->error = WFERR_UNMARSHALLABLE; |
| 536 | return; |
| 537 | } |
| 538 | } |
| 539 | else if (PyCode_Check(v)) { |
| 540 | PyCodeObject *co = (PyCodeObject *)v; |
Kristján Valur Jónsson | d7009c6 | 2013-03-19 18:02:10 -0700 | [diff] [blame] | 541 | W_TYPE(TYPE_CODE, p); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 542 | w_long(co->co_argcount, p); |
| 543 | w_long(co->co_kwonlyargcount, p); |
| 544 | w_long(co->co_nlocals, p); |
| 545 | w_long(co->co_stacksize, p); |
| 546 | w_long(co->co_flags, p); |
| 547 | w_object(co->co_code, p); |
| 548 | w_object(co->co_consts, p); |
| 549 | w_object(co->co_names, p); |
| 550 | w_object(co->co_varnames, p); |
| 551 | w_object(co->co_freevars, p); |
| 552 | w_object(co->co_cellvars, p); |
| 553 | w_object(co->co_filename, p); |
| 554 | w_object(co->co_name, p); |
| 555 | w_long(co->co_firstlineno, p); |
| 556 | w_object(co->co_lnotab, p); |
| 557 | } |
| 558 | else if (PyObject_CheckBuffer(v)) { |
Serhiy Storchaka | c611a5b | 2017-03-12 08:53:22 +0200 | [diff] [blame] | 559 | /* Write unknown bytes-like objects as a bytes object */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 560 | Py_buffer view; |
Antoine Pitrou | 679e9d3 | 2012-03-02 18:12:43 +0100 | [diff] [blame] | 561 | if (PyObject_GetBuffer(v, &view, PyBUF_SIMPLE) != 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 562 | w_byte(TYPE_UNKNOWN, p); |
Antoine Pitrou | 679e9d3 | 2012-03-02 18:12:43 +0100 | [diff] [blame] | 563 | p->depth--; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 564 | p->error = WFERR_UNMARSHALLABLE; |
Antoine Pitrou | 679e9d3 | 2012-03-02 18:12:43 +0100 | [diff] [blame] | 565 | return; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 566 | } |
Kristján Valur Jónsson | d7009c6 | 2013-03-19 18:02:10 -0700 | [diff] [blame] | 567 | W_TYPE(TYPE_STRING, p); |
Serhiy Storchaka | dfde215 | 2013-07-11 19:14:26 +0300 | [diff] [blame] | 568 | w_pstring(view.buf, view.len, p); |
Antoine Pitrou | 679e9d3 | 2012-03-02 18:12:43 +0100 | [diff] [blame] | 569 | PyBuffer_Release(&view); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 570 | } |
| 571 | else { |
Kristján Valur Jónsson | d7009c6 | 2013-03-19 18:02:10 -0700 | [diff] [blame] | 572 | W_TYPE(TYPE_UNKNOWN, p); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 573 | p->error = WFERR_UNMARSHALLABLE; |
| 574 | } |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 575 | } |
| 576 | |
Serhiy Storchaka | ce921c62 | 2015-02-11 15:53:31 +0200 | [diff] [blame] | 577 | static int |
| 578 | w_init_refs(WFILE *wf, int version) |
| 579 | { |
| 580 | if (version >= 3) { |
Victor Stinner | 285cf0a | 2016-03-21 22:00:58 +0100 | [diff] [blame] | 581 | wf->hashtable = _Py_hashtable_new(sizeof(PyObject *), sizeof(int), |
| 582 | _Py_hashtable_hash_ptr, |
Serhiy Storchaka | ce921c62 | 2015-02-11 15:53:31 +0200 | [diff] [blame] | 583 | _Py_hashtable_compare_direct); |
| 584 | if (wf->hashtable == NULL) { |
| 585 | PyErr_NoMemory(); |
| 586 | return -1; |
| 587 | } |
| 588 | } |
| 589 | return 0; |
| 590 | } |
| 591 | |
| 592 | static int |
Victor Stinner | 285cf0a | 2016-03-21 22:00:58 +0100 | [diff] [blame] | 593 | w_decref_entry(_Py_hashtable_t *ht, _Py_hashtable_entry_t *entry, |
| 594 | void *Py_UNUSED(data)) |
Serhiy Storchaka | ce921c62 | 2015-02-11 15:53:31 +0200 | [diff] [blame] | 595 | { |
Victor Stinner | 285cf0a | 2016-03-21 22:00:58 +0100 | [diff] [blame] | 596 | PyObject *entry_key; |
| 597 | |
Victor Stinner | 5dacbd4 | 2016-03-23 09:52:13 +0100 | [diff] [blame] | 598 | _Py_HASHTABLE_ENTRY_READ_KEY(ht, entry, entry_key); |
Victor Stinner | 285cf0a | 2016-03-21 22:00:58 +0100 | [diff] [blame] | 599 | Py_XDECREF(entry_key); |
Serhiy Storchaka | ce921c62 | 2015-02-11 15:53:31 +0200 | [diff] [blame] | 600 | return 0; |
| 601 | } |
| 602 | |
| 603 | static void |
| 604 | w_clear_refs(WFILE *wf) |
| 605 | { |
| 606 | if (wf->hashtable != NULL) { |
| 607 | _Py_hashtable_foreach(wf->hashtable, w_decref_entry, NULL); |
| 608 | _Py_hashtable_destroy(wf->hashtable); |
| 609 | } |
| 610 | } |
| 611 | |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 612 | /* version currently has no effect for writing ints. */ |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 613 | void |
Martin v. Löwis | ef82d2f | 2004-06-27 16:51:46 +0000 | [diff] [blame] | 614 | PyMarshal_WriteLongToFile(long x, FILE *fp, int version) |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 615 | { |
Serhiy Storchaka | c1efe5f | 2015-02-11 15:54:54 +0200 | [diff] [blame] | 616 | char buf[4]; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 617 | WFILE wf; |
Serhiy Storchaka | ce921c62 | 2015-02-11 15:53:31 +0200 | [diff] [blame] | 618 | memset(&wf, 0, sizeof(wf)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 619 | wf.fp = fp; |
Serhiy Storchaka | c1efe5f | 2015-02-11 15:54:54 +0200 | [diff] [blame] | 620 | wf.ptr = wf.buf = buf; |
| 621 | wf.end = wf.ptr + sizeof(buf); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 622 | wf.error = WFERR_OK; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 623 | wf.version = version; |
| 624 | w_long(x, &wf); |
Serhiy Storchaka | c1efe5f | 2015-02-11 15:54:54 +0200 | [diff] [blame] | 625 | w_flush(&wf); |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 626 | } |
| 627 | |
| 628 | void |
Martin v. Löwis | ef82d2f | 2004-06-27 16:51:46 +0000 | [diff] [blame] | 629 | PyMarshal_WriteObjectToFile(PyObject *x, FILE *fp, int version) |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 630 | { |
Serhiy Storchaka | c1efe5f | 2015-02-11 15:54:54 +0200 | [diff] [blame] | 631 | char buf[BUFSIZ]; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 632 | WFILE wf; |
Serhiy Storchaka | ce921c62 | 2015-02-11 15:53:31 +0200 | [diff] [blame] | 633 | memset(&wf, 0, sizeof(wf)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 634 | wf.fp = fp; |
Serhiy Storchaka | c1efe5f | 2015-02-11 15:54:54 +0200 | [diff] [blame] | 635 | wf.ptr = wf.buf = buf; |
| 636 | wf.end = wf.ptr + sizeof(buf); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 637 | wf.error = WFERR_OK; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 638 | wf.version = version; |
Serhiy Storchaka | ce921c62 | 2015-02-11 15:53:31 +0200 | [diff] [blame] | 639 | if (w_init_refs(&wf, version)) |
| 640 | return; /* caller mush check PyErr_Occurred() */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 641 | w_object(x, &wf); |
Serhiy Storchaka | ce921c62 | 2015-02-11 15:53:31 +0200 | [diff] [blame] | 642 | w_clear_refs(&wf); |
Serhiy Storchaka | c1efe5f | 2015-02-11 15:54:54 +0200 | [diff] [blame] | 643 | w_flush(&wf); |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 644 | } |
| 645 | |
Serhiy Storchaka | c07422c | 2015-02-11 16:18:09 +0200 | [diff] [blame] | 646 | typedef struct { |
| 647 | FILE *fp; |
| 648 | int depth; |
| 649 | PyObject *readable; /* Stream-like object being read from */ |
| 650 | PyObject *current_filename; |
| 651 | char *ptr; |
| 652 | char *end; |
| 653 | char *buf; |
| 654 | Py_ssize_t buf_size; |
| 655 | PyObject *refs; /* a list */ |
| 656 | } RFILE; |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 657 | |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 658 | static const char * |
Antoine Pitrou | 1164dfc | 2013-10-12 22:25:39 +0200 | [diff] [blame] | 659 | r_string(Py_ssize_t n, RFILE *p) |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 660 | { |
Antoine Pitrou | 1164dfc | 2013-10-12 22:25:39 +0200 | [diff] [blame] | 661 | Py_ssize_t read = -1; |
Vinay Sajip | 5bdae3b | 2011-07-02 16:42:47 +0100 | [diff] [blame] | 662 | |
Antoine Pitrou | 1164dfc | 2013-10-12 22:25:39 +0200 | [diff] [blame] | 663 | if (p->ptr != NULL) { |
| 664 | /* Fast path for loads() */ |
| 665 | char *res = p->ptr; |
| 666 | Py_ssize_t left = p->end - p->ptr; |
| 667 | if (left < n) { |
| 668 | PyErr_SetString(PyExc_EOFError, |
| 669 | "marshal data too short"); |
| 670 | return NULL; |
Vinay Sajip | 5bdae3b | 2011-07-02 16:42:47 +0100 | [diff] [blame] | 671 | } |
Antoine Pitrou | 1164dfc | 2013-10-12 22:25:39 +0200 | [diff] [blame] | 672 | p->ptr += n; |
| 673 | return res; |
| 674 | } |
| 675 | if (p->buf == NULL) { |
| 676 | p->buf = PyMem_MALLOC(n); |
| 677 | if (p->buf == NULL) { |
| 678 | PyErr_NoMemory(); |
| 679 | return NULL; |
| 680 | } |
| 681 | p->buf_size = n; |
| 682 | } |
| 683 | else if (p->buf_size < n) { |
| 684 | p->buf = PyMem_REALLOC(p->buf, n); |
| 685 | if (p->buf == NULL) { |
| 686 | PyErr_NoMemory(); |
| 687 | return NULL; |
| 688 | } |
| 689 | p->buf_size = n; |
| 690 | } |
Victor Stinner | 763b0d1 | 2013-10-31 16:56:38 +0100 | [diff] [blame] | 691 | |
Antoine Pitrou | 1164dfc | 2013-10-12 22:25:39 +0200 | [diff] [blame] | 692 | if (!p->readable) { |
| 693 | assert(p->fp != NULL); |
Antoine Pitrou | 1164dfc | 2013-10-12 22:25:39 +0200 | [diff] [blame] | 694 | read = fread(p->buf, 1, n, p->fp); |
Vinay Sajip | 5bdae3b | 2011-07-02 16:42:47 +0100 | [diff] [blame] | 695 | } |
| 696 | else { |
Antoine Pitrou | 1164dfc | 2013-10-12 22:25:39 +0200 | [diff] [blame] | 697 | _Py_IDENTIFIER(readinto); |
| 698 | PyObject *res, *mview; |
| 699 | Py_buffer buf; |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 700 | |
Antoine Pitrou | 1164dfc | 2013-10-12 22:25:39 +0200 | [diff] [blame] | 701 | if (PyBuffer_FillInfo(&buf, NULL, p->buf, n, 0, PyBUF_CONTIG) == -1) |
| 702 | return NULL; |
| 703 | mview = PyMemoryView_FromBuffer(&buf); |
| 704 | if (mview == NULL) |
| 705 | return NULL; |
| 706 | |
| 707 | res = _PyObject_CallMethodId(p->readable, &PyId_readinto, "N", mview); |
| 708 | if (res != NULL) { |
| 709 | read = PyNumber_AsSsize_t(res, PyExc_ValueError); |
| 710 | Py_DECREF(res); |
Vinay Sajip | 5bdae3b | 2011-07-02 16:42:47 +0100 | [diff] [blame] | 711 | } |
| 712 | } |
Antoine Pitrou | 1164dfc | 2013-10-12 22:25:39 +0200 | [diff] [blame] | 713 | if (read != n) { |
| 714 | if (!PyErr_Occurred()) { |
| 715 | if (read > n) |
| 716 | PyErr_Format(PyExc_ValueError, |
| 717 | "read() returned too much data: " |
| 718 | "%zd bytes requested, %zd returned", |
| 719 | n, read); |
| 720 | else |
| 721 | PyErr_SetString(PyExc_EOFError, |
| 722 | "EOF read where not expected"); |
| 723 | } |
| 724 | return NULL; |
Vinay Sajip | 5bdae3b | 2011-07-02 16:42:47 +0100 | [diff] [blame] | 725 | } |
Antoine Pitrou | 1164dfc | 2013-10-12 22:25:39 +0200 | [diff] [blame] | 726 | return p->buf; |
Vinay Sajip | 5bdae3b | 2011-07-02 16:42:47 +0100 | [diff] [blame] | 727 | } |
| 728 | |
Vinay Sajip | 5bdae3b | 2011-07-02 16:42:47 +0100 | [diff] [blame] | 729 | static int |
| 730 | r_byte(RFILE *p) |
| 731 | { |
| 732 | int c = EOF; |
Vinay Sajip | 5bdae3b | 2011-07-02 16:42:47 +0100 | [diff] [blame] | 733 | |
Antoine Pitrou | 1164dfc | 2013-10-12 22:25:39 +0200 | [diff] [blame] | 734 | if (p->ptr != NULL) { |
| 735 | if (p->ptr < p->end) |
| 736 | c = (unsigned char) *p->ptr++; |
| 737 | return c; |
| 738 | } |
| 739 | if (!p->readable) { |
| 740 | assert(p->fp); |
| 741 | c = getc(p->fp); |
| 742 | } |
Vinay Sajip | 5bdae3b | 2011-07-02 16:42:47 +0100 | [diff] [blame] | 743 | else { |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 744 | const char *ptr = r_string(1, p); |
Antoine Pitrou | 1164dfc | 2013-10-12 22:25:39 +0200 | [diff] [blame] | 745 | if (ptr != NULL) |
| 746 | c = *(unsigned char *) ptr; |
Vinay Sajip | 5bdae3b | 2011-07-02 16:42:47 +0100 | [diff] [blame] | 747 | } |
| 748 | return c; |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 749 | } |
| 750 | |
| 751 | static int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 752 | r_short(RFILE *p) |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 753 | { |
Antoine Pitrou | 1164dfc | 2013-10-12 22:25:39 +0200 | [diff] [blame] | 754 | short x = -1; |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 755 | const unsigned char *buffer; |
Vinay Sajip | 5bdae3b | 2011-07-02 16:42:47 +0100 | [diff] [blame] | 756 | |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 757 | buffer = (const unsigned char *) r_string(2, p); |
Antoine Pitrou | 1164dfc | 2013-10-12 22:25:39 +0200 | [diff] [blame] | 758 | if (buffer != NULL) { |
| 759 | x = buffer[0]; |
| 760 | x |= buffer[1] << 8; |
| 761 | /* Sign-extension, in case short greater than 16 bits */ |
| 762 | x |= -(x & 0x8000); |
| 763 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 764 | return x; |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 765 | } |
| 766 | |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 767 | static long |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 768 | r_long(RFILE *p) |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 769 | { |
Antoine Pitrou | 1164dfc | 2013-10-12 22:25:39 +0200 | [diff] [blame] | 770 | long x = -1; |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 771 | const unsigned char *buffer; |
Vinay Sajip | 5bdae3b | 2011-07-02 16:42:47 +0100 | [diff] [blame] | 772 | |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 773 | buffer = (const unsigned char *) r_string(4, p); |
Antoine Pitrou | 1164dfc | 2013-10-12 22:25:39 +0200 | [diff] [blame] | 774 | if (buffer != NULL) { |
| 775 | x = buffer[0]; |
| 776 | x |= (long)buffer[1] << 8; |
| 777 | x |= (long)buffer[2] << 16; |
| 778 | x |= (long)buffer[3] << 24; |
Guido van Rossum | c1547d9 | 1996-12-10 15:39:04 +0000 | [diff] [blame] | 779 | #if SIZEOF_LONG > 4 |
Antoine Pitrou | 1164dfc | 2013-10-12 22:25:39 +0200 | [diff] [blame] | 780 | /* Sign extension for 64-bit machines */ |
| 781 | x |= -(x & 0x80000000L); |
Guido van Rossum | c1547d9 | 1996-12-10 15:39:04 +0000 | [diff] [blame] | 782 | #endif |
Antoine Pitrou | 1164dfc | 2013-10-12 22:25:39 +0200 | [diff] [blame] | 783 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 784 | return x; |
Guido van Rossum | b0c168c | 1996-12-05 23:15:02 +0000 | [diff] [blame] | 785 | } |
| 786 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 787 | static PyObject * |
Mark Dickinson | bd79264 | 2009-03-18 20:06:12 +0000 | [diff] [blame] | 788 | r_PyLong(RFILE *p) |
| 789 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 790 | PyLongObject *ob; |
Serhiy Storchaka | 7e01911 | 2013-02-13 12:08:15 +0200 | [diff] [blame] | 791 | long n, size, i; |
| 792 | int j, md, shorts_in_top_digit; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 793 | digit d; |
Mark Dickinson | bd79264 | 2009-03-18 20:06:12 +0000 | [diff] [blame] | 794 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 795 | n = r_long(p); |
Vinay Sajip | 5bdae3b | 2011-07-02 16:42:47 +0100 | [diff] [blame] | 796 | if (PyErr_Occurred()) |
| 797 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 798 | if (n == 0) |
| 799 | return (PyObject *)_PyLong_New(0); |
Serhiy Storchaka | 7e01911 | 2013-02-13 12:08:15 +0200 | [diff] [blame] | 800 | if (n < -SIZE32_MAX || n > SIZE32_MAX) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 801 | PyErr_SetString(PyExc_ValueError, |
| 802 | "bad marshal data (long size out of range)"); |
| 803 | return NULL; |
| 804 | } |
Mark Dickinson | bd79264 | 2009-03-18 20:06:12 +0000 | [diff] [blame] | 805 | |
Victor Stinner | 45e8e2f | 2014-05-14 17:24:35 +0200 | [diff] [blame] | 806 | size = 1 + (Py_ABS(n) - 1) / PyLong_MARSHAL_RATIO; |
| 807 | shorts_in_top_digit = 1 + (Py_ABS(n) - 1) % PyLong_MARSHAL_RATIO; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 808 | ob = _PyLong_New(size); |
| 809 | if (ob == NULL) |
| 810 | return NULL; |
Victor Stinner | 763b0d1 | 2013-10-31 16:56:38 +0100 | [diff] [blame] | 811 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 812 | Py_SIZE(ob) = n > 0 ? size : -size; |
Mark Dickinson | bd79264 | 2009-03-18 20:06:12 +0000 | [diff] [blame] | 813 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 814 | for (i = 0; i < size-1; i++) { |
| 815 | d = 0; |
| 816 | for (j=0; j < PyLong_MARSHAL_RATIO; j++) { |
| 817 | md = r_short(p); |
Victor Stinner | 763b0d1 | 2013-10-31 16:56:38 +0100 | [diff] [blame] | 818 | if (PyErr_Occurred()) { |
| 819 | Py_DECREF(ob); |
| 820 | return NULL; |
| 821 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 822 | if (md < 0 || md > PyLong_MARSHAL_BASE) |
| 823 | goto bad_digit; |
| 824 | d += (digit)md << j*PyLong_MARSHAL_SHIFT; |
| 825 | } |
| 826 | ob->ob_digit[i] = d; |
| 827 | } |
Victor Stinner | 763b0d1 | 2013-10-31 16:56:38 +0100 | [diff] [blame] | 828 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 829 | d = 0; |
| 830 | for (j=0; j < shorts_in_top_digit; j++) { |
| 831 | md = r_short(p); |
Victor Stinner | 763b0d1 | 2013-10-31 16:56:38 +0100 | [diff] [blame] | 832 | if (PyErr_Occurred()) { |
| 833 | Py_DECREF(ob); |
| 834 | return NULL; |
| 835 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 836 | if (md < 0 || md > PyLong_MARSHAL_BASE) |
| 837 | goto bad_digit; |
| 838 | /* topmost marshal digit should be nonzero */ |
| 839 | if (md == 0 && j == shorts_in_top_digit - 1) { |
| 840 | Py_DECREF(ob); |
| 841 | PyErr_SetString(PyExc_ValueError, |
| 842 | "bad marshal data (unnormalized long data)"); |
| 843 | return NULL; |
| 844 | } |
| 845 | d += (digit)md << j*PyLong_MARSHAL_SHIFT; |
| 846 | } |
Vinay Sajip | 5bdae3b | 2011-07-02 16:42:47 +0100 | [diff] [blame] | 847 | if (PyErr_Occurred()) { |
| 848 | Py_DECREF(ob); |
| 849 | return NULL; |
| 850 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 851 | /* top digit should be nonzero, else the resulting PyLong won't be |
| 852 | normalized */ |
| 853 | ob->ob_digit[size-1] = d; |
| 854 | return (PyObject *)ob; |
Mark Dickinson | bd79264 | 2009-03-18 20:06:12 +0000 | [diff] [blame] | 855 | bad_digit: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 856 | Py_DECREF(ob); |
| 857 | PyErr_SetString(PyExc_ValueError, |
| 858 | "bad marshal data (digit out of range in long)"); |
| 859 | return NULL; |
Mark Dickinson | bd79264 | 2009-03-18 20:06:12 +0000 | [diff] [blame] | 860 | } |
| 861 | |
Kristján Valur Jónsson | e178187 | 2013-03-20 11:43:57 -0700 | [diff] [blame] | 862 | /* allocate the reflist index for a new object. Return -1 on failure */ |
| 863 | static Py_ssize_t |
| 864 | r_ref_reserve(int flag, RFILE *p) |
Kristján Valur Jónsson | d7009c6 | 2013-03-19 18:02:10 -0700 | [diff] [blame] | 865 | { |
| 866 | if (flag) { /* currently only FLAG_REF is defined */ |
Antoine Pitrou | 1164dfc | 2013-10-12 22:25:39 +0200 | [diff] [blame] | 867 | Py_ssize_t idx = PyList_GET_SIZE(p->refs); |
Kristján Valur Jónsson | e178187 | 2013-03-20 11:43:57 -0700 | [diff] [blame] | 868 | if (idx >= 0x7ffffffe) { |
Kristján Valur Jónsson | d7009c6 | 2013-03-19 18:02:10 -0700 | [diff] [blame] | 869 | PyErr_SetString(PyExc_ValueError, "bad marshal data (index list too large)"); |
Kristján Valur Jónsson | e178187 | 2013-03-20 11:43:57 -0700 | [diff] [blame] | 870 | return -1; |
Kristján Valur Jónsson | d7009c6 | 2013-03-19 18:02:10 -0700 | [diff] [blame] | 871 | } |
| 872 | if (PyList_Append(p->refs, Py_None) < 0) |
Kristján Valur Jónsson | e178187 | 2013-03-20 11:43:57 -0700 | [diff] [blame] | 873 | return -1; |
| 874 | return idx; |
Kristján Valur Jónsson | d7009c6 | 2013-03-19 18:02:10 -0700 | [diff] [blame] | 875 | } else |
Kristján Valur Jónsson | e178187 | 2013-03-20 11:43:57 -0700 | [diff] [blame] | 876 | return 0; |
Kristján Valur Jónsson | d7009c6 | 2013-03-19 18:02:10 -0700 | [diff] [blame] | 877 | } |
| 878 | |
Kristján Valur Jónsson | e178187 | 2013-03-20 11:43:57 -0700 | [diff] [blame] | 879 | /* insert the new object 'o' to the reflist at previously |
| 880 | * allocated index 'idx'. |
| 881 | * 'o' can be NULL, in which case nothing is done. |
| 882 | * if 'o' was non-NULL, and the function succeeds, 'o' is returned. |
| 883 | * if 'o' was non-NULL, and the function fails, 'o' is released and |
| 884 | * NULL returned. This simplifies error checking at the call site since |
| 885 | * a single test for NULL for the function result is enough. |
| 886 | */ |
Kristján Valur Jónsson | d7009c6 | 2013-03-19 18:02:10 -0700 | [diff] [blame] | 887 | static PyObject * |
| 888 | r_ref_insert(PyObject *o, Py_ssize_t idx, int flag, RFILE *p) |
| 889 | { |
Kristján Valur Jónsson | e178187 | 2013-03-20 11:43:57 -0700 | [diff] [blame] | 890 | if (o != NULL && flag) { /* currently only FLAG_REF is defined */ |
Antoine Pitrou | 1164dfc | 2013-10-12 22:25:39 +0200 | [diff] [blame] | 891 | PyObject *tmp = PyList_GET_ITEM(p->refs, idx); |
| 892 | Py_INCREF(o); |
| 893 | PyList_SET_ITEM(p->refs, idx, o); |
| 894 | Py_DECREF(tmp); |
Kristján Valur Jónsson | d7009c6 | 2013-03-19 18:02:10 -0700 | [diff] [blame] | 895 | } |
| 896 | return o; |
| 897 | } |
| 898 | |
| 899 | /* combination of both above, used when an object can be |
| 900 | * created whenever it is seen in the file, as opposed to |
| 901 | * after having loaded its sub-objects. |
| 902 | */ |
| 903 | static PyObject * |
| 904 | r_ref(PyObject *o, int flag, RFILE *p) |
| 905 | { |
Victor Stinner | 359fabc | 2013-10-31 17:09:01 +0100 | [diff] [blame] | 906 | assert(flag & FLAG_REF); |
| 907 | if (o == NULL) |
| 908 | return NULL; |
| 909 | if (PyList_Append(p->refs, o) < 0) { |
| 910 | Py_DECREF(o); /* release the new object */ |
| 911 | return NULL; |
Kristján Valur Jónsson | d7009c6 | 2013-03-19 18:02:10 -0700 | [diff] [blame] | 912 | } |
| 913 | return o; |
| 914 | } |
Mark Dickinson | bd79264 | 2009-03-18 20:06:12 +0000 | [diff] [blame] | 915 | |
| 916 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 917 | r_object(RFILE *p) |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 918 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 919 | /* NULL is a valid return value, it does not necessarily means that |
| 920 | an exception is set. */ |
| 921 | PyObject *v, *v2; |
Benjamin Peterson | eddb0a7 | 2013-03-20 00:40:07 -0500 | [diff] [blame] | 922 | Py_ssize_t idx = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 923 | long i, n; |
Kristján Valur Jónsson | 6168362 | 2013-03-20 14:26:33 -0700 | [diff] [blame] | 924 | int type, code = r_byte(p); |
Antoine Pitrou | 1164dfc | 2013-10-12 22:25:39 +0200 | [diff] [blame] | 925 | int flag, is_interned = 0; |
Christian Heimes | 3572842 | 2013-10-13 02:29:06 +0200 | [diff] [blame] | 926 | PyObject *retval = NULL; |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 927 | |
Kristján Valur Jónsson | 6168362 | 2013-03-20 14:26:33 -0700 | [diff] [blame] | 928 | if (code == EOF) { |
| 929 | PyErr_SetString(PyExc_EOFError, |
| 930 | "EOF read where object expected"); |
| 931 | return NULL; |
| 932 | } |
| 933 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 934 | p->depth++; |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 935 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 936 | if (p->depth > MAX_MARSHAL_STACK_DEPTH) { |
| 937 | p->depth--; |
| 938 | PyErr_SetString(PyExc_ValueError, "recursion limit exceeded"); |
| 939 | return NULL; |
| 940 | } |
Tim Peters | d9b9ac8 | 2001-01-28 00:27:39 +0000 | [diff] [blame] | 941 | |
Kristján Valur Jónsson | 6168362 | 2013-03-20 14:26:33 -0700 | [diff] [blame] | 942 | flag = code & FLAG_REF; |
| 943 | type = code & ~FLAG_REF; |
Kristján Valur Jónsson | d7009c6 | 2013-03-19 18:02:10 -0700 | [diff] [blame] | 944 | |
| 945 | #define R_REF(O) do{\ |
Kristján Valur Jónsson | e178187 | 2013-03-20 11:43:57 -0700 | [diff] [blame] | 946 | if (flag) \ |
| 947 | O = r_ref(O, flag, p);\ |
Kristján Valur Jónsson | d7009c6 | 2013-03-19 18:02:10 -0700 | [diff] [blame] | 948 | } while (0) |
| 949 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 950 | switch (type) { |
Tim Peters | d9b9ac8 | 2001-01-28 00:27:39 +0000 | [diff] [blame] | 951 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 952 | case TYPE_NULL: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 953 | break; |
Tim Peters | d9b9ac8 | 2001-01-28 00:27:39 +0000 | [diff] [blame] | 954 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 955 | case TYPE_NONE: |
| 956 | Py_INCREF(Py_None); |
| 957 | retval = Py_None; |
| 958 | break; |
Tim Peters | d9b9ac8 | 2001-01-28 00:27:39 +0000 | [diff] [blame] | 959 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 960 | case TYPE_STOPITER: |
| 961 | Py_INCREF(PyExc_StopIteration); |
| 962 | retval = PyExc_StopIteration; |
| 963 | break; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 964 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 965 | case TYPE_ELLIPSIS: |
| 966 | Py_INCREF(Py_Ellipsis); |
| 967 | retval = Py_Ellipsis; |
| 968 | break; |
Tim Peters | d9b9ac8 | 2001-01-28 00:27:39 +0000 | [diff] [blame] | 969 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 970 | case TYPE_FALSE: |
| 971 | Py_INCREF(Py_False); |
| 972 | retval = Py_False; |
| 973 | break; |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 974 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 975 | case TYPE_TRUE: |
| 976 | Py_INCREF(Py_True); |
| 977 | retval = Py_True; |
| 978 | break; |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 979 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 980 | case TYPE_INT: |
Vinay Sajip | 5bdae3b | 2011-07-02 16:42:47 +0100 | [diff] [blame] | 981 | n = r_long(p); |
| 982 | retval = PyErr_Occurred() ? NULL : PyLong_FromLong(n); |
Kristján Valur Jónsson | d7009c6 | 2013-03-19 18:02:10 -0700 | [diff] [blame] | 983 | R_REF(retval); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 984 | break; |
Tim Peters | d9b9ac8 | 2001-01-28 00:27:39 +0000 | [diff] [blame] | 985 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 986 | case TYPE_LONG: |
| 987 | retval = r_PyLong(p); |
Kristján Valur Jónsson | d7009c6 | 2013-03-19 18:02:10 -0700 | [diff] [blame] | 988 | R_REF(retval); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 989 | break; |
Tim Peters | d9b9ac8 | 2001-01-28 00:27:39 +0000 | [diff] [blame] | 990 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 991 | case TYPE_FLOAT: |
| 992 | { |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 993 | char buf[256]; |
| 994 | const char *ptr; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 995 | double dx; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 996 | n = r_byte(p); |
Serhiy Storchaka | 3641a74 | 2013-07-11 22:20:47 +0300 | [diff] [blame] | 997 | if (n == EOF) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 998 | PyErr_SetString(PyExc_EOFError, |
| 999 | "EOF read where object expected"); |
| 1000 | break; |
| 1001 | } |
Antoine Pitrou | 1164dfc | 2013-10-12 22:25:39 +0200 | [diff] [blame] | 1002 | ptr = r_string(n, p); |
| 1003 | if (ptr == NULL) |
Serhiy Storchaka | 3641a74 | 2013-07-11 22:20:47 +0300 | [diff] [blame] | 1004 | break; |
Antoine Pitrou | 1164dfc | 2013-10-12 22:25:39 +0200 | [diff] [blame] | 1005 | memcpy(buf, ptr, n); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1006 | buf[n] = '\0'; |
| 1007 | dx = PyOS_string_to_double(buf, NULL, NULL); |
| 1008 | if (dx == -1.0 && PyErr_Occurred()) |
| 1009 | break; |
| 1010 | retval = PyFloat_FromDouble(dx); |
Kristján Valur Jónsson | d7009c6 | 2013-03-19 18:02:10 -0700 | [diff] [blame] | 1011 | R_REF(retval); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1012 | break; |
| 1013 | } |
Tim Peters | d9b9ac8 | 2001-01-28 00:27:39 +0000 | [diff] [blame] | 1014 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1015 | case TYPE_BINARY_FLOAT: |
| 1016 | { |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1017 | const unsigned char *buf; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1018 | double x; |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1019 | buf = (const unsigned char *) r_string(8, p); |
Kristján Valur Jónsson | 5983258 | 2013-10-13 13:41:59 +0000 | [diff] [blame] | 1020 | if (buf == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1021 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1022 | x = _PyFloat_Unpack8(buf, 1); |
Kristján Valur Jónsson | 5983258 | 2013-10-13 13:41:59 +0000 | [diff] [blame] | 1023 | if (x == -1.0 && PyErr_Occurred()) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1024 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1025 | retval = PyFloat_FromDouble(x); |
Kristján Valur Jónsson | d7009c6 | 2013-03-19 18:02:10 -0700 | [diff] [blame] | 1026 | R_REF(retval); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1027 | break; |
| 1028 | } |
Michael W. Hudson | df88846 | 2005-06-03 14:41:55 +0000 | [diff] [blame] | 1029 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1030 | case TYPE_COMPLEX: |
| 1031 | { |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1032 | char buf[256]; |
| 1033 | const char *ptr; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1034 | Py_complex c; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1035 | n = r_byte(p); |
Serhiy Storchaka | 3641a74 | 2013-07-11 22:20:47 +0300 | [diff] [blame] | 1036 | if (n == EOF) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1037 | PyErr_SetString(PyExc_EOFError, |
| 1038 | "EOF read where object expected"); |
| 1039 | break; |
| 1040 | } |
Antoine Pitrou | 1164dfc | 2013-10-12 22:25:39 +0200 | [diff] [blame] | 1041 | ptr = r_string(n, p); |
| 1042 | if (ptr == NULL) |
Serhiy Storchaka | 3641a74 | 2013-07-11 22:20:47 +0300 | [diff] [blame] | 1043 | break; |
Antoine Pitrou | 1164dfc | 2013-10-12 22:25:39 +0200 | [diff] [blame] | 1044 | memcpy(buf, ptr, n); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1045 | buf[n] = '\0'; |
| 1046 | c.real = PyOS_string_to_double(buf, NULL, NULL); |
| 1047 | if (c.real == -1.0 && PyErr_Occurred()) |
| 1048 | break; |
| 1049 | n = r_byte(p); |
Serhiy Storchaka | 3641a74 | 2013-07-11 22:20:47 +0300 | [diff] [blame] | 1050 | if (n == EOF) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1051 | PyErr_SetString(PyExc_EOFError, |
| 1052 | "EOF read where object expected"); |
| 1053 | break; |
| 1054 | } |
Antoine Pitrou | 1164dfc | 2013-10-12 22:25:39 +0200 | [diff] [blame] | 1055 | ptr = r_string(n, p); |
| 1056 | if (ptr == NULL) |
Serhiy Storchaka | 3641a74 | 2013-07-11 22:20:47 +0300 | [diff] [blame] | 1057 | break; |
Antoine Pitrou | 1164dfc | 2013-10-12 22:25:39 +0200 | [diff] [blame] | 1058 | memcpy(buf, ptr, n); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1059 | buf[n] = '\0'; |
| 1060 | c.imag = PyOS_string_to_double(buf, NULL, NULL); |
| 1061 | if (c.imag == -1.0 && PyErr_Occurred()) |
| 1062 | break; |
| 1063 | retval = PyComplex_FromCComplex(c); |
Kristján Valur Jónsson | d7009c6 | 2013-03-19 18:02:10 -0700 | [diff] [blame] | 1064 | R_REF(retval); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1065 | break; |
| 1066 | } |
Michael W. Hudson | df88846 | 2005-06-03 14:41:55 +0000 | [diff] [blame] | 1067 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1068 | case TYPE_BINARY_COMPLEX: |
| 1069 | { |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1070 | const unsigned char *buf; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1071 | Py_complex c; |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1072 | buf = (const unsigned char *) r_string(8, p); |
Kristján Valur Jónsson | 5983258 | 2013-10-13 13:41:59 +0000 | [diff] [blame] | 1073 | if (buf == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1074 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1075 | c.real = _PyFloat_Unpack8(buf, 1); |
Kristján Valur Jónsson | 5983258 | 2013-10-13 13:41:59 +0000 | [diff] [blame] | 1076 | if (c.real == -1.0 && PyErr_Occurred()) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1077 | break; |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1078 | buf = (const unsigned char *) r_string(8, p); |
Kristján Valur Jónsson | 5983258 | 2013-10-13 13:41:59 +0000 | [diff] [blame] | 1079 | if (buf == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1080 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1081 | c.imag = _PyFloat_Unpack8(buf, 1); |
Kristján Valur Jónsson | 5983258 | 2013-10-13 13:41:59 +0000 | [diff] [blame] | 1082 | if (c.imag == -1.0 && PyErr_Occurred()) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1083 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1084 | retval = PyComplex_FromCComplex(c); |
Kristján Valur Jónsson | d7009c6 | 2013-03-19 18:02:10 -0700 | [diff] [blame] | 1085 | R_REF(retval); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1086 | break; |
| 1087 | } |
Tim Peters | d9b9ac8 | 2001-01-28 00:27:39 +0000 | [diff] [blame] | 1088 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1089 | case TYPE_STRING: |
Antoine Pitrou | 1164dfc | 2013-10-12 22:25:39 +0200 | [diff] [blame] | 1090 | { |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1091 | const char *ptr; |
Antoine Pitrou | 1164dfc | 2013-10-12 22:25:39 +0200 | [diff] [blame] | 1092 | n = r_long(p); |
Kristján Valur Jónsson | 5983258 | 2013-10-13 13:41:59 +0000 | [diff] [blame] | 1093 | if (PyErr_Occurred()) |
Antoine Pitrou | 1164dfc | 2013-10-12 22:25:39 +0200 | [diff] [blame] | 1094 | break; |
Antoine Pitrou | 1164dfc | 2013-10-12 22:25:39 +0200 | [diff] [blame] | 1095 | if (n < 0 || n > SIZE32_MAX) { |
Serhiy Storchaka | c611a5b | 2017-03-12 08:53:22 +0200 | [diff] [blame] | 1096 | PyErr_SetString(PyExc_ValueError, "bad marshal data (bytes object size out of range)"); |
Antoine Pitrou | 1164dfc | 2013-10-12 22:25:39 +0200 | [diff] [blame] | 1097 | break; |
| 1098 | } |
| 1099 | v = PyBytes_FromStringAndSize((char *)NULL, n); |
Kristján Valur Jónsson | 5983258 | 2013-10-13 13:41:59 +0000 | [diff] [blame] | 1100 | if (v == NULL) |
Antoine Pitrou | 1164dfc | 2013-10-12 22:25:39 +0200 | [diff] [blame] | 1101 | break; |
Antoine Pitrou | 1164dfc | 2013-10-12 22:25:39 +0200 | [diff] [blame] | 1102 | ptr = r_string(n, p); |
| 1103 | if (ptr == NULL) { |
| 1104 | Py_DECREF(v); |
Antoine Pitrou | 1164dfc | 2013-10-12 22:25:39 +0200 | [diff] [blame] | 1105 | break; |
| 1106 | } |
| 1107 | memcpy(PyBytes_AS_STRING(v), ptr, n); |
| 1108 | retval = v; |
| 1109 | R_REF(retval); |
| 1110 | break; |
| 1111 | } |
| 1112 | |
| 1113 | case TYPE_ASCII_INTERNED: |
| 1114 | is_interned = 1; |
Stefan Krah | f432a32 | 2017-08-21 13:09:59 +0200 | [diff] [blame] | 1115 | /* fall through */ |
Antoine Pitrou | 1164dfc | 2013-10-12 22:25:39 +0200 | [diff] [blame] | 1116 | case TYPE_ASCII: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1117 | n = r_long(p); |
Kristján Valur Jónsson | 5983258 | 2013-10-13 13:41:59 +0000 | [diff] [blame] | 1118 | if (PyErr_Occurred()) |
Vinay Sajip | 5bdae3b | 2011-07-02 16:42:47 +0100 | [diff] [blame] | 1119 | break; |
Serhiy Storchaka | 7e01911 | 2013-02-13 12:08:15 +0200 | [diff] [blame] | 1120 | if (n < 0 || n > SIZE32_MAX) { |
Serhiy Storchaka | c611a5b | 2017-03-12 08:53:22 +0200 | [diff] [blame] | 1121 | PyErr_SetString(PyExc_ValueError, "bad marshal data (string size out of range)"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1122 | break; |
| 1123 | } |
Antoine Pitrou | 1164dfc | 2013-10-12 22:25:39 +0200 | [diff] [blame] | 1124 | goto _read_ascii; |
Tim Peters | d9b9ac8 | 2001-01-28 00:27:39 +0000 | [diff] [blame] | 1125 | |
Antoine Pitrou | 1164dfc | 2013-10-12 22:25:39 +0200 | [diff] [blame] | 1126 | case TYPE_SHORT_ASCII_INTERNED: |
| 1127 | is_interned = 1; |
Stefan Krah | f432a32 | 2017-08-21 13:09:59 +0200 | [diff] [blame] | 1128 | /* fall through */ |
Antoine Pitrou | 1164dfc | 2013-10-12 22:25:39 +0200 | [diff] [blame] | 1129 | case TYPE_SHORT_ASCII: |
| 1130 | n = r_byte(p); |
| 1131 | if (n == EOF) { |
| 1132 | PyErr_SetString(PyExc_EOFError, |
| 1133 | "EOF read where object expected"); |
| 1134 | break; |
| 1135 | } |
| 1136 | _read_ascii: |
| 1137 | { |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1138 | const char *ptr; |
Antoine Pitrou | 1164dfc | 2013-10-12 22:25:39 +0200 | [diff] [blame] | 1139 | ptr = r_string(n, p); |
Kristján Valur Jónsson | 5983258 | 2013-10-13 13:41:59 +0000 | [diff] [blame] | 1140 | if (ptr == NULL) |
Antoine Pitrou | 1164dfc | 2013-10-12 22:25:39 +0200 | [diff] [blame] | 1141 | break; |
Antoine Pitrou | 1164dfc | 2013-10-12 22:25:39 +0200 | [diff] [blame] | 1142 | v = PyUnicode_FromKindAndData(PyUnicode_1BYTE_KIND, ptr, n); |
Kristján Valur Jónsson | 5983258 | 2013-10-13 13:41:59 +0000 | [diff] [blame] | 1143 | if (v == NULL) |
Antoine Pitrou | 1164dfc | 2013-10-12 22:25:39 +0200 | [diff] [blame] | 1144 | break; |
Antoine Pitrou | 1164dfc | 2013-10-12 22:25:39 +0200 | [diff] [blame] | 1145 | if (is_interned) |
| 1146 | PyUnicode_InternInPlace(&v); |
| 1147 | retval = v; |
| 1148 | R_REF(retval); |
| 1149 | break; |
| 1150 | } |
| 1151 | |
Kristján Valur Jónsson | d7009c6 | 2013-03-19 18:02:10 -0700 | [diff] [blame] | 1152 | case TYPE_INTERNED: |
Antoine Pitrou | 1164dfc | 2013-10-12 22:25:39 +0200 | [diff] [blame] | 1153 | is_interned = 1; |
Stefan Krah | f432a32 | 2017-08-21 13:09:59 +0200 | [diff] [blame] | 1154 | /* fall through */ |
Antoine Pitrou | 1164dfc | 2013-10-12 22:25:39 +0200 | [diff] [blame] | 1155 | case TYPE_UNICODE: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1156 | { |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1157 | const char *buffer; |
Guido van Rossum | c279b53 | 2000-03-10 23:03:02 +0000 | [diff] [blame] | 1158 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1159 | n = r_long(p); |
Kristján Valur Jónsson | 5983258 | 2013-10-13 13:41:59 +0000 | [diff] [blame] | 1160 | if (PyErr_Occurred()) |
Vinay Sajip | 5bdae3b | 2011-07-02 16:42:47 +0100 | [diff] [blame] | 1161 | break; |
Serhiy Storchaka | 7e01911 | 2013-02-13 12:08:15 +0200 | [diff] [blame] | 1162 | if (n < 0 || n > SIZE32_MAX) { |
Serhiy Storchaka | c611a5b | 2017-03-12 08:53:22 +0200 | [diff] [blame] | 1163 | PyErr_SetString(PyExc_ValueError, "bad marshal data (string size out of range)"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1164 | break; |
| 1165 | } |
Victor Stinner | f1913ca | 2013-06-21 19:08:06 +0200 | [diff] [blame] | 1166 | if (n != 0) { |
Antoine Pitrou | 1164dfc | 2013-10-12 22:25:39 +0200 | [diff] [blame] | 1167 | buffer = r_string(n, p); |
Kristján Valur Jónsson | 5983258 | 2013-10-13 13:41:59 +0000 | [diff] [blame] | 1168 | if (buffer == NULL) |
Victor Stinner | f1913ca | 2013-06-21 19:08:06 +0200 | [diff] [blame] | 1169 | break; |
Victor Stinner | f1913ca | 2013-06-21 19:08:06 +0200 | [diff] [blame] | 1170 | v = PyUnicode_DecodeUTF8(buffer, n, "surrogatepass"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1171 | } |
Victor Stinner | f1913ca | 2013-06-21 19:08:06 +0200 | [diff] [blame] | 1172 | else { |
| 1173 | v = PyUnicode_New(0, 0); |
| 1174 | } |
Kristján Valur Jónsson | 5983258 | 2013-10-13 13:41:59 +0000 | [diff] [blame] | 1175 | if (v == NULL) |
Victor Stinner | 3a8b79d | 2013-07-08 22:23:32 +0200 | [diff] [blame] | 1176 | break; |
Antoine Pitrou | 1164dfc | 2013-10-12 22:25:39 +0200 | [diff] [blame] | 1177 | if (is_interned) |
Kristján Valur Jónsson | d7009c6 | 2013-03-19 18:02:10 -0700 | [diff] [blame] | 1178 | PyUnicode_InternInPlace(&v); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1179 | retval = v; |
Kristján Valur Jónsson | d7009c6 | 2013-03-19 18:02:10 -0700 | [diff] [blame] | 1180 | R_REF(retval); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1181 | break; |
| 1182 | } |
Tim Peters | d9b9ac8 | 2001-01-28 00:27:39 +0000 | [diff] [blame] | 1183 | |
Antoine Pitrou | 1164dfc | 2013-10-12 22:25:39 +0200 | [diff] [blame] | 1184 | case TYPE_SMALL_TUPLE: |
| 1185 | n = (unsigned char) r_byte(p); |
Victor Stinner | b1b7b18 | 2013-10-31 17:07:08 +0100 | [diff] [blame] | 1186 | if (PyErr_Occurred()) |
| 1187 | break; |
Antoine Pitrou | 1164dfc | 2013-10-12 22:25:39 +0200 | [diff] [blame] | 1188 | goto _read_tuple; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1189 | case TYPE_TUPLE: |
| 1190 | n = r_long(p); |
Kristján Valur Jónsson | 5983258 | 2013-10-13 13:41:59 +0000 | [diff] [blame] | 1191 | if (PyErr_Occurred()) |
Vinay Sajip | 5bdae3b | 2011-07-02 16:42:47 +0100 | [diff] [blame] | 1192 | break; |
Serhiy Storchaka | 7e01911 | 2013-02-13 12:08:15 +0200 | [diff] [blame] | 1193 | if (n < 0 || n > SIZE32_MAX) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1194 | PyErr_SetString(PyExc_ValueError, "bad marshal data (tuple size out of range)"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1195 | break; |
| 1196 | } |
Antoine Pitrou | 1164dfc | 2013-10-12 22:25:39 +0200 | [diff] [blame] | 1197 | _read_tuple: |
Serhiy Storchaka | 7e01911 | 2013-02-13 12:08:15 +0200 | [diff] [blame] | 1198 | v = PyTuple_New(n); |
Kristján Valur Jónsson | d7009c6 | 2013-03-19 18:02:10 -0700 | [diff] [blame] | 1199 | R_REF(v); |
Kristján Valur Jónsson | 5983258 | 2013-10-13 13:41:59 +0000 | [diff] [blame] | 1200 | if (v == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1201 | break; |
Victor Stinner | d5cae6f | 2013-10-31 17:14:52 +0100 | [diff] [blame] | 1202 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1203 | for (i = 0; i < n; i++) { |
| 1204 | v2 = r_object(p); |
| 1205 | if ( v2 == NULL ) { |
| 1206 | if (!PyErr_Occurred()) |
| 1207 | PyErr_SetString(PyExc_TypeError, |
| 1208 | "NULL object in marshal data for tuple"); |
| 1209 | Py_DECREF(v); |
| 1210 | v = NULL; |
| 1211 | break; |
| 1212 | } |
Serhiy Storchaka | 7e01911 | 2013-02-13 12:08:15 +0200 | [diff] [blame] | 1213 | PyTuple_SET_ITEM(v, i, v2); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1214 | } |
| 1215 | retval = v; |
| 1216 | break; |
Tim Peters | d9b9ac8 | 2001-01-28 00:27:39 +0000 | [diff] [blame] | 1217 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1218 | case TYPE_LIST: |
| 1219 | n = r_long(p); |
Kristján Valur Jónsson | 5983258 | 2013-10-13 13:41:59 +0000 | [diff] [blame] | 1220 | if (PyErr_Occurred()) |
Vinay Sajip | 5bdae3b | 2011-07-02 16:42:47 +0100 | [diff] [blame] | 1221 | break; |
Serhiy Storchaka | 7e01911 | 2013-02-13 12:08:15 +0200 | [diff] [blame] | 1222 | if (n < 0 || n > SIZE32_MAX) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1223 | PyErr_SetString(PyExc_ValueError, "bad marshal data (list size out of range)"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1224 | break; |
| 1225 | } |
Serhiy Storchaka | 7e01911 | 2013-02-13 12:08:15 +0200 | [diff] [blame] | 1226 | v = PyList_New(n); |
Kristján Valur Jónsson | d7009c6 | 2013-03-19 18:02:10 -0700 | [diff] [blame] | 1227 | R_REF(v); |
Kristján Valur Jónsson | 5983258 | 2013-10-13 13:41:59 +0000 | [diff] [blame] | 1228 | if (v == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1229 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1230 | for (i = 0; i < n; i++) { |
| 1231 | v2 = r_object(p); |
| 1232 | if ( v2 == NULL ) { |
| 1233 | if (!PyErr_Occurred()) |
| 1234 | PyErr_SetString(PyExc_TypeError, |
| 1235 | "NULL object in marshal data for list"); |
| 1236 | Py_DECREF(v); |
| 1237 | v = NULL; |
| 1238 | break; |
| 1239 | } |
Serhiy Storchaka | 7e01911 | 2013-02-13 12:08:15 +0200 | [diff] [blame] | 1240 | PyList_SET_ITEM(v, i, v2); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1241 | } |
| 1242 | retval = v; |
| 1243 | break; |
Tim Peters | d9b9ac8 | 2001-01-28 00:27:39 +0000 | [diff] [blame] | 1244 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1245 | case TYPE_DICT: |
| 1246 | v = PyDict_New(); |
Kristján Valur Jónsson | d7009c6 | 2013-03-19 18:02:10 -0700 | [diff] [blame] | 1247 | R_REF(v); |
Kristján Valur Jónsson | 5983258 | 2013-10-13 13:41:59 +0000 | [diff] [blame] | 1248 | if (v == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1249 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1250 | for (;;) { |
| 1251 | PyObject *key, *val; |
| 1252 | key = r_object(p); |
| 1253 | if (key == NULL) |
| 1254 | break; |
| 1255 | val = r_object(p); |
Victor Stinner | d5cae6f | 2013-10-31 17:14:52 +0100 | [diff] [blame] | 1256 | if (val == NULL) { |
| 1257 | Py_DECREF(key); |
| 1258 | break; |
| 1259 | } |
| 1260 | if (PyDict_SetItem(v, key, val) < 0) { |
| 1261 | Py_DECREF(key); |
| 1262 | Py_DECREF(val); |
| 1263 | break; |
| 1264 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1265 | Py_DECREF(key); |
Victor Stinner | d5cae6f | 2013-10-31 17:14:52 +0100 | [diff] [blame] | 1266 | Py_DECREF(val); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1267 | } |
| 1268 | if (PyErr_Occurred()) { |
| 1269 | Py_DECREF(v); |
| 1270 | v = NULL; |
| 1271 | } |
| 1272 | retval = v; |
| 1273 | break; |
Tim Peters | d9b9ac8 | 2001-01-28 00:27:39 +0000 | [diff] [blame] | 1274 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1275 | case TYPE_SET: |
| 1276 | case TYPE_FROZENSET: |
| 1277 | n = r_long(p); |
Kristján Valur Jónsson | 5983258 | 2013-10-13 13:41:59 +0000 | [diff] [blame] | 1278 | if (PyErr_Occurred()) |
Vinay Sajip | 5bdae3b | 2011-07-02 16:42:47 +0100 | [diff] [blame] | 1279 | break; |
Serhiy Storchaka | 7e01911 | 2013-02-13 12:08:15 +0200 | [diff] [blame] | 1280 | if (n < 0 || n > SIZE32_MAX) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1281 | PyErr_SetString(PyExc_ValueError, "bad marshal data (set size out of range)"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1282 | break; |
| 1283 | } |
Victor Stinner | d5cae6f | 2013-10-31 17:14:52 +0100 | [diff] [blame] | 1284 | |
Victor Stinner | 1aa7893 | 2016-01-23 14:15:48 +0100 | [diff] [blame] | 1285 | if (n == 0 && type == TYPE_FROZENSET) { |
| 1286 | /* call frozenset() to get the empty frozenset singleton */ |
Victor Stinner | 4778eab | 2016-12-01 14:51:04 +0100 | [diff] [blame] | 1287 | v = _PyObject_CallNoArg((PyObject*)&PyFrozenSet_Type); |
Victor Stinner | 1aa7893 | 2016-01-23 14:15:48 +0100 | [diff] [blame] | 1288 | if (v == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1289 | break; |
Victor Stinner | 1aa7893 | 2016-01-23 14:15:48 +0100 | [diff] [blame] | 1290 | R_REF(v); |
| 1291 | retval = v; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1292 | } |
Victor Stinner | 1aa7893 | 2016-01-23 14:15:48 +0100 | [diff] [blame] | 1293 | else { |
| 1294 | v = (type == TYPE_SET) ? PySet_New(NULL) : PyFrozenSet_New(NULL); |
| 1295 | if (type == TYPE_SET) { |
| 1296 | R_REF(v); |
| 1297 | } else { |
| 1298 | /* must use delayed registration of frozensets because they must |
| 1299 | * be init with a refcount of 1 |
| 1300 | */ |
| 1301 | idx = r_ref_reserve(flag, p); |
| 1302 | if (idx < 0) |
| 1303 | Py_CLEAR(v); /* signal error */ |
| 1304 | } |
| 1305 | if (v == NULL) |
| 1306 | break; |
| 1307 | |
| 1308 | for (i = 0; i < n; i++) { |
| 1309 | v2 = r_object(p); |
| 1310 | if ( v2 == NULL ) { |
| 1311 | if (!PyErr_Occurred()) |
| 1312 | PyErr_SetString(PyExc_TypeError, |
| 1313 | "NULL object in marshal data for set"); |
| 1314 | Py_DECREF(v); |
| 1315 | v = NULL; |
| 1316 | break; |
| 1317 | } |
| 1318 | if (PySet_Add(v, v2) == -1) { |
| 1319 | Py_DECREF(v); |
| 1320 | Py_DECREF(v2); |
| 1321 | v = NULL; |
| 1322 | break; |
| 1323 | } |
| 1324 | Py_DECREF(v2); |
| 1325 | } |
| 1326 | if (type != TYPE_SET) |
| 1327 | v = r_ref_insert(v, idx, flag, p); |
| 1328 | retval = v; |
| 1329 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1330 | break; |
Raymond Hettinger | a422c34 | 2005-01-11 03:03:27 +0000 | [diff] [blame] | 1331 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1332 | case TYPE_CODE: |
| 1333 | { |
| 1334 | int argcount; |
| 1335 | int kwonlyargcount; |
| 1336 | int nlocals; |
| 1337 | int stacksize; |
| 1338 | int flags; |
| 1339 | PyObject *code = NULL; |
| 1340 | PyObject *consts = NULL; |
| 1341 | PyObject *names = NULL; |
| 1342 | PyObject *varnames = NULL; |
| 1343 | PyObject *freevars = NULL; |
| 1344 | PyObject *cellvars = NULL; |
| 1345 | PyObject *filename = NULL; |
| 1346 | PyObject *name = NULL; |
| 1347 | int firstlineno; |
| 1348 | PyObject *lnotab = NULL; |
Antoine Pitrou | e9bbe8b | 2013-04-13 22:41:09 +0200 | [diff] [blame] | 1349 | |
Kristján Valur Jónsson | e178187 | 2013-03-20 11:43:57 -0700 | [diff] [blame] | 1350 | idx = r_ref_reserve(flag, p); |
Kristján Valur Jónsson | 5983258 | 2013-10-13 13:41:59 +0000 | [diff] [blame] | 1351 | if (idx < 0) |
Kristján Valur Jónsson | d7009c6 | 2013-03-19 18:02:10 -0700 | [diff] [blame] | 1352 | break; |
Tim Peters | d9b9ac8 | 2001-01-28 00:27:39 +0000 | [diff] [blame] | 1353 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1354 | v = NULL; |
Michael W. Hudson | df88846 | 2005-06-03 14:41:55 +0000 | [diff] [blame] | 1355 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1356 | /* XXX ignore long->int overflows for now */ |
| 1357 | argcount = (int)r_long(p); |
Vinay Sajip | 5bdae3b | 2011-07-02 16:42:47 +0100 | [diff] [blame] | 1358 | if (PyErr_Occurred()) |
| 1359 | goto code_error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1360 | kwonlyargcount = (int)r_long(p); |
Vinay Sajip | 5bdae3b | 2011-07-02 16:42:47 +0100 | [diff] [blame] | 1361 | if (PyErr_Occurred()) |
| 1362 | goto code_error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1363 | nlocals = (int)r_long(p); |
Vinay Sajip | 5bdae3b | 2011-07-02 16:42:47 +0100 | [diff] [blame] | 1364 | if (PyErr_Occurred()) |
| 1365 | goto code_error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1366 | stacksize = (int)r_long(p); |
Vinay Sajip | 5bdae3b | 2011-07-02 16:42:47 +0100 | [diff] [blame] | 1367 | if (PyErr_Occurred()) |
| 1368 | goto code_error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1369 | flags = (int)r_long(p); |
Vinay Sajip | 5bdae3b | 2011-07-02 16:42:47 +0100 | [diff] [blame] | 1370 | if (PyErr_Occurred()) |
| 1371 | goto code_error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1372 | code = r_object(p); |
| 1373 | if (code == NULL) |
| 1374 | goto code_error; |
| 1375 | consts = r_object(p); |
| 1376 | if (consts == NULL) |
| 1377 | goto code_error; |
| 1378 | names = r_object(p); |
| 1379 | if (names == NULL) |
| 1380 | goto code_error; |
| 1381 | varnames = r_object(p); |
| 1382 | if (varnames == NULL) |
| 1383 | goto code_error; |
| 1384 | freevars = r_object(p); |
| 1385 | if (freevars == NULL) |
| 1386 | goto code_error; |
| 1387 | cellvars = r_object(p); |
| 1388 | if (cellvars == NULL) |
| 1389 | goto code_error; |
| 1390 | filename = r_object(p); |
| 1391 | if (filename == NULL) |
| 1392 | goto code_error; |
Benjamin Peterson | 43b0686 | 2011-05-27 09:08:01 -0500 | [diff] [blame] | 1393 | if (PyUnicode_CheckExact(filename)) { |
| 1394 | if (p->current_filename != NULL) { |
| 1395 | if (!PyUnicode_Compare(filename, p->current_filename)) { |
| 1396 | Py_DECREF(filename); |
| 1397 | Py_INCREF(p->current_filename); |
| 1398 | filename = p->current_filename; |
| 1399 | } |
| 1400 | } |
| 1401 | else { |
| 1402 | p->current_filename = filename; |
| 1403 | } |
| 1404 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1405 | name = r_object(p); |
| 1406 | if (name == NULL) |
| 1407 | goto code_error; |
| 1408 | firstlineno = (int)r_long(p); |
Kristján Valur Jónsson | 0a7697b | 2013-10-13 15:19:56 +0000 | [diff] [blame] | 1409 | if (firstlineno == -1 && PyErr_Occurred()) |
| 1410 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1411 | lnotab = r_object(p); |
| 1412 | if (lnotab == NULL) |
| 1413 | goto code_error; |
Michael W. Hudson | df88846 | 2005-06-03 14:41:55 +0000 | [diff] [blame] | 1414 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1415 | v = (PyObject *) PyCode_New( |
| 1416 | argcount, kwonlyargcount, |
| 1417 | nlocals, stacksize, flags, |
| 1418 | code, consts, names, varnames, |
| 1419 | freevars, cellvars, filename, name, |
| 1420 | firstlineno, lnotab); |
Kristján Valur Jónsson | d7009c6 | 2013-03-19 18:02:10 -0700 | [diff] [blame] | 1421 | v = r_ref_insert(v, idx, flag, p); |
Tim Peters | d9b9ac8 | 2001-01-28 00:27:39 +0000 | [diff] [blame] | 1422 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1423 | code_error: |
| 1424 | Py_XDECREF(code); |
| 1425 | Py_XDECREF(consts); |
| 1426 | Py_XDECREF(names); |
| 1427 | Py_XDECREF(varnames); |
| 1428 | Py_XDECREF(freevars); |
| 1429 | Py_XDECREF(cellvars); |
| 1430 | Py_XDECREF(filename); |
| 1431 | Py_XDECREF(name); |
| 1432 | Py_XDECREF(lnotab); |
| 1433 | } |
| 1434 | retval = v; |
| 1435 | break; |
Tim Peters | d9b9ac8 | 2001-01-28 00:27:39 +0000 | [diff] [blame] | 1436 | |
Kristján Valur Jónsson | d7009c6 | 2013-03-19 18:02:10 -0700 | [diff] [blame] | 1437 | case TYPE_REF: |
| 1438 | n = r_long(p); |
| 1439 | if (n < 0 || n >= PyList_GET_SIZE(p->refs)) { |
Kristján Valur Jónsson | 0a7697b | 2013-10-13 15:19:56 +0000 | [diff] [blame] | 1440 | if (n == -1 && PyErr_Occurred()) |
| 1441 | break; |
Kristján Valur Jónsson | d7009c6 | 2013-03-19 18:02:10 -0700 | [diff] [blame] | 1442 | PyErr_SetString(PyExc_ValueError, "bad marshal data (invalid reference)"); |
Kristján Valur Jónsson | d7009c6 | 2013-03-19 18:02:10 -0700 | [diff] [blame] | 1443 | break; |
| 1444 | } |
| 1445 | v = PyList_GET_ITEM(p->refs, n); |
| 1446 | if (v == Py_None) { |
| 1447 | PyErr_SetString(PyExc_ValueError, "bad marshal data (invalid reference)"); |
Kristján Valur Jónsson | d7009c6 | 2013-03-19 18:02:10 -0700 | [diff] [blame] | 1448 | break; |
| 1449 | } |
| 1450 | Py_INCREF(v); |
| 1451 | retval = v; |
| 1452 | break; |
| 1453 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1454 | default: |
| 1455 | /* Bogus data got written, which isn't ideal. |
| 1456 | This will let you keep working and recover. */ |
| 1457 | PyErr_SetString(PyExc_ValueError, "bad marshal data (unknown type code)"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1458 | break; |
| 1459 | |
| 1460 | } |
| 1461 | p->depth--; |
| 1462 | return retval; |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 1463 | } |
| 1464 | |
Neal Norwitz | d85c452 | 2004-06-13 20:31:49 +0000 | [diff] [blame] | 1465 | static PyObject * |
Armin Rigo | 01ab279 | 2004-03-26 15:09:27 +0000 | [diff] [blame] | 1466 | read_object(RFILE *p) |
| 1467 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1468 | PyObject *v; |
| 1469 | if (PyErr_Occurred()) { |
| 1470 | fprintf(stderr, "XXX readobject called with exception set\n"); |
| 1471 | return NULL; |
| 1472 | } |
| 1473 | v = r_object(p); |
| 1474 | if (v == NULL && !PyErr_Occurred()) |
| 1475 | PyErr_SetString(PyExc_TypeError, "NULL object in marshal data for object"); |
| 1476 | return v; |
Armin Rigo | 01ab279 | 2004-03-26 15:09:27 +0000 | [diff] [blame] | 1477 | } |
| 1478 | |
Guido van Rossum | b8cf3e6 | 2001-10-19 01:46:21 +0000 | [diff] [blame] | 1479 | int |
| 1480 | PyMarshal_ReadShortFromFile(FILE *fp) |
| 1481 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1482 | RFILE rf; |
Antoine Pitrou | 1164dfc | 2013-10-12 22:25:39 +0200 | [diff] [blame] | 1483 | int res; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1484 | assert(fp); |
Vinay Sajip | 5bdae3b | 2011-07-02 16:42:47 +0100 | [diff] [blame] | 1485 | rf.readable = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1486 | rf.fp = fp; |
Benjamin Peterson | 43b0686 | 2011-05-27 09:08:01 -0500 | [diff] [blame] | 1487 | rf.current_filename = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1488 | rf.end = rf.ptr = NULL; |
Antoine Pitrou | 1164dfc | 2013-10-12 22:25:39 +0200 | [diff] [blame] | 1489 | rf.buf = NULL; |
| 1490 | res = r_short(&rf); |
| 1491 | if (rf.buf != NULL) |
| 1492 | PyMem_FREE(rf.buf); |
| 1493 | return res; |
Guido van Rossum | b8cf3e6 | 2001-10-19 01:46:21 +0000 | [diff] [blame] | 1494 | } |
| 1495 | |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 1496 | long |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1497 | PyMarshal_ReadLongFromFile(FILE *fp) |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 1498 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1499 | RFILE rf; |
Antoine Pitrou | 1164dfc | 2013-10-12 22:25:39 +0200 | [diff] [blame] | 1500 | long res; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1501 | rf.fp = fp; |
Vinay Sajip | 5bdae3b | 2011-07-02 16:42:47 +0100 | [diff] [blame] | 1502 | rf.readable = NULL; |
Benjamin Peterson | 43b0686 | 2011-05-27 09:08:01 -0500 | [diff] [blame] | 1503 | rf.current_filename = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1504 | rf.ptr = rf.end = NULL; |
Antoine Pitrou | 1164dfc | 2013-10-12 22:25:39 +0200 | [diff] [blame] | 1505 | rf.buf = NULL; |
| 1506 | res = r_long(&rf); |
| 1507 | if (rf.buf != NULL) |
| 1508 | PyMem_FREE(rf.buf); |
| 1509 | return res; |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 1510 | } |
| 1511 | |
Steve Dower | f2f373f | 2015-02-21 08:44:05 -0800 | [diff] [blame] | 1512 | /* Return size of file in bytes; < 0 if unknown or INT_MAX if too big */ |
Tim Peters | 691e0e9 | 2001-01-18 04:39:16 +0000 | [diff] [blame] | 1513 | static off_t |
| 1514 | getfilesize(FILE *fp) |
| 1515 | { |
Steve Dower | f2f373f | 2015-02-21 08:44:05 -0800 | [diff] [blame] | 1516 | struct _Py_stat_struct st; |
Victor Stinner | e134a7f | 2015-03-30 10:09:31 +0200 | [diff] [blame] | 1517 | if (_Py_fstat_noraise(fileno(fp), &st) != 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1518 | return -1; |
Steve Dower | f2f373f | 2015-02-21 08:44:05 -0800 | [diff] [blame] | 1519 | #if SIZEOF_OFF_T == 4 |
| 1520 | else if (st.st_size >= INT_MAX) |
| 1521 | return (off_t)INT_MAX; |
| 1522 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1523 | else |
Steve Dower | f2f373f | 2015-02-21 08:44:05 -0800 | [diff] [blame] | 1524 | return (off_t)st.st_size; |
Tim Peters | 691e0e9 | 2001-01-18 04:39:16 +0000 | [diff] [blame] | 1525 | } |
Tim Peters | d9b9ac8 | 2001-01-28 00:27:39 +0000 | [diff] [blame] | 1526 | |
Tim Peters | 691e0e9 | 2001-01-18 04:39:16 +0000 | [diff] [blame] | 1527 | /* If we can get the size of the file up-front, and it's reasonably small, |
| 1528 | * read it in one gulp and delegate to ...FromString() instead. Much quicker |
| 1529 | * than reading a byte at a time from file; speeds .pyc imports. |
Tim Peters | d9b9ac8 | 2001-01-28 00:27:39 +0000 | [diff] [blame] | 1530 | * CAUTION: since this may read the entire remainder of the file, don't |
| 1531 | * call it unless you know you're done with the file. |
Tim Peters | 691e0e9 | 2001-01-18 04:39:16 +0000 | [diff] [blame] | 1532 | */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1533 | PyObject * |
Tim Peters | d9b9ac8 | 2001-01-28 00:27:39 +0000 | [diff] [blame] | 1534 | PyMarshal_ReadLastObjectFromFile(FILE *fp) |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 1535 | { |
Antoine Pitrou | 5bc7ec9 | 2010-04-21 22:56:22 +0000 | [diff] [blame] | 1536 | /* REASONABLE_FILE_LIMIT is by defn something big enough for Tkinter.pyc. */ |
Tim Peters | 691e0e9 | 2001-01-18 04:39:16 +0000 | [diff] [blame] | 1537 | #define REASONABLE_FILE_LIMIT (1L << 18) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1538 | off_t filesize; |
| 1539 | filesize = getfilesize(fp); |
| 1540 | if (filesize > 0 && filesize <= REASONABLE_FILE_LIMIT) { |
| 1541 | char* pBuf = (char *)PyMem_MALLOC(filesize); |
| 1542 | if (pBuf != NULL) { |
Serhiy Storchaka | 7e01911 | 2013-02-13 12:08:15 +0200 | [diff] [blame] | 1543 | size_t n = fread(pBuf, 1, (size_t)filesize, fp); |
| 1544 | PyObject* v = PyMarshal_ReadObjectFromString(pBuf, n); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1545 | PyMem_FREE(pBuf); |
| 1546 | return v; |
| 1547 | } |
Tim Peters | d9b9ac8 | 2001-01-28 00:27:39 +0000 | [diff] [blame] | 1548 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1549 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1550 | /* We don't have fstat, or we do but the file is larger than |
| 1551 | * REASONABLE_FILE_LIMIT or malloc failed -- read a byte at a time. |
| 1552 | */ |
| 1553 | return PyMarshal_ReadObjectFromFile(fp); |
Tim Peters | d9b9ac8 | 2001-01-28 00:27:39 +0000 | [diff] [blame] | 1554 | |
Tim Peters | 691e0e9 | 2001-01-18 04:39:16 +0000 | [diff] [blame] | 1555 | #undef REASONABLE_FILE_LIMIT |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 1556 | } |
| 1557 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1558 | PyObject * |
Tim Peters | d9b9ac8 | 2001-01-28 00:27:39 +0000 | [diff] [blame] | 1559 | PyMarshal_ReadObjectFromFile(FILE *fp) |
| 1560 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1561 | RFILE rf; |
| 1562 | PyObject *result; |
| 1563 | rf.fp = fp; |
Vinay Sajip | 5bdae3b | 2011-07-02 16:42:47 +0100 | [diff] [blame] | 1564 | rf.readable = NULL; |
Benjamin Peterson | 43b0686 | 2011-05-27 09:08:01 -0500 | [diff] [blame] | 1565 | rf.current_filename = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1566 | rf.depth = 0; |
| 1567 | rf.ptr = rf.end = NULL; |
Antoine Pitrou | 1164dfc | 2013-10-12 22:25:39 +0200 | [diff] [blame] | 1568 | rf.buf = NULL; |
Kristján Valur Jónsson | d7009c6 | 2013-03-19 18:02:10 -0700 | [diff] [blame] | 1569 | rf.refs = PyList_New(0); |
| 1570 | if (rf.refs == NULL) |
| 1571 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1572 | result = r_object(&rf); |
Kristján Valur Jónsson | d7009c6 | 2013-03-19 18:02:10 -0700 | [diff] [blame] | 1573 | Py_DECREF(rf.refs); |
Antoine Pitrou | 1164dfc | 2013-10-12 22:25:39 +0200 | [diff] [blame] | 1574 | if (rf.buf != NULL) |
| 1575 | PyMem_FREE(rf.buf); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1576 | return result; |
Tim Peters | d9b9ac8 | 2001-01-28 00:27:39 +0000 | [diff] [blame] | 1577 | } |
| 1578 | |
| 1579 | PyObject * |
Serhiy Storchaka | c679227 | 2013-10-19 21:03:34 +0300 | [diff] [blame] | 1580 | PyMarshal_ReadObjectFromString(const char *str, Py_ssize_t len) |
Guido van Rossum | f56e3db | 1993-04-01 20:59:32 +0000 | [diff] [blame] | 1581 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1582 | RFILE rf; |
| 1583 | PyObject *result; |
| 1584 | rf.fp = NULL; |
Vinay Sajip | 5bdae3b | 2011-07-02 16:42:47 +0100 | [diff] [blame] | 1585 | rf.readable = NULL; |
Benjamin Peterson | 43b0686 | 2011-05-27 09:08:01 -0500 | [diff] [blame] | 1586 | rf.current_filename = NULL; |
Serhiy Storchaka | c679227 | 2013-10-19 21:03:34 +0300 | [diff] [blame] | 1587 | rf.ptr = (char *)str; |
| 1588 | rf.end = (char *)str + len; |
Antoine Pitrou | 1164dfc | 2013-10-12 22:25:39 +0200 | [diff] [blame] | 1589 | rf.buf = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1590 | rf.depth = 0; |
Kristján Valur Jónsson | d7009c6 | 2013-03-19 18:02:10 -0700 | [diff] [blame] | 1591 | rf.refs = PyList_New(0); |
| 1592 | if (rf.refs == NULL) |
| 1593 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1594 | result = r_object(&rf); |
Kristján Valur Jónsson | d7009c6 | 2013-03-19 18:02:10 -0700 | [diff] [blame] | 1595 | Py_DECREF(rf.refs); |
Antoine Pitrou | 1164dfc | 2013-10-12 22:25:39 +0200 | [diff] [blame] | 1596 | if (rf.buf != NULL) |
| 1597 | PyMem_FREE(rf.buf); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1598 | return result; |
Guido van Rossum | f56e3db | 1993-04-01 20:59:32 +0000 | [diff] [blame] | 1599 | } |
| 1600 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1601 | PyObject * |
Martin v. Löwis | ef82d2f | 2004-06-27 16:51:46 +0000 | [diff] [blame] | 1602 | PyMarshal_WriteObjectToString(PyObject *x, int version) |
Guido van Rossum | 3f3bb3d | 1996-08-19 22:07:17 +0000 | [diff] [blame] | 1603 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1604 | WFILE wf; |
Guido van Rossum | e6d3904 | 2007-05-09 00:01:30 +0000 | [diff] [blame] | 1605 | |
Serhiy Storchaka | ce921c62 | 2015-02-11 15:53:31 +0200 | [diff] [blame] | 1606 | memset(&wf, 0, sizeof(wf)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1607 | wf.str = PyBytes_FromStringAndSize((char *)NULL, 50); |
| 1608 | if (wf.str == NULL) |
| 1609 | return NULL; |
Serhiy Storchaka | c1efe5f | 2015-02-11 15:54:54 +0200 | [diff] [blame] | 1610 | wf.ptr = wf.buf = PyBytes_AS_STRING((PyBytesObject *)wf.str); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1611 | wf.end = wf.ptr + PyBytes_Size(wf.str); |
| 1612 | wf.error = WFERR_OK; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1613 | wf.version = version; |
Serhiy Storchaka | ce921c62 | 2015-02-11 15:53:31 +0200 | [diff] [blame] | 1614 | if (w_init_refs(&wf, version)) { |
| 1615 | Py_DECREF(wf.str); |
| 1616 | return NULL; |
| 1617 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1618 | w_object(x, &wf); |
Serhiy Storchaka | ce921c62 | 2015-02-11 15:53:31 +0200 | [diff] [blame] | 1619 | w_clear_refs(&wf); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1620 | if (wf.str != NULL) { |
| 1621 | char *base = PyBytes_AS_STRING((PyBytesObject *)wf.str); |
| 1622 | if (wf.ptr - base > PY_SSIZE_T_MAX) { |
| 1623 | Py_DECREF(wf.str); |
| 1624 | PyErr_SetString(PyExc_OverflowError, |
Serhiy Storchaka | c611a5b | 2017-03-12 08:53:22 +0200 | [diff] [blame] | 1625 | "too much marshal data for a bytes object"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1626 | return NULL; |
| 1627 | } |
| 1628 | if (_PyBytes_Resize(&wf.str, (Py_ssize_t)(wf.ptr - base)) < 0) |
| 1629 | return NULL; |
| 1630 | } |
| 1631 | if (wf.error != WFERR_OK) { |
| 1632 | Py_XDECREF(wf.str); |
| 1633 | if (wf.error == WFERR_NOMEMORY) |
| 1634 | PyErr_NoMemory(); |
| 1635 | else |
| 1636 | PyErr_SetString(PyExc_ValueError, |
| 1637 | (wf.error==WFERR_UNMARSHALLABLE)?"unmarshallable object" |
| 1638 | :"object too deeply nested to marshal"); |
| 1639 | return NULL; |
| 1640 | } |
Antoine Pitrou | 1c13f84 | 2012-03-02 18:22:23 +0100 | [diff] [blame] | 1641 | return wf.str; |
Guido van Rossum | 3f3bb3d | 1996-08-19 22:07:17 +0000 | [diff] [blame] | 1642 | } |
| 1643 | |
Guido van Rossum | 64b4552 | 1991-06-07 13:58:22 +0000 | [diff] [blame] | 1644 | /* And an interface for Python programs... */ |
Serhiy Storchaka | 0767ad4 | 2017-03-12 09:20:15 +0200 | [diff] [blame] | 1645 | /*[clinic input] |
| 1646 | marshal.dump |
| 1647 | |
| 1648 | value: object |
| 1649 | Must be a supported type. |
| 1650 | file: object |
| 1651 | Must be a writeable binary file. |
| 1652 | version: int(c_default="Py_MARSHAL_VERSION") = version |
| 1653 | Indicates the data format that dump should use. |
| 1654 | / |
| 1655 | |
| 1656 | Write the value on the open file. |
| 1657 | |
| 1658 | If the value has (or contains an object that has) an unsupported type, a |
| 1659 | ValueError exception is raised - but garbage data will also be written |
| 1660 | to the file. The object will not be properly read back by load(). |
| 1661 | [clinic start generated code]*/ |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 1662 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1663 | static PyObject * |
Serhiy Storchaka | 0767ad4 | 2017-03-12 09:20:15 +0200 | [diff] [blame] | 1664 | marshal_dump_impl(PyObject *module, PyObject *value, PyObject *file, |
| 1665 | int version) |
| 1666 | /*[clinic end generated code: output=aaee62c7028a7cb2 input=6c7a3c23c6fef556]*/ |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 1667 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1668 | /* XXX Quick hack -- need to do this differently */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1669 | PyObject *s; |
| 1670 | PyObject *res; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 1671 | _Py_IDENTIFIER(write); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1672 | |
Serhiy Storchaka | 0767ad4 | 2017-03-12 09:20:15 +0200 | [diff] [blame] | 1673 | s = PyMarshal_WriteObjectToString(value, version); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1674 | if (s == NULL) |
| 1675 | return NULL; |
Serhiy Storchaka | 0767ad4 | 2017-03-12 09:20:15 +0200 | [diff] [blame] | 1676 | res = _PyObject_CallMethodIdObjArgs(file, &PyId_write, s, NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1677 | Py_DECREF(s); |
| 1678 | return res; |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 1679 | } |
| 1680 | |
Serhiy Storchaka | 0767ad4 | 2017-03-12 09:20:15 +0200 | [diff] [blame] | 1681 | /*[clinic input] |
| 1682 | marshal.load |
| 1683 | |
| 1684 | file: object |
| 1685 | Must be readable binary file. |
| 1686 | / |
| 1687 | |
| 1688 | Read one value from the open file and return it. |
| 1689 | |
| 1690 | If no valid value is read (e.g. because the data has a different Python |
| 1691 | version's incompatible marshal format), raise EOFError, ValueError or |
| 1692 | TypeError. |
| 1693 | |
| 1694 | Note: If an object containing an unsupported type was marshalled with |
| 1695 | dump(), load() will substitute None for the unmarshallable type. |
| 1696 | [clinic start generated code]*/ |
R. David Murray | dd226ea | 2009-05-13 12:27:21 +0000 | [diff] [blame] | 1697 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1698 | static PyObject * |
Serhiy Storchaka | 0767ad4 | 2017-03-12 09:20:15 +0200 | [diff] [blame] | 1699 | marshal_load(PyObject *module, PyObject *file) |
| 1700 | /*[clinic end generated code: output=f8e5c33233566344 input=c85c2b594cd8124a]*/ |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 1701 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1702 | PyObject *data, *result; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 1703 | _Py_IDENTIFIER(read); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1704 | RFILE rf; |
Vinay Sajip | 5bdae3b | 2011-07-02 16:42:47 +0100 | [diff] [blame] | 1705 | |
| 1706 | /* |
| 1707 | * Make a call to the read method, but read zero bytes. |
| 1708 | * This is to ensure that the object passed in at least |
| 1709 | * has a read method which returns bytes. |
Kristján Valur Jónsson | d7009c6 | 2013-03-19 18:02:10 -0700 | [diff] [blame] | 1710 | * This can be removed if we guarantee good error handling |
| 1711 | * for r_string() |
Vinay Sajip | 5bdae3b | 2011-07-02 16:42:47 +0100 | [diff] [blame] | 1712 | */ |
Serhiy Storchaka | 0767ad4 | 2017-03-12 09:20:15 +0200 | [diff] [blame] | 1713 | data = _PyObject_CallMethodId(file, &PyId_read, "i", 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1714 | if (data == NULL) |
| 1715 | return NULL; |
Vinay Sajip | 5bdae3b | 2011-07-02 16:42:47 +0100 | [diff] [blame] | 1716 | if (!PyBytes_Check(data)) { |
| 1717 | PyErr_Format(PyExc_TypeError, |
Serhiy Storchaka | 0767ad4 | 2017-03-12 09:20:15 +0200 | [diff] [blame] | 1718 | "file.read() returned not bytes but %.100s", |
Vinay Sajip | 5bdae3b | 2011-07-02 16:42:47 +0100 | [diff] [blame] | 1719 | data->ob_type->tp_name); |
| 1720 | result = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1721 | } |
| 1722 | else { |
Vinay Sajip | 5bdae3b | 2011-07-02 16:42:47 +0100 | [diff] [blame] | 1723 | rf.depth = 0; |
| 1724 | rf.fp = NULL; |
Serhiy Storchaka | 0767ad4 | 2017-03-12 09:20:15 +0200 | [diff] [blame] | 1725 | rf.readable = file; |
Vinay Sajip | aac0f75 | 2011-07-02 18:42:21 +0100 | [diff] [blame] | 1726 | rf.current_filename = NULL; |
Antoine Pitrou | 1164dfc | 2013-10-12 22:25:39 +0200 | [diff] [blame] | 1727 | rf.ptr = rf.end = NULL; |
| 1728 | rf.buf = NULL; |
Kristján Valur Jónsson | d7009c6 | 2013-03-19 18:02:10 -0700 | [diff] [blame] | 1729 | if ((rf.refs = PyList_New(0)) != NULL) { |
| 1730 | result = read_object(&rf); |
| 1731 | Py_DECREF(rf.refs); |
Antoine Pitrou | 1164dfc | 2013-10-12 22:25:39 +0200 | [diff] [blame] | 1732 | if (rf.buf != NULL) |
| 1733 | PyMem_FREE(rf.buf); |
Kristján Valur Jónsson | d7009c6 | 2013-03-19 18:02:10 -0700 | [diff] [blame] | 1734 | } else |
| 1735 | result = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1736 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1737 | Py_DECREF(data); |
| 1738 | return result; |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 1739 | } |
| 1740 | |
Serhiy Storchaka | 0767ad4 | 2017-03-12 09:20:15 +0200 | [diff] [blame] | 1741 | /*[clinic input] |
| 1742 | marshal.dumps |
R. David Murray | dd226ea | 2009-05-13 12:27:21 +0000 | [diff] [blame] | 1743 | |
Serhiy Storchaka | 0767ad4 | 2017-03-12 09:20:15 +0200 | [diff] [blame] | 1744 | value: object |
| 1745 | Must be a supported type. |
| 1746 | version: int(c_default="Py_MARSHAL_VERSION") = version |
| 1747 | Indicates the data format that dumps should use. |
| 1748 | / |
| 1749 | |
| 1750 | Return the bytes object that would be written to a file by dump(value, file). |
| 1751 | |
| 1752 | Raise a ValueError exception if value has (or contains an object that has) an |
| 1753 | unsupported type. |
| 1754 | [clinic start generated code]*/ |
R. David Murray | dd226ea | 2009-05-13 12:27:21 +0000 | [diff] [blame] | 1755 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1756 | static PyObject * |
Serhiy Storchaka | 0767ad4 | 2017-03-12 09:20:15 +0200 | [diff] [blame] | 1757 | marshal_dumps_impl(PyObject *module, PyObject *value, int version) |
| 1758 | /*[clinic end generated code: output=9c200f98d7256cad input=a2139ea8608e9b27]*/ |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 1759 | { |
Serhiy Storchaka | 0767ad4 | 2017-03-12 09:20:15 +0200 | [diff] [blame] | 1760 | return PyMarshal_WriteObjectToString(value, version); |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 1761 | } |
| 1762 | |
Serhiy Storchaka | 0767ad4 | 2017-03-12 09:20:15 +0200 | [diff] [blame] | 1763 | /*[clinic input] |
| 1764 | marshal.loads |
R. David Murray | dd226ea | 2009-05-13 12:27:21 +0000 | [diff] [blame] | 1765 | |
Serhiy Storchaka | 0767ad4 | 2017-03-12 09:20:15 +0200 | [diff] [blame] | 1766 | bytes: Py_buffer |
| 1767 | / |
| 1768 | |
| 1769 | Convert the bytes-like object to a value. |
| 1770 | |
| 1771 | If no valid value is found, raise EOFError, ValueError or TypeError. Extra |
| 1772 | bytes in the input are ignored. |
| 1773 | [clinic start generated code]*/ |
R. David Murray | dd226ea | 2009-05-13 12:27:21 +0000 | [diff] [blame] | 1774 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1775 | static PyObject * |
Serhiy Storchaka | 0767ad4 | 2017-03-12 09:20:15 +0200 | [diff] [blame] | 1776 | marshal_loads_impl(PyObject *module, Py_buffer *bytes) |
| 1777 | /*[clinic end generated code: output=9fc65985c93d1bb1 input=6f426518459c8495]*/ |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 1778 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1779 | RFILE rf; |
Serhiy Storchaka | 0767ad4 | 2017-03-12 09:20:15 +0200 | [diff] [blame] | 1780 | char *s = bytes->buf; |
| 1781 | Py_ssize_t n = bytes->len; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1782 | PyObject* result; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1783 | rf.fp = NULL; |
Vinay Sajip | 5bdae3b | 2011-07-02 16:42:47 +0100 | [diff] [blame] | 1784 | rf.readable = NULL; |
Benjamin Peterson | 43b0686 | 2011-05-27 09:08:01 -0500 | [diff] [blame] | 1785 | rf.current_filename = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1786 | rf.ptr = s; |
| 1787 | rf.end = s + n; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1788 | rf.depth = 0; |
Kristján Valur Jónsson | d7009c6 | 2013-03-19 18:02:10 -0700 | [diff] [blame] | 1789 | if ((rf.refs = PyList_New(0)) == NULL) |
| 1790 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1791 | result = read_object(&rf); |
Kristján Valur Jónsson | d7009c6 | 2013-03-19 18:02:10 -0700 | [diff] [blame] | 1792 | Py_DECREF(rf.refs); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1793 | return result; |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 1794 | } |
| 1795 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1796 | static PyMethodDef marshal_methods[] = { |
Serhiy Storchaka | 0767ad4 | 2017-03-12 09:20:15 +0200 | [diff] [blame] | 1797 | MARSHAL_DUMP_METHODDEF |
| 1798 | MARSHAL_LOAD_METHODDEF |
| 1799 | MARSHAL_DUMPS_METHODDEF |
| 1800 | MARSHAL_LOADS_METHODDEF |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1801 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 1802 | }; |
| 1803 | |
R. David Murray | dd226ea | 2009-05-13 12:27:21 +0000 | [diff] [blame] | 1804 | |
| 1805 | PyDoc_STRVAR(module_doc, |
| 1806 | "This module contains functions that can read and write Python values in\n\ |
| 1807 | a binary format. The format is specific to Python, but independent of\n\ |
| 1808 | machine architecture issues.\n\ |
| 1809 | \n\ |
| 1810 | Not all Python object types are supported; in general, only objects\n\ |
| 1811 | whose value is independent from a particular invocation of Python can be\n\ |
| 1812 | written and read by this module. The following types are supported:\n\ |
| 1813 | None, integers, floating point numbers, strings, bytes, bytearrays,\n\ |
| 1814 | tuples, lists, sets, dictionaries, and code objects, where it\n\ |
| 1815 | should be understood that tuples, lists and dictionaries are only\n\ |
| 1816 | supported as long as the values contained therein are themselves\n\ |
| 1817 | supported; and recursive lists and dictionaries should not be written\n\ |
| 1818 | (they will cause infinite loops).\n\ |
| 1819 | \n\ |
| 1820 | Variables:\n\ |
| 1821 | \n\ |
| 1822 | version -- indicates the format that the module uses. Version 0 is the\n\ |
| 1823 | historical format, version 1 shares interned strings and version 2\n\ |
| 1824 | uses a binary format for floating point numbers.\n\ |
Kristján Valur Jónsson | d7009c6 | 2013-03-19 18:02:10 -0700 | [diff] [blame] | 1825 | Version 3 shares common object references (New in version 3.4).\n\ |
R. David Murray | dd226ea | 2009-05-13 12:27:21 +0000 | [diff] [blame] | 1826 | \n\ |
| 1827 | Functions:\n\ |
| 1828 | \n\ |
| 1829 | dump() -- write value to a file\n\ |
| 1830 | load() -- read value from a file\n\ |
Serhiy Storchaka | c611a5b | 2017-03-12 08:53:22 +0200 | [diff] [blame] | 1831 | dumps() -- marshal value as a bytes object\n\ |
| 1832 | loads() -- read value from a bytes-like object"); |
R. David Murray | dd226ea | 2009-05-13 12:27:21 +0000 | [diff] [blame] | 1833 | |
| 1834 | |
| 1835 | |
Brett Cannon | 429ef65 | 2008-06-27 00:35:35 +0000 | [diff] [blame] | 1836 | static struct PyModuleDef marshalmodule = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1837 | PyModuleDef_HEAD_INIT, |
| 1838 | "marshal", |
| 1839 | module_doc, |
| 1840 | 0, |
| 1841 | marshal_methods, |
| 1842 | NULL, |
| 1843 | NULL, |
| 1844 | NULL, |
| 1845 | NULL |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1846 | }; |
| 1847 | |
Jason Tishler | 6bc06ec | 2003-09-04 11:59:50 +0000 | [diff] [blame] | 1848 | PyMODINIT_FUNC |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1849 | PyMarshal_Init(void) |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 1850 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1851 | PyObject *mod = PyModule_Create(&marshalmodule); |
| 1852 | if (mod == NULL) |
| 1853 | return NULL; |
| 1854 | PyModule_AddIntConstant(mod, "version", Py_MARSHAL_VERSION); |
| 1855 | return mod; |
Guido van Rossum | dce2e3d | 1991-06-04 19:42:30 +0000 | [diff] [blame] | 1856 | } |