This commit was manufactured by cvs2svn to create tag 'r221c2'.
[python/dscho.git] / Python / marshal.c
blob0d21e847b770b9227a1a4dc77f497d85d8bd2eb1
2 /* Write Python objects to files and read them back.
3 This is intended for writing and reading compiled Python code only;
4 a true persistent storage facility would be much harder, since
5 it would have to take circular links and sharing into account. */
7 #include "Python.h"
8 #include "longintrepr.h"
9 #include "compile.h"
10 #include "marshal.h"
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.
16 #define MAX_MARSHAL_STACK_DEPTH 5000
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 '?'
35 typedef 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;
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)
49 static void
50 w_more(int c, WFILE *p)
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;
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);
68 static void
69 w_string(char *s, int n, WFILE *p)
71 if (p->fp != NULL) {
72 fwrite(s, 1, n, p->fp);
74 else {
75 while (--n >= 0) {
76 w_byte(*s, p);
77 s++;
82 static void
83 w_short(int x, WFILE *p)
85 w_byte( x & 0xff, p);
86 w_byte((x>> 8) & 0xff, p);
89 static void
90 w_long(long x, WFILE *p)
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);
98 #if SIZEOF_LONG > 4
99 static void
100 w_long64(long x, WFILE *p)
102 w_long(x, p);
103 w_long(x>>32, p);
105 #endif
107 static void
108 w_object(PyObject *v, WFILE *p)
110 int i, n;
112 p->depth++;
114 if (p->depth > MAX_MARSHAL_STACK_DEPTH) {
115 p->error = 2;
117 else if (v == NULL) {
118 w_byte(TYPE_NULL, p);
120 else if (v == Py_None) {
121 w_byte(TYPE_NONE, p);
123 else if (v == PyExc_StopIteration) {
124 w_byte(TYPE_STOPITER, p);
126 else if (v == Py_Ellipsis) {
127 w_byte(TYPE_ELLIPSIS, p);
129 else if (PyInt_Check(v)) {
130 long x = PyInt_AS_LONG((PyIntObject *)v);
131 #if SIZEOF_LONG > 4
132 long y = Py_ARITHMETIC_RIGHT_SHIFT(long, x, 31);
133 if (y && y != -1) {
134 w_byte(TYPE_INT64, p);
135 w_long64(x, p);
137 else
138 #endif
140 w_byte(TYPE_INT, p);
141 w_long(x, p);
144 else if (PyLong_Check(v)) {
145 PyLongObject *ob = (PyLongObject *)v;
146 w_byte(TYPE_LONG, p);
147 n = ob->ob_size;
148 w_long((long)n, p);
149 if (n < 0)
150 n = -n;
151 for (i = 0; i < n; i++)
152 w_short(ob->ob_digit[i], p);
154 else if (PyFloat_Check(v)) {
155 char buf[256]; /* Plenty to format any double */
156 PyFloat_AsReprString(buf, (PyFloatObject *)v);
157 n = strlen(buf);
158 w_byte(TYPE_FLOAT, p);
159 w_byte(n, p);
160 w_string(buf, n, p);
162 #ifndef WITHOUT_COMPLEX
163 else if (PyComplex_Check(v)) {
164 char buf[256]; /* Plenty to format any double */
165 PyFloatObject *temp;
166 w_byte(TYPE_COMPLEX, p);
167 temp = (PyFloatObject*)PyFloat_FromDouble(
168 PyComplex_RealAsDouble(v));
169 PyFloat_AsReprString(buf, temp);
170 Py_DECREF(temp);
171 n = strlen(buf);
172 w_byte(n, p);
173 w_string(buf, n, p);
174 temp = (PyFloatObject*)PyFloat_FromDouble(
175 PyComplex_ImagAsDouble(v));
176 PyFloat_AsReprString(buf, temp);
177 Py_DECREF(temp);
178 n = strlen(buf);
179 w_byte(n, p);
180 w_string(buf, n, p);
182 #endif
183 else if (PyString_Check(v)) {
184 w_byte(TYPE_STRING, p);
185 n = PyString_GET_SIZE(v);
186 w_long((long)n, p);
187 w_string(PyString_AS_STRING(v), n, p);
189 #ifdef Py_USING_UNICODE
190 else if (PyUnicode_Check(v)) {
191 PyObject *utf8;
192 utf8 = PyUnicode_AsUTF8String(v);
193 if (utf8 == NULL) {
194 p->depth--;
195 p->error = 1;
196 return;
198 w_byte(TYPE_UNICODE, p);
199 n = PyString_GET_SIZE(utf8);
200 w_long((long)n, p);
201 w_string(PyString_AS_STRING(utf8), n, p);
202 Py_DECREF(utf8);
204 #endif
205 else if (PyTuple_Check(v)) {
206 w_byte(TYPE_TUPLE, p);
207 n = PyTuple_Size(v);
208 w_long((long)n, p);
209 for (i = 0; i < n; i++) {
210 w_object(PyTuple_GET_ITEM(v, i), p);
213 else if (PyList_Check(v)) {
214 w_byte(TYPE_LIST, p);
215 n = PyList_GET_SIZE(v);
216 w_long((long)n, p);
217 for (i = 0; i < n; i++) {
218 w_object(PyList_GET_ITEM(v, i), p);
221 else if (PyDict_Check(v)) {
222 int pos;
223 PyObject *key, *value;
224 w_byte(TYPE_DICT, p);
225 /* This one is NULL object terminated! */
226 pos = 0;
227 while (PyDict_Next(v, &pos, &key, &value)) {
228 w_object(key, p);
229 w_object(value, p);
231 w_object((PyObject *)NULL, p);
233 else if (PyCode_Check(v)) {
234 PyCodeObject *co = (PyCodeObject *)v;
235 w_byte(TYPE_CODE, p);
236 w_short(co->co_argcount, p);
237 w_short(co->co_nlocals, p);
238 w_short(co->co_stacksize, p);
239 w_short(co->co_flags, p);
240 w_object(co->co_code, p);
241 w_object(co->co_consts, p);
242 w_object(co->co_names, p);
243 w_object(co->co_varnames, p);
244 w_object(co->co_freevars, p);
245 w_object(co->co_cellvars, p);
246 w_object(co->co_filename, p);
247 w_object(co->co_name, p);
248 w_short(co->co_firstlineno, p);
249 w_object(co->co_lnotab, p);
251 else if (PyObject_CheckReadBuffer(v)) {
252 /* Write unknown buffer-style objects as a string */
253 char *s;
254 PyBufferProcs *pb = v->ob_type->tp_as_buffer;
255 w_byte(TYPE_STRING, p);
256 n = (*pb->bf_getreadbuffer)(v, 0, (void **)&s);
257 w_long((long)n, p);
258 w_string(s, n, p);
260 else {
261 w_byte(TYPE_UNKNOWN, p);
262 p->error = 1;
265 p->depth--;
268 void
269 PyMarshal_WriteLongToFile(long x, FILE *fp)
271 WFILE wf;
272 wf.fp = fp;
273 wf.error = 0;
274 wf.depth = 0;
275 w_long(x, &wf);
278 void
279 PyMarshal_WriteObjectToFile(PyObject *x, FILE *fp)
281 WFILE wf;
282 wf.fp = fp;
283 wf.error = 0;
284 wf.depth = 0;
285 w_object(x, &wf);
288 typedef WFILE RFILE; /* Same struct with different invariants */
290 #define rs_byte(p) (((p)->ptr != (p)->end) ? (unsigned char)*(p)->ptr++ : EOF)
292 #define r_byte(p) ((p)->fp ? getc((p)->fp) : rs_byte(p))
294 static int
295 r_string(char *s, int n, RFILE *p)
297 if (p->fp != NULL)
298 return fread(s, 1, n, p->fp);
299 if (p->end - p->ptr < n)
300 n = p->end - p->ptr;
301 memcpy(s, p->ptr, n);
302 p->ptr += n;
303 return n;
306 static int
307 r_short(RFILE *p)
309 register short x;
310 x = r_byte(p);
311 x |= r_byte(p) << 8;
312 /* Sign-extension, in case short greater than 16 bits */
313 x |= -(x & 0x8000);
314 return x;
317 static long
318 r_long(RFILE *p)
320 register long x;
321 register FILE *fp = p->fp;
322 if (fp) {
323 x = getc(fp);
324 x |= (long)getc(fp) << 8;
325 x |= (long)getc(fp) << 16;
326 x |= (long)getc(fp) << 24;
328 else {
329 x = rs_byte(p);
330 x |= (long)rs_byte(p) << 8;
331 x |= (long)rs_byte(p) << 16;
332 x |= (long)rs_byte(p) << 24;
334 #if SIZEOF_LONG > 4
335 /* Sign extension for 64-bit machines */
336 x |= -(x & 0x80000000L);
337 #endif
338 return x;
341 /* r_long64 deals with the TYPE_INT64 code. On a machine with
342 sizeof(long) > 4, it returns a Python int object, else a Python long
343 object. Note that w_long64 writes out TYPE_INT if 32 bits is enough,
344 so there's no inefficiency here in returning a PyLong on 32-bit boxes
345 for everything written via TYPE_INT64 (i.e., if an int is written via
346 TYPE_INT64, it *needs* more than 32 bits).
348 static PyObject *
349 r_long64(RFILE *p)
351 long lo4 = r_long(p);
352 long hi4 = r_long(p);
353 #if SIZEOF_LONG > 4
354 long x = (hi4 << 32) | (lo4 & 0xFFFFFFFFL);
355 return PyInt_FromLong(x);
356 #else
357 unsigned char buf[8];
358 int one = 1;
359 int is_little_endian = (int)*(char*)&one;
360 if (is_little_endian) {
361 memcpy(buf, &lo4, 4);
362 memcpy(buf+4, &hi4, 4);
364 else {
365 memcpy(buf, &hi4, 4);
366 memcpy(buf+4, &lo4, 4);
368 return _PyLong_FromByteArray(buf, 8, is_little_endian, 1);
369 #endif
372 static PyObject *
373 r_object(RFILE *p)
375 PyObject *v, *v2;
376 long i, n;
377 int type = r_byte(p);
379 switch (type) {
381 case EOF:
382 PyErr_SetString(PyExc_EOFError,
383 "EOF read where object expected");
384 return NULL;
386 case TYPE_NULL:
387 return NULL;
389 case TYPE_NONE:
390 Py_INCREF(Py_None);
391 return Py_None;
393 case TYPE_STOPITER:
394 Py_INCREF(PyExc_StopIteration);
395 return PyExc_StopIteration;
397 case TYPE_ELLIPSIS:
398 Py_INCREF(Py_Ellipsis);
399 return Py_Ellipsis;
401 case TYPE_INT:
402 return PyInt_FromLong(r_long(p));
404 case TYPE_INT64:
405 return r_long64(p);
407 case TYPE_LONG:
409 int size;
410 PyLongObject *ob;
411 n = r_long(p);
412 size = n<0 ? -n : n;
413 ob = _PyLong_New(size);
414 if (ob == NULL)
415 return NULL;
416 ob->ob_size = n;
417 for (i = 0; i < size; i++)
418 ob->ob_digit[i] = r_short(p);
419 return (PyObject *)ob;
422 case TYPE_FLOAT:
424 char buf[256];
425 double dx;
426 n = r_byte(p);
427 if (r_string(buf, (int)n, p) != n) {
428 PyErr_SetString(PyExc_EOFError,
429 "EOF read where object expected");
430 return NULL;
432 buf[n] = '\0';
433 PyFPE_START_PROTECT("atof", return 0)
434 dx = atof(buf);
435 PyFPE_END_PROTECT(dx)
436 return PyFloat_FromDouble(dx);
439 #ifndef WITHOUT_COMPLEX
440 case TYPE_COMPLEX:
442 char buf[256];
443 Py_complex c;
444 n = r_byte(p);
445 if (r_string(buf, (int)n, p) != n) {
446 PyErr_SetString(PyExc_EOFError,
447 "EOF read where object expected");
448 return NULL;
450 buf[n] = '\0';
451 PyFPE_START_PROTECT("atof", return 0)
452 c.real = atof(buf);
453 PyFPE_END_PROTECT(c)
454 n = r_byte(p);
455 if (r_string(buf, (int)n, p) != n) {
456 PyErr_SetString(PyExc_EOFError,
457 "EOF read where object expected");
458 return NULL;
460 buf[n] = '\0';
461 PyFPE_START_PROTECT("atof", return 0)
462 c.imag = atof(buf);
463 PyFPE_END_PROTECT(c)
464 return PyComplex_FromCComplex(c);
466 #endif
468 case TYPE_STRING:
469 n = r_long(p);
470 if (n < 0) {
471 PyErr_SetString(PyExc_ValueError, "bad marshal data");
472 return NULL;
474 v = PyString_FromStringAndSize((char *)NULL, n);
475 if (v != NULL) {
476 if (r_string(PyString_AS_STRING(v), (int)n, p) != n) {
477 Py_DECREF(v);
478 v = NULL;
479 PyErr_SetString(PyExc_EOFError,
480 "EOF read where object expected");
483 return v;
485 #ifdef Py_USING_UNICODE
486 case TYPE_UNICODE:
488 char *buffer;
490 n = r_long(p);
491 if (n < 0) {
492 PyErr_SetString(PyExc_ValueError, "bad marshal data");
493 return NULL;
495 buffer = PyMem_NEW(char, n);
496 if (buffer == NULL)
497 return PyErr_NoMemory();
498 if (r_string(buffer, (int)n, p) != n) {
499 PyMem_DEL(buffer);
500 PyErr_SetString(PyExc_EOFError,
501 "EOF read where object expected");
502 return NULL;
504 v = PyUnicode_DecodeUTF8(buffer, n, NULL);
505 PyMem_DEL(buffer);
506 return v;
508 #endif
510 case TYPE_TUPLE:
511 n = r_long(p);
512 if (n < 0) {
513 PyErr_SetString(PyExc_ValueError, "bad marshal data");
514 return NULL;
516 v = PyTuple_New((int)n);
517 if (v == NULL)
518 return v;
519 for (i = 0; i < n; i++) {
520 v2 = r_object(p);
521 if ( v2 == NULL ) {
522 Py_DECREF(v);
523 v = NULL;
524 break;
526 PyTuple_SET_ITEM(v, (int)i, v2);
528 return v;
530 case TYPE_LIST:
531 n = r_long(p);
532 if (n < 0) {
533 PyErr_SetString(PyExc_ValueError, "bad marshal data");
534 return NULL;
536 v = PyList_New((int)n);
537 if (v == NULL)
538 return v;
539 for (i = 0; i < n; i++) {
540 v2 = r_object(p);
541 if ( v2 == NULL ) {
542 Py_DECREF(v);
543 v = NULL;
544 break;
546 PyList_SetItem(v, (int)i, v2);
548 return v;
550 case TYPE_DICT:
551 v = PyDict_New();
552 if (v == NULL)
553 return NULL;
554 for (;;) {
555 PyObject *key, *val;
556 key = r_object(p);
557 if (key == NULL)
558 break; /* XXX Assume TYPE_NULL, not an error */
559 val = r_object(p);
560 if (val != NULL)
561 PyDict_SetItem(v, key, val);
562 Py_DECREF(key);
563 Py_XDECREF(val);
565 return v;
567 case TYPE_CODE:
568 if (PyEval_GetRestricted()) {
569 PyErr_SetString(PyExc_RuntimeError,
570 "cannot unmarshal code objects in "
571 "restricted execution mode");
572 return NULL;
574 else {
575 int argcount = r_short(p);
576 int nlocals = r_short(p);
577 int stacksize = r_short(p);
578 int flags = r_short(p);
579 PyObject *code = NULL;
580 PyObject *consts = NULL;
581 PyObject *names = NULL;
582 PyObject *varnames = NULL;
583 PyObject *freevars = NULL;
584 PyObject *cellvars = NULL;
585 PyObject *filename = NULL;
586 PyObject *name = NULL;
587 int firstlineno = 0;
588 PyObject *lnotab = NULL;
590 code = r_object(p);
591 if (code) consts = r_object(p);
592 if (consts) names = r_object(p);
593 if (names) varnames = r_object(p);
594 if (varnames) freevars = r_object(p);
595 if (freevars) cellvars = r_object(p);
596 if (cellvars) filename = r_object(p);
597 if (filename) name = r_object(p);
598 if (name) {
599 firstlineno = r_short(p);
600 lnotab = r_object(p);
603 if (!PyErr_Occurred()) {
604 v = (PyObject *) PyCode_New(
605 argcount, nlocals, stacksize, flags,
606 code, consts, names, varnames,
607 freevars, cellvars, filename, name,
608 firstlineno, lnotab);
610 else
611 v = NULL;
612 Py_XDECREF(code);
613 Py_XDECREF(consts);
614 Py_XDECREF(names);
615 Py_XDECREF(varnames);
616 Py_XDECREF(freevars);
617 Py_XDECREF(cellvars);
618 Py_XDECREF(filename);
619 Py_XDECREF(name);
620 Py_XDECREF(lnotab);
623 return v;
625 default:
626 /* Bogus data got written, which isn't ideal.
627 This will let you keep working and recover. */
628 PyErr_SetString(PyExc_ValueError, "bad marshal data");
629 return NULL;
635 PyMarshal_ReadShortFromFile(FILE *fp)
637 RFILE rf;
638 rf.fp = fp;
639 return r_short(&rf);
642 long
643 PyMarshal_ReadLongFromFile(FILE *fp)
645 RFILE rf;
646 rf.fp = fp;
647 return r_long(&rf);
650 #ifdef HAVE_FSTAT
651 /* Return size of file in bytes; < 0 if unknown. */
652 static off_t
653 getfilesize(FILE *fp)
655 struct stat st;
656 if (fstat(fileno(fp), &st) != 0)
657 return -1;
658 else
659 return st.st_size;
661 #endif
663 /* If we can get the size of the file up-front, and it's reasonably small,
664 * read it in one gulp and delegate to ...FromString() instead. Much quicker
665 * than reading a byte at a time from file; speeds .pyc imports.
666 * CAUTION: since this may read the entire remainder of the file, don't
667 * call it unless you know you're done with the file.
669 PyObject *
670 PyMarshal_ReadLastObjectFromFile(FILE *fp)
672 /* 75% of 2.1's .pyc files can exploit SMALL_FILE_LIMIT.
673 * REASONABLE_FILE_LIMIT is by defn something big enough for Tkinter.pyc.
675 #define SMALL_FILE_LIMIT (1L << 14)
676 #define REASONABLE_FILE_LIMIT (1L << 18)
677 #ifdef HAVE_FSTAT
678 off_t filesize;
679 #endif
680 if (PyErr_Occurred()) {
681 fprintf(stderr, "XXX rd_object called with exception set\n");
682 return NULL;
684 #ifdef HAVE_FSTAT
685 filesize = getfilesize(fp);
686 if (filesize > 0) {
687 char buf[SMALL_FILE_LIMIT];
688 char* pBuf = NULL;
689 if (filesize <= SMALL_FILE_LIMIT)
690 pBuf = buf;
691 else if (filesize <= REASONABLE_FILE_LIMIT)
692 pBuf = (char *)PyMem_MALLOC(filesize);
693 if (pBuf != NULL) {
694 PyObject* v;
695 size_t n = fread(pBuf, 1, filesize, fp);
696 v = PyMarshal_ReadObjectFromString(pBuf, n);
697 if (pBuf != buf)
698 PyMem_FREE(pBuf);
699 return v;
703 #endif
704 /* We don't have fstat, or we do but the file is larger than
705 * REASONABLE_FILE_LIMIT or malloc failed -- read a byte at a time.
707 return PyMarshal_ReadObjectFromFile(fp);
709 #undef SMALL_FILE_LIMIT
710 #undef REASONABLE_FILE_LIMIT
713 PyObject *
714 PyMarshal_ReadObjectFromFile(FILE *fp)
716 RFILE rf;
717 if (PyErr_Occurred()) {
718 fprintf(stderr, "XXX rd_object called with exception set\n");
719 return NULL;
721 rf.fp = fp;
722 return r_object(&rf);
725 PyObject *
726 PyMarshal_ReadObjectFromString(char *str, int len)
728 RFILE rf;
729 if (PyErr_Occurred()) {
730 fprintf(stderr, "XXX rds_object called with exception set\n");
731 return NULL;
733 rf.fp = NULL;
734 rf.str = NULL;
735 rf.ptr = str;
736 rf.end = str + len;
737 return r_object(&rf);
740 PyObject *
741 PyMarshal_WriteObjectToString(PyObject *x) /* wrs_object() */
743 WFILE wf;
744 wf.fp = NULL;
745 wf.str = PyString_FromStringAndSize((char *)NULL, 50);
746 if (wf.str == NULL)
747 return NULL;
748 wf.ptr = PyString_AS_STRING((PyStringObject *)wf.str);
749 wf.end = wf.ptr + PyString_Size(wf.str);
750 wf.error = 0;
751 wf.depth = 0;
752 w_object(x, &wf);
753 if (wf.str != NULL)
754 _PyString_Resize(&wf.str,
755 (int) (wf.ptr -
756 PyString_AS_STRING((PyStringObject *)wf.str)));
757 if (wf.error) {
758 Py_XDECREF(wf.str);
759 PyErr_SetString(PyExc_ValueError,
760 (wf.error==1)?"unmarshallable object"
761 :"object too deeply nested to marshal");
762 return NULL;
764 return wf.str;
767 /* And an interface for Python programs... */
769 static PyObject *
770 marshal_dump(PyObject *self, PyObject *args)
772 WFILE wf;
773 PyObject *x;
774 PyObject *f;
775 if (!PyArg_ParseTuple(args, "OO:dump", &x, &f))
776 return NULL;
777 if (!PyFile_Check(f)) {
778 PyErr_SetString(PyExc_TypeError,
779 "marshal.dump() 2nd arg must be file");
780 return NULL;
782 wf.fp = PyFile_AsFile(f);
783 wf.str = NULL;
784 wf.ptr = wf.end = NULL;
785 wf.error = 0;
786 wf.depth = 0;
787 w_object(x, &wf);
788 if (wf.error) {
789 PyErr_SetString(PyExc_ValueError,
790 (wf.error==1)?"unmarshallable object"
791 :"object too deeply nested to marshal");
792 return NULL;
794 Py_INCREF(Py_None);
795 return Py_None;
798 static PyObject *
799 marshal_load(PyObject *self, PyObject *args)
801 RFILE rf;
802 PyObject *f;
803 PyObject *v;
804 if (!PyArg_ParseTuple(args, "O:load", &f))
805 return NULL;
806 if (!PyFile_Check(f)) {
807 PyErr_SetString(PyExc_TypeError,
808 "marshal.load() arg must be file");
809 return NULL;
811 rf.fp = PyFile_AsFile(f);
812 rf.str = NULL;
813 rf.ptr = rf.end = NULL;
814 PyErr_Clear();
815 v = r_object(&rf);
816 if (PyErr_Occurred()) {
817 Py_XDECREF(v);
818 v = NULL;
820 return v;
823 static PyObject *
824 marshal_dumps(PyObject *self, PyObject *args)
826 PyObject *x;
827 if (!PyArg_ParseTuple(args, "O:dumps", &x))
828 return NULL;
829 return PyMarshal_WriteObjectToString(x);
832 static PyObject *
833 marshal_loads(PyObject *self, PyObject *args)
835 RFILE rf;
836 PyObject *v;
837 char *s;
838 int n;
839 if (!PyArg_ParseTuple(args, "s#:loads", &s, &n))
840 return NULL;
841 rf.fp = NULL;
842 rf.str = args;
843 rf.ptr = s;
844 rf.end = s + n;
845 PyErr_Clear();
846 v = r_object(&rf);
847 if (PyErr_Occurred()) {
848 Py_XDECREF(v);
849 v = NULL;
851 return v;
854 static PyMethodDef marshal_methods[] = {
855 {"dump", marshal_dump, 1},
856 {"load", marshal_load, 1},
857 {"dumps", marshal_dumps, 1},
858 {"loads", marshal_loads, 1},
859 {NULL, NULL} /* sentinel */
862 void
863 PyMarshal_Init(void)
865 (void) Py_InitModule("marshal", marshal_methods);