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.
20 #define MAX_MARSHAL_STACK_DEPTH 2000
24 #define TYPE_FALSE 'F'
26 #define TYPE_STOPITER 'S'
27 #define TYPE_ELLIPSIS '.'
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'
35 #define TYPE_STRING 's'
36 #define TYPE_INTERNED 't'
37 #define TYPE_STRINGREF 'R'
38 #define TYPE_TUPLE '('
42 #define TYPE_UNICODE 'u'
43 #define TYPE_UNKNOWN '?'
45 #define TYPE_FROZENSET '>'
48 #define WFERR_UNMARSHALLABLE 1
49 #define WFERR_NESTEDTOODEEP 2
50 #define WFERR_NOMEMORY 3
54 int error
; /* see WFERR_* values */
56 /* If fp == NULL, the following are valid: */
60 PyObject
*strings
; /* dict on marshal, list on unmarshal */
64 #define w_byte(c, p) if (((p)->fp)) putc((c), (p)->fp); \
65 else if ((p)->ptr != (p)->end) *(p)->ptr++ = (c); \
69 w_more(int c
, WFILE
*p
)
71 Py_ssize_t size
, newsize
;
73 return; /* An error already occurred */
74 size
= PyString_Size(p
->str
);
75 newsize
= size
+ size
+ 1024;
76 if (newsize
> 32*1024*1024) {
77 newsize
= size
+ (size
>> 3); /* 12.5% overallocation */
79 if (_PyString_Resize(&p
->str
, newsize
) != 0) {
80 p
->ptr
= p
->end
= NULL
;
83 p
->ptr
= PyString_AS_STRING((PyStringObject
*)p
->str
) + size
;
85 PyString_AS_STRING((PyStringObject
*)p
->str
) + newsize
;
86 *p
->ptr
++ = Py_SAFE_DOWNCAST(c
, int, char);
91 w_string(char *s
, int n
, WFILE
*p
)
94 fwrite(s
, 1, n
, p
->fp
);
105 w_short(int x
, WFILE
*p
)
107 w_byte((char)( x
& 0xff), p
);
108 w_byte((char)((x
>> 8) & 0xff), p
);
112 w_long(long x
, WFILE
*p
)
114 w_byte((char)( x
& 0xff), p
);
115 w_byte((char)((x
>> 8) & 0xff), p
);
116 w_byte((char)((x
>>16) & 0xff), p
);
117 w_byte((char)((x
>>24) & 0xff), p
);
122 w_long64(long x
, WFILE
*p
)
129 /* We assume that Python longs are stored internally in base some power of
130 2**15; for the sake of portability we'll always read and write them in base
133 #define PyLong_MARSHAL_SHIFT 15
134 #define PyLong_MARSHAL_BASE ((short)1 << PyLong_MARSHAL_SHIFT)
135 #define PyLong_MARSHAL_MASK (PyLong_MARSHAL_BASE - 1)
136 #if PyLong_SHIFT % PyLong_MARSHAL_SHIFT != 0
137 #error "PyLong_SHIFT must be a multiple of PyLong_MARSHAL_SHIFT"
139 #define PyLong_MARSHAL_RATIO (PyLong_SHIFT / PyLong_MARSHAL_SHIFT)
142 w_PyLong(const PyLongObject
*ob
, WFILE
*p
)
144 Py_ssize_t i
, j
, n
, l
;
147 w_byte(TYPE_LONG
, p
);
148 if (Py_SIZE(ob
) == 0) {
153 /* set l to number of base PyLong_MARSHAL_BASE digits */
154 n
= ABS(Py_SIZE(ob
));
155 l
= (n
-1) * PyLong_MARSHAL_RATIO
;
156 d
= ob
->ob_digit
[n
-1];
157 assert(d
!= 0); /* a PyLong is always normalized */
159 d
>>= PyLong_MARSHAL_SHIFT
;
162 w_long((long)(Py_SIZE(ob
) > 0 ? l
: -l
), p
);
164 for (i
=0; i
< n
-1; i
++) {
166 for (j
=0; j
< PyLong_MARSHAL_RATIO
; j
++) {
167 w_short(d
& PyLong_MARSHAL_MASK
, p
);
168 d
>>= PyLong_MARSHAL_SHIFT
;
172 d
= ob
->ob_digit
[n
-1];
174 w_short(d
& PyLong_MARSHAL_MASK
, p
);
175 d
>>= PyLong_MARSHAL_SHIFT
;
180 w_object(PyObject
*v
, WFILE
*p
)
186 if (p
->depth
> MAX_MARSHAL_STACK_DEPTH
) {
187 p
->error
= WFERR_NESTEDTOODEEP
;
189 else if (v
== NULL
) {
190 w_byte(TYPE_NULL
, p
);
192 else if (v
== Py_None
) {
193 w_byte(TYPE_NONE
, p
);
195 else if (v
== PyExc_StopIteration
) {
196 w_byte(TYPE_STOPITER
, p
);
198 else if (v
== Py_Ellipsis
) {
199 w_byte(TYPE_ELLIPSIS
, p
);
201 else if (v
== Py_False
) {
202 w_byte(TYPE_FALSE
, p
);
204 else if (v
== Py_True
) {
205 w_byte(TYPE_TRUE
, p
);
207 else if (PyInt_CheckExact(v
)) {
208 long x
= PyInt_AS_LONG((PyIntObject
*)v
);
210 long y
= Py_ARITHMETIC_RIGHT_SHIFT(long, x
, 31);
212 w_byte(TYPE_INT64
, p
);
222 else if (PyLong_CheckExact(v
)) {
223 PyLongObject
*ob
= (PyLongObject
*)v
;
226 else if (PyFloat_CheckExact(v
)) {
227 if (p
->version
> 1) {
228 unsigned char buf
[8];
229 if (_PyFloat_Pack8(PyFloat_AsDouble(v
),
231 p
->error
= WFERR_UNMARSHALLABLE
;
234 w_byte(TYPE_BINARY_FLOAT
, p
);
235 w_string((char*)buf
, 8, p
);
238 char *buf
= PyOS_double_to_string(PyFloat_AS_DOUBLE(v
),
241 p
->error
= WFERR_NOMEMORY
;
245 w_byte(TYPE_FLOAT
, p
);
247 w_string(buf
, (int)n
, p
);
251 #ifndef WITHOUT_COMPLEX
252 else if (PyComplex_CheckExact(v
)) {
253 if (p
->version
> 1) {
254 unsigned char buf
[8];
255 if (_PyFloat_Pack8(PyComplex_RealAsDouble(v
),
257 p
->error
= WFERR_UNMARSHALLABLE
;
260 w_byte(TYPE_BINARY_COMPLEX
, p
);
261 w_string((char*)buf
, 8, p
);
262 if (_PyFloat_Pack8(PyComplex_ImagAsDouble(v
),
264 p
->error
= WFERR_UNMARSHALLABLE
;
267 w_string((char*)buf
, 8, p
);
271 w_byte(TYPE_COMPLEX
, p
);
272 buf
= PyOS_double_to_string(PyComplex_RealAsDouble(v
),
275 p
->error
= WFERR_NOMEMORY
;
280 w_string(buf
, (int)n
, p
);
282 buf
= PyOS_double_to_string(PyComplex_ImagAsDouble(v
),
285 p
->error
= WFERR_NOMEMORY
;
290 w_string(buf
, (int)n
, p
);
295 else if (PyString_CheckExact(v
)) {
296 if (p
->strings
&& PyString_CHECK_INTERNED(v
)) {
297 PyObject
*o
= PyDict_GetItem(p
->strings
, v
);
299 long w
= PyInt_AsLong(o
);
300 w_byte(TYPE_STRINGREF
, p
);
306 o
= PyInt_FromSsize_t(PyDict_Size(p
->strings
));
308 PyDict_SetItem(p
->strings
, v
, o
) >= 0;
312 p
->error
= WFERR_UNMARSHALLABLE
;
315 w_byte(TYPE_INTERNED
, p
);
319 w_byte(TYPE_STRING
, p
);
321 n
= PyString_GET_SIZE(v
);
323 /* huge strings are not supported */
325 p
->error
= WFERR_UNMARSHALLABLE
;
329 w_string(PyString_AS_STRING(v
), (int)n
, p
);
331 #ifdef Py_USING_UNICODE
332 else if (PyUnicode_CheckExact(v
)) {
334 utf8
= PyUnicode_AsUTF8String(v
);
337 p
->error
= WFERR_UNMARSHALLABLE
;
340 w_byte(TYPE_UNICODE
, p
);
341 n
= PyString_GET_SIZE(utf8
);
344 p
->error
= WFERR_UNMARSHALLABLE
;
348 w_string(PyString_AS_STRING(utf8
), (int)n
, p
);
352 else if (PyTuple_CheckExact(v
)) {
353 w_byte(TYPE_TUPLE
, p
);
356 for (i
= 0; i
< n
; i
++) {
357 w_object(PyTuple_GET_ITEM(v
, i
), p
);
360 else if (PyList_CheckExact(v
)) {
361 w_byte(TYPE_LIST
, p
);
362 n
= PyList_GET_SIZE(v
);
364 for (i
= 0; i
< n
; i
++) {
365 w_object(PyList_GET_ITEM(v
, i
), p
);
368 else if (PyDict_CheckExact(v
)) {
370 PyObject
*key
, *value
;
371 w_byte(TYPE_DICT
, p
);
372 /* This one is NULL object terminated! */
374 while (PyDict_Next(v
, &pos
, &key
, &value
)) {
378 w_object((PyObject
*)NULL
, p
);
380 else if (PyAnySet_CheckExact(v
)) {
381 PyObject
*value
, *it
;
383 if (PyObject_TypeCheck(v
, &PySet_Type
))
386 w_byte(TYPE_FROZENSET
, p
);
387 n
= PyObject_Size(v
);
390 p
->error
= WFERR_UNMARSHALLABLE
;
394 it
= PyObject_GetIter(v
);
397 p
->error
= WFERR_UNMARSHALLABLE
;
400 while ((value
= PyIter_Next(it
)) != NULL
) {
405 if (PyErr_Occurred()) {
407 p
->error
= WFERR_UNMARSHALLABLE
;
411 else if (PyCode_Check(v
)) {
412 PyCodeObject
*co
= (PyCodeObject
*)v
;
413 w_byte(TYPE_CODE
, p
);
414 w_long(co
->co_argcount
, p
);
415 w_long(co
->co_nlocals
, p
);
416 w_long(co
->co_stacksize
, p
);
417 w_long(co
->co_flags
, p
);
418 w_object(co
->co_code
, p
);
419 w_object(co
->co_consts
, p
);
420 w_object(co
->co_names
, p
);
421 w_object(co
->co_varnames
, p
);
422 w_object(co
->co_freevars
, p
);
423 w_object(co
->co_cellvars
, p
);
424 w_object(co
->co_filename
, p
);
425 w_object(co
->co_name
, p
);
426 w_long(co
->co_firstlineno
, p
);
427 w_object(co
->co_lnotab
, p
);
429 else if (PyObject_CheckReadBuffer(v
)) {
430 /* Write unknown buffer-style objects as a string */
432 PyBufferProcs
*pb
= v
->ob_type
->tp_as_buffer
;
433 w_byte(TYPE_STRING
, p
);
434 n
= (*pb
->bf_getreadbuffer
)(v
, 0, (void **)&s
);
437 p
->error
= WFERR_UNMARSHALLABLE
;
441 w_string(s
, (int)n
, p
);
444 w_byte(TYPE_UNKNOWN
, p
);
445 p
->error
= WFERR_UNMARSHALLABLE
;
451 /* version currently has no effect for writing longs. */
453 PyMarshal_WriteLongToFile(long x
, FILE *fp
, int version
)
460 wf
.version
= version
;
465 PyMarshal_WriteObjectToFile(PyObject
*x
, FILE *fp
, int version
)
471 wf
.strings
= (version
> 0) ? PyDict_New() : NULL
;
472 wf
.version
= version
;
474 Py_XDECREF(wf
.strings
);
477 typedef WFILE RFILE
; /* Same struct with different invariants */
479 #define rs_byte(p) (((p)->ptr < (p)->end) ? (unsigned char)*(p)->ptr++ : EOF)
481 #define r_byte(p) ((p)->fp ? getc((p)->fp) : rs_byte(p))
484 r_string(char *s
, int n
, RFILE
*p
)
487 /* The result fits into int because it must be <=n. */
488 return (int)fread(s
, 1, n
, p
->fp
);
489 if (p
->end
- p
->ptr
< n
)
490 n
= (int)(p
->end
- p
->ptr
);
491 memcpy(s
, p
->ptr
, n
);
502 /* Sign-extension, in case short greater than 16 bits */
511 register FILE *fp
= p
->fp
;
514 x
|= (long)getc(fp
) << 8;
515 x
|= (long)getc(fp
) << 16;
516 x
|= (long)getc(fp
) << 24;
520 x
|= (long)rs_byte(p
) << 8;
521 x
|= (long)rs_byte(p
) << 16;
522 x
|= (long)rs_byte(p
) << 24;
525 /* Sign extension for 64-bit machines */
526 x
|= -(x
& 0x80000000L
);
531 /* r_long64 deals with the TYPE_INT64 code. On a machine with
532 sizeof(long) > 4, it returns a Python int object, else a Python long
533 object. Note that w_long64 writes out TYPE_INT if 32 bits is enough,
534 so there's no inefficiency here in returning a PyLong on 32-bit boxes
535 for everything written via TYPE_INT64 (i.e., if an int is written via
536 TYPE_INT64, it *needs* more than 32 bits).
541 long lo4
= r_long(p
);
542 long hi4
= r_long(p
);
544 long x
= (hi4
<< 32) | (lo4
& 0xFFFFFFFFL
);
545 return PyInt_FromLong(x
);
547 unsigned char buf
[8];
549 int is_little_endian
= (int)*(char*)&one
;
550 if (is_little_endian
) {
551 memcpy(buf
, &lo4
, 4);
552 memcpy(buf
+4, &hi4
, 4);
555 memcpy(buf
, &hi4
, 4);
556 memcpy(buf
+4, &lo4
, 4);
558 return _PyLong_FromByteArray(buf
, 8, is_little_endian
, 1);
566 int size
, i
, j
, md
, shorts_in_top_digit
;
572 return (PyObject
*)_PyLong_New(0);
573 if (n
< -INT_MAX
|| n
> INT_MAX
) {
574 PyErr_SetString(PyExc_ValueError
,
575 "bad marshal data (long size out of range)");
579 size
= 1 + (ABS(n
) - 1) / PyLong_MARSHAL_RATIO
;
580 shorts_in_top_digit
= 1 + (ABS(n
) - 1) % PyLong_MARSHAL_RATIO
;
581 ob
= _PyLong_New(size
);
584 Py_SIZE(ob
) = n
> 0 ? size
: -size
;
586 for (i
= 0; i
< size
-1; i
++) {
588 for (j
=0; j
< PyLong_MARSHAL_RATIO
; j
++) {
590 if (md
< 0 || md
> PyLong_MARSHAL_BASE
)
592 d
+= (digit
)md
<< j
*PyLong_MARSHAL_SHIFT
;
597 for (j
=0; j
< shorts_in_top_digit
; j
++) {
599 if (md
< 0 || md
> PyLong_MARSHAL_BASE
)
601 /* topmost marshal digit should be nonzero */
602 if (md
== 0 && j
== shorts_in_top_digit
- 1) {
604 PyErr_SetString(PyExc_ValueError
,
605 "bad marshal data (unnormalized long data)");
608 d
+= (digit
)md
<< j
*PyLong_MARSHAL_SHIFT
;
610 /* top digit should be nonzero, else the resulting PyLong won't be
612 ob
->ob_digit
[size
-1] = d
;
613 return (PyObject
*)ob
;
616 PyErr_SetString(PyExc_ValueError
,
617 "bad marshal data (digit out of range in long)");
625 /* NULL is a valid return value, it does not necessarily means that
626 an exception is set. */
629 int type
= r_byte(p
);
634 if (p
->depth
> MAX_MARSHAL_STACK_DEPTH
) {
636 PyErr_SetString(PyExc_ValueError
, "recursion limit exceeded");
643 PyErr_SetString(PyExc_EOFError
,
644 "EOF read where object expected");
658 Py_INCREF(PyExc_StopIteration
);
659 retval
= PyExc_StopIteration
;
663 Py_INCREF(Py_Ellipsis
);
664 retval
= Py_Ellipsis
;
678 retval
= PyInt_FromLong(r_long(p
));
682 retval
= r_long64(p
);
686 retval
= r_PyLong(p
);
694 if (n
== EOF
|| r_string(buf
, (int)n
, p
) != n
) {
695 PyErr_SetString(PyExc_EOFError
,
696 "EOF read where object expected");
702 PyFPE_START_PROTECT("atof", break)
703 dx
= PyOS_ascii_atof(buf
);
704 PyFPE_END_PROTECT(dx
)
705 retval
= PyFloat_FromDouble(dx
);
709 case TYPE_BINARY_FLOAT
:
711 unsigned char buf
[8];
713 if (r_string((char*)buf
, 8, p
) != 8) {
714 PyErr_SetString(PyExc_EOFError
,
715 "EOF read where object expected");
719 x
= _PyFloat_Unpack8(buf
, 1);
720 if (x
== -1.0 && PyErr_Occurred()) {
724 retval
= PyFloat_FromDouble(x
);
728 #ifndef WITHOUT_COMPLEX
734 if (n
== EOF
|| r_string(buf
, (int)n
, p
) != n
) {
735 PyErr_SetString(PyExc_EOFError
,
736 "EOF read where object expected");
742 PyFPE_START_PROTECT("atof", break;)
743 c
.real
= PyOS_ascii_atof(buf
);
746 if (n
== EOF
|| r_string(buf
, (int)n
, p
) != n
) {
747 PyErr_SetString(PyExc_EOFError
,
748 "EOF read where object expected");
753 PyFPE_START_PROTECT("atof", break)
754 c
.imag
= PyOS_ascii_atof(buf
);
756 retval
= PyComplex_FromCComplex(c
);
760 case TYPE_BINARY_COMPLEX
:
762 unsigned char buf
[8];
764 if (r_string((char*)buf
, 8, p
) != 8) {
765 PyErr_SetString(PyExc_EOFError
,
766 "EOF read where object expected");
770 c
.real
= _PyFloat_Unpack8(buf
, 1);
771 if (c
.real
== -1.0 && PyErr_Occurred()) {
775 if (r_string((char*)buf
, 8, p
) != 8) {
776 PyErr_SetString(PyExc_EOFError
,
777 "EOF read where object expected");
781 c
.imag
= _PyFloat_Unpack8(buf
, 1);
782 if (c
.imag
== -1.0 && PyErr_Occurred()) {
786 retval
= PyComplex_FromCComplex(c
);
794 if (n
< 0 || n
> INT_MAX
) {
795 PyErr_SetString(PyExc_ValueError
, "bad marshal data (string size out of range)");
799 v
= PyString_FromStringAndSize((char *)NULL
, n
);
804 if (r_string(PyString_AS_STRING(v
), (int)n
, p
) != n
) {
806 PyErr_SetString(PyExc_EOFError
,
807 "EOF read where object expected");
811 if (type
== TYPE_INTERNED
) {
812 PyString_InternInPlace(&v
);
813 if (PyList_Append(p
->strings
, v
) < 0) {
823 if (n
< 0 || n
>= PyList_GET_SIZE(p
->strings
)) {
824 PyErr_SetString(PyExc_ValueError
, "bad marshal data (string ref out of range)");
828 v
= PyList_GET_ITEM(p
->strings
, n
);
833 #ifdef Py_USING_UNICODE
839 if (n
< 0 || n
> INT_MAX
) {
840 PyErr_SetString(PyExc_ValueError
, "bad marshal data (unicode size out of range)");
844 buffer
= PyMem_NEW(char, n
);
845 if (buffer
== NULL
) {
846 retval
= PyErr_NoMemory();
849 if (r_string(buffer
, (int)n
, p
) != n
) {
851 PyErr_SetString(PyExc_EOFError
,
852 "EOF read where object expected");
856 v
= PyUnicode_DecodeUTF8(buffer
, n
, NULL
);
865 if (n
< 0 || n
> INT_MAX
) {
866 PyErr_SetString(PyExc_ValueError
, "bad marshal data (tuple size out of range)");
870 v
= PyTuple_New((int)n
);
875 for (i
= 0; i
< n
; i
++) {
878 if (!PyErr_Occurred())
879 PyErr_SetString(PyExc_TypeError
,
880 "NULL object in marshal data for tuple");
885 PyTuple_SET_ITEM(v
, (int)i
, v2
);
892 if (n
< 0 || n
> INT_MAX
) {
893 PyErr_SetString(PyExc_ValueError
, "bad marshal data (list size out of range)");
897 v
= PyList_New((int)n
);
902 for (i
= 0; i
< n
; i
++) {
905 if (!PyErr_Occurred())
906 PyErr_SetString(PyExc_TypeError
,
907 "NULL object in marshal data for list");
912 PyList_SET_ITEM(v
, (int)i
, v2
);
930 PyDict_SetItem(v
, key
, val
);
934 if (PyErr_Occurred()) {
944 if (n
< 0 || n
> INT_MAX
) {
945 PyErr_SetString(PyExc_ValueError
, "bad marshal data (set size out of range)");
949 v
= (type
== TYPE_SET
) ? PySet_New(NULL
) : PyFrozenSet_New(NULL
);
954 for (i
= 0; i
< n
; i
++) {
957 if (!PyErr_Occurred())
958 PyErr_SetString(PyExc_TypeError
,
959 "NULL object in marshal data for set");
964 if (PySet_Add(v
, v2
) == -1) {
976 if (PyEval_GetRestricted()) {
977 PyErr_SetString(PyExc_RuntimeError
,
978 "cannot unmarshal code objects in "
979 "restricted execution mode");
988 PyObject
*code
= NULL
;
989 PyObject
*consts
= NULL
;
990 PyObject
*names
= NULL
;
991 PyObject
*varnames
= NULL
;
992 PyObject
*freevars
= NULL
;
993 PyObject
*cellvars
= NULL
;
994 PyObject
*filename
= NULL
;
995 PyObject
*name
= NULL
;
997 PyObject
*lnotab
= NULL
;
1001 /* XXX ignore long->int overflows for now */
1002 argcount
= (int)r_long(p
);
1003 nlocals
= (int)r_long(p
);
1004 stacksize
= (int)r_long(p
);
1005 flags
= (int)r_long(p
);
1009 consts
= r_object(p
);
1012 names
= r_object(p
);
1015 varnames
= r_object(p
);
1016 if (varnames
== NULL
)
1018 freevars
= r_object(p
);
1019 if (freevars
== NULL
)
1021 cellvars
= r_object(p
);
1022 if (cellvars
== NULL
)
1024 filename
= r_object(p
);
1025 if (filename
== NULL
)
1030 firstlineno
= (int)r_long(p
);
1031 lnotab
= r_object(p
);
1035 v
= (PyObject
*) PyCode_New(
1036 argcount
, nlocals
, stacksize
, flags
,
1037 code
, consts
, names
, varnames
,
1038 freevars
, cellvars
, filename
, name
,
1039 firstlineno
, lnotab
);
1045 Py_XDECREF(varnames
);
1046 Py_XDECREF(freevars
);
1047 Py_XDECREF(cellvars
);
1048 Py_XDECREF(filename
);
1057 /* Bogus data got written, which isn't ideal.
1058 This will let you keep working and recover. */
1059 PyErr_SetString(PyExc_ValueError
, "bad marshal data (unknown type code)");
1069 read_object(RFILE
*p
)
1072 if (PyErr_Occurred()) {
1073 fprintf(stderr
, "XXX readobject called with exception set\n");
1077 if (v
== NULL
&& !PyErr_Occurred())
1078 PyErr_SetString(PyExc_TypeError
, "NULL object in marshal data for object");
1083 PyMarshal_ReadShortFromFile(FILE *fp
)
1089 rf
.end
= rf
.ptr
= NULL
;
1090 return r_short(&rf
);
1094 PyMarshal_ReadLongFromFile(FILE *fp
)
1099 rf
.ptr
= rf
.end
= NULL
;
1104 /* Return size of file in bytes; < 0 if unknown. */
1106 getfilesize(FILE *fp
)
1109 if (fstat(fileno(fp
), &st
) != 0)
1116 /* If we can get the size of the file up-front, and it's reasonably small,
1117 * read it in one gulp and delegate to ...FromString() instead. Much quicker
1118 * than reading a byte at a time from file; speeds .pyc imports.
1119 * CAUTION: since this may read the entire remainder of the file, don't
1120 * call it unless you know you're done with the file.
1123 PyMarshal_ReadLastObjectFromFile(FILE *fp
)
1125 /* 75% of 2.1's .pyc files can exploit SMALL_FILE_LIMIT.
1126 * REASONABLE_FILE_LIMIT is by defn something big enough for Tkinter.pyc.
1128 #define SMALL_FILE_LIMIT (1L << 14)
1129 #define REASONABLE_FILE_LIMIT (1L << 18)
1134 filesize
= getfilesize(fp
);
1136 char buf
[SMALL_FILE_LIMIT
];
1138 if (filesize
<= SMALL_FILE_LIMIT
)
1140 else if (filesize
<= REASONABLE_FILE_LIMIT
)
1141 pBuf
= (char *)PyMem_MALLOC(filesize
);
1145 /* filesize must fit into an int, because it
1146 is smaller than REASONABLE_FILE_LIMIT */
1147 n
= fread(pBuf
, 1, (int)filesize
, fp
);
1148 v
= PyMarshal_ReadObjectFromString(pBuf
, n
);
1156 /* We don't have fstat, or we do but the file is larger than
1157 * REASONABLE_FILE_LIMIT or malloc failed -- read a byte at a time.
1159 return PyMarshal_ReadObjectFromFile(fp
);
1161 #undef SMALL_FILE_LIMIT
1162 #undef REASONABLE_FILE_LIMIT
1166 PyMarshal_ReadObjectFromFile(FILE *fp
)
1171 rf
.strings
= PyList_New(0);
1173 rf
.ptr
= rf
.end
= NULL
;
1174 result
= r_object(&rf
);
1175 Py_DECREF(rf
.strings
);
1180 PyMarshal_ReadObjectFromString(char *str
, Py_ssize_t len
)
1187 rf
.strings
= PyList_New(0);
1189 result
= r_object(&rf
);
1190 Py_DECREF(rf
.strings
);
1195 set_error(int error
)
1198 case WFERR_NOMEMORY
:
1201 case WFERR_UNMARSHALLABLE
:
1202 PyErr_SetString(PyExc_ValueError
, "unmarshallable object");
1204 case WFERR_NESTEDTOODEEP
:
1206 PyErr_SetString(PyExc_ValueError
,
1207 "object too deeply nested to marshal");
1213 PyMarshal_WriteObjectToString(PyObject
*x
, int version
)
1217 wf
.str
= PyString_FromStringAndSize((char *)NULL
, 50);
1220 wf
.ptr
= PyString_AS_STRING((PyStringObject
*)wf
.str
);
1221 wf
.end
= wf
.ptr
+ PyString_Size(wf
.str
);
1222 wf
.error
= WFERR_OK
;
1224 wf
.version
= version
;
1225 wf
.strings
= (version
> 0) ? PyDict_New() : NULL
;
1227 Py_XDECREF(wf
.strings
);
1228 if (wf
.str
!= NULL
) {
1229 char *base
= PyString_AS_STRING((PyStringObject
*)wf
.str
);
1230 if (wf
.ptr
- base
> PY_SSIZE_T_MAX
) {
1232 PyErr_SetString(PyExc_OverflowError
,
1233 "too much marshall data for a string");
1236 _PyString_Resize(&wf
.str
, (Py_ssize_t
)(wf
.ptr
- base
));
1238 if (wf
.error
!= WFERR_OK
) {
1240 set_error(wf
.error
);
1246 /* And an interface for Python programs... */
1249 marshal_dump(PyObject
*self
, PyObject
*args
)
1254 int version
= Py_MARSHAL_VERSION
;
1255 if (!PyArg_ParseTuple(args
, "OO|i:dump", &x
, &f
, &version
))
1257 if (!PyFile_Check(f
)) {
1258 PyErr_SetString(PyExc_TypeError
,
1259 "marshal.dump() 2nd arg must be file");
1262 wf
.fp
= PyFile_AsFile(f
);
1264 wf
.ptr
= wf
.end
= NULL
;
1265 wf
.error
= WFERR_OK
;
1267 wf
.strings
= (version
> 0) ? PyDict_New() : 0;
1268 wf
.version
= version
;
1270 Py_XDECREF(wf
.strings
);
1271 if (wf
.error
!= WFERR_OK
) {
1272 set_error(wf
.error
);
1279 PyDoc_STRVAR(dump_doc
,
1280 "dump(value, file[, version])\n\
1282 Write the value on the open file. The value must be a supported type.\n\
1283 The file must be an open file object such as sys.stdout or returned by\n\
1284 open() or os.popen(). It must be opened in binary mode ('wb' or 'w+b').\n\
1286 If the value has (or contains an object that has) an unsupported type, a\n\
1287 ValueError exception is raised — but garbage data will also be written\n\
1288 to the file. The object will not be properly read back by load()\n\
1290 New in version 2.4: The version argument indicates the data format that\n\
1294 marshal_load(PyObject
*self
, PyObject
*f
)
1298 if (!PyFile_Check(f
)) {
1299 PyErr_SetString(PyExc_TypeError
,
1300 "marshal.load() arg must be file");
1303 rf
.fp
= PyFile_AsFile(f
);
1304 rf
.strings
= PyList_New(0);
1306 result
= read_object(&rf
);
1307 Py_DECREF(rf
.strings
);
1311 PyDoc_STRVAR(load_doc
,
1314 Read one value from the open file and return it. If no valid value is\n\
1315 read (e.g. because the data has a different Python version’s\n\
1316 incompatible marshal format), raise EOFError, ValueError or TypeError.\n\
1317 The file must be an open file object opened in binary mode ('rb' or\n\
1320 Note: If an object containing an unsupported type was marshalled with\n\
1321 dump(), load() will substitute None for the unmarshallable type.");
1325 marshal_dumps(PyObject
*self
, PyObject
*args
)
1328 int version
= Py_MARSHAL_VERSION
;
1329 if (!PyArg_ParseTuple(args
, "O|i:dumps", &x
, &version
))
1331 return PyMarshal_WriteObjectToString(x
, version
);
1334 PyDoc_STRVAR(dumps_doc
,
1335 "dumps(value[, version])\n\
1337 Return the string that would be written to a file by dump(value, file).\n\
1338 The value must be a supported type. Raise a ValueError exception if\n\
1339 value has (or contains an object that has) an unsupported type.\n\
1341 New in version 2.4: The version argument indicates the data format that\n\
1342 dumps should use.");
1346 marshal_loads(PyObject
*self
, PyObject
*args
)
1352 if (!PyArg_ParseTuple(args
, "s#:loads", &s
, &n
))
1357 rf
.strings
= PyList_New(0);
1359 result
= read_object(&rf
);
1360 Py_DECREF(rf
.strings
);
1364 PyDoc_STRVAR(loads_doc
,
1367 Convert the string to a value. If no valid value is found, raise\n\
1368 EOFError, ValueError or TypeError. Extra characters in the string are\n\
1371 static PyMethodDef marshal_methods
[] = {
1372 {"dump", marshal_dump
, METH_VARARGS
, dump_doc
},
1373 {"load", marshal_load
, METH_O
, load_doc
},
1374 {"dumps", marshal_dumps
, METH_VARARGS
, dumps_doc
},
1375 {"loads", marshal_loads
, METH_VARARGS
, loads_doc
},
1376 {NULL
, NULL
} /* sentinel */
1379 PyDoc_STRVAR(marshal_doc
,
1380 "This module contains functions that can read and write Python values in\n\
1381 a binary format. The format is specific to Python, but independent of\n\
1382 machine architecture issues.\n\
1384 Not all Python object types are supported; in general, only objects\n\
1385 whose value is independent from a particular invocation of Python can be\n\
1386 written and read by this module. The following types are supported:\n\
1387 None, integers, long integers, floating point numbers, strings, Unicode\n\
1388 objects, tuples, lists, sets, dictionaries, and code objects, where it\n\
1389 should be understood that tuples, lists and dictionaries are only\n\
1390 supported as long as the values contained therein are themselves\n\
1391 supported; and recursive lists and dictionaries should not be written\n\
1392 (they will cause infinite loops).\n\
1396 version -- indicates the format that the module uses. Version 0 is the\n\
1397 historical format, version 1 (added in Python 2.4) shares interned\n\
1398 strings and version 2 (added in Python 2.5) uses a binary format for\n\
1399 floating point numbers. (New in version 2.4)\n\
1403 dump() -- write value to a file\n\
1404 load() -- read value from a file\n\
1405 dumps() -- write value to a string\n\
1406 loads() -- read value from a string");
1410 PyMarshal_Init(void)
1412 PyObject
*mod
= Py_InitModule3("marshal", marshal_methods
,
1416 PyModule_AddIntConstant(mod
, "version", Py_MARSHAL_VERSION
);