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
)
114 if (p
->depth
> MAX_MARSHAL_STACK_DEPTH
) {
117 else if (v
== NULL
) {
118 w_byte(TYPE_NULL
, p
);
120 else if (v
== Py_None
) {
121 w_byte(TYPE_NONE
, p
);
123 else if (v
== PyExc_StopIteration
) {
124 w_byte(TYPE_STOPITER
, p
);
126 else if (v
== Py_Ellipsis
) {
127 w_byte(TYPE_ELLIPSIS
, p
);
129 else if (PyInt_Check(v
)) {
130 long x
= PyInt_AS_LONG((PyIntObject
*)v
);
132 long y
= Py_ARITHMETIC_RIGHT_SHIFT(long, x
, 31);
134 w_byte(TYPE_INT64
, p
);
144 else if (PyLong_Check(v
)) {
145 PyLongObject
*ob
= (PyLongObject
*)v
;
146 w_byte(TYPE_LONG
, p
);
151 for (i
= 0; i
< n
; i
++)
152 w_short(ob
->ob_digit
[i
], p
);
154 else if (PyFloat_Check(v
)) {
155 char buf
[256]; /* Plenty to format any double */
156 PyFloat_AsReprString(buf
, (PyFloatObject
*)v
);
158 w_byte(TYPE_FLOAT
, p
);
162 #ifndef WITHOUT_COMPLEX
163 else if (PyComplex_Check(v
)) {
164 char buf
[256]; /* Plenty to format any double */
166 w_byte(TYPE_COMPLEX
, p
);
167 temp
= (PyFloatObject
*)PyFloat_FromDouble(
168 PyComplex_RealAsDouble(v
));
169 PyFloat_AsReprString(buf
, temp
);
174 temp
= (PyFloatObject
*)PyFloat_FromDouble(
175 PyComplex_ImagAsDouble(v
));
176 PyFloat_AsReprString(buf
, temp
);
183 else if (PyString_Check(v
)) {
184 w_byte(TYPE_STRING
, p
);
185 n
= PyString_GET_SIZE(v
);
187 w_string(PyString_AS_STRING(v
), n
, p
);
189 #ifdef Py_USING_UNICODE
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
);
205 else if (PyTuple_Check(v
)) {
206 w_byte(TYPE_TUPLE
, p
);
209 for (i
= 0; i
< n
; i
++) {
210 w_object(PyTuple_GET_ITEM(v
, i
), p
);
213 else if (PyList_Check(v
)) {
214 w_byte(TYPE_LIST
, p
);
215 n
= PyList_GET_SIZE(v
);
217 for (i
= 0; i
< n
; i
++) {
218 w_object(PyList_GET_ITEM(v
, i
), p
);
221 else if (PyDict_Check(v
)) {
223 PyObject
*key
, *value
;
224 w_byte(TYPE_DICT
, p
);
225 /* This one is NULL object terminated! */
227 while (PyDict_Next(v
, &pos
, &key
, &value
)) {
231 w_object((PyObject
*)NULL
, p
);
233 else if (PyCode_Check(v
)) {
234 PyCodeObject
*co
= (PyCodeObject
*)v
;
235 w_byte(TYPE_CODE
, p
);
236 w_short(co
->co_argcount
, p
);
237 w_short(co
->co_nlocals
, p
);
238 w_short(co
->co_stacksize
, p
);
239 w_short(co
->co_flags
, p
);
240 w_object(co
->co_code
, p
);
241 w_object(co
->co_consts
, p
);
242 w_object(co
->co_names
, p
);
243 w_object(co
->co_varnames
, p
);
244 w_object(co
->co_freevars
, p
);
245 w_object(co
->co_cellvars
, p
);
246 w_object(co
->co_filename
, p
);
247 w_object(co
->co_name
, p
);
248 w_short(co
->co_firstlineno
, p
);
249 w_object(co
->co_lnotab
, p
);
251 else if (PyObject_CheckReadBuffer(v
)) {
252 /* Write unknown buffer-style objects as a string */
254 PyBufferProcs
*pb
= v
->ob_type
->tp_as_buffer
;
255 w_byte(TYPE_STRING
, p
);
256 n
= (*pb
->bf_getreadbuffer
)(v
, 0, (void **)&s
);
261 w_byte(TYPE_UNKNOWN
, p
);
269 PyMarshal_WriteLongToFile(long x
, FILE *fp
)
279 PyMarshal_WriteObjectToFile(PyObject
*x
, FILE *fp
)
288 typedef WFILE RFILE
; /* Same struct with different invariants */
290 #define rs_byte(p) (((p)->ptr != (p)->end) ? (unsigned char)*(p)->ptr++ : EOF)
292 #define r_byte(p) ((p)->fp ? getc((p)->fp) : rs_byte(p))
295 r_string(char *s
, int n
, RFILE
*p
)
298 return fread(s
, 1, n
, p
->fp
);
299 if (p
->end
- p
->ptr
< n
)
301 memcpy(s
, p
->ptr
, n
);
312 /* Sign-extension, in case short greater than 16 bits */
321 register FILE *fp
= p
->fp
;
324 x
|= (long)getc(fp
) << 8;
325 x
|= (long)getc(fp
) << 16;
326 x
|= (long)getc(fp
) << 24;
330 x
|= (long)rs_byte(p
) << 8;
331 x
|= (long)rs_byte(p
) << 16;
332 x
|= (long)rs_byte(p
) << 24;
335 /* Sign extension for 64-bit machines */
336 x
|= -(x
& 0x80000000L
);
341 /* r_long64 deals with the TYPE_INT64 code. On a machine with
342 sizeof(long) > 4, it returns a Python int object, else a Python long
343 object. Note that w_long64 writes out TYPE_INT if 32 bits is enough,
344 so there's no inefficiency here in returning a PyLong on 32-bit boxes
345 for everything written via TYPE_INT64 (i.e., if an int is written via
346 TYPE_INT64, it *needs* more than 32 bits).
351 long lo4
= r_long(p
);
352 long hi4
= r_long(p
);
354 long x
= (hi4
<< 32) | (lo4
& 0xFFFFFFFFL
);
355 return PyInt_FromLong(x
);
357 unsigned char buf
[8];
359 int is_little_endian
= (int)*(char*)&one
;
360 if (is_little_endian
) {
361 memcpy(buf
, &lo4
, 4);
362 memcpy(buf
+4, &hi4
, 4);
365 memcpy(buf
, &hi4
, 4);
366 memcpy(buf
+4, &lo4
, 4);
368 return _PyLong_FromByteArray(buf
, 8, is_little_endian
, 1);
377 int type
= r_byte(p
);
382 PyErr_SetString(PyExc_EOFError
,
383 "EOF read where object expected");
394 Py_INCREF(PyExc_StopIteration
);
395 return PyExc_StopIteration
;
398 Py_INCREF(Py_Ellipsis
);
402 return PyInt_FromLong(r_long(p
));
413 ob
= _PyLong_New(size
);
417 for (i
= 0; i
< size
; i
++)
418 ob
->ob_digit
[i
] = r_short(p
);
419 return (PyObject
*)ob
;
427 if (r_string(buf
, (int)n
, p
) != n
) {
428 PyErr_SetString(PyExc_EOFError
,
429 "EOF read where object expected");
433 PyFPE_START_PROTECT("atof", return 0)
435 PyFPE_END_PROTECT(dx
)
436 return PyFloat_FromDouble(dx
);
439 #ifndef WITHOUT_COMPLEX
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)
455 if (r_string(buf
, (int)n
, p
) != n
) {
456 PyErr_SetString(PyExc_EOFError
,
457 "EOF read where object expected");
461 PyFPE_START_PROTECT("atof", return 0)
464 return PyComplex_FromCComplex(c
);
471 PyErr_SetString(PyExc_ValueError
, "bad marshal data");
474 v
= PyString_FromStringAndSize((char *)NULL
, n
);
476 if (r_string(PyString_AS_STRING(v
), (int)n
, p
) != n
) {
479 PyErr_SetString(PyExc_EOFError
,
480 "EOF read where object expected");
485 #ifdef Py_USING_UNICODE
492 PyErr_SetString(PyExc_ValueError
, "bad marshal data");
495 buffer
= PyMem_NEW(char, n
);
497 return PyErr_NoMemory();
498 if (r_string(buffer
, (int)n
, p
) != n
) {
500 PyErr_SetString(PyExc_EOFError
,
501 "EOF read where object expected");
504 v
= PyUnicode_DecodeUTF8(buffer
, n
, NULL
);
513 PyErr_SetString(PyExc_ValueError
, "bad marshal data");
516 v
= PyTuple_New((int)n
);
519 for (i
= 0; i
< n
; i
++) {
526 PyTuple_SET_ITEM(v
, (int)i
, v2
);
533 PyErr_SetString(PyExc_ValueError
, "bad marshal data");
536 v
= PyList_New((int)n
);
539 for (i
= 0; i
< n
; i
++) {
546 PyList_SetItem(v
, (int)i
, v2
);
558 break; /* XXX Assume TYPE_NULL, not an error */
561 PyDict_SetItem(v
, key
, val
);
568 if (PyEval_GetRestricted()) {
569 PyErr_SetString(PyExc_RuntimeError
,
570 "cannot unmarshal code objects in "
571 "restricted execution mode");
575 int argcount
= r_short(p
);
576 int nlocals
= r_short(p
);
577 int stacksize
= r_short(p
);
578 int flags
= r_short(p
);
579 PyObject
*code
= NULL
;
580 PyObject
*consts
= NULL
;
581 PyObject
*names
= NULL
;
582 PyObject
*varnames
= NULL
;
583 PyObject
*freevars
= NULL
;
584 PyObject
*cellvars
= NULL
;
585 PyObject
*filename
= NULL
;
586 PyObject
*name
= NULL
;
588 PyObject
*lnotab
= NULL
;
591 if (code
) consts
= r_object(p
);
592 if (consts
) names
= r_object(p
);
593 if (names
) varnames
= r_object(p
);
594 if (varnames
) freevars
= r_object(p
);
595 if (freevars
) cellvars
= r_object(p
);
596 if (cellvars
) filename
= r_object(p
);
597 if (filename
) name
= r_object(p
);
599 firstlineno
= r_short(p
);
600 lnotab
= r_object(p
);
603 if (!PyErr_Occurred()) {
604 v
= (PyObject
*) PyCode_New(
605 argcount
, nlocals
, stacksize
, flags
,
606 code
, consts
, names
, varnames
,
607 freevars
, cellvars
, filename
, name
,
608 firstlineno
, lnotab
);
615 Py_XDECREF(varnames
);
616 Py_XDECREF(freevars
);
617 Py_XDECREF(cellvars
);
618 Py_XDECREF(filename
);
626 /* Bogus data got written, which isn't ideal.
627 This will let you keep working and recover. */
628 PyErr_SetString(PyExc_ValueError
, "bad marshal data");
635 PyMarshal_ReadShortFromFile(FILE *fp
)
643 PyMarshal_ReadLongFromFile(FILE *fp
)
651 /* Return size of file in bytes; < 0 if unknown. */
653 getfilesize(FILE *fp
)
656 if (fstat(fileno(fp
), &st
) != 0)
663 /* If we can get the size of the file up-front, and it's reasonably small,
664 * read it in one gulp and delegate to ...FromString() instead. Much quicker
665 * than reading a byte at a time from file; speeds .pyc imports.
666 * CAUTION: since this may read the entire remainder of the file, don't
667 * call it unless you know you're done with the file.
670 PyMarshal_ReadLastObjectFromFile(FILE *fp
)
672 /* 75% of 2.1's .pyc files can exploit SMALL_FILE_LIMIT.
673 * REASONABLE_FILE_LIMIT is by defn something big enough for Tkinter.pyc.
675 #define SMALL_FILE_LIMIT (1L << 14)
676 #define REASONABLE_FILE_LIMIT (1L << 18)
680 if (PyErr_Occurred()) {
681 fprintf(stderr
, "XXX rd_object called with exception set\n");
685 filesize
= getfilesize(fp
);
687 char buf
[SMALL_FILE_LIMIT
];
689 if (filesize
<= SMALL_FILE_LIMIT
)
691 else if (filesize
<= REASONABLE_FILE_LIMIT
)
692 pBuf
= (char *)PyMem_MALLOC(filesize
);
695 size_t n
= fread(pBuf
, 1, filesize
, fp
);
696 v
= PyMarshal_ReadObjectFromString(pBuf
, n
);
704 /* We don't have fstat, or we do but the file is larger than
705 * REASONABLE_FILE_LIMIT or malloc failed -- read a byte at a time.
707 return PyMarshal_ReadObjectFromFile(fp
);
709 #undef SMALL_FILE_LIMIT
710 #undef REASONABLE_FILE_LIMIT
714 PyMarshal_ReadObjectFromFile(FILE *fp
)
717 if (PyErr_Occurred()) {
718 fprintf(stderr
, "XXX rd_object called with exception set\n");
722 return r_object(&rf
);
726 PyMarshal_ReadObjectFromString(char *str
, int len
)
729 if (PyErr_Occurred()) {
730 fprintf(stderr
, "XXX rds_object called with exception set\n");
737 return r_object(&rf
);
741 PyMarshal_WriteObjectToString(PyObject
*x
) /* wrs_object() */
745 wf
.str
= PyString_FromStringAndSize((char *)NULL
, 50);
748 wf
.ptr
= PyString_AS_STRING((PyStringObject
*)wf
.str
);
749 wf
.end
= wf
.ptr
+ PyString_Size(wf
.str
);
754 _PyString_Resize(&wf
.str
,
756 PyString_AS_STRING((PyStringObject
*)wf
.str
)));
759 PyErr_SetString(PyExc_ValueError
,
760 (wf
.error
==1)?"unmarshallable object"
761 :"object too deeply nested to marshal");
767 /* And an interface for Python programs... */
770 marshal_dump(PyObject
*self
, PyObject
*args
)
775 if (!PyArg_ParseTuple(args
, "OO:dump", &x
, &f
))
777 if (!PyFile_Check(f
)) {
778 PyErr_SetString(PyExc_TypeError
,
779 "marshal.dump() 2nd arg must be file");
782 wf
.fp
= PyFile_AsFile(f
);
784 wf
.ptr
= wf
.end
= NULL
;
789 PyErr_SetString(PyExc_ValueError
,
790 (wf
.error
==1)?"unmarshallable object"
791 :"object too deeply nested to marshal");
799 marshal_load(PyObject
*self
, PyObject
*args
)
804 if (!PyArg_ParseTuple(args
, "O:load", &f
))
806 if (!PyFile_Check(f
)) {
807 PyErr_SetString(PyExc_TypeError
,
808 "marshal.load() arg must be file");
811 rf
.fp
= PyFile_AsFile(f
);
813 rf
.ptr
= rf
.end
= NULL
;
816 if (PyErr_Occurred()) {
824 marshal_dumps(PyObject
*self
, PyObject
*args
)
827 if (!PyArg_ParseTuple(args
, "O:dumps", &x
))
829 return PyMarshal_WriteObjectToString(x
);
833 marshal_loads(PyObject
*self
, PyObject
*args
)
839 if (!PyArg_ParseTuple(args
, "s#:loads", &s
, &n
))
847 if (PyErr_Occurred()) {
854 static PyMethodDef marshal_methods
[] = {
855 {"dump", marshal_dump
, 1},
856 {"load", marshal_load
, 1},
857 {"dumps", marshal_dumps
, 1},
858 {"loads", marshal_loads
, 1},
859 {NULL
, NULL
} /* sentinel */
865 (void) Py_InitModule("marshal", marshal_methods
);