blob: b6337e1450fca76bc2dbb50c5738787b65327c0e [file] [log] [blame]
Guido van Rossumdce2e3d1991-06-04 19:42:30 +00001
2/* Write Python objects to files and read them back.
Kristján Valur Jónssond7009c62013-03-19 18:02:10 -07003 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 Rossumdce2e3d1991-06-04 19:42:30 +00008
Thomas Wouters695934a2006-03-01 23:49:13 +00009#define PY_SSIZE_T_CLEAN
10
Guido van Rossum79f25d91997-04-29 20:08:16 +000011#include "Python.h"
Guido van Rossumdce2e3d1991-06-04 19:42:30 +000012#include "longintrepr.h"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000013#include "code.h"
Guido van Rossumdce2e3d1991-06-04 19:42:30 +000014#include "marshal.h"
Serhiy Storchakace921c622015-02-11 15:53:31 +020015#include "../Modules/hashtable.h"
Guido van Rossumdce2e3d1991-06-04 19:42:30 +000016
Serhiy Storchaka0767ad42017-03-12 09:20:15 +020017/*[clinic input]
18module marshal
19[clinic start generated code]*/
20/*[clinic end generated code: output=da39a3ee5e6b4b0d input=c982b7930dee17db]*/
21
22#include "clinic/marshal.c.h"
23
Fred Drake6da0b912000-06-28 18:47:56 +000024/* 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 Rossum63175a12007-08-29 20:39:13 +000027 * On Windows debug builds, reduce this value.
Fred Drake6da0b912000-06-28 18:47:56 +000028 */
Guido van Rossum63175a12007-08-29 20:39:13 +000029#if defined(MS_WINDOWS) && defined(_DEBUG)
Steve Dowerf6c69e62014-11-01 15:15:16 -070030#define MAX_MARSHAL_STACK_DEPTH 1000
Guido van Rossum63175a12007-08-29 20:39:13 +000031#else
Guido van Rossumd59da4b2007-05-22 18:11:13 +000032#define MAX_MARSHAL_STACK_DEPTH 2000
Guido van Rossum63175a12007-08-29 20:39:13 +000033#endif
Fred Drake6da0b912000-06-28 18:47:56 +000034
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000035#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 Pitrouf95a1b32010-05-09 15:52:27 +000042#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ónssond7009c62013-03-19 18:02:10 -070048#define TYPE_INTERNED 't'
49#define TYPE_REF 'r'
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000050#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ónssond7009c62013-03-19 18:02:10 -070058#define FLAG_REF '\x80' /* with a type, add obj to index */
Guido van Rossumdce2e3d1991-06-04 19:42:30 +000059
Antoine Pitrou1164dfc2013-10-12 22:25:39 +020060#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 Smithb1a03cf2009-04-21 11:57:38 +000066#define WFERR_OK 0
67#define WFERR_UNMARSHALLABLE 1
68#define WFERR_NESTEDTOODEEP 2
69#define WFERR_NOMEMORY 3
70
Guido van Rossum0b0db8e1993-01-21 16:07:51 +000071typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000072 FILE *fp;
73 int error; /* see WFERR_* values */
74 int depth;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000075 PyObject *str;
76 char *ptr;
77 char *end;
Antoine Pitrou1164dfc2013-10-12 22:25:39 +020078 char *buf;
Serhiy Storchakace921c622015-02-11 15:53:31 +020079 _Py_hashtable_t *hashtable;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000080 int version;
Guido van Rossum0b0db8e1993-01-21 16:07:51 +000081} WFILE;
Guido van Rossumdce2e3d1991-06-04 19:42:30 +000082
Serhiy Storchakac1efe5f2015-02-11 15:54:54 +020083#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 Rossum0b0db8e1993-01-21 16:07:51 +000087
88static void
Serhiy Storchakac1efe5f2015-02-11 15:54:54 +020089w_flush(WFILE *p)
Guido van Rossum0b0db8e1993-01-21 16:07:51 +000090{
Serhiy Storchakac1efe5f2015-02-11 15:54:54 +020091 assert(p->fp != NULL);
92 fwrite(p->buf, 1, p->ptr - p->buf, p->fp);
93 p->ptr = p->buf;
94}
95
96static int
97w_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 Pitrouf95a1b32010-05-09 15:52:27 +0000105 }
Serhiy Storchakac1efe5f2015-02-11 15:54:54 +0200106 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 Pitrouf95a1b32010-05-09 15:52:27 +0000122 }
123 else {
Serhiy Storchakac1efe5f2015-02-11 15:54:54 +0200124 p->buf = PyBytes_AS_STRING(p->str);
125 p->ptr = p->buf + pos;
126 p->end = p->buf + size;
127 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000128 }
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000129}
130
131static void
Serhiy Storchakadfde2152013-07-11 19:14:26 +0300132w_string(const char *s, Py_ssize_t n, WFILE *p)
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000133{
Serhiy Storchakac1efe5f2015-02-11 15:54:54 +0200134 Py_ssize_t m;
135 if (!n || p->ptr == NULL)
136 return;
137 m = p->end - p->ptr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000138 if (p->fp != NULL) {
Serhiy Storchakac1efe5f2015-02-11 15:54:54 +0200139 if (n <= m) {
Christian Heimesf051e432016-09-13 20:22:02 +0200140 memcpy(p->ptr, s, n);
Serhiy Storchakac1efe5f2015-02-11 15:54:54 +0200141 p->ptr += n;
142 }
143 else {
144 w_flush(p);
145 fwrite(s, 1, n, p->fp);
146 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000147 }
148 else {
Serhiy Storchakac1efe5f2015-02-11 15:54:54 +0200149 if (n <= m || w_reserve(p, n - m)) {
Christian Heimesf051e432016-09-13 20:22:02 +0200150 memcpy(p->ptr, s, n);
Serhiy Storchakac1efe5f2015-02-11 15:54:54 +0200151 p->ptr += n;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000152 }
153 }
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000154}
155
156static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000157w_short(int x, WFILE *p)
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000158{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000159 w_byte((char)( x & 0xff), p);
160 w_byte((char)((x>> 8) & 0xff), p);
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000161}
162
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000163static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000164w_long(long x, WFILE *p)
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000165{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000166 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 Rossumdce2e3d1991-06-04 19:42:30 +0000170}
171
Serhiy Storchaka7e019112013-02-13 12:08:15 +0200172#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 Storchakadfde2152013-07-11 19:14:26 +0300187static void
188w_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 Pitrou1164dfc2013-10-12 22:25:39 +0200194static void
195w_short_pstring(const char *s, Py_ssize_t n, WFILE *p)
196{
Antoine Pitroub30f2712013-10-12 23:14:47 +0200197 w_byte(Py_SAFE_DOWNCAST(n, Py_ssize_t, unsigned char), p);
Antoine Pitrou1164dfc2013-10-12 22:25:39 +0200198 w_string(s, n, p);
199}
200
Serhiy Storchaka95949422013-08-27 19:40:23 +0300201/* We assume that Python ints are stored internally in base some power of
Mark Dickinsonbd792642009-03-18 20:06:12 +0000202 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ónssond7009c62013-03-19 18:02:10 -0700213#define W_TYPE(t, p) do { \
214 w_byte((t) | flag, (p)); \
215} while(0)
216
Mark Dickinsonbd792642009-03-18 20:06:12 +0000217static void
Kristján Valur Jónssond7009c62013-03-19 18:02:10 -0700218w_PyLong(const PyLongObject *ob, char flag, WFILE *p)
Mark Dickinsonbd792642009-03-18 20:06:12 +0000219{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000220 Py_ssize_t i, j, n, l;
221 digit d;
Mark Dickinsonbd792642009-03-18 20:06:12 +0000222
Kristján Valur Jónssond7009c62013-03-19 18:02:10 -0700223 W_TYPE(TYPE_LONG, p);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000224 if (Py_SIZE(ob) == 0) {
225 w_long((long)0, p);
226 return;
227 }
Mark Dickinsonbd792642009-03-18 20:06:12 +0000228
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000229 /* set l to number of base PyLong_MARSHAL_BASE digits */
Victor Stinner45e8e2f2014-05-14 17:24:35 +0200230 n = Py_ABS(Py_SIZE(ob));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000231 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 Storchaka7e019112013-02-13 12:08:15 +0200238 if (l > SIZE32_MAX) {
239 p->depth--;
240 p->error = WFERR_UNMARSHALLABLE;
241 return;
242 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000243 w_long((long)(Py_SIZE(ob) > 0 ? l : -l), p);
Mark Dickinsonbd792642009-03-18 20:06:12 +0000244
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000245 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 Dickinsonbd792642009-03-18 20:06:12 +0000258}
259
Kristján Valur Jónssond7009c62013-03-19 18:02:10 -0700260static int
261w_ref(PyObject *v, char *flag, WFILE *p)
262{
Serhiy Storchakace921c622015-02-11 15:53:31 +0200263 _Py_hashtable_entry_t *entry;
264 int w;
Kristján Valur Jónssond7009c62013-03-19 18:02:10 -0700265
Serhiy Storchakace921c622015-02-11 15:53:31 +0200266 if (p->version < 3 || p->hashtable == NULL)
Kristján Valur Jónssond7009c62013-03-19 18:02:10 -0700267 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 Stinner285cf0a2016-03-21 22:00:58 +0100273 entry = _Py_HASHTABLE_GET_ENTRY(p->hashtable, v);
Serhiy Storchakace921c622015-02-11 15:53:31 +0200274 if (entry != NULL) {
Kristján Valur Jónssond7009c62013-03-19 18:02:10 -0700275 /* write the reference index to the stream */
Victor Stinnere8c6b2f2016-03-23 09:25:01 +0100276 _Py_HASHTABLE_ENTRY_READ_DATA(p->hashtable, entry, w);
Kristján Valur Jónssond7009c62013-03-19 18:02:10 -0700277 /* 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 Storchakace921c622015-02-11 15:53:31 +0200283 size_t s = p->hashtable->entries;
Kristján Valur Jónssond7009c62013-03-19 18:02:10 -0700284 /* we don't support long indices */
285 if (s >= 0x7fffffff) {
286 PyErr_SetString(PyExc_ValueError, "too many objects");
287 goto err;
288 }
Serhiy Storchaka26861b02015-02-16 20:52:17 +0200289 w = (int)s;
Serhiy Storchakace921c622015-02-11 15:53:31 +0200290 Py_INCREF(v);
291 if (_Py_HASHTABLE_SET(p->hashtable, v, w) < 0) {
292 Py_DECREF(v);
Kristján Valur Jónssond7009c62013-03-19 18:02:10 -0700293 goto err;
Serhiy Storchakace921c622015-02-11 15:53:31 +0200294 }
Kristján Valur Jónssond7009c62013-03-19 18:02:10 -0700295 *flag |= FLAG_REF;
296 return 0;
297 }
298err:
299 p->error = WFERR_UNMARSHALLABLE;
300 return 1;
301}
302
303static void
304w_complex_object(PyObject *v, char flag, WFILE *p);
305
Guido van Rossumb0c168c1996-12-05 23:15:02 +0000306static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000307w_object(PyObject *v, WFILE *p)
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000308{
Kristján Valur Jónssond7009c62013-03-19 18:02:10 -0700309 char flag = '\0';
Fred Drake6da0b912000-06-28 18:47:56 +0000310
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000311 p->depth++;
Tim Petersd9b9ac82001-01-28 00:27:39 +0000312
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000313 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ónssond7009c62013-03-19 18:02:10 -0700334 else if (!w_ref(v, &flag, p))
335 w_complex_object(v, flag, p);
336
337 p->depth--;
338}
339
340static void
341w_complex_object(PyObject *v, char flag, WFILE *p)
342{
343 Py_ssize_t i, n;
344
345 if (PyLong_CheckExact(v)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000346 long x = PyLong_AsLong(v);
347 if ((x == -1) && PyErr_Occurred()) {
348 PyLongObject *ob = (PyLongObject *)v;
349 PyErr_Clear();
Kristján Valur Jónssond7009c62013-03-19 18:02:10 -0700350 w_PyLong(ob, flag, p);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000351 }
352 else {
Guido van Rossumc1547d91996-12-10 15:39:04 +0000353#if SIZEOF_LONG > 4
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000354 long y = Py_ARITHMETIC_RIGHT_SHIFT(long, x, 31);
355 if (y && y != -1) {
Martin v. Löwis7e395722012-07-28 19:44:05 +0200356 /* Too large for TYPE_INT */
Kristján Valur Jónssond7009c62013-03-19 18:02:10 -0700357 w_PyLong((PyLongObject*)v, flag, p);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000358 }
359 else
Guido van Rossumc1547d91996-12-10 15:39:04 +0000360#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000361 {
Kristján Valur Jónssond7009c62013-03-19 18:02:10 -0700362 W_TYPE(TYPE_INT, p);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000363 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ónssond7009c62013-03-19 18:02:10 -0700375 W_TYPE(TYPE_BINARY_FLOAT, p);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000376 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ónssond7009c62013-03-19 18:02:10 -0700386 W_TYPE(TYPE_FLOAT, p);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000387 w_byte((int)n, p);
Serhiy Storchaka7e019112013-02-13 12:08:15 +0200388 w_string(buf, n, p);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000389 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ónssond7009c62013-03-19 18:02:10 -0700400 W_TYPE(TYPE_BINARY_COMPLEX, p);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000401 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ónssond7009c62013-03-19 18:02:10 -0700411 W_TYPE(TYPE_COMPLEX, p);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000412 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 Storchaka7e019112013-02-13 12:08:15 +0200420 w_string(buf, n, p);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000421 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 Storchaka7e019112013-02-13 12:08:15 +0200430 w_string(buf, n, p);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000431 PyMem_Free(buf);
432 }
433 }
434 else if (PyBytes_CheckExact(v)) {
Kristján Valur Jónssond7009c62013-03-19 18:02:10 -0700435 W_TYPE(TYPE_STRING, p);
Serhiy Storchakadfde2152013-07-11 19:14:26 +0300436 w_pstring(PyBytes_AS_STRING(v), PyBytes_GET_SIZE(v), p);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000437 }
438 else if (PyUnicode_CheckExact(v)) {
Antoine Pitrou1164dfc2013-10-12 22:25:39 +0200439 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 Pitrouf95a1b32010-05-09 15:52:27 +0000457 }
Antoine Pitrou1164dfc2013-10-12 22:25:39 +0200458 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 Pitrouf95a1b32010-05-09 15:52:27 +0000473 }
474 else if (PyTuple_CheckExact(v)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000475 n = PyTuple_Size(v);
Antoine Pitrou1164dfc2013-10-12 22:25:39 +0200476 if (p->version >= 4 && n < 256) {
477 W_TYPE(TYPE_SMALL_TUPLE, p);
Victor Stinnerda062552013-11-16 00:13:29 +0100478 w_byte((unsigned char)n, p);
Antoine Pitrou1164dfc2013-10-12 22:25:39 +0200479 }
480 else {
481 W_TYPE(TYPE_TUPLE, p);
482 W_SIZE(n, p);
483 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000484 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ónssond7009c62013-03-19 18:02:10 -0700489 W_TYPE(TYPE_LIST, p);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000490 n = PyList_GET_SIZE(v);
Serhiy Storchaka7e019112013-02-13 12:08:15 +0200491 W_SIZE(n, p);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000492 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ónssond7009c62013-03-19 18:02:10 -0700499 W_TYPE(TYPE_DICT, p);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000500 /* 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 Hettingera422c342005-01-11 03:03:27 +0000510
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000511 if (PyObject_TypeCheck(v, &PySet_Type))
Kristján Valur Jónssond7009c62013-03-19 18:02:10 -0700512 W_TYPE(TYPE_SET, p);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000513 else
Kristján Valur Jónssond7009c62013-03-19 18:02:10 -0700514 W_TYPE(TYPE_FROZENSET, p);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000515 n = PyObject_Size(v);
516 if (n == -1) {
517 p->depth--;
518 p->error = WFERR_UNMARSHALLABLE;
519 return;
520 }
Serhiy Storchaka7e019112013-02-13 12:08:15 +0200521 W_SIZE(n, p);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000522 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ónssond7009c62013-03-19 18:02:10 -0700541 W_TYPE(TYPE_CODE, p);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000542 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 Storchakac611a5b2017-03-12 08:53:22 +0200559 /* Write unknown bytes-like objects as a bytes object */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000560 Py_buffer view;
Antoine Pitrou679e9d32012-03-02 18:12:43 +0100561 if (PyObject_GetBuffer(v, &view, PyBUF_SIMPLE) != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000562 w_byte(TYPE_UNKNOWN, p);
Antoine Pitrou679e9d32012-03-02 18:12:43 +0100563 p->depth--;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000564 p->error = WFERR_UNMARSHALLABLE;
Antoine Pitrou679e9d32012-03-02 18:12:43 +0100565 return;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000566 }
Kristján Valur Jónssond7009c62013-03-19 18:02:10 -0700567 W_TYPE(TYPE_STRING, p);
Serhiy Storchakadfde2152013-07-11 19:14:26 +0300568 w_pstring(view.buf, view.len, p);
Antoine Pitrou679e9d32012-03-02 18:12:43 +0100569 PyBuffer_Release(&view);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000570 }
571 else {
Kristján Valur Jónssond7009c62013-03-19 18:02:10 -0700572 W_TYPE(TYPE_UNKNOWN, p);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000573 p->error = WFERR_UNMARSHALLABLE;
574 }
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000575}
576
Serhiy Storchakace921c622015-02-11 15:53:31 +0200577static int
578w_init_refs(WFILE *wf, int version)
579{
580 if (version >= 3) {
Victor Stinner285cf0a2016-03-21 22:00:58 +0100581 wf->hashtable = _Py_hashtable_new(sizeof(PyObject *), sizeof(int),
582 _Py_hashtable_hash_ptr,
Serhiy Storchakace921c622015-02-11 15:53:31 +0200583 _Py_hashtable_compare_direct);
584 if (wf->hashtable == NULL) {
585 PyErr_NoMemory();
586 return -1;
587 }
588 }
589 return 0;
590}
591
592static int
Victor Stinner285cf0a2016-03-21 22:00:58 +0100593w_decref_entry(_Py_hashtable_t *ht, _Py_hashtable_entry_t *entry,
594 void *Py_UNUSED(data))
Serhiy Storchakace921c622015-02-11 15:53:31 +0200595{
Victor Stinner285cf0a2016-03-21 22:00:58 +0100596 PyObject *entry_key;
597
Victor Stinner5dacbd42016-03-23 09:52:13 +0100598 _Py_HASHTABLE_ENTRY_READ_KEY(ht, entry, entry_key);
Victor Stinner285cf0a2016-03-21 22:00:58 +0100599 Py_XDECREF(entry_key);
Serhiy Storchakace921c622015-02-11 15:53:31 +0200600 return 0;
601}
602
603static void
604w_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 Storchaka95949422013-08-27 19:40:23 +0300612/* version currently has no effect for writing ints. */
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000613void
Martin v. Löwisef82d2f2004-06-27 16:51:46 +0000614PyMarshal_WriteLongToFile(long x, FILE *fp, int version)
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000615{
Serhiy Storchakac1efe5f2015-02-11 15:54:54 +0200616 char buf[4];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000617 WFILE wf;
Serhiy Storchakace921c622015-02-11 15:53:31 +0200618 memset(&wf, 0, sizeof(wf));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000619 wf.fp = fp;
Serhiy Storchakac1efe5f2015-02-11 15:54:54 +0200620 wf.ptr = wf.buf = buf;
621 wf.end = wf.ptr + sizeof(buf);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000622 wf.error = WFERR_OK;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000623 wf.version = version;
624 w_long(x, &wf);
Serhiy Storchakac1efe5f2015-02-11 15:54:54 +0200625 w_flush(&wf);
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000626}
627
628void
Martin v. Löwisef82d2f2004-06-27 16:51:46 +0000629PyMarshal_WriteObjectToFile(PyObject *x, FILE *fp, int version)
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000630{
Serhiy Storchakac1efe5f2015-02-11 15:54:54 +0200631 char buf[BUFSIZ];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000632 WFILE wf;
Serhiy Storchakace921c622015-02-11 15:53:31 +0200633 memset(&wf, 0, sizeof(wf));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000634 wf.fp = fp;
Serhiy Storchakac1efe5f2015-02-11 15:54:54 +0200635 wf.ptr = wf.buf = buf;
636 wf.end = wf.ptr + sizeof(buf);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000637 wf.error = WFERR_OK;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000638 wf.version = version;
Serhiy Storchakace921c622015-02-11 15:53:31 +0200639 if (w_init_refs(&wf, version))
640 return; /* caller mush check PyErr_Occurred() */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000641 w_object(x, &wf);
Serhiy Storchakace921c622015-02-11 15:53:31 +0200642 w_clear_refs(&wf);
Serhiy Storchakac1efe5f2015-02-11 15:54:54 +0200643 w_flush(&wf);
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000644}
645
Serhiy Storchakac07422c2015-02-11 16:18:09 +0200646typedef 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 Rossum0b0db8e1993-01-21 16:07:51 +0000657
Serhiy Storchakaef1585e2015-12-25 20:01:53 +0200658static const char *
Antoine Pitrou1164dfc2013-10-12 22:25:39 +0200659r_string(Py_ssize_t n, RFILE *p)
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000660{
Antoine Pitrou1164dfc2013-10-12 22:25:39 +0200661 Py_ssize_t read = -1;
Vinay Sajip5bdae3b2011-07-02 16:42:47 +0100662
Antoine Pitrou1164dfc2013-10-12 22:25:39 +0200663 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 Sajip5bdae3b2011-07-02 16:42:47 +0100671 }
Antoine Pitrou1164dfc2013-10-12 22:25:39 +0200672 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 Stinner763b0d12013-10-31 16:56:38 +0100691
Antoine Pitrou1164dfc2013-10-12 22:25:39 +0200692 if (!p->readable) {
693 assert(p->fp != NULL);
Antoine Pitrou1164dfc2013-10-12 22:25:39 +0200694 read = fread(p->buf, 1, n, p->fp);
Vinay Sajip5bdae3b2011-07-02 16:42:47 +0100695 }
696 else {
Antoine Pitrou1164dfc2013-10-12 22:25:39 +0200697 _Py_IDENTIFIER(readinto);
698 PyObject *res, *mview;
699 Py_buffer buf;
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200700
Antoine Pitrou1164dfc2013-10-12 22:25:39 +0200701 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 Sajip5bdae3b2011-07-02 16:42:47 +0100711 }
712 }
Antoine Pitrou1164dfc2013-10-12 22:25:39 +0200713 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 Sajip5bdae3b2011-07-02 16:42:47 +0100725 }
Antoine Pitrou1164dfc2013-10-12 22:25:39 +0200726 return p->buf;
Vinay Sajip5bdae3b2011-07-02 16:42:47 +0100727}
728
Vinay Sajip5bdae3b2011-07-02 16:42:47 +0100729static int
730r_byte(RFILE *p)
731{
732 int c = EOF;
Vinay Sajip5bdae3b2011-07-02 16:42:47 +0100733
Antoine Pitrou1164dfc2013-10-12 22:25:39 +0200734 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 Sajip5bdae3b2011-07-02 16:42:47 +0100743 else {
Serhiy Storchakaef1585e2015-12-25 20:01:53 +0200744 const char *ptr = r_string(1, p);
Antoine Pitrou1164dfc2013-10-12 22:25:39 +0200745 if (ptr != NULL)
746 c = *(unsigned char *) ptr;
Vinay Sajip5bdae3b2011-07-02 16:42:47 +0100747 }
748 return c;
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000749}
750
751static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000752r_short(RFILE *p)
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000753{
Antoine Pitrou1164dfc2013-10-12 22:25:39 +0200754 short x = -1;
Serhiy Storchakaef1585e2015-12-25 20:01:53 +0200755 const unsigned char *buffer;
Vinay Sajip5bdae3b2011-07-02 16:42:47 +0100756
Serhiy Storchakaef1585e2015-12-25 20:01:53 +0200757 buffer = (const unsigned char *) r_string(2, p);
Antoine Pitrou1164dfc2013-10-12 22:25:39 +0200758 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 Pitrouf95a1b32010-05-09 15:52:27 +0000764 return x;
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000765}
766
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000767static long
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000768r_long(RFILE *p)
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000769{
Antoine Pitrou1164dfc2013-10-12 22:25:39 +0200770 long x = -1;
Serhiy Storchakaef1585e2015-12-25 20:01:53 +0200771 const unsigned char *buffer;
Vinay Sajip5bdae3b2011-07-02 16:42:47 +0100772
Serhiy Storchakaef1585e2015-12-25 20:01:53 +0200773 buffer = (const unsigned char *) r_string(4, p);
Antoine Pitrou1164dfc2013-10-12 22:25:39 +0200774 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 Rossumc1547d91996-12-10 15:39:04 +0000779#if SIZEOF_LONG > 4
Antoine Pitrou1164dfc2013-10-12 22:25:39 +0200780 /* Sign extension for 64-bit machines */
781 x |= -(x & 0x80000000L);
Guido van Rossumc1547d91996-12-10 15:39:04 +0000782#endif
Antoine Pitrou1164dfc2013-10-12 22:25:39 +0200783 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000784 return x;
Guido van Rossumb0c168c1996-12-05 23:15:02 +0000785}
786
Guido van Rossum79f25d91997-04-29 20:08:16 +0000787static PyObject *
Mark Dickinsonbd792642009-03-18 20:06:12 +0000788r_PyLong(RFILE *p)
789{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000790 PyLongObject *ob;
Serhiy Storchaka7e019112013-02-13 12:08:15 +0200791 long n, size, i;
792 int j, md, shorts_in_top_digit;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000793 digit d;
Mark Dickinsonbd792642009-03-18 20:06:12 +0000794
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000795 n = r_long(p);
Vinay Sajip5bdae3b2011-07-02 16:42:47 +0100796 if (PyErr_Occurred())
797 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000798 if (n == 0)
799 return (PyObject *)_PyLong_New(0);
Serhiy Storchaka7e019112013-02-13 12:08:15 +0200800 if (n < -SIZE32_MAX || n > SIZE32_MAX) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000801 PyErr_SetString(PyExc_ValueError,
802 "bad marshal data (long size out of range)");
803 return NULL;
804 }
Mark Dickinsonbd792642009-03-18 20:06:12 +0000805
Victor Stinner45e8e2f2014-05-14 17:24:35 +0200806 size = 1 + (Py_ABS(n) - 1) / PyLong_MARSHAL_RATIO;
807 shorts_in_top_digit = 1 + (Py_ABS(n) - 1) % PyLong_MARSHAL_RATIO;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000808 ob = _PyLong_New(size);
809 if (ob == NULL)
810 return NULL;
Victor Stinner763b0d12013-10-31 16:56:38 +0100811
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000812 Py_SIZE(ob) = n > 0 ? size : -size;
Mark Dickinsonbd792642009-03-18 20:06:12 +0000813
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000814 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 Stinner763b0d12013-10-31 16:56:38 +0100818 if (PyErr_Occurred()) {
819 Py_DECREF(ob);
820 return NULL;
821 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000822 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 Stinner763b0d12013-10-31 16:56:38 +0100828
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000829 d = 0;
830 for (j=0; j < shorts_in_top_digit; j++) {
831 md = r_short(p);
Victor Stinner763b0d12013-10-31 16:56:38 +0100832 if (PyErr_Occurred()) {
833 Py_DECREF(ob);
834 return NULL;
835 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000836 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 Sajip5bdae3b2011-07-02 16:42:47 +0100847 if (PyErr_Occurred()) {
848 Py_DECREF(ob);
849 return NULL;
850 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000851 /* 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 Dickinsonbd792642009-03-18 20:06:12 +0000855 bad_digit:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000856 Py_DECREF(ob);
857 PyErr_SetString(PyExc_ValueError,
858 "bad marshal data (digit out of range in long)");
859 return NULL;
Mark Dickinsonbd792642009-03-18 20:06:12 +0000860}
861
Kristján Valur Jónssone1781872013-03-20 11:43:57 -0700862/* allocate the reflist index for a new object. Return -1 on failure */
863static Py_ssize_t
864r_ref_reserve(int flag, RFILE *p)
Kristján Valur Jónssond7009c62013-03-19 18:02:10 -0700865{
866 if (flag) { /* currently only FLAG_REF is defined */
Antoine Pitrou1164dfc2013-10-12 22:25:39 +0200867 Py_ssize_t idx = PyList_GET_SIZE(p->refs);
Kristján Valur Jónssone1781872013-03-20 11:43:57 -0700868 if (idx >= 0x7ffffffe) {
Kristján Valur Jónssond7009c62013-03-19 18:02:10 -0700869 PyErr_SetString(PyExc_ValueError, "bad marshal data (index list too large)");
Kristján Valur Jónssone1781872013-03-20 11:43:57 -0700870 return -1;
Kristján Valur Jónssond7009c62013-03-19 18:02:10 -0700871 }
872 if (PyList_Append(p->refs, Py_None) < 0)
Kristján Valur Jónssone1781872013-03-20 11:43:57 -0700873 return -1;
874 return idx;
Kristján Valur Jónssond7009c62013-03-19 18:02:10 -0700875 } else
Kristján Valur Jónssone1781872013-03-20 11:43:57 -0700876 return 0;
Kristján Valur Jónssond7009c62013-03-19 18:02:10 -0700877}
878
Kristján Valur Jónssone1781872013-03-20 11:43:57 -0700879/* 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ónssond7009c62013-03-19 18:02:10 -0700887static PyObject *
888r_ref_insert(PyObject *o, Py_ssize_t idx, int flag, RFILE *p)
889{
Kristján Valur Jónssone1781872013-03-20 11:43:57 -0700890 if (o != NULL && flag) { /* currently only FLAG_REF is defined */
Antoine Pitrou1164dfc2013-10-12 22:25:39 +0200891 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ónssond7009c62013-03-19 18:02:10 -0700895 }
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 */
903static PyObject *
904r_ref(PyObject *o, int flag, RFILE *p)
905{
Victor Stinner359fabc2013-10-31 17:09:01 +0100906 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ónssond7009c62013-03-19 18:02:10 -0700912 }
913 return o;
914}
Mark Dickinsonbd792642009-03-18 20:06:12 +0000915
916static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000917r_object(RFILE *p)
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000918{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000919 /* NULL is a valid return value, it does not necessarily means that
920 an exception is set. */
921 PyObject *v, *v2;
Benjamin Petersoneddb0a72013-03-20 00:40:07 -0500922 Py_ssize_t idx = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000923 long i, n;
Kristján Valur Jónsson61683622013-03-20 14:26:33 -0700924 int type, code = r_byte(p);
Antoine Pitrou1164dfc2013-10-12 22:25:39 +0200925 int flag, is_interned = 0;
Christian Heimes35728422013-10-13 02:29:06 +0200926 PyObject *retval = NULL;
Guido van Rossumd59da4b2007-05-22 18:11:13 +0000927
Kristján Valur Jónsson61683622013-03-20 14:26:33 -0700928 if (code == EOF) {
929 PyErr_SetString(PyExc_EOFError,
930 "EOF read where object expected");
931 return NULL;
932 }
933
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000934 p->depth++;
Guido van Rossumd59da4b2007-05-22 18:11:13 +0000935
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000936 if (p->depth > MAX_MARSHAL_STACK_DEPTH) {
937 p->depth--;
938 PyErr_SetString(PyExc_ValueError, "recursion limit exceeded");
939 return NULL;
940 }
Tim Petersd9b9ac82001-01-28 00:27:39 +0000941
Kristján Valur Jónsson61683622013-03-20 14:26:33 -0700942 flag = code & FLAG_REF;
943 type = code & ~FLAG_REF;
Kristján Valur Jónssond7009c62013-03-19 18:02:10 -0700944
945#define R_REF(O) do{\
Kristján Valur Jónssone1781872013-03-20 11:43:57 -0700946 if (flag) \
947 O = r_ref(O, flag, p);\
Kristján Valur Jónssond7009c62013-03-19 18:02:10 -0700948} while (0)
949
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000950 switch (type) {
Tim Petersd9b9ac82001-01-28 00:27:39 +0000951
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000952 case TYPE_NULL:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000953 break;
Tim Petersd9b9ac82001-01-28 00:27:39 +0000954
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000955 case TYPE_NONE:
956 Py_INCREF(Py_None);
957 retval = Py_None;
958 break;
Tim Petersd9b9ac82001-01-28 00:27:39 +0000959
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000960 case TYPE_STOPITER:
961 Py_INCREF(PyExc_StopIteration);
962 retval = PyExc_StopIteration;
963 break;
Tim Peters5ca576e2001-06-18 22:08:13 +0000964
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000965 case TYPE_ELLIPSIS:
966 Py_INCREF(Py_Ellipsis);
967 retval = Py_Ellipsis;
968 break;
Tim Petersd9b9ac82001-01-28 00:27:39 +0000969
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000970 case TYPE_FALSE:
971 Py_INCREF(Py_False);
972 retval = Py_False;
973 break;
Guido van Rossum77f6a652002-04-03 22:41:51 +0000974
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000975 case TYPE_TRUE:
976 Py_INCREF(Py_True);
977 retval = Py_True;
978 break;
Guido van Rossum77f6a652002-04-03 22:41:51 +0000979
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000980 case TYPE_INT:
Vinay Sajip5bdae3b2011-07-02 16:42:47 +0100981 n = r_long(p);
982 retval = PyErr_Occurred() ? NULL : PyLong_FromLong(n);
Kristján Valur Jónssond7009c62013-03-19 18:02:10 -0700983 R_REF(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000984 break;
Tim Petersd9b9ac82001-01-28 00:27:39 +0000985
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000986 case TYPE_LONG:
987 retval = r_PyLong(p);
Kristján Valur Jónssond7009c62013-03-19 18:02:10 -0700988 R_REF(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000989 break;
Tim Petersd9b9ac82001-01-28 00:27:39 +0000990
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000991 case TYPE_FLOAT:
992 {
Serhiy Storchakaef1585e2015-12-25 20:01:53 +0200993 char buf[256];
994 const char *ptr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000995 double dx;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000996 n = r_byte(p);
Serhiy Storchaka3641a742013-07-11 22:20:47 +0300997 if (n == EOF) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000998 PyErr_SetString(PyExc_EOFError,
999 "EOF read where object expected");
1000 break;
1001 }
Antoine Pitrou1164dfc2013-10-12 22:25:39 +02001002 ptr = r_string(n, p);
1003 if (ptr == NULL)
Serhiy Storchaka3641a742013-07-11 22:20:47 +03001004 break;
Antoine Pitrou1164dfc2013-10-12 22:25:39 +02001005 memcpy(buf, ptr, n);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001006 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ónssond7009c62013-03-19 18:02:10 -07001011 R_REF(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001012 break;
1013 }
Tim Petersd9b9ac82001-01-28 00:27:39 +00001014
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001015 case TYPE_BINARY_FLOAT:
1016 {
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001017 const unsigned char *buf;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001018 double x;
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001019 buf = (const unsigned char *) r_string(8, p);
Kristján Valur Jónsson59832582013-10-13 13:41:59 +00001020 if (buf == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001021 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001022 x = _PyFloat_Unpack8(buf, 1);
Kristján Valur Jónsson59832582013-10-13 13:41:59 +00001023 if (x == -1.0 && PyErr_Occurred())
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001024 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001025 retval = PyFloat_FromDouble(x);
Kristján Valur Jónssond7009c62013-03-19 18:02:10 -07001026 R_REF(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001027 break;
1028 }
Michael W. Hudsondf888462005-06-03 14:41:55 +00001029
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001030 case TYPE_COMPLEX:
1031 {
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001032 char buf[256];
1033 const char *ptr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001034 Py_complex c;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001035 n = r_byte(p);
Serhiy Storchaka3641a742013-07-11 22:20:47 +03001036 if (n == EOF) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001037 PyErr_SetString(PyExc_EOFError,
1038 "EOF read where object expected");
1039 break;
1040 }
Antoine Pitrou1164dfc2013-10-12 22:25:39 +02001041 ptr = r_string(n, p);
1042 if (ptr == NULL)
Serhiy Storchaka3641a742013-07-11 22:20:47 +03001043 break;
Antoine Pitrou1164dfc2013-10-12 22:25:39 +02001044 memcpy(buf, ptr, n);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001045 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 Storchaka3641a742013-07-11 22:20:47 +03001050 if (n == EOF) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001051 PyErr_SetString(PyExc_EOFError,
1052 "EOF read where object expected");
1053 break;
1054 }
Antoine Pitrou1164dfc2013-10-12 22:25:39 +02001055 ptr = r_string(n, p);
1056 if (ptr == NULL)
Serhiy Storchaka3641a742013-07-11 22:20:47 +03001057 break;
Antoine Pitrou1164dfc2013-10-12 22:25:39 +02001058 memcpy(buf, ptr, n);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001059 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ónssond7009c62013-03-19 18:02:10 -07001064 R_REF(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001065 break;
1066 }
Michael W. Hudsondf888462005-06-03 14:41:55 +00001067
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001068 case TYPE_BINARY_COMPLEX:
1069 {
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001070 const unsigned char *buf;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001071 Py_complex c;
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001072 buf = (const unsigned char *) r_string(8, p);
Kristján Valur Jónsson59832582013-10-13 13:41:59 +00001073 if (buf == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001074 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001075 c.real = _PyFloat_Unpack8(buf, 1);
Kristján Valur Jónsson59832582013-10-13 13:41:59 +00001076 if (c.real == -1.0 && PyErr_Occurred())
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001077 break;
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001078 buf = (const unsigned char *) r_string(8, p);
Kristján Valur Jónsson59832582013-10-13 13:41:59 +00001079 if (buf == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001080 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001081 c.imag = _PyFloat_Unpack8(buf, 1);
Kristján Valur Jónsson59832582013-10-13 13:41:59 +00001082 if (c.imag == -1.0 && PyErr_Occurred())
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001083 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001084 retval = PyComplex_FromCComplex(c);
Kristján Valur Jónssond7009c62013-03-19 18:02:10 -07001085 R_REF(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001086 break;
1087 }
Tim Petersd9b9ac82001-01-28 00:27:39 +00001088
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001089 case TYPE_STRING:
Antoine Pitrou1164dfc2013-10-12 22:25:39 +02001090 {
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001091 const char *ptr;
Antoine Pitrou1164dfc2013-10-12 22:25:39 +02001092 n = r_long(p);
Kristján Valur Jónsson59832582013-10-13 13:41:59 +00001093 if (PyErr_Occurred())
Antoine Pitrou1164dfc2013-10-12 22:25:39 +02001094 break;
Antoine Pitrou1164dfc2013-10-12 22:25:39 +02001095 if (n < 0 || n > SIZE32_MAX) {
Serhiy Storchakac611a5b2017-03-12 08:53:22 +02001096 PyErr_SetString(PyExc_ValueError, "bad marshal data (bytes object size out of range)");
Antoine Pitrou1164dfc2013-10-12 22:25:39 +02001097 break;
1098 }
1099 v = PyBytes_FromStringAndSize((char *)NULL, n);
Kristján Valur Jónsson59832582013-10-13 13:41:59 +00001100 if (v == NULL)
Antoine Pitrou1164dfc2013-10-12 22:25:39 +02001101 break;
Antoine Pitrou1164dfc2013-10-12 22:25:39 +02001102 ptr = r_string(n, p);
1103 if (ptr == NULL) {
1104 Py_DECREF(v);
Antoine Pitrou1164dfc2013-10-12 22:25:39 +02001105 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 Krahf432a322017-08-21 13:09:59 +02001115 /* fall through */
Antoine Pitrou1164dfc2013-10-12 22:25:39 +02001116 case TYPE_ASCII:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001117 n = r_long(p);
Kristján Valur Jónsson59832582013-10-13 13:41:59 +00001118 if (PyErr_Occurred())
Vinay Sajip5bdae3b2011-07-02 16:42:47 +01001119 break;
Serhiy Storchaka7e019112013-02-13 12:08:15 +02001120 if (n < 0 || n > SIZE32_MAX) {
Serhiy Storchakac611a5b2017-03-12 08:53:22 +02001121 PyErr_SetString(PyExc_ValueError, "bad marshal data (string size out of range)");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001122 break;
1123 }
Antoine Pitrou1164dfc2013-10-12 22:25:39 +02001124 goto _read_ascii;
Tim Petersd9b9ac82001-01-28 00:27:39 +00001125
Antoine Pitrou1164dfc2013-10-12 22:25:39 +02001126 case TYPE_SHORT_ASCII_INTERNED:
1127 is_interned = 1;
Stefan Krahf432a322017-08-21 13:09:59 +02001128 /* fall through */
Antoine Pitrou1164dfc2013-10-12 22:25:39 +02001129 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 Storchakaef1585e2015-12-25 20:01:53 +02001138 const char *ptr;
Antoine Pitrou1164dfc2013-10-12 22:25:39 +02001139 ptr = r_string(n, p);
Kristján Valur Jónsson59832582013-10-13 13:41:59 +00001140 if (ptr == NULL)
Antoine Pitrou1164dfc2013-10-12 22:25:39 +02001141 break;
Antoine Pitrou1164dfc2013-10-12 22:25:39 +02001142 v = PyUnicode_FromKindAndData(PyUnicode_1BYTE_KIND, ptr, n);
Kristján Valur Jónsson59832582013-10-13 13:41:59 +00001143 if (v == NULL)
Antoine Pitrou1164dfc2013-10-12 22:25:39 +02001144 break;
Antoine Pitrou1164dfc2013-10-12 22:25:39 +02001145 if (is_interned)
1146 PyUnicode_InternInPlace(&v);
1147 retval = v;
1148 R_REF(retval);
1149 break;
1150 }
1151
Kristján Valur Jónssond7009c62013-03-19 18:02:10 -07001152 case TYPE_INTERNED:
Antoine Pitrou1164dfc2013-10-12 22:25:39 +02001153 is_interned = 1;
Stefan Krahf432a322017-08-21 13:09:59 +02001154 /* fall through */
Antoine Pitrou1164dfc2013-10-12 22:25:39 +02001155 case TYPE_UNICODE:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001156 {
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001157 const char *buffer;
Guido van Rossumc279b532000-03-10 23:03:02 +00001158
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001159 n = r_long(p);
Kristján Valur Jónsson59832582013-10-13 13:41:59 +00001160 if (PyErr_Occurred())
Vinay Sajip5bdae3b2011-07-02 16:42:47 +01001161 break;
Serhiy Storchaka7e019112013-02-13 12:08:15 +02001162 if (n < 0 || n > SIZE32_MAX) {
Serhiy Storchakac611a5b2017-03-12 08:53:22 +02001163 PyErr_SetString(PyExc_ValueError, "bad marshal data (string size out of range)");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001164 break;
1165 }
Victor Stinnerf1913ca2013-06-21 19:08:06 +02001166 if (n != 0) {
Antoine Pitrou1164dfc2013-10-12 22:25:39 +02001167 buffer = r_string(n, p);
Kristján Valur Jónsson59832582013-10-13 13:41:59 +00001168 if (buffer == NULL)
Victor Stinnerf1913ca2013-06-21 19:08:06 +02001169 break;
Victor Stinnerf1913ca2013-06-21 19:08:06 +02001170 v = PyUnicode_DecodeUTF8(buffer, n, "surrogatepass");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001171 }
Victor Stinnerf1913ca2013-06-21 19:08:06 +02001172 else {
1173 v = PyUnicode_New(0, 0);
1174 }
Kristján Valur Jónsson59832582013-10-13 13:41:59 +00001175 if (v == NULL)
Victor Stinner3a8b79d2013-07-08 22:23:32 +02001176 break;
Antoine Pitrou1164dfc2013-10-12 22:25:39 +02001177 if (is_interned)
Kristján Valur Jónssond7009c62013-03-19 18:02:10 -07001178 PyUnicode_InternInPlace(&v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001179 retval = v;
Kristján Valur Jónssond7009c62013-03-19 18:02:10 -07001180 R_REF(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001181 break;
1182 }
Tim Petersd9b9ac82001-01-28 00:27:39 +00001183
Antoine Pitrou1164dfc2013-10-12 22:25:39 +02001184 case TYPE_SMALL_TUPLE:
1185 n = (unsigned char) r_byte(p);
Victor Stinnerb1b7b182013-10-31 17:07:08 +01001186 if (PyErr_Occurred())
1187 break;
Antoine Pitrou1164dfc2013-10-12 22:25:39 +02001188 goto _read_tuple;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001189 case TYPE_TUPLE:
1190 n = r_long(p);
Kristján Valur Jónsson59832582013-10-13 13:41:59 +00001191 if (PyErr_Occurred())
Vinay Sajip5bdae3b2011-07-02 16:42:47 +01001192 break;
Serhiy Storchaka7e019112013-02-13 12:08:15 +02001193 if (n < 0 || n > SIZE32_MAX) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001194 PyErr_SetString(PyExc_ValueError, "bad marshal data (tuple size out of range)");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001195 break;
1196 }
Antoine Pitrou1164dfc2013-10-12 22:25:39 +02001197 _read_tuple:
Serhiy Storchaka7e019112013-02-13 12:08:15 +02001198 v = PyTuple_New(n);
Kristján Valur Jónssond7009c62013-03-19 18:02:10 -07001199 R_REF(v);
Kristján Valur Jónsson59832582013-10-13 13:41:59 +00001200 if (v == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001201 break;
Victor Stinnerd5cae6f2013-10-31 17:14:52 +01001202
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001203 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 Storchaka7e019112013-02-13 12:08:15 +02001213 PyTuple_SET_ITEM(v, i, v2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001214 }
1215 retval = v;
1216 break;
Tim Petersd9b9ac82001-01-28 00:27:39 +00001217
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001218 case TYPE_LIST:
1219 n = r_long(p);
Kristján Valur Jónsson59832582013-10-13 13:41:59 +00001220 if (PyErr_Occurred())
Vinay Sajip5bdae3b2011-07-02 16:42:47 +01001221 break;
Serhiy Storchaka7e019112013-02-13 12:08:15 +02001222 if (n < 0 || n > SIZE32_MAX) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001223 PyErr_SetString(PyExc_ValueError, "bad marshal data (list size out of range)");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001224 break;
1225 }
Serhiy Storchaka7e019112013-02-13 12:08:15 +02001226 v = PyList_New(n);
Kristján Valur Jónssond7009c62013-03-19 18:02:10 -07001227 R_REF(v);
Kristján Valur Jónsson59832582013-10-13 13:41:59 +00001228 if (v == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001229 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001230 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 Storchaka7e019112013-02-13 12:08:15 +02001240 PyList_SET_ITEM(v, i, v2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001241 }
1242 retval = v;
1243 break;
Tim Petersd9b9ac82001-01-28 00:27:39 +00001244
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001245 case TYPE_DICT:
1246 v = PyDict_New();
Kristján Valur Jónssond7009c62013-03-19 18:02:10 -07001247 R_REF(v);
Kristján Valur Jónsson59832582013-10-13 13:41:59 +00001248 if (v == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001249 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001250 for (;;) {
1251 PyObject *key, *val;
1252 key = r_object(p);
1253 if (key == NULL)
1254 break;
1255 val = r_object(p);
Victor Stinnerd5cae6f2013-10-31 17:14:52 +01001256 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 Pitrouf95a1b32010-05-09 15:52:27 +00001265 Py_DECREF(key);
Victor Stinnerd5cae6f2013-10-31 17:14:52 +01001266 Py_DECREF(val);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001267 }
1268 if (PyErr_Occurred()) {
1269 Py_DECREF(v);
1270 v = NULL;
1271 }
1272 retval = v;
1273 break;
Tim Petersd9b9ac82001-01-28 00:27:39 +00001274
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001275 case TYPE_SET:
1276 case TYPE_FROZENSET:
1277 n = r_long(p);
Kristján Valur Jónsson59832582013-10-13 13:41:59 +00001278 if (PyErr_Occurred())
Vinay Sajip5bdae3b2011-07-02 16:42:47 +01001279 break;
Serhiy Storchaka7e019112013-02-13 12:08:15 +02001280 if (n < 0 || n > SIZE32_MAX) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001281 PyErr_SetString(PyExc_ValueError, "bad marshal data (set size out of range)");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001282 break;
1283 }
Victor Stinnerd5cae6f2013-10-31 17:14:52 +01001284
Victor Stinner1aa78932016-01-23 14:15:48 +01001285 if (n == 0 && type == TYPE_FROZENSET) {
1286 /* call frozenset() to get the empty frozenset singleton */
Victor Stinner4778eab2016-12-01 14:51:04 +01001287 v = _PyObject_CallNoArg((PyObject*)&PyFrozenSet_Type);
Victor Stinner1aa78932016-01-23 14:15:48 +01001288 if (v == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001289 break;
Victor Stinner1aa78932016-01-23 14:15:48 +01001290 R_REF(v);
1291 retval = v;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001292 }
Victor Stinner1aa78932016-01-23 14:15:48 +01001293 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 Pitrouf95a1b32010-05-09 15:52:27 +00001330 break;
Raymond Hettingera422c342005-01-11 03:03:27 +00001331
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001332 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 Pitroue9bbe8b2013-04-13 22:41:09 +02001349
Kristján Valur Jónssone1781872013-03-20 11:43:57 -07001350 idx = r_ref_reserve(flag, p);
Kristján Valur Jónsson59832582013-10-13 13:41:59 +00001351 if (idx < 0)
Kristján Valur Jónssond7009c62013-03-19 18:02:10 -07001352 break;
Tim Petersd9b9ac82001-01-28 00:27:39 +00001353
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001354 v = NULL;
Michael W. Hudsondf888462005-06-03 14:41:55 +00001355
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001356 /* XXX ignore long->int overflows for now */
1357 argcount = (int)r_long(p);
Vinay Sajip5bdae3b2011-07-02 16:42:47 +01001358 if (PyErr_Occurred())
1359 goto code_error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001360 kwonlyargcount = (int)r_long(p);
Vinay Sajip5bdae3b2011-07-02 16:42:47 +01001361 if (PyErr_Occurred())
1362 goto code_error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001363 nlocals = (int)r_long(p);
Vinay Sajip5bdae3b2011-07-02 16:42:47 +01001364 if (PyErr_Occurred())
1365 goto code_error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001366 stacksize = (int)r_long(p);
Vinay Sajip5bdae3b2011-07-02 16:42:47 +01001367 if (PyErr_Occurred())
1368 goto code_error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001369 flags = (int)r_long(p);
Vinay Sajip5bdae3b2011-07-02 16:42:47 +01001370 if (PyErr_Occurred())
1371 goto code_error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001372 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 Peterson43b06862011-05-27 09:08:01 -05001393 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 Pitrouf95a1b32010-05-09 15:52:27 +00001405 name = r_object(p);
1406 if (name == NULL)
1407 goto code_error;
1408 firstlineno = (int)r_long(p);
Kristján Valur Jónsson0a7697b2013-10-13 15:19:56 +00001409 if (firstlineno == -1 && PyErr_Occurred())
1410 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001411 lnotab = r_object(p);
1412 if (lnotab == NULL)
1413 goto code_error;
Michael W. Hudsondf888462005-06-03 14:41:55 +00001414
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001415 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ónssond7009c62013-03-19 18:02:10 -07001421 v = r_ref_insert(v, idx, flag, p);
Tim Petersd9b9ac82001-01-28 00:27:39 +00001422
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001423 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 Petersd9b9ac82001-01-28 00:27:39 +00001436
Kristján Valur Jónssond7009c62013-03-19 18:02:10 -07001437 case TYPE_REF:
1438 n = r_long(p);
1439 if (n < 0 || n >= PyList_GET_SIZE(p->refs)) {
Kristján Valur Jónsson0a7697b2013-10-13 15:19:56 +00001440 if (n == -1 && PyErr_Occurred())
1441 break;
Kristján Valur Jónssond7009c62013-03-19 18:02:10 -07001442 PyErr_SetString(PyExc_ValueError, "bad marshal data (invalid reference)");
Kristján Valur Jónssond7009c62013-03-19 18:02:10 -07001443 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ónssond7009c62013-03-19 18:02:10 -07001448 break;
1449 }
1450 Py_INCREF(v);
1451 retval = v;
1452 break;
1453
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001454 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 Pitrouf95a1b32010-05-09 15:52:27 +00001458 break;
1459
1460 }
1461 p->depth--;
1462 return retval;
Guido van Rossumdce2e3d1991-06-04 19:42:30 +00001463}
1464
Neal Norwitzd85c4522004-06-13 20:31:49 +00001465static PyObject *
Armin Rigo01ab2792004-03-26 15:09:27 +00001466read_object(RFILE *p)
1467{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001468 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 Rigo01ab2792004-03-26 15:09:27 +00001477}
1478
Guido van Rossumb8cf3e62001-10-19 01:46:21 +00001479int
1480PyMarshal_ReadShortFromFile(FILE *fp)
1481{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001482 RFILE rf;
Antoine Pitrou1164dfc2013-10-12 22:25:39 +02001483 int res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001484 assert(fp);
Vinay Sajip5bdae3b2011-07-02 16:42:47 +01001485 rf.readable = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001486 rf.fp = fp;
Benjamin Peterson43b06862011-05-27 09:08:01 -05001487 rf.current_filename = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001488 rf.end = rf.ptr = NULL;
Antoine Pitrou1164dfc2013-10-12 22:25:39 +02001489 rf.buf = NULL;
1490 res = r_short(&rf);
1491 if (rf.buf != NULL)
1492 PyMem_FREE(rf.buf);
1493 return res;
Guido van Rossumb8cf3e62001-10-19 01:46:21 +00001494}
1495
Guido van Rossum0b0db8e1993-01-21 16:07:51 +00001496long
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001497PyMarshal_ReadLongFromFile(FILE *fp)
Guido van Rossum0b0db8e1993-01-21 16:07:51 +00001498{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001499 RFILE rf;
Antoine Pitrou1164dfc2013-10-12 22:25:39 +02001500 long res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001501 rf.fp = fp;
Vinay Sajip5bdae3b2011-07-02 16:42:47 +01001502 rf.readable = NULL;
Benjamin Peterson43b06862011-05-27 09:08:01 -05001503 rf.current_filename = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001504 rf.ptr = rf.end = NULL;
Antoine Pitrou1164dfc2013-10-12 22:25:39 +02001505 rf.buf = NULL;
1506 res = r_long(&rf);
1507 if (rf.buf != NULL)
1508 PyMem_FREE(rf.buf);
1509 return res;
Guido van Rossum0b0db8e1993-01-21 16:07:51 +00001510}
1511
Steve Dowerf2f373f2015-02-21 08:44:05 -08001512/* Return size of file in bytes; < 0 if unknown or INT_MAX if too big */
Tim Peters691e0e92001-01-18 04:39:16 +00001513static off_t
1514getfilesize(FILE *fp)
1515{
Steve Dowerf2f373f2015-02-21 08:44:05 -08001516 struct _Py_stat_struct st;
Victor Stinnere134a7f2015-03-30 10:09:31 +02001517 if (_Py_fstat_noraise(fileno(fp), &st) != 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001518 return -1;
Steve Dowerf2f373f2015-02-21 08:44:05 -08001519#if SIZEOF_OFF_T == 4
1520 else if (st.st_size >= INT_MAX)
1521 return (off_t)INT_MAX;
1522#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001523 else
Steve Dowerf2f373f2015-02-21 08:44:05 -08001524 return (off_t)st.st_size;
Tim Peters691e0e92001-01-18 04:39:16 +00001525}
Tim Petersd9b9ac82001-01-28 00:27:39 +00001526
Tim Peters691e0e92001-01-18 04:39:16 +00001527/* 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 Petersd9b9ac82001-01-28 00:27:39 +00001530 * 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 Peters691e0e92001-01-18 04:39:16 +00001532 */
Guido van Rossum79f25d91997-04-29 20:08:16 +00001533PyObject *
Tim Petersd9b9ac82001-01-28 00:27:39 +00001534PyMarshal_ReadLastObjectFromFile(FILE *fp)
Guido van Rossum0b0db8e1993-01-21 16:07:51 +00001535{
Antoine Pitrou5bc7ec92010-04-21 22:56:22 +00001536/* REASONABLE_FILE_LIMIT is by defn something big enough for Tkinter.pyc. */
Tim Peters691e0e92001-01-18 04:39:16 +00001537#define REASONABLE_FILE_LIMIT (1L << 18)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001538 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 Storchaka7e019112013-02-13 12:08:15 +02001543 size_t n = fread(pBuf, 1, (size_t)filesize, fp);
1544 PyObject* v = PyMarshal_ReadObjectFromString(pBuf, n);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001545 PyMem_FREE(pBuf);
1546 return v;
1547 }
Tim Petersd9b9ac82001-01-28 00:27:39 +00001548
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001549 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001550 /* 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 Petersd9b9ac82001-01-28 00:27:39 +00001554
Tim Peters691e0e92001-01-18 04:39:16 +00001555#undef REASONABLE_FILE_LIMIT
Guido van Rossum0b0db8e1993-01-21 16:07:51 +00001556}
1557
Guido van Rossum79f25d91997-04-29 20:08:16 +00001558PyObject *
Tim Petersd9b9ac82001-01-28 00:27:39 +00001559PyMarshal_ReadObjectFromFile(FILE *fp)
1560{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001561 RFILE rf;
1562 PyObject *result;
1563 rf.fp = fp;
Vinay Sajip5bdae3b2011-07-02 16:42:47 +01001564 rf.readable = NULL;
Benjamin Peterson43b06862011-05-27 09:08:01 -05001565 rf.current_filename = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001566 rf.depth = 0;
1567 rf.ptr = rf.end = NULL;
Antoine Pitrou1164dfc2013-10-12 22:25:39 +02001568 rf.buf = NULL;
Kristján Valur Jónssond7009c62013-03-19 18:02:10 -07001569 rf.refs = PyList_New(0);
1570 if (rf.refs == NULL)
1571 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001572 result = r_object(&rf);
Kristján Valur Jónssond7009c62013-03-19 18:02:10 -07001573 Py_DECREF(rf.refs);
Antoine Pitrou1164dfc2013-10-12 22:25:39 +02001574 if (rf.buf != NULL)
1575 PyMem_FREE(rf.buf);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001576 return result;
Tim Petersd9b9ac82001-01-28 00:27:39 +00001577}
1578
1579PyObject *
Serhiy Storchakac6792272013-10-19 21:03:34 +03001580PyMarshal_ReadObjectFromString(const char *str, Py_ssize_t len)
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001581{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001582 RFILE rf;
1583 PyObject *result;
1584 rf.fp = NULL;
Vinay Sajip5bdae3b2011-07-02 16:42:47 +01001585 rf.readable = NULL;
Benjamin Peterson43b06862011-05-27 09:08:01 -05001586 rf.current_filename = NULL;
Serhiy Storchakac6792272013-10-19 21:03:34 +03001587 rf.ptr = (char *)str;
1588 rf.end = (char *)str + len;
Antoine Pitrou1164dfc2013-10-12 22:25:39 +02001589 rf.buf = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001590 rf.depth = 0;
Kristján Valur Jónssond7009c62013-03-19 18:02:10 -07001591 rf.refs = PyList_New(0);
1592 if (rf.refs == NULL)
1593 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001594 result = r_object(&rf);
Kristján Valur Jónssond7009c62013-03-19 18:02:10 -07001595 Py_DECREF(rf.refs);
Antoine Pitrou1164dfc2013-10-12 22:25:39 +02001596 if (rf.buf != NULL)
1597 PyMem_FREE(rf.buf);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001598 return result;
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001599}
1600
Guido van Rossum79f25d91997-04-29 20:08:16 +00001601PyObject *
Martin v. Löwisef82d2f2004-06-27 16:51:46 +00001602PyMarshal_WriteObjectToString(PyObject *x, int version)
Guido van Rossum3f3bb3d1996-08-19 22:07:17 +00001603{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001604 WFILE wf;
Guido van Rossume6d39042007-05-09 00:01:30 +00001605
Serhiy Storchakace921c622015-02-11 15:53:31 +02001606 memset(&wf, 0, sizeof(wf));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001607 wf.str = PyBytes_FromStringAndSize((char *)NULL, 50);
1608 if (wf.str == NULL)
1609 return NULL;
Serhiy Storchakac1efe5f2015-02-11 15:54:54 +02001610 wf.ptr = wf.buf = PyBytes_AS_STRING((PyBytesObject *)wf.str);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001611 wf.end = wf.ptr + PyBytes_Size(wf.str);
1612 wf.error = WFERR_OK;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001613 wf.version = version;
Serhiy Storchakace921c622015-02-11 15:53:31 +02001614 if (w_init_refs(&wf, version)) {
1615 Py_DECREF(wf.str);
1616 return NULL;
1617 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001618 w_object(x, &wf);
Serhiy Storchakace921c622015-02-11 15:53:31 +02001619 w_clear_refs(&wf);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001620 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 Storchakac611a5b2017-03-12 08:53:22 +02001625 "too much marshal data for a bytes object");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001626 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 Pitrou1c13f842012-03-02 18:22:23 +01001641 return wf.str;
Guido van Rossum3f3bb3d1996-08-19 22:07:17 +00001642}
1643
Guido van Rossum64b45521991-06-07 13:58:22 +00001644/* And an interface for Python programs... */
Serhiy Storchaka0767ad42017-03-12 09:20:15 +02001645/*[clinic input]
1646marshal.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
1656Write the value on the open file.
1657
1658If the value has (or contains an object that has) an unsupported type, a
1659ValueError exception is raised - but garbage data will also be written
1660to the file. The object will not be properly read back by load().
1661[clinic start generated code]*/
Guido van Rossumdce2e3d1991-06-04 19:42:30 +00001662
Guido van Rossum79f25d91997-04-29 20:08:16 +00001663static PyObject *
Serhiy Storchaka0767ad42017-03-12 09:20:15 +02001664marshal_dump_impl(PyObject *module, PyObject *value, PyObject *file,
1665 int version)
1666/*[clinic end generated code: output=aaee62c7028a7cb2 input=6c7a3c23c6fef556]*/
Guido van Rossumdce2e3d1991-06-04 19:42:30 +00001667{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001668 /* XXX Quick hack -- need to do this differently */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001669 PyObject *s;
1670 PyObject *res;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001671 _Py_IDENTIFIER(write);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02001672
Serhiy Storchaka0767ad42017-03-12 09:20:15 +02001673 s = PyMarshal_WriteObjectToString(value, version);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001674 if (s == NULL)
1675 return NULL;
Serhiy Storchaka0767ad42017-03-12 09:20:15 +02001676 res = _PyObject_CallMethodIdObjArgs(file, &PyId_write, s, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001677 Py_DECREF(s);
1678 return res;
Guido van Rossumdce2e3d1991-06-04 19:42:30 +00001679}
1680
Serhiy Storchaka0767ad42017-03-12 09:20:15 +02001681/*[clinic input]
1682marshal.load
1683
1684 file: object
1685 Must be readable binary file.
1686 /
1687
1688Read one value from the open file and return it.
1689
1690If no valid value is read (e.g. because the data has a different Python
1691version's incompatible marshal format), raise EOFError, ValueError or
1692TypeError.
1693
1694Note: If an object containing an unsupported type was marshalled with
1695dump(), load() will substitute None for the unmarshallable type.
1696[clinic start generated code]*/
R. David Murraydd226ea2009-05-13 12:27:21 +00001697
Guido van Rossum79f25d91997-04-29 20:08:16 +00001698static PyObject *
Serhiy Storchaka0767ad42017-03-12 09:20:15 +02001699marshal_load(PyObject *module, PyObject *file)
1700/*[clinic end generated code: output=f8e5c33233566344 input=c85c2b594cd8124a]*/
Guido van Rossumdce2e3d1991-06-04 19:42:30 +00001701{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001702 PyObject *data, *result;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001703 _Py_IDENTIFIER(read);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001704 RFILE rf;
Vinay Sajip5bdae3b2011-07-02 16:42:47 +01001705
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ónssond7009c62013-03-19 18:02:10 -07001710 * This can be removed if we guarantee good error handling
1711 * for r_string()
Vinay Sajip5bdae3b2011-07-02 16:42:47 +01001712 */
Serhiy Storchaka0767ad42017-03-12 09:20:15 +02001713 data = _PyObject_CallMethodId(file, &PyId_read, "i", 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001714 if (data == NULL)
1715 return NULL;
Vinay Sajip5bdae3b2011-07-02 16:42:47 +01001716 if (!PyBytes_Check(data)) {
1717 PyErr_Format(PyExc_TypeError,
Serhiy Storchaka0767ad42017-03-12 09:20:15 +02001718 "file.read() returned not bytes but %.100s",
Vinay Sajip5bdae3b2011-07-02 16:42:47 +01001719 data->ob_type->tp_name);
1720 result = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001721 }
1722 else {
Vinay Sajip5bdae3b2011-07-02 16:42:47 +01001723 rf.depth = 0;
1724 rf.fp = NULL;
Serhiy Storchaka0767ad42017-03-12 09:20:15 +02001725 rf.readable = file;
Vinay Sajipaac0f752011-07-02 18:42:21 +01001726 rf.current_filename = NULL;
Antoine Pitrou1164dfc2013-10-12 22:25:39 +02001727 rf.ptr = rf.end = NULL;
1728 rf.buf = NULL;
Kristján Valur Jónssond7009c62013-03-19 18:02:10 -07001729 if ((rf.refs = PyList_New(0)) != NULL) {
1730 result = read_object(&rf);
1731 Py_DECREF(rf.refs);
Antoine Pitrou1164dfc2013-10-12 22:25:39 +02001732 if (rf.buf != NULL)
1733 PyMem_FREE(rf.buf);
Kristján Valur Jónssond7009c62013-03-19 18:02:10 -07001734 } else
1735 result = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001736 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001737 Py_DECREF(data);
1738 return result;
Guido van Rossum0b0db8e1993-01-21 16:07:51 +00001739}
1740
Serhiy Storchaka0767ad42017-03-12 09:20:15 +02001741/*[clinic input]
1742marshal.dumps
R. David Murraydd226ea2009-05-13 12:27:21 +00001743
Serhiy Storchaka0767ad42017-03-12 09:20:15 +02001744 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
1750Return the bytes object that would be written to a file by dump(value, file).
1751
1752Raise a ValueError exception if value has (or contains an object that has) an
1753unsupported type.
1754[clinic start generated code]*/
R. David Murraydd226ea2009-05-13 12:27:21 +00001755
Guido van Rossum79f25d91997-04-29 20:08:16 +00001756static PyObject *
Serhiy Storchaka0767ad42017-03-12 09:20:15 +02001757marshal_dumps_impl(PyObject *module, PyObject *value, int version)
1758/*[clinic end generated code: output=9c200f98d7256cad input=a2139ea8608e9b27]*/
Guido van Rossum0b0db8e1993-01-21 16:07:51 +00001759{
Serhiy Storchaka0767ad42017-03-12 09:20:15 +02001760 return PyMarshal_WriteObjectToString(value, version);
Guido van Rossum0b0db8e1993-01-21 16:07:51 +00001761}
1762
Serhiy Storchaka0767ad42017-03-12 09:20:15 +02001763/*[clinic input]
1764marshal.loads
R. David Murraydd226ea2009-05-13 12:27:21 +00001765
Serhiy Storchaka0767ad42017-03-12 09:20:15 +02001766 bytes: Py_buffer
1767 /
1768
1769Convert the bytes-like object to a value.
1770
1771If no valid value is found, raise EOFError, ValueError or TypeError. Extra
1772bytes in the input are ignored.
1773[clinic start generated code]*/
R. David Murraydd226ea2009-05-13 12:27:21 +00001774
Guido van Rossum79f25d91997-04-29 20:08:16 +00001775static PyObject *
Serhiy Storchaka0767ad42017-03-12 09:20:15 +02001776marshal_loads_impl(PyObject *module, Py_buffer *bytes)
1777/*[clinic end generated code: output=9fc65985c93d1bb1 input=6f426518459c8495]*/
Guido van Rossum0b0db8e1993-01-21 16:07:51 +00001778{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001779 RFILE rf;
Serhiy Storchaka0767ad42017-03-12 09:20:15 +02001780 char *s = bytes->buf;
1781 Py_ssize_t n = bytes->len;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001782 PyObject* result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001783 rf.fp = NULL;
Vinay Sajip5bdae3b2011-07-02 16:42:47 +01001784 rf.readable = NULL;
Benjamin Peterson43b06862011-05-27 09:08:01 -05001785 rf.current_filename = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001786 rf.ptr = s;
1787 rf.end = s + n;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001788 rf.depth = 0;
Kristján Valur Jónssond7009c62013-03-19 18:02:10 -07001789 if ((rf.refs = PyList_New(0)) == NULL)
1790 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001791 result = read_object(&rf);
Kristján Valur Jónssond7009c62013-03-19 18:02:10 -07001792 Py_DECREF(rf.refs);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001793 return result;
Guido van Rossumdce2e3d1991-06-04 19:42:30 +00001794}
1795
Guido van Rossum79f25d91997-04-29 20:08:16 +00001796static PyMethodDef marshal_methods[] = {
Serhiy Storchaka0767ad42017-03-12 09:20:15 +02001797 MARSHAL_DUMP_METHODDEF
1798 MARSHAL_LOAD_METHODDEF
1799 MARSHAL_DUMPS_METHODDEF
1800 MARSHAL_LOADS_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001801 {NULL, NULL} /* sentinel */
Guido van Rossumdce2e3d1991-06-04 19:42:30 +00001802};
1803
R. David Murraydd226ea2009-05-13 12:27:21 +00001804
1805PyDoc_STRVAR(module_doc,
1806"This module contains functions that can read and write Python values in\n\
1807a binary format. The format is specific to Python, but independent of\n\
1808machine architecture issues.\n\
1809\n\
1810Not all Python object types are supported; in general, only objects\n\
1811whose value is independent from a particular invocation of Python can be\n\
1812written and read by this module. The following types are supported:\n\
1813None, integers, floating point numbers, strings, bytes, bytearrays,\n\
1814tuples, lists, sets, dictionaries, and code objects, where it\n\
1815should be understood that tuples, lists and dictionaries are only\n\
1816supported as long as the values contained therein are themselves\n\
1817supported; and recursive lists and dictionaries should not be written\n\
1818(they will cause infinite loops).\n\
1819\n\
1820Variables:\n\
1821\n\
1822version -- 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ónssond7009c62013-03-19 18:02:10 -07001825 Version 3 shares common object references (New in version 3.4).\n\
R. David Murraydd226ea2009-05-13 12:27:21 +00001826\n\
1827Functions:\n\
1828\n\
1829dump() -- write value to a file\n\
1830load() -- read value from a file\n\
Serhiy Storchakac611a5b2017-03-12 08:53:22 +02001831dumps() -- marshal value as a bytes object\n\
1832loads() -- read value from a bytes-like object");
R. David Murraydd226ea2009-05-13 12:27:21 +00001833
1834
1835
Brett Cannon429ef652008-06-27 00:35:35 +00001836static struct PyModuleDef marshalmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001837 PyModuleDef_HEAD_INIT,
1838 "marshal",
1839 module_doc,
1840 0,
1841 marshal_methods,
1842 NULL,
1843 NULL,
1844 NULL,
1845 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00001846};
1847
Jason Tishler6bc06ec2003-09-04 11:59:50 +00001848PyMODINIT_FUNC
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001849PyMarshal_Init(void)
Guido van Rossumdce2e3d1991-06-04 19:42:30 +00001850{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001851 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 Rossumdce2e3d1991-06-04 19:42:30 +00001856}