marshal.c 17.9 KB
Newer Older
Guido van Rossum's avatar
Guido van Rossum committed
1 2 3 4 5 6

/* Write Python objects to files and read them back.
   This is intended for writing and reading compiled Python code only;
   a true persistent storage facility would be much harder, since
   it would have to take circular links and sharing into account. */

7
#include "Python.h"
Guido van Rossum's avatar
Guido van Rossum committed
8 9 10 11
#include "longintrepr.h"
#include "compile.h"
#include "marshal.h"

12 13 14 15 16 17
/* High water mark to determine when the marshalled object is dangerously deep
 * and risks coring the interpreter.  When the object stack gets this deep,
 * raise an exception instead of continuing.
 */
#define MAX_MARSHAL_STACK_DEPTH 5000

Guido van Rossum's avatar
Guido van Rossum committed
18 19
#define TYPE_NULL	'0'
#define TYPE_NONE	'N'
20
#define TYPE_STOPITER	'S'
21
#define TYPE_ELLIPSIS   '.'
Guido van Rossum's avatar
Guido van Rossum committed
22
#define TYPE_INT	'i'
23
#define TYPE_INT64	'I'
Guido van Rossum's avatar
Guido van Rossum committed
24
#define TYPE_FLOAT	'f'
25
#define TYPE_COMPLEX	'x'
Guido van Rossum's avatar
Guido van Rossum committed
26 27 28 29 30
#define TYPE_LONG	'l'
#define TYPE_STRING	's'
#define TYPE_TUPLE	'('
#define TYPE_LIST	'['
#define TYPE_DICT	'{'
31
#define TYPE_CODE	'c'
32
#define TYPE_UNICODE	'u'
Guido van Rossum's avatar
Guido van Rossum committed
33 34
#define TYPE_UNKNOWN	'?'

35 36
typedef struct {
	FILE *fp;
37
	int error;
38
	int depth;
39
	/* If fp == NULL, the following are valid: */
40
	PyObject *str;
41 42 43
	char *ptr;
	char *end;
} WFILE;
Guido van Rossum's avatar
Guido van Rossum committed
44

45 46 47 48 49
#define w_byte(c, p) if (((p)->fp)) putc((c), (p)->fp); \
		      else if ((p)->ptr != (p)->end) *(p)->ptr++ = (c); \
			   else w_more(c, p)

static void
50
w_more(int c, WFILE *p)
51 52 53 54
{
	int size, newsize;
	if (p->str == NULL)
		return; /* An error already occurred */
55
	size = PyString_Size(p->str);
56
	newsize = size + 1024;
57
	if (_PyString_Resize(&p->str, newsize) != 0) {
58 59 60
		p->ptr = p->end = NULL;
	}
	else {
61 62 63
		p->ptr = PyString_AS_STRING((PyStringObject *)p->str) + size;
		p->end =
			PyString_AS_STRING((PyStringObject *)p->str) + newsize;
64
		*p->ptr++ = Py_SAFE_DOWNCAST(c, int, char);
65 66 67 68
	}
}

static void
69
w_string(char *s, int n, WFILE *p)
70 71 72 73 74 75 76 77 78 79 80 81 82
{
	if (p->fp != NULL) {
		fwrite(s, 1, n, p->fp);
	}
	else {
		while (--n >= 0) {
			w_byte(*s, p);
			s++;
		}
	}
}

static void
83
w_short(int x, WFILE *p)
Guido van Rossum's avatar
Guido van Rossum committed
84
{
85 86
	w_byte( x      & 0xff, p);
	w_byte((x>> 8) & 0xff, p);
Guido van Rossum's avatar
Guido van Rossum committed
87 88
}

89
static void
90
w_long(long x, WFILE *p)
Guido van Rossum's avatar
Guido van Rossum committed
91
{
92 93 94 95
	w_byte((int)( x      & 0xff), p);
	w_byte((int)((x>> 8) & 0xff), p);
	w_byte((int)((x>>16) & 0xff), p);
	w_byte((int)((x>>24) & 0xff), p);
Guido van Rossum's avatar
Guido van Rossum committed
96 97
}

98
#if SIZEOF_LONG > 4
99
static void
100
w_long64(long x, WFILE *p)
101 102
{
	w_long(x, p);
103
	w_long(x>>32, p);
104
}
105
#endif
106

107
static void
108
w_object(PyObject *v, WFILE *p)
Guido van Rossum's avatar
Guido van Rossum committed
109
{
110
	int i, n;
111
	PyBufferProcs *pb;
112 113

	p->depth++;
114

115 116
	if (p->depth > MAX_MARSHAL_STACK_DEPTH) {
		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 125 126
	else if (v == PyExc_StopIteration) {
		w_byte(TYPE_STOPITER, p);
	}
127 128 129
	else if (v == Py_Ellipsis) {
	        w_byte(TYPE_ELLIPSIS, p);
	}
130 131
	else if (PyInt_Check(v)) {
		long x = PyInt_AS_LONG((PyIntObject *)v);
132
#if SIZEOF_LONG > 4
133
		long y = Py_ARITHMETIC_RIGHT_SHIFT(long, x, 31);
134 135 136 137
		if (y && y != -1) {
			w_byte(TYPE_INT64, p);
			w_long64(x, p);
		}
138 139 140
		else
#endif
			{
141 142 143
			w_byte(TYPE_INT, p);
			w_long(x, p);
		}
Guido van Rossum's avatar
Guido van Rossum committed
144
	}
145 146
	else if (PyLong_Check(v)) {
		PyLongObject *ob = (PyLongObject *)v;
147
		w_byte(TYPE_LONG, p);
Guido van Rossum's avatar
Guido van Rossum committed
148
		n = ob->ob_size;
149
		w_long((long)n, p);
Guido van Rossum's avatar
Guido van Rossum committed
150 151 152
		if (n < 0)
			n = -n;
		for (i = 0; i < n; i++)
153
			w_short(ob->ob_digit[i], p);
Guido van Rossum's avatar
Guido van Rossum committed
154
	}
155
	else if (PyFloat_Check(v)) {
Guido van Rossum's avatar
Guido van Rossum committed
156
		char buf[256]; /* Plenty to format any double */
157
		PyFloat_AsReprString(buf, (PyFloatObject *)v);
Guido van Rossum's avatar
Guido van Rossum committed
158
		n = strlen(buf);
159
		w_byte(TYPE_FLOAT, p);
160 161
		w_byte(n, p);
		w_string(buf, n, p);
Guido van Rossum's avatar
Guido van Rossum committed
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 169
		temp = (PyFloatObject*)PyFloat_FromDouble(
			PyComplex_RealAsDouble(v));
170
		PyFloat_AsReprString(buf, temp);
171
		Py_DECREF(temp);
172 173 174
		n = strlen(buf);
		w_byte(n, p);
		w_string(buf, n, p);
175 176
		temp = (PyFloatObject*)PyFloat_FromDouble(
			PyComplex_ImagAsDouble(v));
177
		PyFloat_AsReprString(buf, temp);
178
		Py_DECREF(temp);
179 180 181 182 183
		n = strlen(buf);
		w_byte(n, p);
		w_string(buf, n, p);
	}
#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 189
		w_string(PyString_AS_STRING(v), n, p);
	}
190
#ifdef Py_USING_UNICODE
191 192 193 194
	else if (PyUnicode_Check(v)) {
	        PyObject *utf8;
		utf8 = PyUnicode_AsUTF8String(v);
		if (utf8 == NULL) {
195 196 197
			p->depth--;
			p->error = 1;
			return;
198 199 200 201 202 203
		}
		w_byte(TYPE_UNICODE, p);
		n = PyString_GET_SIZE(utf8);
		w_long((long)n, p);
		w_string(PyString_AS_STRING(utf8), n, p);
		Py_DECREF(utf8);
Guido van Rossum's avatar
Guido van Rossum committed
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);
Guido van Rossum's avatar
Guido van Rossum committed
210
		for (i = 0; i < n; i++) {
211
			w_object(PyTuple_GET_ITEM(v, i), p);
Guido van Rossum's avatar
Guido van Rossum committed
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);
Guido van Rossum's avatar
Guido van Rossum committed
218
		for (i = 0; i < n; i++) {
219
			w_object(PyList_GET_ITEM(v, i), p);
Guido van Rossum's avatar
Guido van Rossum committed
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 230
			w_object(key, p);
			w_object(value, p);
231
		}
232
		w_object((PyObject *)NULL, p);
233
	}
234 235
	else if (PyCode_Check(v)) {
		PyCodeObject *co = (PyCodeObject *)v;
236
		w_byte(TYPE_CODE, p);
237 238
		w_short(co->co_argcount, p);
		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 243
		w_object(co->co_consts, p);
		w_object(co->co_names, p);
244
		w_object(co->co_varnames, p);
Jeremy Hylton's avatar
Jeremy Hylton committed
245 246
		w_object(co->co_freevars, p);
		w_object(co->co_cellvars, p);
247
		w_object(co->co_filename, p);
248
		w_object(co->co_name, p);
249 250
		w_short(co->co_firstlineno, p);
		w_object(co->co_lnotab, p);
Guido van Rossum's avatar
Guido van Rossum committed
251
	}
252 253 254 255 256 257 258 259 260 261 262 263
	else if ((pb = v->ob_type->tp_as_buffer) != NULL &&
		 pb->bf_getsegcount != NULL &&
		 pb->bf_getreadbuffer != NULL &&
		 (*pb->bf_getsegcount)(v, NULL) == 1)
	{
		/* Write unknown buffer-style objects as a string */
		char *s;
		w_byte(TYPE_STRING, p);
		n = (*pb->bf_getreadbuffer)(v, 0, (void **)&s);
		w_long((long)n, p);
		w_string(s, n, p);
	}
Guido van Rossum's avatar
Guido van Rossum committed
264
	else {
265
		w_byte(TYPE_UNKNOWN, p);
266
		p->error = 1;
Guido van Rossum's avatar
Guido van Rossum committed
267
	}
268 269

	p->depth--;
Guido van Rossum's avatar
Guido van Rossum committed
270 271
}

272
void
273
PyMarshal_WriteLongToFile(long x, FILE *fp)
274 275 276
{
	WFILE wf;
	wf.fp = fp;
277
	wf.error = 0;
278
	wf.depth = 0;
279 280
	w_long(x, &wf);
}
Guido van Rossum's avatar
Guido van Rossum committed
281

282
void
283
PyMarshal_WriteObjectToFile(PyObject *x, FILE *fp)
284 285 286
{
	WFILE wf;
	wf.fp = fp;
287
	wf.error = 0;
288
	wf.depth = 0;
289 290 291 292 293
	w_object(x, &wf);
}

typedef WFILE RFILE; /* Same struct with different invariants */

294 295 296
#define rs_byte(p) (((p)->ptr != (p)->end) ? (unsigned char)*(p)->ptr++ : EOF)

#define r_byte(p) ((p)->fp ? getc((p)->fp) : rs_byte(p))
297 298

static int
299
r_string(char *s, int n, RFILE *p)
300 301 302 303 304 305 306 307 308 309 310
{
	if (p->fp != NULL)
		return fread(s, 1, n, p->fp);
	if (p->end - p->ptr < n)
		n = p->end - p->ptr;
	memcpy(s, p->ptr, n);
	p->ptr += n;
	return n;
}

static int
311
r_short(RFILE *p)
Guido van Rossum's avatar
Guido van Rossum committed
312 313
{
	register short x;
314 315
	x = r_byte(p);
	x |= r_byte(p) << 8;
Tim Peters's avatar
Tim Peters committed
316 317
	/* Sign-extension, in case short greater than 16 bits */
	x |= -(x & 0x8000);
Guido van Rossum's avatar
Guido van Rossum committed
318 319 320
	return x;
}

321
static long
322
r_long(RFILE *p)
Guido van Rossum's avatar
Guido van Rossum committed
323 324
{
	register long x;
325 326 327 328 329 330 331 332 333 334 335 336 337
	register FILE *fp = p->fp;
	if (fp) {
		x = getc(fp);
		x |= (long)getc(fp) << 8;
		x |= (long)getc(fp) << 16;
		x |= (long)getc(fp) << 24;
	}
	else {
		x = rs_byte(p);
		x |= (long)rs_byte(p) << 8;
		x |= (long)rs_byte(p) << 16;
		x |= (long)rs_byte(p) << 24;
	}
338
#if SIZEOF_LONG > 4
339
	/* Sign extension for 64-bit machines */
Tim Peters's avatar
Tim Peters committed
340
	x |= -(x & 0x80000000L);
341
#endif
342 343 344
	return x;
}

345 346 347 348 349 350 351 352
/* r_long64 deals with the TYPE_INT64 code.  On a machine with
   sizeof(long) > 4, it returns a Python int object, else a Python long
   object.  Note that w_long64 writes out TYPE_INT if 32 bits is enough,
   so there's no inefficiency here in returning a PyLong on 32-bit boxes
   for everything written via TYPE_INT64 (i.e., if an int is written via
   TYPE_INT64, it *needs* more than 32 bits).
*/
static PyObject *
353
r_long64(RFILE *p)
354
{
355 356
	long lo4 = r_long(p);
	long hi4 = r_long(p);
357
#if SIZEOF_LONG > 4
358 359
	long x = (hi4 << 32) | (lo4 & 0xFFFFFFFFL);
	return PyInt_FromLong(x);
360
#else
361 362 363 364 365 366
	unsigned char buf[8];
	int one = 1;
	int is_little_endian = (int)*(char*)&one;
	if (is_little_endian) {
		memcpy(buf, &lo4, 4);
		memcpy(buf+4, &hi4, 4);
367
	}
368 369 370 371 372
	else {
		memcpy(buf, &hi4, 4);
		memcpy(buf+4, &lo4, 4);
	}
	return _PyLong_FromByteArray(buf, 8, is_little_endian, 1);
373
#endif
Guido van Rossum's avatar
Guido van Rossum committed
374 375
}

376
static PyObject *
377
r_object(RFILE *p)
Guido van Rossum's avatar
Guido van Rossum committed
378
{
379
	PyObject *v, *v2;
Guido van Rossum's avatar
Guido van Rossum committed
380
	long i, n;
381
	int type = r_byte(p);
382

Guido van Rossum's avatar
Guido van Rossum committed
383
	switch (type) {
384

Guido van Rossum's avatar
Guido van Rossum committed
385
	case EOF:
386 387
		PyErr_SetString(PyExc_EOFError,
				"EOF read where object expected");
Guido van Rossum's avatar
Guido van Rossum committed
388
		return NULL;
389

Guido van Rossum's avatar
Guido van Rossum committed
390 391
	case TYPE_NULL:
		return NULL;
392

Guido van Rossum's avatar
Guido van Rossum committed
393
	case TYPE_NONE:
394 395
		Py_INCREF(Py_None);
		return Py_None;
396

397 398 399 400
	case TYPE_STOPITER:
		Py_INCREF(PyExc_StopIteration);
		return PyExc_StopIteration;

401
	case TYPE_ELLIPSIS:
402
		Py_INCREF(Py_Ellipsis);
403
		return Py_Ellipsis;
404

Guido van Rossum's avatar
Guido van Rossum committed
405
	case TYPE_INT:
406
		return PyInt_FromLong(r_long(p));
407

408
	case TYPE_INT64:
409
		return r_long64(p);
410

Guido van Rossum's avatar
Guido van Rossum committed
411 412 413
	case TYPE_LONG:
		{
			int size;
414
			PyLongObject *ob;
415
			n = r_long(p);
Guido van Rossum's avatar
Guido van Rossum committed
416
			size = n<0 ? -n : n;
417
			ob = _PyLong_New(size);
Guido van Rossum's avatar
Guido van Rossum committed
418 419 420 421
			if (ob == NULL)
				return NULL;
			ob->ob_size = n;
			for (i = 0; i < size; i++)
422
				ob->ob_digit[i] = r_short(p);
423
			return (PyObject *)ob;
Guido van Rossum's avatar
Guido van Rossum committed
424
		}
425

Guido van Rossum's avatar
Guido van Rossum committed
426 427 428
	case TYPE_FLOAT:
		{
			char buf[256];
429
			double dx;
430 431
			n = r_byte(p);
			if (r_string(buf, (int)n, p) != n) {
432
				PyErr_SetString(PyExc_EOFError,
Guido van Rossum's avatar
Guido van Rossum committed
433 434 435 436
					"EOF read where object expected");
				return NULL;
			}
			buf[n] = '\0';
437 438
			PyFPE_START_PROTECT("atof", return 0)
			dx = atof(buf);
439
			PyFPE_END_PROTECT(dx)
440
			return PyFloat_FromDouble(dx);
Guido van Rossum's avatar
Guido van Rossum committed
441
		}
442

443 444 445 446
#ifndef WITHOUT_COMPLEX
	case TYPE_COMPLEX:
		{
			char buf[256];
447
			Py_complex c;
448 449
			n = r_byte(p);
			if (r_string(buf, (int)n, p) != n) {
450
				PyErr_SetString(PyExc_EOFError,
451 452 453 454
					"EOF read where object expected");
				return NULL;
			}
			buf[n] = '\0';
455
			PyFPE_START_PROTECT("atof", return 0)
456
			c.real = atof(buf);
457
			PyFPE_END_PROTECT(c)
458 459
			n = r_byte(p);
			if (r_string(buf, (int)n, p) != n) {
460
				PyErr_SetString(PyExc_EOFError,
461 462 463 464
					"EOF read where object expected");
				return NULL;
			}
			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

Guido van Rossum's avatar
Guido van Rossum committed
472
	case TYPE_STRING:
473
		n = r_long(p);
474 475 476 477
		if (n < 0) {
			PyErr_SetString(PyExc_ValueError, "bad marshal data");
			return NULL;
		}
478
		v = PyString_FromStringAndSize((char *)NULL, n);
Guido van Rossum's avatar
Guido van Rossum committed
479
		if (v != NULL) {
480
			if (r_string(PyString_AS_STRING(v), (int)n, p) != n) {
481
				Py_DECREF(v);
Guido van Rossum's avatar
Guido van Rossum committed
482
				v = NULL;
483
				PyErr_SetString(PyExc_EOFError,
Guido van Rossum's avatar
Guido van Rossum committed
484 485 486 487
					"EOF read where object expected");
			}
		}
		return v;
488

489
#ifdef Py_USING_UNICODE
490 491 492 493 494 495 496 497 498
	case TYPE_UNICODE:
	    {
		char *buffer;

		n = r_long(p);
		if (n < 0) {
			PyErr_SetString(PyExc_ValueError, "bad marshal data");
			return NULL;
		}
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 505 506 507 508
			PyErr_SetString(PyExc_EOFError,
				"EOF read where object expected");
			return NULL;
		}
		v = PyUnicode_DecodeUTF8(buffer, n, NULL);
509
		PyMem_DEL(buffer);
510 511
		return v;
	    }
512
#endif
513

Guido van Rossum's avatar
Guido van Rossum committed
514
	case TYPE_TUPLE:
515
		n = r_long(p);
516 517 518 519
		if (n < 0) {
			PyErr_SetString(PyExc_ValueError, "bad marshal data");
			return NULL;
		}
520
		v = PyTuple_New((int)n);
Guido van Rossum's avatar
Guido van Rossum committed
521 522
		if (v == NULL)
			return v;
523 524 525
		for (i = 0; i < n; i++) {
			v2 = r_object(p);
			if ( v2 == NULL ) {
526
				Py_DECREF(v);
527 528 529
				v = NULL;
				break;
			}
530
			PyTuple_SET_ITEM(v, (int)i, v2);
531
		}
Guido van Rossum's avatar
Guido van Rossum committed
532
		return v;
533

Guido van Rossum's avatar
Guido van Rossum committed
534
	case TYPE_LIST:
535
		n = r_long(p);
536 537 538 539
		if (n < 0) {
			PyErr_SetString(PyExc_ValueError, "bad marshal data");
			return NULL;
		}
540
		v = PyList_New((int)n);
Guido van Rossum's avatar
Guido van Rossum committed
541 542
		if (v == NULL)
			return v;
543 544 545
		for (i = 0; i < n; i++) {
			v2 = r_object(p);
			if ( v2 == NULL ) {
546
				Py_DECREF(v);
547 548 549
				v = NULL;
				break;
			}
550
			PyList_SetItem(v, (int)i, v2);
551
		}
Guido van Rossum's avatar
Guido van Rossum committed
552
		return v;
553

554
	case TYPE_DICT:
555
		v = PyDict_New();
556 557 558
		if (v == NULL)
			return NULL;
		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 566 567
				PyDict_SetItem(v, key, val);
			Py_DECREF(key);
			Py_XDECREF(val);
568 569
		}
		return v;
570

Guido van Rossum's avatar
Guido van Rossum committed
571
	case TYPE_CODE:
572 573 574 575 576 577 578
		if (PyEval_GetRestricted()) {
			PyErr_SetString(PyExc_RuntimeError,
				"cannot unmarshal code objects in "
				"restricted execution mode");
			return NULL;
		}
		else {
579 580
			int argcount = r_short(p);
			int nlocals = r_short(p);
581
			int stacksize = r_short(p);
582
			int flags = r_short(p);
583 584 585 586
			PyObject *code = NULL;
			PyObject *consts = NULL;
			PyObject *names = NULL;
			PyObject *varnames = NULL;
Jeremy Hylton's avatar
Jeremy Hylton committed
587 588
			PyObject *freevars = NULL;
			PyObject *cellvars = NULL;
589 590
			PyObject *filename = NULL;
			PyObject *name = NULL;
Guido van Rossum's avatar
Guido van Rossum committed
591
			int firstlineno = 0;
592
			PyObject *lnotab = NULL;
593

594 595 596 597
			code = r_object(p);
			if (code) consts = r_object(p);
			if (consts) names = r_object(p);
			if (names) varnames = r_object(p);
Jeremy Hylton's avatar
Jeremy Hylton committed
598 599 600
			if (varnames) freevars = r_object(p);
			if (freevars) cellvars = r_object(p);
			if (cellvars) filename = r_object(p);
601
			if (filename) name = r_object(p);
602 603 604 605
			if (name) {
				firstlineno = r_short(p);
				lnotab = r_object(p);
			}
606

607 608
			if (!PyErr_Occurred()) {
				v = (PyObject *) PyCode_New(
609
					argcount, nlocals, stacksize, flags,
610
					code, consts, names, varnames,
611 612
					freevars, cellvars, filename, name,
					firstlineno, lnotab);
Guido van Rossum's avatar
Guido van Rossum committed
613 614 615
			}
			else
				v = NULL;
616 617 618 619
			Py_XDECREF(code);
			Py_XDECREF(consts);
			Py_XDECREF(names);
			Py_XDECREF(varnames);
Jeremy Hylton's avatar
Jeremy Hylton committed
620 621
			Py_XDECREF(freevars);
			Py_XDECREF(cellvars);
622 623
			Py_XDECREF(filename);
			Py_XDECREF(name);
624
			Py_XDECREF(lnotab);
Guido van Rossum's avatar
Guido van Rossum committed
625 626 627

		}
		return v;
628

Guido van Rossum's avatar
Guido van Rossum committed
629
	default:
630 631
		/* Bogus data got written, which isn't ideal.
		   This will let you keep working and recover. */
632 633
		PyErr_SetString(PyExc_ValueError, "bad marshal data");
		return NULL;
634

Guido van Rossum's avatar
Guido van Rossum committed
635 636 637
	}
}

638
long
639
PyMarshal_ReadLongFromFile(FILE *fp)
640 641 642 643 644 645
{
	RFILE rf;
	rf.fp = fp;
	return r_long(&rf);
}

646 647 648 649 650 651 652 653 654 655 656 657
#ifdef HAVE_FSTAT
/* Return size of file in bytes; < 0 if unknown. */
static off_t
getfilesize(FILE *fp)
{
	struct stat st;
	if (fstat(fileno(fp), &st) != 0)
		return -1;
	else
		return st.st_size;
}
#endif
658

659 660 661
/* If we can get the size of the file up-front, and it's reasonably small,
 * read it in one gulp and delegate to ...FromString() instead.  Much quicker
 * than reading a byte at a time from file; speeds .pyc imports.
662 663
 * CAUTION:  since this may read the entire remainder of the file, don't
 * call it unless you know you're done with the file.
664
 */
665
PyObject *
666
PyMarshal_ReadLastObjectFromFile(FILE *fp)
667
{
668 669 670 671 672 673 674 675
/* 75% of 2.1's .pyc files can exploit SMALL_FILE_LIMIT.
 * REASONABLE_FILE_LIMIT is by defn something big enough for Tkinter.pyc.
 */
#define SMALL_FILE_LIMIT (1L << 14)
#define REASONABLE_FILE_LIMIT (1L << 18)
#ifdef HAVE_FSTAT
	off_t filesize;
#endif
676
	if (PyErr_Occurred()) {
677 678 679
		fprintf(stderr, "XXX rd_object called with exception set\n");
		return NULL;
	}
680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696
#ifdef HAVE_FSTAT
	filesize = getfilesize(fp);
	if (filesize > 0) {
		char buf[SMALL_FILE_LIMIT];
		char* pBuf = NULL;
		if (filesize <= SMALL_FILE_LIMIT)
			pBuf = buf;
		else if (filesize <= REASONABLE_FILE_LIMIT)
			pBuf = (char *)PyMem_MALLOC(filesize);
		if (pBuf != NULL) {
			PyObject* v;
			size_t n = fread(pBuf, 1, filesize, fp);
			v = PyMarshal_ReadObjectFromString(pBuf, n);
			if (pBuf != buf)
				PyMem_FREE(pBuf);
			return v;
		}
697

698 699
	}
#endif
700 701 702 703 704
	/* We don't have fstat, or we do but the file is larger than
	 * REASONABLE_FILE_LIMIT or malloc failed -- read a byte at a time.
	 */
	return PyMarshal_ReadObjectFromFile(fp);

705 706
#undef SMALL_FILE_LIMIT
#undef REASONABLE_FILE_LIMIT
707 708
}

709 710 711 712 713 714 715 716 717 718 719 720
PyObject *
PyMarshal_ReadObjectFromFile(FILE *fp)
{
	RFILE rf;
	if (PyErr_Occurred()) {
		fprintf(stderr, "XXX rd_object called with exception set\n");
		return NULL;
	}
	rf.fp = fp;
	return r_object(&rf);
}

721
PyObject *
722
PyMarshal_ReadObjectFromString(char *str, int len)
723 724
{
	RFILE rf;
725
	if (PyErr_Occurred()) {
726 727 728
		fprintf(stderr, "XXX rds_object called with exception set\n");
		return NULL;
	}
729 730 731 732 733 734 735
	rf.fp = NULL;
	rf.str = NULL;
	rf.ptr = str;
	rf.end = str + len;
	return r_object(&rf);
}

736
PyObject *
737
PyMarshal_WriteObjectToString(PyObject *x) /* wrs_object() */
738 739 740
{
	WFILE wf;
	wf.fp = NULL;
741
	wf.str = PyString_FromStringAndSize((char *)NULL, 50);
742 743
	if (wf.str == NULL)
		return NULL;
744 745
	wf.ptr = PyString_AS_STRING((PyStringObject *)wf.str);
	wf.end = wf.ptr + PyString_Size(wf.str);
746
	wf.error = 0;
747
	wf.depth = 0;
748 749
	w_object(x, &wf);
	if (wf.str != NULL)
750 751 752
		_PyString_Resize(&wf.str,
		    (int) (wf.ptr -
			   PyString_AS_STRING((PyStringObject *)wf.str)));
753
	if (wf.error) {
754
		Py_XDECREF(wf.str);
755
		PyErr_SetString(PyExc_ValueError,
756 757
				(wf.error==1)?"unmarshallable object"
				:"object too deeply nested to marshal");
758 759 760 761 762
		return NULL;
	}
	return wf.str;
}

763
/* And an interface for Python programs... */
Guido van Rossum's avatar
Guido van Rossum committed
764

765
static PyObject *
766
marshal_dump(PyObject *self, PyObject *args)
Guido van Rossum's avatar
Guido van Rossum committed
767
{
768
	WFILE wf;
769 770
	PyObject *x;
	PyObject *f;
771
	if (!PyArg_ParseTuple(args, "OO:dump", &x, &f))
Guido van Rossum's avatar
Guido van Rossum committed
772
		return NULL;
773 774 775
	if (!PyFile_Check(f)) {
		PyErr_SetString(PyExc_TypeError,
				"marshal.dump() 2nd arg must be file");
Guido van Rossum's avatar
Guido van Rossum committed
776 777
		return NULL;
	}
778
	wf.fp = PyFile_AsFile(f);
779 780
	wf.str = NULL;
	wf.ptr = wf.end = NULL;
781
	wf.error = 0;
782
	wf.depth = 0;
783
	w_object(x, &wf);
784
	if (wf.error) {
785
		PyErr_SetString(PyExc_ValueError,
786 787
				(wf.error==1)?"unmarshallable object"
				:"object too deeply nested to marshal");
788 789
		return NULL;
	}
790 791
	Py_INCREF(Py_None);
	return Py_None;
Guido van Rossum's avatar
Guido van Rossum committed
792 793
}

794
static PyObject *
795
marshal_load(PyObject *self, PyObject *args)
Guido van Rossum's avatar
Guido van Rossum committed
796
{
797
	RFILE rf;
798 799
	PyObject *f;
	PyObject *v;
800
	if (!PyArg_ParseTuple(args, "O:load", &f))
801
		return NULL;
802 803 804
	if (!PyFile_Check(f)) {
		PyErr_SetString(PyExc_TypeError,
				"marshal.load() arg must be file");
Guido van Rossum's avatar
Guido van Rossum committed
805 806
		return NULL;
	}
807
	rf.fp = PyFile_AsFile(f);
808 809
	rf.str = NULL;
	rf.ptr = rf.end = NULL;
810
	PyErr_Clear();
811
	v = r_object(&rf);
812 813
	if (PyErr_Occurred()) {
		Py_XDECREF(v);
814 815 816 817 818
		v = NULL;
	}
	return v;
}

819
static PyObject *
820
marshal_dumps(PyObject *self, PyObject *args)
821
{
822
	PyObject *x;
823
	if (!PyArg_ParseTuple(args, "O:dumps", &x))
824
		return NULL;
825
	return PyMarshal_WriteObjectToString(x);
826 827
}

828
static PyObject *
829
marshal_loads(PyObject *self, PyObject *args)
830 831
{
	RFILE rf;
832
	PyObject *v;
833 834
	char *s;
	int n;
835
	if (!PyArg_ParseTuple(args, "s#:loads", &s, &n))
836 837 838 839 840
		return NULL;
	rf.fp = NULL;
	rf.str = args;
	rf.ptr = s;
	rf.end = s + n;
841
	PyErr_Clear();
842
	v = r_object(&rf);
843 844
	if (PyErr_Occurred()) {
		Py_XDECREF(v);
Guido van Rossum's avatar
Guido van Rossum committed
845 846 847 848 849
		v = NULL;
	}
	return v;
}

850
static PyMethodDef marshal_methods[] = {
851 852 853 854
	{"dump",	marshal_dump,	1},
	{"load",	marshal_load,	1},
	{"dumps",	marshal_dumps,	1},
	{"loads",	marshal_loads,	1},
Guido van Rossum's avatar
Guido van Rossum committed
855 856 857 858
	{NULL,		NULL}		/* sentinel */
};

void
859
PyMarshal_Init(void)
Guido van Rossum's avatar
Guido van Rossum committed
860
{
861
	(void) Py_InitModule("marshal", marshal_methods);
Guido van Rossum's avatar
Guido van Rossum committed
862
}