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 '>'
54
/* If fp == NULL, the following are valid: */
58
PyObject *strings; /* dict on marshal, list on unmarshal */
62
#define w_byte(c, p) if (((p)->fp)) putc((c), (p)->fp); \
63
else if ((p)->ptr != (p)->end) *(p)->ptr++ = (c); \
67
w_more(int c, WFILE *p)
69
Py_ssize_t size, newsize;
71
return; /* An error already occurred */
72
size = PyBytes_Size(p->str);
73
newsize = size + size + 1024;
74
if (newsize > 32*1024*1024) {
75
newsize = size + (size >> 3); /* 12.5% overallocation */
77
if (_PyBytes_Resize(&p->str, newsize) != 0) {
78
p->ptr = p->end = NULL;
81
p->ptr = PyBytes_AS_STRING((PyBytesObject *)p->str) + size;
83
PyBytes_AS_STRING((PyBytesObject *)p->str) + newsize;
84
*p->ptr++ = Py_SAFE_DOWNCAST(c, int, char);
89
w_string(char *s, int n, WFILE *p)
92
fwrite(s, 1, n, p->fp);
103
w_short(int x, WFILE *p)
105
w_byte((char)( x & 0xff), p);
106
w_byte((char)((x>> 8) & 0xff), p);
110
w_long(long x, WFILE *p)
112
w_byte((char)( x & 0xff), p);
113
w_byte((char)((x>> 8) & 0xff), p);
114
w_byte((char)((x>>16) & 0xff), p);
115
w_byte((char)((x>>24) & 0xff), p);
120
w_long64(long x, WFILE *p)
127
/* We assume that Python longs are stored internally in base some power of
128
2**15; for the sake of portability we'll always read and write them in base
131
#define PyLong_MARSHAL_SHIFT 15
132
#define PyLong_MARSHAL_BASE ((short)1 << PyLong_MARSHAL_SHIFT)
133
#define PyLong_MARSHAL_MASK (PyLong_MARSHAL_BASE - 1)
134
#if PyLong_SHIFT % PyLong_MARSHAL_SHIFT != 0
135
#error "PyLong_SHIFT must be a multiple of PyLong_MARSHAL_SHIFT"
137
#define PyLong_MARSHAL_RATIO (PyLong_SHIFT / PyLong_MARSHAL_SHIFT)
140
w_PyLong(const PyLongObject *ob, WFILE *p)
142
Py_ssize_t i, j, n, l;
145
w_byte(TYPE_LONG, p);
146
if (Py_SIZE(ob) == 0) {
151
/* set l to number of base PyLong_MARSHAL_BASE digits */
152
n = ABS(Py_SIZE(ob));
153
l = (n-1) * PyLong_MARSHAL_RATIO;
154
d = ob->ob_digit[n-1];
155
assert(d != 0); /* a PyLong is always normalized */
157
d >>= PyLong_MARSHAL_SHIFT;
160
w_long((long)(Py_SIZE(ob) > 0 ? l : -l), p);
162
for (i=0; i < n-1; i++) {
164
for (j=0; j < PyLong_MARSHAL_RATIO; j++) {
165
w_short(d & PyLong_MARSHAL_MASK, p);
166
d >>= PyLong_MARSHAL_SHIFT;
170
d = ob->ob_digit[n-1];
172
w_short(d & PyLong_MARSHAL_MASK, p);
173
d >>= PyLong_MARSHAL_SHIFT;
178
w_object(PyObject *v, WFILE *p)
184
if (p->depth > MAX_MARSHAL_STACK_DEPTH) {
187
else if (v == NULL) {
188
w_byte(TYPE_NULL, p);
190
else if (v == Py_None) {
191
w_byte(TYPE_NONE, p);
193
else if (v == PyExc_StopIteration) {
194
w_byte(TYPE_STOPITER, p);
196
else if (v == Py_Ellipsis) {
197
w_byte(TYPE_ELLIPSIS, p);
199
else if (v == Py_False) {
200
w_byte(TYPE_FALSE, p);
202
else if (v == Py_True) {
203
w_byte(TYPE_TRUE, p);
205
else if (PyLong_CheckExact(v)) {
206
long x = PyLong_AsLong(v);
207
if ((x == -1) && PyErr_Occurred()) {
208
PyLongObject *ob = (PyLongObject *)v;
214
long y = Py_ARITHMETIC_RIGHT_SHIFT(long, x, 31);
216
w_byte(TYPE_INT64, p);
227
else if (PyFloat_CheckExact(v)) {
228
if (p->version > 1) {
229
unsigned char buf[8];
230
if (_PyFloat_Pack8(PyFloat_AsDouble(v),
235
w_byte(TYPE_BINARY_FLOAT, p);
236
w_string((char*)buf, 8, p);
239
char buf[256]; /* Plenty to format any double */
240
n = _PyFloat_Repr(PyFloat_AS_DOUBLE(v),
242
w_byte(TYPE_FLOAT, p);
244
w_string(buf, (int)n, p);
247
#ifndef WITHOUT_COMPLEX
248
else if (PyComplex_CheckExact(v)) {
249
if (p->version > 1) {
250
unsigned char buf[8];
251
if (_PyFloat_Pack8(PyComplex_RealAsDouble(v),
256
w_byte(TYPE_BINARY_COMPLEX, p);
257
w_string((char*)buf, 8, p);
258
if (_PyFloat_Pack8(PyComplex_ImagAsDouble(v),
263
w_string((char*)buf, 8, p);
266
char buf[256]; /* Plenty to format any double */
267
w_byte(TYPE_COMPLEX, p);
268
n = _PyFloat_Repr(PyComplex_RealAsDouble(v),
272
w_string(buf, (int)n, p);
273
n = _PyFloat_Repr(PyComplex_ImagAsDouble(v),
276
w_string(buf, (int)n, p);
280
else if (PyBytes_CheckExact(v)) {
281
w_byte(TYPE_STRING, p);
282
n = PyBytes_GET_SIZE(v);
284
/* huge strings are not supported */
290
w_string(PyBytes_AS_STRING(v), (int)n, p);
292
else if (PyUnicode_CheckExact(v)) {
294
utf8 = PyUnicode_AsUTF8String(v);
300
w_byte(TYPE_UNICODE, p);
301
n = PyBytes_GET_SIZE(utf8);
308
w_string(PyBytes_AS_STRING(utf8), (int)n, p);
311
else if (PyTuple_CheckExact(v)) {
312
w_byte(TYPE_TUPLE, p);
315
for (i = 0; i < n; i++) {
316
w_object(PyTuple_GET_ITEM(v, i), p);
319
else if (PyList_CheckExact(v)) {
320
w_byte(TYPE_LIST, p);
321
n = PyList_GET_SIZE(v);
323
for (i = 0; i < n; i++) {
324
w_object(PyList_GET_ITEM(v, i), p);
327
else if (PyDict_CheckExact(v)) {
329
PyObject *key, *value;
330
w_byte(TYPE_DICT, p);
331
/* This one is NULL object terminated! */
333
while (PyDict_Next(v, &pos, &key, &value)) {
337
w_object((PyObject *)NULL, p);
339
else if (PyAnySet_CheckExact(v)) {
340
PyObject *value, *it;
342
if (PyObject_TypeCheck(v, &PySet_Type))
345
w_byte(TYPE_FROZENSET, p);
346
n = PyObject_Size(v);
353
it = PyObject_GetIter(v);
359
while ((value = PyIter_Next(it)) != NULL) {
364
if (PyErr_Occurred()) {
370
else if (PyCode_Check(v)) {
371
PyCodeObject *co = (PyCodeObject *)v;
372
w_byte(TYPE_CODE, p);
373
w_long(co->co_argcount, p);
374
w_long(co->co_kwonlyargcount, p);
375
w_long(co->co_nlocals, p);
376
w_long(co->co_stacksize, p);
377
w_long(co->co_flags, p);
378
w_object(co->co_code, p);
379
w_object(co->co_consts, p);
380
w_object(co->co_names, p);
381
w_object(co->co_varnames, p);
382
w_object(co->co_freevars, p);
383
w_object(co->co_cellvars, p);
384
w_object(co->co_filename, p);
385
w_object(co->co_name, p);
386
w_long(co->co_firstlineno, p);
387
w_object(co->co_lnotab, p);
389
else if (PyObject_CheckBuffer(v)) {
390
/* Write unknown buffer-style objects as a string */
392
PyBufferProcs *pb = v->ob_type->tp_as_buffer;
394
if ((*pb->bf_getbuffer)(v, &view, PyBUF_SIMPLE) != 0) {
395
w_byte(TYPE_UNKNOWN, p);
398
w_byte(TYPE_STRING, p);
407
w_string(s, (int)n, p);
408
if (pb->bf_releasebuffer != NULL)
409
(*pb->bf_releasebuffer)(v, &view);
412
w_byte(TYPE_UNKNOWN, p);
418
/* version currently has no effect for writing longs. */
420
PyMarshal_WriteLongToFile(long x, FILE *fp, int version)
427
wf.version = version;
432
PyMarshal_WriteObjectToFile(PyObject *x, FILE *fp, int version)
438
wf.strings = (version > 0) ? PyDict_New() : NULL;
439
wf.version = version;
441
Py_XDECREF(wf.strings);
444
typedef WFILE RFILE; /* Same struct with different invariants */
446
#define rs_byte(p) (((p)->ptr < (p)->end) ? (unsigned char)*(p)->ptr++ : EOF)
448
#define r_byte(p) ((p)->fp ? getc((p)->fp) : rs_byte(p))
451
r_string(char *s, int n, RFILE *p)
454
/* The result fits into int because it must be <=n. */
455
return (int)fread(s, 1, n, p->fp);
456
if (p->end - p->ptr < n)
457
n = (int)(p->end - p->ptr);
458
memcpy(s, p->ptr, n);
469
/* Sign-extension, in case short greater than 16 bits */
478
register FILE *fp = p->fp;
481
x |= (long)getc(fp) << 8;
482
x |= (long)getc(fp) << 16;
483
x |= (long)getc(fp) << 24;
487
x |= (long)rs_byte(p) << 8;
488
x |= (long)rs_byte(p) << 16;
489
x |= (long)rs_byte(p) << 24;
492
/* Sign extension for 64-bit machines */
493
x |= -(x & 0x80000000L);
498
/* r_long64 deals with the TYPE_INT64 code. On a machine with
499
sizeof(long) > 4, it returns a Python int object, else a Python long
500
object. Note that w_long64 writes out TYPE_INT if 32 bits is enough,
501
so there's no inefficiency here in returning a PyLong on 32-bit boxes
502
for everything written via TYPE_INT64 (i.e., if an int is written via
503
TYPE_INT64, it *needs* more than 32 bits).
508
long lo4 = r_long(p);
509
long hi4 = r_long(p);
511
long x = (hi4 << 32) | (lo4 & 0xFFFFFFFFL);
512
return PyLong_FromLong(x);
514
unsigned char buf[8];
516
int is_little_endian = (int)*(char*)&one;
517
if (is_little_endian) {
518
memcpy(buf, &lo4, 4);
519
memcpy(buf+4, &hi4, 4);
522
memcpy(buf, &hi4, 4);
523
memcpy(buf+4, &lo4, 4);
525
return _PyLong_FromByteArray(buf, 8, is_little_endian, 1);
539
return (PyObject *)_PyLong_New(0);
540
if (n < -INT_MAX || n > INT_MAX) {
541
PyErr_SetString(PyExc_ValueError,
542
"bad marshal data (long size out of range)");
546
size = 1 + (ABS(n)-1) / PyLong_MARSHAL_RATIO;
547
ob = _PyLong_New(size);
550
Py_SIZE(ob) = n > 0 ? size : -size;
552
for (i = 0; i < size-1; i++) {
554
for (j=0; j < PyLong_MARSHAL_RATIO; j++) {
556
if (md < 0 || md > PyLong_MARSHAL_BASE)
558
d += (digit)md << j*PyLong_MARSHAL_SHIFT;
563
for (j=0; j < (ABS(n)-1)%PyLong_MARSHAL_RATIO + 1; j++) {
565
if (md < 0 || md > PyLong_MARSHAL_BASE)
567
d += (digit)md << j*PyLong_MARSHAL_SHIFT;
569
ob->ob_digit[size-1] = d;
570
return (PyObject *)ob;
573
PyErr_SetString(PyExc_ValueError,
574
"bad marshal data (digit out of range in long)");
582
/* NULL is a valid return value, it does not necessarily means that
583
an exception is set. */
586
int type = r_byte(p);
591
if (p->depth > MAX_MARSHAL_STACK_DEPTH) {
593
PyErr_SetString(PyExc_ValueError, "recursion limit exceeded");
600
PyErr_SetString(PyExc_EOFError,
601
"EOF read where object expected");
615
Py_INCREF(PyExc_StopIteration);
616
retval = PyExc_StopIteration;
620
Py_INCREF(Py_Ellipsis);
621
retval = Py_Ellipsis;
635
retval = PyLong_FromLong(r_long(p));
639
retval = r_long64(p);
643
retval = r_PyLong(p);
651
if (n == EOF || r_string(buf, (int)n, p) != n) {
652
PyErr_SetString(PyExc_EOFError,
653
"EOF read where object expected");
659
PyFPE_START_PROTECT("atof", break)
660
dx = PyOS_ascii_atof(buf);
661
PyFPE_END_PROTECT(dx)
662
retval = PyFloat_FromDouble(dx);
666
case TYPE_BINARY_FLOAT:
668
unsigned char buf[8];
670
if (r_string((char*)buf, 8, p) != 8) {
671
PyErr_SetString(PyExc_EOFError,
672
"EOF read where object expected");
676
x = _PyFloat_Unpack8(buf, 1);
677
if (x == -1.0 && PyErr_Occurred()) {
681
retval = PyFloat_FromDouble(x);
685
#ifndef WITHOUT_COMPLEX
691
if (n == EOF || r_string(buf, (int)n, p) != n) {
692
PyErr_SetString(PyExc_EOFError,
693
"EOF read where object expected");
699
PyFPE_START_PROTECT("atof", break;)
700
c.real = PyOS_ascii_atof(buf);
703
if (n == EOF || r_string(buf, (int)n, p) != n) {
704
PyErr_SetString(PyExc_EOFError,
705
"EOF read where object expected");
710
PyFPE_START_PROTECT("atof", break)
711
c.imag = PyOS_ascii_atof(buf);
713
retval = PyComplex_FromCComplex(c);
717
case TYPE_BINARY_COMPLEX:
719
unsigned char buf[8];
721
if (r_string((char*)buf, 8, p) != 8) {
722
PyErr_SetString(PyExc_EOFError,
723
"EOF read where object expected");
727
c.real = _PyFloat_Unpack8(buf, 1);
728
if (c.real == -1.0 && PyErr_Occurred()) {
732
if (r_string((char*)buf, 8, p) != 8) {
733
PyErr_SetString(PyExc_EOFError,
734
"EOF read where object expected");
738
c.imag = _PyFloat_Unpack8(buf, 1);
739
if (c.imag == -1.0 && PyErr_Occurred()) {
743
retval = PyComplex_FromCComplex(c);
750
if (n < 0 || n > INT_MAX) {
751
PyErr_SetString(PyExc_ValueError, "bad marshal data (string size out of range)");
755
v = PyBytes_FromStringAndSize((char *)NULL, n);
760
if (r_string(PyBytes_AS_STRING(v), (int)n, p) != n) {
762
PyErr_SetString(PyExc_EOFError,
763
"EOF read where object expected");
775
if (n < 0 || n > INT_MAX) {
776
PyErr_SetString(PyExc_ValueError, "bad marshal data (unicode size out of range)");
780
buffer = PyMem_NEW(char, n);
781
if (buffer == NULL) {
782
retval = PyErr_NoMemory();
785
if (r_string(buffer, (int)n, p) != n) {
787
PyErr_SetString(PyExc_EOFError,
788
"EOF read where object expected");
792
v = PyUnicode_DecodeUTF8(buffer, n, NULL);
800
if (n < 0 || n > INT_MAX) {
801
PyErr_SetString(PyExc_ValueError, "bad marshal data (tuple size out of range)");
805
v = PyTuple_New((int)n);
810
for (i = 0; i < n; i++) {
813
if (!PyErr_Occurred())
814
PyErr_SetString(PyExc_TypeError,
815
"NULL object in marshal data for tuple");
820
PyTuple_SET_ITEM(v, (int)i, v2);
827
if (n < 0 || n > INT_MAX) {
828
PyErr_SetString(PyExc_ValueError, "bad marshal data (list size out of range)");
832
v = PyList_New((int)n);
837
for (i = 0; i < n; i++) {
840
if (!PyErr_Occurred())
841
PyErr_SetString(PyExc_TypeError,
842
"NULL object in marshal data for list");
847
PyList_SET_ITEM(v, (int)i, v2);
865
PyDict_SetItem(v, key, val);
869
if (PyErr_Occurred()) {
879
if (n < 0 || n > INT_MAX) {
880
PyErr_SetString(PyExc_ValueError, "bad marshal data (set size out of range)");
884
v = (type == TYPE_SET) ? PySet_New(NULL) : PyFrozenSet_New(NULL);
889
for (i = 0; i < n; i++) {
892
if (!PyErr_Occurred())
893
PyErr_SetString(PyExc_TypeError,
894
"NULL object in marshal data for set");
899
if (PySet_Add(v, v2) == -1) {
917
PyObject *code = NULL;
918
PyObject *consts = NULL;
919
PyObject *names = NULL;
920
PyObject *varnames = NULL;
921
PyObject *freevars = NULL;
922
PyObject *cellvars = NULL;
923
PyObject *filename = NULL;
924
PyObject *name = NULL;
926
PyObject *lnotab = NULL;
930
/* XXX ignore long->int overflows for now */
931
argcount = (int)r_long(p);
932
kwonlyargcount = (int)r_long(p);
933
nlocals = (int)r_long(p);
934
stacksize = (int)r_long(p);
935
flags = (int)r_long(p);
939
consts = r_object(p);
945
varnames = r_object(p);
946
if (varnames == NULL)
948
freevars = r_object(p);
949
if (freevars == NULL)
951
cellvars = r_object(p);
952
if (cellvars == NULL)
954
filename = r_object(p);
955
if (filename == NULL)
960
firstlineno = (int)r_long(p);
961
lnotab = r_object(p);
965
v = (PyObject *) PyCode_New(
966
argcount, kwonlyargcount,
967
nlocals, stacksize, flags,
968
code, consts, names, varnames,
969
freevars, cellvars, filename, name,
970
firstlineno, lnotab);
976
Py_XDECREF(varnames);
977
Py_XDECREF(freevars);
978
Py_XDECREF(cellvars);
979
Py_XDECREF(filename);
987
/* Bogus data got written, which isn't ideal.
988
This will let you keep working and recover. */
989
PyErr_SetString(PyExc_ValueError, "bad marshal data (unknown type code)");
999
read_object(RFILE *p)
1002
if (PyErr_Occurred()) {
1003
fprintf(stderr, "XXX readobject called with exception set\n");
1007
if (v == NULL && !PyErr_Occurred())
1008
PyErr_SetString(PyExc_TypeError, "NULL object in marshal data for object");
1013
PyMarshal_ReadShortFromFile(FILE *fp)
1019
rf.end = rf.ptr = NULL;
1020
return r_short(&rf);
1024
PyMarshal_ReadLongFromFile(FILE *fp)
1029
rf.ptr = rf.end = NULL;
1034
/* Return size of file in bytes; < 0 if unknown. */
1036
getfilesize(FILE *fp)
1039
if (fstat(fileno(fp), &st) != 0)
1046
/* If we can get the size of the file up-front, and it's reasonably small,
1047
* read it in one gulp and delegate to ...FromString() instead. Much quicker
1048
* than reading a byte at a time from file; speeds .pyc imports.
1049
* CAUTION: since this may read the entire remainder of the file, don't
1050
* call it unless you know you're done with the file.
1053
PyMarshal_ReadLastObjectFromFile(FILE *fp)
1055
/* 75% of 2.1's .pyc files can exploit SMALL_FILE_LIMIT.
1056
* REASONABLE_FILE_LIMIT is by defn something big enough for Tkinter.pyc.
1058
#define SMALL_FILE_LIMIT (1L << 14)
1059
#define REASONABLE_FILE_LIMIT (1L << 18)
1064
filesize = getfilesize(fp);
1066
char buf[SMALL_FILE_LIMIT];
1068
if (filesize <= SMALL_FILE_LIMIT)
1070
else if (filesize <= REASONABLE_FILE_LIMIT)
1071
pBuf = (char *)PyMem_MALLOC(filesize);
1075
/* filesize must fit into an int, because it
1076
is smaller than REASONABLE_FILE_LIMIT */
1077
n = fread(pBuf, 1, (int)filesize, fp);
1078
v = PyMarshal_ReadObjectFromString(pBuf, n);
1086
/* We don't have fstat, or we do but the file is larger than
1087
* REASONABLE_FILE_LIMIT or malloc failed -- read a byte at a time.
1089
return PyMarshal_ReadObjectFromFile(fp);
1091
#undef SMALL_FILE_LIMIT
1092
#undef REASONABLE_FILE_LIMIT
1096
PyMarshal_ReadObjectFromFile(FILE *fp)
1101
rf.strings = PyList_New(0);
1103
rf.ptr = rf.end = NULL;
1104
result = r_object(&rf);
1105
Py_DECREF(rf.strings);
1110
PyMarshal_ReadObjectFromString(char *str, Py_ssize_t len)
1117
rf.strings = PyList_New(0);
1119
result = r_object(&rf);
1120
Py_DECREF(rf.strings);
1125
PyMarshal_WriteObjectToString(PyObject *x, int version)
1128
PyObject *res = NULL;
1131
wf.str = PyBytes_FromStringAndSize((char *)NULL, 50);
1134
wf.ptr = PyBytes_AS_STRING((PyBytesObject *)wf.str);
1135
wf.end = wf.ptr + PyBytes_Size(wf.str);
1138
wf.version = version;
1139
wf.strings = (version > 0) ? PyDict_New() : NULL;
1141
Py_XDECREF(wf.strings);
1142
if (wf.str != NULL) {
1143
char *base = PyBytes_AS_STRING((PyBytesObject *)wf.str);
1144
if (wf.ptr - base > PY_SSIZE_T_MAX) {
1146
PyErr_SetString(PyExc_OverflowError,
1147
"too much marshal data for a string");
1150
if (_PyBytes_Resize(&wf.str, (Py_ssize_t)(wf.ptr - base)) < 0)
1155
PyErr_SetString(PyExc_ValueError,
1156
(wf.error==1)?"unmarshallable object"
1157
:"object too deeply nested to marshal");
1160
if (wf.str != NULL) {
1161
/* XXX Quick hack -- need to do this differently */
1162
res = PyBytes_FromObject(wf.str);
1168
/* And an interface for Python programs... */
1171
marshal_dump(PyObject *self, PyObject *args)
1173
/* XXX Quick hack -- need to do this differently */
1176
int version = Py_MARSHAL_VERSION;
1179
if (!PyArg_ParseTuple(args, "OO|i:dump", &x, &f, &version))
1181
s = PyMarshal_WriteObjectToString(x, version);
1184
res = PyObject_CallMethod(f, "write", "O", s);
1190
marshal_load(PyObject *self, PyObject *f)
1192
/* XXX Quick hack -- need to do this differently */
1193
PyObject *data, *result;
1195
data = PyObject_CallMethod(f, "read", "");
1199
if (PyBytes_Check(data)) {
1200
rf.ptr = PyBytes_AS_STRING(data);
1201
rf.end = rf.ptr + PyBytes_GET_SIZE(data);
1203
else if (PyBytes_Check(data)) {
1204
rf.ptr = PyBytes_AS_STRING(data);
1205
rf.end = rf.ptr + PyBytes_GET_SIZE(data);
1208
PyErr_Format(PyExc_TypeError,
1209
"f.read() returned neither string "
1210
"nor bytes but %.100s",
1211
data->ob_type->tp_name);
1215
rf.strings = PyList_New(0);
1217
result = read_object(&rf);
1218
Py_DECREF(rf.strings);
1224
marshal_dumps(PyObject *self, PyObject *args)
1227
int version = Py_MARSHAL_VERSION;
1228
if (!PyArg_ParseTuple(args, "O|i:dumps", &x, &version))
1230
return PyMarshal_WriteObjectToString(x, version);
1234
marshal_loads(PyObject *self, PyObject *args)
1241
if (!PyArg_ParseTuple(args, "s*:loads", &p))
1248
rf.strings = PyList_New(0);
1250
result = read_object(&rf);
1251
Py_DECREF(rf.strings);
1252
PyBuffer_Release(&p);
1256
static PyMethodDef marshal_methods[] = {
1257
{"dump", marshal_dump, METH_VARARGS},
1258
{"load", marshal_load, METH_O},
1259
{"dumps", marshal_dumps, METH_VARARGS},
1260
{"loads", marshal_loads, METH_VARARGS},
1261
{NULL, NULL} /* sentinel */
1264
static struct PyModuleDef marshalmodule = {
1265
PyModuleDef_HEAD_INIT,
1279
PyMarshal_Init(void)
1281
PyObject *mod = PyModule_Create(&marshalmodule);
1284
PyModule_AddIntConstant(mod, "version", Py_MARSHAL_VERSION);