1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
|
/***********************************************************
Copyright 1991 by Stichting Mathematisch Centrum, Amsterdam, The
Netherlands.
All Rights Reserved
Permission to use, copy, modify, and distribute this software and its
documentation for any purpose and without fee is hereby granted,
provided that the above copyright notice appear in all copies and that
both that copyright notice and this permission notice appear in
supporting documentation, and that the names of Stichting Mathematisch
Centrum or CWI not be used in advertising or publicity pertaining to
distribution of the software without specific, written prior permission.
STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
******************************************************************/
/* Write Python objects to files and read them back.
This is intended for writing and reading compiled Python code only;
a true persistent storage facility would be much harder, since
it would have to take circular links and sharing into account. */
#include "allobjects.h"
#include "modsupport.h"
#include "longintrepr.h"
#include "compile.h"
#include "marshal.h"
#include <errno.h>
extern int errno;
#define TYPE_NULL '0'
#define TYPE_NONE 'N'
#define TYPE_INT 'i'
#define TYPE_FLOAT 'f'
#define TYPE_LONG 'l'
#define TYPE_STRING 's'
#define TYPE_TUPLE '('
#define TYPE_LIST '['
#define TYPE_DICT '{'
#define TYPE_CODE 'C'
#define TYPE_UNKNOWN '?'
#define wr_byte(c, fp) putc((c), (fp))
void
wr_short(x, fp)
int x;
FILE *fp;
{
wr_byte( x & 0xff, fp);
wr_byte((x>> 8) & 0xff, fp);
}
void
wr_long(x, fp)
long x;
FILE *fp;
{
wr_byte((int)( x & 0xff), fp);
wr_byte((int)((x>> 8) & 0xff), fp);
wr_byte((int)((x>>16) & 0xff), fp);
wr_byte((int)((x>>24) & 0xff), fp);
}
void
wr_object(v, fp)
object *v;
FILE *fp;
{
long i, n;
if (v == NULL)
wr_byte(TYPE_NULL, fp);
else if (v == None)
wr_byte(TYPE_NONE, fp);
else if (is_intobject(v)) {
wr_byte(TYPE_INT, fp);
wr_long(getintvalue(v), fp);
}
else if (is_longobject(v)) {
longobject *ob = (longobject *)v;
wr_byte(TYPE_LONG, fp);
n = ob->ob_size;
wr_long((long)n, fp);
if (n < 0)
n = -n;
for (i = 0; i < n; i++)
wr_short(ob->ob_digit[i], fp);
}
else if (is_floatobject(v)) {
extern void float_buf_repr PROTO((char *, floatobject *));
char buf[256]; /* Plenty to format any double */
float_buf_repr(buf, (floatobject *)v);
n = strlen(buf);
wr_byte(TYPE_FLOAT, fp);
wr_byte((int)n, fp);
fwrite(buf, 1, (int)n, fp);
}
else if (is_stringobject(v)) {
wr_byte(TYPE_STRING, fp);
n = getstringsize(v);
wr_long(n, fp);
fwrite(getstringvalue(v), 1, (int)n, fp);
}
else if (is_tupleobject(v)) {
wr_byte(TYPE_TUPLE, fp);
n = gettuplesize(v);
wr_long(n, fp);
for (i = 0; i < n; i++) {
wr_object(gettupleitem(v, (int)i), fp);
}
}
else if (is_listobject(v)) {
wr_byte(TYPE_LIST, fp);
n = getlistsize(v);
wr_long(n, fp);
for (i = 0; i < n; i++) {
wr_object(getlistitem(v, (int)i), fp);
}
}
else if (is_dictobject(v)) {
wr_byte(TYPE_DICT, fp);
/* This one is NULL object terminated! */
n = getdictsize(v);
for (i = 0; i < n; i++) {
object *key, *val;
key = getdict2key(v, (int)i);
if (key != NULL) {
val = dict2lookup(v, key); /* Can't be NULL */
wr_object(key, fp);
wr_object(val, fp);
}
}
wr_object((object *)NULL, fp);
}
else if (is_codeobject(v)) {
codeobject *co = (codeobject *)v;
wr_byte(TYPE_CODE, fp);
wr_object((object *)co->co_code, fp);
wr_object(co->co_consts, fp);
wr_object(co->co_names, fp);
wr_object(co->co_filename, fp);
}
else {
wr_byte(TYPE_UNKNOWN, fp);
}
}
#define rd_byte(fp) getc(fp)
int
rd_short(fp)
FILE *fp;
{
register short x;
x = rd_byte(fp);
x |= rd_byte(fp) << 8;
/* XXX If your short is > 16 bits, add sign-extension here!!! */
return x;
}
long
rd_long(fp)
FILE *fp;
{
register long x;
x = rd_byte(fp);
x |= (long)rd_byte(fp) << 8;
x |= (long)rd_byte(fp) << 16;
x |= (long)rd_byte(fp) << 24;
/* XXX If your long is > 32 bits, add sign-extension here!!! */
return x;
}
object *
rd_object(fp)
FILE *fp;
{
object *v;
long i, n;
int type = rd_byte(fp);
switch (type) {
case EOF:
err_setstr(EOFError, "EOF read where object expected");
return NULL;
case TYPE_NULL:
return NULL;
case TYPE_NONE:
INCREF(None);
return None;
case TYPE_INT:
return newintobject(rd_long(fp));
case TYPE_LONG:
{
int size;
longobject *ob;
n = rd_long(fp);
size = n<0 ? -n : n;
ob = alloclongobject(size);
if (ob == NULL)
return NULL;
ob->ob_size = n;
for (i = 0; i < size; i++)
ob->ob_digit[i] = rd_short(fp);
return (object *)ob;
}
case TYPE_FLOAT:
{
extern double strtod PROTO((const char *, char **));
char buf[256];
double res;
char *end;
n = rd_byte(fp);
if (fread(buf, 1, (int)n, fp) != n) {
err_setstr(EOFError,
"EOF read where object expected");
return NULL;
}
buf[n] = '\0';
errno = 0;
res = strtod(buf, &end);
if (*end != '\0') {
err_setstr(ValueError, "bad float syntax");
return NULL;
}
if (errno != 0) {
err_setstr(ValueError,
"float constant too large");
return NULL;
}
return newfloatobject(res);
}
case TYPE_STRING:
n = rd_long(fp);
v = newsizedstringobject((char *)NULL, n);
if (v != NULL) {
if (fread(getstringvalue(v), 1, (int)n, fp) != n) {
DECREF(v);
v = NULL;
err_setstr(EOFError,
"EOF read where object expected");
}
}
return v;
case TYPE_TUPLE:
n = rd_long(fp);
v = newtupleobject((int)n);
if (v == NULL)
return v;
for (i = 0; i < n; i++)
settupleitem(v, (int)i, rd_object(fp));
return v;
case TYPE_LIST:
n = rd_long(fp);
v = newlistobject((int)n);
if (v == NULL)
return v;
for (i = 0; i < n; i++)
setlistitem(v, (int)i, rd_object(fp));
return v;
case TYPE_DICT:
v = newdictobject();
if (v == NULL)
return NULL;
for (;;) {
object *key, *val;
key = rd_object(fp);
if (key == NULL)
break;
val = rd_object(fp);
dict2insert(v, key, val);
DECREF(key);
XDECREF(val);
}
return v;
case TYPE_CODE:
{
object *code = rd_object(fp);
object *consts = rd_object(fp);
object *names = rd_object(fp);
object *filename = rd_object(fp);
if (!err_occurred()) {
v = (object *) newcodeobject(code,
consts, names, filename);
}
else
v = NULL;
XDECREF(code);
XDECREF(consts);
XDECREF(names);
XDECREF(filename);
}
return v;
default:
err_setstr(TypeError, "read unknown object");
return NULL;
}
}
/* And an interface for Python programs... */
static object *
dump(self, args)
object *self;
object *args;
{
object *f;
if (args == NULL || !is_tupleobject(args) || gettuplesize(args) != 2) {
err_badarg();
return NULL;
}
f = gettupleitem(args, 1);
if (f == NULL || !is_fileobject(f)) {
err_badarg();
return NULL;
}
wr_object(gettupleitem(args, 0), getfilefile(f));
INCREF(None);
return None;
}
static object *
load(self, f)
object *self;
object *f;
{
object *v;
if (f == NULL || !is_fileobject(f)) {
err_badarg();
return NULL;
}
v = rd_object(getfilefile(f));
if (err_occurred()) {
XDECREF(v);
v = NULL;
}
return v;
}
static struct methodlist marshal_methods[] = {
{"dump", dump},
{"load", load},
{NULL, NULL} /* sentinel */
};
void
initmarshal()
{
(void) initmodule("marshal", marshal_methods);
}
|