Issue #5262: Fixed bug in next roll over time computation in TimedRotatingFileHandler.
[python.git] / Python / marshal.c
blob8f8ac365765360700ef9f0a82ad1ac6d018c7ad4
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. */
7 #define PY_SSIZE_T_CLEAN
9 #include "Python.h"
10 #include "longintrepr.h"
11 #include "code.h"
12 #include "marshal.h"
14 #define ABS(x) ((x) < 0 ? -(x) : (x))
16 /* High water mark to determine when the marshalled object is dangerously deep
17 * and risks coring the interpreter. When the object stack gets this deep,
18 * raise an exception instead of continuing.
20 #define MAX_MARSHAL_STACK_DEPTH 2000
22 #define TYPE_NULL '0'
23 #define TYPE_NONE 'N'
24 #define TYPE_FALSE 'F'
25 #define TYPE_TRUE 'T'
26 #define TYPE_STOPITER 'S'
27 #define TYPE_ELLIPSIS '.'
28 #define TYPE_INT 'i'
29 #define TYPE_INT64 'I'
30 #define TYPE_FLOAT 'f'
31 #define TYPE_BINARY_FLOAT 'g'
32 #define TYPE_COMPLEX 'x'
33 #define TYPE_BINARY_COMPLEX 'y'
34 #define TYPE_LONG 'l'
35 #define TYPE_STRING 's'
36 #define TYPE_INTERNED 't'
37 #define TYPE_STRINGREF 'R'
38 #define TYPE_TUPLE '('
39 #define TYPE_LIST '['
40 #define TYPE_DICT '{'
41 #define TYPE_CODE 'c'
42 #define TYPE_UNICODE 'u'
43 #define TYPE_UNKNOWN '?'
44 #define TYPE_SET '<'
45 #define TYPE_FROZENSET '>'
47 typedef struct {
48 FILE *fp;
49 int error;
50 int depth;
51 /* If fp == NULL, the following are valid: */
52 PyObject *str;
53 char *ptr;
54 char *end;
55 PyObject *strings; /* dict on marshal, list on unmarshal */
56 int version;
57 } WFILE;
59 #define w_byte(c, p) if (((p)->fp)) putc((c), (p)->fp); \
60 else if ((p)->ptr != (p)->end) *(p)->ptr++ = (c); \
61 else w_more(c, p)
63 static void
64 w_more(int c, WFILE *p)
66 Py_ssize_t size, newsize;
67 if (p->str == NULL)
68 return; /* An error already occurred */
69 size = PyString_Size(p->str);
70 newsize = size + size + 1024;
71 if (newsize > 32*1024*1024) {
72 newsize = size + (size >> 3); /* 12.5% overallocation */
74 if (_PyString_Resize(&p->str, newsize) != 0) {
75 p->ptr = p->end = NULL;
77 else {
78 p->ptr = PyString_AS_STRING((PyStringObject *)p->str) + size;
79 p->end =
80 PyString_AS_STRING((PyStringObject *)p->str) + newsize;
81 *p->ptr++ = Py_SAFE_DOWNCAST(c, int, char);
85 static void
86 w_string(char *s, int n, WFILE *p)
88 if (p->fp != NULL) {
89 fwrite(s, 1, n, p->fp);
91 else {
92 while (--n >= 0) {
93 w_byte(*s, p);
94 s++;
99 static void
100 w_short(int x, WFILE *p)
102 w_byte((char)( x & 0xff), p);
103 w_byte((char)((x>> 8) & 0xff), p);
106 static void
107 w_long(long x, WFILE *p)
109 w_byte((char)( x & 0xff), p);
110 w_byte((char)((x>> 8) & 0xff), p);
111 w_byte((char)((x>>16) & 0xff), p);
112 w_byte((char)((x>>24) & 0xff), p);
115 #if SIZEOF_LONG > 4
116 static void
117 w_long64(long x, WFILE *p)
119 w_long(x, p);
120 w_long(x>>32, p);
122 #endif
124 /* We assume that Python longs are stored internally in base some power of
125 2**15; for the sake of portability we'll always read and write them in base
126 exactly 2**15. */
128 #define PyLong_MARSHAL_SHIFT 15
129 #define PyLong_MARSHAL_BASE ((short)1 << PyLong_MARSHAL_SHIFT)
130 #define PyLong_MARSHAL_MASK (PyLong_MARSHAL_BASE - 1)
131 #if PyLong_SHIFT % PyLong_MARSHAL_SHIFT != 0
132 #error "PyLong_SHIFT must be a multiple of PyLong_MARSHAL_SHIFT"
133 #endif
134 #define PyLong_MARSHAL_RATIO (PyLong_SHIFT / PyLong_MARSHAL_SHIFT)
136 static void
137 w_PyLong(const PyLongObject *ob, WFILE *p)
139 Py_ssize_t i, j, n, l;
140 digit d;
142 w_byte(TYPE_LONG, p);
143 if (Py_SIZE(ob) == 0) {
144 w_long((long)0, p);
145 return;
148 /* set l to number of base PyLong_MARSHAL_BASE digits */
149 n = ABS(Py_SIZE(ob));
150 l = (n-1) * PyLong_MARSHAL_RATIO;
151 d = ob->ob_digit[n-1];
152 assert(d != 0); /* a PyLong is always normalized */
153 do {
154 d >>= PyLong_MARSHAL_SHIFT;
155 l++;
156 } while (d != 0);
157 w_long((long)(Py_SIZE(ob) > 0 ? l : -l), p);
159 for (i=0; i < n-1; i++) {
160 d = ob->ob_digit[i];
161 for (j=0; j < PyLong_MARSHAL_RATIO; j++) {
162 w_short(d & PyLong_MARSHAL_MASK, p);
163 d >>= PyLong_MARSHAL_SHIFT;
165 assert (d == 0);
167 d = ob->ob_digit[n-1];
168 do {
169 w_short(d & PyLong_MARSHAL_MASK, p);
170 d >>= PyLong_MARSHAL_SHIFT;
171 } while (d != 0);
174 static void
175 w_object(PyObject *v, WFILE *p)
177 Py_ssize_t i, n;
179 p->depth++;
181 if (p->depth > MAX_MARSHAL_STACK_DEPTH) {
182 p->error = 2;
184 else if (v == NULL) {
185 w_byte(TYPE_NULL, p);
187 else if (v == Py_None) {
188 w_byte(TYPE_NONE, p);
190 else if (v == PyExc_StopIteration) {
191 w_byte(TYPE_STOPITER, p);
193 else if (v == Py_Ellipsis) {
194 w_byte(TYPE_ELLIPSIS, p);
196 else if (v == Py_False) {
197 w_byte(TYPE_FALSE, p);
199 else if (v == Py_True) {
200 w_byte(TYPE_TRUE, p);
202 else if (PyInt_CheckExact(v)) {
203 long x = PyInt_AS_LONG((PyIntObject *)v);
204 #if SIZEOF_LONG > 4
205 long y = Py_ARITHMETIC_RIGHT_SHIFT(long, x, 31);
206 if (y && y != -1) {
207 w_byte(TYPE_INT64, p);
208 w_long64(x, p);
210 else
211 #endif
213 w_byte(TYPE_INT, p);
214 w_long(x, p);
217 else if (PyLong_CheckExact(v)) {
218 PyLongObject *ob = (PyLongObject *)v;
219 w_PyLong(ob, p);
221 else if (PyFloat_CheckExact(v)) {
222 if (p->version > 1) {
223 unsigned char buf[8];
224 if (_PyFloat_Pack8(PyFloat_AsDouble(v),
225 buf, 1) < 0) {
226 p->error = 1;
227 return;
229 w_byte(TYPE_BINARY_FLOAT, p);
230 w_string((char*)buf, 8, p);
232 else {
233 char buf[256]; /* Plenty to format any double */
234 PyFloat_AsReprString(buf, (PyFloatObject *)v);
235 n = strlen(buf);
236 w_byte(TYPE_FLOAT, p);
237 w_byte((int)n, p);
238 w_string(buf, (int)n, p);
241 #ifndef WITHOUT_COMPLEX
242 else if (PyComplex_CheckExact(v)) {
243 if (p->version > 1) {
244 unsigned char buf[8];
245 if (_PyFloat_Pack8(PyComplex_RealAsDouble(v),
246 buf, 1) < 0) {
247 p->error = 1;
248 return;
250 w_byte(TYPE_BINARY_COMPLEX, p);
251 w_string((char*)buf, 8, p);
252 if (_PyFloat_Pack8(PyComplex_ImagAsDouble(v),
253 buf, 1) < 0) {
254 p->error = 1;
255 return;
257 w_string((char*)buf, 8, p);
259 else {
260 char buf[256]; /* Plenty to format any double */
261 PyFloatObject *temp;
262 w_byte(TYPE_COMPLEX, p);
263 temp = (PyFloatObject*)PyFloat_FromDouble(
264 PyComplex_RealAsDouble(v));
265 if (!temp) {
266 p->error = 1;
267 return;
269 PyFloat_AsReprString(buf, temp);
270 Py_DECREF(temp);
271 n = strlen(buf);
272 w_byte((int)n, p);
273 w_string(buf, (int)n, p);
274 temp = (PyFloatObject*)PyFloat_FromDouble(
275 PyComplex_ImagAsDouble(v));
276 if (!temp) {
277 p->error = 1;
278 return;
280 PyFloat_AsReprString(buf, temp);
281 Py_DECREF(temp);
282 n = strlen(buf);
283 w_byte((int)n, p);
284 w_string(buf, (int)n, p);
287 #endif
288 else if (PyString_CheckExact(v)) {
289 if (p->strings && PyString_CHECK_INTERNED(v)) {
290 PyObject *o = PyDict_GetItem(p->strings, v);
291 if (o) {
292 long w = PyInt_AsLong(o);
293 w_byte(TYPE_STRINGREF, p);
294 w_long(w, p);
295 goto exit;
297 else {
298 int ok;
299 o = PyInt_FromSsize_t(PyDict_Size(p->strings));
300 ok = o &&
301 PyDict_SetItem(p->strings, v, o) >= 0;
302 Py_XDECREF(o);
303 if (!ok) {
304 p->depth--;
305 p->error = 1;
306 return;
308 w_byte(TYPE_INTERNED, p);
311 else {
312 w_byte(TYPE_STRING, p);
314 n = PyString_GET_SIZE(v);
315 if (n > INT_MAX) {
316 /* huge strings are not supported */
317 p->depth--;
318 p->error = 1;
319 return;
321 w_long((long)n, p);
322 w_string(PyString_AS_STRING(v), (int)n, p);
324 #ifdef Py_USING_UNICODE
325 else if (PyUnicode_CheckExact(v)) {
326 PyObject *utf8;
327 utf8 = PyUnicode_AsUTF8String(v);
328 if (utf8 == NULL) {
329 p->depth--;
330 p->error = 1;
331 return;
333 w_byte(TYPE_UNICODE, p);
334 n = PyString_GET_SIZE(utf8);
335 if (n > INT_MAX) {
336 p->depth--;
337 p->error = 1;
338 return;
340 w_long((long)n, p);
341 w_string(PyString_AS_STRING(utf8), (int)n, p);
342 Py_DECREF(utf8);
344 #endif
345 else if (PyTuple_CheckExact(v)) {
346 w_byte(TYPE_TUPLE, p);
347 n = PyTuple_Size(v);
348 w_long((long)n, p);
349 for (i = 0; i < n; i++) {
350 w_object(PyTuple_GET_ITEM(v, i), p);
353 else if (PyList_CheckExact(v)) {
354 w_byte(TYPE_LIST, p);
355 n = PyList_GET_SIZE(v);
356 w_long((long)n, p);
357 for (i = 0; i < n; i++) {
358 w_object(PyList_GET_ITEM(v, i), p);
361 else if (PyDict_CheckExact(v)) {
362 Py_ssize_t pos;
363 PyObject *key, *value;
364 w_byte(TYPE_DICT, p);
365 /* This one is NULL object terminated! */
366 pos = 0;
367 while (PyDict_Next(v, &pos, &key, &value)) {
368 w_object(key, p);
369 w_object(value, p);
371 w_object((PyObject *)NULL, p);
373 else if (PyAnySet_CheckExact(v)) {
374 PyObject *value, *it;
376 if (PyObject_TypeCheck(v, &PySet_Type))
377 w_byte(TYPE_SET, p);
378 else
379 w_byte(TYPE_FROZENSET, p);
380 n = PyObject_Size(v);
381 if (n == -1) {
382 p->depth--;
383 p->error = 1;
384 return;
386 w_long((long)n, p);
387 it = PyObject_GetIter(v);
388 if (it == NULL) {
389 p->depth--;
390 p->error = 1;
391 return;
393 while ((value = PyIter_Next(it)) != NULL) {
394 w_object(value, p);
395 Py_DECREF(value);
397 Py_DECREF(it);
398 if (PyErr_Occurred()) {
399 p->depth--;
400 p->error = 1;
401 return;
404 else if (PyCode_Check(v)) {
405 PyCodeObject *co = (PyCodeObject *)v;
406 w_byte(TYPE_CODE, p);
407 w_long(co->co_argcount, p);
408 w_long(co->co_nlocals, p);
409 w_long(co->co_stacksize, p);
410 w_long(co->co_flags, p);
411 w_object(co->co_code, p);
412 w_object(co->co_consts, p);
413 w_object(co->co_names, p);
414 w_object(co->co_varnames, p);
415 w_object(co->co_freevars, p);
416 w_object(co->co_cellvars, p);
417 w_object(co->co_filename, p);
418 w_object(co->co_name, p);
419 w_long(co->co_firstlineno, p);
420 w_object(co->co_lnotab, p);
422 else if (PyObject_CheckReadBuffer(v)) {
423 /* Write unknown buffer-style objects as a string */
424 char *s;
425 PyBufferProcs *pb = v->ob_type->tp_as_buffer;
426 w_byte(TYPE_STRING, p);
427 n = (*pb->bf_getreadbuffer)(v, 0, (void **)&s);
428 if (n > INT_MAX) {
429 p->depth--;
430 p->error = 1;
431 return;
433 w_long((long)n, p);
434 w_string(s, (int)n, p);
436 else {
437 w_byte(TYPE_UNKNOWN, p);
438 p->error = 1;
440 exit:
441 p->depth--;
444 /* version currently has no effect for writing longs. */
445 void
446 PyMarshal_WriteLongToFile(long x, FILE *fp, int version)
448 WFILE wf;
449 wf.fp = fp;
450 wf.error = 0;
451 wf.depth = 0;
452 wf.strings = NULL;
453 wf.version = version;
454 w_long(x, &wf);
457 void
458 PyMarshal_WriteObjectToFile(PyObject *x, FILE *fp, int version)
460 WFILE wf;
461 wf.fp = fp;
462 wf.error = 0;
463 wf.depth = 0;
464 wf.strings = (version > 0) ? PyDict_New() : NULL;
465 wf.version = version;
466 w_object(x, &wf);
467 Py_XDECREF(wf.strings);
470 typedef WFILE RFILE; /* Same struct with different invariants */
472 #define rs_byte(p) (((p)->ptr < (p)->end) ? (unsigned char)*(p)->ptr++ : EOF)
474 #define r_byte(p) ((p)->fp ? getc((p)->fp) : rs_byte(p))
476 static int
477 r_string(char *s, int n, RFILE *p)
479 if (p->fp != NULL)
480 /* The result fits into int because it must be <=n. */
481 return (int)fread(s, 1, n, p->fp);
482 if (p->end - p->ptr < n)
483 n = (int)(p->end - p->ptr);
484 memcpy(s, p->ptr, n);
485 p->ptr += n;
486 return n;
489 static int
490 r_short(RFILE *p)
492 register short x;
493 x = r_byte(p);
494 x |= r_byte(p) << 8;
495 /* Sign-extension, in case short greater than 16 bits */
496 x |= -(x & 0x8000);
497 return x;
500 static long
501 r_long(RFILE *p)
503 register long x;
504 register FILE *fp = p->fp;
505 if (fp) {
506 x = getc(fp);
507 x |= (long)getc(fp) << 8;
508 x |= (long)getc(fp) << 16;
509 x |= (long)getc(fp) << 24;
511 else {
512 x = rs_byte(p);
513 x |= (long)rs_byte(p) << 8;
514 x |= (long)rs_byte(p) << 16;
515 x |= (long)rs_byte(p) << 24;
517 #if SIZEOF_LONG > 4
518 /* Sign extension for 64-bit machines */
519 x |= -(x & 0x80000000L);
520 #endif
521 return x;
524 /* r_long64 deals with the TYPE_INT64 code. On a machine with
525 sizeof(long) > 4, it returns a Python int object, else a Python long
526 object. Note that w_long64 writes out TYPE_INT if 32 bits is enough,
527 so there's no inefficiency here in returning a PyLong on 32-bit boxes
528 for everything written via TYPE_INT64 (i.e., if an int is written via
529 TYPE_INT64, it *needs* more than 32 bits).
531 static PyObject *
532 r_long64(RFILE *p)
534 long lo4 = r_long(p);
535 long hi4 = r_long(p);
536 #if SIZEOF_LONG > 4
537 long x = (hi4 << 32) | (lo4 & 0xFFFFFFFFL);
538 return PyInt_FromLong(x);
539 #else
540 unsigned char buf[8];
541 int one = 1;
542 int is_little_endian = (int)*(char*)&one;
543 if (is_little_endian) {
544 memcpy(buf, &lo4, 4);
545 memcpy(buf+4, &hi4, 4);
547 else {
548 memcpy(buf, &hi4, 4);
549 memcpy(buf+4, &lo4, 4);
551 return _PyLong_FromByteArray(buf, 8, is_little_endian, 1);
552 #endif
555 static PyObject *
556 r_PyLong(RFILE *p)
558 PyLongObject *ob;
559 int size, i, j, md;
560 long n;
561 digit d;
563 n = r_long(p);
564 if (n == 0)
565 return (PyObject *)_PyLong_New(0);
566 if (n < -INT_MAX || n > INT_MAX) {
567 PyErr_SetString(PyExc_ValueError,
568 "bad marshal data (long size out of range)");
569 return NULL;
572 size = 1 + (ABS(n)-1) / PyLong_MARSHAL_RATIO;
573 ob = _PyLong_New(size);
574 if (ob == NULL)
575 return NULL;
576 Py_SIZE(ob) = n > 0 ? size : -size;
578 for (i = 0; i < size-1; i++) {
579 d = 0;
580 for (j=0; j < PyLong_MARSHAL_RATIO; j++) {
581 md = r_short(p);
582 if (md < 0 || md > PyLong_MARSHAL_BASE)
583 goto bad_digit;
584 d += (digit)md << j*PyLong_MARSHAL_SHIFT;
586 ob->ob_digit[i] = d;
588 d = 0;
589 for (j=0; j < (ABS(n)-1)%PyLong_MARSHAL_RATIO + 1; j++) {
590 md = r_short(p);
591 if (md < 0 || md > PyLong_MARSHAL_BASE)
592 goto bad_digit;
593 d += (digit)md << j*PyLong_MARSHAL_SHIFT;
595 ob->ob_digit[size-1] = d;
596 return (PyObject *)ob;
597 bad_digit:
598 Py_DECREF(ob);
599 PyErr_SetString(PyExc_ValueError,
600 "bad marshal data (digit out of range in long)");
601 return NULL;
605 static PyObject *
606 r_object(RFILE *p)
608 /* NULL is a valid return value, it does not necessarily means that
609 an exception is set. */
610 PyObject *v, *v2;
611 long i, n;
612 int type = r_byte(p);
613 PyObject *retval;
615 p->depth++;
617 if (p->depth > MAX_MARSHAL_STACK_DEPTH) {
618 p->depth--;
619 PyErr_SetString(PyExc_ValueError, "recursion limit exceeded");
620 return NULL;
623 switch (type) {
625 case EOF:
626 PyErr_SetString(PyExc_EOFError,
627 "EOF read where object expected");
628 retval = NULL;
629 break;
631 case TYPE_NULL:
632 retval = NULL;
633 break;
635 case TYPE_NONE:
636 Py_INCREF(Py_None);
637 retval = Py_None;
638 break;
640 case TYPE_STOPITER:
641 Py_INCREF(PyExc_StopIteration);
642 retval = PyExc_StopIteration;
643 break;
645 case TYPE_ELLIPSIS:
646 Py_INCREF(Py_Ellipsis);
647 retval = Py_Ellipsis;
648 break;
650 case TYPE_FALSE:
651 Py_INCREF(Py_False);
652 retval = Py_False;
653 break;
655 case TYPE_TRUE:
656 Py_INCREF(Py_True);
657 retval = Py_True;
658 break;
660 case TYPE_INT:
661 retval = PyInt_FromLong(r_long(p));
662 break;
664 case TYPE_INT64:
665 retval = r_long64(p);
666 break;
668 case TYPE_LONG:
669 retval = r_PyLong(p);
670 break;
672 case TYPE_FLOAT:
674 char buf[256];
675 double dx;
676 n = r_byte(p);
677 if (n == EOF || r_string(buf, (int)n, p) != n) {
678 PyErr_SetString(PyExc_EOFError,
679 "EOF read where object expected");
680 retval = NULL;
681 break;
683 buf[n] = '\0';
684 retval = NULL;
685 PyFPE_START_PROTECT("atof", break)
686 dx = PyOS_ascii_atof(buf);
687 PyFPE_END_PROTECT(dx)
688 retval = PyFloat_FromDouble(dx);
689 break;
692 case TYPE_BINARY_FLOAT:
694 unsigned char buf[8];
695 double x;
696 if (r_string((char*)buf, 8, p) != 8) {
697 PyErr_SetString(PyExc_EOFError,
698 "EOF read where object expected");
699 retval = NULL;
700 break;
702 x = _PyFloat_Unpack8(buf, 1);
703 if (x == -1.0 && PyErr_Occurred()) {
704 retval = NULL;
705 break;
707 retval = PyFloat_FromDouble(x);
708 break;
711 #ifndef WITHOUT_COMPLEX
712 case TYPE_COMPLEX:
714 char buf[256];
715 Py_complex c;
716 n = r_byte(p);
717 if (n == EOF || r_string(buf, (int)n, p) != n) {
718 PyErr_SetString(PyExc_EOFError,
719 "EOF read where object expected");
720 retval = NULL;
721 break;
723 buf[n] = '\0';
724 retval = NULL;
725 PyFPE_START_PROTECT("atof", break;)
726 c.real = PyOS_ascii_atof(buf);
727 PyFPE_END_PROTECT(c)
728 n = r_byte(p);
729 if (n == EOF || r_string(buf, (int)n, p) != n) {
730 PyErr_SetString(PyExc_EOFError,
731 "EOF read where object expected");
732 retval = NULL;
733 break;
735 buf[n] = '\0';
736 PyFPE_START_PROTECT("atof", break)
737 c.imag = PyOS_ascii_atof(buf);
738 PyFPE_END_PROTECT(c)
739 retval = PyComplex_FromCComplex(c);
740 break;
743 case TYPE_BINARY_COMPLEX:
745 unsigned char buf[8];
746 Py_complex c;
747 if (r_string((char*)buf, 8, p) != 8) {
748 PyErr_SetString(PyExc_EOFError,
749 "EOF read where object expected");
750 retval = NULL;
751 break;
753 c.real = _PyFloat_Unpack8(buf, 1);
754 if (c.real == -1.0 && PyErr_Occurred()) {
755 retval = NULL;
756 break;
758 if (r_string((char*)buf, 8, p) != 8) {
759 PyErr_SetString(PyExc_EOFError,
760 "EOF read where object expected");
761 retval = NULL;
762 break;
764 c.imag = _PyFloat_Unpack8(buf, 1);
765 if (c.imag == -1.0 && PyErr_Occurred()) {
766 retval = NULL;
767 break;
769 retval = PyComplex_FromCComplex(c);
770 break;
772 #endif
774 case TYPE_INTERNED:
775 case TYPE_STRING:
776 n = r_long(p);
777 if (n < 0 || n > INT_MAX) {
778 PyErr_SetString(PyExc_ValueError, "bad marshal data (string size out of range)");
779 retval = NULL;
780 break;
782 v = PyString_FromStringAndSize((char *)NULL, n);
783 if (v == NULL) {
784 retval = NULL;
785 break;
787 if (r_string(PyString_AS_STRING(v), (int)n, p) != n) {
788 Py_DECREF(v);
789 PyErr_SetString(PyExc_EOFError,
790 "EOF read where object expected");
791 retval = NULL;
792 break;
794 if (type == TYPE_INTERNED) {
795 PyString_InternInPlace(&v);
796 if (PyList_Append(p->strings, v) < 0) {
797 retval = NULL;
798 break;
801 retval = v;
802 break;
804 case TYPE_STRINGREF:
805 n = r_long(p);
806 if (n < 0 || n >= PyList_GET_SIZE(p->strings)) {
807 PyErr_SetString(PyExc_ValueError, "bad marshal data (string ref out of range)");
808 retval = NULL;
809 break;
811 v = PyList_GET_ITEM(p->strings, n);
812 Py_INCREF(v);
813 retval = v;
814 break;
816 #ifdef Py_USING_UNICODE
817 case TYPE_UNICODE:
819 char *buffer;
821 n = r_long(p);
822 if (n < 0 || n > INT_MAX) {
823 PyErr_SetString(PyExc_ValueError, "bad marshal data (unicode size out of range)");
824 retval = NULL;
825 break;
827 buffer = PyMem_NEW(char, n);
828 if (buffer == NULL) {
829 retval = PyErr_NoMemory();
830 break;
832 if (r_string(buffer, (int)n, p) != n) {
833 PyMem_DEL(buffer);
834 PyErr_SetString(PyExc_EOFError,
835 "EOF read where object expected");
836 retval = NULL;
837 break;
839 v = PyUnicode_DecodeUTF8(buffer, n, NULL);
840 PyMem_DEL(buffer);
841 retval = v;
842 break;
844 #endif
846 case TYPE_TUPLE:
847 n = r_long(p);
848 if (n < 0 || n > INT_MAX) {
849 PyErr_SetString(PyExc_ValueError, "bad marshal data (tuple size out of range)");
850 retval = NULL;
851 break;
853 v = PyTuple_New((int)n);
854 if (v == NULL) {
855 retval = NULL;
856 break;
858 for (i = 0; i < n; i++) {
859 v2 = r_object(p);
860 if ( v2 == NULL ) {
861 if (!PyErr_Occurred())
862 PyErr_SetString(PyExc_TypeError,
863 "NULL object in marshal data for tuple");
864 Py_DECREF(v);
865 v = NULL;
866 break;
868 PyTuple_SET_ITEM(v, (int)i, v2);
870 retval = v;
871 break;
873 case TYPE_LIST:
874 n = r_long(p);
875 if (n < 0 || n > INT_MAX) {
876 PyErr_SetString(PyExc_ValueError, "bad marshal data (list size out of range)");
877 retval = NULL;
878 break;
880 v = PyList_New((int)n);
881 if (v == NULL) {
882 retval = NULL;
883 break;
885 for (i = 0; i < n; i++) {
886 v2 = r_object(p);
887 if ( v2 == NULL ) {
888 if (!PyErr_Occurred())
889 PyErr_SetString(PyExc_TypeError,
890 "NULL object in marshal data for list");
891 Py_DECREF(v);
892 v = NULL;
893 break;
895 PyList_SET_ITEM(v, (int)i, v2);
897 retval = v;
898 break;
900 case TYPE_DICT:
901 v = PyDict_New();
902 if (v == NULL) {
903 retval = NULL;
904 break;
906 for (;;) {
907 PyObject *key, *val;
908 key = r_object(p);
909 if (key == NULL)
910 break;
911 val = r_object(p);
912 if (val != NULL)
913 PyDict_SetItem(v, key, val);
914 Py_DECREF(key);
915 Py_XDECREF(val);
917 if (PyErr_Occurred()) {
918 Py_DECREF(v);
919 v = NULL;
921 retval = v;
922 break;
924 case TYPE_SET:
925 case TYPE_FROZENSET:
926 n = r_long(p);
927 if (n < 0 || n > INT_MAX) {
928 PyErr_SetString(PyExc_ValueError, "bad marshal data (set size out of range)");
929 retval = NULL;
930 break;
932 v = (type == TYPE_SET) ? PySet_New(NULL) : PyFrozenSet_New(NULL);
933 if (v == NULL) {
934 retval = NULL;
935 break;
937 for (i = 0; i < n; i++) {
938 v2 = r_object(p);
939 if ( v2 == NULL ) {
940 if (!PyErr_Occurred())
941 PyErr_SetString(PyExc_TypeError,
942 "NULL object in marshal data for set");
943 Py_DECREF(v);
944 v = NULL;
945 break;
947 if (PySet_Add(v, v2) == -1) {
948 Py_DECREF(v);
949 Py_DECREF(v2);
950 v = NULL;
951 break;
953 Py_DECREF(v2);
955 retval = v;
956 break;
958 case TYPE_CODE:
959 if (PyEval_GetRestricted()) {
960 PyErr_SetString(PyExc_RuntimeError,
961 "cannot unmarshal code objects in "
962 "restricted execution mode");
963 retval = NULL;
964 break;
966 else {
967 int argcount;
968 int nlocals;
969 int stacksize;
970 int flags;
971 PyObject *code = NULL;
972 PyObject *consts = NULL;
973 PyObject *names = NULL;
974 PyObject *varnames = NULL;
975 PyObject *freevars = NULL;
976 PyObject *cellvars = NULL;
977 PyObject *filename = NULL;
978 PyObject *name = NULL;
979 int firstlineno;
980 PyObject *lnotab = NULL;
982 v = NULL;
984 /* XXX ignore long->int overflows for now */
985 argcount = (int)r_long(p);
986 nlocals = (int)r_long(p);
987 stacksize = (int)r_long(p);
988 flags = (int)r_long(p);
989 code = r_object(p);
990 if (code == NULL)
991 goto code_error;
992 consts = r_object(p);
993 if (consts == NULL)
994 goto code_error;
995 names = r_object(p);
996 if (names == NULL)
997 goto code_error;
998 varnames = r_object(p);
999 if (varnames == NULL)
1000 goto code_error;
1001 freevars = r_object(p);
1002 if (freevars == NULL)
1003 goto code_error;
1004 cellvars = r_object(p);
1005 if (cellvars == NULL)
1006 goto code_error;
1007 filename = r_object(p);
1008 if (filename == NULL)
1009 goto code_error;
1010 name = r_object(p);
1011 if (name == NULL)
1012 goto code_error;
1013 firstlineno = (int)r_long(p);
1014 lnotab = r_object(p);
1015 if (lnotab == NULL)
1016 goto code_error;
1018 v = (PyObject *) PyCode_New(
1019 argcount, nlocals, stacksize, flags,
1020 code, consts, names, varnames,
1021 freevars, cellvars, filename, name,
1022 firstlineno, lnotab);
1024 code_error:
1025 Py_XDECREF(code);
1026 Py_XDECREF(consts);
1027 Py_XDECREF(names);
1028 Py_XDECREF(varnames);
1029 Py_XDECREF(freevars);
1030 Py_XDECREF(cellvars);
1031 Py_XDECREF(filename);
1032 Py_XDECREF(name);
1033 Py_XDECREF(lnotab);
1036 retval = v;
1037 break;
1039 default:
1040 /* Bogus data got written, which isn't ideal.
1041 This will let you keep working and recover. */
1042 PyErr_SetString(PyExc_ValueError, "bad marshal data (unknown type code)");
1043 retval = NULL;
1044 break;
1047 p->depth--;
1048 return retval;
1051 static PyObject *
1052 read_object(RFILE *p)
1054 PyObject *v;
1055 if (PyErr_Occurred()) {
1056 fprintf(stderr, "XXX readobject called with exception set\n");
1057 return NULL;
1059 v = r_object(p);
1060 if (v == NULL && !PyErr_Occurred())
1061 PyErr_SetString(PyExc_TypeError, "NULL object in marshal data for object");
1062 return v;
1066 PyMarshal_ReadShortFromFile(FILE *fp)
1068 RFILE rf;
1069 assert(fp);
1070 rf.fp = fp;
1071 rf.strings = NULL;
1072 rf.end = rf.ptr = NULL;
1073 return r_short(&rf);
1076 long
1077 PyMarshal_ReadLongFromFile(FILE *fp)
1079 RFILE rf;
1080 rf.fp = fp;
1081 rf.strings = NULL;
1082 rf.ptr = rf.end = NULL;
1083 return r_long(&rf);
1086 #ifdef HAVE_FSTAT
1087 /* Return size of file in bytes; < 0 if unknown. */
1088 static off_t
1089 getfilesize(FILE *fp)
1091 struct stat st;
1092 if (fstat(fileno(fp), &st) != 0)
1093 return -1;
1094 else
1095 return st.st_size;
1097 #endif
1099 /* If we can get the size of the file up-front, and it's reasonably small,
1100 * read it in one gulp and delegate to ...FromString() instead. Much quicker
1101 * than reading a byte at a time from file; speeds .pyc imports.
1102 * CAUTION: since this may read the entire remainder of the file, don't
1103 * call it unless you know you're done with the file.
1105 PyObject *
1106 PyMarshal_ReadLastObjectFromFile(FILE *fp)
1108 /* 75% of 2.1's .pyc files can exploit SMALL_FILE_LIMIT.
1109 * REASONABLE_FILE_LIMIT is by defn something big enough for Tkinter.pyc.
1111 #define SMALL_FILE_LIMIT (1L << 14)
1112 #define REASONABLE_FILE_LIMIT (1L << 18)
1113 #ifdef HAVE_FSTAT
1114 off_t filesize;
1115 #endif
1116 #ifdef HAVE_FSTAT
1117 filesize = getfilesize(fp);
1118 if (filesize > 0) {
1119 char buf[SMALL_FILE_LIMIT];
1120 char* pBuf = NULL;
1121 if (filesize <= SMALL_FILE_LIMIT)
1122 pBuf = buf;
1123 else if (filesize <= REASONABLE_FILE_LIMIT)
1124 pBuf = (char *)PyMem_MALLOC(filesize);
1125 if (pBuf != NULL) {
1126 PyObject* v;
1127 size_t n;
1128 /* filesize must fit into an int, because it
1129 is smaller than REASONABLE_FILE_LIMIT */
1130 n = fread(pBuf, 1, (int)filesize, fp);
1131 v = PyMarshal_ReadObjectFromString(pBuf, n);
1132 if (pBuf != buf)
1133 PyMem_FREE(pBuf);
1134 return v;
1138 #endif
1139 /* We don't have fstat, or we do but the file is larger than
1140 * REASONABLE_FILE_LIMIT or malloc failed -- read a byte at a time.
1142 return PyMarshal_ReadObjectFromFile(fp);
1144 #undef SMALL_FILE_LIMIT
1145 #undef REASONABLE_FILE_LIMIT
1148 PyObject *
1149 PyMarshal_ReadObjectFromFile(FILE *fp)
1151 RFILE rf;
1152 PyObject *result;
1153 rf.fp = fp;
1154 rf.strings = PyList_New(0);
1155 rf.depth = 0;
1156 rf.ptr = rf.end = NULL;
1157 result = r_object(&rf);
1158 Py_DECREF(rf.strings);
1159 return result;
1162 PyObject *
1163 PyMarshal_ReadObjectFromString(char *str, Py_ssize_t len)
1165 RFILE rf;
1166 PyObject *result;
1167 rf.fp = NULL;
1168 rf.ptr = str;
1169 rf.end = str + len;
1170 rf.strings = PyList_New(0);
1171 rf.depth = 0;
1172 result = r_object(&rf);
1173 Py_DECREF(rf.strings);
1174 return result;
1177 PyObject *
1178 PyMarshal_WriteObjectToString(PyObject *x, int version)
1180 WFILE wf;
1181 wf.fp = NULL;
1182 wf.str = PyString_FromStringAndSize((char *)NULL, 50);
1183 if (wf.str == NULL)
1184 return NULL;
1185 wf.ptr = PyString_AS_STRING((PyStringObject *)wf.str);
1186 wf.end = wf.ptr + PyString_Size(wf.str);
1187 wf.error = 0;
1188 wf.depth = 0;
1189 wf.version = version;
1190 wf.strings = (version > 0) ? PyDict_New() : NULL;
1191 w_object(x, &wf);
1192 Py_XDECREF(wf.strings);
1193 if (wf.str != NULL) {
1194 char *base = PyString_AS_STRING((PyStringObject *)wf.str);
1195 if (wf.ptr - base > PY_SSIZE_T_MAX) {
1196 Py_DECREF(wf.str);
1197 PyErr_SetString(PyExc_OverflowError,
1198 "too much marshall data for a string");
1199 return NULL;
1201 _PyString_Resize(&wf.str, (Py_ssize_t)(wf.ptr - base));
1203 if (wf.error) {
1204 Py_XDECREF(wf.str);
1205 PyErr_SetString(PyExc_ValueError,
1206 (wf.error==1)?"unmarshallable object"
1207 :"object too deeply nested to marshal");
1208 return NULL;
1210 return wf.str;
1213 /* And an interface for Python programs... */
1215 static PyObject *
1216 marshal_dump(PyObject *self, PyObject *args)
1218 WFILE wf;
1219 PyObject *x;
1220 PyObject *f;
1221 int version = Py_MARSHAL_VERSION;
1222 if (!PyArg_ParseTuple(args, "OO|i:dump", &x, &f, &version))
1223 return NULL;
1224 if (!PyFile_Check(f)) {
1225 PyErr_SetString(PyExc_TypeError,
1226 "marshal.dump() 2nd arg must be file");
1227 return NULL;
1229 wf.fp = PyFile_AsFile(f);
1230 wf.str = NULL;
1231 wf.ptr = wf.end = NULL;
1232 wf.error = 0;
1233 wf.depth = 0;
1234 wf.strings = (version > 0) ? PyDict_New() : 0;
1235 wf.version = version;
1236 w_object(x, &wf);
1237 Py_XDECREF(wf.strings);
1238 if (wf.error) {
1239 PyErr_SetString(PyExc_ValueError,
1240 (wf.error==1)?"unmarshallable object"
1241 :"object too deeply nested to marshal");
1242 return NULL;
1244 Py_INCREF(Py_None);
1245 return Py_None;
1248 PyDoc_STRVAR(dump_doc,
1249 "dump(value, file[, version])\n\
1251 Write the value on the open file. The value must be a supported type.\n\
1252 The file must be an open file object such as sys.stdout or returned by\n\
1253 open() or os.popen(). It must be opened in binary mode ('wb' or 'w+b').\n\
1255 If the value has (or contains an object that has) an unsupported type, a\n\
1256 ValueError exception is raised — but garbage data will also be written\n\
1257 to the file. The object will not be properly read back by load()\n\
1259 New in version 2.4: The version argument indicates the data format that\n\
1260 dump should use.");
1262 static PyObject *
1263 marshal_load(PyObject *self, PyObject *f)
1265 RFILE rf;
1266 PyObject *result;
1267 if (!PyFile_Check(f)) {
1268 PyErr_SetString(PyExc_TypeError,
1269 "marshal.load() arg must be file");
1270 return NULL;
1272 rf.fp = PyFile_AsFile(f);
1273 rf.strings = PyList_New(0);
1274 rf.depth = 0;
1275 result = read_object(&rf);
1276 Py_DECREF(rf.strings);
1277 return result;
1280 PyDoc_STRVAR(load_doc,
1281 "load(file)\n\
1283 Read one value from the open file and return it. If no valid value is\n\
1284 read (e.g. because the data has a different Python version’s\n\
1285 incompatible marshal format), raise EOFError, ValueError or TypeError.\n\
1286 The file must be an open file object opened in binary mode ('rb' or\n\
1287 'r+b').\n\
1289 Note: If an object containing an unsupported type was marshalled with\n\
1290 dump(), load() will substitute None for the unmarshallable type.");
1293 static PyObject *
1294 marshal_dumps(PyObject *self, PyObject *args)
1296 PyObject *x;
1297 int version = Py_MARSHAL_VERSION;
1298 if (!PyArg_ParseTuple(args, "O|i:dumps", &x, &version))
1299 return NULL;
1300 return PyMarshal_WriteObjectToString(x, version);
1303 PyDoc_STRVAR(dumps_doc,
1304 "dumps(value[, version])\n\
1306 Return the string that would be written to a file by dump(value, file).\n\
1307 The value must be a supported type. Raise a ValueError exception if\n\
1308 value has (or contains an object that has) an unsupported type.\n\
1310 New in version 2.4: The version argument indicates the data format that\n\
1311 dumps should use.");
1314 static PyObject *
1315 marshal_loads(PyObject *self, PyObject *args)
1317 RFILE rf;
1318 char *s;
1319 Py_ssize_t n;
1320 PyObject* result;
1321 if (!PyArg_ParseTuple(args, "s#:loads", &s, &n))
1322 return NULL;
1323 rf.fp = NULL;
1324 rf.ptr = s;
1325 rf.end = s + n;
1326 rf.strings = PyList_New(0);
1327 rf.depth = 0;
1328 result = read_object(&rf);
1329 Py_DECREF(rf.strings);
1330 return result;
1333 PyDoc_STRVAR(loads_doc,
1334 "loads(string)\n\
1336 Convert the string to a value. If no valid value is found, raise\n\
1337 EOFError, ValueError or TypeError. Extra characters in the string are\n\
1338 ignored.");
1340 static PyMethodDef marshal_methods[] = {
1341 {"dump", marshal_dump, METH_VARARGS, dump_doc},
1342 {"load", marshal_load, METH_O, load_doc},
1343 {"dumps", marshal_dumps, METH_VARARGS, dumps_doc},
1344 {"loads", marshal_loads, METH_VARARGS, loads_doc},
1345 {NULL, NULL} /* sentinel */
1348 PyDoc_STRVAR(marshal_doc,
1349 "This module contains functions that can read and write Python values in\n\
1350 a binary format. The format is specific to Python, but independent of\n\
1351 machine architecture issues.\n\
1353 Not all Python object types are supported; in general, only objects\n\
1354 whose value is independent from a particular invocation of Python can be\n\
1355 written and read by this module. The following types are supported:\n\
1356 None, integers, long integers, floating point numbers, strings, Unicode\n\
1357 objects, tuples, lists, sets, dictionaries, and code objects, where it\n\
1358 should be understood that tuples, lists and dictionaries are only\n\
1359 supported as long as the values contained therein are themselves\n\
1360 supported; and recursive lists and dictionaries should not be written\n\
1361 (they will cause infinite loops).\n\
1363 Variables:\n\
1365 version -- indicates the format that the module uses. Version 0 is the\n\
1366 historical format, version 1 (added in Python 2.4) shares interned\n\
1367 strings and version 2 (added in Python 2.5) uses a binary format for\n\
1368 floating point numbers. (New in version 2.4)\n\
1370 Functions:\n\
1372 dump() -- write value to a file\n\
1373 load() -- read value from a file\n\
1374 dumps() -- write value to a string\n\
1375 loads() -- read value from a string");
1378 PyMODINIT_FUNC
1379 PyMarshal_Init(void)
1381 PyObject *mod = Py_InitModule3("marshal", marshal_methods,
1382 marshal_doc);
1383 if (mod == NULL)
1384 return;
1385 PyModule_AddIntConstant(mod, "version", Py_MARSHAL_VERSION);