This commit was manufactured by cvs2svn to create tag 'r22a4-fork'.
[python/dscho.git] / Python / marshal.c
blob944ae35a0c1f4373f0458785d8f1afac58bac0f0
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;
111 PyBufferProcs *pb;
113 p->depth++;
115 if (p->depth > MAX_MARSHAL_STACK_DEPTH) {
116 p->error = 2;
118 else if (v == NULL) {
119 w_byte(TYPE_NULL, p);
121 else if (v == Py_None) {
122 w_byte(TYPE_NONE, p);
124 else if (v == PyExc_StopIteration) {
125 w_byte(TYPE_STOPITER, p);
127 else if (v == Py_Ellipsis) {
128 w_byte(TYPE_ELLIPSIS, p);
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);
138 else
139 #endif
141 w_byte(TYPE_INT, p);
142 w_long(x, p);
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);
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);
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);
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);
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;
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);
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);
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);
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);
232 w_object((PyObject *)NULL, p);
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);
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)
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);
264 else {
265 w_byte(TYPE_UNKNOWN, p);
266 p->error = 1;
269 p->depth--;
272 void
273 PyMarshal_WriteLongToFile(long x, FILE *fp)
275 WFILE wf;
276 wf.fp = fp;
277 wf.error = 0;
278 wf.depth = 0;
279 w_long(x, &wf);
282 void
283 PyMarshal_WriteObjectToFile(PyObject *x, FILE *fp)
285 WFILE wf;
286 wf.fp = fp;
287 wf.error = 0;
288 wf.depth = 0;
289 w_object(x, &wf);
292 typedef WFILE RFILE; /* Same struct with different invariants */
294 #define rs_byte(p) (((p)->ptr != (p)->end) ? (unsigned char)*(p)->ptr++ : EOF)
296 #define r_byte(p) ((p)->fp ? getc((p)->fp) : rs_byte(p))
298 static int
299 r_string(char *s, int n, RFILE *p)
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;
310 static int
311 r_short(RFILE *p)
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;
321 static long
322 r_long(RFILE *p)
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;
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;
338 #if SIZEOF_LONG > 4
339 /* Sign extension for 64-bit machines */
340 x |= -(x & 0x80000000L);
341 #endif
342 return x;
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).
352 static PyObject *
353 r_long64(RFILE *p)
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);
368 else {
369 memcpy(buf, &hi4, 4);
370 memcpy(buf+4, &lo4, 4);
372 return _PyLong_FromByteArray(buf, 8, is_little_endian, 1);
373 #endif
376 static PyObject *
377 r_object(RFILE *p)
379 PyObject *v, *v2;
380 long i, n;
381 int type = r_byte(p);
383 switch (type) {
385 case EOF:
386 PyErr_SetString(PyExc_EOFError,
387 "EOF read where object expected");
388 return NULL;
390 case TYPE_NULL:
391 return NULL;
393 case TYPE_NONE:
394 Py_INCREF(Py_None);
395 return Py_None;
397 case TYPE_STOPITER:
398 Py_INCREF(PyExc_StopIteration);
399 return PyExc_StopIteration;
401 case TYPE_ELLIPSIS:
402 Py_INCREF(Py_Ellipsis);
403 return Py_Ellipsis;
405 case TYPE_INT:
406 return PyInt_FromLong(r_long(p));
408 case TYPE_INT64:
409 return r_long64(p);
411 case TYPE_LONG:
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;
426 case TYPE_FLOAT:
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;
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);
443 #ifndef WITHOUT_COMPLEX
444 case TYPE_COMPLEX:
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;
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;
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);
470 #endif
472 case TYPE_STRING:
473 n = r_long(p);
474 if (n < 0) {
475 PyErr_SetString(PyExc_ValueError, "bad marshal data");
476 return NULL;
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");
487 return v;
489 #ifdef Py_USING_UNICODE
490 case TYPE_UNICODE:
492 char *buffer;
494 n = r_long(p);
495 if (n < 0) {
496 PyErr_SetString(PyExc_ValueError, "bad marshal data");
497 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 PyErr_SetString(PyExc_EOFError,
505 "EOF read where object expected");
506 return NULL;
508 v = PyUnicode_DecodeUTF8(buffer, n, NULL);
509 PyMem_DEL(buffer);
510 return v;
512 #endif
514 case TYPE_TUPLE:
515 n = r_long(p);
516 if (n < 0) {
517 PyErr_SetString(PyExc_ValueError, "bad marshal data");
518 return NULL;
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;
530 PyTuple_SET_ITEM(v, (int)i, v2);
532 return v;
534 case TYPE_LIST:
535 n = r_long(p);
536 if (n < 0) {
537 PyErr_SetString(PyExc_ValueError, "bad marshal data");
538 return NULL;
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;
550 PyList_SetItem(v, (int)i, v2);
552 return v;
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);
569 return v;
571 case TYPE_CODE:
572 if (PyEval_GetRestricted()) {
573 PyErr_SetString(PyExc_RuntimeError,
574 "cannot unmarshal code objects in "
575 "restricted execution mode");
576 return NULL;
578 else {
579 int argcount = r_short(p);
580 int nlocals = r_short(p);
581 int stacksize = r_short(p);
582 int flags = r_short(p);
583 PyObject *code = NULL;
584 PyObject *consts = NULL;
585 PyObject *names = NULL;
586 PyObject *varnames = NULL;
587 PyObject *freevars = NULL;
588 PyObject *cellvars = NULL;
589 PyObject *filename = NULL;
590 PyObject *name = NULL;
591 int firstlineno = 0;
592 PyObject *lnotab = NULL;
594 code = r_object(p);
595 if (code) consts = r_object(p);
596 if (consts) names = r_object(p);
597 if (names) varnames = r_object(p);
598 if (varnames) freevars = r_object(p);
599 if (freevars) cellvars = r_object(p);
600 if (cellvars) filename = r_object(p);
601 if (filename) name = r_object(p);
602 if (name) {
603 firstlineno = r_short(p);
604 lnotab = r_object(p);
607 if (!PyErr_Occurred()) {
608 v = (PyObject *) PyCode_New(
609 argcount, nlocals, stacksize, flags,
610 code, consts, names, varnames,
611 freevars, cellvars, filename, name,
612 firstlineno, lnotab);
614 else
615 v = NULL;
616 Py_XDECREF(code);
617 Py_XDECREF(consts);
618 Py_XDECREF(names);
619 Py_XDECREF(varnames);
620 Py_XDECREF(freevars);
621 Py_XDECREF(cellvars);
622 Py_XDECREF(filename);
623 Py_XDECREF(name);
624 Py_XDECREF(lnotab);
627 return v;
629 default:
630 /* Bogus data got written, which isn't ideal.
631 This will let you keep working and recover. */
632 PyErr_SetString(PyExc_ValueError, "bad marshal data");
633 return NULL;
638 long
639 PyMarshal_ReadLongFromFile(FILE *fp)
641 RFILE rf;
642 rf.fp = fp;
643 return r_long(&rf);
646 #ifdef HAVE_FSTAT
647 /* Return size of file in bytes; < 0 if unknown. */
648 static off_t
649 getfilesize(FILE *fp)
651 struct stat st;
652 if (fstat(fileno(fp), &st) != 0)
653 return -1;
654 else
655 return st.st_size;
657 #endif
659 /* If we can get the size of the file up-front, and it's reasonably small,
660 * read it in one gulp and delegate to ...FromString() instead. Much quicker
661 * than reading a byte at a time from file; speeds .pyc imports.
662 * CAUTION: since this may read the entire remainder of the file, don't
663 * call it unless you know you're done with the file.
665 PyObject *
666 PyMarshal_ReadLastObjectFromFile(FILE *fp)
668 /* 75% of 2.1's .pyc files can exploit SMALL_FILE_LIMIT.
669 * REASONABLE_FILE_LIMIT is by defn something big enough for Tkinter.pyc.
671 #define SMALL_FILE_LIMIT (1L << 14)
672 #define REASONABLE_FILE_LIMIT (1L << 18)
673 #ifdef HAVE_FSTAT
674 off_t filesize;
675 #endif
676 if (PyErr_Occurred()) {
677 fprintf(stderr, "XXX rd_object called with exception set\n");
678 return NULL;
680 #ifdef HAVE_FSTAT
681 filesize = getfilesize(fp);
682 if (filesize > 0) {
683 char buf[SMALL_FILE_LIMIT];
684 char* pBuf = NULL;
685 if (filesize <= SMALL_FILE_LIMIT)
686 pBuf = buf;
687 else if (filesize <= REASONABLE_FILE_LIMIT)
688 pBuf = (char *)PyMem_MALLOC(filesize);
689 if (pBuf != NULL) {
690 PyObject* v;
691 size_t n = fread(pBuf, 1, filesize, fp);
692 v = PyMarshal_ReadObjectFromString(pBuf, n);
693 if (pBuf != buf)
694 PyMem_FREE(pBuf);
695 return v;
699 #endif
700 /* We don't have fstat, or we do but the file is larger than
701 * REASONABLE_FILE_LIMIT or malloc failed -- read a byte at a time.
703 return PyMarshal_ReadObjectFromFile(fp);
705 #undef SMALL_FILE_LIMIT
706 #undef REASONABLE_FILE_LIMIT
709 PyObject *
710 PyMarshal_ReadObjectFromFile(FILE *fp)
712 RFILE rf;
713 if (PyErr_Occurred()) {
714 fprintf(stderr, "XXX rd_object called with exception set\n");
715 return NULL;
717 rf.fp = fp;
718 return r_object(&rf);
721 PyObject *
722 PyMarshal_ReadObjectFromString(char *str, int len)
724 RFILE rf;
725 if (PyErr_Occurred()) {
726 fprintf(stderr, "XXX rds_object called with exception set\n");
727 return NULL;
729 rf.fp = NULL;
730 rf.str = NULL;
731 rf.ptr = str;
732 rf.end = str + len;
733 return r_object(&rf);
736 PyObject *
737 PyMarshal_WriteObjectToString(PyObject *x) /* wrs_object() */
739 WFILE wf;
740 wf.fp = NULL;
741 wf.str = PyString_FromStringAndSize((char *)NULL, 50);
742 if (wf.str == NULL)
743 return NULL;
744 wf.ptr = PyString_AS_STRING((PyStringObject *)wf.str);
745 wf.end = wf.ptr + PyString_Size(wf.str);
746 wf.error = 0;
747 wf.depth = 0;
748 w_object(x, &wf);
749 if (wf.str != NULL)
750 _PyString_Resize(&wf.str,
751 (int) (wf.ptr -
752 PyString_AS_STRING((PyStringObject *)wf.str)));
753 if (wf.error) {
754 Py_XDECREF(wf.str);
755 PyErr_SetString(PyExc_ValueError,
756 (wf.error==1)?"unmarshallable object"
757 :"object too deeply nested to marshal");
758 return NULL;
760 return wf.str;
763 /* And an interface for Python programs... */
765 static PyObject *
766 marshal_dump(PyObject *self, PyObject *args)
768 WFILE wf;
769 PyObject *x;
770 PyObject *f;
771 if (!PyArg_ParseTuple(args, "OO:dump", &x, &f))
772 return NULL;
773 if (!PyFile_Check(f)) {
774 PyErr_SetString(PyExc_TypeError,
775 "marshal.dump() 2nd arg must be file");
776 return NULL;
778 wf.fp = PyFile_AsFile(f);
779 wf.str = NULL;
780 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 (wf.error==1)?"unmarshallable object"
787 :"object too deeply nested to marshal");
788 return NULL;
790 Py_INCREF(Py_None);
791 return Py_None;
794 static PyObject *
795 marshal_load(PyObject *self, PyObject *args)
797 RFILE rf;
798 PyObject *f;
799 PyObject *v;
800 if (!PyArg_ParseTuple(args, "O:load", &f))
801 return NULL;
802 if (!PyFile_Check(f)) {
803 PyErr_SetString(PyExc_TypeError,
804 "marshal.load() arg must be file");
805 return NULL;
807 rf.fp = PyFile_AsFile(f);
808 rf.str = NULL;
809 rf.ptr = rf.end = NULL;
810 PyErr_Clear();
811 v = r_object(&rf);
812 if (PyErr_Occurred()) {
813 Py_XDECREF(v);
814 v = NULL;
816 return v;
819 static PyObject *
820 marshal_dumps(PyObject *self, PyObject *args)
822 PyObject *x;
823 if (!PyArg_ParseTuple(args, "O:dumps", &x))
824 return NULL;
825 return PyMarshal_WriteObjectToString(x);
828 static PyObject *
829 marshal_loads(PyObject *self, PyObject *args)
831 RFILE rf;
832 PyObject *v;
833 char *s;
834 int n;
835 if (!PyArg_ParseTuple(args, "s#:loads", &s, &n))
836 return NULL;
837 rf.fp = NULL;
838 rf.str = args;
839 rf.ptr = s;
840 rf.end = s + n;
841 PyErr_Clear();
842 v = r_object(&rf);
843 if (PyErr_Occurred()) {
844 Py_XDECREF(v);
845 v = NULL;
847 return v;
850 static PyMethodDef marshal_methods[] = {
851 {"dump", marshal_dump, 1},
852 {"load", marshal_load, 1},
853 {"dumps", marshal_dumps, 1},
854 {"loads", marshal_loads, 1},
855 {NULL, NULL} /* sentinel */
858 void
859 PyMarshal_Init(void)
861 (void) Py_InitModule("marshal", marshal_methods);