marshal.c revision 8211237db89e1d77aff9d4da80059bae1cb3e881
1
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. */
6
7#include "Python.h"
8#include "longintrepr.h"
9#include "compile.h"
10#include "marshal.h"
11
12/* High water mark to determine when the marshalled object is dangerously deep
13 * and risks coring the interpreter.  When the object stack gets this deep,
14 * raise an exception instead of continuing.
15 */
16#define MAX_MARSHAL_STACK_DEPTH 5000
17
18#define TYPE_NULL	'0'
19#define TYPE_NONE	'N'
20#define TYPE_STOPITER	'S'
21#define TYPE_ELLIPSIS   '.'
22#define TYPE_INT	'i'
23#define TYPE_INT64	'I'
24#define TYPE_FLOAT	'f'
25#define TYPE_COMPLEX	'x'
26#define TYPE_LONG	'l'
27#define TYPE_STRING	's'
28#define TYPE_TUPLE	'('
29#define TYPE_LIST	'['
30#define TYPE_DICT	'{'
31#define TYPE_CODE	'c'
32#define TYPE_UNICODE	'u'
33#define TYPE_UNKNOWN	'?'
34
35typedef struct {
36	FILE *fp;
37	int error;
38	int depth;
39	/* If fp == NULL, the following are valid: */
40	PyObject *str;
41	char *ptr;
42	char *end;
43} WFILE;
44
45#define w_byte(c, p) if (((p)->fp)) putc((c), (p)->fp); \
46		      else if ((p)->ptr != (p)->end) *(p)->ptr++ = (c); \
47			   else w_more(c, p)
48
49static void
50w_more(int c, WFILE *p)
51{
52	int size, newsize;
53	if (p->str == NULL)
54		return; /* An error already occurred */
55	size = PyString_Size(p->str);
56	newsize = size + 1024;
57	if (_PyString_Resize(&p->str, newsize) != 0) {
58		p->ptr = p->end = NULL;
59	}
60	else {
61		p->ptr = PyString_AS_STRING((PyStringObject *)p->str) + size;
62		p->end =
63			PyString_AS_STRING((PyStringObject *)p->str) + newsize;
64		*p->ptr++ = Py_SAFE_DOWNCAST(c, int, char);
65	}
66}
67
68static void
69w_string(char *s, int n, WFILE *p)
70{
71	if (p->fp != NULL) {
72		fwrite(s, 1, n, p->fp);
73	}
74	else {
75		while (--n >= 0) {
76			w_byte(*s, p);
77			s++;
78		}
79	}
80}
81
82static void
83w_short(int x, WFILE *p)
84{
85	w_byte( x      & 0xff, p);
86	w_byte((x>> 8) & 0xff, p);
87}
88
89static void
90w_long(long x, WFILE *p)
91{
92	w_byte((int)( x      & 0xff), p);
93	w_byte((int)((x>> 8) & 0xff), p);
94	w_byte((int)((x>>16) & 0xff), p);
95	w_byte((int)((x>>24) & 0xff), p);
96}
97
98#if SIZEOF_LONG > 4
99static void
100w_long64(long x, WFILE *p)
101{
102	w_long(x, p);
103	w_long(x>>32, p);
104}
105#endif
106
107static void
108w_object(PyObject *v, WFILE *p)
109{
110	int i, n;
111	PyBufferProcs *pb;
112
113	p->depth++;
114
115	if (p->depth > MAX_MARSHAL_STACK_DEPTH) {
116		p->error = 2;
117	}
118	else if (v == NULL) {
119		w_byte(TYPE_NULL, p);
120	}
121	else if (v == Py_None) {
122		w_byte(TYPE_NONE, p);
123	}
124	else if (v == PyExc_StopIteration) {
125		w_byte(TYPE_STOPITER, p);
126	}
127	else if (v == Py_Ellipsis) {
128	        w_byte(TYPE_ELLIPSIS, p);
129	}
130	else if (PyInt_Check(v)) {
131		long x = PyInt_AS_LONG((PyIntObject *)v);
132#if SIZEOF_LONG > 4
133		long y = Py_ARITHMETIC_RIGHT_SHIFT(long, x, 31);
134		if (y && y != -1) {
135			w_byte(TYPE_INT64, p);
136			w_long64(x, p);
137		}
138		else
139#endif
140			{
141			w_byte(TYPE_INT, p);
142			w_long(x, p);
143		}
144	}
145	else if (PyLong_Check(v)) {
146		PyLongObject *ob = (PyLongObject *)v;
147		w_byte(TYPE_LONG, p);
148		n = ob->ob_size;
149		w_long((long)n, p);
150		if (n < 0)
151			n = -n;
152		for (i = 0; i < n; i++)
153			w_short(ob->ob_digit[i], p);
154	}
155	else if (PyFloat_Check(v)) {
156		char buf[256]; /* Plenty to format any double */
157		PyFloat_AsReprString(buf, (PyFloatObject *)v);
158		n = strlen(buf);
159		w_byte(TYPE_FLOAT, p);
160		w_byte(n, p);
161		w_string(buf, n, p);
162	}
163#ifndef WITHOUT_COMPLEX
164	else if (PyComplex_Check(v)) {
165		char buf[256]; /* Plenty to format any double */
166		PyFloatObject *temp;
167		w_byte(TYPE_COMPLEX, p);
168		temp = (PyFloatObject*)PyFloat_FromDouble(
169			PyComplex_RealAsDouble(v));
170		PyFloat_AsReprString(buf, temp);
171		Py_DECREF(temp);
172		n = strlen(buf);
173		w_byte(n, p);
174		w_string(buf, n, p);
175		temp = (PyFloatObject*)PyFloat_FromDouble(
176			PyComplex_ImagAsDouble(v));
177		PyFloat_AsReprString(buf, temp);
178		Py_DECREF(temp);
179		n = strlen(buf);
180		w_byte(n, p);
181		w_string(buf, n, p);
182	}
183#endif
184	else if (PyString_Check(v)) {
185		w_byte(TYPE_STRING, p);
186		n = PyString_GET_SIZE(v);
187		w_long((long)n, p);
188		w_string(PyString_AS_STRING(v), n, p);
189	}
190#ifdef Py_USING_UNICODE
191	else if (PyUnicode_Check(v)) {
192	        PyObject *utf8;
193		utf8 = PyUnicode_AsUTF8String(v);
194		if (utf8 == NULL) {
195			p->depth--;
196			p->error = 1;
197			return;
198		}
199		w_byte(TYPE_UNICODE, p);
200		n = PyString_GET_SIZE(utf8);
201		w_long((long)n, p);
202		w_string(PyString_AS_STRING(utf8), n, p);
203		Py_DECREF(utf8);
204	}
205#endif
206	else if (PyTuple_Check(v)) {
207		w_byte(TYPE_TUPLE, p);
208		n = PyTuple_Size(v);
209		w_long((long)n, p);
210		for (i = 0; i < n; i++) {
211			w_object(PyTuple_GET_ITEM(v, i), p);
212		}
213	}
214	else if (PyList_Check(v)) {
215		w_byte(TYPE_LIST, p);
216		n = PyList_GET_SIZE(v);
217		w_long((long)n, p);
218		for (i = 0; i < n; i++) {
219			w_object(PyList_GET_ITEM(v, i), p);
220		}
221	}
222	else if (PyDict_Check(v)) {
223		int pos;
224		PyObject *key, *value;
225		w_byte(TYPE_DICT, p);
226		/* This one is NULL object terminated! */
227		pos = 0;
228		while (PyDict_Next(v, &pos, &key, &value)) {
229			w_object(key, p);
230			w_object(value, p);
231		}
232		w_object((PyObject *)NULL, p);
233	}
234	else if (PyCode_Check(v)) {
235		PyCodeObject *co = (PyCodeObject *)v;
236		w_byte(TYPE_CODE, p);
237		w_short(co->co_argcount, p);
238		w_short(co->co_nlocals, p);
239		w_short(co->co_stacksize, p);
240		w_short(co->co_flags, p);
241		w_object(co->co_code, p);
242		w_object(co->co_consts, p);
243		w_object(co->co_names, p);
244		w_object(co->co_varnames, p);
245		w_object(co->co_freevars, p);
246		w_object(co->co_cellvars, p);
247		w_object(co->co_filename, p);
248		w_object(co->co_name, p);
249		w_short(co->co_firstlineno, p);
250		w_object(co->co_lnotab, p);
251	}
252	else if ((pb = v->ob_type->tp_as_buffer) != NULL &&
253		 pb->bf_getsegcount != NULL &&
254		 pb->bf_getreadbuffer != NULL &&
255		 (*pb->bf_getsegcount)(v, NULL) == 1)
256	{
257		/* Write unknown buffer-style objects as a string */
258		char *s;
259		w_byte(TYPE_STRING, p);
260		n = (*pb->bf_getreadbuffer)(v, 0, (void **)&s);
261		w_long((long)n, p);
262		w_string(s, n, p);
263	}
264	else {
265		w_byte(TYPE_UNKNOWN, p);
266		p->error = 1;
267	}
268
269	p->depth--;
270}
271
272void
273PyMarshal_WriteLongToFile(long x, FILE *fp)
274{
275	WFILE wf;
276	wf.fp = fp;
277	wf.error = 0;
278	wf.depth = 0;
279	w_long(x, &wf);
280}
281
282void
283PyMarshal_WriteObjectToFile(PyObject *x, FILE *fp)
284{
285	WFILE wf;
286	wf.fp = fp;
287	wf.error = 0;
288	wf.depth = 0;
289	w_object(x, &wf);
290}
291
292typedef WFILE RFILE; /* Same struct with different invariants */
293
294#define rs_byte(p) (((p)->ptr != (p)->end) ? (unsigned char)*(p)->ptr++ : EOF)
295
296#define r_byte(p) ((p)->fp ? getc((p)->fp) : rs_byte(p))
297
298static int
299r_string(char *s, int n, RFILE *p)
300{
301	if (p->fp != NULL)
302		return fread(s, 1, n, p->fp);
303	if (p->end - p->ptr < n)
304		n = p->end - p->ptr;
305	memcpy(s, p->ptr, n);
306	p->ptr += n;
307	return n;
308}
309
310static int
311r_short(RFILE *p)
312{
313	register short x;
314	x = r_byte(p);
315	x |= r_byte(p) << 8;
316	/* Sign-extension, in case short greater than 16 bits */
317	x |= -(x & 0x8000);
318	return x;
319}
320
321static long
322r_long(RFILE *p)
323{
324	register long x;
325	register FILE *fp = p->fp;
326	if (fp) {
327		x = getc(fp);
328		x |= (long)getc(fp) << 8;
329		x |= (long)getc(fp) << 16;
330		x |= (long)getc(fp) << 24;
331	}
332	else {
333		x = rs_byte(p);
334		x |= (long)rs_byte(p) << 8;
335		x |= (long)rs_byte(p) << 16;
336		x |= (long)rs_byte(p) << 24;
337	}
338#if SIZEOF_LONG > 4
339	/* Sign extension for 64-bit machines */
340	x |= -(x & 0x80000000L);
341#endif
342	return x;
343}
344
345/* r_long64 deals with the TYPE_INT64 code.  On a machine with
346   sizeof(long) > 4, it returns a Python int object, else a Python long
347   object.  Note that w_long64 writes out TYPE_INT if 32 bits is enough,
348   so there's no inefficiency here in returning a PyLong on 32-bit boxes
349   for everything written via TYPE_INT64 (i.e., if an int is written via
350   TYPE_INT64, it *needs* more than 32 bits).
351*/
352static PyObject *
353r_long64(RFILE *p)
354{
355	long lo4 = r_long(p);
356	long hi4 = r_long(p);
357#if SIZEOF_LONG > 4
358	long x = (hi4 << 32) | (lo4 & 0xFFFFFFFFL);
359	return PyInt_FromLong(x);
360#else
361	unsigned char buf[8];
362	int one = 1;
363	int is_little_endian = (int)*(char*)&one;
364	if (is_little_endian) {
365		memcpy(buf, &lo4, 4);
366		memcpy(buf+4, &hi4, 4);
367	}
368	else {
369		memcpy(buf, &hi4, 4);
370		memcpy(buf+4, &lo4, 4);
371	}
372	return _PyLong_FromByteArray(buf, 8, is_little_endian, 1);
373#endif
374}
375
376static PyObject *
377r_object(RFILE *p)
378{
379	PyObject *v, *v2;
380	long i, n;
381	int type = r_byte(p);
382
383	switch (type) {
384
385	case EOF:
386		PyErr_SetString(PyExc_EOFError,
387				"EOF read where object expected");
388		return NULL;
389
390	case TYPE_NULL:
391		return NULL;
392
393	case TYPE_NONE:
394		Py_INCREF(Py_None);
395		return Py_None;
396
397	case TYPE_STOPITER:
398		Py_INCREF(PyExc_StopIteration);
399		return PyExc_StopIteration;
400
401	case TYPE_ELLIPSIS:
402		Py_INCREF(Py_Ellipsis);
403		return Py_Ellipsis;
404
405	case TYPE_INT:
406		return PyInt_FromLong(r_long(p));
407
408	case TYPE_INT64:
409		return r_long64(p);
410
411	case TYPE_LONG:
412		{
413			int size;
414			PyLongObject *ob;
415			n = r_long(p);
416			size = n<0 ? -n : n;
417			ob = _PyLong_New(size);
418			if (ob == NULL)
419				return NULL;
420			ob->ob_size = n;
421			for (i = 0; i < size; i++)
422				ob->ob_digit[i] = r_short(p);
423			return (PyObject *)ob;
424		}
425
426	case TYPE_FLOAT:
427		{
428			char buf[256];
429			double dx;
430			n = r_byte(p);
431			if (r_string(buf, (int)n, p) != n) {
432				PyErr_SetString(PyExc_EOFError,
433					"EOF read where object expected");
434				return NULL;
435			}
436			buf[n] = '\0';
437			PyFPE_START_PROTECT("atof", return 0)
438			dx = atof(buf);
439			PyFPE_END_PROTECT(dx)
440			return PyFloat_FromDouble(dx);
441		}
442
443#ifndef WITHOUT_COMPLEX
444	case TYPE_COMPLEX:
445		{
446			char buf[256];
447			Py_complex c;
448			n = r_byte(p);
449			if (r_string(buf, (int)n, p) != n) {
450				PyErr_SetString(PyExc_EOFError,
451					"EOF read where object expected");
452				return NULL;
453			}
454			buf[n] = '\0';
455			PyFPE_START_PROTECT("atof", return 0)
456			c.real = atof(buf);
457			PyFPE_END_PROTECT(c)
458			n = r_byte(p);
459			if (r_string(buf, (int)n, p) != n) {
460				PyErr_SetString(PyExc_EOFError,
461					"EOF read where object expected");
462				return NULL;
463			}
464			buf[n] = '\0';
465			PyFPE_START_PROTECT("atof", return 0)
466			c.imag = atof(buf);
467			PyFPE_END_PROTECT(c)
468			return PyComplex_FromCComplex(c);
469		}
470#endif
471
472	case TYPE_STRING:
473		n = r_long(p);
474		if (n < 0) {
475			PyErr_SetString(PyExc_ValueError, "bad marshal data");
476			return NULL;
477		}
478		v = PyString_FromStringAndSize((char *)NULL, n);
479		if (v != NULL) {
480			if (r_string(PyString_AS_STRING(v), (int)n, p) != n) {
481				Py_DECREF(v);
482				v = NULL;
483				PyErr_SetString(PyExc_EOFError,
484					"EOF read where object expected");
485			}
486		}
487		return v;
488
489#ifdef Py_USING_UNICODE
490	case TYPE_UNICODE:
491	    {
492		char *buffer;
493
494		n = r_long(p);
495		if (n < 0) {
496			PyErr_SetString(PyExc_ValueError, "bad marshal data");
497			return NULL;
498		}
499		buffer = PyMem_NEW(char, n);
500		if (buffer == NULL)
501			return PyErr_NoMemory();
502		if (r_string(buffer, (int)n, p) != n) {
503			PyMem_DEL(buffer);
504			PyErr_SetString(PyExc_EOFError,
505				"EOF read where object expected");
506			return NULL;
507		}
508		v = PyUnicode_DecodeUTF8(buffer, n, NULL);
509		PyMem_DEL(buffer);
510		return v;
511	    }
512#endif
513
514	case TYPE_TUPLE:
515		n = r_long(p);
516		if (n < 0) {
517			PyErr_SetString(PyExc_ValueError, "bad marshal data");
518			return NULL;
519		}
520		v = PyTuple_New((int)n);
521		if (v == NULL)
522			return v;
523		for (i = 0; i < n; i++) {
524			v2 = r_object(p);
525			if ( v2 == NULL ) {
526				Py_DECREF(v);
527				v = NULL;
528				break;
529			}
530			PyTuple_SET_ITEM(v, (int)i, v2);
531		}
532		return v;
533
534	case TYPE_LIST:
535		n = r_long(p);
536		if (n < 0) {
537			PyErr_SetString(PyExc_ValueError, "bad marshal data");
538			return NULL;
539		}
540		v = PyList_New((int)n);
541		if (v == NULL)
542			return v;
543		for (i = 0; i < n; i++) {
544			v2 = r_object(p);
545			if ( v2 == NULL ) {
546				Py_DECREF(v);
547				v = NULL;
548				break;
549			}
550			PyList_SetItem(v, (int)i, v2);
551		}
552		return v;
553
554	case TYPE_DICT:
555		v = PyDict_New();
556		if (v == NULL)
557			return NULL;
558		for (;;) {
559			PyObject *key, *val;
560			key = r_object(p);
561			if (key == NULL)
562				break; /* XXX Assume TYPE_NULL, not an error */
563			val = r_object(p);
564			if (val != NULL)
565				PyDict_SetItem(v, key, val);
566			Py_DECREF(key);
567			Py_XDECREF(val);
568		}
569		return v;
570
571	case TYPE_CODE:
572		{
573			int argcount = r_short(p);
574			int nlocals = r_short(p);
575			int stacksize = r_short(p);
576			int flags = r_short(p);
577			PyObject *code = NULL;
578			PyObject *consts = NULL;
579			PyObject *names = NULL;
580			PyObject *varnames = NULL;
581			PyObject *freevars = NULL;
582			PyObject *cellvars = NULL;
583			PyObject *filename = NULL;
584			PyObject *name = NULL;
585			int firstlineno = 0;
586			PyObject *lnotab = NULL;
587
588			code = r_object(p);
589			if (code) consts = r_object(p);
590			if (consts) names = r_object(p);
591			if (names) varnames = r_object(p);
592			if (varnames) freevars = r_object(p);
593			if (freevars) cellvars = r_object(p);
594			if (cellvars) filename = r_object(p);
595			if (filename) name = r_object(p);
596			if (name) {
597				firstlineno = r_short(p);
598				lnotab = r_object(p);
599			}
600
601			if (!PyErr_Occurred()) {
602				v = (PyObject *) PyCode_New(
603					argcount, nlocals, stacksize, flags,
604					code, consts, names, varnames,
605					freevars, cellvars, filename, name,
606					firstlineno, lnotab);
607			}
608			else
609				v = NULL;
610			Py_XDECREF(code);
611			Py_XDECREF(consts);
612			Py_XDECREF(names);
613			Py_XDECREF(varnames);
614			Py_XDECREF(freevars);
615			Py_XDECREF(cellvars);
616			Py_XDECREF(filename);
617			Py_XDECREF(name);
618			Py_XDECREF(lnotab);
619
620		}
621		return v;
622
623	default:
624		/* Bogus data got written, which isn't ideal.
625		   This will let you keep working and recover. */
626		PyErr_SetString(PyExc_ValueError, "bad marshal data");
627		return NULL;
628
629	}
630}
631
632long
633PyMarshal_ReadLongFromFile(FILE *fp)
634{
635	RFILE rf;
636	rf.fp = fp;
637	return r_long(&rf);
638}
639
640#ifdef HAVE_FSTAT
641/* Return size of file in bytes; < 0 if unknown. */
642static off_t
643getfilesize(FILE *fp)
644{
645	struct stat st;
646	if (fstat(fileno(fp), &st) != 0)
647		return -1;
648	else
649		return st.st_size;
650}
651#endif
652
653/* If we can get the size of the file up-front, and it's reasonably small,
654 * read it in one gulp and delegate to ...FromString() instead.  Much quicker
655 * than reading a byte at a time from file; speeds .pyc imports.
656 * CAUTION:  since this may read the entire remainder of the file, don't
657 * call it unless you know you're done with the file.
658 */
659PyObject *
660PyMarshal_ReadLastObjectFromFile(FILE *fp)
661{
662/* 75% of 2.1's .pyc files can exploit SMALL_FILE_LIMIT.
663 * REASONABLE_FILE_LIMIT is by defn something big enough for Tkinter.pyc.
664 */
665#define SMALL_FILE_LIMIT (1L << 14)
666#define REASONABLE_FILE_LIMIT (1L << 18)
667#ifdef HAVE_FSTAT
668	off_t filesize;
669#endif
670	if (PyErr_Occurred()) {
671		fprintf(stderr, "XXX rd_object called with exception set\n");
672		return NULL;
673	}
674#ifdef HAVE_FSTAT
675	filesize = getfilesize(fp);
676	if (filesize > 0) {
677		char buf[SMALL_FILE_LIMIT];
678		char* pBuf = NULL;
679		if (filesize <= SMALL_FILE_LIMIT)
680			pBuf = buf;
681		else if (filesize <= REASONABLE_FILE_LIMIT)
682			pBuf = (char *)PyMem_MALLOC(filesize);
683		if (pBuf != NULL) {
684			PyObject* v;
685			size_t n = fread(pBuf, 1, filesize, fp);
686			v = PyMarshal_ReadObjectFromString(pBuf, n);
687			if (pBuf != buf)
688				PyMem_FREE(pBuf);
689			return v;
690		}
691
692	}
693#endif
694	/* We don't have fstat, or we do but the file is larger than
695	 * REASONABLE_FILE_LIMIT or malloc failed -- read a byte at a time.
696	 */
697	return PyMarshal_ReadObjectFromFile(fp);
698
699#undef SMALL_FILE_LIMIT
700#undef REASONABLE_FILE_LIMIT
701}
702
703PyObject *
704PyMarshal_ReadObjectFromFile(FILE *fp)
705{
706	RFILE rf;
707	if (PyErr_Occurred()) {
708		fprintf(stderr, "XXX rd_object called with exception set\n");
709		return NULL;
710	}
711	rf.fp = fp;
712	return r_object(&rf);
713}
714
715PyObject *
716PyMarshal_ReadObjectFromString(char *str, int len)
717{
718	RFILE rf;
719	if (PyErr_Occurred()) {
720		fprintf(stderr, "XXX rds_object called with exception set\n");
721		return NULL;
722	}
723	rf.fp = NULL;
724	rf.str = NULL;
725	rf.ptr = str;
726	rf.end = str + len;
727	return r_object(&rf);
728}
729
730PyObject *
731PyMarshal_WriteObjectToString(PyObject *x) /* wrs_object() */
732{
733	WFILE wf;
734	wf.fp = NULL;
735	wf.str = PyString_FromStringAndSize((char *)NULL, 50);
736	if (wf.str == NULL)
737		return NULL;
738	wf.ptr = PyString_AS_STRING((PyStringObject *)wf.str);
739	wf.end = wf.ptr + PyString_Size(wf.str);
740	wf.error = 0;
741	wf.depth = 0;
742	w_object(x, &wf);
743	if (wf.str != NULL)
744		_PyString_Resize(&wf.str,
745		    (int) (wf.ptr -
746			   PyString_AS_STRING((PyStringObject *)wf.str)));
747	if (wf.error) {
748		Py_XDECREF(wf.str);
749		PyErr_SetString(PyExc_ValueError,
750				(wf.error==1)?"unmarshallable object"
751				:"object too deeply nested to marshal");
752		return NULL;
753	}
754	return wf.str;
755}
756
757/* And an interface for Python programs... */
758
759static PyObject *
760marshal_dump(PyObject *self, PyObject *args)
761{
762	WFILE wf;
763	PyObject *x;
764	PyObject *f;
765	if (!PyArg_ParseTuple(args, "OO:dump", &x, &f))
766		return NULL;
767	if (!PyFile_Check(f)) {
768		PyErr_SetString(PyExc_TypeError,
769				"marshal.dump() 2nd arg must be file");
770		return NULL;
771	}
772	wf.fp = PyFile_AsFile(f);
773	wf.str = NULL;
774	wf.ptr = wf.end = NULL;
775	wf.error = 0;
776	wf.depth = 0;
777	w_object(x, &wf);
778	if (wf.error) {
779		PyErr_SetString(PyExc_ValueError,
780				(wf.error==1)?"unmarshallable object"
781				:"object too deeply nested to marshal");
782		return NULL;
783	}
784	Py_INCREF(Py_None);
785	return Py_None;
786}
787
788static PyObject *
789marshal_load(PyObject *self, PyObject *args)
790{
791	RFILE rf;
792	PyObject *f;
793	PyObject *v;
794	if (!PyArg_ParseTuple(args, "O:load", &f))
795		return NULL;
796	if (!PyFile_Check(f)) {
797		PyErr_SetString(PyExc_TypeError,
798				"marshal.load() arg must be file");
799		return NULL;
800	}
801	rf.fp = PyFile_AsFile(f);
802	rf.str = NULL;
803	rf.ptr = rf.end = NULL;
804	PyErr_Clear();
805	v = r_object(&rf);
806	if (PyErr_Occurred()) {
807		Py_XDECREF(v);
808		v = NULL;
809	}
810	return v;
811}
812
813static PyObject *
814marshal_dumps(PyObject *self, PyObject *args)
815{
816	PyObject *x;
817	if (!PyArg_ParseTuple(args, "O:dumps", &x))
818		return NULL;
819	return PyMarshal_WriteObjectToString(x);
820}
821
822static PyObject *
823marshal_loads(PyObject *self, PyObject *args)
824{
825	RFILE rf;
826	PyObject *v;
827	char *s;
828	int n;
829	if (!PyArg_ParseTuple(args, "s#:loads", &s, &n))
830		return NULL;
831	rf.fp = NULL;
832	rf.str = args;
833	rf.ptr = s;
834	rf.end = s + n;
835	PyErr_Clear();
836	v = r_object(&rf);
837	if (PyErr_Occurred()) {
838		Py_XDECREF(v);
839		v = NULL;
840	}
841	return v;
842}
843
844static PyMethodDef marshal_methods[] = {
845	{"dump",	marshal_dump,	1},
846	{"load",	marshal_load,	1},
847	{"dumps",	marshal_dumps,	1},
848	{"loads",	marshal_loads,	1},
849	{NULL,		NULL}		/* sentinel */
850};
851
852void
853PyMarshal_Init(void)
854{
855	(void) Py_InitModule("marshal", marshal_methods);
856}
857