blob: 120c3fade67a8f8dc2dce81edf6f8ee539319fcb [file] [log] [blame]
Guido van Rossumdce2e3d1991-06-04 19:42:30 +00001
2/* Write Python objects to files and read them back.
3 This is intended for writing and reading compiled Python code only;
4 a true persistent storage facility would be much harder, since
5 it would have to take circular links and sharing into account. */
6
Guido van Rossum79f25d91997-04-29 20:08:16 +00007#include "Python.h"
Guido van Rossumdce2e3d1991-06-04 19:42:30 +00008#include "longintrepr.h"
9#include "compile.h"
10#include "marshal.h"
11
Fred Drake6da0b912000-06-28 18:47:56 +000012/* High water mark to determine when the marshalled object is dangerously deep
13 * and risks coring the interpreter. When the object stack gets this deep,
14 * raise an exception instead of continuing.
15 */
16#define MAX_MARSHAL_STACK_DEPTH 5000
17
Guido van Rossumdce2e3d1991-06-04 19:42:30 +000018#define TYPE_NULL '0'
19#define TYPE_NONE 'N'
Guido van Rossume449af71996-10-11 16:25:41 +000020#define TYPE_ELLIPSIS '.'
Guido van Rossumdce2e3d1991-06-04 19:42:30 +000021#define TYPE_INT 'i'
Guido van Rossumb0c168c1996-12-05 23:15:02 +000022#define TYPE_INT64 'I'
Guido van Rossumdce2e3d1991-06-04 19:42:30 +000023#define TYPE_FLOAT 'f'
Guido van Rossum8a5c5d21996-01-12 01:09:56 +000024#define TYPE_COMPLEX 'x'
Guido van Rossumdce2e3d1991-06-04 19:42:30 +000025#define TYPE_LONG 'l'
26#define TYPE_STRING 's'
27#define TYPE_TUPLE '('
28#define TYPE_LIST '['
29#define TYPE_DICT '{'
Guido van Rossum681d79a1995-07-18 14:51:37 +000030#define TYPE_CODE 'c'
Guido van Rossumc279b532000-03-10 23:03:02 +000031#define TYPE_UNICODE 'u'
Guido van Rossumdce2e3d1991-06-04 19:42:30 +000032#define TYPE_UNKNOWN '?'
33
Guido van Rossum0b0db8e1993-01-21 16:07:51 +000034typedef struct {
35 FILE *fp;
Guido van Rossumf2150601996-06-26 20:41:23 +000036 int error;
Fred Drake6da0b912000-06-28 18:47:56 +000037 int depth;
Guido van Rossum0b0db8e1993-01-21 16:07:51 +000038 /* If fp == NULL, the following are valid: */
Guido van Rossum79f25d91997-04-29 20:08:16 +000039 PyObject *str;
Guido van Rossum0b0db8e1993-01-21 16:07:51 +000040 char *ptr;
41 char *end;
42} WFILE;
Guido van Rossumdce2e3d1991-06-04 19:42:30 +000043
Guido van Rossum0b0db8e1993-01-21 16:07:51 +000044#define w_byte(c, p) if (((p)->fp)) putc((c), (p)->fp); \
45 else if ((p)->ptr != (p)->end) *(p)->ptr++ = (c); \
46 else w_more(c, p)
47
48static void
Fredrik Lundh11534382000-07-23 18:24:06 +000049w_more(int c, WFILE *p)
Guido van Rossum0b0db8e1993-01-21 16:07:51 +000050{
51 int size, newsize;
52 if (p->str == NULL)
53 return; /* An error already occurred */
Guido van Rossum79f25d91997-04-29 20:08:16 +000054 size = PyString_Size(p->str);
Guido van Rossum0b0db8e1993-01-21 16:07:51 +000055 newsize = size + 1024;
Guido van Rossum79f25d91997-04-29 20:08:16 +000056 if (_PyString_Resize(&p->str, newsize) != 0) {
Guido van Rossum0b0db8e1993-01-21 16:07:51 +000057 p->ptr = p->end = NULL;
58 }
59 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +000060 p->ptr = PyString_AS_STRING((PyStringObject *)p->str) + size;
61 p->end =
62 PyString_AS_STRING((PyStringObject *)p->str) + newsize;
Tim Peters8315ea52000-07-23 19:28:35 +000063 *p->ptr++ = Py_SAFE_DOWNCAST(c, int, char);
Guido van Rossum0b0db8e1993-01-21 16:07:51 +000064 }
65}
66
67static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000068w_string(char *s, int n, WFILE *p)
Guido van Rossum0b0db8e1993-01-21 16:07:51 +000069{
70 if (p->fp != NULL) {
71 fwrite(s, 1, n, p->fp);
72 }
73 else {
74 while (--n >= 0) {
75 w_byte(*s, p);
76 s++;
77 }
78 }
79}
80
81static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000082w_short(int x, WFILE *p)
Guido van Rossumdce2e3d1991-06-04 19:42:30 +000083{
Guido van Rossum0b0db8e1993-01-21 16:07:51 +000084 w_byte( x & 0xff, p);
85 w_byte((x>> 8) & 0xff, p);
Guido van Rossumdce2e3d1991-06-04 19:42:30 +000086}
87
Guido van Rossum0b0db8e1993-01-21 16:07:51 +000088static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000089w_long(long x, WFILE *p)
Guido van Rossumdce2e3d1991-06-04 19:42:30 +000090{
Guido van Rossum0b0db8e1993-01-21 16:07:51 +000091 w_byte((int)( x & 0xff), p);
92 w_byte((int)((x>> 8) & 0xff), p);
93 w_byte((int)((x>>16) & 0xff), p);
94 w_byte((int)((x>>24) & 0xff), p);
Guido van Rossumdce2e3d1991-06-04 19:42:30 +000095}
96
Guido van Rossumc1547d91996-12-10 15:39:04 +000097#if SIZEOF_LONG > 4
Guido van Rossum0b0db8e1993-01-21 16:07:51 +000098static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000099w_long64(long x, WFILE *p)
Guido van Rossumb0c168c1996-12-05 23:15:02 +0000100{
101 w_long(x, p);
Guido van Rossumc1547d91996-12-10 15:39:04 +0000102 w_long(x>>32, p);
Guido van Rossumb0c168c1996-12-05 23:15:02 +0000103}
Guido van Rossumc1547d91996-12-10 15:39:04 +0000104#endif
Guido van Rossumb0c168c1996-12-05 23:15:02 +0000105
106static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000107w_object(PyObject *v, WFILE *p)
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000108{
Guido van Rossum3a205f71995-02-17 15:10:07 +0000109 int i, n;
Guido van Rossumd076c731998-10-07 19:42:25 +0000110 PyBufferProcs *pb;
Fred Drake6da0b912000-06-28 18:47:56 +0000111
112 p->depth++;
Tim Petersd9b9ac82001-01-28 00:27:39 +0000113
Fred Drake6da0b912000-06-28 18:47:56 +0000114 if (p->depth > MAX_MARSHAL_STACK_DEPTH) {
115 p->error = 2;
Tim Petersd9b9ac82001-01-28 00:27:39 +0000116 }
Fred Drake6da0b912000-06-28 18:47:56 +0000117 else if (v == NULL) {
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000118 w_byte(TYPE_NULL, p);
Guido van Rossum730806d1998-04-10 22:27:42 +0000119 }
120 else if (v == Py_None) {
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000121 w_byte(TYPE_NONE, p);
Guido van Rossum730806d1998-04-10 22:27:42 +0000122 }
123 else if (v == Py_Ellipsis) {
124 w_byte(TYPE_ELLIPSIS, p);
125 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000126 else if (PyInt_Check(v)) {
127 long x = PyInt_AS_LONG((PyIntObject *)v);
Guido van Rossumc1547d91996-12-10 15:39:04 +0000128#if SIZEOF_LONG > 4
Tim Peters44714002001-04-10 05:02:52 +0000129 long y = Py_ARITHMETIC_RIGHT_SHIFT(long, x, 31);
Guido van Rossumb0c168c1996-12-05 23:15:02 +0000130 if (y && y != -1) {
131 w_byte(TYPE_INT64, p);
132 w_long64(x, p);
133 }
Guido van Rossumc1547d91996-12-10 15:39:04 +0000134 else
135#endif
136 {
Guido van Rossumb0c168c1996-12-05 23:15:02 +0000137 w_byte(TYPE_INT, p);
138 w_long(x, p);
139 }
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000140 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000141 else if (PyLong_Check(v)) {
142 PyLongObject *ob = (PyLongObject *)v;
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000143 w_byte(TYPE_LONG, p);
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000144 n = ob->ob_size;
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000145 w_long((long)n, p);
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000146 if (n < 0)
147 n = -n;
148 for (i = 0; i < n; i++)
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000149 w_short(ob->ob_digit[i], p);
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000150 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000151 else if (PyFloat_Check(v)) {
Tim Petersdbd9ba62000-07-09 03:09:57 +0000152 extern void PyFloat_AsString(char *, PyFloatObject *);
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000153 char buf[256]; /* Plenty to format any double */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000154 PyFloat_AsString(buf, (PyFloatObject *)v);
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000155 n = strlen(buf);
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000156 w_byte(TYPE_FLOAT, p);
Guido van Rossum3a205f71995-02-17 15:10:07 +0000157 w_byte(n, p);
158 w_string(buf, n, p);
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000159 }
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000160#ifndef WITHOUT_COMPLEX
Guido van Rossum79f25d91997-04-29 20:08:16 +0000161 else if (PyComplex_Check(v)) {
Tim Petersdbd9ba62000-07-09 03:09:57 +0000162 extern void PyFloat_AsString(char *, PyFloatObject *);
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000163 char buf[256]; /* Plenty to format any double */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000164 PyFloatObject *temp;
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000165 w_byte(TYPE_COMPLEX, p);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000166 temp = (PyFloatObject*)PyFloat_FromDouble(
167 PyComplex_RealAsDouble(v));
168 PyFloat_AsString(buf, temp);
169 Py_DECREF(temp);
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000170 n = strlen(buf);
171 w_byte(n, p);
172 w_string(buf, n, p);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000173 temp = (PyFloatObject*)PyFloat_FromDouble(
174 PyComplex_ImagAsDouble(v));
175 PyFloat_AsString(buf, temp);
176 Py_DECREF(temp);
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000177 n = strlen(buf);
178 w_byte(n, p);
179 w_string(buf, n, p);
180 }
181#endif
Guido van Rossum79f25d91997-04-29 20:08:16 +0000182 else if (PyString_Check(v)) {
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000183 w_byte(TYPE_STRING, p);
Guido van Rossumc279b532000-03-10 23:03:02 +0000184 n = PyString_GET_SIZE(v);
Guido van Rossum3a205f71995-02-17 15:10:07 +0000185 w_long((long)n, p);
Guido van Rossumc279b532000-03-10 23:03:02 +0000186 w_string(PyString_AS_STRING(v), n, p);
187 }
188 else if (PyUnicode_Check(v)) {
189 PyObject *utf8;
190 utf8 = PyUnicode_AsUTF8String(v);
191 if (utf8 == NULL) {
Guido van Rossum98626cd2000-06-28 23:24:19 +0000192 p->depth--;
193 p->error = 1;
194 return;
Guido van Rossumc279b532000-03-10 23:03:02 +0000195 }
196 w_byte(TYPE_UNICODE, p);
197 n = PyString_GET_SIZE(utf8);
198 w_long((long)n, p);
199 w_string(PyString_AS_STRING(utf8), n, p);
200 Py_DECREF(utf8);
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000201 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000202 else if (PyTuple_Check(v)) {
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000203 w_byte(TYPE_TUPLE, p);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000204 n = PyTuple_Size(v);
Guido van Rossum3a205f71995-02-17 15:10:07 +0000205 w_long((long)n, p);
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000206 for (i = 0; i < n; i++) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000207 w_object(PyTuple_GET_ITEM(v, i), p);
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000208 }
209 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000210 else if (PyList_Check(v)) {
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000211 w_byte(TYPE_LIST, p);
Guido van Rossumc279b532000-03-10 23:03:02 +0000212 n = PyList_GET_SIZE(v);
Guido van Rossum3a205f71995-02-17 15:10:07 +0000213 w_long((long)n, p);
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000214 for (i = 0; i < n; i++) {
Guido van Rossumc279b532000-03-10 23:03:02 +0000215 w_object(PyList_GET_ITEM(v, i), p);
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000216 }
217 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000218 else if (PyDict_Check(v)) {
Guido van Rossum25831651993-05-19 14:50:45 +0000219 int pos;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000220 PyObject *key, *value;
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000221 w_byte(TYPE_DICT, p);
Guido van Rossum64b45521991-06-07 13:58:22 +0000222 /* This one is NULL object terminated! */
Guido van Rossum25831651993-05-19 14:50:45 +0000223 pos = 0;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000224 while (PyDict_Next(v, &pos, &key, &value)) {
Guido van Rossum25831651993-05-19 14:50:45 +0000225 w_object(key, p);
226 w_object(value, p);
Guido van Rossum64b45521991-06-07 13:58:22 +0000227 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000228 w_object((PyObject *)NULL, p);
Guido van Rossum64b45521991-06-07 13:58:22 +0000229 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000230 else if (PyCode_Check(v)) {
231 PyCodeObject *co = (PyCodeObject *)v;
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000232 w_byte(TYPE_CODE, p);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000233 w_short(co->co_argcount, p);
234 w_short(co->co_nlocals, p);
Guido van Rossum98a9b311997-01-17 21:07:08 +0000235 w_short(co->co_stacksize, p);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000236 w_short(co->co_flags, p);
Guido van Rossumd076c731998-10-07 19:42:25 +0000237 w_object(co->co_code, p);
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000238 w_object(co->co_consts, p);
239 w_object(co->co_names, p);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000240 w_object(co->co_varnames, p);
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000241 w_object(co->co_freevars, p);
242 w_object(co->co_cellvars, p);
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000243 w_object(co->co_filename, p);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000244 w_object(co->co_name, p);
Guido van Rossumd031c891997-01-24 03:44:17 +0000245 w_short(co->co_firstlineno, p);
246 w_object(co->co_lnotab, p);
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000247 }
Guido van Rossumd076c731998-10-07 19:42:25 +0000248 else if ((pb = v->ob_type->tp_as_buffer) != NULL &&
249 pb->bf_getsegcount != NULL &&
250 pb->bf_getreadbuffer != NULL &&
251 (*pb->bf_getsegcount)(v, NULL) == 1)
252 {
253 /* Write unknown buffer-style objects as a string */
254 char *s;
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);
259 }
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000260 else {
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000261 w_byte(TYPE_UNKNOWN, p);
Guido van Rossumf2150601996-06-26 20:41:23 +0000262 p->error = 1;
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000263 }
Guido van Rossum98626cd2000-06-28 23:24:19 +0000264
265 p->depth--;
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000266}
267
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000268void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000269PyMarshal_WriteLongToFile(long x, FILE *fp)
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000270{
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000271 WFILE wf;
272 wf.fp = fp;
Guido van Rossumf2150601996-06-26 20:41:23 +0000273 wf.error = 0;
Fred Drake6da0b912000-06-28 18:47:56 +0000274 wf.depth = 0;
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000275 w_long(x, &wf);
276}
277
278void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000279PyMarshal_WriteObjectToFile(PyObject *x, FILE *fp)
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000280{
281 WFILE wf;
282 wf.fp = fp;
Guido van Rossumf2150601996-06-26 20:41:23 +0000283 wf.error = 0;
Guido van Rossum98626cd2000-06-28 23:24:19 +0000284 wf.depth = 0;
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000285 w_object(x, &wf);
286}
287
288typedef WFILE RFILE; /* Same struct with different invariants */
289
Guido van Rossum8d617a61995-03-09 12:12:11 +0000290#define rs_byte(p) (((p)->ptr != (p)->end) ? (unsigned char)*(p)->ptr++ : EOF)
291
292#define r_byte(p) ((p)->fp ? getc((p)->fp) : rs_byte(p))
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000293
294static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000295r_string(char *s, int n, RFILE *p)
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000296{
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;
304}
305
306static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000307r_short(RFILE *p)
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000308{
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000309 register short x;
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000310 x = r_byte(p);
311 x |= r_byte(p) << 8;
Tim Peterse84b7402000-09-19 08:54:13 +0000312 /* Sign-extension, in case short greater than 16 bits */
313 x |= -(x & 0x8000);
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000314 return x;
315}
316
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000317static long
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000318r_long(RFILE *p)
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000319{
320 register long x;
Guido van Rossum8d617a61995-03-09 12:12:11 +0000321 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;
327 }
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;
333 }
Guido van Rossumc1547d91996-12-10 15:39:04 +0000334#if SIZEOF_LONG > 4
Guido van Rossumb0c168c1996-12-05 23:15:02 +0000335 /* Sign extension for 64-bit machines */
Tim Peterse84b7402000-09-19 08:54:13 +0000336 x |= -(x & 0x80000000L);
Guido van Rossumc1547d91996-12-10 15:39:04 +0000337#endif
Guido van Rossumb0c168c1996-12-05 23:15:02 +0000338 return x;
339}
340
341static long
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000342r_long64(RFILE *p)
Guido van Rossumb0c168c1996-12-05 23:15:02 +0000343{
344 register long x;
Guido van Rossumc1547d91996-12-10 15:39:04 +0000345 x = r_long(p);
346#if SIZEOF_LONG > 4
Tim Peterse84b7402000-09-19 08:54:13 +0000347 x = (x & 0xFFFFFFFFL) | (r_long(p) << 32);
Guido van Rossumc1547d91996-12-10 15:39:04 +0000348#else
349 if (r_long(p) != 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000350 PyObject *f = PySys_GetObject("stderr");
Guido van Rossumc1547d91996-12-10 15:39:04 +0000351 if (f != NULL)
Guido van Rossum7e8d26d1997-05-22 22:35:47 +0000352 (void) PyFile_WriteString(
Guido van Rossumc1547d91996-12-10 15:39:04 +0000353 "Warning: un-marshal 64-bit int in 32-bit mode\n",
354 f);
Guido van Rossumb0c168c1996-12-05 23:15:02 +0000355 }
Guido van Rossumc1547d91996-12-10 15:39:04 +0000356#endif
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000357 return x;
358}
359
Guido van Rossum79f25d91997-04-29 20:08:16 +0000360static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000361r_object(RFILE *p)
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000362{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000363 PyObject *v, *v2;
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000364 long i, n;
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000365 int type = r_byte(p);
Tim Petersd9b9ac82001-01-28 00:27:39 +0000366
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000367 switch (type) {
Tim Petersd9b9ac82001-01-28 00:27:39 +0000368
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000369 case EOF:
Guido van Rossum79f25d91997-04-29 20:08:16 +0000370 PyErr_SetString(PyExc_EOFError,
371 "EOF read where object expected");
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000372 return NULL;
Tim Petersd9b9ac82001-01-28 00:27:39 +0000373
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000374 case TYPE_NULL:
375 return NULL;
Tim Petersd9b9ac82001-01-28 00:27:39 +0000376
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000377 case TYPE_NONE:
Guido van Rossum79f25d91997-04-29 20:08:16 +0000378 Py_INCREF(Py_None);
379 return Py_None;
Tim Petersd9b9ac82001-01-28 00:27:39 +0000380
Guido van Rossume449af71996-10-11 16:25:41 +0000381 case TYPE_ELLIPSIS:
Guido van Rossum79f25d91997-04-29 20:08:16 +0000382 Py_INCREF(Py_Ellipsis);
Guido van Rossume449af71996-10-11 16:25:41 +0000383 return Py_Ellipsis;
Tim Petersd9b9ac82001-01-28 00:27:39 +0000384
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000385 case TYPE_INT:
Guido van Rossum79f25d91997-04-29 20:08:16 +0000386 return PyInt_FromLong(r_long(p));
Tim Petersd9b9ac82001-01-28 00:27:39 +0000387
Guido van Rossumb0c168c1996-12-05 23:15:02 +0000388 case TYPE_INT64:
Guido van Rossum79f25d91997-04-29 20:08:16 +0000389 return PyInt_FromLong(r_long64(p));
Tim Petersd9b9ac82001-01-28 00:27:39 +0000390
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000391 case TYPE_LONG:
392 {
393 int size;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000394 PyLongObject *ob;
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000395 n = r_long(p);
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000396 size = n<0 ? -n : n;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000397 ob = _PyLong_New(size);
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000398 if (ob == NULL)
399 return NULL;
400 ob->ob_size = n;
401 for (i = 0; i < size; i++)
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000402 ob->ob_digit[i] = r_short(p);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000403 return (PyObject *)ob;
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000404 }
Tim Petersd9b9ac82001-01-28 00:27:39 +0000405
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000406 case TYPE_FLOAT:
407 {
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000408 char buf[256];
Guido van Rossum0ae748d1997-02-14 22:58:07 +0000409 double dx;
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000410 n = r_byte(p);
411 if (r_string(buf, (int)n, p) != n) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000412 PyErr_SetString(PyExc_EOFError,
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000413 "EOF read where object expected");
414 return NULL;
415 }
416 buf[n] = '\0';
Guido van Rossum0ae748d1997-02-14 22:58:07 +0000417 PyFPE_START_PROTECT("atof", return 0)
418 dx = atof(buf);
Guido van Rossum45b83911997-03-14 04:32:50 +0000419 PyFPE_END_PROTECT(dx)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000420 return PyFloat_FromDouble(dx);
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000421 }
Tim Petersd9b9ac82001-01-28 00:27:39 +0000422
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000423#ifndef WITHOUT_COMPLEX
424 case TYPE_COMPLEX:
425 {
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000426 char buf[256];
Guido van Rossum530956d1996-07-21 02:27:43 +0000427 Py_complex c;
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000428 n = r_byte(p);
429 if (r_string(buf, (int)n, p) != n) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000430 PyErr_SetString(PyExc_EOFError,
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000431 "EOF read where object expected");
432 return NULL;
433 }
434 buf[n] = '\0';
Guido van Rossum0ae748d1997-02-14 22:58:07 +0000435 PyFPE_START_PROTECT("atof", return 0)
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000436 c.real = atof(buf);
Guido van Rossum45b83911997-03-14 04:32:50 +0000437 PyFPE_END_PROTECT(c)
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000438 n = r_byte(p);
439 if (r_string(buf, (int)n, p) != n) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000440 PyErr_SetString(PyExc_EOFError,
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000441 "EOF read where object expected");
442 return NULL;
443 }
444 buf[n] = '\0';
Guido van Rossum0ae748d1997-02-14 22:58:07 +0000445 PyFPE_START_PROTECT("atof", return 0)
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000446 c.imag = atof(buf);
Guido van Rossum45b83911997-03-14 04:32:50 +0000447 PyFPE_END_PROTECT(c)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000448 return PyComplex_FromCComplex(c);
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000449 }
450#endif
Tim Petersd9b9ac82001-01-28 00:27:39 +0000451
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000452 case TYPE_STRING:
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000453 n = r_long(p);
Guido van Rossuma45cb451998-06-08 20:27:29 +0000454 if (n < 0) {
455 PyErr_SetString(PyExc_ValueError, "bad marshal data");
456 return NULL;
457 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000458 v = PyString_FromStringAndSize((char *)NULL, n);
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000459 if (v != NULL) {
Guido van Rossumc279b532000-03-10 23:03:02 +0000460 if (r_string(PyString_AS_STRING(v), (int)n, p) != n) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000461 Py_DECREF(v);
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000462 v = NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000463 PyErr_SetString(PyExc_EOFError,
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000464 "EOF read where object expected");
465 }
466 }
467 return v;
Tim Petersd9b9ac82001-01-28 00:27:39 +0000468
Guido van Rossumc279b532000-03-10 23:03:02 +0000469 case TYPE_UNICODE:
470 {
471 char *buffer;
472
473 n = r_long(p);
474 if (n < 0) {
475 PyErr_SetString(PyExc_ValueError, "bad marshal data");
476 return NULL;
477 }
Guido van Rossumb18618d2000-05-03 23:44:39 +0000478 buffer = PyMem_NEW(char, n);
Guido van Rossumc279b532000-03-10 23:03:02 +0000479 if (buffer == NULL)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000480 return PyErr_NoMemory();
Guido van Rossumc279b532000-03-10 23:03:02 +0000481 if (r_string(buffer, (int)n, p) != n) {
Guido van Rossumb18618d2000-05-03 23:44:39 +0000482 PyMem_DEL(buffer);
Guido van Rossumc279b532000-03-10 23:03:02 +0000483 PyErr_SetString(PyExc_EOFError,
484 "EOF read where object expected");
485 return NULL;
486 }
487 v = PyUnicode_DecodeUTF8(buffer, n, NULL);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000488 PyMem_DEL(buffer);
Guido van Rossumc279b532000-03-10 23:03:02 +0000489 return v;
490 }
Tim Petersd9b9ac82001-01-28 00:27:39 +0000491
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000492 case TYPE_TUPLE:
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000493 n = r_long(p);
Guido van Rossuma45cb451998-06-08 20:27:29 +0000494 if (n < 0) {
495 PyErr_SetString(PyExc_ValueError, "bad marshal data");
496 return NULL;
497 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000498 v = PyTuple_New((int)n);
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000499 if (v == NULL)
500 return v;
Jack Jansen9513f2c1995-10-27 13:21:28 +0000501 for (i = 0; i < n; i++) {
502 v2 = r_object(p);
503 if ( v2 == NULL ) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000504 Py_DECREF(v);
Jack Jansen9513f2c1995-10-27 13:21:28 +0000505 v = NULL;
506 break;
507 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000508 PyTuple_SET_ITEM(v, (int)i, v2);
Jack Jansen9513f2c1995-10-27 13:21:28 +0000509 }
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000510 return v;
Tim Petersd9b9ac82001-01-28 00:27:39 +0000511
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000512 case TYPE_LIST:
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000513 n = r_long(p);
Guido van Rossuma45cb451998-06-08 20:27:29 +0000514 if (n < 0) {
515 PyErr_SetString(PyExc_ValueError, "bad marshal data");
516 return NULL;
517 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000518 v = PyList_New((int)n);
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000519 if (v == NULL)
520 return v;
Jack Jansen9513f2c1995-10-27 13:21:28 +0000521 for (i = 0; i < n; i++) {
522 v2 = r_object(p);
523 if ( v2 == NULL ) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000524 Py_DECREF(v);
Jack Jansen9513f2c1995-10-27 13:21:28 +0000525 v = NULL;
526 break;
527 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000528 PyList_SetItem(v, (int)i, v2);
Jack Jansen9513f2c1995-10-27 13:21:28 +0000529 }
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000530 return v;
Tim Petersd9b9ac82001-01-28 00:27:39 +0000531
Guido van Rossum64b45521991-06-07 13:58:22 +0000532 case TYPE_DICT:
Guido van Rossum79f25d91997-04-29 20:08:16 +0000533 v = PyDict_New();
Guido van Rossum64b45521991-06-07 13:58:22 +0000534 if (v == NULL)
535 return NULL;
536 for (;;) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000537 PyObject *key, *val;
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000538 key = r_object(p);
Guido van Rossum64b45521991-06-07 13:58:22 +0000539 if (key == NULL)
Guido van Rossumf2150601996-06-26 20:41:23 +0000540 break; /* XXX Assume TYPE_NULL, not an error */
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000541 val = r_object(p);
Guido van Rossumf2150601996-06-26 20:41:23 +0000542 if (val != NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000543 PyDict_SetItem(v, key, val);
544 Py_DECREF(key);
545 Py_XDECREF(val);
Guido van Rossum64b45521991-06-07 13:58:22 +0000546 }
547 return v;
Tim Petersd9b9ac82001-01-28 00:27:39 +0000548
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000549 case TYPE_CODE:
550 {
Guido van Rossum681d79a1995-07-18 14:51:37 +0000551 int argcount = r_short(p);
552 int nlocals = r_short(p);
Guido van Rossum98a9b311997-01-17 21:07:08 +0000553 int stacksize = r_short(p);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000554 int flags = r_short(p);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000555 PyObject *code = NULL;
556 PyObject *consts = NULL;
557 PyObject *names = NULL;
558 PyObject *varnames = NULL;
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000559 PyObject *freevars = NULL;
560 PyObject *cellvars = NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000561 PyObject *filename = NULL;
562 PyObject *name = NULL;
Guido van Rossum0f4bbd21997-02-14 21:12:56 +0000563 int firstlineno = 0;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000564 PyObject *lnotab = NULL;
Tim Petersd9b9ac82001-01-28 00:27:39 +0000565
Jack Jansen9513f2c1995-10-27 13:21:28 +0000566 code = r_object(p);
567 if (code) consts = r_object(p);
568 if (consts) names = r_object(p);
569 if (names) varnames = r_object(p);
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000570 if (varnames) freevars = r_object(p);
571 if (freevars) cellvars = r_object(p);
572 if (cellvars) filename = r_object(p);
Jack Jansen9513f2c1995-10-27 13:21:28 +0000573 if (filename) name = r_object(p);
Guido van Rossumd031c891997-01-24 03:44:17 +0000574 if (name) {
575 firstlineno = r_short(p);
576 lnotab = r_object(p);
577 }
Tim Petersd9b9ac82001-01-28 00:27:39 +0000578
Guido van Rossum79f25d91997-04-29 20:08:16 +0000579 if (!PyErr_Occurred()) {
580 v = (PyObject *) PyCode_New(
Tim Petersd9b9ac82001-01-28 00:27:39 +0000581 argcount, nlocals, stacksize, flags,
Guido van Rossum681d79a1995-07-18 14:51:37 +0000582 code, consts, names, varnames,
Tim Petersd9b9ac82001-01-28 00:27:39 +0000583 freevars, cellvars, filename, name,
584 firstlineno, lnotab);
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000585 }
586 else
587 v = NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000588 Py_XDECREF(code);
589 Py_XDECREF(consts);
590 Py_XDECREF(names);
591 Py_XDECREF(varnames);
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000592 Py_XDECREF(freevars);
593 Py_XDECREF(cellvars);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000594 Py_XDECREF(filename);
595 Py_XDECREF(name);
Guido van Rossum6fc06e71997-07-26 23:30:18 +0000596 Py_XDECREF(lnotab);
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000597
598 }
599 return v;
Tim Petersd9b9ac82001-01-28 00:27:39 +0000600
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000601 default:
Guido van Rossumf2150601996-06-26 20:41:23 +0000602 /* Bogus data got written, which isn't ideal.
603 This will let you keep working and recover. */
Guido van Rossuma45cb451998-06-08 20:27:29 +0000604 PyErr_SetString(PyExc_ValueError, "bad marshal data");
605 return NULL;
Tim Petersd9b9ac82001-01-28 00:27:39 +0000606
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000607 }
608}
609
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000610long
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000611PyMarshal_ReadLongFromFile(FILE *fp)
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000612{
613 RFILE rf;
614 rf.fp = fp;
615 return r_long(&rf);
616}
617
Tim Peters691e0e92001-01-18 04:39:16 +0000618#ifdef HAVE_FSTAT
619/* Return size of file in bytes; < 0 if unknown. */
620static off_t
621getfilesize(FILE *fp)
622{
623 struct stat st;
624 if (fstat(fileno(fp), &st) != 0)
625 return -1;
626 else
627 return st.st_size;
628}
629#endif
Tim Petersd9b9ac82001-01-28 00:27:39 +0000630
Tim Peters691e0e92001-01-18 04:39:16 +0000631/* If we can get the size of the file up-front, and it's reasonably small,
632 * read it in one gulp and delegate to ...FromString() instead. Much quicker
633 * than reading a byte at a time from file; speeds .pyc imports.
Tim Petersd9b9ac82001-01-28 00:27:39 +0000634 * CAUTION: since this may read the entire remainder of the file, don't
635 * call it unless you know you're done with the file.
Tim Peters691e0e92001-01-18 04:39:16 +0000636 */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000637PyObject *
Tim Petersd9b9ac82001-01-28 00:27:39 +0000638PyMarshal_ReadLastObjectFromFile(FILE *fp)
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000639{
Tim Peters691e0e92001-01-18 04:39:16 +0000640/* 75% of 2.1's .pyc files can exploit SMALL_FILE_LIMIT.
641 * REASONABLE_FILE_LIMIT is by defn something big enough for Tkinter.pyc.
642 */
643#define SMALL_FILE_LIMIT (1L << 14)
644#define REASONABLE_FILE_LIMIT (1L << 18)
Tim Peters691e0e92001-01-18 04:39:16 +0000645#ifdef HAVE_FSTAT
646 off_t filesize;
647#endif
Guido van Rossum79f25d91997-04-29 20:08:16 +0000648 if (PyErr_Occurred()) {
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000649 fprintf(stderr, "XXX rd_object called with exception set\n");
650 return NULL;
651 }
Tim Peters691e0e92001-01-18 04:39:16 +0000652#ifdef HAVE_FSTAT
653 filesize = getfilesize(fp);
654 if (filesize > 0) {
655 char buf[SMALL_FILE_LIMIT];
656 char* pBuf = NULL;
657 if (filesize <= SMALL_FILE_LIMIT)
658 pBuf = buf;
659 else if (filesize <= REASONABLE_FILE_LIMIT)
660 pBuf = (char *)PyMem_MALLOC(filesize);
661 if (pBuf != NULL) {
662 PyObject* v;
663 size_t n = fread(pBuf, 1, filesize, fp);
664 v = PyMarshal_ReadObjectFromString(pBuf, n);
665 if (pBuf != buf)
666 PyMem_FREE(pBuf);
667 return v;
668 }
Tim Petersd9b9ac82001-01-28 00:27:39 +0000669
Tim Peters691e0e92001-01-18 04:39:16 +0000670 }
671#endif
Tim Petersd9b9ac82001-01-28 00:27:39 +0000672 /* We don't have fstat, or we do but the file is larger than
673 * REASONABLE_FILE_LIMIT or malloc failed -- read a byte at a time.
674 */
675 return PyMarshal_ReadObjectFromFile(fp);
676
Tim Peters691e0e92001-01-18 04:39:16 +0000677#undef SMALL_FILE_LIMIT
678#undef REASONABLE_FILE_LIMIT
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000679}
680
Guido van Rossum79f25d91997-04-29 20:08:16 +0000681PyObject *
Tim Petersd9b9ac82001-01-28 00:27:39 +0000682PyMarshal_ReadObjectFromFile(FILE *fp)
683{
684 RFILE rf;
685 if (PyErr_Occurred()) {
686 fprintf(stderr, "XXX rd_object called with exception set\n");
687 return NULL;
688 }
689 rf.fp = fp;
690 return r_object(&rf);
691}
692
693PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000694PyMarshal_ReadObjectFromString(char *str, int len)
Guido van Rossumf56e3db1993-04-01 20:59:32 +0000695{
696 RFILE rf;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000697 if (PyErr_Occurred()) {
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000698 fprintf(stderr, "XXX rds_object called with exception set\n");
699 return NULL;
700 }
Guido van Rossumf56e3db1993-04-01 20:59:32 +0000701 rf.fp = NULL;
702 rf.str = NULL;
703 rf.ptr = str;
704 rf.end = str + len;
705 return r_object(&rf);
706}
707
Guido van Rossum79f25d91997-04-29 20:08:16 +0000708PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000709PyMarshal_WriteObjectToString(PyObject *x) /* wrs_object() */
Guido van Rossum3f3bb3d1996-08-19 22:07:17 +0000710{
711 WFILE wf;
712 wf.fp = NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000713 wf.str = PyString_FromStringAndSize((char *)NULL, 50);
Guido van Rossum3f3bb3d1996-08-19 22:07:17 +0000714 if (wf.str == NULL)
715 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000716 wf.ptr = PyString_AS_STRING((PyStringObject *)wf.str);
717 wf.end = wf.ptr + PyString_Size(wf.str);
Guido van Rossum3f3bb3d1996-08-19 22:07:17 +0000718 wf.error = 0;
Fred Drake6da0b912000-06-28 18:47:56 +0000719 wf.depth = 0;
Guido van Rossum3f3bb3d1996-08-19 22:07:17 +0000720 w_object(x, &wf);
721 if (wf.str != NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000722 _PyString_Resize(&wf.str,
723 (int) (wf.ptr -
724 PyString_AS_STRING((PyStringObject *)wf.str)));
Guido van Rossum3f3bb3d1996-08-19 22:07:17 +0000725 if (wf.error) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000726 Py_XDECREF(wf.str);
Tim Petersd9b9ac82001-01-28 00:27:39 +0000727 PyErr_SetString(PyExc_ValueError,
Fred Drake6da0b912000-06-28 18:47:56 +0000728 (wf.error==1)?"unmarshallable object"
729 :"object too deeply nested to marshal");
Guido van Rossum3f3bb3d1996-08-19 22:07:17 +0000730 return NULL;
731 }
732 return wf.str;
733}
734
Guido van Rossum64b45521991-06-07 13:58:22 +0000735/* And an interface for Python programs... */
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000736
Guido van Rossum79f25d91997-04-29 20:08:16 +0000737static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000738marshal_dump(PyObject *self, PyObject *args)
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000739{
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000740 WFILE wf;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000741 PyObject *x;
742 PyObject *f;
Guido van Rossum2efa3692000-03-31 00:37:41 +0000743 if (!PyArg_ParseTuple(args, "OO:dump", &x, &f))
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000744 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000745 if (!PyFile_Check(f)) {
746 PyErr_SetString(PyExc_TypeError,
747 "marshal.dump() 2nd arg must be file");
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000748 return NULL;
749 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000750 wf.fp = PyFile_AsFile(f);
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000751 wf.str = NULL;
752 wf.ptr = wf.end = NULL;
Guido van Rossumf2150601996-06-26 20:41:23 +0000753 wf.error = 0;
Fred Drake6da0b912000-06-28 18:47:56 +0000754 wf.depth = 0;
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000755 w_object(x, &wf);
Guido van Rossumf2150601996-06-26 20:41:23 +0000756 if (wf.error) {
Tim Petersd9b9ac82001-01-28 00:27:39 +0000757 PyErr_SetString(PyExc_ValueError,
Fred Drake6da0b912000-06-28 18:47:56 +0000758 (wf.error==1)?"unmarshallable object"
759 :"object too deeply nested to marshal");
Guido van Rossumf2150601996-06-26 20:41:23 +0000760 return NULL;
761 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000762 Py_INCREF(Py_None);
763 return Py_None;
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000764}
765
Guido van Rossum79f25d91997-04-29 20:08:16 +0000766static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000767marshal_load(PyObject *self, PyObject *args)
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000768{
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000769 RFILE rf;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000770 PyObject *f;
771 PyObject *v;
Guido van Rossum2efa3692000-03-31 00:37:41 +0000772 if (!PyArg_ParseTuple(args, "O:load", &f))
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000773 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000774 if (!PyFile_Check(f)) {
775 PyErr_SetString(PyExc_TypeError,
776 "marshal.load() arg must be file");
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000777 return NULL;
778 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000779 rf.fp = PyFile_AsFile(f);
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000780 rf.str = NULL;
781 rf.ptr = rf.end = NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000782 PyErr_Clear();
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000783 v = r_object(&rf);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000784 if (PyErr_Occurred()) {
785 Py_XDECREF(v);
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000786 v = NULL;
787 }
788 return v;
789}
790
Guido van Rossum79f25d91997-04-29 20:08:16 +0000791static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000792marshal_dumps(PyObject *self, PyObject *args)
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000793{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000794 PyObject *x;
Guido van Rossum2efa3692000-03-31 00:37:41 +0000795 if (!PyArg_ParseTuple(args, "O:dumps", &x))
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000796 return NULL;
Guido van Rossum3f3bb3d1996-08-19 22:07:17 +0000797 return PyMarshal_WriteObjectToString(x);
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000798}
799
Guido van Rossum79f25d91997-04-29 20:08:16 +0000800static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000801marshal_loads(PyObject *self, PyObject *args)
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000802{
803 RFILE rf;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000804 PyObject *v;
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000805 char *s;
806 int n;
Guido van Rossum2efa3692000-03-31 00:37:41 +0000807 if (!PyArg_ParseTuple(args, "s#:loads", &s, &n))
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000808 return NULL;
809 rf.fp = NULL;
810 rf.str = args;
811 rf.ptr = s;
812 rf.end = s + n;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000813 PyErr_Clear();
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000814 v = r_object(&rf);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000815 if (PyErr_Occurred()) {
816 Py_XDECREF(v);
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000817 v = NULL;
818 }
819 return v;
820}
821
Guido van Rossum79f25d91997-04-29 20:08:16 +0000822static PyMethodDef marshal_methods[] = {
Guido van Rossum2efa3692000-03-31 00:37:41 +0000823 {"dump", marshal_dump, 1},
824 {"load", marshal_load, 1},
825 {"dumps", marshal_dumps, 1},
826 {"loads", marshal_loads, 1},
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000827 {NULL, NULL} /* sentinel */
828};
829
830void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000831PyMarshal_Init(void)
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000832{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000833 (void) Py_InitModule("marshal", marshal_methods);
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000834}