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 /* High water mark to determine when the marshalled object is dangerously deep
15 * and risks coring the interpreter. When the object stack gets this deep,
16 * raise an exception instead of continuing.
18 #define MAX_MARSHAL_STACK_DEPTH 2000
22 #define TYPE_FALSE 'F'
24 #define TYPE_STOPITER 'S'
25 #define TYPE_ELLIPSIS '.'
27 #define TYPE_INT64 'I'
28 #define TYPE_FLOAT 'f'
29 #define TYPE_BINARY_FLOAT 'g'
30 #define TYPE_COMPLEX 'x'
31 #define TYPE_BINARY_COMPLEX 'y'
33 #define TYPE_STRING 's'
34 #define TYPE_INTERNED 't'
35 #define TYPE_STRINGREF 'R'
36 #define TYPE_TUPLE '('
40 #define TYPE_UNICODE 'u'
41 #define TYPE_UNKNOWN '?'
43 #define TYPE_FROZENSET '>'
49 /* If fp == NULL, the following are valid: */
53 PyObject
*strings
; /* dict on marshal, list on unmarshal */
57 #define w_byte(c, p) if (((p)->fp)) putc((c), (p)->fp); \
58 else if ((p)->ptr != (p)->end) *(p)->ptr++ = (c); \
62 w_more(int c
, WFILE
*p
)
64 Py_ssize_t size
, newsize
;
66 return; /* An error already occurred */
67 size
= PyString_Size(p
->str
);
68 newsize
= size
+ 1024;
69 if (_PyString_Resize(&p
->str
, newsize
) != 0) {
70 p
->ptr
= p
->end
= NULL
;
73 p
->ptr
= PyString_AS_STRING((PyStringObject
*)p
->str
) + size
;
75 PyString_AS_STRING((PyStringObject
*)p
->str
) + newsize
;
76 *p
->ptr
++ = Py_SAFE_DOWNCAST(c
, int, char);
81 w_string(char *s
, int n
, WFILE
*p
)
84 fwrite(s
, 1, n
, p
->fp
);
95 w_short(int x
, WFILE
*p
)
97 w_byte((char)( x
& 0xff), p
);
98 w_byte((char)((x
>> 8) & 0xff), p
);
102 w_long(long x
, WFILE
*p
)
104 w_byte((char)( x
& 0xff), p
);
105 w_byte((char)((x
>> 8) & 0xff), p
);
106 w_byte((char)((x
>>16) & 0xff), p
);
107 w_byte((char)((x
>>24) & 0xff), p
);
112 w_long64(long x
, WFILE
*p
)
120 w_object(PyObject
*v
, WFILE
*p
)
126 if (p
->depth
> MAX_MARSHAL_STACK_DEPTH
) {
129 else if (v
== NULL
) {
130 w_byte(TYPE_NULL
, p
);
132 else if (v
== Py_None
) {
133 w_byte(TYPE_NONE
, p
);
135 else if (v
== PyExc_StopIteration
) {
136 w_byte(TYPE_STOPITER
, p
);
138 else if (v
== Py_Ellipsis
) {
139 w_byte(TYPE_ELLIPSIS
, p
);
141 else if (v
== Py_False
) {
142 w_byte(TYPE_FALSE
, p
);
144 else if (v
== Py_True
) {
145 w_byte(TYPE_TRUE
, p
);
147 else if (PyInt_CheckExact(v
)) {
148 long x
= PyInt_AS_LONG((PyIntObject
*)v
);
150 long y
= Py_ARITHMETIC_RIGHT_SHIFT(long, x
, 31);
152 w_byte(TYPE_INT64
, p
);
162 else if (PyLong_CheckExact(v
)) {
163 PyLongObject
*ob
= (PyLongObject
*)v
;
164 w_byte(TYPE_LONG
, p
);
169 for (i
= 0; i
< n
; i
++)
170 w_short(ob
->ob_digit
[i
], p
);
172 else if (PyFloat_CheckExact(v
)) {
173 if (p
->version
> 1) {
174 unsigned char buf
[8];
175 if (_PyFloat_Pack8(PyFloat_AsDouble(v
),
180 w_byte(TYPE_BINARY_FLOAT
, p
);
181 w_string((char*)buf
, 8, p
);
184 char buf
[256]; /* Plenty to format any double */
185 PyFloat_AsReprString(buf
, (PyFloatObject
*)v
);
187 w_byte(TYPE_FLOAT
, p
);
189 w_string(buf
, (int)n
, p
);
192 #ifndef WITHOUT_COMPLEX
193 else if (PyComplex_CheckExact(v
)) {
194 if (p
->version
> 1) {
195 unsigned char buf
[8];
196 if (_PyFloat_Pack8(PyComplex_RealAsDouble(v
),
201 w_byte(TYPE_BINARY_COMPLEX
, p
);
202 w_string((char*)buf
, 8, p
);
203 if (_PyFloat_Pack8(PyComplex_ImagAsDouble(v
),
208 w_string((char*)buf
, 8, p
);
211 char buf
[256]; /* Plenty to format any double */
213 w_byte(TYPE_COMPLEX
, p
);
214 temp
= (PyFloatObject
*)PyFloat_FromDouble(
215 PyComplex_RealAsDouble(v
));
220 PyFloat_AsReprString(buf
, temp
);
224 w_string(buf
, (int)n
, p
);
225 temp
= (PyFloatObject
*)PyFloat_FromDouble(
226 PyComplex_ImagAsDouble(v
));
231 PyFloat_AsReprString(buf
, temp
);
235 w_string(buf
, (int)n
, p
);
239 else if (PyString_CheckExact(v
)) {
240 if (p
->strings
&& PyString_CHECK_INTERNED(v
)) {
241 PyObject
*o
= PyDict_GetItem(p
->strings
, v
);
243 long w
= PyInt_AsLong(o
);
244 w_byte(TYPE_STRINGREF
, p
);
250 o
= PyInt_FromSsize_t(PyDict_Size(p
->strings
));
252 PyDict_SetItem(p
->strings
, v
, o
) >= 0;
259 w_byte(TYPE_INTERNED
, p
);
263 w_byte(TYPE_STRING
, p
);
265 n
= PyString_GET_SIZE(v
);
267 /* huge strings are not supported */
273 w_string(PyString_AS_STRING(v
), (int)n
, p
);
275 #ifdef Py_USING_UNICODE
276 else if (PyUnicode_CheckExact(v
)) {
278 utf8
= PyUnicode_AsUTF8String(v
);
284 w_byte(TYPE_UNICODE
, p
);
285 n
= PyString_GET_SIZE(utf8
);
292 w_string(PyString_AS_STRING(utf8
), (int)n
, p
);
296 else if (PyTuple_CheckExact(v
)) {
297 w_byte(TYPE_TUPLE
, p
);
300 for (i
= 0; i
< n
; i
++) {
301 w_object(PyTuple_GET_ITEM(v
, i
), p
);
304 else if (PyList_CheckExact(v
)) {
305 w_byte(TYPE_LIST
, p
);
306 n
= PyList_GET_SIZE(v
);
308 for (i
= 0; i
< n
; i
++) {
309 w_object(PyList_GET_ITEM(v
, i
), p
);
312 else if (PyDict_CheckExact(v
)) {
314 PyObject
*key
, *value
;
315 w_byte(TYPE_DICT
, p
);
316 /* This one is NULL object terminated! */
318 while (PyDict_Next(v
, &pos
, &key
, &value
)) {
322 w_object((PyObject
*)NULL
, p
);
324 else if (PyAnySet_CheckExact(v
)) {
325 PyObject
*value
, *it
;
327 if (PyObject_TypeCheck(v
, &PySet_Type
))
330 w_byte(TYPE_FROZENSET
, p
);
331 n
= PyObject_Size(v
);
338 it
= PyObject_GetIter(v
);
344 while ((value
= PyIter_Next(it
)) != NULL
) {
349 if (PyErr_Occurred()) {
355 else if (PyCode_Check(v
)) {
356 PyCodeObject
*co
= (PyCodeObject
*)v
;
357 w_byte(TYPE_CODE
, p
);
358 w_long(co
->co_argcount
, p
);
359 w_long(co
->co_nlocals
, p
);
360 w_long(co
->co_stacksize
, p
);
361 w_long(co
->co_flags
, p
);
362 w_object(co
->co_code
, p
);
363 w_object(co
->co_consts
, p
);
364 w_object(co
->co_names
, p
);
365 w_object(co
->co_varnames
, p
);
366 w_object(co
->co_freevars
, p
);
367 w_object(co
->co_cellvars
, p
);
368 w_object(co
->co_filename
, p
);
369 w_object(co
->co_name
, p
);
370 w_long(co
->co_firstlineno
, p
);
371 w_object(co
->co_lnotab
, p
);
373 else if (PyObject_CheckReadBuffer(v
)) {
374 /* Write unknown buffer-style objects as a string */
376 PyBufferProcs
*pb
= v
->ob_type
->tp_as_buffer
;
377 w_byte(TYPE_STRING
, p
);
378 n
= (*pb
->bf_getreadbuffer
)(v
, 0, (void **)&s
);
385 w_string(s
, (int)n
, p
);
388 w_byte(TYPE_UNKNOWN
, p
);
395 /* version currently has no effect for writing longs. */
397 PyMarshal_WriteLongToFile(long x
, FILE *fp
, int version
)
404 wf
.version
= version
;
409 PyMarshal_WriteObjectToFile(PyObject
*x
, FILE *fp
, int version
)
415 wf
.strings
= (version
> 0) ? PyDict_New() : NULL
;
416 wf
.version
= version
;
418 Py_XDECREF(wf
.strings
);
421 typedef WFILE RFILE
; /* Same struct with different invariants */
423 #define rs_byte(p) (((p)->ptr < (p)->end) ? (unsigned char)*(p)->ptr++ : EOF)
425 #define r_byte(p) ((p)->fp ? getc((p)->fp) : rs_byte(p))
428 r_string(char *s
, int n
, RFILE
*p
)
431 /* The result fits into int because it must be <=n. */
432 return (int)fread(s
, 1, n
, p
->fp
);
433 if (p
->end
- p
->ptr
< n
)
434 n
= (int)(p
->end
- p
->ptr
);
435 memcpy(s
, p
->ptr
, n
);
446 /* Sign-extension, in case short greater than 16 bits */
455 register FILE *fp
= p
->fp
;
458 x
|= (long)getc(fp
) << 8;
459 x
|= (long)getc(fp
) << 16;
460 x
|= (long)getc(fp
) << 24;
464 x
|= (long)rs_byte(p
) << 8;
465 x
|= (long)rs_byte(p
) << 16;
466 x
|= (long)rs_byte(p
) << 24;
469 /* Sign extension for 64-bit machines */
470 x
|= -(x
& 0x80000000L
);
475 /* r_long64 deals with the TYPE_INT64 code. On a machine with
476 sizeof(long) > 4, it returns a Python int object, else a Python long
477 object. Note that w_long64 writes out TYPE_INT if 32 bits is enough,
478 so there's no inefficiency here in returning a PyLong on 32-bit boxes
479 for everything written via TYPE_INT64 (i.e., if an int is written via
480 TYPE_INT64, it *needs* more than 32 bits).
485 long lo4
= r_long(p
);
486 long hi4
= r_long(p
);
488 long x
= (hi4
<< 32) | (lo4
& 0xFFFFFFFFL
);
489 return PyInt_FromLong(x
);
491 unsigned char buf
[8];
493 int is_little_endian
= (int)*(char*)&one
;
494 if (is_little_endian
) {
495 memcpy(buf
, &lo4
, 4);
496 memcpy(buf
+4, &hi4
, 4);
499 memcpy(buf
, &hi4
, 4);
500 memcpy(buf
+4, &lo4
, 4);
502 return _PyLong_FromByteArray(buf
, 8, is_little_endian
, 1);
509 /* NULL is a valid return value, it does not necessarily means that
510 an exception is set. */
513 int type
= r_byte(p
);
518 if (p
->depth
> MAX_MARSHAL_STACK_DEPTH
) {
520 PyErr_SetString(PyExc_ValueError
, "recursion limit exceeded");
527 PyErr_SetString(PyExc_EOFError
,
528 "EOF read where object expected");
542 Py_INCREF(PyExc_StopIteration
);
543 retval
= PyExc_StopIteration
;
547 Py_INCREF(Py_Ellipsis
);
548 retval
= Py_Ellipsis
;
562 retval
= PyInt_FromLong(r_long(p
));
566 retval
= r_long64(p
);
574 if (n
< -INT_MAX
|| n
> INT_MAX
) {
575 PyErr_SetString(PyExc_ValueError
,
581 ob
= _PyLong_New(size
);
587 for (i
= 0; i
< size
; i
++) {
588 int digit
= r_short(p
);
591 PyErr_SetString(PyExc_ValueError
,
597 ob
->ob_digit
[i
] = digit
;
599 retval
= (PyObject
*)ob
;
608 if (n
== EOF
|| r_string(buf
, (int)n
, p
) != n
) {
609 PyErr_SetString(PyExc_EOFError
,
610 "EOF read where object expected");
616 PyFPE_START_PROTECT("atof", break)
617 dx
= PyOS_ascii_atof(buf
);
618 PyFPE_END_PROTECT(dx
)
619 retval
= PyFloat_FromDouble(dx
);
623 case TYPE_BINARY_FLOAT
:
625 unsigned char buf
[8];
627 if (r_string((char*)buf
, 8, p
) != 8) {
628 PyErr_SetString(PyExc_EOFError
,
629 "EOF read where object expected");
633 x
= _PyFloat_Unpack8(buf
, 1);
634 if (x
== -1.0 && PyErr_Occurred()) {
638 retval
= PyFloat_FromDouble(x
);
642 #ifndef WITHOUT_COMPLEX
648 if (n
== EOF
|| r_string(buf
, (int)n
, p
) != n
) {
649 PyErr_SetString(PyExc_EOFError
,
650 "EOF read where object expected");
656 PyFPE_START_PROTECT("atof", break;)
657 c
.real
= PyOS_ascii_atof(buf
);
660 if (n
== EOF
|| r_string(buf
, (int)n
, p
) != n
) {
661 PyErr_SetString(PyExc_EOFError
,
662 "EOF read where object expected");
667 PyFPE_START_PROTECT("atof", break)
668 c
.imag
= PyOS_ascii_atof(buf
);
670 retval
= PyComplex_FromCComplex(c
);
674 case TYPE_BINARY_COMPLEX
:
676 unsigned char buf
[8];
678 if (r_string((char*)buf
, 8, p
) != 8) {
679 PyErr_SetString(PyExc_EOFError
,
680 "EOF read where object expected");
684 c
.real
= _PyFloat_Unpack8(buf
, 1);
685 if (c
.real
== -1.0 && PyErr_Occurred()) {
689 if (r_string((char*)buf
, 8, p
) != 8) {
690 PyErr_SetString(PyExc_EOFError
,
691 "EOF read where object expected");
695 c
.imag
= _PyFloat_Unpack8(buf
, 1);
696 if (c
.imag
== -1.0 && PyErr_Occurred()) {
700 retval
= PyComplex_FromCComplex(c
);
708 if (n
< 0 || n
> INT_MAX
) {
709 PyErr_SetString(PyExc_ValueError
, "bad marshal data");
713 v
= PyString_FromStringAndSize((char *)NULL
, n
);
718 if (r_string(PyString_AS_STRING(v
), (int)n
, p
) != n
) {
720 PyErr_SetString(PyExc_EOFError
,
721 "EOF read where object expected");
725 if (type
== TYPE_INTERNED
) {
726 PyString_InternInPlace(&v
);
727 if (PyList_Append(p
->strings
, v
) < 0) {
737 if (n
< 0 || n
>= PyList_GET_SIZE(p
->strings
)) {
738 PyErr_SetString(PyExc_ValueError
, "bad marshal data");
742 v
= PyList_GET_ITEM(p
->strings
, n
);
747 #ifdef Py_USING_UNICODE
753 if (n
< 0 || n
> INT_MAX
) {
754 PyErr_SetString(PyExc_ValueError
, "bad marshal data");
758 buffer
= PyMem_NEW(char, n
);
759 if (buffer
== NULL
) {
760 retval
= PyErr_NoMemory();
763 if (r_string(buffer
, (int)n
, p
) != n
) {
765 PyErr_SetString(PyExc_EOFError
,
766 "EOF read where object expected");
770 v
= PyUnicode_DecodeUTF8(buffer
, n
, NULL
);
779 if (n
< 0 || n
> INT_MAX
) {
780 PyErr_SetString(PyExc_ValueError
, "bad marshal data");
784 v
= PyTuple_New((int)n
);
789 for (i
= 0; i
< n
; i
++) {
792 if (!PyErr_Occurred())
793 PyErr_SetString(PyExc_TypeError
,
794 "NULL object in marshal data");
799 PyTuple_SET_ITEM(v
, (int)i
, v2
);
806 if (n
< 0 || n
> INT_MAX
) {
807 PyErr_SetString(PyExc_ValueError
, "bad marshal data");
811 v
= PyList_New((int)n
);
816 for (i
= 0; i
< n
; i
++) {
819 if (!PyErr_Occurred())
820 PyErr_SetString(PyExc_TypeError
,
821 "NULL object in marshal data");
826 PyList_SET_ITEM(v
, (int)i
, v2
);
844 PyDict_SetItem(v
, key
, val
);
848 if (PyErr_Occurred()) {
858 if (n
< 0 || n
> INT_MAX
) {
859 PyErr_SetString(PyExc_ValueError
, "bad marshal data");
863 v
= (type
== TYPE_SET
) ? PySet_New(NULL
) : PyFrozenSet_New(NULL
);
868 for (i
= 0; i
< n
; i
++) {
871 if (!PyErr_Occurred())
872 PyErr_SetString(PyExc_TypeError
,
873 "NULL object in marshal data");
878 if (PySet_Add(v
, v2
) == -1) {
890 if (PyEval_GetRestricted()) {
891 PyErr_SetString(PyExc_RuntimeError
,
892 "cannot unmarshal code objects in "
893 "restricted execution mode");
902 PyObject
*code
= NULL
;
903 PyObject
*consts
= NULL
;
904 PyObject
*names
= NULL
;
905 PyObject
*varnames
= NULL
;
906 PyObject
*freevars
= NULL
;
907 PyObject
*cellvars
= NULL
;
908 PyObject
*filename
= NULL
;
909 PyObject
*name
= NULL
;
911 PyObject
*lnotab
= NULL
;
915 /* XXX ignore long->int overflows for now */
916 argcount
= (int)r_long(p
);
917 nlocals
= (int)r_long(p
);
918 stacksize
= (int)r_long(p
);
919 flags
= (int)r_long(p
);
923 consts
= r_object(p
);
929 varnames
= r_object(p
);
930 if (varnames
== NULL
)
932 freevars
= r_object(p
);
933 if (freevars
== NULL
)
935 cellvars
= r_object(p
);
936 if (cellvars
== NULL
)
938 filename
= r_object(p
);
939 if (filename
== NULL
)
944 firstlineno
= (int)r_long(p
);
945 lnotab
= r_object(p
);
949 v
= (PyObject
*) PyCode_New(
950 argcount
, nlocals
, stacksize
, flags
,
951 code
, consts
, names
, varnames
,
952 freevars
, cellvars
, filename
, name
,
953 firstlineno
, lnotab
);
959 Py_XDECREF(varnames
);
960 Py_XDECREF(freevars
);
961 Py_XDECREF(cellvars
);
962 Py_XDECREF(filename
);
971 /* Bogus data got written, which isn't ideal.
972 This will let you keep working and recover. */
973 PyErr_SetString(PyExc_ValueError
, "bad marshal data");
983 read_object(RFILE
*p
)
986 if (PyErr_Occurred()) {
987 fprintf(stderr
, "XXX readobject called with exception set\n");
991 if (v
== NULL
&& !PyErr_Occurred())
992 PyErr_SetString(PyExc_TypeError
, "NULL object in marshal data");
997 PyMarshal_ReadShortFromFile(FILE *fp
)
1003 rf
.end
= rf
.ptr
= NULL
;
1004 return r_short(&rf
);
1008 PyMarshal_ReadLongFromFile(FILE *fp
)
1013 rf
.ptr
= rf
.end
= NULL
;
1018 /* Return size of file in bytes; < 0 if unknown. */
1020 getfilesize(FILE *fp
)
1023 if (fstat(fileno(fp
), &st
) != 0)
1030 /* If we can get the size of the file up-front, and it's reasonably small,
1031 * read it in one gulp and delegate to ...FromString() instead. Much quicker
1032 * than reading a byte at a time from file; speeds .pyc imports.
1033 * CAUTION: since this may read the entire remainder of the file, don't
1034 * call it unless you know you're done with the file.
1037 PyMarshal_ReadLastObjectFromFile(FILE *fp
)
1039 /* 75% of 2.1's .pyc files can exploit SMALL_FILE_LIMIT.
1040 * REASONABLE_FILE_LIMIT is by defn something big enough for Tkinter.pyc.
1042 #define SMALL_FILE_LIMIT (1L << 14)
1043 #define REASONABLE_FILE_LIMIT (1L << 18)
1048 filesize
= getfilesize(fp
);
1050 char buf
[SMALL_FILE_LIMIT
];
1052 if (filesize
<= SMALL_FILE_LIMIT
)
1054 else if (filesize
<= REASONABLE_FILE_LIMIT
)
1055 pBuf
= (char *)PyMem_MALLOC(filesize
);
1059 /* filesize must fit into an int, because it
1060 is smaller than REASONABLE_FILE_LIMIT */
1061 n
= fread(pBuf
, 1, (int)filesize
, fp
);
1062 v
= PyMarshal_ReadObjectFromString(pBuf
, n
);
1070 /* We don't have fstat, or we do but the file is larger than
1071 * REASONABLE_FILE_LIMIT or malloc failed -- read a byte at a time.
1073 return PyMarshal_ReadObjectFromFile(fp
);
1075 #undef SMALL_FILE_LIMIT
1076 #undef REASONABLE_FILE_LIMIT
1080 PyMarshal_ReadObjectFromFile(FILE *fp
)
1085 rf
.strings
= PyList_New(0);
1087 rf
.ptr
= rf
.end
= NULL
;
1088 result
= r_object(&rf
);
1089 Py_DECREF(rf
.strings
);
1094 PyMarshal_ReadObjectFromString(char *str
, Py_ssize_t len
)
1101 rf
.strings
= PyList_New(0);
1103 result
= r_object(&rf
);
1104 Py_DECREF(rf
.strings
);
1109 PyMarshal_WriteObjectToString(PyObject
*x
, int version
)
1113 wf
.str
= PyString_FromStringAndSize((char *)NULL
, 50);
1116 wf
.ptr
= PyString_AS_STRING((PyStringObject
*)wf
.str
);
1117 wf
.end
= wf
.ptr
+ PyString_Size(wf
.str
);
1120 wf
.version
= version
;
1121 wf
.strings
= (version
> 0) ? PyDict_New() : NULL
;
1123 Py_XDECREF(wf
.strings
);
1124 if (wf
.str
!= NULL
) {
1125 char *base
= PyString_AS_STRING((PyStringObject
*)wf
.str
);
1126 if (wf
.ptr
- base
> PY_SSIZE_T_MAX
) {
1128 PyErr_SetString(PyExc_OverflowError
,
1129 "too much marshall data for a string");
1132 _PyString_Resize(&wf
.str
, (Py_ssize_t
)(wf
.ptr
- base
));
1136 PyErr_SetString(PyExc_ValueError
,
1137 (wf
.error
==1)?"unmarshallable object"
1138 :"object too deeply nested to marshal");
1144 /* And an interface for Python programs... */
1147 marshal_dump(PyObject
*self
, PyObject
*args
)
1152 int version
= Py_MARSHAL_VERSION
;
1153 if (!PyArg_ParseTuple(args
, "OO|i:dump", &x
, &f
, &version
))
1155 if (!PyFile_Check(f
)) {
1156 PyErr_SetString(PyExc_TypeError
,
1157 "marshal.dump() 2nd arg must be file");
1160 wf
.fp
= PyFile_AsFile(f
);
1162 wf
.ptr
= wf
.end
= NULL
;
1165 wf
.strings
= (version
> 0) ? PyDict_New() : 0;
1166 wf
.version
= version
;
1168 Py_XDECREF(wf
.strings
);
1170 PyErr_SetString(PyExc_ValueError
,
1171 (wf
.error
==1)?"unmarshallable object"
1172 :"object too deeply nested to marshal");
1180 marshal_load(PyObject
*self
, PyObject
*f
)
1184 if (!PyFile_Check(f
)) {
1185 PyErr_SetString(PyExc_TypeError
,
1186 "marshal.load() arg must be file");
1189 rf
.fp
= PyFile_AsFile(f
);
1190 rf
.strings
= PyList_New(0);
1192 result
= read_object(&rf
);
1193 Py_DECREF(rf
.strings
);
1198 marshal_dumps(PyObject
*self
, PyObject
*args
)
1201 int version
= Py_MARSHAL_VERSION
;
1202 if (!PyArg_ParseTuple(args
, "O|i:dumps", &x
, &version
))
1204 return PyMarshal_WriteObjectToString(x
, version
);
1208 marshal_loads(PyObject
*self
, PyObject
*args
)
1214 if (!PyArg_ParseTuple(args
, "s#:loads", &s
, &n
))
1219 rf
.strings
= PyList_New(0);
1221 result
= read_object(&rf
);
1222 Py_DECREF(rf
.strings
);
1226 static PyMethodDef marshal_methods
[] = {
1227 {"dump", marshal_dump
, METH_VARARGS
},
1228 {"load", marshal_load
, METH_O
},
1229 {"dumps", marshal_dumps
, METH_VARARGS
},
1230 {"loads", marshal_loads
, METH_VARARGS
},
1231 {NULL
, NULL
} /* sentinel */
1235 PyMarshal_Init(void)
1237 PyObject
*mod
= Py_InitModule("marshal", marshal_methods
);
1240 PyModule_AddIntConstant(mod
, "version", Py_MARSHAL_VERSION
);