blob: 9daeeb3eec9eb058721492bac4ee7aabf5478994 [file] [log] [blame]
Guido van Rossumdce2e3d1991-06-04 19:42:30 +00001/***********************************************************
Guido van Rossum6d023c91995-01-04 19:12:13 +00002Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
3The Netherlands.
Guido van Rossumdce2e3d1991-06-04 19:42:30 +00004
5 All Rights Reserved
6
Guido van Rossumd266eb41996-10-25 14:44:06 +00007Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
Guido van Rossumdce2e3d1991-06-04 19:42:30 +00009provided that the above copyright notice appear in all copies and that
Guido van Rossumd266eb41996-10-25 14:44:06 +000010both that copyright notice and this permission notice appear in
Guido van Rossumdce2e3d1991-06-04 19:42:30 +000011supporting documentation, and that the names of Stichting Mathematisch
Guido van Rossumd266eb41996-10-25 14:44:06 +000012Centrum or CWI or Corporation for National Research Initiatives or
13CNRI not be used in advertising or publicity pertaining to
14distribution of the software without specific, written prior
15permission.
Guido van Rossumdce2e3d1991-06-04 19:42:30 +000016
Guido van Rossumd266eb41996-10-25 14:44:06 +000017While CWI is the initial source for this software, a modified version
18is made available by the Corporation for National Research Initiatives
19(CNRI) at the Internet address ftp://ftp.python.org.
20
21STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
22REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
23MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
24CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
25DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
26PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
27TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
28PERFORMANCE OF THIS SOFTWARE.
Guido van Rossumdce2e3d1991-06-04 19:42:30 +000029
30******************************************************************/
31
32/* Write Python objects to files and read them back.
33 This is intended for writing and reading compiled Python code only;
34 a true persistent storage facility would be much harder, since
35 it would have to take circular links and sharing into account. */
36
Guido van Rossum79f25d91997-04-29 20:08:16 +000037#include "Python.h"
Guido van Rossumdce2e3d1991-06-04 19:42:30 +000038#include "longintrepr.h"
39#include "compile.h"
40#include "marshal.h"
41
Guido van Rossumdce2e3d1991-06-04 19:42:30 +000042#define TYPE_NULL '0'
43#define TYPE_NONE 'N'
Guido van Rossume449af71996-10-11 16:25:41 +000044#define TYPE_ELLIPSIS '.'
Guido van Rossumdce2e3d1991-06-04 19:42:30 +000045#define TYPE_INT 'i'
Guido van Rossumb0c168c1996-12-05 23:15:02 +000046#define TYPE_INT64 'I'
Guido van Rossumdce2e3d1991-06-04 19:42:30 +000047#define TYPE_FLOAT 'f'
Guido van Rossum8a5c5d21996-01-12 01:09:56 +000048#define TYPE_COMPLEX 'x'
Guido van Rossumdce2e3d1991-06-04 19:42:30 +000049#define TYPE_LONG 'l'
50#define TYPE_STRING 's'
51#define TYPE_TUPLE '('
52#define TYPE_LIST '['
53#define TYPE_DICT '{'
Guido van Rossum681d79a1995-07-18 14:51:37 +000054#define TYPE_CODE 'c'
Guido van Rossumdce2e3d1991-06-04 19:42:30 +000055#define TYPE_UNKNOWN '?'
56
Guido van Rossum0b0db8e1993-01-21 16:07:51 +000057typedef struct {
58 FILE *fp;
Guido van Rossumf2150601996-06-26 20:41:23 +000059 int error;
Guido van Rossum0b0db8e1993-01-21 16:07:51 +000060 /* If fp == NULL, the following are valid: */
Guido van Rossum79f25d91997-04-29 20:08:16 +000061 PyObject *str;
Guido van Rossum0b0db8e1993-01-21 16:07:51 +000062 char *ptr;
63 char *end;
64} WFILE;
Guido van Rossumdce2e3d1991-06-04 19:42:30 +000065
Guido van Rossum0b0db8e1993-01-21 16:07:51 +000066#define w_byte(c, p) if (((p)->fp)) putc((c), (p)->fp); \
67 else if ((p)->ptr != (p)->end) *(p)->ptr++ = (c); \
68 else w_more(c, p)
69
70static void
71w_more(c, p)
72 char c;
73 WFILE *p;
74{
75 int size, newsize;
76 if (p->str == NULL)
77 return; /* An error already occurred */
Guido van Rossum79f25d91997-04-29 20:08:16 +000078 size = PyString_Size(p->str);
Guido van Rossum0b0db8e1993-01-21 16:07:51 +000079 newsize = size + 1024;
Guido van Rossum79f25d91997-04-29 20:08:16 +000080 if (_PyString_Resize(&p->str, newsize) != 0) {
Guido van Rossum0b0db8e1993-01-21 16:07:51 +000081 p->ptr = p->end = NULL;
82 }
83 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +000084 p->ptr = PyString_AS_STRING((PyStringObject *)p->str) + size;
85 p->end =
86 PyString_AS_STRING((PyStringObject *)p->str) + newsize;
Guido van Rossum0b0db8e1993-01-21 16:07:51 +000087 *p->ptr++ = c;
88 }
89}
90
91static void
92w_string(s, n, p)
93 char *s;
94 int n;
95 WFILE *p;
96{
97 if (p->fp != NULL) {
98 fwrite(s, 1, n, p->fp);
99 }
100 else {
101 while (--n >= 0) {
102 w_byte(*s, p);
103 s++;
104 }
105 }
106}
107
108static void
109w_short(x, p)
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000110 int x;
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000111 WFILE *p;
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000112{
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000113 w_byte( x & 0xff, p);
114 w_byte((x>> 8) & 0xff, p);
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000115}
116
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000117static void
118w_long(x, p)
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000119 long x;
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000120 WFILE *p;
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000121{
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000122 w_byte((int)( x & 0xff), p);
123 w_byte((int)((x>> 8) & 0xff), p);
124 w_byte((int)((x>>16) & 0xff), p);
125 w_byte((int)((x>>24) & 0xff), p);
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000126}
127
Guido van Rossumc1547d91996-12-10 15:39:04 +0000128#if SIZEOF_LONG > 4
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000129static void
Guido van Rossumb0c168c1996-12-05 23:15:02 +0000130w_long64(x, p)
131 long x;
132 WFILE *p;
133{
134 w_long(x, p);
Guido van Rossumc1547d91996-12-10 15:39:04 +0000135 w_long(x>>32, p);
Guido van Rossumb0c168c1996-12-05 23:15:02 +0000136}
Guido van Rossumc1547d91996-12-10 15:39:04 +0000137#endif
Guido van Rossumb0c168c1996-12-05 23:15:02 +0000138
139static void
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000140w_object(v, p)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000141 PyObject *v;
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000142 WFILE *p;
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000143{
Guido van Rossum3a205f71995-02-17 15:10:07 +0000144 int i, n;
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000145
Guido van Rossum730806d1998-04-10 22:27:42 +0000146 if (v == NULL) {
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000147 w_byte(TYPE_NULL, p);
Guido van Rossum730806d1998-04-10 22:27:42 +0000148 }
149 else if (v == Py_None) {
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000150 w_byte(TYPE_NONE, p);
Guido van Rossum730806d1998-04-10 22:27:42 +0000151 }
152 else if (v == Py_Ellipsis) {
153 w_byte(TYPE_ELLIPSIS, p);
154 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000155 else if (PyInt_Check(v)) {
156 long x = PyInt_AS_LONG((PyIntObject *)v);
Guido van Rossumc1547d91996-12-10 15:39:04 +0000157#if SIZEOF_LONG > 4
Guido van Rossumb0c168c1996-12-05 23:15:02 +0000158 long y = x>>31;
159 if (y && y != -1) {
160 w_byte(TYPE_INT64, p);
161 w_long64(x, p);
162 }
Guido van Rossumc1547d91996-12-10 15:39:04 +0000163 else
164#endif
165 {
Guido van Rossumb0c168c1996-12-05 23:15:02 +0000166 w_byte(TYPE_INT, p);
167 w_long(x, p);
168 }
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000169 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000170 else if (PyLong_Check(v)) {
171 PyLongObject *ob = (PyLongObject *)v;
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000172 w_byte(TYPE_LONG, p);
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000173 n = ob->ob_size;
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000174 w_long((long)n, p);
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000175 if (n < 0)
176 n = -n;
177 for (i = 0; i < n; i++)
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000178 w_short(ob->ob_digit[i], p);
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000179 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000180 else if (PyFloat_Check(v)) {
181 extern void PyFloat_AsString
182 Py_PROTO((char *, PyFloatObject *));
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000183 char buf[256]; /* Plenty to format any double */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000184 PyFloat_AsString(buf, (PyFloatObject *)v);
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000185 n = strlen(buf);
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000186 w_byte(TYPE_FLOAT, p);
Guido van Rossum3a205f71995-02-17 15:10:07 +0000187 w_byte(n, p);
188 w_string(buf, n, p);
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000189 }
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000190#ifndef WITHOUT_COMPLEX
Guido van Rossum79f25d91997-04-29 20:08:16 +0000191 else if (PyComplex_Check(v)) {
192 extern void PyFloat_AsString
193 Py_PROTO((char *, PyFloatObject *));
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000194 char buf[256]; /* Plenty to format any double */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000195 PyFloatObject *temp;
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000196 w_byte(TYPE_COMPLEX, p);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000197 temp = (PyFloatObject*)PyFloat_FromDouble(
198 PyComplex_RealAsDouble(v));
199 PyFloat_AsString(buf, temp);
200 Py_DECREF(temp);
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000201 n = strlen(buf);
202 w_byte(n, p);
203 w_string(buf, n, p);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000204 temp = (PyFloatObject*)PyFloat_FromDouble(
205 PyComplex_ImagAsDouble(v));
206 PyFloat_AsString(buf, temp);
207 Py_DECREF(temp);
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000208 n = strlen(buf);
209 w_byte(n, p);
210 w_string(buf, n, p);
211 }
212#endif
Guido van Rossum79f25d91997-04-29 20:08:16 +0000213 else if (PyString_Check(v)) {
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000214 w_byte(TYPE_STRING, p);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000215 n = PyString_Size(v);
Guido van Rossum3a205f71995-02-17 15:10:07 +0000216 w_long((long)n, p);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000217 w_string(PyString_AsString(v), n, p);
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000218 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000219 else if (PyTuple_Check(v)) {
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000220 w_byte(TYPE_TUPLE, p);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000221 n = PyTuple_Size(v);
Guido van Rossum3a205f71995-02-17 15:10:07 +0000222 w_long((long)n, p);
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000223 for (i = 0; i < n; i++) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000224 w_object(PyTuple_GET_ITEM(v, i), p);
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000225 }
226 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000227 else if (PyList_Check(v)) {
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000228 w_byte(TYPE_LIST, p);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000229 n = PyList_Size(v);
Guido van Rossum3a205f71995-02-17 15:10:07 +0000230 w_long((long)n, p);
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000231 for (i = 0; i < n; i++) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000232 w_object(PyList_GetItem(v, i), p);
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000233 }
234 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000235 else if (PyDict_Check(v)) {
Guido van Rossum25831651993-05-19 14:50:45 +0000236 int pos;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000237 PyObject *key, *value;
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000238 w_byte(TYPE_DICT, p);
Guido van Rossum64b45521991-06-07 13:58:22 +0000239 /* This one is NULL object terminated! */
Guido van Rossum25831651993-05-19 14:50:45 +0000240 pos = 0;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000241 while (PyDict_Next(v, &pos, &key, &value)) {
Guido van Rossum25831651993-05-19 14:50:45 +0000242 w_object(key, p);
243 w_object(value, p);
Guido van Rossum64b45521991-06-07 13:58:22 +0000244 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000245 w_object((PyObject *)NULL, p);
Guido van Rossum64b45521991-06-07 13:58:22 +0000246 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000247 else if (PyCode_Check(v)) {
248 PyCodeObject *co = (PyCodeObject *)v;
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000249 w_byte(TYPE_CODE, p);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000250 w_short(co->co_argcount, p);
251 w_short(co->co_nlocals, p);
Guido van Rossum98a9b311997-01-17 21:07:08 +0000252 w_short(co->co_stacksize, p);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000253 w_short(co->co_flags, p);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000254 w_object((PyObject *)co->co_code, p);
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000255 w_object(co->co_consts, p);
256 w_object(co->co_names, p);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000257 w_object(co->co_varnames, p);
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000258 w_object(co->co_filename, p);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000259 w_object(co->co_name, p);
Guido van Rossumd031c891997-01-24 03:44:17 +0000260 w_short(co->co_firstlineno, p);
261 w_object(co->co_lnotab, p);
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000262 }
263 else {
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000264 w_byte(TYPE_UNKNOWN, p);
Guido van Rossumf2150601996-06-26 20:41:23 +0000265 p->error = 1;
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000266 }
267}
268
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000269void
Guido van Rossum79f25d91997-04-29 20:08:16 +0000270PyMarshal_WriteLongToFile(x, fp)
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000271 long x;
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000272 FILE *fp;
273{
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000274 WFILE wf;
275 wf.fp = fp;
Guido van Rossumf2150601996-06-26 20:41:23 +0000276 wf.error = 0;
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000277 w_long(x, &wf);
278}
279
280void
Guido van Rossum79f25d91997-04-29 20:08:16 +0000281PyMarshal_WriteObjectToFile(x, fp)
282 PyObject *x;
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000283 FILE *fp;
284{
285 WFILE wf;
286 wf.fp = fp;
Guido van Rossumf2150601996-06-26 20:41:23 +0000287 wf.error = 0;
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000288 w_object(x, &wf);
289}
290
291typedef WFILE RFILE; /* Same struct with different invariants */
292
Guido van Rossum8d617a61995-03-09 12:12:11 +0000293#define rs_byte(p) (((p)->ptr != (p)->end) ? (unsigned char)*(p)->ptr++ : EOF)
294
295#define r_byte(p) ((p)->fp ? getc((p)->fp) : rs_byte(p))
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000296
297static int
298r_string(s, n, p)
299 char *s;
300 int n;
301 RFILE *p;
302{
303 if (p->fp != NULL)
304 return fread(s, 1, n, p->fp);
305 if (p->end - p->ptr < n)
306 n = p->end - p->ptr;
307 memcpy(s, p->ptr, n);
308 p->ptr += n;
309 return n;
310}
311
312static int
313r_short(p)
314 RFILE *p;
315{
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000316 register short x;
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000317 x = r_byte(p);
318 x |= r_byte(p) << 8;
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000319 /* XXX If your short is > 16 bits, add sign-extension here!!! */
320 return x;
321}
322
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000323static long
324r_long(p)
325 RFILE *p;
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000326{
327 register long x;
Guido van Rossum8d617a61995-03-09 12:12:11 +0000328 register FILE *fp = p->fp;
329 if (fp) {
330 x = getc(fp);
331 x |= (long)getc(fp) << 8;
332 x |= (long)getc(fp) << 16;
333 x |= (long)getc(fp) << 24;
334 }
335 else {
336 x = rs_byte(p);
337 x |= (long)rs_byte(p) << 8;
338 x |= (long)rs_byte(p) << 16;
339 x |= (long)rs_byte(p) << 24;
340 }
Guido van Rossumc1547d91996-12-10 15:39:04 +0000341#if SIZEOF_LONG > 4
Guido van Rossumb0c168c1996-12-05 23:15:02 +0000342 /* Sign extension for 64-bit machines */
343 x <<= (8*sizeof(long) - 32);
344 x >>= (8*sizeof(long) - 32);
Guido van Rossumc1547d91996-12-10 15:39:04 +0000345#endif
Guido van Rossumb0c168c1996-12-05 23:15:02 +0000346 return x;
347}
348
349static long
350r_long64(p)
351 RFILE *p;
352{
353 register long x;
Guido van Rossumc1547d91996-12-10 15:39:04 +0000354 x = r_long(p);
355#if SIZEOF_LONG > 4
356 x = (x & 0xFFFFFFFF) | (r_long(p) << 32);
357#else
358 if (r_long(p) != 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000359 PyObject *f = PySys_GetObject("stderr");
Guido van Rossumc1547d91996-12-10 15:39:04 +0000360 if (f != NULL)
Guido van Rossum7e8d26d1997-05-22 22:35:47 +0000361 (void) PyFile_WriteString(
Guido van Rossumc1547d91996-12-10 15:39:04 +0000362 "Warning: un-marshal 64-bit int in 32-bit mode\n",
363 f);
Guido van Rossum7e8d26d1997-05-22 22:35:47 +0000364 PyErr_Clear();
Guido van Rossumb0c168c1996-12-05 23:15:02 +0000365 }
Guido van Rossumc1547d91996-12-10 15:39:04 +0000366#endif
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000367 return x;
368}
369
Guido van Rossum79f25d91997-04-29 20:08:16 +0000370static PyObject *
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000371r_object(p)
372 RFILE *p;
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000373{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000374 PyObject *v, *v2;
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000375 long i, n;
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000376 int type = r_byte(p);
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000377
378 switch (type) {
379
380 case EOF:
Guido van Rossum79f25d91997-04-29 20:08:16 +0000381 PyErr_SetString(PyExc_EOFError,
382 "EOF read where object expected");
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000383 return NULL;
384
385 case TYPE_NULL:
386 return NULL;
387
388 case TYPE_NONE:
Guido van Rossum79f25d91997-04-29 20:08:16 +0000389 Py_INCREF(Py_None);
390 return Py_None;
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000391
Guido van Rossume449af71996-10-11 16:25:41 +0000392 case TYPE_ELLIPSIS:
Guido van Rossum79f25d91997-04-29 20:08:16 +0000393 Py_INCREF(Py_Ellipsis);
Guido van Rossume449af71996-10-11 16:25:41 +0000394 return Py_Ellipsis;
Guido van Rossum8861b741996-07-30 16:49:37 +0000395
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000396 case TYPE_INT:
Guido van Rossum79f25d91997-04-29 20:08:16 +0000397 return PyInt_FromLong(r_long(p));
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000398
Guido van Rossumb0c168c1996-12-05 23:15:02 +0000399 case TYPE_INT64:
Guido van Rossum79f25d91997-04-29 20:08:16 +0000400 return PyInt_FromLong(r_long64(p));
Guido van Rossumb0c168c1996-12-05 23:15:02 +0000401
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000402 case TYPE_LONG:
403 {
404 int size;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000405 PyLongObject *ob;
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000406 n = r_long(p);
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000407 size = n<0 ? -n : n;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000408 ob = _PyLong_New(size);
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000409 if (ob == NULL)
410 return NULL;
411 ob->ob_size = n;
412 for (i = 0; i < size; i++)
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000413 ob->ob_digit[i] = r_short(p);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000414 return (PyObject *)ob;
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000415 }
416
417 case TYPE_FLOAT:
418 {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000419 extern double atof Py_PROTO((const char *));
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000420 char buf[256];
Guido van Rossum0ae748d1997-02-14 22:58:07 +0000421 double dx;
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000422 n = r_byte(p);
423 if (r_string(buf, (int)n, p) != n) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000424 PyErr_SetString(PyExc_EOFError,
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000425 "EOF read where object expected");
426 return NULL;
427 }
428 buf[n] = '\0';
Guido van Rossum0ae748d1997-02-14 22:58:07 +0000429 PyFPE_START_PROTECT("atof", return 0)
430 dx = atof(buf);
Guido van Rossum45b83911997-03-14 04:32:50 +0000431 PyFPE_END_PROTECT(dx)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000432 return PyFloat_FromDouble(dx);
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000433 }
434
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000435#ifndef WITHOUT_COMPLEX
436 case TYPE_COMPLEX:
437 {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000438 extern double atof Py_PROTO((const char *));
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000439 char buf[256];
Guido van Rossum530956d1996-07-21 02:27:43 +0000440 Py_complex c;
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000441 n = r_byte(p);
442 if (r_string(buf, (int)n, p) != n) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000443 PyErr_SetString(PyExc_EOFError,
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000444 "EOF read where object expected");
445 return NULL;
446 }
447 buf[n] = '\0';
Guido van Rossum0ae748d1997-02-14 22:58:07 +0000448 PyFPE_START_PROTECT("atof", return 0)
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000449 c.real = atof(buf);
Guido van Rossum45b83911997-03-14 04:32:50 +0000450 PyFPE_END_PROTECT(c)
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000451 n = r_byte(p);
452 if (r_string(buf, (int)n, p) != n) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000453 PyErr_SetString(PyExc_EOFError,
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000454 "EOF read where object expected");
455 return NULL;
456 }
457 buf[n] = '\0';
Guido van Rossum0ae748d1997-02-14 22:58:07 +0000458 PyFPE_START_PROTECT("atof", return 0)
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000459 c.imag = atof(buf);
Guido van Rossum45b83911997-03-14 04:32:50 +0000460 PyFPE_END_PROTECT(c)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000461 return PyComplex_FromCComplex(c);
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000462 }
463#endif
464
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000465 case TYPE_STRING:
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000466 n = r_long(p);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000467 v = PyString_FromStringAndSize((char *)NULL, n);
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000468 if (v != NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000469 if (r_string(PyString_AsString(v), (int)n, p) != n) {
470 Py_DECREF(v);
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000471 v = NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000472 PyErr_SetString(PyExc_EOFError,
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000473 "EOF read where object expected");
474 }
475 }
476 return v;
477
478 case TYPE_TUPLE:
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000479 n = r_long(p);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000480 v = PyTuple_New((int)n);
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000481 if (v == NULL)
482 return v;
Jack Jansen9513f2c1995-10-27 13:21:28 +0000483 for (i = 0; i < n; i++) {
484 v2 = r_object(p);
485 if ( v2 == NULL ) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000486 Py_DECREF(v);
Jack Jansen9513f2c1995-10-27 13:21:28 +0000487 v = NULL;
488 break;
489 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000490 PyTuple_SET_ITEM(v, (int)i, v2);
Jack Jansen9513f2c1995-10-27 13:21:28 +0000491 }
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000492 return v;
493
494 case TYPE_LIST:
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000495 n = r_long(p);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000496 v = PyList_New((int)n);
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000497 if (v == NULL)
498 return v;
Jack Jansen9513f2c1995-10-27 13:21:28 +0000499 for (i = 0; i < n; i++) {
500 v2 = r_object(p);
501 if ( v2 == NULL ) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000502 Py_DECREF(v);
Jack Jansen9513f2c1995-10-27 13:21:28 +0000503 v = NULL;
504 break;
505 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000506 PyList_SetItem(v, (int)i, v2);
Jack Jansen9513f2c1995-10-27 13:21:28 +0000507 }
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000508 return v;
509
Guido van Rossum64b45521991-06-07 13:58:22 +0000510 case TYPE_DICT:
Guido van Rossum79f25d91997-04-29 20:08:16 +0000511 v = PyDict_New();
Guido van Rossum64b45521991-06-07 13:58:22 +0000512 if (v == NULL)
513 return NULL;
514 for (;;) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000515 PyObject *key, *val;
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000516 key = r_object(p);
Guido van Rossum64b45521991-06-07 13:58:22 +0000517 if (key == NULL)
Guido van Rossumf2150601996-06-26 20:41:23 +0000518 break; /* XXX Assume TYPE_NULL, not an error */
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000519 val = r_object(p);
Guido van Rossumf2150601996-06-26 20:41:23 +0000520 if (val != NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000521 PyDict_SetItem(v, key, val);
522 Py_DECREF(key);
523 Py_XDECREF(val);
Guido van Rossum64b45521991-06-07 13:58:22 +0000524 }
525 return v;
526
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000527 case TYPE_CODE:
528 {
Guido van Rossum681d79a1995-07-18 14:51:37 +0000529 int argcount = r_short(p);
530 int nlocals = r_short(p);
Guido van Rossum98a9b311997-01-17 21:07:08 +0000531 int stacksize = r_short(p);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000532 int flags = r_short(p);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000533 PyObject *code = NULL;
534 PyObject *consts = NULL;
535 PyObject *names = NULL;
536 PyObject *varnames = NULL;
537 PyObject *filename = NULL;
538 PyObject *name = NULL;
Guido van Rossum0f4bbd21997-02-14 21:12:56 +0000539 int firstlineno = 0;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000540 PyObject *lnotab = NULL;
Jack Jansen9513f2c1995-10-27 13:21:28 +0000541
542 code = r_object(p);
543 if (code) consts = r_object(p);
544 if (consts) names = r_object(p);
545 if (names) varnames = r_object(p);
546 if (varnames) filename = r_object(p);
547 if (filename) name = r_object(p);
Guido van Rossumd031c891997-01-24 03:44:17 +0000548 if (name) {
549 firstlineno = r_short(p);
550 lnotab = r_object(p);
551 }
Jack Jansen9513f2c1995-10-27 13:21:28 +0000552
Guido van Rossum79f25d91997-04-29 20:08:16 +0000553 if (!PyErr_Occurred()) {
554 v = (PyObject *) PyCode_New(
Guido van Rossum98a9b311997-01-17 21:07:08 +0000555 argcount, nlocals, stacksize, flags,
Guido van Rossum681d79a1995-07-18 14:51:37 +0000556 code, consts, names, varnames,
Guido van Rossumd031c891997-01-24 03:44:17 +0000557 filename, name, firstlineno, lnotab);
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000558 }
559 else
560 v = NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000561 Py_XDECREF(code);
562 Py_XDECREF(consts);
563 Py_XDECREF(names);
564 Py_XDECREF(varnames);
565 Py_XDECREF(filename);
566 Py_XDECREF(name);
Guido van Rossum6fc06e71997-07-26 23:30:18 +0000567 Py_XDECREF(lnotab);
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000568
569 }
570 return v;
571
572 default:
Guido van Rossumf2150601996-06-26 20:41:23 +0000573 /* Bogus data got written, which isn't ideal.
574 This will let you keep working and recover. */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000575 Py_INCREF(Py_None);
576 return Py_None;
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000577
578 }
579}
580
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000581long
Guido van Rossum79f25d91997-04-29 20:08:16 +0000582PyMarshal_ReadLongFromFile(fp)
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000583 FILE *fp;
584{
585 RFILE rf;
586 rf.fp = fp;
587 return r_long(&rf);
588}
589
Guido van Rossum79f25d91997-04-29 20:08:16 +0000590PyObject *
591PyMarshal_ReadObjectFromFile(fp)
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000592 FILE *fp;
593{
594 RFILE rf;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000595 if (PyErr_Occurred()) {
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000596 fprintf(stderr, "XXX rd_object called with exception set\n");
597 return NULL;
598 }
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000599 rf.fp = fp;
600 return r_object(&rf);
601}
602
Guido van Rossum79f25d91997-04-29 20:08:16 +0000603PyObject *
604PyMarshal_ReadObjectFromString(str, len)
Guido van Rossumf56e3db1993-04-01 20:59:32 +0000605 char *str;
606 int len;
607{
608 RFILE rf;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000609 if (PyErr_Occurred()) {
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000610 fprintf(stderr, "XXX rds_object called with exception set\n");
611 return NULL;
612 }
Guido van Rossumf56e3db1993-04-01 20:59:32 +0000613 rf.fp = NULL;
614 rf.str = NULL;
615 rf.ptr = str;
616 rf.end = str + len;
617 return r_object(&rf);
618}
619
Guido van Rossum79f25d91997-04-29 20:08:16 +0000620PyObject *
Guido van Rossum3f3bb3d1996-08-19 22:07:17 +0000621PyMarshal_WriteObjectToString(x) /* wrs_object() */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000622 PyObject *x;
Guido van Rossum3f3bb3d1996-08-19 22:07:17 +0000623{
624 WFILE wf;
625 wf.fp = NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000626 wf.str = PyString_FromStringAndSize((char *)NULL, 50);
Guido van Rossum3f3bb3d1996-08-19 22:07:17 +0000627 if (wf.str == NULL)
628 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000629 wf.ptr = PyString_AS_STRING((PyStringObject *)wf.str);
630 wf.end = wf.ptr + PyString_Size(wf.str);
Guido van Rossum3f3bb3d1996-08-19 22:07:17 +0000631 wf.error = 0;
632 w_object(x, &wf);
633 if (wf.str != NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000634 _PyString_Resize(&wf.str,
635 (int) (wf.ptr -
636 PyString_AS_STRING((PyStringObject *)wf.str)));
Guido van Rossum3f3bb3d1996-08-19 22:07:17 +0000637 if (wf.error) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000638 Py_XDECREF(wf.str);
639 PyErr_SetString(PyExc_ValueError, "unmarshallable object");
Guido van Rossum3f3bb3d1996-08-19 22:07:17 +0000640 return NULL;
641 }
642 return wf.str;
643}
644
Guido van Rossum64b45521991-06-07 13:58:22 +0000645/* And an interface for Python programs... */
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000646
Guido van Rossum79f25d91997-04-29 20:08:16 +0000647static PyObject *
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000648marshal_dump(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000649 PyObject *self;
650 PyObject *args;
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000651{
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000652 WFILE wf;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000653 PyObject *x;
654 PyObject *f;
655 if (!PyArg_Parse(args, "(OO)", &x, &f))
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000656 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000657 if (!PyFile_Check(f)) {
658 PyErr_SetString(PyExc_TypeError,
659 "marshal.dump() 2nd arg must be file");
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000660 return NULL;
661 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000662 wf.fp = PyFile_AsFile(f);
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000663 wf.str = NULL;
664 wf.ptr = wf.end = NULL;
Guido van Rossumf2150601996-06-26 20:41:23 +0000665 wf.error = 0;
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000666 w_object(x, &wf);
Guido van Rossumf2150601996-06-26 20:41:23 +0000667 if (wf.error) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000668 PyErr_SetString(PyExc_ValueError, "unmarshallable object");
Guido van Rossumf2150601996-06-26 20:41:23 +0000669 return NULL;
670 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000671 Py_INCREF(Py_None);
672 return Py_None;
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000673}
674
Guido van Rossum79f25d91997-04-29 20:08:16 +0000675static PyObject *
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000676marshal_load(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000677 PyObject *self;
678 PyObject *args;
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000679{
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000680 RFILE rf;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000681 PyObject *f;
682 PyObject *v;
683 if (!PyArg_Parse(args, "O", &f))
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000684 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000685 if (!PyFile_Check(f)) {
686 PyErr_SetString(PyExc_TypeError,
687 "marshal.load() arg must be file");
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000688 return NULL;
689 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000690 rf.fp = PyFile_AsFile(f);
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000691 rf.str = NULL;
692 rf.ptr = rf.end = NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000693 PyErr_Clear();
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000694 v = r_object(&rf);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000695 if (PyErr_Occurred()) {
696 Py_XDECREF(v);
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000697 v = NULL;
698 }
699 return v;
700}
701
Guido van Rossum79f25d91997-04-29 20:08:16 +0000702static PyObject *
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000703marshal_dumps(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000704 PyObject *self;
705 PyObject *args;
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000706{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000707 PyObject *x;
708 if (!PyArg_Parse(args, "O", &x))
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000709 return NULL;
Guido van Rossum3f3bb3d1996-08-19 22:07:17 +0000710 return PyMarshal_WriteObjectToString(x);
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000711}
712
Guido van Rossum79f25d91997-04-29 20:08:16 +0000713static PyObject *
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000714marshal_loads(self, args)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000715 PyObject *self;
716 PyObject *args;
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000717{
718 RFILE rf;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000719 PyObject *v;
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000720 char *s;
721 int n;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000722 if (!PyArg_Parse(args, "s#", &s, &n))
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000723 return NULL;
724 rf.fp = NULL;
725 rf.str = args;
726 rf.ptr = s;
727 rf.end = s + n;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000728 PyErr_Clear();
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000729 v = r_object(&rf);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000730 if (PyErr_Occurred()) {
731 Py_XDECREF(v);
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000732 v = NULL;
733 }
734 return v;
735}
736
Guido van Rossum79f25d91997-04-29 20:08:16 +0000737static PyMethodDef marshal_methods[] = {
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000738 {"dump", marshal_dump},
739 {"load", marshal_load},
740 {"dumps", marshal_dumps},
741 {"loads", marshal_loads},
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000742 {NULL, NULL} /* sentinel */
743};
744
745void
Guido van Rossum79f25d91997-04-29 20:08:16 +0000746PyMarshal_Init()
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000747{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000748 (void) Py_InitModule("marshal", marshal_methods);
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000749}