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 #ifdef Py_USING_UNICODE
191 else if (PyUnicode_Check(v
)) {
193 utf8
= PyUnicode_AsUTF8String(v
);
199 w_byte(TYPE_UNICODE
, p
);
200 n
= PyString_GET_SIZE(utf8
);
202 w_string(PyString_AS_STRING(utf8
), n
, p
);
206 else if (PyTuple_Check(v
)) {
207 w_byte(TYPE_TUPLE
, p
);
210 for (i
= 0; i
< n
; i
++) {
211 w_object(PyTuple_GET_ITEM(v
, i
), p
);
214 else if (PyList_Check(v
)) {
215 w_byte(TYPE_LIST
, p
);
216 n
= PyList_GET_SIZE(v
);
218 for (i
= 0; i
< n
; i
++) {
219 w_object(PyList_GET_ITEM(v
, i
), p
);
222 else if (PyDict_Check(v
)) {
224 PyObject
*key
, *value
;
225 w_byte(TYPE_DICT
, p
);
226 /* This one is NULL object terminated! */
228 while (PyDict_Next(v
, &pos
, &key
, &value
)) {
232 w_object((PyObject
*)NULL
, p
);
234 else if (PyCode_Check(v
)) {
235 PyCodeObject
*co
= (PyCodeObject
*)v
;
236 w_byte(TYPE_CODE
, p
);
237 w_short(co
->co_argcount
, p
);
238 w_short(co
->co_nlocals
, p
);
239 w_short(co
->co_stacksize
, p
);
240 w_short(co
->co_flags
, p
);
241 w_object(co
->co_code
, p
);
242 w_object(co
->co_consts
, p
);
243 w_object(co
->co_names
, p
);
244 w_object(co
->co_varnames
, p
);
245 w_object(co
->co_freevars
, p
);
246 w_object(co
->co_cellvars
, p
);
247 w_object(co
->co_filename
, p
);
248 w_object(co
->co_name
, p
);
249 w_short(co
->co_firstlineno
, p
);
250 w_object(co
->co_lnotab
, p
);
252 else if ((pb
= v
->ob_type
->tp_as_buffer
) != NULL
&&
253 pb
->bf_getsegcount
!= NULL
&&
254 pb
->bf_getreadbuffer
!= NULL
&&
255 (*pb
->bf_getsegcount
)(v
, NULL
) == 1)
257 /* Write unknown buffer-style objects as a string */
259 w_byte(TYPE_STRING
, p
);
260 n
= (*pb
->bf_getreadbuffer
)(v
, 0, (void **)&s
);
265 w_byte(TYPE_UNKNOWN
, p
);
273 PyMarshal_WriteLongToFile(long x
, FILE *fp
)
283 PyMarshal_WriteObjectToFile(PyObject
*x
, FILE *fp
)
292 typedef WFILE RFILE
; /* Same struct with different invariants */
294 #define rs_byte(p) (((p)->ptr != (p)->end) ? (unsigned char)*(p)->ptr++ : EOF)
296 #define r_byte(p) ((p)->fp ? getc((p)->fp) : rs_byte(p))
299 r_string(char *s
, int n
, RFILE
*p
)
302 return fread(s
, 1, n
, p
->fp
);
303 if (p
->end
- p
->ptr
< n
)
305 memcpy(s
, p
->ptr
, n
);
316 /* Sign-extension, in case short greater than 16 bits */
325 register FILE *fp
= p
->fp
;
328 x
|= (long)getc(fp
) << 8;
329 x
|= (long)getc(fp
) << 16;
330 x
|= (long)getc(fp
) << 24;
334 x
|= (long)rs_byte(p
) << 8;
335 x
|= (long)rs_byte(p
) << 16;
336 x
|= (long)rs_byte(p
) << 24;
339 /* Sign extension for 64-bit machines */
340 x
|= -(x
& 0x80000000L
);
345 /* r_long64 deals with the TYPE_INT64 code. On a machine with
346 sizeof(long) > 4, it returns a Python int object, else a Python long
347 object. Note that w_long64 writes out TYPE_INT if 32 bits is enough,
348 so there's no inefficiency here in returning a PyLong on 32-bit boxes
349 for everything written via TYPE_INT64 (i.e., if an int is written via
350 TYPE_INT64, it *needs* more than 32 bits).
355 long lo4
= r_long(p
);
356 long hi4
= r_long(p
);
358 long x
= (hi4
<< 32) | (lo4
& 0xFFFFFFFFL
);
359 return PyInt_FromLong(x
);
361 unsigned char buf
[8];
363 int is_little_endian
= (int)*(char*)&one
;
364 if (is_little_endian
) {
365 memcpy(buf
, &lo4
, 4);
366 memcpy(buf
+4, &hi4
, 4);
369 memcpy(buf
, &hi4
, 4);
370 memcpy(buf
+4, &lo4
, 4);
372 return _PyLong_FromByteArray(buf
, 8, is_little_endian
, 1);
381 int type
= r_byte(p
);
386 PyErr_SetString(PyExc_EOFError
,
387 "EOF read where object expected");
398 Py_INCREF(PyExc_StopIteration
);
399 return PyExc_StopIteration
;
402 Py_INCREF(Py_Ellipsis
);
406 return PyInt_FromLong(r_long(p
));
417 ob
= _PyLong_New(size
);
421 for (i
= 0; i
< size
; i
++)
422 ob
->ob_digit
[i
] = r_short(p
);
423 return (PyObject
*)ob
;
431 if (r_string(buf
, (int)n
, p
) != n
) {
432 PyErr_SetString(PyExc_EOFError
,
433 "EOF read where object expected");
437 PyFPE_START_PROTECT("atof", return 0)
439 PyFPE_END_PROTECT(dx
)
440 return PyFloat_FromDouble(dx
);
443 #ifndef WITHOUT_COMPLEX
449 if (r_string(buf
, (int)n
, p
) != n
) {
450 PyErr_SetString(PyExc_EOFError
,
451 "EOF read where object expected");
455 PyFPE_START_PROTECT("atof", return 0)
459 if (r_string(buf
, (int)n
, p
) != n
) {
460 PyErr_SetString(PyExc_EOFError
,
461 "EOF read where object expected");
465 PyFPE_START_PROTECT("atof", return 0)
468 return PyComplex_FromCComplex(c
);
475 PyErr_SetString(PyExc_ValueError
, "bad marshal data");
478 v
= PyString_FromStringAndSize((char *)NULL
, n
);
480 if (r_string(PyString_AS_STRING(v
), (int)n
, p
) != n
) {
483 PyErr_SetString(PyExc_EOFError
,
484 "EOF read where object expected");
489 #ifdef Py_USING_UNICODE
496 PyErr_SetString(PyExc_ValueError
, "bad marshal data");
499 buffer
= PyMem_NEW(char, n
);
501 return PyErr_NoMemory();
502 if (r_string(buffer
, (int)n
, p
) != n
) {
504 PyErr_SetString(PyExc_EOFError
,
505 "EOF read where object expected");
508 v
= PyUnicode_DecodeUTF8(buffer
, n
, NULL
);
517 PyErr_SetString(PyExc_ValueError
, "bad marshal data");
520 v
= PyTuple_New((int)n
);
523 for (i
= 0; i
< n
; i
++) {
530 PyTuple_SET_ITEM(v
, (int)i
, v2
);
537 PyErr_SetString(PyExc_ValueError
, "bad marshal data");
540 v
= PyList_New((int)n
);
543 for (i
= 0; i
< n
; i
++) {
550 PyList_SetItem(v
, (int)i
, v2
);
562 break; /* XXX Assume TYPE_NULL, not an error */
565 PyDict_SetItem(v
, key
, val
);
572 if (PyEval_GetRestricted()) {
573 PyErr_SetString(PyExc_RuntimeError
,
574 "cannot unmarshal code objects in "
575 "restricted execution mode");
579 int argcount
= r_short(p
);
580 int nlocals
= r_short(p
);
581 int stacksize
= r_short(p
);
582 int flags
= r_short(p
);
583 PyObject
*code
= NULL
;
584 PyObject
*consts
= NULL
;
585 PyObject
*names
= NULL
;
586 PyObject
*varnames
= NULL
;
587 PyObject
*freevars
= NULL
;
588 PyObject
*cellvars
= NULL
;
589 PyObject
*filename
= NULL
;
590 PyObject
*name
= NULL
;
592 PyObject
*lnotab
= NULL
;
595 if (code
) consts
= r_object(p
);
596 if (consts
) names
= r_object(p
);
597 if (names
) varnames
= r_object(p
);
598 if (varnames
) freevars
= r_object(p
);
599 if (freevars
) cellvars
= r_object(p
);
600 if (cellvars
) filename
= r_object(p
);
601 if (filename
) name
= r_object(p
);
603 firstlineno
= r_short(p
);
604 lnotab
= r_object(p
);
607 if (!PyErr_Occurred()) {
608 v
= (PyObject
*) PyCode_New(
609 argcount
, nlocals
, stacksize
, flags
,
610 code
, consts
, names
, varnames
,
611 freevars
, cellvars
, filename
, name
,
612 firstlineno
, lnotab
);
619 Py_XDECREF(varnames
);
620 Py_XDECREF(freevars
);
621 Py_XDECREF(cellvars
);
622 Py_XDECREF(filename
);
630 /* Bogus data got written, which isn't ideal.
631 This will let you keep working and recover. */
632 PyErr_SetString(PyExc_ValueError
, "bad marshal data");
639 PyMarshal_ReadLongFromFile(FILE *fp
)
647 /* Return size of file in bytes; < 0 if unknown. */
649 getfilesize(FILE *fp
)
652 if (fstat(fileno(fp
), &st
) != 0)
659 /* If we can get the size of the file up-front, and it's reasonably small,
660 * read it in one gulp and delegate to ...FromString() instead. Much quicker
661 * than reading a byte at a time from file; speeds .pyc imports.
662 * CAUTION: since this may read the entire remainder of the file, don't
663 * call it unless you know you're done with the file.
666 PyMarshal_ReadLastObjectFromFile(FILE *fp
)
668 /* 75% of 2.1's .pyc files can exploit SMALL_FILE_LIMIT.
669 * REASONABLE_FILE_LIMIT is by defn something big enough for Tkinter.pyc.
671 #define SMALL_FILE_LIMIT (1L << 14)
672 #define REASONABLE_FILE_LIMIT (1L << 18)
676 if (PyErr_Occurred()) {
677 fprintf(stderr
, "XXX rd_object called with exception set\n");
681 filesize
= getfilesize(fp
);
683 char buf
[SMALL_FILE_LIMIT
];
685 if (filesize
<= SMALL_FILE_LIMIT
)
687 else if (filesize
<= REASONABLE_FILE_LIMIT
)
688 pBuf
= (char *)PyMem_MALLOC(filesize
);
691 size_t n
= fread(pBuf
, 1, filesize
, fp
);
692 v
= PyMarshal_ReadObjectFromString(pBuf
, n
);
700 /* We don't have fstat, or we do but the file is larger than
701 * REASONABLE_FILE_LIMIT or malloc failed -- read a byte at a time.
703 return PyMarshal_ReadObjectFromFile(fp
);
705 #undef SMALL_FILE_LIMIT
706 #undef REASONABLE_FILE_LIMIT
710 PyMarshal_ReadObjectFromFile(FILE *fp
)
713 if (PyErr_Occurred()) {
714 fprintf(stderr
, "XXX rd_object called with exception set\n");
718 return r_object(&rf
);
722 PyMarshal_ReadObjectFromString(char *str
, int len
)
725 if (PyErr_Occurred()) {
726 fprintf(stderr
, "XXX rds_object called with exception set\n");
733 return r_object(&rf
);
737 PyMarshal_WriteObjectToString(PyObject
*x
) /* wrs_object() */
741 wf
.str
= PyString_FromStringAndSize((char *)NULL
, 50);
744 wf
.ptr
= PyString_AS_STRING((PyStringObject
*)wf
.str
);
745 wf
.end
= wf
.ptr
+ PyString_Size(wf
.str
);
750 _PyString_Resize(&wf
.str
,
752 PyString_AS_STRING((PyStringObject
*)wf
.str
)));
755 PyErr_SetString(PyExc_ValueError
,
756 (wf
.error
==1)?"unmarshallable object"
757 :"object too deeply nested to marshal");
763 /* And an interface for Python programs... */
766 marshal_dump(PyObject
*self
, PyObject
*args
)
771 if (!PyArg_ParseTuple(args
, "OO:dump", &x
, &f
))
773 if (!PyFile_Check(f
)) {
774 PyErr_SetString(PyExc_TypeError
,
775 "marshal.dump() 2nd arg must be file");
778 wf
.fp
= PyFile_AsFile(f
);
780 wf
.ptr
= wf
.end
= NULL
;
785 PyErr_SetString(PyExc_ValueError
,
786 (wf
.error
==1)?"unmarshallable object"
787 :"object too deeply nested to marshal");
795 marshal_load(PyObject
*self
, PyObject
*args
)
800 if (!PyArg_ParseTuple(args
, "O:load", &f
))
802 if (!PyFile_Check(f
)) {
803 PyErr_SetString(PyExc_TypeError
,
804 "marshal.load() arg must be file");
807 rf
.fp
= PyFile_AsFile(f
);
809 rf
.ptr
= rf
.end
= NULL
;
812 if (PyErr_Occurred()) {
820 marshal_dumps(PyObject
*self
, PyObject
*args
)
823 if (!PyArg_ParseTuple(args
, "O:dumps", &x
))
825 return PyMarshal_WriteObjectToString(x
);
829 marshal_loads(PyObject
*self
, PyObject
*args
)
835 if (!PyArg_ParseTuple(args
, "s#:loads", &s
, &n
))
843 if (PyErr_Occurred()) {
850 static PyMethodDef marshal_methods
[] = {
851 {"dump", marshal_dump
, 1},
852 {"load", marshal_load
, 1},
853 {"dumps", marshal_dumps
, 1},
854 {"loads", marshal_loads
, 1},
855 {NULL
, NULL
} /* sentinel */
861 (void) Py_InitModule("marshal", marshal_methods
);