ยปCore Development>Code coverage>Python/marshal.c

Python code coverage for Python/marshal.c

#countcontent
1n/a
2n/a/* Write Python objects to files and read them back.
3n/a This is primarily intended for writing and reading compiled Python code,
4n/a even though dicts, lists, sets and frozensets, not commonly seen in
5n/a code objects, are supported.
6n/a Version 3 of this protocol properly supports circular links
7n/a and sharing. */
8n/a
9n/a#define PY_SSIZE_T_CLEAN
10n/a
11n/a#include "Python.h"
12n/a#include "longintrepr.h"
13n/a#include "code.h"
14n/a#include "marshal.h"
15n/a#include "../Modules/hashtable.h"
16n/a
17n/a/* High water mark to determine when the marshalled object is dangerously deep
18n/a * and risks coring the interpreter. When the object stack gets this deep,
19n/a * raise an exception instead of continuing.
20n/a * On Windows debug builds, reduce this value.
21n/a */
22n/a#if defined(MS_WINDOWS) && defined(_DEBUG)
23n/a#define MAX_MARSHAL_STACK_DEPTH 1000
24n/a#else
25n/a#define MAX_MARSHAL_STACK_DEPTH 2000
26n/a#endif
27n/a
28n/a#define TYPE_NULL '0'
29n/a#define TYPE_NONE 'N'
30n/a#define TYPE_FALSE 'F'
31n/a#define TYPE_TRUE 'T'
32n/a#define TYPE_STOPITER 'S'
33n/a#define TYPE_ELLIPSIS '.'
34n/a#define TYPE_INT 'i'
35n/a#define TYPE_FLOAT 'f'
36n/a#define TYPE_BINARY_FLOAT 'g'
37n/a#define TYPE_COMPLEX 'x'
38n/a#define TYPE_BINARY_COMPLEX 'y'
39n/a#define TYPE_LONG 'l'
40n/a#define TYPE_STRING 's'
41n/a#define TYPE_INTERNED 't'
42n/a#define TYPE_REF 'r'
43n/a#define TYPE_TUPLE '('
44n/a#define TYPE_LIST '['
45n/a#define TYPE_DICT '{'
46n/a#define TYPE_CODE 'c'
47n/a#define TYPE_UNICODE 'u'
48n/a#define TYPE_UNKNOWN '?'
49n/a#define TYPE_SET '<'
50n/a#define TYPE_FROZENSET '>'
51n/a#define FLAG_REF '\x80' /* with a type, add obj to index */
52n/a
53n/a#define TYPE_ASCII 'a'
54n/a#define TYPE_ASCII_INTERNED 'A'
55n/a#define TYPE_SMALL_TUPLE ')'
56n/a#define TYPE_SHORT_ASCII 'z'
57n/a#define TYPE_SHORT_ASCII_INTERNED 'Z'
58n/a
59n/a#define WFERR_OK 0
60n/a#define WFERR_UNMARSHALLABLE 1
61n/a#define WFERR_NESTEDTOODEEP 2
62n/a#define WFERR_NOMEMORY 3
63n/a
64n/atypedef struct {
65n/a FILE *fp;
66n/a int error; /* see WFERR_* values */
67n/a int depth;
68n/a PyObject *str;
69n/a char *ptr;
70n/a char *end;
71n/a char *buf;
72n/a _Py_hashtable_t *hashtable;
73n/a int version;
74n/a} WFILE;
75n/a
76n/a#define w_byte(c, p) do { \
77n/a if ((p)->ptr != (p)->end || w_reserve((p), 1)) \
78n/a *(p)->ptr++ = (c); \
79n/a } while(0)
80n/a
81n/astatic void
82n/aw_flush(WFILE *p)
83n/a{
84n/a assert(p->fp != NULL);
85n/a fwrite(p->buf, 1, p->ptr - p->buf, p->fp);
86n/a p->ptr = p->buf;
87n/a}
88n/a
89n/astatic int
90n/aw_reserve(WFILE *p, Py_ssize_t needed)
91n/a{
92n/a Py_ssize_t pos, size, delta;
93n/a if (p->ptr == NULL)
94n/a return 0; /* An error already occurred */
95n/a if (p->fp != NULL) {
96n/a w_flush(p);
97n/a return needed <= p->end - p->ptr;
98n/a }
99n/a assert(p->str != NULL);
100n/a pos = p->ptr - p->buf;
101n/a size = PyBytes_Size(p->str);
102n/a if (size > 16*1024*1024)
103n/a delta = (size >> 3); /* 12.5% overallocation */
104n/a else
105n/a delta = size + 1024;
106n/a delta = Py_MAX(delta, needed);
107n/a if (delta > PY_SSIZE_T_MAX - size) {
108n/a p->error = WFERR_NOMEMORY;
109n/a return 0;
110n/a }
111n/a size += delta;
112n/a if (_PyBytes_Resize(&p->str, size) != 0) {
113n/a p->ptr = p->buf = p->end = NULL;
114n/a return 0;
115n/a }
116n/a else {
117n/a p->buf = PyBytes_AS_STRING(p->str);
118n/a p->ptr = p->buf + pos;
119n/a p->end = p->buf + size;
120n/a return 1;
121n/a }
122n/a}
123n/a
124n/astatic void
125n/aw_string(const char *s, Py_ssize_t n, WFILE *p)
126n/a{
127n/a Py_ssize_t m;
128n/a if (!n || p->ptr == NULL)
129n/a return;
130n/a m = p->end - p->ptr;
131n/a if (p->fp != NULL) {
132n/a if (n <= m) {
133n/a memcpy(p->ptr, s, n);
134n/a p->ptr += n;
135n/a }
136n/a else {
137n/a w_flush(p);
138n/a fwrite(s, 1, n, p->fp);
139n/a }
140n/a }
141n/a else {
142n/a if (n <= m || w_reserve(p, n - m)) {
143n/a memcpy(p->ptr, s, n);
144n/a p->ptr += n;
145n/a }
146n/a }
147n/a}
148n/a
149n/astatic void
150n/aw_short(int x, WFILE *p)
151n/a{
152n/a w_byte((char)( x & 0xff), p);
153n/a w_byte((char)((x>> 8) & 0xff), p);
154n/a}
155n/a
156n/astatic void
157n/aw_long(long x, WFILE *p)
158n/a{
159n/a w_byte((char)( x & 0xff), p);
160n/a w_byte((char)((x>> 8) & 0xff), p);
161n/a w_byte((char)((x>>16) & 0xff), p);
162n/a w_byte((char)((x>>24) & 0xff), p);
163n/a}
164n/a
165n/a#define SIZE32_MAX 0x7FFFFFFF
166n/a
167n/a#if SIZEOF_SIZE_T > 4
168n/a# define W_SIZE(n, p) do { \
169n/a if ((n) > SIZE32_MAX) { \
170n/a (p)->depth--; \
171n/a (p)->error = WFERR_UNMARSHALLABLE; \
172n/a return; \
173n/a } \
174n/a w_long((long)(n), p); \
175n/a } while(0)
176n/a#else
177n/a# define W_SIZE w_long
178n/a#endif
179n/a
180n/astatic void
181n/aw_pstring(const char *s, Py_ssize_t n, WFILE *p)
182n/a{
183n/a W_SIZE(n, p);
184n/a w_string(s, n, p);
185n/a}
186n/a
187n/astatic void
188n/aw_short_pstring(const char *s, Py_ssize_t n, WFILE *p)
189n/a{
190n/a w_byte(Py_SAFE_DOWNCAST(n, Py_ssize_t, unsigned char), p);
191n/a w_string(s, n, p);
192n/a}
193n/a
194n/a/* We assume that Python ints are stored internally in base some power of
195n/a 2**15; for the sake of portability we'll always read and write them in base
196n/a exactly 2**15. */
197n/a
198n/a#define PyLong_MARSHAL_SHIFT 15
199n/a#define PyLong_MARSHAL_BASE ((short)1 << PyLong_MARSHAL_SHIFT)
200n/a#define PyLong_MARSHAL_MASK (PyLong_MARSHAL_BASE - 1)
201n/a#if PyLong_SHIFT % PyLong_MARSHAL_SHIFT != 0
202n/a#error "PyLong_SHIFT must be a multiple of PyLong_MARSHAL_SHIFT"
203n/a#endif
204n/a#define PyLong_MARSHAL_RATIO (PyLong_SHIFT / PyLong_MARSHAL_SHIFT)
205n/a
206n/a#define W_TYPE(t, p) do { \
207n/a w_byte((t) | flag, (p)); \
208n/a} while(0)
209n/a
210n/astatic void
211n/aw_PyLong(const PyLongObject *ob, char flag, WFILE *p)
212n/a{
213n/a Py_ssize_t i, j, n, l;
214n/a digit d;
215n/a
216n/a W_TYPE(TYPE_LONG, p);
217n/a if (Py_SIZE(ob) == 0) {
218n/a w_long((long)0, p);
219n/a return;
220n/a }
221n/a
222n/a /* set l to number of base PyLong_MARSHAL_BASE digits */
223n/a n = Py_ABS(Py_SIZE(ob));
224n/a l = (n-1) * PyLong_MARSHAL_RATIO;
225n/a d = ob->ob_digit[n-1];
226n/a assert(d != 0); /* a PyLong is always normalized */
227n/a do {
228n/a d >>= PyLong_MARSHAL_SHIFT;
229n/a l++;
230n/a } while (d != 0);
231n/a if (l > SIZE32_MAX) {
232n/a p->depth--;
233n/a p->error = WFERR_UNMARSHALLABLE;
234n/a return;
235n/a }
236n/a w_long((long)(Py_SIZE(ob) > 0 ? l : -l), p);
237n/a
238n/a for (i=0; i < n-1; i++) {
239n/a d = ob->ob_digit[i];
240n/a for (j=0; j < PyLong_MARSHAL_RATIO; j++) {
241n/a w_short(d & PyLong_MARSHAL_MASK, p);
242n/a d >>= PyLong_MARSHAL_SHIFT;
243n/a }
244n/a assert (d == 0);
245n/a }
246n/a d = ob->ob_digit[n-1];
247n/a do {
248n/a w_short(d & PyLong_MARSHAL_MASK, p);
249n/a d >>= PyLong_MARSHAL_SHIFT;
250n/a } while (d != 0);
251n/a}
252n/a
253n/astatic int
254n/aw_ref(PyObject *v, char *flag, WFILE *p)
255n/a{
256n/a _Py_hashtable_entry_t *entry;
257n/a int w;
258n/a
259n/a if (p->version < 3 || p->hashtable == NULL)
260n/a return 0; /* not writing object references */
261n/a
262n/a /* if it has only one reference, it definitely isn't shared */
263n/a if (Py_REFCNT(v) == 1)
264n/a return 0;
265n/a
266n/a entry = _Py_HASHTABLE_GET_ENTRY(p->hashtable, v);
267n/a if (entry != NULL) {
268n/a /* write the reference index to the stream */
269n/a _Py_HASHTABLE_ENTRY_READ_DATA(p->hashtable, entry, w);
270n/a /* we don't store "long" indices in the dict */
271n/a assert(0 <= w && w <= 0x7fffffff);
272n/a w_byte(TYPE_REF, p);
273n/a w_long(w, p);
274n/a return 1;
275n/a } else {
276n/a size_t s = p->hashtable->entries;
277n/a /* we don't support long indices */
278n/a if (s >= 0x7fffffff) {
279n/a PyErr_SetString(PyExc_ValueError, "too many objects");
280n/a goto err;
281n/a }
282n/a w = (int)s;
283n/a Py_INCREF(v);
284n/a if (_Py_HASHTABLE_SET(p->hashtable, v, w) < 0) {
285n/a Py_DECREF(v);
286n/a goto err;
287n/a }
288n/a *flag |= FLAG_REF;
289n/a return 0;
290n/a }
291n/aerr:
292n/a p->error = WFERR_UNMARSHALLABLE;
293n/a return 1;
294n/a}
295n/a
296n/astatic void
297n/aw_complex_object(PyObject *v, char flag, WFILE *p);
298n/a
299n/astatic void
300n/aw_object(PyObject *v, WFILE *p)
301n/a{
302n/a char flag = '\0';
303n/a
304n/a p->depth++;
305n/a
306n/a if (p->depth > MAX_MARSHAL_STACK_DEPTH) {
307n/a p->error = WFERR_NESTEDTOODEEP;
308n/a }
309n/a else if (v == NULL) {
310n/a w_byte(TYPE_NULL, p);
311n/a }
312n/a else if (v == Py_None) {
313n/a w_byte(TYPE_NONE, p);
314n/a }
315n/a else if (v == PyExc_StopIteration) {
316n/a w_byte(TYPE_STOPITER, p);
317n/a }
318n/a else if (v == Py_Ellipsis) {
319n/a w_byte(TYPE_ELLIPSIS, p);
320n/a }
321n/a else if (v == Py_False) {
322n/a w_byte(TYPE_FALSE, p);
323n/a }
324n/a else if (v == Py_True) {
325n/a w_byte(TYPE_TRUE, p);
326n/a }
327n/a else if (!w_ref(v, &flag, p))
328n/a w_complex_object(v, flag, p);
329n/a
330n/a p->depth--;
331n/a}
332n/a
333n/astatic void
334n/aw_complex_object(PyObject *v, char flag, WFILE *p)
335n/a{
336n/a Py_ssize_t i, n;
337n/a
338n/a if (PyLong_CheckExact(v)) {
339n/a long x = PyLong_AsLong(v);
340n/a if ((x == -1) && PyErr_Occurred()) {
341n/a PyLongObject *ob = (PyLongObject *)v;
342n/a PyErr_Clear();
343n/a w_PyLong(ob, flag, p);
344n/a }
345n/a else {
346n/a#if SIZEOF_LONG > 4
347n/a long y = Py_ARITHMETIC_RIGHT_SHIFT(long, x, 31);
348n/a if (y && y != -1) {
349n/a /* Too large for TYPE_INT */
350n/a w_PyLong((PyLongObject*)v, flag, p);
351n/a }
352n/a else
353n/a#endif
354n/a {
355n/a W_TYPE(TYPE_INT, p);
356n/a w_long(x, p);
357n/a }
358n/a }
359n/a }
360n/a else if (PyFloat_CheckExact(v)) {
361n/a if (p->version > 1) {
362n/a unsigned char buf[8];
363n/a if (_PyFloat_Pack8(PyFloat_AsDouble(v),
364n/a buf, 1) < 0) {
365n/a p->error = WFERR_UNMARSHALLABLE;
366n/a return;
367n/a }
368n/a W_TYPE(TYPE_BINARY_FLOAT, p);
369n/a w_string((char*)buf, 8, p);
370n/a }
371n/a else {
372n/a char *buf = PyOS_double_to_string(PyFloat_AS_DOUBLE(v),
373n/a 'g', 17, 0, NULL);
374n/a if (!buf) {
375n/a p->error = WFERR_NOMEMORY;
376n/a return;
377n/a }
378n/a n = strlen(buf);
379n/a W_TYPE(TYPE_FLOAT, p);
380n/a w_byte((int)n, p);
381n/a w_string(buf, n, p);
382n/a PyMem_Free(buf);
383n/a }
384n/a }
385n/a else if (PyComplex_CheckExact(v)) {
386n/a if (p->version > 1) {
387n/a unsigned char buf[8];
388n/a if (_PyFloat_Pack8(PyComplex_RealAsDouble(v),
389n/a buf, 1) < 0) {
390n/a p->error = WFERR_UNMARSHALLABLE;
391n/a return;
392n/a }
393n/a W_TYPE(TYPE_BINARY_COMPLEX, p);
394n/a w_string((char*)buf, 8, p);
395n/a if (_PyFloat_Pack8(PyComplex_ImagAsDouble(v),
396n/a buf, 1) < 0) {
397n/a p->error = WFERR_UNMARSHALLABLE;
398n/a return;
399n/a }
400n/a w_string((char*)buf, 8, p);
401n/a }
402n/a else {
403n/a char *buf;
404n/a W_TYPE(TYPE_COMPLEX, p);
405n/a buf = PyOS_double_to_string(PyComplex_RealAsDouble(v),
406n/a 'g', 17, 0, NULL);
407n/a if (!buf) {
408n/a p->error = WFERR_NOMEMORY;
409n/a return;
410n/a }
411n/a n = strlen(buf);
412n/a w_byte((int)n, p);
413n/a w_string(buf, n, p);
414n/a PyMem_Free(buf);
415n/a buf = PyOS_double_to_string(PyComplex_ImagAsDouble(v),
416n/a 'g', 17, 0, NULL);
417n/a if (!buf) {
418n/a p->error = WFERR_NOMEMORY;
419n/a return;
420n/a }
421n/a n = strlen(buf);
422n/a w_byte((int)n, p);
423n/a w_string(buf, n, p);
424n/a PyMem_Free(buf);
425n/a }
426n/a }
427n/a else if (PyBytes_CheckExact(v)) {
428n/a W_TYPE(TYPE_STRING, p);
429n/a w_pstring(PyBytes_AS_STRING(v), PyBytes_GET_SIZE(v), p);
430n/a }
431n/a else if (PyUnicode_CheckExact(v)) {
432n/a if (p->version >= 4 && PyUnicode_IS_ASCII(v)) {
433n/a int is_short = PyUnicode_GET_LENGTH(v) < 256;
434n/a if (is_short) {
435n/a if (PyUnicode_CHECK_INTERNED(v))
436n/a W_TYPE(TYPE_SHORT_ASCII_INTERNED, p);
437n/a else
438n/a W_TYPE(TYPE_SHORT_ASCII, p);
439n/a w_short_pstring((char *) PyUnicode_1BYTE_DATA(v),
440n/a PyUnicode_GET_LENGTH(v), p);
441n/a }
442n/a else {
443n/a if (PyUnicode_CHECK_INTERNED(v))
444n/a W_TYPE(TYPE_ASCII_INTERNED, p);
445n/a else
446n/a W_TYPE(TYPE_ASCII, p);
447n/a w_pstring((char *) PyUnicode_1BYTE_DATA(v),
448n/a PyUnicode_GET_LENGTH(v), p);
449n/a }
450n/a }
451n/a else {
452n/a PyObject *utf8;
453n/a utf8 = PyUnicode_AsEncodedString(v, "utf8", "surrogatepass");
454n/a if (utf8 == NULL) {
455n/a p->depth--;
456n/a p->error = WFERR_UNMARSHALLABLE;
457n/a return;
458n/a }
459n/a if (p->version >= 3 && PyUnicode_CHECK_INTERNED(v))
460n/a W_TYPE(TYPE_INTERNED, p);
461n/a else
462n/a W_TYPE(TYPE_UNICODE, p);
463n/a w_pstring(PyBytes_AS_STRING(utf8), PyBytes_GET_SIZE(utf8), p);
464n/a Py_DECREF(utf8);
465n/a }
466n/a }
467n/a else if (PyTuple_CheckExact(v)) {
468n/a n = PyTuple_Size(v);
469n/a if (p->version >= 4 && n < 256) {
470n/a W_TYPE(TYPE_SMALL_TUPLE, p);
471n/a w_byte((unsigned char)n, p);
472n/a }
473n/a else {
474n/a W_TYPE(TYPE_TUPLE, p);
475n/a W_SIZE(n, p);
476n/a }
477n/a for (i = 0; i < n; i++) {
478n/a w_object(PyTuple_GET_ITEM(v, i), p);
479n/a }
480n/a }
481n/a else if (PyList_CheckExact(v)) {
482n/a W_TYPE(TYPE_LIST, p);
483n/a n = PyList_GET_SIZE(v);
484n/a W_SIZE(n, p);
485n/a for (i = 0; i < n; i++) {
486n/a w_object(PyList_GET_ITEM(v, i), p);
487n/a }
488n/a }
489n/a else if (PyDict_CheckExact(v)) {
490n/a Py_ssize_t pos;
491n/a PyObject *key, *value;
492n/a W_TYPE(TYPE_DICT, p);
493n/a /* This one is NULL object terminated! */
494n/a pos = 0;
495n/a while (PyDict_Next(v, &pos, &key, &value)) {
496n/a w_object(key, p);
497n/a w_object(value, p);
498n/a }
499n/a w_object((PyObject *)NULL, p);
500n/a }
501n/a else if (PyAnySet_CheckExact(v)) {
502n/a PyObject *value, *it;
503n/a
504n/a if (PyObject_TypeCheck(v, &PySet_Type))
505n/a W_TYPE(TYPE_SET, p);
506n/a else
507n/a W_TYPE(TYPE_FROZENSET, p);
508n/a n = PyObject_Size(v);
509n/a if (n == -1) {
510n/a p->depth--;
511n/a p->error = WFERR_UNMARSHALLABLE;
512n/a return;
513n/a }
514n/a W_SIZE(n, p);
515n/a it = PyObject_GetIter(v);
516n/a if (it == NULL) {
517n/a p->depth--;
518n/a p->error = WFERR_UNMARSHALLABLE;
519n/a return;
520n/a }
521n/a while ((value = PyIter_Next(it)) != NULL) {
522n/a w_object(value, p);
523n/a Py_DECREF(value);
524n/a }
525n/a Py_DECREF(it);
526n/a if (PyErr_Occurred()) {
527n/a p->depth--;
528n/a p->error = WFERR_UNMARSHALLABLE;
529n/a return;
530n/a }
531n/a }
532n/a else if (PyCode_Check(v)) {
533n/a PyCodeObject *co = (PyCodeObject *)v;
534n/a W_TYPE(TYPE_CODE, p);
535n/a w_long(co->co_argcount, p);
536n/a w_long(co->co_kwonlyargcount, p);
537n/a w_long(co->co_nlocals, p);
538n/a w_long(co->co_stacksize, p);
539n/a w_long(co->co_flags, p);
540n/a w_object(co->co_code, p);
541n/a w_object(co->co_consts, p);
542n/a w_object(co->co_names, p);
543n/a w_object(co->co_varnames, p);
544n/a w_object(co->co_freevars, p);
545n/a w_object(co->co_cellvars, p);
546n/a w_object(co->co_filename, p);
547n/a w_object(co->co_name, p);
548n/a w_long(co->co_firstlineno, p);
549n/a w_object(co->co_lnotab, p);
550n/a }
551n/a else if (PyObject_CheckBuffer(v)) {
552n/a /* Write unknown bytes-like objects as a byte string */
553n/a Py_buffer view;
554n/a if (PyObject_GetBuffer(v, &view, PyBUF_SIMPLE) != 0) {
555n/a w_byte(TYPE_UNKNOWN, p);
556n/a p->depth--;
557n/a p->error = WFERR_UNMARSHALLABLE;
558n/a return;
559n/a }
560n/a W_TYPE(TYPE_STRING, p);
561n/a w_pstring(view.buf, view.len, p);
562n/a PyBuffer_Release(&view);
563n/a }
564n/a else {
565n/a W_TYPE(TYPE_UNKNOWN, p);
566n/a p->error = WFERR_UNMARSHALLABLE;
567n/a }
568n/a}
569n/a
570n/astatic int
571n/aw_init_refs(WFILE *wf, int version)
572n/a{
573n/a if (version >= 3) {
574n/a wf->hashtable = _Py_hashtable_new(sizeof(PyObject *), sizeof(int),
575n/a _Py_hashtable_hash_ptr,
576n/a _Py_hashtable_compare_direct);
577n/a if (wf->hashtable == NULL) {
578n/a PyErr_NoMemory();
579n/a return -1;
580n/a }
581n/a }
582n/a return 0;
583n/a}
584n/a
585n/astatic int
586n/aw_decref_entry(_Py_hashtable_t *ht, _Py_hashtable_entry_t *entry,
587n/a void *Py_UNUSED(data))
588n/a{
589n/a PyObject *entry_key;
590n/a
591n/a _Py_HASHTABLE_ENTRY_READ_KEY(ht, entry, entry_key);
592n/a Py_XDECREF(entry_key);
593n/a return 0;
594n/a}
595n/a
596n/astatic void
597n/aw_clear_refs(WFILE *wf)
598n/a{
599n/a if (wf->hashtable != NULL) {
600n/a _Py_hashtable_foreach(wf->hashtable, w_decref_entry, NULL);
601n/a _Py_hashtable_destroy(wf->hashtable);
602n/a }
603n/a}
604n/a
605n/a/* version currently has no effect for writing ints. */
606n/avoid
607n/aPyMarshal_WriteLongToFile(long x, FILE *fp, int version)
608n/a{
609n/a char buf[4];
610n/a WFILE wf;
611n/a memset(&wf, 0, sizeof(wf));
612n/a wf.fp = fp;
613n/a wf.ptr = wf.buf = buf;
614n/a wf.end = wf.ptr + sizeof(buf);
615n/a wf.error = WFERR_OK;
616n/a wf.version = version;
617n/a w_long(x, &wf);
618n/a w_flush(&wf);
619n/a}
620n/a
621n/avoid
622n/aPyMarshal_WriteObjectToFile(PyObject *x, FILE *fp, int version)
623n/a{
624n/a char buf[BUFSIZ];
625n/a WFILE wf;
626n/a memset(&wf, 0, sizeof(wf));
627n/a wf.fp = fp;
628n/a wf.ptr = wf.buf = buf;
629n/a wf.end = wf.ptr + sizeof(buf);
630n/a wf.error = WFERR_OK;
631n/a wf.version = version;
632n/a if (w_init_refs(&wf, version))
633n/a return; /* caller mush check PyErr_Occurred() */
634n/a w_object(x, &wf);
635n/a w_clear_refs(&wf);
636n/a w_flush(&wf);
637n/a}
638n/a
639n/atypedef struct {
640n/a FILE *fp;
641n/a int depth;
642n/a PyObject *readable; /* Stream-like object being read from */
643n/a PyObject *current_filename;
644n/a char *ptr;
645n/a char *end;
646n/a char *buf;
647n/a Py_ssize_t buf_size;
648n/a PyObject *refs; /* a list */
649n/a} RFILE;
650n/a
651n/astatic const char *
652n/ar_string(Py_ssize_t n, RFILE *p)
653n/a{
654n/a Py_ssize_t read = -1;
655n/a
656n/a if (p->ptr != NULL) {
657n/a /* Fast path for loads() */
658n/a char *res = p->ptr;
659n/a Py_ssize_t left = p->end - p->ptr;
660n/a if (left < n) {
661n/a PyErr_SetString(PyExc_EOFError,
662n/a "marshal data too short");
663n/a return NULL;
664n/a }
665n/a p->ptr += n;
666n/a return res;
667n/a }
668n/a if (p->buf == NULL) {
669n/a p->buf = PyMem_MALLOC(n);
670n/a if (p->buf == NULL) {
671n/a PyErr_NoMemory();
672n/a return NULL;
673n/a }
674n/a p->buf_size = n;
675n/a }
676n/a else if (p->buf_size < n) {
677n/a p->buf = PyMem_REALLOC(p->buf, n);
678n/a if (p->buf == NULL) {
679n/a PyErr_NoMemory();
680n/a return NULL;
681n/a }
682n/a p->buf_size = n;
683n/a }
684n/a
685n/a if (!p->readable) {
686n/a assert(p->fp != NULL);
687n/a read = fread(p->buf, 1, n, p->fp);
688n/a }
689n/a else {
690n/a _Py_IDENTIFIER(readinto);
691n/a PyObject *res, *mview;
692n/a Py_buffer buf;
693n/a
694n/a if (PyBuffer_FillInfo(&buf, NULL, p->buf, n, 0, PyBUF_CONTIG) == -1)
695n/a return NULL;
696n/a mview = PyMemoryView_FromBuffer(&buf);
697n/a if (mview == NULL)
698n/a return NULL;
699n/a
700n/a res = _PyObject_CallMethodId(p->readable, &PyId_readinto, "N", mview);
701n/a if (res != NULL) {
702n/a read = PyNumber_AsSsize_t(res, PyExc_ValueError);
703n/a Py_DECREF(res);
704n/a }
705n/a }
706n/a if (read != n) {
707n/a if (!PyErr_Occurred()) {
708n/a if (read > n)
709n/a PyErr_Format(PyExc_ValueError,
710n/a "read() returned too much data: "
711n/a "%zd bytes requested, %zd returned",
712n/a n, read);
713n/a else
714n/a PyErr_SetString(PyExc_EOFError,
715n/a "EOF read where not expected");
716n/a }
717n/a return NULL;
718n/a }
719n/a return p->buf;
720n/a}
721n/a
722n/astatic int
723n/ar_byte(RFILE *p)
724n/a{
725n/a int c = EOF;
726n/a
727n/a if (p->ptr != NULL) {
728n/a if (p->ptr < p->end)
729n/a c = (unsigned char) *p->ptr++;
730n/a return c;
731n/a }
732n/a if (!p->readable) {
733n/a assert(p->fp);
734n/a c = getc(p->fp);
735n/a }
736n/a else {
737n/a const char *ptr = r_string(1, p);
738n/a if (ptr != NULL)
739n/a c = *(unsigned char *) ptr;
740n/a }
741n/a return c;
742n/a}
743n/a
744n/astatic int
745n/ar_short(RFILE *p)
746n/a{
747n/a short x = -1;
748n/a const unsigned char *buffer;
749n/a
750n/a buffer = (const unsigned char *) r_string(2, p);
751n/a if (buffer != NULL) {
752n/a x = buffer[0];
753n/a x |= buffer[1] << 8;
754n/a /* Sign-extension, in case short greater than 16 bits */
755n/a x |= -(x & 0x8000);
756n/a }
757n/a return x;
758n/a}
759n/a
760n/astatic long
761n/ar_long(RFILE *p)
762n/a{
763n/a long x = -1;
764n/a const unsigned char *buffer;
765n/a
766n/a buffer = (const unsigned char *) r_string(4, p);
767n/a if (buffer != NULL) {
768n/a x = buffer[0];
769n/a x |= (long)buffer[1] << 8;
770n/a x |= (long)buffer[2] << 16;
771n/a x |= (long)buffer[3] << 24;
772n/a#if SIZEOF_LONG > 4
773n/a /* Sign extension for 64-bit machines */
774n/a x |= -(x & 0x80000000L);
775n/a#endif
776n/a }
777n/a return x;
778n/a}
779n/a
780n/astatic PyObject *
781n/ar_PyLong(RFILE *p)
782n/a{
783n/a PyLongObject *ob;
784n/a long n, size, i;
785n/a int j, md, shorts_in_top_digit;
786n/a digit d;
787n/a
788n/a n = r_long(p);
789n/a if (PyErr_Occurred())
790n/a return NULL;
791n/a if (n == 0)
792n/a return (PyObject *)_PyLong_New(0);
793n/a if (n < -SIZE32_MAX || n > SIZE32_MAX) {
794n/a PyErr_SetString(PyExc_ValueError,
795n/a "bad marshal data (long size out of range)");
796n/a return NULL;
797n/a }
798n/a
799n/a size = 1 + (Py_ABS(n) - 1) / PyLong_MARSHAL_RATIO;
800n/a shorts_in_top_digit = 1 + (Py_ABS(n) - 1) % PyLong_MARSHAL_RATIO;
801n/a ob = _PyLong_New(size);
802n/a if (ob == NULL)
803n/a return NULL;
804n/a
805n/a Py_SIZE(ob) = n > 0 ? size : -size;
806n/a
807n/a for (i = 0; i < size-1; i++) {
808n/a d = 0;
809n/a for (j=0; j < PyLong_MARSHAL_RATIO; j++) {
810n/a md = r_short(p);
811n/a if (PyErr_Occurred()) {
812n/a Py_DECREF(ob);
813n/a return NULL;
814n/a }
815n/a if (md < 0 || md > PyLong_MARSHAL_BASE)
816n/a goto bad_digit;
817n/a d += (digit)md << j*PyLong_MARSHAL_SHIFT;
818n/a }
819n/a ob->ob_digit[i] = d;
820n/a }
821n/a
822n/a d = 0;
823n/a for (j=0; j < shorts_in_top_digit; j++) {
824n/a md = r_short(p);
825n/a if (PyErr_Occurred()) {
826n/a Py_DECREF(ob);
827n/a return NULL;
828n/a }
829n/a if (md < 0 || md > PyLong_MARSHAL_BASE)
830n/a goto bad_digit;
831n/a /* topmost marshal digit should be nonzero */
832n/a if (md == 0 && j == shorts_in_top_digit - 1) {
833n/a Py_DECREF(ob);
834n/a PyErr_SetString(PyExc_ValueError,
835n/a "bad marshal data (unnormalized long data)");
836n/a return NULL;
837n/a }
838n/a d += (digit)md << j*PyLong_MARSHAL_SHIFT;
839n/a }
840n/a if (PyErr_Occurred()) {
841n/a Py_DECREF(ob);
842n/a return NULL;
843n/a }
844n/a /* top digit should be nonzero, else the resulting PyLong won't be
845n/a normalized */
846n/a ob->ob_digit[size-1] = d;
847n/a return (PyObject *)ob;
848n/a bad_digit:
849n/a Py_DECREF(ob);
850n/a PyErr_SetString(PyExc_ValueError,
851n/a "bad marshal data (digit out of range in long)");
852n/a return NULL;
853n/a}
854n/a
855n/a/* allocate the reflist index for a new object. Return -1 on failure */
856n/astatic Py_ssize_t
857n/ar_ref_reserve(int flag, RFILE *p)
858n/a{
859n/a if (flag) { /* currently only FLAG_REF is defined */
860n/a Py_ssize_t idx = PyList_GET_SIZE(p->refs);
861n/a if (idx >= 0x7ffffffe) {
862n/a PyErr_SetString(PyExc_ValueError, "bad marshal data (index list too large)");
863n/a return -1;
864n/a }
865n/a if (PyList_Append(p->refs, Py_None) < 0)
866n/a return -1;
867n/a return idx;
868n/a } else
869n/a return 0;
870n/a}
871n/a
872n/a/* insert the new object 'o' to the reflist at previously
873n/a * allocated index 'idx'.
874n/a * 'o' can be NULL, in which case nothing is done.
875n/a * if 'o' was non-NULL, and the function succeeds, 'o' is returned.
876n/a * if 'o' was non-NULL, and the function fails, 'o' is released and
877n/a * NULL returned. This simplifies error checking at the call site since
878n/a * a single test for NULL for the function result is enough.
879n/a */
880n/astatic PyObject *
881n/ar_ref_insert(PyObject *o, Py_ssize_t idx, int flag, RFILE *p)
882n/a{
883n/a if (o != NULL && flag) { /* currently only FLAG_REF is defined */
884n/a PyObject *tmp = PyList_GET_ITEM(p->refs, idx);
885n/a Py_INCREF(o);
886n/a PyList_SET_ITEM(p->refs, idx, o);
887n/a Py_DECREF(tmp);
888n/a }
889n/a return o;
890n/a}
891n/a
892n/a/* combination of both above, used when an object can be
893n/a * created whenever it is seen in the file, as opposed to
894n/a * after having loaded its sub-objects.
895n/a */
896n/astatic PyObject *
897n/ar_ref(PyObject *o, int flag, RFILE *p)
898n/a{
899n/a assert(flag & FLAG_REF);
900n/a if (o == NULL)
901n/a return NULL;
902n/a if (PyList_Append(p->refs, o) < 0) {
903n/a Py_DECREF(o); /* release the new object */
904n/a return NULL;
905n/a }
906n/a return o;
907n/a}
908n/a
909n/astatic PyObject *
910n/ar_object(RFILE *p)
911n/a{
912n/a /* NULL is a valid return value, it does not necessarily means that
913n/a an exception is set. */
914n/a PyObject *v, *v2;
915n/a Py_ssize_t idx = 0;
916n/a long i, n;
917n/a int type, code = r_byte(p);
918n/a int flag, is_interned = 0;
919n/a PyObject *retval = NULL;
920n/a
921n/a if (code == EOF) {
922n/a PyErr_SetString(PyExc_EOFError,
923n/a "EOF read where object expected");
924n/a return NULL;
925n/a }
926n/a
927n/a p->depth++;
928n/a
929n/a if (p->depth > MAX_MARSHAL_STACK_DEPTH) {
930n/a p->depth--;
931n/a PyErr_SetString(PyExc_ValueError, "recursion limit exceeded");
932n/a return NULL;
933n/a }
934n/a
935n/a flag = code & FLAG_REF;
936n/a type = code & ~FLAG_REF;
937n/a
938n/a#define R_REF(O) do{\
939n/a if (flag) \
940n/a O = r_ref(O, flag, p);\
941n/a} while (0)
942n/a
943n/a switch (type) {
944n/a
945n/a case TYPE_NULL:
946n/a break;
947n/a
948n/a case TYPE_NONE:
949n/a Py_INCREF(Py_None);
950n/a retval = Py_None;
951n/a break;
952n/a
953n/a case TYPE_STOPITER:
954n/a Py_INCREF(PyExc_StopIteration);
955n/a retval = PyExc_StopIteration;
956n/a break;
957n/a
958n/a case TYPE_ELLIPSIS:
959n/a Py_INCREF(Py_Ellipsis);
960n/a retval = Py_Ellipsis;
961n/a break;
962n/a
963n/a case TYPE_FALSE:
964n/a Py_INCREF(Py_False);
965n/a retval = Py_False;
966n/a break;
967n/a
968n/a case TYPE_TRUE:
969n/a Py_INCREF(Py_True);
970n/a retval = Py_True;
971n/a break;
972n/a
973n/a case TYPE_INT:
974n/a n = r_long(p);
975n/a retval = PyErr_Occurred() ? NULL : PyLong_FromLong(n);
976n/a R_REF(retval);
977n/a break;
978n/a
979n/a case TYPE_LONG:
980n/a retval = r_PyLong(p);
981n/a R_REF(retval);
982n/a break;
983n/a
984n/a case TYPE_FLOAT:
985n/a {
986n/a char buf[256];
987n/a const char *ptr;
988n/a double dx;
989n/a n = r_byte(p);
990n/a if (n == EOF) {
991n/a PyErr_SetString(PyExc_EOFError,
992n/a "EOF read where object expected");
993n/a break;
994n/a }
995n/a ptr = r_string(n, p);
996n/a if (ptr == NULL)
997n/a break;
998n/a memcpy(buf, ptr, n);
999n/a buf[n] = '\0';
1000n/a dx = PyOS_string_to_double(buf, NULL, NULL);
1001n/a if (dx == -1.0 && PyErr_Occurred())
1002n/a break;
1003n/a retval = PyFloat_FromDouble(dx);
1004n/a R_REF(retval);
1005n/a break;
1006n/a }
1007n/a
1008n/a case TYPE_BINARY_FLOAT:
1009n/a {
1010n/a const unsigned char *buf;
1011n/a double x;
1012n/a buf = (const unsigned char *) r_string(8, p);
1013n/a if (buf == NULL)
1014n/a break;
1015n/a x = _PyFloat_Unpack8(buf, 1);
1016n/a if (x == -1.0 && PyErr_Occurred())
1017n/a break;
1018n/a retval = PyFloat_FromDouble(x);
1019n/a R_REF(retval);
1020n/a break;
1021n/a }
1022n/a
1023n/a case TYPE_COMPLEX:
1024n/a {
1025n/a char buf[256];
1026n/a const char *ptr;
1027n/a Py_complex c;
1028n/a n = r_byte(p);
1029n/a if (n == EOF) {
1030n/a PyErr_SetString(PyExc_EOFError,
1031n/a "EOF read where object expected");
1032n/a break;
1033n/a }
1034n/a ptr = r_string(n, p);
1035n/a if (ptr == NULL)
1036n/a break;
1037n/a memcpy(buf, ptr, n);
1038n/a buf[n] = '\0';
1039n/a c.real = PyOS_string_to_double(buf, NULL, NULL);
1040n/a if (c.real == -1.0 && PyErr_Occurred())
1041n/a break;
1042n/a n = r_byte(p);
1043n/a if (n == EOF) {
1044n/a PyErr_SetString(PyExc_EOFError,
1045n/a "EOF read where object expected");
1046n/a break;
1047n/a }
1048n/a ptr = r_string(n, p);
1049n/a if (ptr == NULL)
1050n/a break;
1051n/a memcpy(buf, ptr, n);
1052n/a buf[n] = '\0';
1053n/a c.imag = PyOS_string_to_double(buf, NULL, NULL);
1054n/a if (c.imag == -1.0 && PyErr_Occurred())
1055n/a break;
1056n/a retval = PyComplex_FromCComplex(c);
1057n/a R_REF(retval);
1058n/a break;
1059n/a }
1060n/a
1061n/a case TYPE_BINARY_COMPLEX:
1062n/a {
1063n/a const unsigned char *buf;
1064n/a Py_complex c;
1065n/a buf = (const unsigned char *) r_string(8, p);
1066n/a if (buf == NULL)
1067n/a break;
1068n/a c.real = _PyFloat_Unpack8(buf, 1);
1069n/a if (c.real == -1.0 && PyErr_Occurred())
1070n/a break;
1071n/a buf = (const unsigned char *) r_string(8, p);
1072n/a if (buf == NULL)
1073n/a break;
1074n/a c.imag = _PyFloat_Unpack8(buf, 1);
1075n/a if (c.imag == -1.0 && PyErr_Occurred())
1076n/a break;
1077n/a retval = PyComplex_FromCComplex(c);
1078n/a R_REF(retval);
1079n/a break;
1080n/a }
1081n/a
1082n/a case TYPE_STRING:
1083n/a {
1084n/a const char *ptr;
1085n/a n = r_long(p);
1086n/a if (PyErr_Occurred())
1087n/a break;
1088n/a if (n < 0 || n > SIZE32_MAX) {
1089n/a PyErr_SetString(PyExc_ValueError, "bad marshal data (string size out of range)");
1090n/a break;
1091n/a }
1092n/a v = PyBytes_FromStringAndSize((char *)NULL, n);
1093n/a if (v == NULL)
1094n/a break;
1095n/a ptr = r_string(n, p);
1096n/a if (ptr == NULL) {
1097n/a Py_DECREF(v);
1098n/a break;
1099n/a }
1100n/a memcpy(PyBytes_AS_STRING(v), ptr, n);
1101n/a retval = v;
1102n/a R_REF(retval);
1103n/a break;
1104n/a }
1105n/a
1106n/a case TYPE_ASCII_INTERNED:
1107n/a is_interned = 1;
1108n/a case TYPE_ASCII:
1109n/a n = r_long(p);
1110n/a if (PyErr_Occurred())
1111n/a break;
1112n/a if (n < 0 || n > SIZE32_MAX) {
1113n/a PyErr_SetString(PyExc_ValueError, "bad marshal data (unicode size out of range)");
1114n/a break;
1115n/a }
1116n/a goto _read_ascii;
1117n/a
1118n/a case TYPE_SHORT_ASCII_INTERNED:
1119n/a is_interned = 1;
1120n/a case TYPE_SHORT_ASCII:
1121n/a n = r_byte(p);
1122n/a if (n == EOF) {
1123n/a PyErr_SetString(PyExc_EOFError,
1124n/a "EOF read where object expected");
1125n/a break;
1126n/a }
1127n/a _read_ascii:
1128n/a {
1129n/a const char *ptr;
1130n/a ptr = r_string(n, p);
1131n/a if (ptr == NULL)
1132n/a break;
1133n/a v = PyUnicode_FromKindAndData(PyUnicode_1BYTE_KIND, ptr, n);
1134n/a if (v == NULL)
1135n/a break;
1136n/a if (is_interned)
1137n/a PyUnicode_InternInPlace(&v);
1138n/a retval = v;
1139n/a R_REF(retval);
1140n/a break;
1141n/a }
1142n/a
1143n/a case TYPE_INTERNED:
1144n/a is_interned = 1;
1145n/a case TYPE_UNICODE:
1146n/a {
1147n/a const char *buffer;
1148n/a
1149n/a n = r_long(p);
1150n/a if (PyErr_Occurred())
1151n/a break;
1152n/a if (n < 0 || n > SIZE32_MAX) {
1153n/a PyErr_SetString(PyExc_ValueError, "bad marshal data (unicode size out of range)");
1154n/a break;
1155n/a }
1156n/a if (n != 0) {
1157n/a buffer = r_string(n, p);
1158n/a if (buffer == NULL)
1159n/a break;
1160n/a v = PyUnicode_DecodeUTF8(buffer, n, "surrogatepass");
1161n/a }
1162n/a else {
1163n/a v = PyUnicode_New(0, 0);
1164n/a }
1165n/a if (v == NULL)
1166n/a break;
1167n/a if (is_interned)
1168n/a PyUnicode_InternInPlace(&v);
1169n/a retval = v;
1170n/a R_REF(retval);
1171n/a break;
1172n/a }
1173n/a
1174n/a case TYPE_SMALL_TUPLE:
1175n/a n = (unsigned char) r_byte(p);
1176n/a if (PyErr_Occurred())
1177n/a break;
1178n/a goto _read_tuple;
1179n/a case TYPE_TUPLE:
1180n/a n = r_long(p);
1181n/a if (PyErr_Occurred())
1182n/a break;
1183n/a if (n < 0 || n > SIZE32_MAX) {
1184n/a PyErr_SetString(PyExc_ValueError, "bad marshal data (tuple size out of range)");
1185n/a break;
1186n/a }
1187n/a _read_tuple:
1188n/a v = PyTuple_New(n);
1189n/a R_REF(v);
1190n/a if (v == NULL)
1191n/a break;
1192n/a
1193n/a for (i = 0; i < n; i++) {
1194n/a v2 = r_object(p);
1195n/a if ( v2 == NULL ) {
1196n/a if (!PyErr_Occurred())
1197n/a PyErr_SetString(PyExc_TypeError,
1198n/a "NULL object in marshal data for tuple");
1199n/a Py_DECREF(v);
1200n/a v = NULL;
1201n/a break;
1202n/a }
1203n/a PyTuple_SET_ITEM(v, i, v2);
1204n/a }
1205n/a retval = v;
1206n/a break;
1207n/a
1208n/a case TYPE_LIST:
1209n/a n = r_long(p);
1210n/a if (PyErr_Occurred())
1211n/a break;
1212n/a if (n < 0 || n > SIZE32_MAX) {
1213n/a PyErr_SetString(PyExc_ValueError, "bad marshal data (list size out of range)");
1214n/a break;
1215n/a }
1216n/a v = PyList_New(n);
1217n/a R_REF(v);
1218n/a if (v == NULL)
1219n/a break;
1220n/a for (i = 0; i < n; i++) {
1221n/a v2 = r_object(p);
1222n/a if ( v2 == NULL ) {
1223n/a if (!PyErr_Occurred())
1224n/a PyErr_SetString(PyExc_TypeError,
1225n/a "NULL object in marshal data for list");
1226n/a Py_DECREF(v);
1227n/a v = NULL;
1228n/a break;
1229n/a }
1230n/a PyList_SET_ITEM(v, i, v2);
1231n/a }
1232n/a retval = v;
1233n/a break;
1234n/a
1235n/a case TYPE_DICT:
1236n/a v = PyDict_New();
1237n/a R_REF(v);
1238n/a if (v == NULL)
1239n/a break;
1240n/a for (;;) {
1241n/a PyObject *key, *val;
1242n/a key = r_object(p);
1243n/a if (key == NULL)
1244n/a break;
1245n/a val = r_object(p);
1246n/a if (val == NULL) {
1247n/a Py_DECREF(key);
1248n/a break;
1249n/a }
1250n/a if (PyDict_SetItem(v, key, val) < 0) {
1251n/a Py_DECREF(key);
1252n/a Py_DECREF(val);
1253n/a break;
1254n/a }
1255n/a Py_DECREF(key);
1256n/a Py_DECREF(val);
1257n/a }
1258n/a if (PyErr_Occurred()) {
1259n/a Py_DECREF(v);
1260n/a v = NULL;
1261n/a }
1262n/a retval = v;
1263n/a break;
1264n/a
1265n/a case TYPE_SET:
1266n/a case TYPE_FROZENSET:
1267n/a n = r_long(p);
1268n/a if (PyErr_Occurred())
1269n/a break;
1270n/a if (n < 0 || n > SIZE32_MAX) {
1271n/a PyErr_SetString(PyExc_ValueError, "bad marshal data (set size out of range)");
1272n/a break;
1273n/a }
1274n/a
1275n/a if (n == 0 && type == TYPE_FROZENSET) {
1276n/a /* call frozenset() to get the empty frozenset singleton */
1277n/a v = _PyObject_CallNoArg((PyObject*)&PyFrozenSet_Type);
1278n/a if (v == NULL)
1279n/a break;
1280n/a R_REF(v);
1281n/a retval = v;
1282n/a }
1283n/a else {
1284n/a v = (type == TYPE_SET) ? PySet_New(NULL) : PyFrozenSet_New(NULL);
1285n/a if (type == TYPE_SET) {
1286n/a R_REF(v);
1287n/a } else {
1288n/a /* must use delayed registration of frozensets because they must
1289n/a * be init with a refcount of 1
1290n/a */
1291n/a idx = r_ref_reserve(flag, p);
1292n/a if (idx < 0)
1293n/a Py_CLEAR(v); /* signal error */
1294n/a }
1295n/a if (v == NULL)
1296n/a break;
1297n/a
1298n/a for (i = 0; i < n; i++) {
1299n/a v2 = r_object(p);
1300n/a if ( v2 == NULL ) {
1301n/a if (!PyErr_Occurred())
1302n/a PyErr_SetString(PyExc_TypeError,
1303n/a "NULL object in marshal data for set");
1304n/a Py_DECREF(v);
1305n/a v = NULL;
1306n/a break;
1307n/a }
1308n/a if (PySet_Add(v, v2) == -1) {
1309n/a Py_DECREF(v);
1310n/a Py_DECREF(v2);
1311n/a v = NULL;
1312n/a break;
1313n/a }
1314n/a Py_DECREF(v2);
1315n/a }
1316n/a if (type != TYPE_SET)
1317n/a v = r_ref_insert(v, idx, flag, p);
1318n/a retval = v;
1319n/a }
1320n/a break;
1321n/a
1322n/a case TYPE_CODE:
1323n/a {
1324n/a int argcount;
1325n/a int kwonlyargcount;
1326n/a int nlocals;
1327n/a int stacksize;
1328n/a int flags;
1329n/a PyObject *code = NULL;
1330n/a PyObject *consts = NULL;
1331n/a PyObject *names = NULL;
1332n/a PyObject *varnames = NULL;
1333n/a PyObject *freevars = NULL;
1334n/a PyObject *cellvars = NULL;
1335n/a PyObject *filename = NULL;
1336n/a PyObject *name = NULL;
1337n/a int firstlineno;
1338n/a PyObject *lnotab = NULL;
1339n/a
1340n/a idx = r_ref_reserve(flag, p);
1341n/a if (idx < 0)
1342n/a break;
1343n/a
1344n/a v = NULL;
1345n/a
1346n/a /* XXX ignore long->int overflows for now */
1347n/a argcount = (int)r_long(p);
1348n/a if (PyErr_Occurred())
1349n/a goto code_error;
1350n/a kwonlyargcount = (int)r_long(p);
1351n/a if (PyErr_Occurred())
1352n/a goto code_error;
1353n/a nlocals = (int)r_long(p);
1354n/a if (PyErr_Occurred())
1355n/a goto code_error;
1356n/a stacksize = (int)r_long(p);
1357n/a if (PyErr_Occurred())
1358n/a goto code_error;
1359n/a flags = (int)r_long(p);
1360n/a if (PyErr_Occurred())
1361n/a goto code_error;
1362n/a code = r_object(p);
1363n/a if (code == NULL)
1364n/a goto code_error;
1365n/a consts = r_object(p);
1366n/a if (consts == NULL)
1367n/a goto code_error;
1368n/a names = r_object(p);
1369n/a if (names == NULL)
1370n/a goto code_error;
1371n/a varnames = r_object(p);
1372n/a if (varnames == NULL)
1373n/a goto code_error;
1374n/a freevars = r_object(p);
1375n/a if (freevars == NULL)
1376n/a goto code_error;
1377n/a cellvars = r_object(p);
1378n/a if (cellvars == NULL)
1379n/a goto code_error;
1380n/a filename = r_object(p);
1381n/a if (filename == NULL)
1382n/a goto code_error;
1383n/a if (PyUnicode_CheckExact(filename)) {
1384n/a if (p->current_filename != NULL) {
1385n/a if (!PyUnicode_Compare(filename, p->current_filename)) {
1386n/a Py_DECREF(filename);
1387n/a Py_INCREF(p->current_filename);
1388n/a filename = p->current_filename;
1389n/a }
1390n/a }
1391n/a else {
1392n/a p->current_filename = filename;
1393n/a }
1394n/a }
1395n/a name = r_object(p);
1396n/a if (name == NULL)
1397n/a goto code_error;
1398n/a firstlineno = (int)r_long(p);
1399n/a if (firstlineno == -1 && PyErr_Occurred())
1400n/a break;
1401n/a lnotab = r_object(p);
1402n/a if (lnotab == NULL)
1403n/a goto code_error;
1404n/a
1405n/a v = (PyObject *) PyCode_New(
1406n/a argcount, kwonlyargcount,
1407n/a nlocals, stacksize, flags,
1408n/a code, consts, names, varnames,
1409n/a freevars, cellvars, filename, name,
1410n/a firstlineno, lnotab);
1411n/a v = r_ref_insert(v, idx, flag, p);
1412n/a
1413n/a code_error:
1414n/a Py_XDECREF(code);
1415n/a Py_XDECREF(consts);
1416n/a Py_XDECREF(names);
1417n/a Py_XDECREF(varnames);
1418n/a Py_XDECREF(freevars);
1419n/a Py_XDECREF(cellvars);
1420n/a Py_XDECREF(filename);
1421n/a Py_XDECREF(name);
1422n/a Py_XDECREF(lnotab);
1423n/a }
1424n/a retval = v;
1425n/a break;
1426n/a
1427n/a case TYPE_REF:
1428n/a n = r_long(p);
1429n/a if (n < 0 || n >= PyList_GET_SIZE(p->refs)) {
1430n/a if (n == -1 && PyErr_Occurred())
1431n/a break;
1432n/a PyErr_SetString(PyExc_ValueError, "bad marshal data (invalid reference)");
1433n/a break;
1434n/a }
1435n/a v = PyList_GET_ITEM(p->refs, n);
1436n/a if (v == Py_None) {
1437n/a PyErr_SetString(PyExc_ValueError, "bad marshal data (invalid reference)");
1438n/a break;
1439n/a }
1440n/a Py_INCREF(v);
1441n/a retval = v;
1442n/a break;
1443n/a
1444n/a default:
1445n/a /* Bogus data got written, which isn't ideal.
1446n/a This will let you keep working and recover. */
1447n/a PyErr_SetString(PyExc_ValueError, "bad marshal data (unknown type code)");
1448n/a break;
1449n/a
1450n/a }
1451n/a p->depth--;
1452n/a return retval;
1453n/a}
1454n/a
1455n/astatic PyObject *
1456n/aread_object(RFILE *p)
1457n/a{
1458n/a PyObject *v;
1459n/a if (PyErr_Occurred()) {
1460n/a fprintf(stderr, "XXX readobject called with exception set\n");
1461n/a return NULL;
1462n/a }
1463n/a v = r_object(p);
1464n/a if (v == NULL && !PyErr_Occurred())
1465n/a PyErr_SetString(PyExc_TypeError, "NULL object in marshal data for object");
1466n/a return v;
1467n/a}
1468n/a
1469n/aint
1470n/aPyMarshal_ReadShortFromFile(FILE *fp)
1471n/a{
1472n/a RFILE rf;
1473n/a int res;
1474n/a assert(fp);
1475n/a rf.readable = NULL;
1476n/a rf.fp = fp;
1477n/a rf.current_filename = NULL;
1478n/a rf.end = rf.ptr = NULL;
1479n/a rf.buf = NULL;
1480n/a res = r_short(&rf);
1481n/a if (rf.buf != NULL)
1482n/a PyMem_FREE(rf.buf);
1483n/a return res;
1484n/a}
1485n/a
1486n/along
1487n/aPyMarshal_ReadLongFromFile(FILE *fp)
1488n/a{
1489n/a RFILE rf;
1490n/a long res;
1491n/a rf.fp = fp;
1492n/a rf.readable = NULL;
1493n/a rf.current_filename = NULL;
1494n/a rf.ptr = rf.end = NULL;
1495n/a rf.buf = NULL;
1496n/a res = r_long(&rf);
1497n/a if (rf.buf != NULL)
1498n/a PyMem_FREE(rf.buf);
1499n/a return res;
1500n/a}
1501n/a
1502n/a/* Return size of file in bytes; < 0 if unknown or INT_MAX if too big */
1503n/astatic off_t
1504n/agetfilesize(FILE *fp)
1505n/a{
1506n/a struct _Py_stat_struct st;
1507n/a if (_Py_fstat_noraise(fileno(fp), &st) != 0)
1508n/a return -1;
1509n/a#if SIZEOF_OFF_T == 4
1510n/a else if (st.st_size >= INT_MAX)
1511n/a return (off_t)INT_MAX;
1512n/a#endif
1513n/a else
1514n/a return (off_t)st.st_size;
1515n/a}
1516n/a
1517n/a/* If we can get the size of the file up-front, and it's reasonably small,
1518n/a * read it in one gulp and delegate to ...FromString() instead. Much quicker
1519n/a * than reading a byte at a time from file; speeds .pyc imports.
1520n/a * CAUTION: since this may read the entire remainder of the file, don't
1521n/a * call it unless you know you're done with the file.
1522n/a */
1523n/aPyObject *
1524n/aPyMarshal_ReadLastObjectFromFile(FILE *fp)
1525n/a{
1526n/a/* REASONABLE_FILE_LIMIT is by defn something big enough for Tkinter.pyc. */
1527n/a#define REASONABLE_FILE_LIMIT (1L << 18)
1528n/a off_t filesize;
1529n/a filesize = getfilesize(fp);
1530n/a if (filesize > 0 && filesize <= REASONABLE_FILE_LIMIT) {
1531n/a char* pBuf = (char *)PyMem_MALLOC(filesize);
1532n/a if (pBuf != NULL) {
1533n/a size_t n = fread(pBuf, 1, (size_t)filesize, fp);
1534n/a PyObject* v = PyMarshal_ReadObjectFromString(pBuf, n);
1535n/a PyMem_FREE(pBuf);
1536n/a return v;
1537n/a }
1538n/a
1539n/a }
1540n/a /* We don't have fstat, or we do but the file is larger than
1541n/a * REASONABLE_FILE_LIMIT or malloc failed -- read a byte at a time.
1542n/a */
1543n/a return PyMarshal_ReadObjectFromFile(fp);
1544n/a
1545n/a#undef REASONABLE_FILE_LIMIT
1546n/a}
1547n/a
1548n/aPyObject *
1549n/aPyMarshal_ReadObjectFromFile(FILE *fp)
1550n/a{
1551n/a RFILE rf;
1552n/a PyObject *result;
1553n/a rf.fp = fp;
1554n/a rf.readable = NULL;
1555n/a rf.current_filename = NULL;
1556n/a rf.depth = 0;
1557n/a rf.ptr = rf.end = NULL;
1558n/a rf.buf = NULL;
1559n/a rf.refs = PyList_New(0);
1560n/a if (rf.refs == NULL)
1561n/a return NULL;
1562n/a result = r_object(&rf);
1563n/a Py_DECREF(rf.refs);
1564n/a if (rf.buf != NULL)
1565n/a PyMem_FREE(rf.buf);
1566n/a return result;
1567n/a}
1568n/a
1569n/aPyObject *
1570n/aPyMarshal_ReadObjectFromString(const char *str, Py_ssize_t len)
1571n/a{
1572n/a RFILE rf;
1573n/a PyObject *result;
1574n/a rf.fp = NULL;
1575n/a rf.readable = NULL;
1576n/a rf.current_filename = NULL;
1577n/a rf.ptr = (char *)str;
1578n/a rf.end = (char *)str + len;
1579n/a rf.buf = NULL;
1580n/a rf.depth = 0;
1581n/a rf.refs = PyList_New(0);
1582n/a if (rf.refs == NULL)
1583n/a return NULL;
1584n/a result = r_object(&rf);
1585n/a Py_DECREF(rf.refs);
1586n/a if (rf.buf != NULL)
1587n/a PyMem_FREE(rf.buf);
1588n/a return result;
1589n/a}
1590n/a
1591n/aPyObject *
1592n/aPyMarshal_WriteObjectToString(PyObject *x, int version)
1593n/a{
1594n/a WFILE wf;
1595n/a
1596n/a memset(&wf, 0, sizeof(wf));
1597n/a wf.str = PyBytes_FromStringAndSize((char *)NULL, 50);
1598n/a if (wf.str == NULL)
1599n/a return NULL;
1600n/a wf.ptr = wf.buf = PyBytes_AS_STRING((PyBytesObject *)wf.str);
1601n/a wf.end = wf.ptr + PyBytes_Size(wf.str);
1602n/a wf.error = WFERR_OK;
1603n/a wf.version = version;
1604n/a if (w_init_refs(&wf, version)) {
1605n/a Py_DECREF(wf.str);
1606n/a return NULL;
1607n/a }
1608n/a w_object(x, &wf);
1609n/a w_clear_refs(&wf);
1610n/a if (wf.str != NULL) {
1611n/a char *base = PyBytes_AS_STRING((PyBytesObject *)wf.str);
1612n/a if (wf.ptr - base > PY_SSIZE_T_MAX) {
1613n/a Py_DECREF(wf.str);
1614n/a PyErr_SetString(PyExc_OverflowError,
1615n/a "too much marshal data for a string");
1616n/a return NULL;
1617n/a }
1618n/a if (_PyBytes_Resize(&wf.str, (Py_ssize_t)(wf.ptr - base)) < 0)
1619n/a return NULL;
1620n/a }
1621n/a if (wf.error != WFERR_OK) {
1622n/a Py_XDECREF(wf.str);
1623n/a if (wf.error == WFERR_NOMEMORY)
1624n/a PyErr_NoMemory();
1625n/a else
1626n/a PyErr_SetString(PyExc_ValueError,
1627n/a (wf.error==WFERR_UNMARSHALLABLE)?"unmarshallable object"
1628n/a :"object too deeply nested to marshal");
1629n/a return NULL;
1630n/a }
1631n/a return wf.str;
1632n/a}
1633n/a
1634n/a/* And an interface for Python programs... */
1635n/a
1636n/astatic PyObject *
1637n/amarshal_dump(PyObject *self, PyObject *args)
1638n/a{
1639n/a /* XXX Quick hack -- need to do this differently */
1640n/a PyObject *x;
1641n/a PyObject *f;
1642n/a int version = Py_MARSHAL_VERSION;
1643n/a PyObject *s;
1644n/a PyObject *res;
1645n/a _Py_IDENTIFIER(write);
1646n/a
1647n/a if (!PyArg_ParseTuple(args, "OO|i:dump", &x, &f, &version))
1648n/a return NULL;
1649n/a s = PyMarshal_WriteObjectToString(x, version);
1650n/a if (s == NULL)
1651n/a return NULL;
1652n/a res = _PyObject_CallMethodIdObjArgs(f, &PyId_write, s, NULL);
1653n/a Py_DECREF(s);
1654n/a return res;
1655n/a}
1656n/a
1657n/aPyDoc_STRVAR(dump_doc,
1658n/a"dump(value, file[, version])\n\
1659n/a\n\
1660n/aWrite the value on the open file. The value must be a supported type.\n\
1661n/aThe file must be an open file object such as sys.stdout or returned by\n\
1662n/aopen() or os.popen(). It must be opened in binary mode ('wb' or 'w+b').\n\
1663n/a\n\
1664n/aIf the value has (or contains an object that has) an unsupported type, a\n\
1665n/aValueError exception is raised - but garbage data will also be written\n\
1666n/ato the file. The object will not be properly read back by load()\n\
1667n/a\n\
1668n/aThe version argument indicates the data format that dump should use.");
1669n/a
1670n/astatic PyObject *
1671n/amarshal_load(PyObject *self, PyObject *f)
1672n/a{
1673n/a PyObject *data, *result;
1674n/a _Py_IDENTIFIER(read);
1675n/a RFILE rf;
1676n/a
1677n/a /*
1678n/a * Make a call to the read method, but read zero bytes.
1679n/a * This is to ensure that the object passed in at least
1680n/a * has a read method which returns bytes.
1681n/a * This can be removed if we guarantee good error handling
1682n/a * for r_string()
1683n/a */
1684n/a data = _PyObject_CallMethodId(f, &PyId_read, "i", 0);
1685n/a if (data == NULL)
1686n/a return NULL;
1687n/a if (!PyBytes_Check(data)) {
1688n/a PyErr_Format(PyExc_TypeError,
1689n/a "f.read() returned not bytes but %.100s",
1690n/a data->ob_type->tp_name);
1691n/a result = NULL;
1692n/a }
1693n/a else {
1694n/a rf.depth = 0;
1695n/a rf.fp = NULL;
1696n/a rf.readable = f;
1697n/a rf.current_filename = NULL;
1698n/a rf.ptr = rf.end = NULL;
1699n/a rf.buf = NULL;
1700n/a if ((rf.refs = PyList_New(0)) != NULL) {
1701n/a result = read_object(&rf);
1702n/a Py_DECREF(rf.refs);
1703n/a if (rf.buf != NULL)
1704n/a PyMem_FREE(rf.buf);
1705n/a } else
1706n/a result = NULL;
1707n/a }
1708n/a Py_DECREF(data);
1709n/a return result;
1710n/a}
1711n/a
1712n/aPyDoc_STRVAR(load_doc,
1713n/a"load(file)\n\
1714n/a\n\
1715n/aRead one value from the open file and return it. If no valid value is\n\
1716n/aread (e.g. because the data has a different Python version's\n\
1717n/aincompatible marshal format), raise EOFError, ValueError or TypeError.\n\
1718n/aThe file must be an open file object opened in binary mode ('rb' or\n\
1719n/a'r+b').\n\
1720n/a\n\
1721n/aNote: If an object containing an unsupported type was marshalled with\n\
1722n/adump(), load() will substitute None for the unmarshallable type.");
1723n/a
1724n/a
1725n/astatic PyObject *
1726n/amarshal_dumps(PyObject *self, PyObject *args)
1727n/a{
1728n/a PyObject *x;
1729n/a int version = Py_MARSHAL_VERSION;
1730n/a if (!PyArg_ParseTuple(args, "O|i:dumps", &x, &version))
1731n/a return NULL;
1732n/a return PyMarshal_WriteObjectToString(x, version);
1733n/a}
1734n/a
1735n/aPyDoc_STRVAR(dumps_doc,
1736n/a"dumps(value[, version])\n\
1737n/a\n\
1738n/aReturn the string that would be written to a file by dump(value, file).\n\
1739n/aThe value must be a supported type. Raise a ValueError exception if\n\
1740n/avalue has (or contains an object that has) an unsupported type.\n\
1741n/a\n\
1742n/aThe version argument indicates the data format that dumps should use.");
1743n/a
1744n/a
1745n/astatic PyObject *
1746n/amarshal_loads(PyObject *self, PyObject *args)
1747n/a{
1748n/a RFILE rf;
1749n/a Py_buffer p;
1750n/a char *s;
1751n/a Py_ssize_t n;
1752n/a PyObject* result;
1753n/a if (!PyArg_ParseTuple(args, "y*:loads", &p))
1754n/a return NULL;
1755n/a s = p.buf;
1756n/a n = p.len;
1757n/a rf.fp = NULL;
1758n/a rf.readable = NULL;
1759n/a rf.current_filename = NULL;
1760n/a rf.ptr = s;
1761n/a rf.end = s + n;
1762n/a rf.depth = 0;
1763n/a if ((rf.refs = PyList_New(0)) == NULL)
1764n/a return NULL;
1765n/a result = read_object(&rf);
1766n/a PyBuffer_Release(&p);
1767n/a Py_DECREF(rf.refs);
1768n/a return result;
1769n/a}
1770n/a
1771n/aPyDoc_STRVAR(loads_doc,
1772n/a"loads(bytes)\n\
1773n/a\n\
1774n/aConvert the bytes object to a value. If no valid value is found, raise\n\
1775n/aEOFError, ValueError or TypeError. Extra characters in the input are\n\
1776n/aignored.");
1777n/a
1778n/astatic PyMethodDef marshal_methods[] = {
1779n/a {"dump", marshal_dump, METH_VARARGS, dump_doc},
1780n/a {"load", marshal_load, METH_O, load_doc},
1781n/a {"dumps", marshal_dumps, METH_VARARGS, dumps_doc},
1782n/a {"loads", marshal_loads, METH_VARARGS, loads_doc},
1783n/a {NULL, NULL} /* sentinel */
1784n/a};
1785n/a
1786n/a
1787n/aPyDoc_STRVAR(module_doc,
1788n/a"This module contains functions that can read and write Python values in\n\
1789n/aa binary format. The format is specific to Python, but independent of\n\
1790n/amachine architecture issues.\n\
1791n/a\n\
1792n/aNot all Python object types are supported; in general, only objects\n\
1793n/awhose value is independent from a particular invocation of Python can be\n\
1794n/awritten and read by this module. The following types are supported:\n\
1795n/aNone, integers, floating point numbers, strings, bytes, bytearrays,\n\
1796n/atuples, lists, sets, dictionaries, and code objects, where it\n\
1797n/ashould be understood that tuples, lists and dictionaries are only\n\
1798n/asupported as long as the values contained therein are themselves\n\
1799n/asupported; and recursive lists and dictionaries should not be written\n\
1800n/a(they will cause infinite loops).\n\
1801n/a\n\
1802n/aVariables:\n\
1803n/a\n\
1804n/aversion -- indicates the format that the module uses. Version 0 is the\n\
1805n/a historical format, version 1 shares interned strings and version 2\n\
1806n/a uses a binary format for floating point numbers.\n\
1807n/a Version 3 shares common object references (New in version 3.4).\n\
1808n/a\n\
1809n/aFunctions:\n\
1810n/a\n\
1811n/adump() -- write value to a file\n\
1812n/aload() -- read value from a file\n\
1813n/adumps() -- write value to a string\n\
1814n/aloads() -- read value from a string");
1815n/a
1816n/a
1817n/a
1818n/astatic struct PyModuleDef marshalmodule = {
1819n/a PyModuleDef_HEAD_INIT,
1820n/a "marshal",
1821n/a module_doc,
1822n/a 0,
1823n/a marshal_methods,
1824n/a NULL,
1825n/a NULL,
1826n/a NULL,
1827n/a NULL
1828n/a};
1829n/a
1830n/aPyMODINIT_FUNC
1831n/aPyMarshal_Init(void)
1832n/a{
1833n/a PyObject *mod = PyModule_Create(&marshalmodule);
1834n/a if (mod == NULL)
1835n/a return NULL;
1836n/a PyModule_AddIntConstant(mod, "version", Py_MARSHAL_VERSION);
1837n/a return mod;
1838n/a}