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
10 #include "longintrepr.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.
19 * On Windows debug builds, reduce this value.
21 #if defined(MS_WINDOWS) && defined(_DEBUG)
22 #define MAX_MARSHAL_STACK_DEPTH 1500
24 #define MAX_MARSHAL_STACK_DEPTH 2000
29 #define TYPE_FALSE 'F'
31 #define TYPE_STOPITER 'S'
32 #define TYPE_ELLIPSIS '.'
34 #define TYPE_INT64 'I'
35 #define TYPE_FLOAT 'f'
36 #define TYPE_BINARY_FLOAT 'g'
37 #define TYPE_COMPLEX 'x'
38 #define TYPE_BINARY_COMPLEX 'y'
40 #define TYPE_STRING 's'
41 #define TYPE_TUPLE '('
45 #define TYPE_UNICODE 'u'
46 #define TYPE_UNKNOWN '?'
48 #define TYPE_FROZENSET '>'
51 #define WFERR_UNMARSHALLABLE 1
52 #define WFERR_NESTEDTOODEEP 2
53 #define WFERR_NOMEMORY 3
57 int error
; /* see WFERR_* values */
59 /* If fp == NULL, the following are valid: */
63 PyObject
*strings
; /* dict on marshal, list on unmarshal */
67 #define w_byte(c, p) if (((p)->fp)) putc((c), (p)->fp); \
68 else if ((p)->ptr != (p)->end) *(p)->ptr++ = (c); \
72 w_more(int c
, WFILE
*p
)
74 Py_ssize_t size
, newsize
;
76 return; /* An error already occurred */
77 size
= PyBytes_Size(p
->str
);
78 newsize
= size
+ size
+ 1024;
79 if (newsize
> 32*1024*1024) {
80 newsize
= size
+ (size
>> 3); /* 12.5% overallocation */
82 if (_PyBytes_Resize(&p
->str
, newsize
) != 0) {
83 p
->ptr
= p
->end
= NULL
;
86 p
->ptr
= PyBytes_AS_STRING((PyBytesObject
*)p
->str
) + size
;
88 PyBytes_AS_STRING((PyBytesObject
*)p
->str
) + newsize
;
89 *p
->ptr
++ = Py_SAFE_DOWNCAST(c
, int, char);
94 w_string(char *s
, int n
, WFILE
*p
)
97 fwrite(s
, 1, n
, p
->fp
);
108 w_short(int x
, WFILE
*p
)
110 w_byte((char)( x
& 0xff), p
);
111 w_byte((char)((x
>> 8) & 0xff), p
);
115 w_long(long x
, WFILE
*p
)
117 w_byte((char)( x
& 0xff), p
);
118 w_byte((char)((x
>> 8) & 0xff), p
);
119 w_byte((char)((x
>>16) & 0xff), p
);
120 w_byte((char)((x
>>24) & 0xff), p
);
125 w_long64(long x
, WFILE
*p
)
132 /* We assume that Python longs are stored internally in base some power of
133 2**15; for the sake of portability we'll always read and write them in base
136 #define PyLong_MARSHAL_SHIFT 15
137 #define PyLong_MARSHAL_BASE ((short)1 << PyLong_MARSHAL_SHIFT)
138 #define PyLong_MARSHAL_MASK (PyLong_MARSHAL_BASE - 1)
139 #if PyLong_SHIFT % PyLong_MARSHAL_SHIFT != 0
140 #error "PyLong_SHIFT must be a multiple of PyLong_MARSHAL_SHIFT"
142 #define PyLong_MARSHAL_RATIO (PyLong_SHIFT / PyLong_MARSHAL_SHIFT)
145 w_PyLong(const PyLongObject
*ob
, WFILE
*p
)
147 Py_ssize_t i
, j
, n
, l
;
150 w_byte(TYPE_LONG
, p
);
151 if (Py_SIZE(ob
) == 0) {
156 /* set l to number of base PyLong_MARSHAL_BASE digits */
157 n
= ABS(Py_SIZE(ob
));
158 l
= (n
-1) * PyLong_MARSHAL_RATIO
;
159 d
= ob
->ob_digit
[n
-1];
160 assert(d
!= 0); /* a PyLong is always normalized */
162 d
>>= PyLong_MARSHAL_SHIFT
;
165 w_long((long)(Py_SIZE(ob
) > 0 ? l
: -l
), p
);
167 for (i
=0; i
< n
-1; i
++) {
169 for (j
=0; j
< PyLong_MARSHAL_RATIO
; j
++) {
170 w_short(d
& PyLong_MARSHAL_MASK
, p
);
171 d
>>= PyLong_MARSHAL_SHIFT
;
175 d
= ob
->ob_digit
[n
-1];
177 w_short(d
& PyLong_MARSHAL_MASK
, p
);
178 d
>>= PyLong_MARSHAL_SHIFT
;
183 w_object(PyObject
*v
, WFILE
*p
)
189 if (p
->depth
> MAX_MARSHAL_STACK_DEPTH
) {
190 p
->error
= WFERR_NESTEDTOODEEP
;
192 else if (v
== NULL
) {
193 w_byte(TYPE_NULL
, p
);
195 else if (v
== Py_None
) {
196 w_byte(TYPE_NONE
, p
);
198 else if (v
== PyExc_StopIteration
) {
199 w_byte(TYPE_STOPITER
, p
);
201 else if (v
== Py_Ellipsis
) {
202 w_byte(TYPE_ELLIPSIS
, p
);
204 else if (v
== Py_False
) {
205 w_byte(TYPE_FALSE
, p
);
207 else if (v
== Py_True
) {
208 w_byte(TYPE_TRUE
, p
);
210 else if (PyLong_CheckExact(v
)) {
211 long x
= PyLong_AsLong(v
);
212 if ((x
== -1) && PyErr_Occurred()) {
213 PyLongObject
*ob
= (PyLongObject
*)v
;
219 long y
= Py_ARITHMETIC_RIGHT_SHIFT(long, x
, 31);
221 w_byte(TYPE_INT64
, p
);
232 else if (PyFloat_CheckExact(v
)) {
233 if (p
->version
> 1) {
234 unsigned char buf
[8];
235 if (_PyFloat_Pack8(PyFloat_AsDouble(v
),
237 p
->error
= WFERR_UNMARSHALLABLE
;
240 w_byte(TYPE_BINARY_FLOAT
, p
);
241 w_string((char*)buf
, 8, p
);
244 char *buf
= PyOS_double_to_string(PyFloat_AS_DOUBLE(v
),
247 p
->error
= WFERR_NOMEMORY
;
251 w_byte(TYPE_FLOAT
, p
);
253 w_string(buf
, (int)n
, p
);
257 #ifndef WITHOUT_COMPLEX
258 else if (PyComplex_CheckExact(v
)) {
259 if (p
->version
> 1) {
260 unsigned char buf
[8];
261 if (_PyFloat_Pack8(PyComplex_RealAsDouble(v
),
263 p
->error
= WFERR_UNMARSHALLABLE
;
266 w_byte(TYPE_BINARY_COMPLEX
, p
);
267 w_string((char*)buf
, 8, p
);
268 if (_PyFloat_Pack8(PyComplex_ImagAsDouble(v
),
270 p
->error
= WFERR_UNMARSHALLABLE
;
273 w_string((char*)buf
, 8, p
);
277 w_byte(TYPE_COMPLEX
, p
);
278 buf
= PyOS_double_to_string(PyComplex_RealAsDouble(v
),
281 p
->error
= WFERR_NOMEMORY
;
286 w_string(buf
, (int)n
, p
);
288 buf
= PyOS_double_to_string(PyComplex_ImagAsDouble(v
),
291 p
->error
= WFERR_NOMEMORY
;
296 w_string(buf
, (int)n
, p
);
301 else if (PyBytes_CheckExact(v
)) {
302 w_byte(TYPE_STRING
, p
);
303 n
= PyBytes_GET_SIZE(v
);
305 /* huge strings are not supported */
307 p
->error
= WFERR_UNMARSHALLABLE
;
311 w_string(PyBytes_AS_STRING(v
), (int)n
, p
);
313 else if (PyUnicode_CheckExact(v
)) {
315 utf8
= PyUnicode_EncodeUTF8(PyUnicode_AS_UNICODE(v
),
316 PyUnicode_GET_SIZE(v
),
320 p
->error
= WFERR_UNMARSHALLABLE
;
323 w_byte(TYPE_UNICODE
, p
);
324 n
= PyBytes_GET_SIZE(utf8
);
327 p
->error
= WFERR_UNMARSHALLABLE
;
331 w_string(PyBytes_AS_STRING(utf8
), (int)n
, p
);
334 else if (PyTuple_CheckExact(v
)) {
335 w_byte(TYPE_TUPLE
, p
);
338 for (i
= 0; i
< n
; i
++) {
339 w_object(PyTuple_GET_ITEM(v
, i
), p
);
342 else if (PyList_CheckExact(v
)) {
343 w_byte(TYPE_LIST
, p
);
344 n
= PyList_GET_SIZE(v
);
346 for (i
= 0; i
< n
; i
++) {
347 w_object(PyList_GET_ITEM(v
, i
), p
);
350 else if (PyDict_CheckExact(v
)) {
352 PyObject
*key
, *value
;
353 w_byte(TYPE_DICT
, p
);
354 /* This one is NULL object terminated! */
356 while (PyDict_Next(v
, &pos
, &key
, &value
)) {
360 w_object((PyObject
*)NULL
, p
);
362 else if (PyAnySet_CheckExact(v
)) {
363 PyObject
*value
, *it
;
365 if (PyObject_TypeCheck(v
, &PySet_Type
))
368 w_byte(TYPE_FROZENSET
, p
);
369 n
= PyObject_Size(v
);
372 p
->error
= WFERR_UNMARSHALLABLE
;
376 it
= PyObject_GetIter(v
);
379 p
->error
= WFERR_UNMARSHALLABLE
;
382 while ((value
= PyIter_Next(it
)) != NULL
) {
387 if (PyErr_Occurred()) {
389 p
->error
= WFERR_UNMARSHALLABLE
;
393 else if (PyCode_Check(v
)) {
394 PyCodeObject
*co
= (PyCodeObject
*)v
;
395 w_byte(TYPE_CODE
, p
);
396 w_long(co
->co_argcount
, p
);
397 w_long(co
->co_kwonlyargcount
, p
);
398 w_long(co
->co_nlocals
, p
);
399 w_long(co
->co_stacksize
, p
);
400 w_long(co
->co_flags
, p
);
401 w_object(co
->co_code
, p
);
402 w_object(co
->co_consts
, p
);
403 w_object(co
->co_names
, p
);
404 w_object(co
->co_varnames
, p
);
405 w_object(co
->co_freevars
, p
);
406 w_object(co
->co_cellvars
, p
);
407 w_object(co
->co_filename
, p
);
408 w_object(co
->co_name
, p
);
409 w_long(co
->co_firstlineno
, p
);
410 w_object(co
->co_lnotab
, p
);
412 else if (PyObject_CheckBuffer(v
)) {
413 /* Write unknown buffer-style objects as a string */
415 PyBufferProcs
*pb
= v
->ob_type
->tp_as_buffer
;
417 if ((*pb
->bf_getbuffer
)(v
, &view
, PyBUF_SIMPLE
) != 0) {
418 w_byte(TYPE_UNKNOWN
, p
);
419 p
->error
= WFERR_UNMARSHALLABLE
;
421 w_byte(TYPE_STRING
, p
);
426 p
->error
= WFERR_UNMARSHALLABLE
;
430 w_string(s
, (int)n
, p
);
431 if (pb
->bf_releasebuffer
!= NULL
)
432 (*pb
->bf_releasebuffer
)(v
, &view
);
435 w_byte(TYPE_UNKNOWN
, p
);
436 p
->error
= WFERR_UNMARSHALLABLE
;
441 /* version currently has no effect for writing longs. */
443 PyMarshal_WriteLongToFile(long x
, FILE *fp
, int version
)
450 wf
.version
= version
;
455 PyMarshal_WriteObjectToFile(PyObject
*x
, FILE *fp
, int version
)
461 wf
.strings
= (version
> 0) ? PyDict_New() : NULL
;
462 wf
.version
= version
;
464 Py_XDECREF(wf
.strings
);
467 typedef WFILE RFILE
; /* Same struct with different invariants */
469 #define rs_byte(p) (((p)->ptr < (p)->end) ? (unsigned char)*(p)->ptr++ : EOF)
471 #define r_byte(p) ((p)->fp ? getc((p)->fp) : rs_byte(p))
474 r_string(char *s
, int n
, RFILE
*p
)
477 /* The result fits into int because it must be <=n. */
478 return (int)fread(s
, 1, n
, p
->fp
);
479 if (p
->end
- p
->ptr
< n
)
480 n
= (int)(p
->end
- p
->ptr
);
481 memcpy(s
, p
->ptr
, n
);
492 /* Sign-extension, in case short greater than 16 bits */
501 register FILE *fp
= p
->fp
;
504 x
|= (long)getc(fp
) << 8;
505 x
|= (long)getc(fp
) << 16;
506 x
|= (long)getc(fp
) << 24;
510 x
|= (long)rs_byte(p
) << 8;
511 x
|= (long)rs_byte(p
) << 16;
512 x
|= (long)rs_byte(p
) << 24;
515 /* Sign extension for 64-bit machines */
516 x
|= -(x
& 0x80000000L
);
521 /* r_long64 deals with the TYPE_INT64 code. On a machine with
522 sizeof(long) > 4, it returns a Python int object, else a Python long
523 object. Note that w_long64 writes out TYPE_INT if 32 bits is enough,
524 so there's no inefficiency here in returning a PyLong on 32-bit boxes
525 for everything written via TYPE_INT64 (i.e., if an int is written via
526 TYPE_INT64, it *needs* more than 32 bits).
531 long lo4
= r_long(p
);
532 long hi4
= r_long(p
);
534 long x
= (hi4
<< 32) | (lo4
& 0xFFFFFFFFL
);
535 return PyLong_FromLong(x
);
537 unsigned char buf
[8];
539 int is_little_endian
= (int)*(char*)&one
;
540 if (is_little_endian
) {
541 memcpy(buf
, &lo4
, 4);
542 memcpy(buf
+4, &hi4
, 4);
545 memcpy(buf
, &hi4
, 4);
546 memcpy(buf
+4, &lo4
, 4);
548 return _PyLong_FromByteArray(buf
, 8, is_little_endian
, 1);
556 int size
, i
, j
, md
, shorts_in_top_digit
;
562 return (PyObject
*)_PyLong_New(0);
563 if (n
< -INT_MAX
|| n
> INT_MAX
) {
564 PyErr_SetString(PyExc_ValueError
,
565 "bad marshal data (long size out of range)");
569 size
= 1 + (ABS(n
) - 1) / PyLong_MARSHAL_RATIO
;
570 shorts_in_top_digit
= 1 + (ABS(n
) - 1) % PyLong_MARSHAL_RATIO
;
571 ob
= _PyLong_New(size
);
574 Py_SIZE(ob
) = n
> 0 ? size
: -size
;
576 for (i
= 0; i
< size
-1; i
++) {
578 for (j
=0; j
< PyLong_MARSHAL_RATIO
; j
++) {
580 if (md
< 0 || md
> PyLong_MARSHAL_BASE
)
582 d
+= (digit
)md
<< j
*PyLong_MARSHAL_SHIFT
;
587 for (j
=0; j
< shorts_in_top_digit
; j
++) {
589 if (md
< 0 || md
> PyLong_MARSHAL_BASE
)
591 /* topmost marshal digit should be nonzero */
592 if (md
== 0 && j
== shorts_in_top_digit
- 1) {
594 PyErr_SetString(PyExc_ValueError
,
595 "bad marshal data (unnormalized long data)");
598 d
+= (digit
)md
<< j
*PyLong_MARSHAL_SHIFT
;
600 /* top digit should be nonzero, else the resulting PyLong won't be
602 ob
->ob_digit
[size
-1] = d
;
603 return (PyObject
*)ob
;
606 PyErr_SetString(PyExc_ValueError
,
607 "bad marshal data (digit out of range in long)");
615 /* NULL is a valid return value, it does not necessarily means that
616 an exception is set. */
619 int type
= r_byte(p
);
624 if (p
->depth
> MAX_MARSHAL_STACK_DEPTH
) {
626 PyErr_SetString(PyExc_ValueError
, "recursion limit exceeded");
633 PyErr_SetString(PyExc_EOFError
,
634 "EOF read where object expected");
648 Py_INCREF(PyExc_StopIteration
);
649 retval
= PyExc_StopIteration
;
653 Py_INCREF(Py_Ellipsis
);
654 retval
= Py_Ellipsis
;
668 retval
= PyLong_FromLong(r_long(p
));
672 retval
= r_long64(p
);
676 retval
= r_PyLong(p
);
685 if (n
== EOF
|| r_string(buf
, (int)n
, p
) != n
) {
686 PyErr_SetString(PyExc_EOFError
,
687 "EOF read where object expected");
691 dx
= PyOS_string_to_double(buf
, NULL
, NULL
);
692 if (dx
== -1.0 && PyErr_Occurred())
694 retval
= PyFloat_FromDouble(dx
);
698 case TYPE_BINARY_FLOAT
:
700 unsigned char buf
[8];
702 if (r_string((char*)buf
, 8, p
) != 8) {
703 PyErr_SetString(PyExc_EOFError
,
704 "EOF read where object expected");
708 x
= _PyFloat_Unpack8(buf
, 1);
709 if (x
== -1.0 && PyErr_Occurred()) {
713 retval
= PyFloat_FromDouble(x
);
717 #ifndef WITHOUT_COMPLEX
724 if (n
== EOF
|| r_string(buf
, (int)n
, p
) != n
) {
725 PyErr_SetString(PyExc_EOFError
,
726 "EOF read where object expected");
730 c
.real
= PyOS_string_to_double(buf
, NULL
, NULL
);
731 if (c
.real
== -1.0 && PyErr_Occurred())
734 if (n
== EOF
|| r_string(buf
, (int)n
, p
) != n
) {
735 PyErr_SetString(PyExc_EOFError
,
736 "EOF read where object expected");
740 c
.imag
= PyOS_string_to_double(buf
, NULL
, NULL
);
741 if (c
.imag
== -1.0 && PyErr_Occurred())
743 retval
= PyComplex_FromCComplex(c
);
747 case TYPE_BINARY_COMPLEX
:
749 unsigned char buf
[8];
751 if (r_string((char*)buf
, 8, p
) != 8) {
752 PyErr_SetString(PyExc_EOFError
,
753 "EOF read where object expected");
757 c
.real
= _PyFloat_Unpack8(buf
, 1);
758 if (c
.real
== -1.0 && PyErr_Occurred()) {
762 if (r_string((char*)buf
, 8, p
) != 8) {
763 PyErr_SetString(PyExc_EOFError
,
764 "EOF read where object expected");
768 c
.imag
= _PyFloat_Unpack8(buf
, 1);
769 if (c
.imag
== -1.0 && PyErr_Occurred()) {
773 retval
= PyComplex_FromCComplex(c
);
780 if (n
< 0 || n
> INT_MAX
) {
781 PyErr_SetString(PyExc_ValueError
, "bad marshal data (string size out of range)");
785 v
= PyBytes_FromStringAndSize((char *)NULL
, n
);
790 if (r_string(PyBytes_AS_STRING(v
), (int)n
, p
) != n
) {
792 PyErr_SetString(PyExc_EOFError
,
793 "EOF read where object expected");
805 if (n
< 0 || n
> INT_MAX
) {
806 PyErr_SetString(PyExc_ValueError
, "bad marshal data (unicode size out of range)");
810 buffer
= PyMem_NEW(char, n
);
811 if (buffer
== NULL
) {
812 retval
= PyErr_NoMemory();
815 if (r_string(buffer
, (int)n
, p
) != n
) {
817 PyErr_SetString(PyExc_EOFError
,
818 "EOF read where object expected");
822 v
= PyUnicode_DecodeUTF8(buffer
, n
, "surrogatepass");
830 if (n
< 0 || n
> INT_MAX
) {
831 PyErr_SetString(PyExc_ValueError
, "bad marshal data (tuple size out of range)");
835 v
= PyTuple_New((int)n
);
840 for (i
= 0; i
< n
; i
++) {
843 if (!PyErr_Occurred())
844 PyErr_SetString(PyExc_TypeError
,
845 "NULL object in marshal data for tuple");
850 PyTuple_SET_ITEM(v
, (int)i
, v2
);
857 if (n
< 0 || n
> INT_MAX
) {
858 PyErr_SetString(PyExc_ValueError
, "bad marshal data (list size out of range)");
862 v
= PyList_New((int)n
);
867 for (i
= 0; i
< n
; i
++) {
870 if (!PyErr_Occurred())
871 PyErr_SetString(PyExc_TypeError
,
872 "NULL object in marshal data for list");
877 PyList_SET_ITEM(v
, (int)i
, v2
);
895 PyDict_SetItem(v
, key
, val
);
899 if (PyErr_Occurred()) {
909 if (n
< 0 || n
> INT_MAX
) {
910 PyErr_SetString(PyExc_ValueError
, "bad marshal data (set size out of range)");
914 v
= (type
== TYPE_SET
) ? PySet_New(NULL
) : PyFrozenSet_New(NULL
);
919 for (i
= 0; i
< n
; i
++) {
922 if (!PyErr_Occurred())
923 PyErr_SetString(PyExc_TypeError
,
924 "NULL object in marshal data for set");
929 if (PySet_Add(v
, v2
) == -1) {
947 PyObject
*code
= NULL
;
948 PyObject
*consts
= NULL
;
949 PyObject
*names
= NULL
;
950 PyObject
*varnames
= NULL
;
951 PyObject
*freevars
= NULL
;
952 PyObject
*cellvars
= NULL
;
953 PyObject
*filename
= NULL
;
954 PyObject
*name
= NULL
;
956 PyObject
*lnotab
= NULL
;
960 /* XXX ignore long->int overflows for now */
961 argcount
= (int)r_long(p
);
962 kwonlyargcount
= (int)r_long(p
);
963 nlocals
= (int)r_long(p
);
964 stacksize
= (int)r_long(p
);
965 flags
= (int)r_long(p
);
969 consts
= r_object(p
);
975 varnames
= r_object(p
);
976 if (varnames
== NULL
)
978 freevars
= r_object(p
);
979 if (freevars
== NULL
)
981 cellvars
= r_object(p
);
982 if (cellvars
== NULL
)
984 filename
= r_object(p
);
985 if (filename
== NULL
)
990 firstlineno
= (int)r_long(p
);
991 lnotab
= r_object(p
);
995 v
= (PyObject
*) PyCode_New(
996 argcount
, kwonlyargcount
,
997 nlocals
, stacksize
, flags
,
998 code
, consts
, names
, varnames
,
999 freevars
, cellvars
, filename
, name
,
1000 firstlineno
, lnotab
);
1006 Py_XDECREF(varnames
);
1007 Py_XDECREF(freevars
);
1008 Py_XDECREF(cellvars
);
1009 Py_XDECREF(filename
);
1017 /* Bogus data got written, which isn't ideal.
1018 This will let you keep working and recover. */
1019 PyErr_SetString(PyExc_ValueError
, "bad marshal data (unknown type code)");
1029 read_object(RFILE
*p
)
1032 if (PyErr_Occurred()) {
1033 fprintf(stderr
, "XXX readobject called with exception set\n");
1037 if (v
== NULL
&& !PyErr_Occurred())
1038 PyErr_SetString(PyExc_TypeError
, "NULL object in marshal data for object");
1043 PyMarshal_ReadShortFromFile(FILE *fp
)
1049 rf
.end
= rf
.ptr
= NULL
;
1050 return r_short(&rf
);
1054 PyMarshal_ReadLongFromFile(FILE *fp
)
1059 rf
.ptr
= rf
.end
= NULL
;
1064 /* Return size of file in bytes; < 0 if unknown. */
1066 getfilesize(FILE *fp
)
1069 if (fstat(fileno(fp
), &st
) != 0)
1076 /* If we can get the size of the file up-front, and it's reasonably small,
1077 * read it in one gulp and delegate to ...FromString() instead. Much quicker
1078 * than reading a byte at a time from file; speeds .pyc imports.
1079 * CAUTION: since this may read the entire remainder of the file, don't
1080 * call it unless you know you're done with the file.
1083 PyMarshal_ReadLastObjectFromFile(FILE *fp
)
1085 /* 75% of 2.1's .pyc files can exploit SMALL_FILE_LIMIT.
1086 * REASONABLE_FILE_LIMIT is by defn something big enough for Tkinter.pyc.
1088 #define SMALL_FILE_LIMIT (1L << 14)
1089 #define REASONABLE_FILE_LIMIT (1L << 18)
1094 filesize
= getfilesize(fp
);
1096 char buf
[SMALL_FILE_LIMIT
];
1098 if (filesize
<= SMALL_FILE_LIMIT
)
1100 else if (filesize
<= REASONABLE_FILE_LIMIT
)
1101 pBuf
= (char *)PyMem_MALLOC(filesize
);
1105 /* filesize must fit into an int, because it
1106 is smaller than REASONABLE_FILE_LIMIT */
1107 n
= fread(pBuf
, 1, (int)filesize
, fp
);
1108 v
= PyMarshal_ReadObjectFromString(pBuf
, n
);
1116 /* We don't have fstat, or we do but the file is larger than
1117 * REASONABLE_FILE_LIMIT or malloc failed -- read a byte at a time.
1119 return PyMarshal_ReadObjectFromFile(fp
);
1121 #undef SMALL_FILE_LIMIT
1122 #undef REASONABLE_FILE_LIMIT
1126 PyMarshal_ReadObjectFromFile(FILE *fp
)
1131 rf
.strings
= PyList_New(0);
1133 rf
.ptr
= rf
.end
= NULL
;
1134 result
= r_object(&rf
);
1135 Py_DECREF(rf
.strings
);
1140 PyMarshal_ReadObjectFromString(char *str
, Py_ssize_t len
)
1147 rf
.strings
= PyList_New(0);
1149 result
= r_object(&rf
);
1150 Py_DECREF(rf
.strings
);
1155 PyMarshal_WriteObjectToString(PyObject
*x
, int version
)
1158 PyObject
*res
= NULL
;
1161 wf
.str
= PyBytes_FromStringAndSize((char *)NULL
, 50);
1164 wf
.ptr
= PyBytes_AS_STRING((PyBytesObject
*)wf
.str
);
1165 wf
.end
= wf
.ptr
+ PyBytes_Size(wf
.str
);
1166 wf
.error
= WFERR_OK
;
1168 wf
.version
= version
;
1169 wf
.strings
= (version
> 0) ? PyDict_New() : NULL
;
1171 Py_XDECREF(wf
.strings
);
1172 if (wf
.str
!= NULL
) {
1173 char *base
= PyBytes_AS_STRING((PyBytesObject
*)wf
.str
);
1174 if (wf
.ptr
- base
> PY_SSIZE_T_MAX
) {
1176 PyErr_SetString(PyExc_OverflowError
,
1177 "too much marshal data for a string");
1180 if (_PyBytes_Resize(&wf
.str
, (Py_ssize_t
)(wf
.ptr
- base
)) < 0)
1183 if (wf
.error
!= WFERR_OK
) {
1185 if (wf
.error
== WFERR_NOMEMORY
)
1188 PyErr_SetString(PyExc_ValueError
,
1189 (wf
.error
==WFERR_UNMARSHALLABLE
)?"unmarshallable object"
1190 :"object too deeply nested to marshal");
1193 if (wf
.str
!= NULL
) {
1194 /* XXX Quick hack -- need to do this differently */
1195 res
= PyBytes_FromObject(wf
.str
);
1201 /* And an interface for Python programs... */
1204 marshal_dump(PyObject
*self
, PyObject
*args
)
1206 /* XXX Quick hack -- need to do this differently */
1209 int version
= Py_MARSHAL_VERSION
;
1212 if (!PyArg_ParseTuple(args
, "OO|i:dump", &x
, &f
, &version
))
1214 s
= PyMarshal_WriteObjectToString(x
, version
);
1217 res
= PyObject_CallMethod(f
, "write", "O", s
);
1222 PyDoc_STRVAR(dump_doc
,
1223 "dump(value, file[, version])\n\
1225 Write the value on the open file. The value must be a supported type.\n\
1226 The file must be an open file object such as sys.stdout or returned by\n\
1227 open() or os.popen(). It must be opened in binary mode ('wb' or 'w+b').\n\
1229 If the value has (or contains an object that has) an unsupported type, a\n\
1230 ValueError exception is raised — but garbage data will also be written\n\
1231 to the file. The object will not be properly read back by load()\n\
1233 The version argument indicates the data format that dump should use.");
1236 marshal_load(PyObject
*self
, PyObject
*f
)
1238 /* XXX Quick hack -- need to do this differently */
1239 PyObject
*data
, *result
;
1241 data
= PyObject_CallMethod(f
, "read", "");
1245 if (PyBytes_Check(data
)) {
1246 rf
.ptr
= PyBytes_AS_STRING(data
);
1247 rf
.end
= rf
.ptr
+ PyBytes_GET_SIZE(data
);
1249 else if (PyBytes_Check(data
)) {
1250 rf
.ptr
= PyBytes_AS_STRING(data
);
1251 rf
.end
= rf
.ptr
+ PyBytes_GET_SIZE(data
);
1254 PyErr_Format(PyExc_TypeError
,
1255 "f.read() returned neither string "
1256 "nor bytes but %.100s",
1257 data
->ob_type
->tp_name
);
1261 rf
.strings
= PyList_New(0);
1263 result
= read_object(&rf
);
1264 Py_DECREF(rf
.strings
);
1269 PyDoc_STRVAR(load_doc
,
1272 Read one value from the open file and return it. If no valid value is\n\
1273 read (e.g. because the data has a different Python version’s\n\
1274 incompatible marshal format), raise EOFError, ValueError or TypeError.\n\
1275 The file must be an open file object opened in binary mode ('rb' or\n\
1278 Note: If an object containing an unsupported type was marshalled with\n\
1279 dump(), load() will substitute None for the unmarshallable type.");
1283 marshal_dumps(PyObject
*self
, PyObject
*args
)
1286 int version
= Py_MARSHAL_VERSION
;
1287 if (!PyArg_ParseTuple(args
, "O|i:dumps", &x
, &version
))
1289 return PyMarshal_WriteObjectToString(x
, version
);
1292 PyDoc_STRVAR(dumps_doc
,
1293 "dumps(value[, version])\n\
1295 Return the string that would be written to a file by dump(value, file).\n\
1296 The value must be a supported type. Raise a ValueError exception if\n\
1297 value has (or contains an object that has) an unsupported type.\n\
1299 The version argument indicates the data format that dumps should use.");
1303 marshal_loads(PyObject
*self
, PyObject
*args
)
1310 if (!PyArg_ParseTuple(args
, "s*:loads", &p
))
1317 rf
.strings
= PyList_New(0);
1319 result
= read_object(&rf
);
1320 Py_DECREF(rf
.strings
);
1321 PyBuffer_Release(&p
);
1325 PyDoc_STRVAR(loads_doc
,
1328 Convert the string to a value. If no valid value is found, raise\n\
1329 EOFError, ValueError or TypeError. Extra characters in the string are\n\
1332 static PyMethodDef marshal_methods
[] = {
1333 {"dump", marshal_dump
, METH_VARARGS
, dump_doc
},
1334 {"load", marshal_load
, METH_O
, load_doc
},
1335 {"dumps", marshal_dumps
, METH_VARARGS
, dumps_doc
},
1336 {"loads", marshal_loads
, METH_VARARGS
, loads_doc
},
1337 {NULL
, NULL
} /* sentinel */
1341 PyDoc_STRVAR(module_doc
,
1342 "This module contains functions that can read and write Python values in\n\
1343 a binary format. The format is specific to Python, but independent of\n\
1344 machine architecture issues.\n\
1346 Not all Python object types are supported; in general, only objects\n\
1347 whose value is independent from a particular invocation of Python can be\n\
1348 written and read by this module. The following types are supported:\n\
1349 None, integers, floating point numbers, strings, bytes, bytearrays,\n\
1350 tuples, lists, sets, dictionaries, and code objects, where it\n\
1351 should be understood that tuples, lists and dictionaries are only\n\
1352 supported as long as the values contained therein are themselves\n\
1353 supported; and recursive lists and dictionaries should not be written\n\
1354 (they will cause infinite loops).\n\
1358 version -- indicates the format that the module uses. Version 0 is the\n\
1359 historical format, version 1 shares interned strings and version 2\n\
1360 uses a binary format for floating point numbers.\n\
1364 dump() -- write value to a file\n\
1365 load() -- read value from a file\n\
1366 dumps() -- write value to a string\n\
1367 loads() -- read value from a string");
1371 static struct PyModuleDef marshalmodule
= {
1372 PyModuleDef_HEAD_INIT
,
1384 PyMarshal_Init(void)
1386 PyObject
*mod
= PyModule_Create(&marshalmodule
);
1389 PyModule_AddIntConstant(mod
, "version", Py_MARSHAL_VERSION
);