blob: 5aaa3821b42cf7a01f737bdc47d47ee87f2c6393 [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 Rossum77f6a652002-04-03 22:41:51 +000020#define TYPE_FALSE 'F'
21#define TYPE_TRUE 'T'
Tim Peters5ca576e2001-06-18 22:08:13 +000022#define TYPE_STOPITER 'S'
Guido van Rossume449af71996-10-11 16:25:41 +000023#define TYPE_ELLIPSIS '.'
Guido van Rossumdce2e3d1991-06-04 19:42:30 +000024#define TYPE_INT 'i'
Guido van Rossumb0c168c1996-12-05 23:15:02 +000025#define TYPE_INT64 'I'
Guido van Rossumdce2e3d1991-06-04 19:42:30 +000026#define TYPE_FLOAT 'f'
Guido van Rossum8a5c5d21996-01-12 01:09:56 +000027#define TYPE_COMPLEX 'x'
Guido van Rossumdce2e3d1991-06-04 19:42:30 +000028#define TYPE_LONG 'l'
29#define TYPE_STRING 's'
30#define TYPE_TUPLE '('
31#define TYPE_LIST '['
32#define TYPE_DICT '{'
Guido van Rossum681d79a1995-07-18 14:51:37 +000033#define TYPE_CODE 'c'
Guido van Rossumc279b532000-03-10 23:03:02 +000034#define TYPE_UNICODE 'u'
Guido van Rossumdce2e3d1991-06-04 19:42:30 +000035#define TYPE_UNKNOWN '?'
36
Guido van Rossum0b0db8e1993-01-21 16:07:51 +000037typedef struct {
38 FILE *fp;
Guido van Rossumf2150601996-06-26 20:41:23 +000039 int error;
Fred Drake6da0b912000-06-28 18:47:56 +000040 int depth;
Guido van Rossum0b0db8e1993-01-21 16:07:51 +000041 /* If fp == NULL, the following are valid: */
Guido van Rossum79f25d91997-04-29 20:08:16 +000042 PyObject *str;
Guido van Rossum0b0db8e1993-01-21 16:07:51 +000043 char *ptr;
44 char *end;
45} WFILE;
Guido van Rossumdce2e3d1991-06-04 19:42:30 +000046
Guido van Rossum0b0db8e1993-01-21 16:07:51 +000047#define w_byte(c, p) if (((p)->fp)) putc((c), (p)->fp); \
48 else if ((p)->ptr != (p)->end) *(p)->ptr++ = (c); \
49 else w_more(c, p)
50
51static void
Fredrik Lundh11534382000-07-23 18:24:06 +000052w_more(int c, WFILE *p)
Guido van Rossum0b0db8e1993-01-21 16:07:51 +000053{
54 int size, newsize;
55 if (p->str == NULL)
56 return; /* An error already occurred */
Guido van Rossum79f25d91997-04-29 20:08:16 +000057 size = PyString_Size(p->str);
Guido van Rossum0b0db8e1993-01-21 16:07:51 +000058 newsize = size + 1024;
Guido van Rossum79f25d91997-04-29 20:08:16 +000059 if (_PyString_Resize(&p->str, newsize) != 0) {
Guido van Rossum0b0db8e1993-01-21 16:07:51 +000060 p->ptr = p->end = NULL;
61 }
62 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +000063 p->ptr = PyString_AS_STRING((PyStringObject *)p->str) + size;
64 p->end =
65 PyString_AS_STRING((PyStringObject *)p->str) + newsize;
Tim Peters8315ea52000-07-23 19:28:35 +000066 *p->ptr++ = Py_SAFE_DOWNCAST(c, int, char);
Guido van Rossum0b0db8e1993-01-21 16:07:51 +000067 }
68}
69
70static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000071w_string(char *s, int n, WFILE *p)
Guido van Rossum0b0db8e1993-01-21 16:07:51 +000072{
73 if (p->fp != NULL) {
74 fwrite(s, 1, n, p->fp);
75 }
76 else {
77 while (--n >= 0) {
78 w_byte(*s, p);
79 s++;
80 }
81 }
82}
83
84static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000085w_short(int x, WFILE *p)
Guido van Rossumdce2e3d1991-06-04 19:42:30 +000086{
Thomas Heller3e1c18a2002-07-30 11:40:57 +000087 w_byte((char)( x & 0xff), p);
88 w_byte((char)((x>> 8) & 0xff), p);
Guido van Rossumdce2e3d1991-06-04 19:42:30 +000089}
90
Guido van Rossum0b0db8e1993-01-21 16:07:51 +000091static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000092w_long(long x, WFILE *p)
Guido van Rossumdce2e3d1991-06-04 19:42:30 +000093{
Thomas Heller37d5a152002-07-30 11:44:44 +000094 w_byte((char)( x & 0xff), p);
95 w_byte((char)((x>> 8) & 0xff), p);
96 w_byte((char)((x>>16) & 0xff), p);
97 w_byte((char)((x>>24) & 0xff), p);
Guido van Rossumdce2e3d1991-06-04 19:42:30 +000098}
99
Guido van Rossumc1547d91996-12-10 15:39:04 +0000100#if SIZEOF_LONG > 4
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000101static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000102w_long64(long x, WFILE *p)
Guido van Rossumb0c168c1996-12-05 23:15:02 +0000103{
104 w_long(x, p);
Guido van Rossumc1547d91996-12-10 15:39:04 +0000105 w_long(x>>32, p);
Guido van Rossumb0c168c1996-12-05 23:15:02 +0000106}
Guido van Rossumc1547d91996-12-10 15:39:04 +0000107#endif
Guido van Rossumb0c168c1996-12-05 23:15:02 +0000108
109static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000110w_object(PyObject *v, WFILE *p)
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000111{
Guido van Rossum3a205f71995-02-17 15:10:07 +0000112 int i, n;
Fred Drake6da0b912000-06-28 18:47:56 +0000113
114 p->depth++;
Tim Petersd9b9ac82001-01-28 00:27:39 +0000115
Fred Drake6da0b912000-06-28 18:47:56 +0000116 if (p->depth > MAX_MARSHAL_STACK_DEPTH) {
117 p->error = 2;
Tim Petersd9b9ac82001-01-28 00:27:39 +0000118 }
Fred Drake6da0b912000-06-28 18:47:56 +0000119 else if (v == NULL) {
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000120 w_byte(TYPE_NULL, p);
Guido van Rossum730806d1998-04-10 22:27:42 +0000121 }
122 else if (v == Py_None) {
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000123 w_byte(TYPE_NONE, p);
Guido van Rossum730806d1998-04-10 22:27:42 +0000124 }
Tim Peters5ca576e2001-06-18 22:08:13 +0000125 else if (v == PyExc_StopIteration) {
126 w_byte(TYPE_STOPITER, p);
127 }
Guido van Rossum730806d1998-04-10 22:27:42 +0000128 else if (v == Py_Ellipsis) {
129 w_byte(TYPE_ELLIPSIS, p);
130 }
Guido van Rossum77f6a652002-04-03 22:41:51 +0000131 else if (v == Py_False) {
132 w_byte(TYPE_FALSE, p);
133 }
134 else if (v == Py_True) {
135 w_byte(TYPE_TRUE, p);
136 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000137 else if (PyInt_Check(v)) {
138 long x = PyInt_AS_LONG((PyIntObject *)v);
Guido van Rossumc1547d91996-12-10 15:39:04 +0000139#if SIZEOF_LONG > 4
Tim Peters44714002001-04-10 05:02:52 +0000140 long y = Py_ARITHMETIC_RIGHT_SHIFT(long, x, 31);
Guido van Rossumb0c168c1996-12-05 23:15:02 +0000141 if (y && y != -1) {
142 w_byte(TYPE_INT64, p);
143 w_long64(x, p);
144 }
Guido van Rossumc1547d91996-12-10 15:39:04 +0000145 else
146#endif
147 {
Guido van Rossumb0c168c1996-12-05 23:15:02 +0000148 w_byte(TYPE_INT, p);
149 w_long(x, p);
150 }
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000151 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000152 else if (PyLong_Check(v)) {
153 PyLongObject *ob = (PyLongObject *)v;
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000154 w_byte(TYPE_LONG, p);
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000155 n = ob->ob_size;
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000156 w_long((long)n, p);
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000157 if (n < 0)
158 n = -n;
159 for (i = 0; i < n; i++)
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000160 w_short(ob->ob_digit[i], p);
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000161 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000162 else if (PyFloat_Check(v)) {
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000163 char buf[256]; /* Plenty to format any double */
Tim Peters72f98e92001-05-08 15:19:57 +0000164 PyFloat_AsReprString(buf, (PyFloatObject *)v);
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000165 n = strlen(buf);
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000166 w_byte(TYPE_FLOAT, p);
Guido van Rossum3a205f71995-02-17 15:10:07 +0000167 w_byte(n, p);
168 w_string(buf, n, p);
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000169 }
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000170#ifndef WITHOUT_COMPLEX
Guido van Rossum79f25d91997-04-29 20:08:16 +0000171 else if (PyComplex_Check(v)) {
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000172 char buf[256]; /* Plenty to format any double */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000173 PyFloatObject *temp;
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000174 w_byte(TYPE_COMPLEX, p);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000175 temp = (PyFloatObject*)PyFloat_FromDouble(
176 PyComplex_RealAsDouble(v));
Tim Peters72f98e92001-05-08 15:19:57 +0000177 PyFloat_AsReprString(buf, temp);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000178 Py_DECREF(temp);
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000179 n = strlen(buf);
180 w_byte(n, p);
181 w_string(buf, n, p);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000182 temp = (PyFloatObject*)PyFloat_FromDouble(
183 PyComplex_ImagAsDouble(v));
Tim Peters72f98e92001-05-08 15:19:57 +0000184 PyFloat_AsReprString(buf, temp);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000185 Py_DECREF(temp);
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000186 n = strlen(buf);
187 w_byte(n, p);
188 w_string(buf, n, p);
189 }
190#endif
Guido van Rossum79f25d91997-04-29 20:08:16 +0000191 else if (PyString_Check(v)) {
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000192 w_byte(TYPE_STRING, p);
Guido van Rossumc279b532000-03-10 23:03:02 +0000193 n = PyString_GET_SIZE(v);
Guido van Rossum3a205f71995-02-17 15:10:07 +0000194 w_long((long)n, p);
Guido van Rossumc279b532000-03-10 23:03:02 +0000195 w_string(PyString_AS_STRING(v), n, p);
196 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000197#ifdef Py_USING_UNICODE
Guido van Rossumc279b532000-03-10 23:03:02 +0000198 else if (PyUnicode_Check(v)) {
199 PyObject *utf8;
200 utf8 = PyUnicode_AsUTF8String(v);
201 if (utf8 == NULL) {
Guido van Rossum98626cd2000-06-28 23:24:19 +0000202 p->depth--;
203 p->error = 1;
204 return;
Guido van Rossumc279b532000-03-10 23:03:02 +0000205 }
206 w_byte(TYPE_UNICODE, p);
207 n = PyString_GET_SIZE(utf8);
208 w_long((long)n, p);
209 w_string(PyString_AS_STRING(utf8), n, p);
210 Py_DECREF(utf8);
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000211 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000212#endif
Guido van Rossum79f25d91997-04-29 20:08:16 +0000213 else if (PyTuple_Check(v)) {
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000214 w_byte(TYPE_TUPLE, p);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000215 n = PyTuple_Size(v);
Guido van Rossum3a205f71995-02-17 15:10:07 +0000216 w_long((long)n, p);
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000217 for (i = 0; i < n; i++) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000218 w_object(PyTuple_GET_ITEM(v, i), p);
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000219 }
220 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000221 else if (PyList_Check(v)) {
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000222 w_byte(TYPE_LIST, p);
Guido van Rossumc279b532000-03-10 23:03:02 +0000223 n = PyList_GET_SIZE(v);
Guido van Rossum3a205f71995-02-17 15:10:07 +0000224 w_long((long)n, p);
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000225 for (i = 0; i < n; i++) {
Guido van Rossumc279b532000-03-10 23:03:02 +0000226 w_object(PyList_GET_ITEM(v, i), p);
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000227 }
228 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000229 else if (PyDict_Check(v)) {
Guido van Rossum25831651993-05-19 14:50:45 +0000230 int pos;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000231 PyObject *key, *value;
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000232 w_byte(TYPE_DICT, p);
Guido van Rossum64b45521991-06-07 13:58:22 +0000233 /* This one is NULL object terminated! */
Guido van Rossum25831651993-05-19 14:50:45 +0000234 pos = 0;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000235 while (PyDict_Next(v, &pos, &key, &value)) {
Guido van Rossum25831651993-05-19 14:50:45 +0000236 w_object(key, p);
237 w_object(value, p);
Guido van Rossum64b45521991-06-07 13:58:22 +0000238 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000239 w_object((PyObject *)NULL, p);
Guido van Rossum64b45521991-06-07 13:58:22 +0000240 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000241 else if (PyCode_Check(v)) {
242 PyCodeObject *co = (PyCodeObject *)v;
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000243 w_byte(TYPE_CODE, p);
Neal Norwitz7fdcb412002-06-14 01:07:39 +0000244 w_long(co->co_argcount, p);
245 w_long(co->co_nlocals, p);
246 w_long(co->co_stacksize, p);
247 w_long(co->co_flags, p);
Guido van Rossumd076c731998-10-07 19:42:25 +0000248 w_object(co->co_code, p);
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000249 w_object(co->co_consts, p);
250 w_object(co->co_names, p);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000251 w_object(co->co_varnames, p);
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000252 w_object(co->co_freevars, p);
253 w_object(co->co_cellvars, p);
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000254 w_object(co->co_filename, p);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000255 w_object(co->co_name, p);
Neal Norwitz7fdcb412002-06-14 01:07:39 +0000256 w_long(co->co_firstlineno, p);
Guido van Rossumd031c891997-01-24 03:44:17 +0000257 w_object(co->co_lnotab, p);
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000258 }
Jeremy Hylton9f64caa2001-11-09 22:02:48 +0000259 else if (PyObject_CheckReadBuffer(v)) {
Guido van Rossumd076c731998-10-07 19:42:25 +0000260 /* Write unknown buffer-style objects as a string */
261 char *s;
Jeremy Hylton9f64caa2001-11-09 22:02:48 +0000262 PyBufferProcs *pb = v->ob_type->tp_as_buffer;
Guido van Rossumd076c731998-10-07 19:42:25 +0000263 w_byte(TYPE_STRING, p);
264 n = (*pb->bf_getreadbuffer)(v, 0, (void **)&s);
265 w_long((long)n, p);
266 w_string(s, n, p);
267 }
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000268 else {
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000269 w_byte(TYPE_UNKNOWN, p);
Guido van Rossumf2150601996-06-26 20:41:23 +0000270 p->error = 1;
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000271 }
Guido van Rossum98626cd2000-06-28 23:24:19 +0000272
273 p->depth--;
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000274}
275
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000276void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000277PyMarshal_WriteLongToFile(long x, FILE *fp)
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000278{
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000279 WFILE wf;
280 wf.fp = fp;
Guido van Rossumf2150601996-06-26 20:41:23 +0000281 wf.error = 0;
Fred Drake6da0b912000-06-28 18:47:56 +0000282 wf.depth = 0;
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000283 w_long(x, &wf);
284}
285
286void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000287PyMarshal_WriteObjectToFile(PyObject *x, FILE *fp)
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000288{
289 WFILE wf;
290 wf.fp = fp;
Guido van Rossumf2150601996-06-26 20:41:23 +0000291 wf.error = 0;
Guido van Rossum98626cd2000-06-28 23:24:19 +0000292 wf.depth = 0;
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000293 w_object(x, &wf);
294}
295
296typedef WFILE RFILE; /* Same struct with different invariants */
297
Guido van Rossum8d617a61995-03-09 12:12:11 +0000298#define rs_byte(p) (((p)->ptr != (p)->end) ? (unsigned char)*(p)->ptr++ : EOF)
299
300#define r_byte(p) ((p)->fp ? getc((p)->fp) : rs_byte(p))
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000301
302static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000303r_string(char *s, int n, RFILE *p)
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000304{
305 if (p->fp != NULL)
306 return fread(s, 1, n, p->fp);
307 if (p->end - p->ptr < n)
308 n = p->end - p->ptr;
309 memcpy(s, p->ptr, n);
310 p->ptr += n;
311 return n;
312}
313
314static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000315r_short(RFILE *p)
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000316{
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000317 register short x;
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000318 x = r_byte(p);
319 x |= r_byte(p) << 8;
Tim Peterse84b7402000-09-19 08:54:13 +0000320 /* Sign-extension, in case short greater than 16 bits */
321 x |= -(x & 0x8000);
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000322 return x;
323}
324
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000325static long
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000326r_long(RFILE *p)
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000327{
328 register long x;
Guido van Rossum8d617a61995-03-09 12:12:11 +0000329 register FILE *fp = p->fp;
330 if (fp) {
331 x = getc(fp);
332 x |= (long)getc(fp) << 8;
333 x |= (long)getc(fp) << 16;
334 x |= (long)getc(fp) << 24;
335 }
336 else {
337 x = rs_byte(p);
338 x |= (long)rs_byte(p) << 8;
339 x |= (long)rs_byte(p) << 16;
340 x |= (long)rs_byte(p) << 24;
341 }
Guido van Rossumc1547d91996-12-10 15:39:04 +0000342#if SIZEOF_LONG > 4
Guido van Rossumb0c168c1996-12-05 23:15:02 +0000343 /* Sign extension for 64-bit machines */
Tim Peterse84b7402000-09-19 08:54:13 +0000344 x |= -(x & 0x80000000L);
Guido van Rossumc1547d91996-12-10 15:39:04 +0000345#endif
Guido van Rossumb0c168c1996-12-05 23:15:02 +0000346 return x;
347}
348
Tim Peters82112372001-08-29 02:28:42 +0000349/* r_long64 deals with the TYPE_INT64 code. On a machine with
350 sizeof(long) > 4, it returns a Python int object, else a Python long
351 object. Note that w_long64 writes out TYPE_INT if 32 bits is enough,
352 so there's no inefficiency here in returning a PyLong on 32-bit boxes
353 for everything written via TYPE_INT64 (i.e., if an int is written via
354 TYPE_INT64, it *needs* more than 32 bits).
355*/
356static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000357r_long64(RFILE *p)
Guido van Rossumb0c168c1996-12-05 23:15:02 +0000358{
Tim Peters82112372001-08-29 02:28:42 +0000359 long lo4 = r_long(p);
360 long hi4 = r_long(p);
Guido van Rossumc1547d91996-12-10 15:39:04 +0000361#if SIZEOF_LONG > 4
Tim Peters82112372001-08-29 02:28:42 +0000362 long x = (hi4 << 32) | (lo4 & 0xFFFFFFFFL);
363 return PyInt_FromLong(x);
Guido van Rossumc1547d91996-12-10 15:39:04 +0000364#else
Tim Peters82112372001-08-29 02:28:42 +0000365 unsigned char buf[8];
366 int one = 1;
367 int is_little_endian = (int)*(char*)&one;
368 if (is_little_endian) {
369 memcpy(buf, &lo4, 4);
370 memcpy(buf+4, &hi4, 4);
Guido van Rossumb0c168c1996-12-05 23:15:02 +0000371 }
Tim Peters82112372001-08-29 02:28:42 +0000372 else {
373 memcpy(buf, &hi4, 4);
374 memcpy(buf+4, &lo4, 4);
375 }
376 return _PyLong_FromByteArray(buf, 8, is_little_endian, 1);
Guido van Rossumc1547d91996-12-10 15:39:04 +0000377#endif
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000378}
379
Guido van Rossum79f25d91997-04-29 20:08:16 +0000380static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000381r_object(RFILE *p)
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000382{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000383 PyObject *v, *v2;
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000384 long i, n;
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000385 int type = r_byte(p);
Tim Petersd9b9ac82001-01-28 00:27:39 +0000386
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000387 switch (type) {
Tim Petersd9b9ac82001-01-28 00:27:39 +0000388
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000389 case EOF:
Guido van Rossum79f25d91997-04-29 20:08:16 +0000390 PyErr_SetString(PyExc_EOFError,
391 "EOF read where object expected");
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000392 return NULL;
Tim Petersd9b9ac82001-01-28 00:27:39 +0000393
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000394 case TYPE_NULL:
395 return NULL;
Tim Petersd9b9ac82001-01-28 00:27:39 +0000396
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000397 case TYPE_NONE:
Guido van Rossum79f25d91997-04-29 20:08:16 +0000398 Py_INCREF(Py_None);
399 return Py_None;
Tim Petersd9b9ac82001-01-28 00:27:39 +0000400
Tim Peters5ca576e2001-06-18 22:08:13 +0000401 case TYPE_STOPITER:
402 Py_INCREF(PyExc_StopIteration);
403 return PyExc_StopIteration;
404
Guido van Rossume449af71996-10-11 16:25:41 +0000405 case TYPE_ELLIPSIS:
Guido van Rossum79f25d91997-04-29 20:08:16 +0000406 Py_INCREF(Py_Ellipsis);
Guido van Rossume449af71996-10-11 16:25:41 +0000407 return Py_Ellipsis;
Tim Petersd9b9ac82001-01-28 00:27:39 +0000408
Guido van Rossum77f6a652002-04-03 22:41:51 +0000409 case TYPE_FALSE:
410 Py_INCREF(Py_False);
411 return Py_False;
412
413 case TYPE_TRUE:
414 Py_INCREF(Py_True);
415 return Py_True;
416
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000417 case TYPE_INT:
Guido van Rossum79f25d91997-04-29 20:08:16 +0000418 return PyInt_FromLong(r_long(p));
Tim Petersd9b9ac82001-01-28 00:27:39 +0000419
Guido van Rossumb0c168c1996-12-05 23:15:02 +0000420 case TYPE_INT64:
Tim Peters82112372001-08-29 02:28:42 +0000421 return r_long64(p);
Tim Petersd9b9ac82001-01-28 00:27:39 +0000422
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000423 case TYPE_LONG:
424 {
425 int size;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000426 PyLongObject *ob;
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000427 n = r_long(p);
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000428 size = n<0 ? -n : n;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000429 ob = _PyLong_New(size);
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000430 if (ob == NULL)
431 return NULL;
432 ob->ob_size = n;
433 for (i = 0; i < size; i++)
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000434 ob->ob_digit[i] = r_short(p);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000435 return (PyObject *)ob;
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000436 }
Tim Petersd9b9ac82001-01-28 00:27:39 +0000437
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000438 case TYPE_FLOAT:
439 {
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 }
Tim Petersd9b9ac82001-01-28 00:27:39 +0000454
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000455#ifndef WITHOUT_COMPLEX
456 case TYPE_COMPLEX:
457 {
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000458 char buf[256];
Guido van Rossum530956d1996-07-21 02:27:43 +0000459 Py_complex c;
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000460 n = r_byte(p);
461 if (r_string(buf, (int)n, p) != n) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000462 PyErr_SetString(PyExc_EOFError,
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000463 "EOF read where object expected");
464 return NULL;
465 }
466 buf[n] = '\0';
Guido van Rossum0ae748d1997-02-14 22:58:07 +0000467 PyFPE_START_PROTECT("atof", return 0)
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000468 c.real = atof(buf);
Guido van Rossum45b83911997-03-14 04:32:50 +0000469 PyFPE_END_PROTECT(c)
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000470 n = r_byte(p);
471 if (r_string(buf, (int)n, p) != n) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000472 PyErr_SetString(PyExc_EOFError,
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000473 "EOF read where object expected");
474 return NULL;
475 }
476 buf[n] = '\0';
Guido van Rossum0ae748d1997-02-14 22:58:07 +0000477 PyFPE_START_PROTECT("atof", return 0)
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000478 c.imag = atof(buf);
Guido van Rossum45b83911997-03-14 04:32:50 +0000479 PyFPE_END_PROTECT(c)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000480 return PyComplex_FromCComplex(c);
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000481 }
482#endif
Tim Petersd9b9ac82001-01-28 00:27:39 +0000483
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000484 case TYPE_STRING:
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000485 n = r_long(p);
Guido van Rossuma45cb451998-06-08 20:27:29 +0000486 if (n < 0) {
487 PyErr_SetString(PyExc_ValueError, "bad marshal data");
488 return NULL;
489 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000490 v = PyString_FromStringAndSize((char *)NULL, n);
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000491 if (v != NULL) {
Guido van Rossumc279b532000-03-10 23:03:02 +0000492 if (r_string(PyString_AS_STRING(v), (int)n, p) != n) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000493 Py_DECREF(v);
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000494 v = NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000495 PyErr_SetString(PyExc_EOFError,
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000496 "EOF read where object expected");
497 }
498 }
499 return v;
Tim Petersd9b9ac82001-01-28 00:27:39 +0000500
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000501#ifdef Py_USING_UNICODE
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 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000524#endif
Tim Petersd9b9ac82001-01-28 00:27:39 +0000525
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000526 case TYPE_TUPLE:
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000527 n = r_long(p);
Guido van Rossuma45cb451998-06-08 20:27:29 +0000528 if (n < 0) {
529 PyErr_SetString(PyExc_ValueError, "bad marshal data");
530 return NULL;
531 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000532 v = PyTuple_New((int)n);
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000533 if (v == NULL)
534 return v;
Jack Jansen9513f2c1995-10-27 13:21:28 +0000535 for (i = 0; i < n; i++) {
536 v2 = r_object(p);
537 if ( v2 == NULL ) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000538 Py_DECREF(v);
Jack Jansen9513f2c1995-10-27 13:21:28 +0000539 v = NULL;
540 break;
541 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000542 PyTuple_SET_ITEM(v, (int)i, v2);
Jack Jansen9513f2c1995-10-27 13:21:28 +0000543 }
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000544 return v;
Tim Petersd9b9ac82001-01-28 00:27:39 +0000545
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000546 case TYPE_LIST:
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000547 n = r_long(p);
Guido van Rossuma45cb451998-06-08 20:27:29 +0000548 if (n < 0) {
549 PyErr_SetString(PyExc_ValueError, "bad marshal data");
550 return NULL;
551 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000552 v = PyList_New((int)n);
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000553 if (v == NULL)
554 return v;
Jack Jansen9513f2c1995-10-27 13:21:28 +0000555 for (i = 0; i < n; i++) {
556 v2 = r_object(p);
557 if ( v2 == NULL ) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000558 Py_DECREF(v);
Jack Jansen9513f2c1995-10-27 13:21:28 +0000559 v = NULL;
560 break;
561 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000562 PyList_SetItem(v, (int)i, v2);
Jack Jansen9513f2c1995-10-27 13:21:28 +0000563 }
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000564 return v;
Tim Petersd9b9ac82001-01-28 00:27:39 +0000565
Guido van Rossum64b45521991-06-07 13:58:22 +0000566 case TYPE_DICT:
Guido van Rossum79f25d91997-04-29 20:08:16 +0000567 v = PyDict_New();
Guido van Rossum64b45521991-06-07 13:58:22 +0000568 if (v == NULL)
569 return NULL;
570 for (;;) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000571 PyObject *key, *val;
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000572 key = r_object(p);
Guido van Rossum64b45521991-06-07 13:58:22 +0000573 if (key == NULL)
Guido van Rossumf2150601996-06-26 20:41:23 +0000574 break; /* XXX Assume TYPE_NULL, not an error */
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000575 val = r_object(p);
Guido van Rossumf2150601996-06-26 20:41:23 +0000576 if (val != NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000577 PyDict_SetItem(v, key, val);
578 Py_DECREF(key);
579 Py_XDECREF(val);
Guido van Rossum64b45521991-06-07 13:58:22 +0000580 }
581 return v;
Tim Petersd9b9ac82001-01-28 00:27:39 +0000582
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000583 case TYPE_CODE:
Michael W. Hudson80199132001-08-30 14:50:20 +0000584 if (PyEval_GetRestricted()) {
585 PyErr_SetString(PyExc_RuntimeError,
586 "cannot unmarshal code objects in "
587 "restricted execution mode");
588 return NULL;
589 }
590 else {
Neal Norwitz7fdcb412002-06-14 01:07:39 +0000591 int argcount = r_long(p);
592 int nlocals = r_long(p);
593 int stacksize = r_long(p);
594 int flags = r_long(p);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000595 PyObject *code = NULL;
596 PyObject *consts = NULL;
597 PyObject *names = NULL;
598 PyObject *varnames = NULL;
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000599 PyObject *freevars = NULL;
600 PyObject *cellvars = NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000601 PyObject *filename = NULL;
602 PyObject *name = NULL;
Guido van Rossum0f4bbd21997-02-14 21:12:56 +0000603 int firstlineno = 0;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000604 PyObject *lnotab = NULL;
Tim Petersd9b9ac82001-01-28 00:27:39 +0000605
Jack Jansen9513f2c1995-10-27 13:21:28 +0000606 code = r_object(p);
607 if (code) consts = r_object(p);
608 if (consts) names = r_object(p);
609 if (names) varnames = r_object(p);
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000610 if (varnames) freevars = r_object(p);
611 if (freevars) cellvars = r_object(p);
612 if (cellvars) filename = r_object(p);
Jack Jansen9513f2c1995-10-27 13:21:28 +0000613 if (filename) name = r_object(p);
Guido van Rossumd031c891997-01-24 03:44:17 +0000614 if (name) {
Neal Norwitz7fdcb412002-06-14 01:07:39 +0000615 firstlineno = r_long(p);
Guido van Rossumd031c891997-01-24 03:44:17 +0000616 lnotab = r_object(p);
617 }
Tim Petersd9b9ac82001-01-28 00:27:39 +0000618
Guido van Rossum79f25d91997-04-29 20:08:16 +0000619 if (!PyErr_Occurred()) {
620 v = (PyObject *) PyCode_New(
Tim Petersd9b9ac82001-01-28 00:27:39 +0000621 argcount, nlocals, stacksize, flags,
Guido van Rossum681d79a1995-07-18 14:51:37 +0000622 code, consts, names, varnames,
Tim Petersd9b9ac82001-01-28 00:27:39 +0000623 freevars, cellvars, filename, name,
624 firstlineno, lnotab);
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000625 }
626 else
627 v = NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000628 Py_XDECREF(code);
629 Py_XDECREF(consts);
630 Py_XDECREF(names);
631 Py_XDECREF(varnames);
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000632 Py_XDECREF(freevars);
633 Py_XDECREF(cellvars);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000634 Py_XDECREF(filename);
635 Py_XDECREF(name);
Guido van Rossum6fc06e71997-07-26 23:30:18 +0000636 Py_XDECREF(lnotab);
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000637
638 }
639 return v;
Tim Petersd9b9ac82001-01-28 00:27:39 +0000640
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000641 default:
Guido van Rossumf2150601996-06-26 20:41:23 +0000642 /* Bogus data got written, which isn't ideal.
643 This will let you keep working and recover. */
Guido van Rossuma45cb451998-06-08 20:27:29 +0000644 PyErr_SetString(PyExc_ValueError, "bad marshal data");
645 return NULL;
Tim Petersd9b9ac82001-01-28 00:27:39 +0000646
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000647 }
648}
649
Guido van Rossumb8cf3e62001-10-19 01:46:21 +0000650int
651PyMarshal_ReadShortFromFile(FILE *fp)
652{
653 RFILE rf;
654 rf.fp = fp;
655 return r_short(&rf);
656}
657
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000658long
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000659PyMarshal_ReadLongFromFile(FILE *fp)
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000660{
661 RFILE rf;
662 rf.fp = fp;
663 return r_long(&rf);
664}
665
Tim Peters691e0e92001-01-18 04:39:16 +0000666#ifdef HAVE_FSTAT
667/* Return size of file in bytes; < 0 if unknown. */
668static off_t
669getfilesize(FILE *fp)
670{
671 struct stat st;
672 if (fstat(fileno(fp), &st) != 0)
673 return -1;
674 else
675 return st.st_size;
676}
677#endif
Tim Petersd9b9ac82001-01-28 00:27:39 +0000678
Tim Peters691e0e92001-01-18 04:39:16 +0000679/* If we can get the size of the file up-front, and it's reasonably small,
680 * read it in one gulp and delegate to ...FromString() instead. Much quicker
681 * than reading a byte at a time from file; speeds .pyc imports.
Tim Petersd9b9ac82001-01-28 00:27:39 +0000682 * CAUTION: since this may read the entire remainder of the file, don't
683 * call it unless you know you're done with the file.
Tim Peters691e0e92001-01-18 04:39:16 +0000684 */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000685PyObject *
Tim Petersd9b9ac82001-01-28 00:27:39 +0000686PyMarshal_ReadLastObjectFromFile(FILE *fp)
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000687{
Tim Peters691e0e92001-01-18 04:39:16 +0000688/* 75% of 2.1's .pyc files can exploit SMALL_FILE_LIMIT.
689 * REASONABLE_FILE_LIMIT is by defn something big enough for Tkinter.pyc.
690 */
691#define SMALL_FILE_LIMIT (1L << 14)
692#define REASONABLE_FILE_LIMIT (1L << 18)
Tim Peters691e0e92001-01-18 04:39:16 +0000693#ifdef HAVE_FSTAT
694 off_t filesize;
695#endif
Guido van Rossum79f25d91997-04-29 20:08:16 +0000696 if (PyErr_Occurred()) {
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000697 fprintf(stderr, "XXX rd_object called with exception set\n");
698 return NULL;
699 }
Tim Peters691e0e92001-01-18 04:39:16 +0000700#ifdef HAVE_FSTAT
701 filesize = getfilesize(fp);
702 if (filesize > 0) {
703 char buf[SMALL_FILE_LIMIT];
704 char* pBuf = NULL;
705 if (filesize <= SMALL_FILE_LIMIT)
706 pBuf = buf;
707 else if (filesize <= REASONABLE_FILE_LIMIT)
708 pBuf = (char *)PyMem_MALLOC(filesize);
709 if (pBuf != NULL) {
710 PyObject* v;
711 size_t n = fread(pBuf, 1, filesize, fp);
712 v = PyMarshal_ReadObjectFromString(pBuf, n);
713 if (pBuf != buf)
714 PyMem_FREE(pBuf);
715 return v;
716 }
Tim Petersd9b9ac82001-01-28 00:27:39 +0000717
Tim Peters691e0e92001-01-18 04:39:16 +0000718 }
719#endif
Tim Petersd9b9ac82001-01-28 00:27:39 +0000720 /* We don't have fstat, or we do but the file is larger than
721 * REASONABLE_FILE_LIMIT or malloc failed -- read a byte at a time.
722 */
723 return PyMarshal_ReadObjectFromFile(fp);
724
Tim Peters691e0e92001-01-18 04:39:16 +0000725#undef SMALL_FILE_LIMIT
726#undef REASONABLE_FILE_LIMIT
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000727}
728
Guido van Rossum79f25d91997-04-29 20:08:16 +0000729PyObject *
Tim Petersd9b9ac82001-01-28 00:27:39 +0000730PyMarshal_ReadObjectFromFile(FILE *fp)
731{
732 RFILE rf;
733 if (PyErr_Occurred()) {
734 fprintf(stderr, "XXX rd_object called with exception set\n");
735 return NULL;
736 }
737 rf.fp = fp;
738 return r_object(&rf);
739}
740
741PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000742PyMarshal_ReadObjectFromString(char *str, int len)
Guido van Rossumf56e3db1993-04-01 20:59:32 +0000743{
744 RFILE rf;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000745 if (PyErr_Occurred()) {
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000746 fprintf(stderr, "XXX rds_object called with exception set\n");
747 return NULL;
748 }
Guido van Rossumf56e3db1993-04-01 20:59:32 +0000749 rf.fp = NULL;
750 rf.str = NULL;
751 rf.ptr = str;
752 rf.end = str + len;
753 return r_object(&rf);
754}
755
Guido van Rossum79f25d91997-04-29 20:08:16 +0000756PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000757PyMarshal_WriteObjectToString(PyObject *x) /* wrs_object() */
Guido van Rossum3f3bb3d1996-08-19 22:07:17 +0000758{
759 WFILE wf;
760 wf.fp = NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000761 wf.str = PyString_FromStringAndSize((char *)NULL, 50);
Guido van Rossum3f3bb3d1996-08-19 22:07:17 +0000762 if (wf.str == NULL)
763 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000764 wf.ptr = PyString_AS_STRING((PyStringObject *)wf.str);
765 wf.end = wf.ptr + PyString_Size(wf.str);
Guido van Rossum3f3bb3d1996-08-19 22:07:17 +0000766 wf.error = 0;
Fred Drake6da0b912000-06-28 18:47:56 +0000767 wf.depth = 0;
Guido van Rossum3f3bb3d1996-08-19 22:07:17 +0000768 w_object(x, &wf);
769 if (wf.str != NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000770 _PyString_Resize(&wf.str,
771 (int) (wf.ptr -
772 PyString_AS_STRING((PyStringObject *)wf.str)));
Guido van Rossum3f3bb3d1996-08-19 22:07:17 +0000773 if (wf.error) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000774 Py_XDECREF(wf.str);
Tim Petersd9b9ac82001-01-28 00:27:39 +0000775 PyErr_SetString(PyExc_ValueError,
Fred Drake6da0b912000-06-28 18:47:56 +0000776 (wf.error==1)?"unmarshallable object"
777 :"object too deeply nested to marshal");
Guido van Rossum3f3bb3d1996-08-19 22:07:17 +0000778 return NULL;
779 }
780 return wf.str;
781}
782
Guido van Rossum64b45521991-06-07 13:58:22 +0000783/* And an interface for Python programs... */
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000784
Guido van Rossum79f25d91997-04-29 20:08:16 +0000785static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000786marshal_dump(PyObject *self, PyObject *args)
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000787{
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000788 WFILE wf;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000789 PyObject *x;
790 PyObject *f;
Guido van Rossum2efa3692000-03-31 00:37:41 +0000791 if (!PyArg_ParseTuple(args, "OO:dump", &x, &f))
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000792 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000793 if (!PyFile_Check(f)) {
794 PyErr_SetString(PyExc_TypeError,
795 "marshal.dump() 2nd arg must be file");
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000796 return NULL;
797 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000798 wf.fp = PyFile_AsFile(f);
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000799 wf.str = NULL;
800 wf.ptr = wf.end = NULL;
Guido van Rossumf2150601996-06-26 20:41:23 +0000801 wf.error = 0;
Fred Drake6da0b912000-06-28 18:47:56 +0000802 wf.depth = 0;
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000803 w_object(x, &wf);
Guido van Rossumf2150601996-06-26 20:41:23 +0000804 if (wf.error) {
Tim Petersd9b9ac82001-01-28 00:27:39 +0000805 PyErr_SetString(PyExc_ValueError,
Fred Drake6da0b912000-06-28 18:47:56 +0000806 (wf.error==1)?"unmarshallable object"
807 :"object too deeply nested to marshal");
Guido van Rossumf2150601996-06-26 20:41:23 +0000808 return NULL;
809 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000810 Py_INCREF(Py_None);
811 return Py_None;
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000812}
813
Guido van Rossum79f25d91997-04-29 20:08:16 +0000814static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000815marshal_load(PyObject *self, PyObject *args)
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000816{
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000817 RFILE rf;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000818 PyObject *f;
819 PyObject *v;
Guido van Rossum2efa3692000-03-31 00:37:41 +0000820 if (!PyArg_ParseTuple(args, "O:load", &f))
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000821 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000822 if (!PyFile_Check(f)) {
823 PyErr_SetString(PyExc_TypeError,
824 "marshal.load() arg must be file");
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000825 return NULL;
826 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000827 rf.fp = PyFile_AsFile(f);
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000828 rf.str = NULL;
829 rf.ptr = rf.end = NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000830 PyErr_Clear();
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000831 v = r_object(&rf);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000832 if (PyErr_Occurred()) {
833 Py_XDECREF(v);
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000834 v = NULL;
835 }
836 return v;
837}
838
Guido van Rossum79f25d91997-04-29 20:08:16 +0000839static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000840marshal_dumps(PyObject *self, PyObject *args)
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000841{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000842 PyObject *x;
Guido van Rossum2efa3692000-03-31 00:37:41 +0000843 if (!PyArg_ParseTuple(args, "O:dumps", &x))
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000844 return NULL;
Guido van Rossum3f3bb3d1996-08-19 22:07:17 +0000845 return PyMarshal_WriteObjectToString(x);
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000846}
847
Guido van Rossum79f25d91997-04-29 20:08:16 +0000848static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000849marshal_loads(PyObject *self, PyObject *args)
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000850{
851 RFILE rf;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000852 PyObject *v;
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000853 char *s;
854 int n;
Guido van Rossum2efa3692000-03-31 00:37:41 +0000855 if (!PyArg_ParseTuple(args, "s#:loads", &s, &n))
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000856 return NULL;
857 rf.fp = NULL;
858 rf.str = args;
859 rf.ptr = s;
860 rf.end = s + n;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000861 PyErr_Clear();
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000862 v = r_object(&rf);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000863 if (PyErr_Occurred()) {
864 Py_XDECREF(v);
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000865 v = NULL;
866 }
867 return v;
868}
869
Guido van Rossum79f25d91997-04-29 20:08:16 +0000870static PyMethodDef marshal_methods[] = {
Neal Norwitz031829d2002-03-31 14:37:44 +0000871 {"dump", marshal_dump, METH_VARARGS},
872 {"load", marshal_load, METH_VARARGS},
873 {"dumps", marshal_dumps, METH_VARARGS},
874 {"loads", marshal_loads, METH_VARARGS},
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000875 {NULL, NULL} /* sentinel */
876};
877
878void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000879PyMarshal_Init(void)
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000880{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000881 (void) Py_InitModule("marshal", marshal_methods);
Guido van Rossumdce2e3d1991-06-04 19:42:30 +0000882}