2
/* Write Python objects to files and read them back.
3
This is primarily intended for writing and reading compiled Python code,
4
even though dicts, lists, sets and frozensets, not commonly seen in
5
code objects, are supported.
6
Version 3 of this protocol properly supports circular links
9
#define PY_SSIZE_T_CLEAN
12
#include "longintrepr.h"
16
#define ABS(x) ((x) < 0 ? -(x) : (x))
18
/* High water mark to determine when the marshalled object is dangerously deep
19
* and risks coring the interpreter. When the object stack gets this deep,
20
* raise an exception instead of continuing.
21
* On Windows debug builds, reduce this value.
23
#if defined(MS_WINDOWS) && defined(_DEBUG)
24
#define MAX_MARSHAL_STACK_DEPTH 1500
26
#define MAX_MARSHAL_STACK_DEPTH 2000
31
#define TYPE_FALSE 'F'
33
#define TYPE_STOPITER 'S'
34
#define TYPE_ELLIPSIS '.'
36
#define TYPE_FLOAT 'f'
37
#define TYPE_BINARY_FLOAT 'g'
38
#define TYPE_COMPLEX 'x'
39
#define TYPE_BINARY_COMPLEX 'y'
41
#define TYPE_STRING 's'
42
#define TYPE_INTERNED 't'
44
#define TYPE_TUPLE '('
48
#define TYPE_UNICODE 'u'
49
#define TYPE_UNKNOWN '?'
51
#define TYPE_FROZENSET '>'
52
#define FLAG_REF '\x80' /* with a type, add obj to index */
54
#define TYPE_ASCII 'a'
55
#define TYPE_ASCII_INTERNED 'A'
56
#define TYPE_SMALL_TUPLE ')'
57
#define TYPE_SHORT_ASCII 'z'
58
#define TYPE_SHORT_ASCII_INTERNED 'Z'
61
#define WFERR_UNMARSHALLABLE 1
62
#define WFERR_NESTEDTOODEEP 2
63
#define WFERR_NOMEMORY 3
67
int error; /* see WFERR_* values */
69
/* If fp == NULL, the following are valid: */
70
PyObject *readable; /* Stream-like object being read from */
72
PyObject *current_filename;
77
PyObject *refs; /* dict on marshal, list on unmarshal */
81
#define w_byte(c, p) if (((p)->fp)) putc((c), (p)->fp); \
82
else if ((p)->ptr != (p)->end) *(p)->ptr++ = (c); \
86
w_more(char c, WFILE *p)
88
Py_ssize_t size, newsize;
90
return; /* An error already occurred */
91
size = PyBytes_Size(p->str);
92
newsize = size + size + 1024;
93
if (newsize > 32*1024*1024) {
94
newsize = size + (size >> 3); /* 12.5% overallocation */
96
if (_PyBytes_Resize(&p->str, newsize) != 0) {
97
p->ptr = p->end = NULL;
100
p->ptr = PyBytes_AS_STRING((PyBytesObject *)p->str) + size;
102
PyBytes_AS_STRING((PyBytesObject *)p->str) + newsize;
108
w_string(const char *s, Py_ssize_t n, WFILE *p)
111
fwrite(s, 1, n, p->fp);
122
w_short(int x, WFILE *p)
124
w_byte((char)( x & 0xff), p);
125
w_byte((char)((x>> 8) & 0xff), p);
129
w_long(long x, WFILE *p)
131
w_byte((char)( x & 0xff), p);
132
w_byte((char)((x>> 8) & 0xff), p);
133
w_byte((char)((x>>16) & 0xff), p);
134
w_byte((char)((x>>24) & 0xff), p);
137
#define SIZE32_MAX 0x7FFFFFFF
139
#if SIZEOF_SIZE_T > 4
140
# define W_SIZE(n, p) do { \
141
if ((n) > SIZE32_MAX) { \
143
(p)->error = WFERR_UNMARSHALLABLE; \
146
w_long((long)(n), p); \
149
# define W_SIZE w_long
153
w_pstring(const char *s, Py_ssize_t n, WFILE *p)
160
w_short_pstring(const char *s, Py_ssize_t n, WFILE *p)
162
w_byte(Py_SAFE_DOWNCAST(n, Py_ssize_t, unsigned char), p);
166
/* We assume that Python ints are stored internally in base some power of
167
2**15; for the sake of portability we'll always read and write them in base
170
#define PyLong_MARSHAL_SHIFT 15
171
#define PyLong_MARSHAL_BASE ((short)1 << PyLong_MARSHAL_SHIFT)
172
#define PyLong_MARSHAL_MASK (PyLong_MARSHAL_BASE - 1)
173
#if PyLong_SHIFT % PyLong_MARSHAL_SHIFT != 0
174
#error "PyLong_SHIFT must be a multiple of PyLong_MARSHAL_SHIFT"
176
#define PyLong_MARSHAL_RATIO (PyLong_SHIFT / PyLong_MARSHAL_SHIFT)
178
#define W_TYPE(t, p) do { \
179
w_byte((t) | flag, (p)); \
183
w_PyLong(const PyLongObject *ob, char flag, WFILE *p)
185
Py_ssize_t i, j, n, l;
188
W_TYPE(TYPE_LONG, p);
189
if (Py_SIZE(ob) == 0) {
194
/* set l to number of base PyLong_MARSHAL_BASE digits */
195
n = ABS(Py_SIZE(ob));
196
l = (n-1) * PyLong_MARSHAL_RATIO;
197
d = ob->ob_digit[n-1];
198
assert(d != 0); /* a PyLong is always normalized */
200
d >>= PyLong_MARSHAL_SHIFT;
203
if (l > SIZE32_MAX) {
205
p->error = WFERR_UNMARSHALLABLE;
208
w_long((long)(Py_SIZE(ob) > 0 ? l : -l), p);
210
for (i=0; i < n-1; i++) {
212
for (j=0; j < PyLong_MARSHAL_RATIO; j++) {
213
w_short(d & PyLong_MARSHAL_MASK, p);
214
d >>= PyLong_MARSHAL_SHIFT;
218
d = ob->ob_digit[n-1];
220
w_short(d & PyLong_MARSHAL_MASK, p);
221
d >>= PyLong_MARSHAL_SHIFT;
226
w_ref(PyObject *v, char *flag, WFILE *p)
231
if (p->version < 3 || p->refs == NULL)
232
return 0; /* not writing object references */
234
/* if it has only one reference, it definitely isn't shared */
235
if (Py_REFCNT(v) == 1)
238
id = PyLong_FromVoidPtr((void*)v);
241
idx = PyDict_GetItem(p->refs, id);
243
/* write the reference index to the stream */
244
long w = PyLong_AsLong(idx);
246
if (w == -1 && PyErr_Occurred()) {
249
/* we don't store "long" indices in the dict */
250
assert(0 <= w && w <= 0x7fffffff);
256
Py_ssize_t s = PyDict_Size(p->refs);
257
/* we don't support long indices */
258
if (s >= 0x7fffffff) {
259
PyErr_SetString(PyExc_ValueError, "too many objects");
262
idx = PyLong_FromSsize_t(s);
263
ok = idx && PyDict_SetItem(p->refs, id, idx) == 0;
272
p->error = WFERR_UNMARSHALLABLE;
277
w_complex_object(PyObject *v, char flag, WFILE *p);
280
w_object(PyObject *v, WFILE *p)
286
if (p->depth > MAX_MARSHAL_STACK_DEPTH) {
287
p->error = WFERR_NESTEDTOODEEP;
289
else if (v == NULL) {
290
w_byte(TYPE_NULL, p);
292
else if (v == Py_None) {
293
w_byte(TYPE_NONE, p);
295
else if (v == PyExc_StopIteration) {
296
w_byte(TYPE_STOPITER, p);
298
else if (v == Py_Ellipsis) {
299
w_byte(TYPE_ELLIPSIS, p);
301
else if (v == Py_False) {
302
w_byte(TYPE_FALSE, p);
304
else if (v == Py_True) {
305
w_byte(TYPE_TRUE, p);
307
else if (!w_ref(v, &flag, p))
308
w_complex_object(v, flag, p);
314
w_complex_object(PyObject *v, char flag, WFILE *p)
318
if (PyLong_CheckExact(v)) {
319
long x = PyLong_AsLong(v);
320
if ((x == -1) && PyErr_Occurred()) {
321
PyLongObject *ob = (PyLongObject *)v;
323
w_PyLong(ob, flag, p);
327
long y = Py_ARITHMETIC_RIGHT_SHIFT(long, x, 31);
329
/* Too large for TYPE_INT */
330
w_PyLong((PyLongObject*)v, flag, p);
340
else if (PyFloat_CheckExact(v)) {
341
if (p->version > 1) {
342
unsigned char buf[8];
343
if (_PyFloat_Pack8(PyFloat_AsDouble(v),
345
p->error = WFERR_UNMARSHALLABLE;
348
W_TYPE(TYPE_BINARY_FLOAT, p);
349
w_string((char*)buf, 8, p);
352
char *buf = PyOS_double_to_string(PyFloat_AS_DOUBLE(v),
355
p->error = WFERR_NOMEMORY;
359
W_TYPE(TYPE_FLOAT, p);
365
else if (PyComplex_CheckExact(v)) {
366
if (p->version > 1) {
367
unsigned char buf[8];
368
if (_PyFloat_Pack8(PyComplex_RealAsDouble(v),
370
p->error = WFERR_UNMARSHALLABLE;
373
W_TYPE(TYPE_BINARY_COMPLEX, p);
374
w_string((char*)buf, 8, p);
375
if (_PyFloat_Pack8(PyComplex_ImagAsDouble(v),
377
p->error = WFERR_UNMARSHALLABLE;
380
w_string((char*)buf, 8, p);
384
W_TYPE(TYPE_COMPLEX, p);
385
buf = PyOS_double_to_string(PyComplex_RealAsDouble(v),
388
p->error = WFERR_NOMEMORY;
395
buf = PyOS_double_to_string(PyComplex_ImagAsDouble(v),
398
p->error = WFERR_NOMEMORY;
407
else if (PyBytes_CheckExact(v)) {
408
W_TYPE(TYPE_STRING, p);
409
w_pstring(PyBytes_AS_STRING(v), PyBytes_GET_SIZE(v), p);
411
else if (PyUnicode_CheckExact(v)) {
412
if (p->version >= 4 && PyUnicode_IS_ASCII(v)) {
413
int is_short = PyUnicode_GET_LENGTH(v) < 256;
415
if (PyUnicode_CHECK_INTERNED(v))
416
W_TYPE(TYPE_SHORT_ASCII_INTERNED, p);
418
W_TYPE(TYPE_SHORT_ASCII, p);
419
w_short_pstring((char *) PyUnicode_1BYTE_DATA(v),
420
PyUnicode_GET_LENGTH(v), p);
423
if (PyUnicode_CHECK_INTERNED(v))
424
W_TYPE(TYPE_ASCII_INTERNED, p);
426
W_TYPE(TYPE_ASCII, p);
427
w_pstring((char *) PyUnicode_1BYTE_DATA(v),
428
PyUnicode_GET_LENGTH(v), p);
433
utf8 = PyUnicode_AsEncodedString(v, "utf8", "surrogatepass");
436
p->error = WFERR_UNMARSHALLABLE;
439
if (p->version >= 3 && PyUnicode_CHECK_INTERNED(v))
440
W_TYPE(TYPE_INTERNED, p);
442
W_TYPE(TYPE_UNICODE, p);
443
w_pstring(PyBytes_AS_STRING(utf8), PyBytes_GET_SIZE(utf8), p);
447
else if (PyTuple_CheckExact(v)) {
449
if (p->version >= 4 && n < 256) {
450
W_TYPE(TYPE_SMALL_TUPLE, p);
451
w_byte((unsigned char)n, p);
454
W_TYPE(TYPE_TUPLE, p);
457
for (i = 0; i < n; i++) {
458
w_object(PyTuple_GET_ITEM(v, i), p);
461
else if (PyList_CheckExact(v)) {
462
W_TYPE(TYPE_LIST, p);
463
n = PyList_GET_SIZE(v);
465
for (i = 0; i < n; i++) {
466
w_object(PyList_GET_ITEM(v, i), p);
469
else if (PyDict_CheckExact(v)) {
471
PyObject *key, *value;
472
W_TYPE(TYPE_DICT, p);
473
/* This one is NULL object terminated! */
475
while (PyDict_Next(v, &pos, &key, &value)) {
479
w_object((PyObject *)NULL, p);
481
else if (PyAnySet_CheckExact(v)) {
482
PyObject *value, *it;
484
if (PyObject_TypeCheck(v, &PySet_Type))
487
W_TYPE(TYPE_FROZENSET, p);
488
n = PyObject_Size(v);
491
p->error = WFERR_UNMARSHALLABLE;
495
it = PyObject_GetIter(v);
498
p->error = WFERR_UNMARSHALLABLE;
501
while ((value = PyIter_Next(it)) != NULL) {
506
if (PyErr_Occurred()) {
508
p->error = WFERR_UNMARSHALLABLE;
512
else if (PyCode_Check(v)) {
513
PyCodeObject *co = (PyCodeObject *)v;
514
W_TYPE(TYPE_CODE, p);
515
w_long(co->co_argcount, p);
516
w_long(co->co_kwonlyargcount, p);
517
w_long(co->co_nlocals, p);
518
w_long(co->co_stacksize, p);
519
w_long(co->co_flags, p);
520
w_object(co->co_code, p);
521
w_object(co->co_consts, p);
522
w_object(co->co_names, p);
523
w_object(co->co_varnames, p);
524
w_object(co->co_freevars, p);
525
w_object(co->co_cellvars, p);
526
w_object(co->co_filename, p);
527
w_object(co->co_name, p);
528
w_long(co->co_firstlineno, p);
529
w_object(co->co_lnotab, p);
531
else if (PyObject_CheckBuffer(v)) {
532
/* Write unknown buffer-style objects as a string */
534
if (PyObject_GetBuffer(v, &view, PyBUF_SIMPLE) != 0) {
535
w_byte(TYPE_UNKNOWN, p);
537
p->error = WFERR_UNMARSHALLABLE;
540
W_TYPE(TYPE_STRING, p);
541
w_pstring(view.buf, view.len, p);
542
PyBuffer_Release(&view);
545
W_TYPE(TYPE_UNKNOWN, p);
546
p->error = WFERR_UNMARSHALLABLE;
550
/* version currently has no effect for writing ints. */
552
PyMarshal_WriteLongToFile(long x, FILE *fp, int version)
559
wf.version = version;
564
PyMarshal_WriteObjectToFile(PyObject *x, FILE *fp, int version)
571
if ((wf.refs = PyDict_New()) == NULL)
572
return; /* caller mush check PyErr_Occurred() */
575
wf.version = version;
580
typedef WFILE RFILE; /* Same struct with different invariants */
583
r_string(Py_ssize_t n, RFILE *p)
585
Py_ssize_t read = -1;
587
if (p->ptr != NULL) {
588
/* Fast path for loads() */
590
Py_ssize_t left = p->end - p->ptr;
592
PyErr_SetString(PyExc_EOFError,
593
"marshal data too short");
599
if (p->buf == NULL) {
600
p->buf = PyMem_MALLOC(n);
601
if (p->buf == NULL) {
607
else if (p->buf_size < n) {
608
p->buf = PyMem_REALLOC(p->buf, n);
609
if (p->buf == NULL) {
617
assert(p->fp != NULL);
618
read = fread(p->buf, 1, n, p->fp);
621
_Py_IDENTIFIER(readinto);
622
PyObject *res, *mview;
625
if (PyBuffer_FillInfo(&buf, NULL, p->buf, n, 0, PyBUF_CONTIG) == -1)
627
mview = PyMemoryView_FromBuffer(&buf);
631
res = _PyObject_CallMethodId(p->readable, &PyId_readinto, "N", mview);
633
read = PyNumber_AsSsize_t(res, PyExc_ValueError);
638
if (!PyErr_Occurred()) {
640
PyErr_Format(PyExc_ValueError,
641
"read() returned too much data: "
642
"%zd bytes requested, %zd returned",
645
PyErr_SetString(PyExc_EOFError,
646
"EOF read where not expected");
658
if (p->ptr != NULL) {
660
c = (unsigned char) *p->ptr++;
668
char *ptr = r_string(1, p);
670
c = *(unsigned char *) ptr;
679
unsigned char *buffer;
681
buffer = (unsigned char *) r_string(2, p);
682
if (buffer != NULL) {
685
/* Sign-extension, in case short greater than 16 bits */
695
unsigned char *buffer;
697
buffer = (unsigned char *) r_string(4, p);
698
if (buffer != NULL) {
700
x |= (long)buffer[1] << 8;
701
x |= (long)buffer[2] << 16;
702
x |= (long)buffer[3] << 24;
704
/* Sign extension for 64-bit machines */
705
x |= -(x & 0x80000000L);
716
int j, md, shorts_in_top_digit;
720
if (PyErr_Occurred())
723
return (PyObject *)_PyLong_New(0);
724
if (n < -SIZE32_MAX || n > SIZE32_MAX) {
725
PyErr_SetString(PyExc_ValueError,
726
"bad marshal data (long size out of range)");
730
size = 1 + (ABS(n) - 1) / PyLong_MARSHAL_RATIO;
731
shorts_in_top_digit = 1 + (ABS(n) - 1) % PyLong_MARSHAL_RATIO;
732
ob = _PyLong_New(size);
736
Py_SIZE(ob) = n > 0 ? size : -size;
738
for (i = 0; i < size-1; i++) {
740
for (j=0; j < PyLong_MARSHAL_RATIO; j++) {
742
if (PyErr_Occurred()) {
746
if (md < 0 || md > PyLong_MARSHAL_BASE)
748
d += (digit)md << j*PyLong_MARSHAL_SHIFT;
754
for (j=0; j < shorts_in_top_digit; j++) {
756
if (PyErr_Occurred()) {
760
if (md < 0 || md > PyLong_MARSHAL_BASE)
762
/* topmost marshal digit should be nonzero */
763
if (md == 0 && j == shorts_in_top_digit - 1) {
765
PyErr_SetString(PyExc_ValueError,
766
"bad marshal data (unnormalized long data)");
769
d += (digit)md << j*PyLong_MARSHAL_SHIFT;
771
if (PyErr_Occurred()) {
775
/* top digit should be nonzero, else the resulting PyLong won't be
777
ob->ob_digit[size-1] = d;
778
return (PyObject *)ob;
781
PyErr_SetString(PyExc_ValueError,
782
"bad marshal data (digit out of range in long)");
786
/* allocate the reflist index for a new object. Return -1 on failure */
788
r_ref_reserve(int flag, RFILE *p)
790
if (flag) { /* currently only FLAG_REF is defined */
791
Py_ssize_t idx = PyList_GET_SIZE(p->refs);
792
if (idx >= 0x7ffffffe) {
793
PyErr_SetString(PyExc_ValueError, "bad marshal data (index list too large)");
796
if (PyList_Append(p->refs, Py_None) < 0)
803
/* insert the new object 'o' to the reflist at previously
804
* allocated index 'idx'.
805
* 'o' can be NULL, in which case nothing is done.
806
* if 'o' was non-NULL, and the function succeeds, 'o' is returned.
807
* if 'o' was non-NULL, and the function fails, 'o' is released and
808
* NULL returned. This simplifies error checking at the call site since
809
* a single test for NULL for the function result is enough.
812
r_ref_insert(PyObject *o, Py_ssize_t idx, int flag, RFILE *p)
814
if (o != NULL && flag) { /* currently only FLAG_REF is defined */
815
PyObject *tmp = PyList_GET_ITEM(p->refs, idx);
817
PyList_SET_ITEM(p->refs, idx, o);
823
/* combination of both above, used when an object can be
824
* created whenever it is seen in the file, as opposed to
825
* after having loaded its sub-objects.
828
r_ref(PyObject *o, int flag, RFILE *p)
830
assert(flag & FLAG_REF);
833
if (PyList_Append(p->refs, o) < 0) {
834
Py_DECREF(o); /* release the new object */
843
/* NULL is a valid return value, it does not necessarily means that
844
an exception is set. */
848
int type, code = r_byte(p);
849
int flag, is_interned = 0;
850
PyObject *retval = NULL;
853
PyErr_SetString(PyExc_EOFError,
854
"EOF read where object expected");
860
if (p->depth > MAX_MARSHAL_STACK_DEPTH) {
862
PyErr_SetString(PyExc_ValueError, "recursion limit exceeded");
866
flag = code & FLAG_REF;
867
type = code & ~FLAG_REF;
869
#define R_REF(O) do{\
871
O = r_ref(O, flag, p);\
885
Py_INCREF(PyExc_StopIteration);
886
retval = PyExc_StopIteration;
890
Py_INCREF(Py_Ellipsis);
891
retval = Py_Ellipsis;
906
retval = PyErr_Occurred() ? NULL : PyLong_FromLong(n);
911
retval = r_PyLong(p);
921
PyErr_SetString(PyExc_EOFError,
922
"EOF read where object expected");
925
ptr = r_string(n, p);
930
dx = PyOS_string_to_double(buf, NULL, NULL);
931
if (dx == -1.0 && PyErr_Occurred())
933
retval = PyFloat_FromDouble(dx);
938
case TYPE_BINARY_FLOAT:
942
buf = (unsigned char *) r_string(8, p);
945
x = _PyFloat_Unpack8(buf, 1);
946
if (x == -1.0 && PyErr_Occurred())
948
retval = PyFloat_FromDouble(x);
959
PyErr_SetString(PyExc_EOFError,
960
"EOF read where object expected");
963
ptr = r_string(n, p);
968
c.real = PyOS_string_to_double(buf, NULL, NULL);
969
if (c.real == -1.0 && PyErr_Occurred())
973
PyErr_SetString(PyExc_EOFError,
974
"EOF read where object expected");
977
ptr = r_string(n, p);
982
c.imag = PyOS_string_to_double(buf, NULL, NULL);
983
if (c.imag == -1.0 && PyErr_Occurred())
985
retval = PyComplex_FromCComplex(c);
990
case TYPE_BINARY_COMPLEX:
994
buf = (unsigned char *) r_string(8, p);
997
c.real = _PyFloat_Unpack8(buf, 1);
998
if (c.real == -1.0 && PyErr_Occurred())
1000
buf = (unsigned char *) r_string(8, p);
1003
c.imag = _PyFloat_Unpack8(buf, 1);
1004
if (c.imag == -1.0 && PyErr_Occurred())
1006
retval = PyComplex_FromCComplex(c);
1015
if (PyErr_Occurred())
1017
if (n < 0 || n > SIZE32_MAX) {
1018
PyErr_SetString(PyExc_ValueError, "bad marshal data (string size out of range)");
1021
v = PyBytes_FromStringAndSize((char *)NULL, n);
1024
ptr = r_string(n, p);
1029
memcpy(PyBytes_AS_STRING(v), ptr, n);
1035
case TYPE_ASCII_INTERNED:
1039
if (PyErr_Occurred())
1041
if (n < 0 || n > SIZE32_MAX) {
1042
PyErr_SetString(PyExc_ValueError, "bad marshal data (unicode size out of range)");
1047
case TYPE_SHORT_ASCII_INTERNED:
1049
case TYPE_SHORT_ASCII:
1052
PyErr_SetString(PyExc_EOFError,
1053
"EOF read where object expected");
1059
ptr = r_string(n, p);
1062
v = PyUnicode_FromKindAndData(PyUnicode_1BYTE_KIND, ptr, n);
1066
PyUnicode_InternInPlace(&v);
1079
if (PyErr_Occurred())
1081
if (n < 0 || n > SIZE32_MAX) {
1082
PyErr_SetString(PyExc_ValueError, "bad marshal data (unicode size out of range)");
1086
buffer = r_string(n, p);
1089
v = PyUnicode_DecodeUTF8(buffer, n, "surrogatepass");
1092
v = PyUnicode_New(0, 0);
1097
PyUnicode_InternInPlace(&v);
1103
case TYPE_SMALL_TUPLE:
1104
n = (unsigned char) r_byte(p);
1105
if (PyErr_Occurred())
1110
if (PyErr_Occurred())
1112
if (n < 0 || n > SIZE32_MAX) {
1113
PyErr_SetString(PyExc_ValueError, "bad marshal data (tuple size out of range)");
1122
for (i = 0; i < n; i++) {
1125
if (!PyErr_Occurred())
1126
PyErr_SetString(PyExc_TypeError,
1127
"NULL object in marshal data for tuple");
1132
PyTuple_SET_ITEM(v, i, v2);
1139
if (PyErr_Occurred())
1141
if (n < 0 || n > SIZE32_MAX) {
1142
PyErr_SetString(PyExc_ValueError, "bad marshal data (list size out of range)");
1149
for (i = 0; i < n; i++) {
1152
if (!PyErr_Occurred())
1153
PyErr_SetString(PyExc_TypeError,
1154
"NULL object in marshal data for list");
1159
PyList_SET_ITEM(v, i, v2);
1170
PyObject *key, *val;
1179
if (PyDict_SetItem(v, key, val) < 0) {
1187
if (PyErr_Occurred()) {
1195
case TYPE_FROZENSET:
1197
if (PyErr_Occurred())
1199
if (n < 0 || n > SIZE32_MAX) {
1200
PyErr_SetString(PyExc_ValueError, "bad marshal data (set size out of range)");
1203
v = (type == TYPE_SET) ? PySet_New(NULL) : PyFrozenSet_New(NULL);
1204
if (type == TYPE_SET) {
1207
/* must use delayed registration of frozensets because they must
1208
* be init with a refcount of 1
1210
idx = r_ref_reserve(flag, p);
1212
Py_CLEAR(v); /* signal error */
1217
for (i = 0; i < n; i++) {
1220
if (!PyErr_Occurred())
1221
PyErr_SetString(PyExc_TypeError,
1222
"NULL object in marshal data for set");
1227
if (PySet_Add(v, v2) == -1) {
1235
if (type != TYPE_SET)
1236
v = r_ref_insert(v, idx, flag, p);
1247
PyObject *code = NULL;
1248
PyObject *consts = NULL;
1249
PyObject *names = NULL;
1250
PyObject *varnames = NULL;
1251
PyObject *freevars = NULL;
1252
PyObject *cellvars = NULL;
1253
PyObject *filename = NULL;
1254
PyObject *name = NULL;
1256
PyObject *lnotab = NULL;
1258
idx = r_ref_reserve(flag, p);
1264
/* XXX ignore long->int overflows for now */
1265
argcount = (int)r_long(p);
1266
if (PyErr_Occurred())
1268
kwonlyargcount = (int)r_long(p);
1269
if (PyErr_Occurred())
1271
nlocals = (int)r_long(p);
1272
if (PyErr_Occurred())
1274
stacksize = (int)r_long(p);
1275
if (PyErr_Occurred())
1277
flags = (int)r_long(p);
1278
if (PyErr_Occurred())
1283
consts = r_object(p);
1286
names = r_object(p);
1289
varnames = r_object(p);
1290
if (varnames == NULL)
1292
freevars = r_object(p);
1293
if (freevars == NULL)
1295
cellvars = r_object(p);
1296
if (cellvars == NULL)
1298
filename = r_object(p);
1299
if (filename == NULL)
1301
if (PyUnicode_CheckExact(filename)) {
1302
if (p->current_filename != NULL) {
1303
if (!PyUnicode_Compare(filename, p->current_filename)) {
1304
Py_DECREF(filename);
1305
Py_INCREF(p->current_filename);
1306
filename = p->current_filename;
1310
p->current_filename = filename;
1316
firstlineno = (int)r_long(p);
1317
if (firstlineno == -1 && PyErr_Occurred())
1319
lnotab = r_object(p);
1323
v = (PyObject *) PyCode_New(
1324
argcount, kwonlyargcount,
1325
nlocals, stacksize, flags,
1326
code, consts, names, varnames,
1327
freevars, cellvars, filename, name,
1328
firstlineno, lnotab);
1329
v = r_ref_insert(v, idx, flag, p);
1335
Py_XDECREF(varnames);
1336
Py_XDECREF(freevars);
1337
Py_XDECREF(cellvars);
1338
Py_XDECREF(filename);
1347
if (n < 0 || n >= PyList_GET_SIZE(p->refs)) {
1348
if (n == -1 && PyErr_Occurred())
1350
PyErr_SetString(PyExc_ValueError, "bad marshal data (invalid reference)");
1353
v = PyList_GET_ITEM(p->refs, n);
1355
PyErr_SetString(PyExc_ValueError, "bad marshal data (invalid reference)");
1363
/* Bogus data got written, which isn't ideal.
1364
This will let you keep working and recover. */
1365
PyErr_SetString(PyExc_ValueError, "bad marshal data (unknown type code)");
1374
read_object(RFILE *p)
1377
if (PyErr_Occurred()) {
1378
fprintf(stderr, "XXX readobject called with exception set\n");
1382
if (v == NULL && !PyErr_Occurred())
1383
PyErr_SetString(PyExc_TypeError, "NULL object in marshal data for object");
1388
PyMarshal_ReadShortFromFile(FILE *fp)
1395
rf.current_filename = NULL;
1396
rf.end = rf.ptr = NULL;
1405
PyMarshal_ReadLongFromFile(FILE *fp)
1411
rf.current_filename = NULL;
1412
rf.ptr = rf.end = NULL;
1421
/* Return size of file in bytes; < 0 if unknown. */
1423
getfilesize(FILE *fp)
1426
if (fstat(fileno(fp), &st) != 0)
1433
/* If we can get the size of the file up-front, and it's reasonably small,
1434
* read it in one gulp and delegate to ...FromString() instead. Much quicker
1435
* than reading a byte at a time from file; speeds .pyc imports.
1436
* CAUTION: since this may read the entire remainder of the file, don't
1437
* call it unless you know you're done with the file.
1440
PyMarshal_ReadLastObjectFromFile(FILE *fp)
1442
/* REASONABLE_FILE_LIMIT is by defn something big enough for Tkinter.pyc. */
1443
#define REASONABLE_FILE_LIMIT (1L << 18)
1446
filesize = getfilesize(fp);
1447
if (filesize > 0 && filesize <= REASONABLE_FILE_LIMIT) {
1448
char* pBuf = (char *)PyMem_MALLOC(filesize);
1450
size_t n = fread(pBuf, 1, (size_t)filesize, fp);
1451
PyObject* v = PyMarshal_ReadObjectFromString(pBuf, n);
1458
/* We don't have fstat, or we do but the file is larger than
1459
* REASONABLE_FILE_LIMIT or malloc failed -- read a byte at a time.
1461
return PyMarshal_ReadObjectFromFile(fp);
1463
#undef REASONABLE_FILE_LIMIT
1467
PyMarshal_ReadObjectFromFile(FILE *fp)
1473
rf.current_filename = NULL;
1475
rf.ptr = rf.end = NULL;
1477
rf.refs = PyList_New(0);
1478
if (rf.refs == NULL)
1480
result = r_object(&rf);
1488
PyMarshal_ReadObjectFromString(const char *str, Py_ssize_t len)
1494
rf.current_filename = NULL;
1495
rf.ptr = (char *)str;
1496
rf.end = (char *)str + len;
1499
rf.refs = PyList_New(0);
1500
if (rf.refs == NULL)
1502
result = r_object(&rf);
1510
PyMarshal_WriteObjectToString(PyObject *x, int version)
1516
wf.str = PyBytes_FromStringAndSize((char *)NULL, 50);
1519
wf.ptr = PyBytes_AS_STRING((PyBytesObject *)wf.str);
1520
wf.end = wf.ptr + PyBytes_Size(wf.str);
1521
wf.error = WFERR_OK;
1523
wf.version = version;
1525
if ((wf.refs = PyDict_New()) == NULL)
1530
Py_XDECREF(wf.refs);
1531
if (wf.str != NULL) {
1532
char *base = PyBytes_AS_STRING((PyBytesObject *)wf.str);
1533
if (wf.ptr - base > PY_SSIZE_T_MAX) {
1535
PyErr_SetString(PyExc_OverflowError,
1536
"too much marshal data for a string");
1539
if (_PyBytes_Resize(&wf.str, (Py_ssize_t)(wf.ptr - base)) < 0)
1542
if (wf.error != WFERR_OK) {
1544
if (wf.error == WFERR_NOMEMORY)
1547
PyErr_SetString(PyExc_ValueError,
1548
(wf.error==WFERR_UNMARSHALLABLE)?"unmarshallable object"
1549
:"object too deeply nested to marshal");
1555
/* And an interface for Python programs... */
1558
marshal_dump(PyObject *self, PyObject *args)
1560
/* XXX Quick hack -- need to do this differently */
1563
int version = Py_MARSHAL_VERSION;
1566
_Py_IDENTIFIER(write);
1568
if (!PyArg_ParseTuple(args, "OO|i:dump", &x, &f, &version))
1570
s = PyMarshal_WriteObjectToString(x, version);
1573
res = _PyObject_CallMethodId(f, &PyId_write, "O", s);
1578
PyDoc_STRVAR(dump_doc,
1579
"dump(value, file[, version])\n\
1581
Write the value on the open file. The value must be a supported type.\n\
1582
The file must be an open file object such as sys.stdout or returned by\n\
1583
open() or os.popen(). It must be opened in binary mode ('wb' or 'w+b').\n\
1585
If the value has (or contains an object that has) an unsupported type, a\n\
1586
ValueError exception is raised — but garbage data will also be written\n\
1587
to the file. The object will not be properly read back by load()\n\
1589
The version argument indicates the data format that dump should use.");
1592
marshal_load(PyObject *self, PyObject *f)
1594
PyObject *data, *result;
1595
_Py_IDENTIFIER(read);
1599
* Make a call to the read method, but read zero bytes.
1600
* This is to ensure that the object passed in at least
1601
* has a read method which returns bytes.
1602
* This can be removed if we guarantee good error handling
1605
data = _PyObject_CallMethodId(f, &PyId_read, "i", 0);
1608
if (!PyBytes_Check(data)) {
1609
PyErr_Format(PyExc_TypeError,
1610
"f.read() returned not bytes but %.100s",
1611
data->ob_type->tp_name);
1618
rf.current_filename = NULL;
1619
rf.ptr = rf.end = NULL;
1621
if ((rf.refs = PyList_New(0)) != NULL) {
1622
result = read_object(&rf);
1633
PyDoc_STRVAR(load_doc,
1636
Read one value from the open file and return it. If no valid value is\n\
1637
read (e.g. because the data has a different Python version’s\n\
1638
incompatible marshal format), raise EOFError, ValueError or TypeError.\n\
1639
The file must be an open file object opened in binary mode ('rb' or\n\
1642
Note: If an object containing an unsupported type was marshalled with\n\
1643
dump(), load() will substitute None for the unmarshallable type.");
1647
marshal_dumps(PyObject *self, PyObject *args)
1650
int version = Py_MARSHAL_VERSION;
1651
if (!PyArg_ParseTuple(args, "O|i:dumps", &x, &version))
1653
return PyMarshal_WriteObjectToString(x, version);
1656
PyDoc_STRVAR(dumps_doc,
1657
"dumps(value[, version])\n\
1659
Return the string that would be written to a file by dump(value, file).\n\
1660
The value must be a supported type. Raise a ValueError exception if\n\
1661
value has (or contains an object that has) an unsupported type.\n\
1663
The version argument indicates the data format that dumps should use.");
1667
marshal_loads(PyObject *self, PyObject *args)
1674
if (!PyArg_ParseTuple(args, "y*:loads", &p))
1680
rf.current_filename = NULL;
1684
if ((rf.refs = PyList_New(0)) == NULL)
1686
result = read_object(&rf);
1687
PyBuffer_Release(&p);
1692
PyDoc_STRVAR(loads_doc,
1695
Convert the bytes object to a value. If no valid value is found, raise\n\
1696
EOFError, ValueError or TypeError. Extra characters in the input are\n\
1699
static PyMethodDef marshal_methods[] = {
1700
{"dump", marshal_dump, METH_VARARGS, dump_doc},
1701
{"load", marshal_load, METH_O, load_doc},
1702
{"dumps", marshal_dumps, METH_VARARGS, dumps_doc},
1703
{"loads", marshal_loads, METH_VARARGS, loads_doc},
1704
{NULL, NULL} /* sentinel */
1708
PyDoc_STRVAR(module_doc,
1709
"This module contains functions that can read and write Python values in\n\
1710
a binary format. The format is specific to Python, but independent of\n\
1711
machine architecture issues.\n\
1713
Not all Python object types are supported; in general, only objects\n\
1714
whose value is independent from a particular invocation of Python can be\n\
1715
written and read by this module. The following types are supported:\n\
1716
None, integers, floating point numbers, strings, bytes, bytearrays,\n\
1717
tuples, lists, sets, dictionaries, and code objects, where it\n\
1718
should be understood that tuples, lists and dictionaries are only\n\
1719
supported as long as the values contained therein are themselves\n\
1720
supported; and recursive lists and dictionaries should not be written\n\
1721
(they will cause infinite loops).\n\
1725
version -- indicates the format that the module uses. Version 0 is the\n\
1726
historical format, version 1 shares interned strings and version 2\n\
1727
uses a binary format for floating point numbers.\n\
1728
Version 3 shares common object references (New in version 3.4).\n\
1732
dump() -- write value to a file\n\
1733
load() -- read value from a file\n\
1734
dumps() -- write value to a string\n\
1735
loads() -- read value from a string");
1739
static struct PyModuleDef marshalmodule = {
1740
PyModuleDef_HEAD_INIT,
1752
PyMarshal_Init(void)
1754
PyObject *mod = PyModule_Create(&marshalmodule);
1757
PyModule_AddIntConstant(mod, "version", Py_MARSHAL_VERSION);