blob: 78fe6242cf7433bf4928b0b4a2686a3f7ca649f0 [file] [log] [blame]
Guido van Rossumdce2e3d1991-06-04 19:42:30 +00001/***********************************************************
Guido van Rossumfd71b9e2000-06-30 23:50:40 +00002Copyright (c) 2000, BeOpen.com.
3Copyright (c) 1995-2000, Corporation for National Research Initiatives.
4Copyright (c) 1990-1995, Stichting Mathematisch Centrum.
5All rights reserved.
Guido van Rossumdce2e3d1991-06-04 19:42:30 +00006
Guido van Rossumfd71b9e2000-06-30 23:50:40 +00007See the file "Misc/COPYRIGHT" for information on usage and
8redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
Guido van Rossumdce2e3d1991-06-04 19:42:30 +00009******************************************************************/
10
11/* Write Python objects to files and read them back.
12 This is intended for writing and reading compiled Python code only;
13 a true persistent storage facility would be much harder, since
14 it would have to take circular links and sharing into account. */
15
Guido van Rossum79f25d91997-04-29 20:08:16 +000016#include "Python.h"
Guido van Rossumdce2e3d1991-06-04 19:42:30 +000017#include "longintrepr.h"
18#include "compile.h"
19#include "marshal.h"
20
Fred Drake6da0b912000-06-28 18:47:56 +000021/* High water mark to determine when the marshalled object is dangerously deep
22 * and risks coring the interpreter. When the object stack gets this deep,
23 * raise an exception instead of continuing.
24 */
25#define MAX_MARSHAL_STACK_DEPTH 5000
26
Guido van Rossumdce2e3d1991-06-04 19:42:30 +000027#define TYPE_NULL '0'
28#define TYPE_NONE 'N'
Guido van Rossume449af71996-10-11 16:25:41 +000029#define TYPE_ELLIPSIS '.'
Guido van Rossumdce2e3d1991-06-04 19:42:30 +000030#define TYPE_INT 'i'
Guido van Rossumb0c168c1996-12-05 23:15:02 +000031#define TYPE_INT64 'I'
Guido van Rossumdce2e3d1991-06-04 19:42:30 +000032#define TYPE_FLOAT 'f'
Guido van Rossum8a5c5d21996-01-12 01:09:56 +000033#define TYPE_COMPLEX 'x'
Guido van Rossumdce2e3d1991-06-04 19:42:30 +000034#define TYPE_LONG 'l'
35#define TYPE_STRING 's'
36#define TYPE_TUPLE '('
37#define TYPE_LIST '['
38#define TYPE_DICT '{'
Guido van Rossum681d79a1995-07-18 14:51:37 +000039#define TYPE_CODE 'c'
Guido van Rossumc279b532000-03-10 23:03:02 +000040#define TYPE_UNICODE 'u'
Guido van Rossumdce2e3d1991-06-04 19:42:30 +000041#define TYPE_UNKNOWN '?'
42
Guido van Rossum0b0db8e1993-01-21 16:07:51 +000043typedef struct {
44 FILE *fp;
Guido van Rossumf2150601996-06-26 20:41:23 +000045 int error;
Fred Drake6da0b912000-06-28 18:47:56 +000046 int depth;
Guido van Rossum0b0db8e1993-01-21 16:07:51 +000047 /* If fp == NULL, the following are valid: */
Guido van Rossum79f25d91997-04-29 20:08:16 +000048 PyObject *str;
Guido van Rossum0b0db8e1993-01-21 16:07:51 +000049 char *ptr;
50 char *end;
51} WFILE;
Guido van Rossumdce2e3d1991-06-04 19:42:30 +000052
Guido van Rossum0b0db8e1993-01-21 16:07:51 +000053#define w_byte(c, p) if (((p)->fp)) putc((c), (p)->fp); \
54 else if ((p)->ptr != (p)->end) *(p)->ptr++ = (c); \
55 else w_more(c, p)
56
57static void
58w_more(c, p)
59 char c;
60 WFILE *p;
61{
62 int size, newsize;
63 if (p->str == NULL)
64 return; /* An error already occurred */
Guido van Rossum79f25d91997-04-29 20:08:16 +000065 size = PyString_Size(p->str);
Guido van Rossum0b0db8e1993-01-21 16:07:51 +000066 newsize = size + 1024;
Guido van Rossum79f25d91997-04-29 20:08:16 +000067 if (_PyString_Resize(&p->str, newsize) != 0) {
Guido van Rossum0b0db8e1993-01-21 16:07:51 +000068 p->ptr = p->end = NULL;
69 }
70 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +000071 p->ptr = PyString_AS_STRING((PyStringObject *)p->str) + size;
72 p->end =
73 PyString_AS_STRING((PyStringObject *)p->str) + newsize;
Guido van Rossum0b0db8e1993-01-21 16:07:51 +000074 *p->ptr++ = c;
75 }
76}
77
78static void
79w_string(s, n, p)
80 char *s;
81 int n;
82 WFILE *p;
83{
84 if (p->fp != NULL) {
85 fwrite(s, 1, n, p->fp);
86 }
87 else {
88 while (--n >= 0) {
89 w_byte(*s, p);
90 s++;
91 }
92 }
93}
94
95static void
96w_short(x, p)
Guido van Rossumdce2e3d1991-06-04 19:42:30 +000097 int x;
Guido van Rossum0b0db8e1993-01-21 16:07:51 +000098 WFILE *p;
Guido van Rossumdce2e3d1991-06-04 19:42:30 +000099{
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000100 w_byte( x & 0xff, p);
101 w_byte((x>> 8) & 0xff, p);
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000102}
103
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000104static void
105w_long(x, p)
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000106 long x;
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000107 WFILE *p;
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000108{
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000109 w_byte((int)( x & 0xff), p);
110 w_byte((int)((x>> 8) & 0xff), p);
111 w_byte((int)((x>>16) & 0xff), p);
112 w_byte((int)((x>>24) & 0xff), p);
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000113}
114
Guido van Rossumc1547d91996-12-10 15:39:04 +0000115#if SIZEOF_LONG > 4
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000116static void
Guido van Rossumb0c168c1996-12-05 23:15:02 +0000117w_long64(x, p)
118 long x;
119 WFILE *p;
120{
121 w_long(x, p);
Guido van Rossumc1547d91996-12-10 15:39:04 +0000122 w_long(x>>32, p);
Guido van Rossumb0c168c1996-12-05 23:15:02 +0000123}
Guido van Rossumc1547d91996-12-10 15:39:04 +0000124#endif
Guido van Rossumb0c168c1996-12-05 23:15:02 +0000125
126static void
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000127w_object(v, p)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000128 PyObject *v;
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000129 WFILE *p;
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000130{
Guido van Rossum3a205f71995-02-17 15:10:07 +0000131 int i, n;
Guido van Rossumd076c731998-10-07 19:42:25 +0000132 PyBufferProcs *pb;
Fred Drake6da0b912000-06-28 18:47:56 +0000133
134 p->depth++;
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000135
Fred Drake6da0b912000-06-28 18:47:56 +0000136 if (p->depth > MAX_MARSHAL_STACK_DEPTH) {
137 p->error = 2;
138 }
139 else if (v == NULL) {
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000140 w_byte(TYPE_NULL, p);
Guido van Rossum730806d1998-04-10 22:27:42 +0000141 }
142 else if (v == Py_None) {
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000143 w_byte(TYPE_NONE, p);
Guido van Rossum730806d1998-04-10 22:27:42 +0000144 }
145 else if (v == Py_Ellipsis) {
146 w_byte(TYPE_ELLIPSIS, p);
147 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000148 else if (PyInt_Check(v)) {
149 long x = PyInt_AS_LONG((PyIntObject *)v);
Guido van Rossumc1547d91996-12-10 15:39:04 +0000150#if SIZEOF_LONG > 4
Guido van Rossumb0c168c1996-12-05 23:15:02 +0000151 long y = x>>31;
152 if (y && y != -1) {
153 w_byte(TYPE_INT64, p);
154 w_long64(x, p);
155 }
Guido van Rossumc1547d91996-12-10 15:39:04 +0000156 else
157#endif
158 {
Guido van Rossumb0c168c1996-12-05 23:15:02 +0000159 w_byte(TYPE_INT, p);
160 w_long(x, p);
161 }
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000162 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000163 else if (PyLong_Check(v)) {
164 PyLongObject *ob = (PyLongObject *)v;
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000165 w_byte(TYPE_LONG, p);
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000166 n = ob->ob_size;
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000167 w_long((long)n, p);
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000168 if (n < 0)
169 n = -n;
170 for (i = 0; i < n; i++)
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000171 w_short(ob->ob_digit[i], p);
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000172 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000173 else if (PyFloat_Check(v)) {
Tim Petersdbd9ba62000-07-09 03:09:57 +0000174 extern void PyFloat_AsString(char *, PyFloatObject *);
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000175 char buf[256]; /* Plenty to format any double */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000176 PyFloat_AsString(buf, (PyFloatObject *)v);
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000177 n = strlen(buf);
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000178 w_byte(TYPE_FLOAT, p);
Guido van Rossum3a205f71995-02-17 15:10:07 +0000179 w_byte(n, p);
180 w_string(buf, n, p);
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000181 }
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000182#ifndef WITHOUT_COMPLEX
Guido van Rossum79f25d91997-04-29 20:08:16 +0000183 else if (PyComplex_Check(v)) {
Tim Petersdbd9ba62000-07-09 03:09:57 +0000184 extern void PyFloat_AsString(char *, PyFloatObject *);
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000185 char buf[256]; /* Plenty to format any double */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000186 PyFloatObject *temp;
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000187 w_byte(TYPE_COMPLEX, p);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000188 temp = (PyFloatObject*)PyFloat_FromDouble(
189 PyComplex_RealAsDouble(v));
190 PyFloat_AsString(buf, temp);
191 Py_DECREF(temp);
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000192 n = strlen(buf);
193 w_byte(n, p);
194 w_string(buf, n, p);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000195 temp = (PyFloatObject*)PyFloat_FromDouble(
196 PyComplex_ImagAsDouble(v));
197 PyFloat_AsString(buf, temp);
198 Py_DECREF(temp);
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000199 n = strlen(buf);
200 w_byte(n, p);
201 w_string(buf, n, p);
202 }
203#endif
Guido van Rossum79f25d91997-04-29 20:08:16 +0000204 else if (PyString_Check(v)) {
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000205 w_byte(TYPE_STRING, p);
Guido van Rossumc279b532000-03-10 23:03:02 +0000206 n = PyString_GET_SIZE(v);
Guido van Rossum3a205f71995-02-17 15:10:07 +0000207 w_long((long)n, p);
Guido van Rossumc279b532000-03-10 23:03:02 +0000208 w_string(PyString_AS_STRING(v), n, p);
209 }
210 else if (PyUnicode_Check(v)) {
211 PyObject *utf8;
212 utf8 = PyUnicode_AsUTF8String(v);
213 if (utf8 == NULL) {
Guido van Rossum98626cd2000-06-28 23:24:19 +0000214 p->depth--;
215 p->error = 1;
216 return;
Guido van Rossumc279b532000-03-10 23:03:02 +0000217 }
218 w_byte(TYPE_UNICODE, p);
219 n = PyString_GET_SIZE(utf8);
220 w_long((long)n, p);
221 w_string(PyString_AS_STRING(utf8), n, p);
222 Py_DECREF(utf8);
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000223 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000224 else if (PyTuple_Check(v)) {
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000225 w_byte(TYPE_TUPLE, p);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000226 n = PyTuple_Size(v);
Guido van Rossum3a205f71995-02-17 15:10:07 +0000227 w_long((long)n, p);
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000228 for (i = 0; i < n; i++) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000229 w_object(PyTuple_GET_ITEM(v, i), p);
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000230 }
231 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000232 else if (PyList_Check(v)) {
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000233 w_byte(TYPE_LIST, p);
Guido van Rossumc279b532000-03-10 23:03:02 +0000234 n = PyList_GET_SIZE(v);
Guido van Rossum3a205f71995-02-17 15:10:07 +0000235 w_long((long)n, p);
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000236 for (i = 0; i < n; i++) {
Guido van Rossumc279b532000-03-10 23:03:02 +0000237 w_object(PyList_GET_ITEM(v, i), p);
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000238 }
239 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000240 else if (PyDict_Check(v)) {
Guido van Rossum25831651993-05-19 14:50:45 +0000241 int pos;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000242 PyObject *key, *value;
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000243 w_byte(TYPE_DICT, p);
Guido van Rossum64b45521991-06-07 13:58:22 +0000244 /* This one is NULL object terminated! */
Guido van Rossum25831651993-05-19 14:50:45 +0000245 pos = 0;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000246 while (PyDict_Next(v, &pos, &key, &value)) {
Guido van Rossum25831651993-05-19 14:50:45 +0000247 w_object(key, p);
248 w_object(value, p);
Guido van Rossum64b45521991-06-07 13:58:22 +0000249 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000250 w_object((PyObject *)NULL, p);
Guido van Rossum64b45521991-06-07 13:58:22 +0000251 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000252 else if (PyCode_Check(v)) {
253 PyCodeObject *co = (PyCodeObject *)v;
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000254 w_byte(TYPE_CODE, p);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000255 w_short(co->co_argcount, p);
256 w_short(co->co_nlocals, p);
Guido van Rossum98a9b311997-01-17 21:07:08 +0000257 w_short(co->co_stacksize, p);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000258 w_short(co->co_flags, p);
Guido van Rossumd076c731998-10-07 19:42:25 +0000259 w_object(co->co_code, p);
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000260 w_object(co->co_consts, p);
261 w_object(co->co_names, p);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000262 w_object(co->co_varnames, p);
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000263 w_object(co->co_filename, p);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000264 w_object(co->co_name, p);
Guido van Rossumd031c891997-01-24 03:44:17 +0000265 w_short(co->co_firstlineno, p);
266 w_object(co->co_lnotab, p);
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000267 }
Guido van Rossumd076c731998-10-07 19:42:25 +0000268 else if ((pb = v->ob_type->tp_as_buffer) != NULL &&
269 pb->bf_getsegcount != NULL &&
270 pb->bf_getreadbuffer != NULL &&
271 (*pb->bf_getsegcount)(v, NULL) == 1)
272 {
273 /* Write unknown buffer-style objects as a string */
274 char *s;
275 w_byte(TYPE_STRING, p);
276 n = (*pb->bf_getreadbuffer)(v, 0, (void **)&s);
277 w_long((long)n, p);
278 w_string(s, n, p);
279 }
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000280 else {
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000281 w_byte(TYPE_UNKNOWN, p);
Guido van Rossumf2150601996-06-26 20:41:23 +0000282 p->error = 1;
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000283 }
Guido van Rossum98626cd2000-06-28 23:24:19 +0000284
285 p->depth--;
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000286}
287
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000288void
Guido van Rossum79f25d91997-04-29 20:08:16 +0000289PyMarshal_WriteLongToFile(x, fp)
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000290 long x;
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000291 FILE *fp;
292{
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000293 WFILE wf;
294 wf.fp = fp;
Guido van Rossumf2150601996-06-26 20:41:23 +0000295 wf.error = 0;
Fred Drake6da0b912000-06-28 18:47:56 +0000296 wf.depth = 0;
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000297 w_long(x, &wf);
298}
299
300void
Guido van Rossum79f25d91997-04-29 20:08:16 +0000301PyMarshal_WriteObjectToFile(x, fp)
302 PyObject *x;
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000303 FILE *fp;
304{
305 WFILE wf;
306 wf.fp = fp;
Guido van Rossumf2150601996-06-26 20:41:23 +0000307 wf.error = 0;
Guido van Rossum98626cd2000-06-28 23:24:19 +0000308 wf.depth = 0;
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000309 w_object(x, &wf);
310}
311
312typedef WFILE RFILE; /* Same struct with different invariants */
313
Guido van Rossum8d617a61995-03-09 12:12:11 +0000314#define rs_byte(p) (((p)->ptr != (p)->end) ? (unsigned char)*(p)->ptr++ : EOF)
315
316#define r_byte(p) ((p)->fp ? getc((p)->fp) : rs_byte(p))
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000317
318static int
319r_string(s, n, p)
320 char *s;
321 int n;
322 RFILE *p;
323{
324 if (p->fp != NULL)
325 return fread(s, 1, n, p->fp);
326 if (p->end - p->ptr < n)
327 n = p->end - p->ptr;
328 memcpy(s, p->ptr, n);
329 p->ptr += n;
330 return n;
331}
332
333static int
334r_short(p)
335 RFILE *p;
336{
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000337 register short x;
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000338 x = r_byte(p);
339 x |= r_byte(p) << 8;
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000340 /* XXX If your short is > 16 bits, add sign-extension here!!! */
341 return x;
342}
343
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000344static long
345r_long(p)
346 RFILE *p;
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000347{
348 register long x;
Guido van Rossum8d617a61995-03-09 12:12:11 +0000349 register FILE *fp = p->fp;
350 if (fp) {
351 x = getc(fp);
352 x |= (long)getc(fp) << 8;
353 x |= (long)getc(fp) << 16;
354 x |= (long)getc(fp) << 24;
355 }
356 else {
357 x = rs_byte(p);
358 x |= (long)rs_byte(p) << 8;
359 x |= (long)rs_byte(p) << 16;
360 x |= (long)rs_byte(p) << 24;
361 }
Guido van Rossumc1547d91996-12-10 15:39:04 +0000362#if SIZEOF_LONG > 4
Guido van Rossumb0c168c1996-12-05 23:15:02 +0000363 /* Sign extension for 64-bit machines */
364 x <<= (8*sizeof(long) - 32);
365 x >>= (8*sizeof(long) - 32);
Guido van Rossumc1547d91996-12-10 15:39:04 +0000366#endif
Guido van Rossumb0c168c1996-12-05 23:15:02 +0000367 return x;
368}
369
370static long
371r_long64(p)
372 RFILE *p;
373{
374 register long x;
Guido van Rossumc1547d91996-12-10 15:39:04 +0000375 x = r_long(p);
376#if SIZEOF_LONG > 4
377 x = (x & 0xFFFFFFFF) | (r_long(p) << 32);
378#else
379 if (r_long(p) != 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000380 PyObject *f = PySys_GetObject("stderr");
Guido van Rossumc1547d91996-12-10 15:39:04 +0000381 if (f != NULL)
Guido van Rossum7e8d26d1997-05-22 22:35:47 +0000382 (void) PyFile_WriteString(
Guido van Rossumc1547d91996-12-10 15:39:04 +0000383 "Warning: un-marshal 64-bit int in 32-bit mode\n",
384 f);
Guido van Rossumb0c168c1996-12-05 23:15:02 +0000385 }
Guido van Rossumc1547d91996-12-10 15:39:04 +0000386#endif
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000387 return x;
388}
389
Guido van Rossum79f25d91997-04-29 20:08:16 +0000390static PyObject *
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000391r_object(p)
392 RFILE *p;
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000393{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000394 PyObject *v, *v2;
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000395 long i, n;
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000396 int type = r_byte(p);
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000397
398 switch (type) {
399
400 case EOF:
Guido van Rossum79f25d91997-04-29 20:08:16 +0000401 PyErr_SetString(PyExc_EOFError,
402 "EOF read where object expected");
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000403 return NULL;
404
405 case TYPE_NULL:
406 return NULL;
407
408 case TYPE_NONE:
Guido van Rossum79f25d91997-04-29 20:08:16 +0000409 Py_INCREF(Py_None);
410 return Py_None;
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000411
Guido van Rossume449af71996-10-11 16:25:41 +0000412 case TYPE_ELLIPSIS:
Guido van Rossum79f25d91997-04-29 20:08:16 +0000413 Py_INCREF(Py_Ellipsis);
Guido van Rossume449af71996-10-11 16:25:41 +0000414 return Py_Ellipsis;
Guido van Rossum8861b741996-07-30 16:49:37 +0000415
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000416 case TYPE_INT:
Guido van Rossum79f25d91997-04-29 20:08:16 +0000417 return PyInt_FromLong(r_long(p));
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000418
Guido van Rossumb0c168c1996-12-05 23:15:02 +0000419 case TYPE_INT64:
Guido van Rossum79f25d91997-04-29 20:08:16 +0000420 return PyInt_FromLong(r_long64(p));
Guido van Rossumb0c168c1996-12-05 23:15:02 +0000421
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000422 case TYPE_LONG:
423 {
424 int size;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000425 PyLongObject *ob;
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000426 n = r_long(p);
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000427 size = n<0 ? -n : n;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000428 ob = _PyLong_New(size);
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000429 if (ob == NULL)
430 return NULL;
431 ob->ob_size = n;
432 for (i = 0; i < size; i++)
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000433 ob->ob_digit[i] = r_short(p);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000434 return (PyObject *)ob;
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000435 }
436
437 case TYPE_FLOAT:
438 {
Tim Petersdbd9ba62000-07-09 03:09:57 +0000439 extern double atof(const char *);
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000440 char buf[256];
Guido van Rossum0ae748d1997-02-14 22:58:07 +0000441 double dx;
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000442 n = r_byte(p);
443 if (r_string(buf, (int)n, p) != n) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000444 PyErr_SetString(PyExc_EOFError,
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000445 "EOF read where object expected");
446 return NULL;
447 }
448 buf[n] = '\0';
Guido van Rossum0ae748d1997-02-14 22:58:07 +0000449 PyFPE_START_PROTECT("atof", return 0)
450 dx = atof(buf);
Guido van Rossum45b83911997-03-14 04:32:50 +0000451 PyFPE_END_PROTECT(dx)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000452 return PyFloat_FromDouble(dx);
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000453 }
454
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000455#ifndef WITHOUT_COMPLEX
456 case TYPE_COMPLEX:
457 {
Tim Petersdbd9ba62000-07-09 03:09:57 +0000458 extern double atof(const char *);
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000459 char buf[256];
Guido van Rossum530956d1996-07-21 02:27:43 +0000460 Py_complex c;
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000461 n = r_byte(p);
462 if (r_string(buf, (int)n, p) != n) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000463 PyErr_SetString(PyExc_EOFError,
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000464 "EOF read where object expected");
465 return NULL;
466 }
467 buf[n] = '\0';
Guido van Rossum0ae748d1997-02-14 22:58:07 +0000468 PyFPE_START_PROTECT("atof", return 0)
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000469 c.real = atof(buf);
Guido van Rossum45b83911997-03-14 04:32:50 +0000470 PyFPE_END_PROTECT(c)
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000471 n = r_byte(p);
472 if (r_string(buf, (int)n, p) != n) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000473 PyErr_SetString(PyExc_EOFError,
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000474 "EOF read where object expected");
475 return NULL;
476 }
477 buf[n] = '\0';
Guido van Rossum0ae748d1997-02-14 22:58:07 +0000478 PyFPE_START_PROTECT("atof", return 0)
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000479 c.imag = atof(buf);
Guido van Rossum45b83911997-03-14 04:32:50 +0000480 PyFPE_END_PROTECT(c)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000481 return PyComplex_FromCComplex(c);
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000482 }
483#endif
484
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000485 case TYPE_STRING:
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000486 n = r_long(p);
Guido van Rossuma45cb451998-06-08 20:27:29 +0000487 if (n < 0) {
488 PyErr_SetString(PyExc_ValueError, "bad marshal data");
489 return NULL;
490 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000491 v = PyString_FromStringAndSize((char *)NULL, n);
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000492 if (v != NULL) {
Guido van Rossumc279b532000-03-10 23:03:02 +0000493 if (r_string(PyString_AS_STRING(v), (int)n, p) != n) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000494 Py_DECREF(v);
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000495 v = NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000496 PyErr_SetString(PyExc_EOFError,
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000497 "EOF read where object expected");
498 }
499 }
500 return v;
501
Guido van Rossumc279b532000-03-10 23:03:02 +0000502 case TYPE_UNICODE:
503 {
504 char *buffer;
505
506 n = r_long(p);
507 if (n < 0) {
508 PyErr_SetString(PyExc_ValueError, "bad marshal data");
509 return NULL;
510 }
Guido van Rossumb18618d2000-05-03 23:44:39 +0000511 buffer = PyMem_NEW(char, n);
Guido van Rossumc279b532000-03-10 23:03:02 +0000512 if (buffer == NULL)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000513 return PyErr_NoMemory();
Guido van Rossumc279b532000-03-10 23:03:02 +0000514 if (r_string(buffer, (int)n, p) != n) {
Guido van Rossumb18618d2000-05-03 23:44:39 +0000515 PyMem_DEL(buffer);
Guido van Rossumc279b532000-03-10 23:03:02 +0000516 PyErr_SetString(PyExc_EOFError,
517 "EOF read where object expected");
518 return NULL;
519 }
520 v = PyUnicode_DecodeUTF8(buffer, n, NULL);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000521 PyMem_DEL(buffer);
Guido van Rossumc279b532000-03-10 23:03:02 +0000522 return v;
523 }
524
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000525 case TYPE_TUPLE:
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000526 n = r_long(p);
Guido van Rossuma45cb451998-06-08 20:27:29 +0000527 if (n < 0) {
528 PyErr_SetString(PyExc_ValueError, "bad marshal data");
529 return NULL;
530 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000531 v = PyTuple_New((int)n);
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000532 if (v == NULL)
533 return v;
Jack Jansen9513f2c1995-10-27 13:21:28 +0000534 for (i = 0; i < n; i++) {
535 v2 = r_object(p);
536 if ( v2 == NULL ) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000537 Py_DECREF(v);
Jack Jansen9513f2c1995-10-27 13:21:28 +0000538 v = NULL;
539 break;
540 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000541 PyTuple_SET_ITEM(v, (int)i, v2);
Jack Jansen9513f2c1995-10-27 13:21:28 +0000542 }
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000543 return v;
544
545 case TYPE_LIST:
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000546 n = r_long(p);
Guido van Rossuma45cb451998-06-08 20:27:29 +0000547 if (n < 0) {
548 PyErr_SetString(PyExc_ValueError, "bad marshal data");
549 return NULL;
550 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000551 v = PyList_New((int)n);
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000552 if (v == NULL)
553 return v;
Jack Jansen9513f2c1995-10-27 13:21:28 +0000554 for (i = 0; i < n; i++) {
555 v2 = r_object(p);
556 if ( v2 == NULL ) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000557 Py_DECREF(v);
Jack Jansen9513f2c1995-10-27 13:21:28 +0000558 v = NULL;
559 break;
560 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000561 PyList_SetItem(v, (int)i, v2);
Jack Jansen9513f2c1995-10-27 13:21:28 +0000562 }
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000563 return v;
564
Guido van Rossum64b45521991-06-07 13:58:22 +0000565 case TYPE_DICT:
Guido van Rossum79f25d91997-04-29 20:08:16 +0000566 v = PyDict_New();
Guido van Rossum64b45521991-06-07 13:58:22 +0000567 if (v == NULL)
568 return NULL;
569 for (;;) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000570 PyObject *key, *val;
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000571 key = r_object(p);
Guido van Rossum64b45521991-06-07 13:58:22 +0000572 if (key == NULL)
Guido van Rossumf2150601996-06-26 20:41:23 +0000573 break; /* XXX Assume TYPE_NULL, not an error */
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000574 val = r_object(p);
Guido van Rossumf2150601996-06-26 20:41:23 +0000575 if (val != NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000576 PyDict_SetItem(v, key, val);
577 Py_DECREF(key);
578 Py_XDECREF(val);
Guido van Rossum64b45521991-06-07 13:58:22 +0000579 }
580 return v;
581
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000582 case TYPE_CODE:
583 {
Guido van Rossum681d79a1995-07-18 14:51:37 +0000584 int argcount = r_short(p);
585 int nlocals = r_short(p);
Guido van Rossum98a9b311997-01-17 21:07:08 +0000586 int stacksize = r_short(p);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000587 int flags = r_short(p);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000588 PyObject *code = NULL;
589 PyObject *consts = NULL;
590 PyObject *names = NULL;
591 PyObject *varnames = NULL;
592 PyObject *filename = NULL;
593 PyObject *name = NULL;
Guido van Rossum0f4bbd21997-02-14 21:12:56 +0000594 int firstlineno = 0;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000595 PyObject *lnotab = NULL;
Jack Jansen9513f2c1995-10-27 13:21:28 +0000596
597 code = r_object(p);
598 if (code) consts = r_object(p);
599 if (consts) names = r_object(p);
600 if (names) varnames = r_object(p);
601 if (varnames) filename = r_object(p);
602 if (filename) name = r_object(p);
Guido van Rossumd031c891997-01-24 03:44:17 +0000603 if (name) {
604 firstlineno = r_short(p);
605 lnotab = r_object(p);
606 }
Jack Jansen9513f2c1995-10-27 13:21:28 +0000607
Guido van Rossum79f25d91997-04-29 20:08:16 +0000608 if (!PyErr_Occurred()) {
609 v = (PyObject *) PyCode_New(
Guido van Rossum98a9b311997-01-17 21:07:08 +0000610 argcount, nlocals, stacksize, flags,
Guido van Rossum681d79a1995-07-18 14:51:37 +0000611 code, consts, names, varnames,
Guido van Rossumd031c891997-01-24 03:44:17 +0000612 filename, name, firstlineno, lnotab);
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000613 }
614 else
615 v = NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000616 Py_XDECREF(code);
617 Py_XDECREF(consts);
618 Py_XDECREF(names);
619 Py_XDECREF(varnames);
620 Py_XDECREF(filename);
621 Py_XDECREF(name);
Guido van Rossum6fc06e71997-07-26 23:30:18 +0000622 Py_XDECREF(lnotab);
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000623
624 }
625 return v;
626
627 default:
Guido van Rossumf2150601996-06-26 20:41:23 +0000628 /* Bogus data got written, which isn't ideal.
629 This will let you keep working and recover. */
Guido van Rossuma45cb451998-06-08 20:27:29 +0000630 PyErr_SetString(PyExc_ValueError, "bad marshal data");
631 return NULL;
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000632
633 }
634}
635
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000636long
Guido van Rossum79f25d91997-04-29 20:08:16 +0000637PyMarshal_ReadLongFromFile(fp)
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000638 FILE *fp;
639{
640 RFILE rf;
641 rf.fp = fp;
642 return r_long(&rf);
643}
644
Guido van Rossum79f25d91997-04-29 20:08:16 +0000645PyObject *
646PyMarshal_ReadObjectFromFile(fp)
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000647 FILE *fp;
648{
649 RFILE rf;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000650 if (PyErr_Occurred()) {
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000651 fprintf(stderr, "XXX rd_object called with exception set\n");
652 return NULL;
653 }
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000654 rf.fp = fp;
655 return r_object(&rf);
656}
657
Guido van Rossum79f25d91997-04-29 20:08:16 +0000658PyObject *
659PyMarshal_ReadObjectFromString(str, len)
Guido van Rossumf56e3db1993-04-01 20:59:32 +0000660 char *str;
661 int len;
662{
663 RFILE rf;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000664 if (PyErr_Occurred()) {
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000665 fprintf(stderr, "XXX rds_object called with exception set\n");
666 return NULL;
667 }
Guido van Rossumf56e3db1993-04-01 20:59:32 +0000668 rf.fp = NULL;
669 rf.str = NULL;
670 rf.ptr = str;
671 rf.end = str + len;
672 return r_object(&rf);
673}
674
Guido van Rossum79f25d91997-04-29 20:08:16 +0000675PyObject *
Guido van Rossum3f3bb3d1996-08-19 22:07:17 +0000676PyMarshal_WriteObjectToString(x) /* wrs_object() */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000677 PyObject *x;
Guido van Rossum3f3bb3d1996-08-19 22:07:17 +0000678{
679 WFILE wf;
680 wf.fp = NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000681 wf.str = PyString_FromStringAndSize((char *)NULL, 50);
Guido van Rossum3f3bb3d1996-08-19 22:07:17 +0000682 if (wf.str == NULL)
683 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000684 wf.ptr = PyString_AS_STRING((PyStringObject *)wf.str);
685 wf.end = wf.ptr + PyString_Size(wf.str);
Guido van Rossum3f3bb3d1996-08-19 22:07:17 +0000686 wf.error = 0;
Fred Drake6da0b912000-06-28 18:47:56 +0000687 wf.depth = 0;
Guido van Rossum3f3bb3d1996-08-19 22:07:17 +0000688 w_object(x, &wf);
689 if (wf.str != NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000690 _PyString_Resize(&wf.str,
691 (int) (wf.ptr -
692 PyString_AS_STRING((PyStringObject *)wf.str)));
Guido van Rossum3f3bb3d1996-08-19 22:07:17 +0000693 if (wf.error) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000694 Py_XDECREF(wf.str);
Fred Drake6da0b912000-06-28 18:47:56 +0000695 PyErr_SetString(PyExc_ValueError,
696 (wf.error==1)?"unmarshallable object"
697 :"object too deeply nested to marshal");
Guido van Rossum3f3bb3d1996-08-19 22:07:17 +0000698 return NULL;
699 }
700 return wf.str;
701}
702
Guido van Rossum64b45521991-06-07 13:58:22 +0000703/* And an interface for Python programs... */
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000704
Guido van Rossum79f25d91997-04-29 20:08:16 +0000705static PyObject *
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000706marshal_dump(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000707 PyObject *self;
708 PyObject *args;
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000709{
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000710 WFILE wf;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000711 PyObject *x;
712 PyObject *f;
Guido van Rossum2efa3692000-03-31 00:37:41 +0000713 if (!PyArg_ParseTuple(args, "OO:dump", &x, &f))
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000714 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000715 if (!PyFile_Check(f)) {
716 PyErr_SetString(PyExc_TypeError,
717 "marshal.dump() 2nd arg must be file");
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000718 return NULL;
719 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000720 wf.fp = PyFile_AsFile(f);
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000721 wf.str = NULL;
722 wf.ptr = wf.end = NULL;
Guido van Rossumf2150601996-06-26 20:41:23 +0000723 wf.error = 0;
Fred Drake6da0b912000-06-28 18:47:56 +0000724 wf.depth = 0;
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000725 w_object(x, &wf);
Guido van Rossumf2150601996-06-26 20:41:23 +0000726 if (wf.error) {
Fred Drake6da0b912000-06-28 18:47:56 +0000727 PyErr_SetString(PyExc_ValueError,
728 (wf.error==1)?"unmarshallable object"
729 :"object too deeply nested to marshal");
Guido van Rossumf2150601996-06-26 20:41:23 +0000730 return NULL;
731 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000732 Py_INCREF(Py_None);
733 return Py_None;
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000734}
735
Guido van Rossum79f25d91997-04-29 20:08:16 +0000736static PyObject *
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000737marshal_load(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000738 PyObject *self;
739 PyObject *args;
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000740{
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000741 RFILE rf;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000742 PyObject *f;
743 PyObject *v;
Guido van Rossum2efa3692000-03-31 00:37:41 +0000744 if (!PyArg_ParseTuple(args, "O:load", &f))
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000745 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000746 if (!PyFile_Check(f)) {
747 PyErr_SetString(PyExc_TypeError,
748 "marshal.load() arg must be file");
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000749 return NULL;
750 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000751 rf.fp = PyFile_AsFile(f);
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000752 rf.str = NULL;
753 rf.ptr = rf.end = NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000754 PyErr_Clear();
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000755 v = r_object(&rf);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000756 if (PyErr_Occurred()) {
757 Py_XDECREF(v);
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000758 v = NULL;
759 }
760 return v;
761}
762
Guido van Rossum79f25d91997-04-29 20:08:16 +0000763static PyObject *
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000764marshal_dumps(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000765 PyObject *self;
766 PyObject *args;
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000767{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000768 PyObject *x;
Guido van Rossum2efa3692000-03-31 00:37:41 +0000769 if (!PyArg_ParseTuple(args, "O:dumps", &x))
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000770 return NULL;
Guido van Rossum3f3bb3d1996-08-19 22:07:17 +0000771 return PyMarshal_WriteObjectToString(x);
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000772}
773
Guido van Rossum79f25d91997-04-29 20:08:16 +0000774static PyObject *
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000775marshal_loads(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000776 PyObject *self;
777 PyObject *args;
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000778{
779 RFILE rf;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000780 PyObject *v;
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000781 char *s;
782 int n;
Guido van Rossum2efa3692000-03-31 00:37:41 +0000783 if (!PyArg_ParseTuple(args, "s#:loads", &s, &n))
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000784 return NULL;
785 rf.fp = NULL;
786 rf.str = args;
787 rf.ptr = s;
788 rf.end = s + n;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000789 PyErr_Clear();
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000790 v = r_object(&rf);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000791 if (PyErr_Occurred()) {
792 Py_XDECREF(v);
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000793 v = NULL;
794 }
795 return v;
796}
797
Guido van Rossum79f25d91997-04-29 20:08:16 +0000798static PyMethodDef marshal_methods[] = {
Guido van Rossum2efa3692000-03-31 00:37:41 +0000799 {"dump", marshal_dump, 1},
800 {"load", marshal_load, 1},
801 {"dumps", marshal_dumps, 1},
802 {"loads", marshal_loads, 1},
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000803 {NULL, NULL} /* sentinel */
804};
805
806void
Guido van Rossum79f25d91997-04-29 20:08:16 +0000807PyMarshal_Init()
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000808{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000809 (void) Py_InitModule("marshal", marshal_methods);
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000810}