blob: 6d52a840ce354bc923ec0e81bdfd24d2619da40b [file] [log] [blame]
Guido van Rossumdce2e3d1991-06-04 19:42:30 +00001
2/* Write Python objects to files and read them back.
3 This is intended for writing and reading compiled Python code only;
4 a true persistent storage facility would be much harder, since
5 it would have to take circular links and sharing into account. */
6
Thomas Wouters695934a2006-03-01 23:49:13 +00007#define PY_SSIZE_T_CLEAN
8
Guido van Rossum79f25d91997-04-29 20:08:16 +00009#include "Python.h"
Guido van Rossumdce2e3d1991-06-04 19:42:30 +000010#include "longintrepr.h"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000011#include "code.h"
Guido van Rossumdce2e3d1991-06-04 19:42:30 +000012#include "marshal.h"
13
Mark Dickinsonbd792642009-03-18 20:06:12 +000014#define ABS(x) ((x) < 0 ? -(x) : (x))
15
Fred Drake6da0b912000-06-28 18:47:56 +000016/* High water mark to determine when the marshalled object is dangerously deep
17 * and risks coring the interpreter. When the object stack gets this deep,
18 * raise an exception instead of continuing.
Guido van Rossum63175a12007-08-29 20:39:13 +000019 * On Windows debug builds, reduce this value.
Fred Drake6da0b912000-06-28 18:47:56 +000020 */
Guido van Rossum63175a12007-08-29 20:39:13 +000021#if defined(MS_WINDOWS) && defined(_DEBUG)
22#define MAX_MARSHAL_STACK_DEPTH 1500
23#else
Guido van Rossumd59da4b2007-05-22 18:11:13 +000024#define MAX_MARSHAL_STACK_DEPTH 2000
Guido van Rossum63175a12007-08-29 20:39:13 +000025#endif
Fred Drake6da0b912000-06-28 18:47:56 +000026
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000027#define TYPE_NULL '0'
28#define TYPE_NONE 'N'
29#define TYPE_FALSE 'F'
30#define TYPE_TRUE 'T'
31#define TYPE_STOPITER 'S'
32#define TYPE_ELLIPSIS '.'
33#define TYPE_INT 'i'
Martin v. Löwis7e395722012-07-28 19:44:05 +020034/* TYPE_INT64 is deprecated. It is not
35 generated anymore, and support for reading it
36 will be removed in Python 3.4. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000037#define TYPE_INT64 'I'
38#define TYPE_FLOAT 'f'
39#define TYPE_BINARY_FLOAT 'g'
40#define TYPE_COMPLEX 'x'
41#define TYPE_BINARY_COMPLEX 'y'
42#define TYPE_LONG 'l'
43#define TYPE_STRING 's'
44#define TYPE_TUPLE '('
45#define TYPE_LIST '['
46#define TYPE_DICT '{'
47#define TYPE_CODE 'c'
48#define TYPE_UNICODE 'u'
49#define TYPE_UNKNOWN '?'
50#define TYPE_SET '<'
51#define TYPE_FROZENSET '>'
Guido van Rossumdce2e3d1991-06-04 19:42:30 +000052
Eric Smithb1a03cf2009-04-21 11:57:38 +000053#define WFERR_OK 0
54#define WFERR_UNMARSHALLABLE 1
55#define WFERR_NESTEDTOODEEP 2
56#define WFERR_NOMEMORY 3
57
Guido van Rossum0b0db8e1993-01-21 16:07:51 +000058typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000059 FILE *fp;
60 int error; /* see WFERR_* values */
61 int depth;
62 /* If fp == NULL, the following are valid: */
Éric Araujo6c0ba442011-07-26 17:23:57 +020063 PyObject *readable; /* Stream-like object being read from */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000064 PyObject *str;
Benjamin Peterson43b06862011-05-27 09:08:01 -050065 PyObject *current_filename;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000066 char *ptr;
67 char *end;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000068 int version;
Guido van Rossum0b0db8e1993-01-21 16:07:51 +000069} WFILE;
Guido van Rossumdce2e3d1991-06-04 19:42:30 +000070
Guido van Rossum0b0db8e1993-01-21 16:07:51 +000071#define w_byte(c, p) if (((p)->fp)) putc((c), (p)->fp); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000072 else if ((p)->ptr != (p)->end) *(p)->ptr++ = (c); \
73 else w_more(c, p)
Guido van Rossum0b0db8e1993-01-21 16:07:51 +000074
75static void
Fredrik Lundh11534382000-07-23 18:24:06 +000076w_more(int c, WFILE *p)
Guido van Rossum0b0db8e1993-01-21 16:07:51 +000077{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000078 Py_ssize_t size, newsize;
79 if (p->str == NULL)
80 return; /* An error already occurred */
81 size = PyBytes_Size(p->str);
82 newsize = size + size + 1024;
83 if (newsize > 32*1024*1024) {
84 newsize = size + (size >> 3); /* 12.5% overallocation */
85 }
86 if (_PyBytes_Resize(&p->str, newsize) != 0) {
87 p->ptr = p->end = NULL;
88 }
89 else {
90 p->ptr = PyBytes_AS_STRING((PyBytesObject *)p->str) + size;
91 p->end =
92 PyBytes_AS_STRING((PyBytesObject *)p->str) + newsize;
93 *p->ptr++ = Py_SAFE_DOWNCAST(c, int, char);
94 }
Guido van Rossum0b0db8e1993-01-21 16:07:51 +000095}
96
97static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000098w_string(char *s, int n, WFILE *p)
Guido van Rossum0b0db8e1993-01-21 16:07:51 +000099{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000100 if (p->fp != NULL) {
101 fwrite(s, 1, n, p->fp);
102 }
103 else {
104 while (--n >= 0) {
105 w_byte(*s, p);
106 s++;
107 }
108 }
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000109}
110
111static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000112w_short(int x, WFILE *p)
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000113{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000114 w_byte((char)( x & 0xff), p);
115 w_byte((char)((x>> 8) & 0xff), p);
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000116}
117
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000118static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000119w_long(long x, WFILE *p)
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000120{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000121 w_byte((char)( x & 0xff), p);
122 w_byte((char)((x>> 8) & 0xff), p);
123 w_byte((char)((x>>16) & 0xff), p);
124 w_byte((char)((x>>24) & 0xff), p);
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000125}
126
Mark Dickinsonbd792642009-03-18 20:06:12 +0000127/* We assume that Python longs are stored internally in base some power of
128 2**15; for the sake of portability we'll always read and write them in base
129 exactly 2**15. */
130
131#define PyLong_MARSHAL_SHIFT 15
132#define PyLong_MARSHAL_BASE ((short)1 << PyLong_MARSHAL_SHIFT)
133#define PyLong_MARSHAL_MASK (PyLong_MARSHAL_BASE - 1)
134#if PyLong_SHIFT % PyLong_MARSHAL_SHIFT != 0
135#error "PyLong_SHIFT must be a multiple of PyLong_MARSHAL_SHIFT"
136#endif
137#define PyLong_MARSHAL_RATIO (PyLong_SHIFT / PyLong_MARSHAL_SHIFT)
138
139static void
140w_PyLong(const PyLongObject *ob, WFILE *p)
141{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000142 Py_ssize_t i, j, n, l;
143 digit d;
Mark Dickinsonbd792642009-03-18 20:06:12 +0000144
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000145 w_byte(TYPE_LONG, p);
146 if (Py_SIZE(ob) == 0) {
147 w_long((long)0, p);
148 return;
149 }
Mark Dickinsonbd792642009-03-18 20:06:12 +0000150
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000151 /* set l to number of base PyLong_MARSHAL_BASE digits */
152 n = ABS(Py_SIZE(ob));
153 l = (n-1) * PyLong_MARSHAL_RATIO;
154 d = ob->ob_digit[n-1];
155 assert(d != 0); /* a PyLong is always normalized */
156 do {
157 d >>= PyLong_MARSHAL_SHIFT;
158 l++;
159 } while (d != 0);
160 w_long((long)(Py_SIZE(ob) > 0 ? l : -l), p);
Mark Dickinsonbd792642009-03-18 20:06:12 +0000161
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000162 for (i=0; i < n-1; i++) {
163 d = ob->ob_digit[i];
164 for (j=0; j < PyLong_MARSHAL_RATIO; j++) {
165 w_short(d & PyLong_MARSHAL_MASK, p);
166 d >>= PyLong_MARSHAL_SHIFT;
167 }
168 assert (d == 0);
169 }
170 d = ob->ob_digit[n-1];
171 do {
172 w_short(d & PyLong_MARSHAL_MASK, p);
173 d >>= PyLong_MARSHAL_SHIFT;
174 } while (d != 0);
Mark Dickinsonbd792642009-03-18 20:06:12 +0000175}
176
Guido van Rossumb0c168c1996-12-05 23:15:02 +0000177static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000178w_object(PyObject *v, WFILE *p)
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000179{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000180 Py_ssize_t i, n;
Fred Drake6da0b912000-06-28 18:47:56 +0000181
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000182 p->depth++;
Tim Petersd9b9ac82001-01-28 00:27:39 +0000183
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000184 if (p->depth > MAX_MARSHAL_STACK_DEPTH) {
185 p->error = WFERR_NESTEDTOODEEP;
186 }
187 else if (v == NULL) {
188 w_byte(TYPE_NULL, p);
189 }
190 else if (v == Py_None) {
191 w_byte(TYPE_NONE, p);
192 }
193 else if (v == PyExc_StopIteration) {
194 w_byte(TYPE_STOPITER, p);
195 }
196 else if (v == Py_Ellipsis) {
197 w_byte(TYPE_ELLIPSIS, p);
198 }
199 else if (v == Py_False) {
200 w_byte(TYPE_FALSE, p);
201 }
202 else if (v == Py_True) {
203 w_byte(TYPE_TRUE, p);
204 }
205 else if (PyLong_CheckExact(v)) {
206 long x = PyLong_AsLong(v);
207 if ((x == -1) && PyErr_Occurred()) {
208 PyLongObject *ob = (PyLongObject *)v;
209 PyErr_Clear();
210 w_PyLong(ob, p);
211 }
212 else {
Guido van Rossumc1547d91996-12-10 15:39:04 +0000213#if SIZEOF_LONG > 4
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000214 long y = Py_ARITHMETIC_RIGHT_SHIFT(long, x, 31);
215 if (y && y != -1) {
Martin v. Löwis7e395722012-07-28 19:44:05 +0200216 /* Too large for TYPE_INT */
217 w_PyLong((PyLongObject*)v, p);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000218 }
219 else
Guido van Rossumc1547d91996-12-10 15:39:04 +0000220#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000221 {
222 w_byte(TYPE_INT, p);
223 w_long(x, p);
224 }
225 }
226 }
227 else if (PyFloat_CheckExact(v)) {
228 if (p->version > 1) {
229 unsigned char buf[8];
230 if (_PyFloat_Pack8(PyFloat_AsDouble(v),
231 buf, 1) < 0) {
232 p->error = WFERR_UNMARSHALLABLE;
233 return;
234 }
235 w_byte(TYPE_BINARY_FLOAT, p);
236 w_string((char*)buf, 8, p);
237 }
238 else {
239 char *buf = PyOS_double_to_string(PyFloat_AS_DOUBLE(v),
240 'g', 17, 0, NULL);
241 if (!buf) {
242 p->error = WFERR_NOMEMORY;
243 return;
244 }
245 n = strlen(buf);
246 w_byte(TYPE_FLOAT, p);
247 w_byte((int)n, p);
248 w_string(buf, (int)n, p);
249 PyMem_Free(buf);
250 }
251 }
252 else if (PyComplex_CheckExact(v)) {
253 if (p->version > 1) {
254 unsigned char buf[8];
255 if (_PyFloat_Pack8(PyComplex_RealAsDouble(v),
256 buf, 1) < 0) {
257 p->error = WFERR_UNMARSHALLABLE;
258 return;
259 }
260 w_byte(TYPE_BINARY_COMPLEX, p);
261 w_string((char*)buf, 8, p);
262 if (_PyFloat_Pack8(PyComplex_ImagAsDouble(v),
263 buf, 1) < 0) {
264 p->error = WFERR_UNMARSHALLABLE;
265 return;
266 }
267 w_string((char*)buf, 8, p);
268 }
269 else {
270 char *buf;
271 w_byte(TYPE_COMPLEX, p);
272 buf = PyOS_double_to_string(PyComplex_RealAsDouble(v),
273 'g', 17, 0, NULL);
274 if (!buf) {
275 p->error = WFERR_NOMEMORY;
276 return;
277 }
278 n = strlen(buf);
279 w_byte((int)n, p);
280 w_string(buf, (int)n, p);
281 PyMem_Free(buf);
282 buf = PyOS_double_to_string(PyComplex_ImagAsDouble(v),
283 'g', 17, 0, NULL);
284 if (!buf) {
285 p->error = WFERR_NOMEMORY;
286 return;
287 }
288 n = strlen(buf);
289 w_byte((int)n, p);
290 w_string(buf, (int)n, p);
291 PyMem_Free(buf);
292 }
293 }
294 else if (PyBytes_CheckExact(v)) {
295 w_byte(TYPE_STRING, p);
296 n = PyBytes_GET_SIZE(v);
297 if (n > INT_MAX) {
298 /* huge strings are not supported */
299 p->depth--;
300 p->error = WFERR_UNMARSHALLABLE;
301 return;
302 }
303 w_long((long)n, p);
304 w_string(PyBytes_AS_STRING(v), (int)n, p);
305 }
306 else if (PyUnicode_CheckExact(v)) {
307 PyObject *utf8;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200308 utf8 = PyUnicode_AsEncodedString(v, "utf8", "surrogatepass");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000309 if (utf8 == NULL) {
310 p->depth--;
311 p->error = WFERR_UNMARSHALLABLE;
312 return;
313 }
314 w_byte(TYPE_UNICODE, p);
315 n = PyBytes_GET_SIZE(utf8);
316 if (n > INT_MAX) {
317 p->depth--;
318 p->error = WFERR_UNMARSHALLABLE;
319 return;
320 }
321 w_long((long)n, p);
322 w_string(PyBytes_AS_STRING(utf8), (int)n, p);
323 Py_DECREF(utf8);
324 }
325 else if (PyTuple_CheckExact(v)) {
326 w_byte(TYPE_TUPLE, p);
327 n = PyTuple_Size(v);
328 w_long((long)n, p);
329 for (i = 0; i < n; i++) {
330 w_object(PyTuple_GET_ITEM(v, i), p);
331 }
332 }
333 else if (PyList_CheckExact(v)) {
334 w_byte(TYPE_LIST, p);
335 n = PyList_GET_SIZE(v);
336 w_long((long)n, p);
337 for (i = 0; i < n; i++) {
338 w_object(PyList_GET_ITEM(v, i), p);
339 }
340 }
341 else if (PyDict_CheckExact(v)) {
342 Py_ssize_t pos;
343 PyObject *key, *value;
344 w_byte(TYPE_DICT, p);
345 /* This one is NULL object terminated! */
346 pos = 0;
347 while (PyDict_Next(v, &pos, &key, &value)) {
348 w_object(key, p);
349 w_object(value, p);
350 }
351 w_object((PyObject *)NULL, p);
352 }
353 else if (PyAnySet_CheckExact(v)) {
354 PyObject *value, *it;
Raymond Hettingera422c342005-01-11 03:03:27 +0000355
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000356 if (PyObject_TypeCheck(v, &PySet_Type))
357 w_byte(TYPE_SET, p);
358 else
359 w_byte(TYPE_FROZENSET, p);
360 n = PyObject_Size(v);
361 if (n == -1) {
362 p->depth--;
363 p->error = WFERR_UNMARSHALLABLE;
364 return;
365 }
366 w_long((long)n, p);
367 it = PyObject_GetIter(v);
368 if (it == NULL) {
369 p->depth--;
370 p->error = WFERR_UNMARSHALLABLE;
371 return;
372 }
373 while ((value = PyIter_Next(it)) != NULL) {
374 w_object(value, p);
375 Py_DECREF(value);
376 }
377 Py_DECREF(it);
378 if (PyErr_Occurred()) {
379 p->depth--;
380 p->error = WFERR_UNMARSHALLABLE;
381 return;
382 }
383 }
384 else if (PyCode_Check(v)) {
385 PyCodeObject *co = (PyCodeObject *)v;
386 w_byte(TYPE_CODE, p);
387 w_long(co->co_argcount, p);
388 w_long(co->co_kwonlyargcount, p);
389 w_long(co->co_nlocals, p);
390 w_long(co->co_stacksize, p);
391 w_long(co->co_flags, p);
392 w_object(co->co_code, p);
393 w_object(co->co_consts, p);
394 w_object(co->co_names, p);
395 w_object(co->co_varnames, p);
396 w_object(co->co_freevars, p);
397 w_object(co->co_cellvars, p);
398 w_object(co->co_filename, p);
399 w_object(co->co_name, p);
400 w_long(co->co_firstlineno, p);
401 w_object(co->co_lnotab, p);
402 }
403 else if (PyObject_CheckBuffer(v)) {
404 /* Write unknown buffer-style objects as a string */
405 char *s;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000406 Py_buffer view;
Antoine Pitrou679e9d32012-03-02 18:12:43 +0100407 if (PyObject_GetBuffer(v, &view, PyBUF_SIMPLE) != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000408 w_byte(TYPE_UNKNOWN, p);
Antoine Pitrou679e9d32012-03-02 18:12:43 +0100409 p->depth--;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000410 p->error = WFERR_UNMARSHALLABLE;
Antoine Pitrou679e9d32012-03-02 18:12:43 +0100411 return;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000412 }
413 w_byte(TYPE_STRING, p);
414 n = view.len;
415 s = view.buf;
416 if (n > INT_MAX) {
417 p->depth--;
418 p->error = WFERR_UNMARSHALLABLE;
419 return;
420 }
421 w_long((long)n, p);
422 w_string(s, (int)n, p);
Antoine Pitrou679e9d32012-03-02 18:12:43 +0100423 PyBuffer_Release(&view);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000424 }
425 else {
426 w_byte(TYPE_UNKNOWN, p);
427 p->error = WFERR_UNMARSHALLABLE;
428 }
429 p->depth--;
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000430}
431
Martin v. Löwisef82d2f2004-06-27 16:51:46 +0000432/* version currently has no effect for writing longs. */
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000433void
Martin v. Löwisef82d2f2004-06-27 16:51:46 +0000434PyMarshal_WriteLongToFile(long x, FILE *fp, int version)
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000435{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000436 WFILE wf;
437 wf.fp = fp;
438 wf.error = WFERR_OK;
439 wf.depth = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000440 wf.version = version;
441 w_long(x, &wf);
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000442}
443
444void
Martin v. Löwisef82d2f2004-06-27 16:51:46 +0000445PyMarshal_WriteObjectToFile(PyObject *x, FILE *fp, int version)
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000446{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000447 WFILE wf;
448 wf.fp = fp;
449 wf.error = WFERR_OK;
450 wf.depth = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000451 wf.version = version;
452 w_object(x, &wf);
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000453}
454
455typedef WFILE RFILE; /* Same struct with different invariants */
456
Guido van Rossumd59da4b2007-05-22 18:11:13 +0000457#define rs_byte(p) (((p)->ptr < (p)->end) ? (unsigned char)*(p)->ptr++ : EOF)
Guido van Rossum8d617a61995-03-09 12:12:11 +0000458
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000459static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000460r_string(char *s, int n, RFILE *p)
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000461{
Éric Araujo6c0ba442011-07-26 17:23:57 +0200462 char *ptr;
Vinay Sajip5bdae3b2011-07-02 16:42:47 +0100463 int read, left;
464
465 if (!p->readable) {
466 if (p->fp != NULL)
467 /* The result fits into int because it must be <=n. */
468 read = (int) fread(s, 1, n, p->fp);
469 else {
470 left = (int)(p->end - p->ptr);
471 read = (left < n) ? left : n;
472 memcpy(s, p->ptr, read);
473 p->ptr += read;
474 }
475 }
476 else {
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200477 _Py_IDENTIFIER(read);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200478
479 PyObject *data = _PyObject_CallMethodId(p->readable, &PyId_read, "i", n);
Vinay Sajip5bdae3b2011-07-02 16:42:47 +0100480 read = 0;
481 if (data != NULL) {
482 if (!PyBytes_Check(data)) {
483 PyErr_Format(PyExc_TypeError,
484 "f.read() returned not bytes but %.100s",
485 data->ob_type->tp_name);
486 }
487 else {
488 read = PyBytes_GET_SIZE(data);
489 if (read > 0) {
490 ptr = PyBytes_AS_STRING(data);
491 memcpy(s, ptr, read);
492 }
493 }
494 Py_DECREF(data);
495 }
496 }
497 if (!PyErr_Occurred() && (read < n)) {
498 PyErr_SetString(PyExc_EOFError, "EOF read where not expected");
499 }
500 return read;
501}
502
503
504static int
505r_byte(RFILE *p)
506{
507 int c = EOF;
508 unsigned char ch;
509 int n;
510
511 if (!p->readable)
512 c = p->fp ? getc(p->fp) : rs_byte(p);
513 else {
514 n = r_string((char *) &ch, 1, p);
515 if (n > 0)
516 c = ch;
517 }
518 return c;
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000519}
520
521static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000522r_short(RFILE *p)
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000523{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000524 register short x;
Vinay Sajip5bdae3b2011-07-02 16:42:47 +0100525 unsigned char buffer[2];
526
527 r_string((char *) buffer, 2, p);
528 x = buffer[0];
529 x |= buffer[1] << 8;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000530 /* Sign-extension, in case short greater than 16 bits */
531 x |= -(x & 0x8000);
532 return x;
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000533}
534
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000535static long
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000536r_long(RFILE *p)
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000537{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000538 register long x;
Vinay Sajip5bdae3b2011-07-02 16:42:47 +0100539 unsigned char buffer[4];
540
541 r_string((char *) buffer, 4, p);
542 x = buffer[0];
543 x |= (long)buffer[1] << 8;
544 x |= (long)buffer[2] << 16;
545 x |= (long)buffer[3] << 24;
Guido van Rossumc1547d91996-12-10 15:39:04 +0000546#if SIZEOF_LONG > 4
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000547 /* Sign extension for 64-bit machines */
548 x |= -(x & 0x80000000L);
Guido van Rossumc1547d91996-12-10 15:39:04 +0000549#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000550 return x;
Guido van Rossumb0c168c1996-12-05 23:15:02 +0000551}
552
Tim Peters82112372001-08-29 02:28:42 +0000553/* r_long64 deals with the TYPE_INT64 code. On a machine with
554 sizeof(long) > 4, it returns a Python int object, else a Python long
555 object. Note that w_long64 writes out TYPE_INT if 32 bits is enough,
556 so there's no inefficiency here in returning a PyLong on 32-bit boxes
557 for everything written via TYPE_INT64 (i.e., if an int is written via
558 TYPE_INT64, it *needs* more than 32 bits).
559*/
560static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000561r_long64(RFILE *p)
Guido van Rossumb0c168c1996-12-05 23:15:02 +0000562{
Éric Araujo6c0ba442011-07-26 17:23:57 +0200563 PyObject *result = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000564 long lo4 = r_long(p);
565 long hi4 = r_long(p);
Vinay Sajip5bdae3b2011-07-02 16:42:47 +0100566
567 if (!PyErr_Occurred()) {
Guido van Rossumc1547d91996-12-10 15:39:04 +0000568#if SIZEOF_LONG > 4
Vinay Sajip5bdae3b2011-07-02 16:42:47 +0100569 long x = (hi4 << 32) | (lo4 & 0xFFFFFFFFL);
570 result = PyLong_FromLong(x);
Guido van Rossumc1547d91996-12-10 15:39:04 +0000571#else
Vinay Sajip5bdae3b2011-07-02 16:42:47 +0100572 unsigned char buf[8];
573 int one = 1;
574 int is_little_endian = (int)*(char*)&one;
575 if (is_little_endian) {
576 memcpy(buf, &lo4, 4);
577 memcpy(buf+4, &hi4, 4);
578 }
579 else {
580 memcpy(buf, &hi4, 4);
581 memcpy(buf+4, &lo4, 4);
582 }
583 result = _PyLong_FromByteArray(buf, 8, is_little_endian, 1);
Guido van Rossumc1547d91996-12-10 15:39:04 +0000584#endif
Vinay Sajip5bdae3b2011-07-02 16:42:47 +0100585 }
586 return result;
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000587}
588
Guido van Rossum79f25d91997-04-29 20:08:16 +0000589static PyObject *
Mark Dickinsonbd792642009-03-18 20:06:12 +0000590r_PyLong(RFILE *p)
591{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000592 PyLongObject *ob;
593 int size, i, j, md, shorts_in_top_digit;
594 long n;
595 digit d;
Mark Dickinsonbd792642009-03-18 20:06:12 +0000596
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000597 n = r_long(p);
Vinay Sajip5bdae3b2011-07-02 16:42:47 +0100598 if (PyErr_Occurred())
599 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000600 if (n == 0)
601 return (PyObject *)_PyLong_New(0);
602 if (n < -INT_MAX || n > INT_MAX) {
603 PyErr_SetString(PyExc_ValueError,
604 "bad marshal data (long size out of range)");
605 return NULL;
606 }
Mark Dickinsonbd792642009-03-18 20:06:12 +0000607
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000608 size = 1 + (ABS(n) - 1) / PyLong_MARSHAL_RATIO;
609 shorts_in_top_digit = 1 + (ABS(n) - 1) % PyLong_MARSHAL_RATIO;
610 ob = _PyLong_New(size);
611 if (ob == NULL)
612 return NULL;
613 Py_SIZE(ob) = n > 0 ? size : -size;
Mark Dickinsonbd792642009-03-18 20:06:12 +0000614
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000615 for (i = 0; i < size-1; i++) {
616 d = 0;
617 for (j=0; j < PyLong_MARSHAL_RATIO; j++) {
618 md = r_short(p);
Vinay Sajip5bdae3b2011-07-02 16:42:47 +0100619 if (PyErr_Occurred())
620 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000621 if (md < 0 || md > PyLong_MARSHAL_BASE)
622 goto bad_digit;
623 d += (digit)md << j*PyLong_MARSHAL_SHIFT;
624 }
625 ob->ob_digit[i] = d;
626 }
627 d = 0;
628 for (j=0; j < shorts_in_top_digit; j++) {
629 md = r_short(p);
Vinay Sajip5bdae3b2011-07-02 16:42:47 +0100630 if (PyErr_Occurred())
631 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000632 if (md < 0 || md > PyLong_MARSHAL_BASE)
633 goto bad_digit;
634 /* topmost marshal digit should be nonzero */
635 if (md == 0 && j == shorts_in_top_digit - 1) {
636 Py_DECREF(ob);
637 PyErr_SetString(PyExc_ValueError,
638 "bad marshal data (unnormalized long data)");
639 return NULL;
640 }
641 d += (digit)md << j*PyLong_MARSHAL_SHIFT;
642 }
Vinay Sajip5bdae3b2011-07-02 16:42:47 +0100643 if (PyErr_Occurred()) {
644 Py_DECREF(ob);
645 return NULL;
646 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000647 /* top digit should be nonzero, else the resulting PyLong won't be
648 normalized */
649 ob->ob_digit[size-1] = d;
650 return (PyObject *)ob;
Mark Dickinsonbd792642009-03-18 20:06:12 +0000651 bad_digit:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000652 Py_DECREF(ob);
653 PyErr_SetString(PyExc_ValueError,
654 "bad marshal data (digit out of range in long)");
655 return NULL;
Mark Dickinsonbd792642009-03-18 20:06:12 +0000656}
657
658
659static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000660r_object(RFILE *p)
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000661{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000662 /* NULL is a valid return value, it does not necessarily means that
663 an exception is set. */
664 PyObject *v, *v2;
665 long i, n;
666 int type = r_byte(p);
667 PyObject *retval;
Guido van Rossumd59da4b2007-05-22 18:11:13 +0000668
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000669 p->depth++;
Guido van Rossumd59da4b2007-05-22 18:11:13 +0000670
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000671 if (p->depth > MAX_MARSHAL_STACK_DEPTH) {
672 p->depth--;
673 PyErr_SetString(PyExc_ValueError, "recursion limit exceeded");
674 return NULL;
675 }
Tim Petersd9b9ac82001-01-28 00:27:39 +0000676
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000677 switch (type) {
Tim Petersd9b9ac82001-01-28 00:27:39 +0000678
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000679 case EOF:
680 PyErr_SetString(PyExc_EOFError,
681 "EOF read where object expected");
682 retval = NULL;
683 break;
Tim Petersd9b9ac82001-01-28 00:27:39 +0000684
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000685 case TYPE_NULL:
686 retval = NULL;
687 break;
Tim Petersd9b9ac82001-01-28 00:27:39 +0000688
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000689 case TYPE_NONE:
690 Py_INCREF(Py_None);
691 retval = Py_None;
692 break;
Tim Petersd9b9ac82001-01-28 00:27:39 +0000693
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000694 case TYPE_STOPITER:
695 Py_INCREF(PyExc_StopIteration);
696 retval = PyExc_StopIteration;
697 break;
Tim Peters5ca576e2001-06-18 22:08:13 +0000698
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000699 case TYPE_ELLIPSIS:
700 Py_INCREF(Py_Ellipsis);
701 retval = Py_Ellipsis;
702 break;
Tim Petersd9b9ac82001-01-28 00:27:39 +0000703
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000704 case TYPE_FALSE:
705 Py_INCREF(Py_False);
706 retval = Py_False;
707 break;
Guido van Rossum77f6a652002-04-03 22:41:51 +0000708
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000709 case TYPE_TRUE:
710 Py_INCREF(Py_True);
711 retval = Py_True;
712 break;
Guido van Rossum77f6a652002-04-03 22:41:51 +0000713
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000714 case TYPE_INT:
Vinay Sajip5bdae3b2011-07-02 16:42:47 +0100715 n = r_long(p);
716 retval = PyErr_Occurred() ? NULL : PyLong_FromLong(n);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000717 break;
Tim Petersd9b9ac82001-01-28 00:27:39 +0000718
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000719 case TYPE_INT64:
720 retval = r_long64(p);
721 break;
Tim Petersd9b9ac82001-01-28 00:27:39 +0000722
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000723 case TYPE_LONG:
724 retval = r_PyLong(p);
725 break;
Tim Petersd9b9ac82001-01-28 00:27:39 +0000726
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000727 case TYPE_FLOAT:
728 {
729 char buf[256];
730 double dx;
731 retval = NULL;
732 n = r_byte(p);
733 if (n == EOF || r_string(buf, (int)n, p) != n) {
734 PyErr_SetString(PyExc_EOFError,
735 "EOF read where object expected");
736 break;
737 }
738 buf[n] = '\0';
739 dx = PyOS_string_to_double(buf, NULL, NULL);
740 if (dx == -1.0 && PyErr_Occurred())
741 break;
742 retval = PyFloat_FromDouble(dx);
743 break;
744 }
Tim Petersd9b9ac82001-01-28 00:27:39 +0000745
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000746 case TYPE_BINARY_FLOAT:
747 {
748 unsigned char buf[8];
749 double x;
750 if (r_string((char*)buf, 8, p) != 8) {
751 PyErr_SetString(PyExc_EOFError,
752 "EOF read where object expected");
753 retval = NULL;
754 break;
755 }
756 x = _PyFloat_Unpack8(buf, 1);
757 if (x == -1.0 && PyErr_Occurred()) {
758 retval = NULL;
759 break;
760 }
761 retval = PyFloat_FromDouble(x);
762 break;
763 }
Michael W. Hudsondf888462005-06-03 14:41:55 +0000764
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000765 case TYPE_COMPLEX:
766 {
767 char buf[256];
768 Py_complex c;
769 retval = NULL;
770 n = r_byte(p);
771 if (n == EOF || r_string(buf, (int)n, p) != n) {
772 PyErr_SetString(PyExc_EOFError,
773 "EOF read where object expected");
774 break;
775 }
776 buf[n] = '\0';
777 c.real = PyOS_string_to_double(buf, NULL, NULL);
778 if (c.real == -1.0 && PyErr_Occurred())
779 break;
780 n = r_byte(p);
781 if (n == EOF || r_string(buf, (int)n, p) != n) {
782 PyErr_SetString(PyExc_EOFError,
783 "EOF read where object expected");
784 break;
785 }
786 buf[n] = '\0';
787 c.imag = PyOS_string_to_double(buf, NULL, NULL);
788 if (c.imag == -1.0 && PyErr_Occurred())
789 break;
790 retval = PyComplex_FromCComplex(c);
791 break;
792 }
Michael W. Hudsondf888462005-06-03 14:41:55 +0000793
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000794 case TYPE_BINARY_COMPLEX:
795 {
796 unsigned char buf[8];
797 Py_complex c;
798 if (r_string((char*)buf, 8, p) != 8) {
799 PyErr_SetString(PyExc_EOFError,
800 "EOF read where object expected");
801 retval = NULL;
802 break;
803 }
804 c.real = _PyFloat_Unpack8(buf, 1);
805 if (c.real == -1.0 && PyErr_Occurred()) {
806 retval = NULL;
807 break;
808 }
809 if (r_string((char*)buf, 8, p) != 8) {
810 PyErr_SetString(PyExc_EOFError,
811 "EOF read where object expected");
812 retval = NULL;
813 break;
814 }
815 c.imag = _PyFloat_Unpack8(buf, 1);
816 if (c.imag == -1.0 && PyErr_Occurred()) {
817 retval = NULL;
818 break;
819 }
820 retval = PyComplex_FromCComplex(c);
821 break;
822 }
Tim Petersd9b9ac82001-01-28 00:27:39 +0000823
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000824 case TYPE_STRING:
825 n = r_long(p);
Vinay Sajip5bdae3b2011-07-02 16:42:47 +0100826 if (PyErr_Occurred()) {
827 retval = NULL;
828 break;
829 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000830 if (n < 0 || n > INT_MAX) {
831 PyErr_SetString(PyExc_ValueError, "bad marshal data (string size out of range)");
832 retval = NULL;
833 break;
834 }
835 v = PyBytes_FromStringAndSize((char *)NULL, n);
836 if (v == NULL) {
837 retval = NULL;
838 break;
839 }
840 if (r_string(PyBytes_AS_STRING(v), (int)n, p) != n) {
841 Py_DECREF(v);
842 PyErr_SetString(PyExc_EOFError,
843 "EOF read where object expected");
844 retval = NULL;
845 break;
846 }
847 retval = v;
848 break;
Tim Petersd9b9ac82001-01-28 00:27:39 +0000849
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000850 case TYPE_UNICODE:
851 {
852 char *buffer;
Guido van Rossumc279b532000-03-10 23:03:02 +0000853
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000854 n = r_long(p);
Vinay Sajip5bdae3b2011-07-02 16:42:47 +0100855 if (PyErr_Occurred()) {
856 retval = NULL;
857 break;
858 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000859 if (n < 0 || n > INT_MAX) {
860 PyErr_SetString(PyExc_ValueError, "bad marshal data (unicode size out of range)");
861 retval = NULL;
862 break;
863 }
864 buffer = PyMem_NEW(char, n);
865 if (buffer == NULL) {
866 retval = PyErr_NoMemory();
867 break;
868 }
869 if (r_string(buffer, (int)n, p) != n) {
870 PyMem_DEL(buffer);
871 PyErr_SetString(PyExc_EOFError,
872 "EOF read where object expected");
873 retval = NULL;
874 break;
875 }
876 v = PyUnicode_DecodeUTF8(buffer, n, "surrogatepass");
877 PyMem_DEL(buffer);
878 retval = v;
879 break;
880 }
Tim Petersd9b9ac82001-01-28 00:27:39 +0000881
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000882 case TYPE_TUPLE:
883 n = r_long(p);
Vinay Sajip5bdae3b2011-07-02 16:42:47 +0100884 if (PyErr_Occurred()) {
885 retval = NULL;
886 break;
887 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000888 if (n < 0 || n > INT_MAX) {
889 PyErr_SetString(PyExc_ValueError, "bad marshal data (tuple size out of range)");
890 retval = NULL;
891 break;
892 }
893 v = PyTuple_New((int)n);
894 if (v == NULL) {
895 retval = NULL;
896 break;
897 }
898 for (i = 0; i < n; i++) {
899 v2 = r_object(p);
900 if ( v2 == NULL ) {
901 if (!PyErr_Occurred())
902 PyErr_SetString(PyExc_TypeError,
903 "NULL object in marshal data for tuple");
904 Py_DECREF(v);
905 v = NULL;
906 break;
907 }
908 PyTuple_SET_ITEM(v, (int)i, v2);
909 }
910 retval = v;
911 break;
Tim Petersd9b9ac82001-01-28 00:27:39 +0000912
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000913 case TYPE_LIST:
914 n = r_long(p);
Vinay Sajip5bdae3b2011-07-02 16:42:47 +0100915 if (PyErr_Occurred()) {
916 retval = NULL;
917 break;
918 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000919 if (n < 0 || n > INT_MAX) {
920 PyErr_SetString(PyExc_ValueError, "bad marshal data (list size out of range)");
921 retval = NULL;
922 break;
923 }
924 v = PyList_New((int)n);
925 if (v == NULL) {
926 retval = NULL;
927 break;
928 }
929 for (i = 0; i < n; i++) {
930 v2 = r_object(p);
931 if ( v2 == NULL ) {
932 if (!PyErr_Occurred())
933 PyErr_SetString(PyExc_TypeError,
934 "NULL object in marshal data for list");
935 Py_DECREF(v);
936 v = NULL;
937 break;
938 }
939 PyList_SET_ITEM(v, (int)i, v2);
940 }
941 retval = v;
942 break;
Tim Petersd9b9ac82001-01-28 00:27:39 +0000943
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000944 case TYPE_DICT:
945 v = PyDict_New();
946 if (v == NULL) {
947 retval = NULL;
948 break;
949 }
950 for (;;) {
951 PyObject *key, *val;
952 key = r_object(p);
953 if (key == NULL)
954 break;
955 val = r_object(p);
956 if (val != NULL)
957 PyDict_SetItem(v, key, val);
958 Py_DECREF(key);
959 Py_XDECREF(val);
960 }
961 if (PyErr_Occurred()) {
962 Py_DECREF(v);
963 v = NULL;
964 }
965 retval = v;
966 break;
Tim Petersd9b9ac82001-01-28 00:27:39 +0000967
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000968 case TYPE_SET:
969 case TYPE_FROZENSET:
970 n = r_long(p);
Vinay Sajip5bdae3b2011-07-02 16:42:47 +0100971 if (PyErr_Occurred()) {
972 retval = NULL;
973 break;
974 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000975 if (n < 0 || n > INT_MAX) {
976 PyErr_SetString(PyExc_ValueError, "bad marshal data (set size out of range)");
977 retval = NULL;
978 break;
979 }
980 v = (type == TYPE_SET) ? PySet_New(NULL) : PyFrozenSet_New(NULL);
981 if (v == NULL) {
982 retval = NULL;
983 break;
984 }
985 for (i = 0; i < n; i++) {
986 v2 = r_object(p);
987 if ( v2 == NULL ) {
988 if (!PyErr_Occurred())
989 PyErr_SetString(PyExc_TypeError,
990 "NULL object in marshal data for set");
991 Py_DECREF(v);
992 v = NULL;
993 break;
994 }
995 if (PySet_Add(v, v2) == -1) {
996 Py_DECREF(v);
997 Py_DECREF(v2);
998 v = NULL;
999 break;
1000 }
1001 Py_DECREF(v2);
1002 }
1003 retval = v;
1004 break;
Raymond Hettingera422c342005-01-11 03:03:27 +00001005
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001006 case TYPE_CODE:
1007 {
1008 int argcount;
1009 int kwonlyargcount;
1010 int nlocals;
1011 int stacksize;
1012 int flags;
1013 PyObject *code = NULL;
1014 PyObject *consts = NULL;
1015 PyObject *names = NULL;
1016 PyObject *varnames = NULL;
1017 PyObject *freevars = NULL;
1018 PyObject *cellvars = NULL;
1019 PyObject *filename = NULL;
1020 PyObject *name = NULL;
1021 int firstlineno;
1022 PyObject *lnotab = NULL;
Tim Petersd9b9ac82001-01-28 00:27:39 +00001023
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001024 v = NULL;
Michael W. Hudsondf888462005-06-03 14:41:55 +00001025
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001026 /* XXX ignore long->int overflows for now */
1027 argcount = (int)r_long(p);
Vinay Sajip5bdae3b2011-07-02 16:42:47 +01001028 if (PyErr_Occurred())
1029 goto code_error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001030 kwonlyargcount = (int)r_long(p);
Vinay Sajip5bdae3b2011-07-02 16:42:47 +01001031 if (PyErr_Occurred())
1032 goto code_error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001033 nlocals = (int)r_long(p);
Vinay Sajip5bdae3b2011-07-02 16:42:47 +01001034 if (PyErr_Occurred())
1035 goto code_error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001036 stacksize = (int)r_long(p);
Vinay Sajip5bdae3b2011-07-02 16:42:47 +01001037 if (PyErr_Occurred())
1038 goto code_error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001039 flags = (int)r_long(p);
Vinay Sajip5bdae3b2011-07-02 16:42:47 +01001040 if (PyErr_Occurred())
1041 goto code_error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001042 code = r_object(p);
1043 if (code == NULL)
1044 goto code_error;
1045 consts = r_object(p);
1046 if (consts == NULL)
1047 goto code_error;
1048 names = r_object(p);
1049 if (names == NULL)
1050 goto code_error;
1051 varnames = r_object(p);
1052 if (varnames == NULL)
1053 goto code_error;
1054 freevars = r_object(p);
1055 if (freevars == NULL)
1056 goto code_error;
1057 cellvars = r_object(p);
1058 if (cellvars == NULL)
1059 goto code_error;
1060 filename = r_object(p);
1061 if (filename == NULL)
1062 goto code_error;
Benjamin Peterson43b06862011-05-27 09:08:01 -05001063 if (PyUnicode_CheckExact(filename)) {
1064 if (p->current_filename != NULL) {
1065 if (!PyUnicode_Compare(filename, p->current_filename)) {
1066 Py_DECREF(filename);
1067 Py_INCREF(p->current_filename);
1068 filename = p->current_filename;
1069 }
1070 }
1071 else {
1072 p->current_filename = filename;
1073 }
1074 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001075 name = r_object(p);
1076 if (name == NULL)
1077 goto code_error;
1078 firstlineno = (int)r_long(p);
1079 lnotab = r_object(p);
1080 if (lnotab == NULL)
1081 goto code_error;
Michael W. Hudsondf888462005-06-03 14:41:55 +00001082
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001083 v = (PyObject *) PyCode_New(
1084 argcount, kwonlyargcount,
1085 nlocals, stacksize, flags,
1086 code, consts, names, varnames,
1087 freevars, cellvars, filename, name,
1088 firstlineno, lnotab);
Tim Petersd9b9ac82001-01-28 00:27:39 +00001089
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001090 code_error:
1091 Py_XDECREF(code);
1092 Py_XDECREF(consts);
1093 Py_XDECREF(names);
1094 Py_XDECREF(varnames);
1095 Py_XDECREF(freevars);
1096 Py_XDECREF(cellvars);
1097 Py_XDECREF(filename);
1098 Py_XDECREF(name);
1099 Py_XDECREF(lnotab);
1100 }
1101 retval = v;
1102 break;
Tim Petersd9b9ac82001-01-28 00:27:39 +00001103
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001104 default:
1105 /* Bogus data got written, which isn't ideal.
1106 This will let you keep working and recover. */
1107 PyErr_SetString(PyExc_ValueError, "bad marshal data (unknown type code)");
1108 retval = NULL;
1109 break;
1110
1111 }
1112 p->depth--;
1113 return retval;
Guido van Rossumdce2e3d1991-06-04 19:42:30 +00001114}
1115
Neal Norwitzd85c4522004-06-13 20:31:49 +00001116static PyObject *
Armin Rigo01ab2792004-03-26 15:09:27 +00001117read_object(RFILE *p)
1118{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001119 PyObject *v;
1120 if (PyErr_Occurred()) {
1121 fprintf(stderr, "XXX readobject called with exception set\n");
1122 return NULL;
1123 }
1124 v = r_object(p);
1125 if (v == NULL && !PyErr_Occurred())
1126 PyErr_SetString(PyExc_TypeError, "NULL object in marshal data for object");
1127 return v;
Armin Rigo01ab2792004-03-26 15:09:27 +00001128}
1129
Guido van Rossumb8cf3e62001-10-19 01:46:21 +00001130int
1131PyMarshal_ReadShortFromFile(FILE *fp)
1132{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001133 RFILE rf;
1134 assert(fp);
Vinay Sajip5bdae3b2011-07-02 16:42:47 +01001135 rf.readable = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001136 rf.fp = fp;
Benjamin Peterson43b06862011-05-27 09:08:01 -05001137 rf.current_filename = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001138 rf.end = rf.ptr = NULL;
1139 return r_short(&rf);
Guido van Rossumb8cf3e62001-10-19 01:46:21 +00001140}
1141
Guido van Rossum0b0db8e1993-01-21 16:07:51 +00001142long
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001143PyMarshal_ReadLongFromFile(FILE *fp)
Guido van Rossum0b0db8e1993-01-21 16:07:51 +00001144{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001145 RFILE rf;
1146 rf.fp = fp;
Vinay Sajip5bdae3b2011-07-02 16:42:47 +01001147 rf.readable = NULL;
Benjamin Peterson43b06862011-05-27 09:08:01 -05001148 rf.current_filename = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001149 rf.ptr = rf.end = NULL;
1150 return r_long(&rf);
Guido van Rossum0b0db8e1993-01-21 16:07:51 +00001151}
1152
Tim Peters691e0e92001-01-18 04:39:16 +00001153#ifdef HAVE_FSTAT
1154/* Return size of file in bytes; < 0 if unknown. */
1155static off_t
1156getfilesize(FILE *fp)
1157{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001158 struct stat st;
1159 if (fstat(fileno(fp), &st) != 0)
1160 return -1;
1161 else
1162 return st.st_size;
Tim Peters691e0e92001-01-18 04:39:16 +00001163}
1164#endif
Tim Petersd9b9ac82001-01-28 00:27:39 +00001165
Tim Peters691e0e92001-01-18 04:39:16 +00001166/* If we can get the size of the file up-front, and it's reasonably small,
1167 * read it in one gulp and delegate to ...FromString() instead. Much quicker
1168 * than reading a byte at a time from file; speeds .pyc imports.
Tim Petersd9b9ac82001-01-28 00:27:39 +00001169 * CAUTION: since this may read the entire remainder of the file, don't
1170 * call it unless you know you're done with the file.
Tim Peters691e0e92001-01-18 04:39:16 +00001171 */
Guido van Rossum79f25d91997-04-29 20:08:16 +00001172PyObject *
Tim Petersd9b9ac82001-01-28 00:27:39 +00001173PyMarshal_ReadLastObjectFromFile(FILE *fp)
Guido van Rossum0b0db8e1993-01-21 16:07:51 +00001174{
Antoine Pitrou5bc7ec92010-04-21 22:56:22 +00001175/* REASONABLE_FILE_LIMIT is by defn something big enough for Tkinter.pyc. */
Tim Peters691e0e92001-01-18 04:39:16 +00001176#define REASONABLE_FILE_LIMIT (1L << 18)
Tim Peters691e0e92001-01-18 04:39:16 +00001177#ifdef HAVE_FSTAT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001178 off_t filesize;
1179 filesize = getfilesize(fp);
1180 if (filesize > 0 && filesize <= REASONABLE_FILE_LIMIT) {
1181 char* pBuf = (char *)PyMem_MALLOC(filesize);
1182 if (pBuf != NULL) {
1183 PyObject* v;
1184 size_t n;
1185 /* filesize must fit into an int, because it
1186 is smaller than REASONABLE_FILE_LIMIT */
1187 n = fread(pBuf, 1, (int)filesize, fp);
1188 v = PyMarshal_ReadObjectFromString(pBuf, n);
1189 PyMem_FREE(pBuf);
1190 return v;
1191 }
Tim Petersd9b9ac82001-01-28 00:27:39 +00001192
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001193 }
Tim Peters691e0e92001-01-18 04:39:16 +00001194#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001195 /* We don't have fstat, or we do but the file is larger than
1196 * REASONABLE_FILE_LIMIT or malloc failed -- read a byte at a time.
1197 */
1198 return PyMarshal_ReadObjectFromFile(fp);
Tim Petersd9b9ac82001-01-28 00:27:39 +00001199
Tim Peters691e0e92001-01-18 04:39:16 +00001200#undef REASONABLE_FILE_LIMIT
Guido van Rossum0b0db8e1993-01-21 16:07:51 +00001201}
1202
Guido van Rossum79f25d91997-04-29 20:08:16 +00001203PyObject *
Tim Petersd9b9ac82001-01-28 00:27:39 +00001204PyMarshal_ReadObjectFromFile(FILE *fp)
1205{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001206 RFILE rf;
1207 PyObject *result;
1208 rf.fp = fp;
Vinay Sajip5bdae3b2011-07-02 16:42:47 +01001209 rf.readable = NULL;
Benjamin Peterson43b06862011-05-27 09:08:01 -05001210 rf.current_filename = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001211 rf.depth = 0;
1212 rf.ptr = rf.end = NULL;
1213 result = r_object(&rf);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001214 return result;
Tim Petersd9b9ac82001-01-28 00:27:39 +00001215}
1216
1217PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +00001218PyMarshal_ReadObjectFromString(char *str, Py_ssize_t len)
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001219{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001220 RFILE rf;
1221 PyObject *result;
1222 rf.fp = NULL;
Vinay Sajip5bdae3b2011-07-02 16:42:47 +01001223 rf.readable = NULL;
Benjamin Peterson43b06862011-05-27 09:08:01 -05001224 rf.current_filename = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001225 rf.ptr = str;
1226 rf.end = str + len;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001227 rf.depth = 0;
1228 result = r_object(&rf);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001229 return result;
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001230}
1231
Guido van Rossum79f25d91997-04-29 20:08:16 +00001232PyObject *
Martin v. Löwisef82d2f2004-06-27 16:51:46 +00001233PyMarshal_WriteObjectToString(PyObject *x, int version)
Guido van Rossum3f3bb3d1996-08-19 22:07:17 +00001234{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001235 WFILE wf;
Guido van Rossume6d39042007-05-09 00:01:30 +00001236
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001237 wf.fp = NULL;
Vinay Sajip5bdae3b2011-07-02 16:42:47 +01001238 wf.readable = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001239 wf.str = PyBytes_FromStringAndSize((char *)NULL, 50);
1240 if (wf.str == NULL)
1241 return NULL;
1242 wf.ptr = PyBytes_AS_STRING((PyBytesObject *)wf.str);
1243 wf.end = wf.ptr + PyBytes_Size(wf.str);
1244 wf.error = WFERR_OK;
1245 wf.depth = 0;
1246 wf.version = version;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001247 w_object(x, &wf);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001248 if (wf.str != NULL) {
1249 char *base = PyBytes_AS_STRING((PyBytesObject *)wf.str);
1250 if (wf.ptr - base > PY_SSIZE_T_MAX) {
1251 Py_DECREF(wf.str);
1252 PyErr_SetString(PyExc_OverflowError,
1253 "too much marshal data for a string");
1254 return NULL;
1255 }
1256 if (_PyBytes_Resize(&wf.str, (Py_ssize_t)(wf.ptr - base)) < 0)
1257 return NULL;
1258 }
1259 if (wf.error != WFERR_OK) {
1260 Py_XDECREF(wf.str);
1261 if (wf.error == WFERR_NOMEMORY)
1262 PyErr_NoMemory();
1263 else
1264 PyErr_SetString(PyExc_ValueError,
1265 (wf.error==WFERR_UNMARSHALLABLE)?"unmarshallable object"
1266 :"object too deeply nested to marshal");
1267 return NULL;
1268 }
Antoine Pitrou1c13f842012-03-02 18:22:23 +01001269 return wf.str;
Guido van Rossum3f3bb3d1996-08-19 22:07:17 +00001270}
1271
Guido van Rossum64b45521991-06-07 13:58:22 +00001272/* And an interface for Python programs... */
Guido van Rossumdce2e3d1991-06-04 19:42:30 +00001273
Guido van Rossum79f25d91997-04-29 20:08:16 +00001274static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001275marshal_dump(PyObject *self, PyObject *args)
Guido van Rossumdce2e3d1991-06-04 19:42:30 +00001276{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001277 /* XXX Quick hack -- need to do this differently */
1278 PyObject *x;
1279 PyObject *f;
1280 int version = Py_MARSHAL_VERSION;
1281 PyObject *s;
1282 PyObject *res;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001283 _Py_IDENTIFIER(write);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02001284
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001285 if (!PyArg_ParseTuple(args, "OO|i:dump", &x, &f, &version))
1286 return NULL;
1287 s = PyMarshal_WriteObjectToString(x, version);
1288 if (s == NULL)
1289 return NULL;
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02001290 res = _PyObject_CallMethodId(f, &PyId_write, "O", s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001291 Py_DECREF(s);
1292 return res;
Guido van Rossumdce2e3d1991-06-04 19:42:30 +00001293}
1294
R. David Murraydd226ea2009-05-13 12:27:21 +00001295PyDoc_STRVAR(dump_doc,
1296"dump(value, file[, version])\n\
1297\n\
1298Write the value on the open file. The value must be a supported type.\n\
1299The file must be an open file object such as sys.stdout or returned by\n\
1300open() or os.popen(). It must be opened in binary mode ('wb' or 'w+b').\n\
1301\n\
1302If the value has (or contains an object that has) an unsupported type, a\n\
1303ValueError exception is raised but garbage data will also be written\n\
1304to the file. The object will not be properly read back by load()\n\
1305\n\
1306The version argument indicates the data format that dump should use.");
1307
Guido van Rossum79f25d91997-04-29 20:08:16 +00001308static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001309marshal_load(PyObject *self, PyObject *f)
Guido van Rossumdce2e3d1991-06-04 19:42:30 +00001310{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001311 PyObject *data, *result;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001312 _Py_IDENTIFIER(read);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001313 RFILE rf;
Vinay Sajip5bdae3b2011-07-02 16:42:47 +01001314
1315 /*
1316 * Make a call to the read method, but read zero bytes.
1317 * This is to ensure that the object passed in at least
1318 * has a read method which returns bytes.
1319 */
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02001320 data = _PyObject_CallMethodId(f, &PyId_read, "i", 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001321 if (data == NULL)
1322 return NULL;
Vinay Sajip5bdae3b2011-07-02 16:42:47 +01001323 if (!PyBytes_Check(data)) {
1324 PyErr_Format(PyExc_TypeError,
1325 "f.read() returned not bytes but %.100s",
1326 data->ob_type->tp_name);
1327 result = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001328 }
1329 else {
Vinay Sajip5bdae3b2011-07-02 16:42:47 +01001330 rf.depth = 0;
1331 rf.fp = NULL;
1332 rf.readable = f;
Vinay Sajipaac0f752011-07-02 18:42:21 +01001333 rf.current_filename = NULL;
Vinay Sajip5bdae3b2011-07-02 16:42:47 +01001334 result = read_object(&rf);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001335 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001336 Py_DECREF(data);
1337 return result;
Guido van Rossum0b0db8e1993-01-21 16:07:51 +00001338}
1339
R. David Murraydd226ea2009-05-13 12:27:21 +00001340PyDoc_STRVAR(load_doc,
1341"load(file)\n\
1342\n\
1343Read one value from the open file and return it. If no valid value is\n\
1344read (e.g. because the data has a different Python versions\n\
1345incompatible marshal format), raise EOFError, ValueError or TypeError.\n\
1346The file must be an open file object opened in binary mode ('rb' or\n\
1347'r+b').\n\
1348\n\
1349Note: If an object containing an unsupported type was marshalled with\n\
1350dump(), load() will substitute None for the unmarshallable type.");
1351
1352
Guido van Rossum79f25d91997-04-29 20:08:16 +00001353static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001354marshal_dumps(PyObject *self, PyObject *args)
Guido van Rossum0b0db8e1993-01-21 16:07:51 +00001355{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001356 PyObject *x;
1357 int version = Py_MARSHAL_VERSION;
1358 if (!PyArg_ParseTuple(args, "O|i:dumps", &x, &version))
1359 return NULL;
1360 return PyMarshal_WriteObjectToString(x, version);
Guido van Rossum0b0db8e1993-01-21 16:07:51 +00001361}
1362
R. David Murraydd226ea2009-05-13 12:27:21 +00001363PyDoc_STRVAR(dumps_doc,
1364"dumps(value[, version])\n\
1365\n\
1366Return the string that would be written to a file by dump(value, file).\n\
1367The value must be a supported type. Raise a ValueError exception if\n\
1368value has (or contains an object that has) an unsupported type.\n\
1369\n\
1370The version argument indicates the data format that dumps should use.");
1371
1372
Guido van Rossum79f25d91997-04-29 20:08:16 +00001373static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001374marshal_loads(PyObject *self, PyObject *args)
Guido van Rossum0b0db8e1993-01-21 16:07:51 +00001375{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001376 RFILE rf;
1377 Py_buffer p;
1378 char *s;
1379 Py_ssize_t n;
1380 PyObject* result;
Antoine Pitrou4a90ef02012-03-03 02:35:32 +01001381 if (!PyArg_ParseTuple(args, "y*:loads", &p))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001382 return NULL;
1383 s = p.buf;
1384 n = p.len;
1385 rf.fp = NULL;
Vinay Sajip5bdae3b2011-07-02 16:42:47 +01001386 rf.readable = NULL;
Benjamin Peterson43b06862011-05-27 09:08:01 -05001387 rf.current_filename = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001388 rf.ptr = s;
1389 rf.end = s + n;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001390 rf.depth = 0;
1391 result = read_object(&rf);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001392 PyBuffer_Release(&p);
1393 return result;
Guido van Rossumdce2e3d1991-06-04 19:42:30 +00001394}
1395
R. David Murraydd226ea2009-05-13 12:27:21 +00001396PyDoc_STRVAR(loads_doc,
Antoine Pitrou4a90ef02012-03-03 02:35:32 +01001397"loads(bytes)\n\
R. David Murraydd226ea2009-05-13 12:27:21 +00001398\n\
Antoine Pitrou4a90ef02012-03-03 02:35:32 +01001399Convert the bytes object to a value. If no valid value is found, raise\n\
1400EOFError, ValueError or TypeError. Extra characters in the input are\n\
R. David Murraydd226ea2009-05-13 12:27:21 +00001401ignored.");
1402
Guido van Rossum79f25d91997-04-29 20:08:16 +00001403static PyMethodDef marshal_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001404 {"dump", marshal_dump, METH_VARARGS, dump_doc},
1405 {"load", marshal_load, METH_O, load_doc},
1406 {"dumps", marshal_dumps, METH_VARARGS, dumps_doc},
1407 {"loads", marshal_loads, METH_VARARGS, loads_doc},
1408 {NULL, NULL} /* sentinel */
Guido van Rossumdce2e3d1991-06-04 19:42:30 +00001409};
1410
R. David Murraydd226ea2009-05-13 12:27:21 +00001411
1412PyDoc_STRVAR(module_doc,
1413"This module contains functions that can read and write Python values in\n\
1414a binary format. The format is specific to Python, but independent of\n\
1415machine architecture issues.\n\
1416\n\
1417Not all Python object types are supported; in general, only objects\n\
1418whose value is independent from a particular invocation of Python can be\n\
1419written and read by this module. The following types are supported:\n\
1420None, integers, floating point numbers, strings, bytes, bytearrays,\n\
1421tuples, lists, sets, dictionaries, and code objects, where it\n\
1422should be understood that tuples, lists and dictionaries are only\n\
1423supported as long as the values contained therein are themselves\n\
1424supported; and recursive lists and dictionaries should not be written\n\
1425(they will cause infinite loops).\n\
1426\n\
1427Variables:\n\
1428\n\
1429version -- indicates the format that the module uses. Version 0 is the\n\
1430 historical format, version 1 shares interned strings and version 2\n\
1431 uses a binary format for floating point numbers.\n\
1432\n\
1433Functions:\n\
1434\n\
1435dump() -- write value to a file\n\
1436load() -- read value from a file\n\
1437dumps() -- write value to a string\n\
1438loads() -- read value from a string");
1439
1440
1441
Brett Cannon429ef652008-06-27 00:35:35 +00001442static struct PyModuleDef marshalmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001443 PyModuleDef_HEAD_INIT,
1444 "marshal",
1445 module_doc,
1446 0,
1447 marshal_methods,
1448 NULL,
1449 NULL,
1450 NULL,
1451 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00001452};
1453
Jason Tishler6bc06ec2003-09-04 11:59:50 +00001454PyMODINIT_FUNC
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001455PyMarshal_Init(void)
Guido van Rossumdce2e3d1991-06-04 19:42:30 +00001456{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001457 PyObject *mod = PyModule_Create(&marshalmodule);
1458 if (mod == NULL)
1459 return NULL;
1460 PyModule_AddIntConstant(mod, "version", Py_MARSHAL_VERSION);
1461 return mod;
Guido van Rossumdce2e3d1991-06-04 19:42:30 +00001462}