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. */
8 #include "longintrepr.h"
12 /* High water mark to determine when the marshalled object is dangerously deep
13 * and risks coring the interpreter. When the object stack gets this deep,
14 * raise an exception instead of continuing.
16 #define MAX_MARSHAL_STACK_DEPTH 5000
20 #define TYPE_STOPITER 'S'
21 #define TYPE_ELLIPSIS '.'
23 #define TYPE_INT64 'I'
24 #define TYPE_FLOAT 'f'
25 #define TYPE_COMPLEX 'x'
27 #define TYPE_STRING 's'
28 #define TYPE_TUPLE '('
32 #define TYPE_UNICODE 'u'
33 #define TYPE_UNKNOWN '?'
39 /* If fp == NULL, the following are valid: */
45 #define w_byte(c, p) if (((p)->fp)) putc((c), (p)->fp); \
46 else if ((p)->ptr != (p)->end) *(p)->ptr++ = (c); \
50 w_more(int c
, WFILE
*p
)
54 return; /* An error already occurred */
55 size
= PyString_Size(p
->str
);
56 newsize
= size
+ 1024;
57 if (_PyString_Resize(&p
->str
, newsize
) != 0) {
58 p
->ptr
= p
->end
= NULL
;
61 p
->ptr
= PyString_AS_STRING((PyStringObject
*)p
->str
) + size
;
63 PyString_AS_STRING((PyStringObject
*)p
->str
) + newsize
;
64 *p
->ptr
++ = Py_SAFE_DOWNCAST(c
, int, char);
69 w_string(char *s
, int n
, WFILE
*p
)
72 fwrite(s
, 1, n
, p
->fp
);
83 w_short(int x
, WFILE
*p
)
86 w_byte((x
>> 8) & 0xff, p
);
90 w_long(long x
, WFILE
*p
)
92 w_byte((int)( x
& 0xff), p
);
93 w_byte((int)((x
>> 8) & 0xff), p
);
94 w_byte((int)((x
>>16) & 0xff), p
);
95 w_byte((int)((x
>>24) & 0xff), p
);
100 w_long64(long x
, WFILE
*p
)
108 w_object(PyObject
*v
, WFILE
*p
)
115 if (p
->depth
> MAX_MARSHAL_STACK_DEPTH
) {
118 else if (v
== NULL
) {
119 w_byte(TYPE_NULL
, p
);
121 else if (v
== Py_None
) {
122 w_byte(TYPE_NONE
, p
);
124 else if (v
== PyExc_StopIteration
) {
125 w_byte(TYPE_STOPITER
, p
);
127 else if (v
== Py_Ellipsis
) {
128 w_byte(TYPE_ELLIPSIS
, p
);
130 else if (PyInt_Check(v
)) {
131 long x
= PyInt_AS_LONG((PyIntObject
*)v
);
133 long y
= Py_ARITHMETIC_RIGHT_SHIFT(long, x
, 31);
135 w_byte(TYPE_INT64
, p
);
145 else if (PyLong_Check(v
)) {
146 PyLongObject
*ob
= (PyLongObject
*)v
;
147 w_byte(TYPE_LONG
, p
);
152 for (i
= 0; i
< n
; i
++)
153 w_short(ob
->ob_digit
[i
], p
);
155 else if (PyFloat_Check(v
)) {
156 char buf
[256]; /* Plenty to format any double */
157 PyFloat_AsReprString(buf
, (PyFloatObject
*)v
);
159 w_byte(TYPE_FLOAT
, p
);
163 #ifndef WITHOUT_COMPLEX
164 else if (PyComplex_Check(v
)) {
165 char buf
[256]; /* Plenty to format any double */
167 w_byte(TYPE_COMPLEX
, p
);
168 temp
= (PyFloatObject
*)PyFloat_FromDouble(
169 PyComplex_RealAsDouble(v
));
170 PyFloat_AsReprString(buf
, temp
);
175 temp
= (PyFloatObject
*)PyFloat_FromDouble(
176 PyComplex_ImagAsDouble(v
));
177 PyFloat_AsReprString(buf
, temp
);
184 else if (PyString_Check(v
)) {
185 w_byte(TYPE_STRING
, p
);
186 n
= PyString_GET_SIZE(v
);
188 w_string(PyString_AS_STRING(v
), n
, p
);
190 else if (PyUnicode_Check(v
)) {
192 utf8
= PyUnicode_AsUTF8String(v
);
198 w_byte(TYPE_UNICODE
, p
);
199 n
= PyString_GET_SIZE(utf8
);
201 w_string(PyString_AS_STRING(utf8
), n
, p
);
204 else if (PyTuple_Check(v
)) {
205 w_byte(TYPE_TUPLE
, p
);
208 for (i
= 0; i
< n
; i
++) {
209 w_object(PyTuple_GET_ITEM(v
, i
), p
);
212 else if (PyList_Check(v
)) {
213 w_byte(TYPE_LIST
, p
);
214 n
= PyList_GET_SIZE(v
);
216 for (i
= 0; i
< n
; i
++) {
217 w_object(PyList_GET_ITEM(v
, i
), p
);
220 else if (PyDict_Check(v
)) {
222 PyObject
*key
, *value
;
223 w_byte(TYPE_DICT
, p
);
224 /* This one is NULL object terminated! */
226 while (PyDict_Next(v
, &pos
, &key
, &value
)) {
230 w_object((PyObject
*)NULL
, p
);
232 else if (PyCode_Check(v
)) {
233 PyCodeObject
*co
= (PyCodeObject
*)v
;
234 w_byte(TYPE_CODE
, p
);
235 w_short(co
->co_argcount
, p
);
236 w_short(co
->co_nlocals
, p
);
237 w_short(co
->co_stacksize
, p
);
238 w_short(co
->co_flags
, p
);
239 w_object(co
->co_code
, p
);
240 w_object(co
->co_consts
, p
);
241 w_object(co
->co_names
, p
);
242 w_object(co
->co_varnames
, p
);
243 w_object(co
->co_freevars
, p
);
244 w_object(co
->co_cellvars
, p
);
245 w_object(co
->co_filename
, p
);
246 w_object(co
->co_name
, p
);
247 w_short(co
->co_firstlineno
, p
);
248 w_object(co
->co_lnotab
, p
);
250 else if ((pb
= v
->ob_type
->tp_as_buffer
) != NULL
&&
251 pb
->bf_getsegcount
!= NULL
&&
252 pb
->bf_getreadbuffer
!= NULL
&&
253 (*pb
->bf_getsegcount
)(v
, NULL
) == 1)
255 /* Write unknown buffer-style objects as a string */
257 w_byte(TYPE_STRING
, p
);
258 n
= (*pb
->bf_getreadbuffer
)(v
, 0, (void **)&s
);
263 w_byte(TYPE_UNKNOWN
, p
);
271 PyMarshal_WriteLongToFile(long x
, FILE *fp
)
281 PyMarshal_WriteObjectToFile(PyObject
*x
, FILE *fp
)
290 typedef WFILE RFILE
; /* Same struct with different invariants */
292 #define rs_byte(p) (((p)->ptr != (p)->end) ? (unsigned char)*(p)->ptr++ : EOF)
294 #define r_byte(p) ((p)->fp ? getc((p)->fp) : rs_byte(p))
297 r_string(char *s
, int n
, RFILE
*p
)
300 return fread(s
, 1, n
, p
->fp
);
301 if (p
->end
- p
->ptr
< n
)
303 memcpy(s
, p
->ptr
, n
);
314 /* Sign-extension, in case short greater than 16 bits */
323 register FILE *fp
= p
->fp
;
326 x
|= (long)getc(fp
) << 8;
327 x
|= (long)getc(fp
) << 16;
328 x
|= (long)getc(fp
) << 24;
332 x
|= (long)rs_byte(p
) << 8;
333 x
|= (long)rs_byte(p
) << 16;
334 x
|= (long)rs_byte(p
) << 24;
337 /* Sign extension for 64-bit machines */
338 x
|= -(x
& 0x80000000L
);
349 x
= (x
& 0xFFFFFFFFL
) | (r_long(p
) << 32);
351 if (r_long(p
) != 0) {
352 PyObject
*f
= PySys_GetObject("stderr");
354 (void) PyFile_WriteString(
355 "Warning: un-marshal 64-bit int in 32-bit mode\n",
367 int type
= r_byte(p
);
372 PyErr_SetString(PyExc_EOFError
,
373 "EOF read where object expected");
384 Py_INCREF(PyExc_StopIteration
);
385 return PyExc_StopIteration
;
388 Py_INCREF(Py_Ellipsis
);
392 return PyInt_FromLong(r_long(p
));
395 return PyInt_FromLong(r_long64(p
));
403 ob
= _PyLong_New(size
);
407 for (i
= 0; i
< size
; i
++)
408 ob
->ob_digit
[i
] = r_short(p
);
409 return (PyObject
*)ob
;
417 if (r_string(buf
, (int)n
, p
) != n
) {
418 PyErr_SetString(PyExc_EOFError
,
419 "EOF read where object expected");
423 PyFPE_START_PROTECT("atof", return 0)
425 PyFPE_END_PROTECT(dx
)
426 return PyFloat_FromDouble(dx
);
429 #ifndef WITHOUT_COMPLEX
435 if (r_string(buf
, (int)n
, p
) != n
) {
436 PyErr_SetString(PyExc_EOFError
,
437 "EOF read where object expected");
441 PyFPE_START_PROTECT("atof", return 0)
445 if (r_string(buf
, (int)n
, p
) != n
) {
446 PyErr_SetString(PyExc_EOFError
,
447 "EOF read where object expected");
451 PyFPE_START_PROTECT("atof", return 0)
454 return PyComplex_FromCComplex(c
);
461 PyErr_SetString(PyExc_ValueError
, "bad marshal data");
464 v
= PyString_FromStringAndSize((char *)NULL
, n
);
466 if (r_string(PyString_AS_STRING(v
), (int)n
, p
) != n
) {
469 PyErr_SetString(PyExc_EOFError
,
470 "EOF read where object expected");
481 PyErr_SetString(PyExc_ValueError
, "bad marshal data");
484 buffer
= PyMem_NEW(char, n
);
486 return PyErr_NoMemory();
487 if (r_string(buffer
, (int)n
, p
) != n
) {
489 PyErr_SetString(PyExc_EOFError
,
490 "EOF read where object expected");
493 v
= PyUnicode_DecodeUTF8(buffer
, n
, NULL
);
501 PyErr_SetString(PyExc_ValueError
, "bad marshal data");
504 v
= PyTuple_New((int)n
);
507 for (i
= 0; i
< n
; i
++) {
514 PyTuple_SET_ITEM(v
, (int)i
, v2
);
521 PyErr_SetString(PyExc_ValueError
, "bad marshal data");
524 v
= PyList_New((int)n
);
527 for (i
= 0; i
< n
; i
++) {
534 PyList_SetItem(v
, (int)i
, v2
);
546 break; /* XXX Assume TYPE_NULL, not an error */
549 PyDict_SetItem(v
, key
, val
);
557 int argcount
= r_short(p
);
558 int nlocals
= r_short(p
);
559 int stacksize
= r_short(p
);
560 int flags
= r_short(p
);
561 PyObject
*code
= NULL
;
562 PyObject
*consts
= NULL
;
563 PyObject
*names
= NULL
;
564 PyObject
*varnames
= NULL
;
565 PyObject
*freevars
= NULL
;
566 PyObject
*cellvars
= NULL
;
567 PyObject
*filename
= NULL
;
568 PyObject
*name
= NULL
;
570 PyObject
*lnotab
= NULL
;
573 if (code
) consts
= r_object(p
);
574 if (consts
) names
= r_object(p
);
575 if (names
) varnames
= r_object(p
);
576 if (varnames
) freevars
= r_object(p
);
577 if (freevars
) cellvars
= r_object(p
);
578 if (cellvars
) filename
= r_object(p
);
579 if (filename
) name
= r_object(p
);
581 firstlineno
= r_short(p
);
582 lnotab
= r_object(p
);
585 if (!PyErr_Occurred()) {
586 v
= (PyObject
*) PyCode_New(
587 argcount
, nlocals
, stacksize
, flags
,
588 code
, consts
, names
, varnames
,
589 freevars
, cellvars
, filename
, name
,
590 firstlineno
, lnotab
);
597 Py_XDECREF(varnames
);
598 Py_XDECREF(freevars
);
599 Py_XDECREF(cellvars
);
600 Py_XDECREF(filename
);
608 /* Bogus data got written, which isn't ideal.
609 This will let you keep working and recover. */
610 PyErr_SetString(PyExc_ValueError
, "bad marshal data");
617 PyMarshal_ReadLongFromFile(FILE *fp
)
625 /* Return size of file in bytes; < 0 if unknown. */
627 getfilesize(FILE *fp
)
630 if (fstat(fileno(fp
), &st
) != 0)
637 /* If we can get the size of the file up-front, and it's reasonably small,
638 * read it in one gulp and delegate to ...FromString() instead. Much quicker
639 * than reading a byte at a time from file; speeds .pyc imports.
640 * CAUTION: since this may read the entire remainder of the file, don't
641 * call it unless you know you're done with the file.
644 PyMarshal_ReadLastObjectFromFile(FILE *fp
)
646 /* 75% of 2.1's .pyc files can exploit SMALL_FILE_LIMIT.
647 * REASONABLE_FILE_LIMIT is by defn something big enough for Tkinter.pyc.
649 #define SMALL_FILE_LIMIT (1L << 14)
650 #define REASONABLE_FILE_LIMIT (1L << 18)
654 if (PyErr_Occurred()) {
655 fprintf(stderr
, "XXX rd_object called with exception set\n");
659 filesize
= getfilesize(fp
);
661 char buf
[SMALL_FILE_LIMIT
];
663 if (filesize
<= SMALL_FILE_LIMIT
)
665 else if (filesize
<= REASONABLE_FILE_LIMIT
)
666 pBuf
= (char *)PyMem_MALLOC(filesize
);
669 size_t n
= fread(pBuf
, 1, filesize
, fp
);
670 v
= PyMarshal_ReadObjectFromString(pBuf
, n
);
678 /* We don't have fstat, or we do but the file is larger than
679 * REASONABLE_FILE_LIMIT or malloc failed -- read a byte at a time.
681 return PyMarshal_ReadObjectFromFile(fp
);
683 #undef SMALL_FILE_LIMIT
684 #undef REASONABLE_FILE_LIMIT
688 PyMarshal_ReadObjectFromFile(FILE *fp
)
691 if (PyErr_Occurred()) {
692 fprintf(stderr
, "XXX rd_object called with exception set\n");
696 return r_object(&rf
);
700 PyMarshal_ReadObjectFromString(char *str
, int len
)
703 if (PyErr_Occurred()) {
704 fprintf(stderr
, "XXX rds_object called with exception set\n");
711 return r_object(&rf
);
715 PyMarshal_WriteObjectToString(PyObject
*x
) /* wrs_object() */
719 wf
.str
= PyString_FromStringAndSize((char *)NULL
, 50);
722 wf
.ptr
= PyString_AS_STRING((PyStringObject
*)wf
.str
);
723 wf
.end
= wf
.ptr
+ PyString_Size(wf
.str
);
728 _PyString_Resize(&wf
.str
,
730 PyString_AS_STRING((PyStringObject
*)wf
.str
)));
733 PyErr_SetString(PyExc_ValueError
,
734 (wf
.error
==1)?"unmarshallable object"
735 :"object too deeply nested to marshal");
741 /* And an interface for Python programs... */
744 marshal_dump(PyObject
*self
, PyObject
*args
)
749 if (!PyArg_ParseTuple(args
, "OO:dump", &x
, &f
))
751 if (!PyFile_Check(f
)) {
752 PyErr_SetString(PyExc_TypeError
,
753 "marshal.dump() 2nd arg must be file");
756 wf
.fp
= PyFile_AsFile(f
);
758 wf
.ptr
= wf
.end
= NULL
;
763 PyErr_SetString(PyExc_ValueError
,
764 (wf
.error
==1)?"unmarshallable object"
765 :"object too deeply nested to marshal");
773 marshal_load(PyObject
*self
, PyObject
*args
)
778 if (!PyArg_ParseTuple(args
, "O:load", &f
))
780 if (!PyFile_Check(f
)) {
781 PyErr_SetString(PyExc_TypeError
,
782 "marshal.load() arg must be file");
785 rf
.fp
= PyFile_AsFile(f
);
787 rf
.ptr
= rf
.end
= NULL
;
790 if (PyErr_Occurred()) {
798 marshal_dumps(PyObject
*self
, PyObject
*args
)
801 if (!PyArg_ParseTuple(args
, "O:dumps", &x
))
803 return PyMarshal_WriteObjectToString(x
);
807 marshal_loads(PyObject
*self
, PyObject
*args
)
813 if (!PyArg_ParseTuple(args
, "s#:loads", &s
, &n
))
821 if (PyErr_Occurred()) {
828 static PyMethodDef marshal_methods
[] = {
829 {"dump", marshal_dump
, 1},
830 {"load", marshal_load
, 1},
831 {"dumps", marshal_dumps
, 1},
832 {"loads", marshal_loads
, 1},
833 {NULL
, NULL
} /* sentinel */
839 (void) Py_InitModule("marshal", marshal_methods
);