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");
701 dx
= PyOS_string_to_double(buf
, NULL
, NULL
);
702 if (dx
== -1.0 && PyErr_Occurred()) {
706 retval
= PyFloat_FromDouble(dx
);
710 case TYPE_BINARY_FLOAT
:
712 unsigned char buf
[8];
714 if (r_string((char*)buf
, 8, p
) != 8) {
715 PyErr_SetString(PyExc_EOFError
,
716 "EOF read where object expected");
720 x
= _PyFloat_Unpack8(buf
, 1);
721 if (x
== -1.0 && PyErr_Occurred()) {
725 retval
= PyFloat_FromDouble(x
);
729 #ifndef WITHOUT_COMPLEX
735 if (n
== EOF
|| r_string(buf
, (int)n
, p
) != n
) {
736 PyErr_SetString(PyExc_EOFError
,
737 "EOF read where object expected");
742 c
.real
= PyOS_string_to_double(buf
, NULL
, NULL
);
743 if (c
.real
== -1.0 && PyErr_Occurred()) {
748 if (n
== EOF
|| r_string(buf
, (int)n
, p
) != n
) {
749 PyErr_SetString(PyExc_EOFError
,
750 "EOF read where object expected");
755 c
.imag
= PyOS_string_to_double(buf
, NULL
, NULL
);
756 if (c
.imag
== -1.0 && PyErr_Occurred()) {
760 retval
= PyComplex_FromCComplex(c
);
764 case TYPE_BINARY_COMPLEX
:
766 unsigned char buf
[8];
768 if (r_string((char*)buf
, 8, p
) != 8) {
769 PyErr_SetString(PyExc_EOFError
,
770 "EOF read where object expected");
774 c
.real
= _PyFloat_Unpack8(buf
, 1);
775 if (c
.real
== -1.0 && PyErr_Occurred()) {
779 if (r_string((char*)buf
, 8, p
) != 8) {
780 PyErr_SetString(PyExc_EOFError
,
781 "EOF read where object expected");
785 c
.imag
= _PyFloat_Unpack8(buf
, 1);
786 if (c
.imag
== -1.0 && PyErr_Occurred()) {
790 retval
= PyComplex_FromCComplex(c
);
798 if (n
< 0 || n
> INT_MAX
) {
799 PyErr_SetString(PyExc_ValueError
, "bad marshal data (string size out of range)");
803 v
= PyString_FromStringAndSize((char *)NULL
, n
);
808 if (r_string(PyString_AS_STRING(v
), (int)n
, p
) != n
) {
810 PyErr_SetString(PyExc_EOFError
,
811 "EOF read where object expected");
815 if (type
== TYPE_INTERNED
) {
816 PyString_InternInPlace(&v
);
817 if (PyList_Append(p
->strings
, v
) < 0) {
827 if (n
< 0 || n
>= PyList_GET_SIZE(p
->strings
)) {
828 PyErr_SetString(PyExc_ValueError
, "bad marshal data (string ref out of range)");
832 v
= PyList_GET_ITEM(p
->strings
, n
);
837 #ifdef Py_USING_UNICODE
843 if (n
< 0 || n
> INT_MAX
) {
844 PyErr_SetString(PyExc_ValueError
, "bad marshal data (unicode size out of range)");
848 buffer
= PyMem_NEW(char, n
);
849 if (buffer
== NULL
) {
850 retval
= PyErr_NoMemory();
853 if (r_string(buffer
, (int)n
, p
) != n
) {
855 PyErr_SetString(PyExc_EOFError
,
856 "EOF read where object expected");
860 v
= PyUnicode_DecodeUTF8(buffer
, n
, NULL
);
869 if (n
< 0 || n
> INT_MAX
) {
870 PyErr_SetString(PyExc_ValueError
, "bad marshal data (tuple size out of range)");
874 v
= PyTuple_New((int)n
);
879 for (i
= 0; i
< n
; i
++) {
882 if (!PyErr_Occurred())
883 PyErr_SetString(PyExc_TypeError
,
884 "NULL object in marshal data for tuple");
889 PyTuple_SET_ITEM(v
, (int)i
, v2
);
896 if (n
< 0 || n
> INT_MAX
) {
897 PyErr_SetString(PyExc_ValueError
, "bad marshal data (list size out of range)");
901 v
= PyList_New((int)n
);
906 for (i
= 0; i
< n
; i
++) {
909 if (!PyErr_Occurred())
910 PyErr_SetString(PyExc_TypeError
,
911 "NULL object in marshal data for list");
916 PyList_SET_ITEM(v
, (int)i
, v2
);
934 PyDict_SetItem(v
, key
, val
);
938 if (PyErr_Occurred()) {
948 if (n
< 0 || n
> INT_MAX
) {
949 PyErr_SetString(PyExc_ValueError
, "bad marshal data (set size out of range)");
953 v
= (type
== TYPE_SET
) ? PySet_New(NULL
) : PyFrozenSet_New(NULL
);
958 for (i
= 0; i
< n
; i
++) {
961 if (!PyErr_Occurred())
962 PyErr_SetString(PyExc_TypeError
,
963 "NULL object in marshal data for set");
968 if (PySet_Add(v
, v2
) == -1) {
980 if (PyEval_GetRestricted()) {
981 PyErr_SetString(PyExc_RuntimeError
,
982 "cannot unmarshal code objects in "
983 "restricted execution mode");
992 PyObject
*code
= NULL
;
993 PyObject
*consts
= NULL
;
994 PyObject
*names
= NULL
;
995 PyObject
*varnames
= NULL
;
996 PyObject
*freevars
= NULL
;
997 PyObject
*cellvars
= NULL
;
998 PyObject
*filename
= NULL
;
999 PyObject
*name
= NULL
;
1001 PyObject
*lnotab
= NULL
;
1005 /* XXX ignore long->int overflows for now */
1006 argcount
= (int)r_long(p
);
1007 nlocals
= (int)r_long(p
);
1008 stacksize
= (int)r_long(p
);
1009 flags
= (int)r_long(p
);
1013 consts
= r_object(p
);
1016 names
= r_object(p
);
1019 varnames
= r_object(p
);
1020 if (varnames
== NULL
)
1022 freevars
= r_object(p
);
1023 if (freevars
== NULL
)
1025 cellvars
= r_object(p
);
1026 if (cellvars
== NULL
)
1028 filename
= r_object(p
);
1029 if (filename
== NULL
)
1034 firstlineno
= (int)r_long(p
);
1035 lnotab
= r_object(p
);
1039 v
= (PyObject
*) PyCode_New(
1040 argcount
, nlocals
, stacksize
, flags
,
1041 code
, consts
, names
, varnames
,
1042 freevars
, cellvars
, filename
, name
,
1043 firstlineno
, lnotab
);
1049 Py_XDECREF(varnames
);
1050 Py_XDECREF(freevars
);
1051 Py_XDECREF(cellvars
);
1052 Py_XDECREF(filename
);
1061 /* Bogus data got written, which isn't ideal.
1062 This will let you keep working and recover. */
1063 PyErr_SetString(PyExc_ValueError
, "bad marshal data (unknown type code)");
1073 read_object(RFILE
*p
)
1076 if (PyErr_Occurred()) {
1077 fprintf(stderr
, "XXX readobject called with exception set\n");
1081 if (v
== NULL
&& !PyErr_Occurred())
1082 PyErr_SetString(PyExc_TypeError
, "NULL object in marshal data for object");
1087 PyMarshal_ReadShortFromFile(FILE *fp
)
1093 rf
.end
= rf
.ptr
= NULL
;
1094 return r_short(&rf
);
1098 PyMarshal_ReadLongFromFile(FILE *fp
)
1103 rf
.ptr
= rf
.end
= NULL
;
1108 /* Return size of file in bytes; < 0 if unknown. */
1110 getfilesize(FILE *fp
)
1113 if (fstat(fileno(fp
), &st
) != 0)
1120 /* If we can get the size of the file up-front, and it's reasonably small,
1121 * read it in one gulp and delegate to ...FromString() instead. Much quicker
1122 * than reading a byte at a time from file; speeds .pyc imports.
1123 * CAUTION: since this may read the entire remainder of the file, don't
1124 * call it unless you know you're done with the file.
1127 PyMarshal_ReadLastObjectFromFile(FILE *fp
)
1129 /* 75% of 2.1's .pyc files can exploit SMALL_FILE_LIMIT.
1130 * REASONABLE_FILE_LIMIT is by defn something big enough for Tkinter.pyc.
1132 #define SMALL_FILE_LIMIT (1L << 14)
1133 #define REASONABLE_FILE_LIMIT (1L << 18)
1138 filesize
= getfilesize(fp
);
1140 char buf
[SMALL_FILE_LIMIT
];
1142 if (filesize
<= SMALL_FILE_LIMIT
)
1144 else if (filesize
<= REASONABLE_FILE_LIMIT
)
1145 pBuf
= (char *)PyMem_MALLOC(filesize
);
1149 /* filesize must fit into an int, because it
1150 is smaller than REASONABLE_FILE_LIMIT */
1151 n
= fread(pBuf
, 1, (int)filesize
, fp
);
1152 v
= PyMarshal_ReadObjectFromString(pBuf
, n
);
1160 /* We don't have fstat, or we do but the file is larger than
1161 * REASONABLE_FILE_LIMIT or malloc failed -- read a byte at a time.
1163 return PyMarshal_ReadObjectFromFile(fp
);
1165 #undef SMALL_FILE_LIMIT
1166 #undef REASONABLE_FILE_LIMIT
1170 PyMarshal_ReadObjectFromFile(FILE *fp
)
1175 rf
.strings
= PyList_New(0);
1177 rf
.ptr
= rf
.end
= NULL
;
1178 result
= r_object(&rf
);
1179 Py_DECREF(rf
.strings
);
1184 PyMarshal_ReadObjectFromString(char *str
, Py_ssize_t len
)
1191 rf
.strings
= PyList_New(0);
1193 result
= r_object(&rf
);
1194 Py_DECREF(rf
.strings
);
1199 set_error(int error
)
1202 case WFERR_NOMEMORY
:
1205 case WFERR_UNMARSHALLABLE
:
1206 PyErr_SetString(PyExc_ValueError
, "unmarshallable object");
1208 case WFERR_NESTEDTOODEEP
:
1210 PyErr_SetString(PyExc_ValueError
,
1211 "object too deeply nested to marshal");
1217 PyMarshal_WriteObjectToString(PyObject
*x
, int version
)
1221 wf
.str
= PyString_FromStringAndSize((char *)NULL
, 50);
1224 wf
.ptr
= PyString_AS_STRING((PyStringObject
*)wf
.str
);
1225 wf
.end
= wf
.ptr
+ PyString_Size(wf
.str
);
1226 wf
.error
= WFERR_OK
;
1228 wf
.version
= version
;
1229 wf
.strings
= (version
> 0) ? PyDict_New() : NULL
;
1231 Py_XDECREF(wf
.strings
);
1232 if (wf
.str
!= NULL
) {
1233 char *base
= PyString_AS_STRING((PyStringObject
*)wf
.str
);
1234 if (wf
.ptr
- base
> PY_SSIZE_T_MAX
) {
1236 PyErr_SetString(PyExc_OverflowError
,
1237 "too much marshall data for a string");
1240 _PyString_Resize(&wf
.str
, (Py_ssize_t
)(wf
.ptr
- base
));
1242 if (wf
.error
!= WFERR_OK
) {
1244 set_error(wf
.error
);
1250 /* And an interface for Python programs... */
1253 marshal_dump(PyObject
*self
, PyObject
*args
)
1258 int version
= Py_MARSHAL_VERSION
;
1259 if (!PyArg_ParseTuple(args
, "OO|i:dump", &x
, &f
, &version
))
1261 if (!PyFile_Check(f
)) {
1262 PyErr_SetString(PyExc_TypeError
,
1263 "marshal.dump() 2nd arg must be file");
1266 wf
.fp
= PyFile_AsFile(f
);
1268 wf
.ptr
= wf
.end
= NULL
;
1269 wf
.error
= WFERR_OK
;
1271 wf
.strings
= (version
> 0) ? PyDict_New() : 0;
1272 wf
.version
= version
;
1274 Py_XDECREF(wf
.strings
);
1275 if (wf
.error
!= WFERR_OK
) {
1276 set_error(wf
.error
);
1283 PyDoc_STRVAR(dump_doc
,
1284 "dump(value, file[, version])\n\
1286 Write the value on the open file. The value must be a supported type.\n\
1287 The file must be an open file object such as sys.stdout or returned by\n\
1288 open() or os.popen(). It must be opened in binary mode ('wb' or 'w+b').\n\
1290 If the value has (or contains an object that has) an unsupported type, a\n\
1291 ValueError exception is raised — but garbage data will also be written\n\
1292 to the file. The object will not be properly read back by load()\n\
1294 New in version 2.4: The version argument indicates the data format that\n\
1298 marshal_load(PyObject
*self
, PyObject
*f
)
1302 if (!PyFile_Check(f
)) {
1303 PyErr_SetString(PyExc_TypeError
,
1304 "marshal.load() arg must be file");
1307 rf
.fp
= PyFile_AsFile(f
);
1308 rf
.strings
= PyList_New(0);
1310 result
= read_object(&rf
);
1311 Py_DECREF(rf
.strings
);
1315 PyDoc_STRVAR(load_doc
,
1318 Read one value from the open file and return it. If no valid value is\n\
1319 read (e.g. because the data has a different Python version’s\n\
1320 incompatible marshal format), raise EOFError, ValueError or TypeError.\n\
1321 The file must be an open file object opened in binary mode ('rb' or\n\
1324 Note: If an object containing an unsupported type was marshalled with\n\
1325 dump(), load() will substitute None for the unmarshallable type.");
1329 marshal_dumps(PyObject
*self
, PyObject
*args
)
1332 int version
= Py_MARSHAL_VERSION
;
1333 if (!PyArg_ParseTuple(args
, "O|i:dumps", &x
, &version
))
1335 return PyMarshal_WriteObjectToString(x
, version
);
1338 PyDoc_STRVAR(dumps_doc
,
1339 "dumps(value[, version])\n\
1341 Return the string that would be written to a file by dump(value, file).\n\
1342 The value must be a supported type. Raise a ValueError exception if\n\
1343 value has (or contains an object that has) an unsupported type.\n\
1345 New in version 2.4: The version argument indicates the data format that\n\
1346 dumps should use.");
1350 marshal_loads(PyObject
*self
, PyObject
*args
)
1356 if (!PyArg_ParseTuple(args
, "s#:loads", &s
, &n
))
1361 rf
.strings
= PyList_New(0);
1363 result
= read_object(&rf
);
1364 Py_DECREF(rf
.strings
);
1368 PyDoc_STRVAR(loads_doc
,
1371 Convert the string to a value. If no valid value is found, raise\n\
1372 EOFError, ValueError or TypeError. Extra characters in the string are\n\
1375 static PyMethodDef marshal_methods
[] = {
1376 {"dump", marshal_dump
, METH_VARARGS
, dump_doc
},
1377 {"load", marshal_load
, METH_O
, load_doc
},
1378 {"dumps", marshal_dumps
, METH_VARARGS
, dumps_doc
},
1379 {"loads", marshal_loads
, METH_VARARGS
, loads_doc
},
1380 {NULL
, NULL
} /* sentinel */
1383 PyDoc_STRVAR(marshal_doc
,
1384 "This module contains functions that can read and write Python values in\n\
1385 a binary format. The format is specific to Python, but independent of\n\
1386 machine architecture issues.\n\
1388 Not all Python object types are supported; in general, only objects\n\
1389 whose value is independent from a particular invocation of Python can be\n\
1390 written and read by this module. The following types are supported:\n\
1391 None, integers, long integers, floating point numbers, strings, Unicode\n\
1392 objects, tuples, lists, sets, dictionaries, and code objects, where it\n\
1393 should be understood that tuples, lists and dictionaries are only\n\
1394 supported as long as the values contained therein are themselves\n\
1395 supported; and recursive lists and dictionaries should not be written\n\
1396 (they will cause infinite loops).\n\
1400 version -- indicates the format that the module uses. Version 0 is the\n\
1401 historical format, version 1 (added in Python 2.4) shares interned\n\
1402 strings and version 2 (added in Python 2.5) uses a binary format for\n\
1403 floating point numbers. (New in version 2.4)\n\
1407 dump() -- write value to a file\n\
1408 load() -- read value from a file\n\
1409 dumps() -- write value to a string\n\
1410 loads() -- read value from a string");
1414 PyMarshal_Init(void)
1416 PyObject
*mod
= Py_InitModule3("marshal", marshal_methods
,
1420 PyModule_AddIntConstant(mod
, "version", Py_MARSHAL_VERSION
);