1 |
|
---|
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 |
|
---|
7 | #define PY_SSIZE_T_CLEAN
|
---|
8 |
|
---|
9 | #include "Python.h"
|
---|
10 | #include "longintrepr.h"
|
---|
11 | #include "code.h"
|
---|
12 | #include "marshal.h"
|
---|
13 |
|
---|
14 | /* High water mark to determine when the marshalled object is dangerously deep
|
---|
15 | * and risks coring the interpreter. When the object stack gets this deep,
|
---|
16 | * raise an exception instead of continuing.
|
---|
17 | */
|
---|
18 | #define MAX_MARSHAL_STACK_DEPTH 5000
|
---|
19 |
|
---|
20 | #define TYPE_NULL '0'
|
---|
21 | #define TYPE_NONE 'N'
|
---|
22 | #define TYPE_FALSE 'F'
|
---|
23 | #define TYPE_TRUE 'T'
|
---|
24 | #define TYPE_STOPITER 'S'
|
---|
25 | #define TYPE_ELLIPSIS '.'
|
---|
26 | #define TYPE_INT 'i'
|
---|
27 | #define TYPE_INT64 'I'
|
---|
28 | #define TYPE_FLOAT 'f'
|
---|
29 | #define TYPE_BINARY_FLOAT 'g'
|
---|
30 | #define TYPE_COMPLEX 'x'
|
---|
31 | #define TYPE_BINARY_COMPLEX 'y'
|
---|
32 | #define TYPE_LONG 'l'
|
---|
33 | #define TYPE_STRING 's'
|
---|
34 | #define TYPE_INTERNED 't'
|
---|
35 | #define TYPE_STRINGREF 'R'
|
---|
36 | #define TYPE_TUPLE '('
|
---|
37 | #define TYPE_LIST '['
|
---|
38 | #define TYPE_DICT '{'
|
---|
39 | #define TYPE_CODE 'c'
|
---|
40 | #define TYPE_UNICODE 'u'
|
---|
41 | #define TYPE_UNKNOWN '?'
|
---|
42 | #define TYPE_SET '<'
|
---|
43 | #define TYPE_FROZENSET '>'
|
---|
44 |
|
---|
45 | typedef struct {
|
---|
46 | FILE *fp;
|
---|
47 | int error;
|
---|
48 | int depth;
|
---|
49 | /* If fp == NULL, the following are valid: */
|
---|
50 | PyObject *str;
|
---|
51 | char *ptr;
|
---|
52 | char *end;
|
---|
53 | PyObject *strings; /* dict on marshal, list on unmarshal */
|
---|
54 | int version;
|
---|
55 | } WFILE;
|
---|
56 |
|
---|
57 | #define w_byte(c, p) if (((p)->fp)) putc((c), (p)->fp); \
|
---|
58 | else if ((p)->ptr != (p)->end) *(p)->ptr++ = (c); \
|
---|
59 | else w_more(c, p)
|
---|
60 |
|
---|
61 | static void
|
---|
62 | w_more(int c, WFILE *p)
|
---|
63 | {
|
---|
64 | Py_ssize_t size, newsize;
|
---|
65 | if (p->str == NULL)
|
---|
66 | return; /* An error already occurred */
|
---|
67 | size = PyString_Size(p->str);
|
---|
68 | newsize = size + 1024;
|
---|
69 | if (_PyString_Resize(&p->str, newsize) != 0) {
|
---|
70 | p->ptr = p->end = NULL;
|
---|
71 | }
|
---|
72 | else {
|
---|
73 | p->ptr = PyString_AS_STRING((PyStringObject *)p->str) + size;
|
---|
74 | p->end =
|
---|
75 | PyString_AS_STRING((PyStringObject *)p->str) + newsize;
|
---|
76 | *p->ptr++ = Py_SAFE_DOWNCAST(c, int, char);
|
---|
77 | }
|
---|
78 | }
|
---|
79 |
|
---|
80 | static void
|
---|
81 | w_string(char *s, int n, WFILE *p)
|
---|
82 | {
|
---|
83 | if (p->fp != NULL) {
|
---|
84 | fwrite(s, 1, n, p->fp);
|
---|
85 | }
|
---|
86 | else {
|
---|
87 | while (--n >= 0) {
|
---|
88 | w_byte(*s, p);
|
---|
89 | s++;
|
---|
90 | }
|
---|
91 | }
|
---|
92 | }
|
---|
93 |
|
---|
94 | static void
|
---|
95 | w_short(int x, WFILE *p)
|
---|
96 | {
|
---|
97 | w_byte((char)( x & 0xff), p);
|
---|
98 | w_byte((char)((x>> 8) & 0xff), p);
|
---|
99 | }
|
---|
100 |
|
---|
101 | static void
|
---|
102 | w_long(long x, WFILE *p)
|
---|
103 | {
|
---|
104 | w_byte((char)( x & 0xff), p);
|
---|
105 | w_byte((char)((x>> 8) & 0xff), p);
|
---|
106 | w_byte((char)((x>>16) & 0xff), p);
|
---|
107 | w_byte((char)((x>>24) & 0xff), p);
|
---|
108 | }
|
---|
109 |
|
---|
110 | #if SIZEOF_LONG > 4
|
---|
111 | static void
|
---|
112 | w_long64(long x, WFILE *p)
|
---|
113 | {
|
---|
114 | w_long(x, p);
|
---|
115 | w_long(x>>32, p);
|
---|
116 | }
|
---|
117 | #endif
|
---|
118 |
|
---|
119 | static void
|
---|
120 | w_object(PyObject *v, WFILE *p)
|
---|
121 | {
|
---|
122 | Py_ssize_t i, n;
|
---|
123 |
|
---|
124 | p->depth++;
|
---|
125 |
|
---|
126 | if (p->depth > MAX_MARSHAL_STACK_DEPTH) {
|
---|
127 | p->error = 2;
|
---|
128 | }
|
---|
129 | else if (v == NULL) {
|
---|
130 | w_byte(TYPE_NULL, p);
|
---|
131 | }
|
---|
132 | else if (v == Py_None) {
|
---|
133 | w_byte(TYPE_NONE, p);
|
---|
134 | }
|
---|
135 | else if (v == PyExc_StopIteration) {
|
---|
136 | w_byte(TYPE_STOPITER, p);
|
---|
137 | }
|
---|
138 | else if (v == Py_Ellipsis) {
|
---|
139 | w_byte(TYPE_ELLIPSIS, p);
|
---|
140 | }
|
---|
141 | else if (v == Py_False) {
|
---|
142 | w_byte(TYPE_FALSE, p);
|
---|
143 | }
|
---|
144 | else if (v == Py_True) {
|
---|
145 | w_byte(TYPE_TRUE, p);
|
---|
146 | }
|
---|
147 | else if (PyInt_Check(v)) {
|
---|
148 | long x = PyInt_AS_LONG((PyIntObject *)v);
|
---|
149 | #if SIZEOF_LONG > 4
|
---|
150 | long y = Py_ARITHMETIC_RIGHT_SHIFT(long, x, 31);
|
---|
151 | if (y && y != -1) {
|
---|
152 | w_byte(TYPE_INT64, p);
|
---|
153 | w_long64(x, p);
|
---|
154 | }
|
---|
155 | else
|
---|
156 | #endif
|
---|
157 | {
|
---|
158 | w_byte(TYPE_INT, p);
|
---|
159 | w_long(x, p);
|
---|
160 | }
|
---|
161 | }
|
---|
162 | else if (PyLong_Check(v)) {
|
---|
163 | PyLongObject *ob = (PyLongObject *)v;
|
---|
164 | w_byte(TYPE_LONG, p);
|
---|
165 | n = ob->ob_size;
|
---|
166 | w_long((long)n, p);
|
---|
167 | if (n < 0)
|
---|
168 | n = -n;
|
---|
169 | for (i = 0; i < n; i++)
|
---|
170 | w_short(ob->ob_digit[i], p);
|
---|
171 | }
|
---|
172 | else if (PyFloat_Check(v)) {
|
---|
173 | if (p->version > 1) {
|
---|
174 | unsigned char buf[8];
|
---|
175 | if (_PyFloat_Pack8(PyFloat_AsDouble(v),
|
---|
176 | buf, 1) < 0) {
|
---|
177 | p->error = 1;
|
---|
178 | return;
|
---|
179 | }
|
---|
180 | w_byte(TYPE_BINARY_FLOAT, p);
|
---|
181 | w_string((char*)buf, 8, p);
|
---|
182 | }
|
---|
183 | else {
|
---|
184 | char buf[256]; /* Plenty to format any double */
|
---|
185 | PyFloat_AsReprString(buf, (PyFloatObject *)v);
|
---|
186 | n = strlen(buf);
|
---|
187 | w_byte(TYPE_FLOAT, p);
|
---|
188 | w_byte((int)n, p);
|
---|
189 | w_string(buf, (int)n, p);
|
---|
190 | }
|
---|
191 | }
|
---|
192 | #ifndef WITHOUT_COMPLEX
|
---|
193 | else if (PyComplex_Check(v)) {
|
---|
194 | if (p->version > 1) {
|
---|
195 | unsigned char buf[8];
|
---|
196 | if (_PyFloat_Pack8(PyComplex_RealAsDouble(v),
|
---|
197 | buf, 1) < 0) {
|
---|
198 | p->error = 1;
|
---|
199 | return;
|
---|
200 | }
|
---|
201 | w_byte(TYPE_BINARY_COMPLEX, p);
|
---|
202 | w_string((char*)buf, 8, p);
|
---|
203 | if (_PyFloat_Pack8(PyComplex_ImagAsDouble(v),
|
---|
204 | buf, 1) < 0) {
|
---|
205 | p->error = 1;
|
---|
206 | return;
|
---|
207 | }
|
---|
208 | w_string((char*)buf, 8, p);
|
---|
209 | }
|
---|
210 | else {
|
---|
211 | char buf[256]; /* Plenty to format any double */
|
---|
212 | PyFloatObject *temp;
|
---|
213 | w_byte(TYPE_COMPLEX, p);
|
---|
214 | temp = (PyFloatObject*)PyFloat_FromDouble(
|
---|
215 | PyComplex_RealAsDouble(v));
|
---|
216 | if (!temp) {
|
---|
217 | p->error = 1;
|
---|
218 | return;
|
---|
219 | }
|
---|
220 | PyFloat_AsReprString(buf, temp);
|
---|
221 | Py_DECREF(temp);
|
---|
222 | n = strlen(buf);
|
---|
223 | w_byte((int)n, p);
|
---|
224 | w_string(buf, (int)n, p);
|
---|
225 | temp = (PyFloatObject*)PyFloat_FromDouble(
|
---|
226 | PyComplex_ImagAsDouble(v));
|
---|
227 | if (!temp) {
|
---|
228 | p->error = 1;
|
---|
229 | return;
|
---|
230 | }
|
---|
231 | PyFloat_AsReprString(buf, temp);
|
---|
232 | Py_DECREF(temp);
|
---|
233 | n = strlen(buf);
|
---|
234 | w_byte((int)n, p);
|
---|
235 | w_string(buf, (int)n, p);
|
---|
236 | }
|
---|
237 | }
|
---|
238 | #endif
|
---|
239 | else if (PyString_Check(v)) {
|
---|
240 | if (p->strings && PyString_CHECK_INTERNED(v)) {
|
---|
241 | PyObject *o = PyDict_GetItem(p->strings, v);
|
---|
242 | if (o) {
|
---|
243 | long w = PyInt_AsLong(o);
|
---|
244 | w_byte(TYPE_STRINGREF, p);
|
---|
245 | w_long(w, p);
|
---|
246 | goto exit;
|
---|
247 | }
|
---|
248 | else {
|
---|
249 | o = PyInt_FromSsize_t(PyDict_Size(p->strings));
|
---|
250 | PyDict_SetItem(p->strings, v, o);
|
---|
251 | Py_DECREF(o);
|
---|
252 | w_byte(TYPE_INTERNED, p);
|
---|
253 | }
|
---|
254 | }
|
---|
255 | else {
|
---|
256 | w_byte(TYPE_STRING, p);
|
---|
257 | }
|
---|
258 | n = PyString_GET_SIZE(v);
|
---|
259 | if (n > INT_MAX) {
|
---|
260 | /* huge strings are not supported */
|
---|
261 | p->depth--;
|
---|
262 | p->error = 1;
|
---|
263 | return;
|
---|
264 | }
|
---|
265 | w_long((long)n, p);
|
---|
266 | w_string(PyString_AS_STRING(v), (int)n, p);
|
---|
267 | }
|
---|
268 | #ifdef Py_USING_UNICODE
|
---|
269 | else if (PyUnicode_Check(v)) {
|
---|
270 | PyObject *utf8;
|
---|
271 | utf8 = PyUnicode_AsUTF8String(v);
|
---|
272 | if (utf8 == NULL) {
|
---|
273 | p->depth--;
|
---|
274 | p->error = 1;
|
---|
275 | return;
|
---|
276 | }
|
---|
277 | w_byte(TYPE_UNICODE, p);
|
---|
278 | n = PyString_GET_SIZE(utf8);
|
---|
279 | if (n > INT_MAX) {
|
---|
280 | p->depth--;
|
---|
281 | p->error = 1;
|
---|
282 | return;
|
---|
283 | }
|
---|
284 | w_long((long)n, p);
|
---|
285 | w_string(PyString_AS_STRING(utf8), (int)n, p);
|
---|
286 | Py_DECREF(utf8);
|
---|
287 | }
|
---|
288 | #endif
|
---|
289 | else if (PyTuple_Check(v)) {
|
---|
290 | w_byte(TYPE_TUPLE, p);
|
---|
291 | n = PyTuple_Size(v);
|
---|
292 | w_long((long)n, p);
|
---|
293 | for (i = 0; i < n; i++) {
|
---|
294 | w_object(PyTuple_GET_ITEM(v, i), p);
|
---|
295 | }
|
---|
296 | }
|
---|
297 | else if (PyList_Check(v)) {
|
---|
298 | w_byte(TYPE_LIST, p);
|
---|
299 | n = PyList_GET_SIZE(v);
|
---|
300 | w_long((long)n, p);
|
---|
301 | for (i = 0; i < n; i++) {
|
---|
302 | w_object(PyList_GET_ITEM(v, i), p);
|
---|
303 | }
|
---|
304 | }
|
---|
305 | else if (PyDict_Check(v)) {
|
---|
306 | Py_ssize_t pos;
|
---|
307 | PyObject *key, *value;
|
---|
308 | w_byte(TYPE_DICT, p);
|
---|
309 | /* This one is NULL object terminated! */
|
---|
310 | pos = 0;
|
---|
311 | while (PyDict_Next(v, &pos, &key, &value)) {
|
---|
312 | w_object(key, p);
|
---|
313 | w_object(value, p);
|
---|
314 | }
|
---|
315 | w_object((PyObject *)NULL, p);
|
---|
316 | }
|
---|
317 | else if (PyAnySet_Check(v)) {
|
---|
318 | PyObject *value, *it;
|
---|
319 |
|
---|
320 | if (PyObject_TypeCheck(v, &PySet_Type))
|
---|
321 | w_byte(TYPE_SET, p);
|
---|
322 | else
|
---|
323 | w_byte(TYPE_FROZENSET, p);
|
---|
324 | n = PyObject_Size(v);
|
---|
325 | if (n == -1) {
|
---|
326 | p->depth--;
|
---|
327 | p->error = 1;
|
---|
328 | return;
|
---|
329 | }
|
---|
330 | w_long((long)n, p);
|
---|
331 | it = PyObject_GetIter(v);
|
---|
332 | if (it == NULL) {
|
---|
333 | p->depth--;
|
---|
334 | p->error = 1;
|
---|
335 | return;
|
---|
336 | }
|
---|
337 | while ((value = PyIter_Next(it)) != NULL) {
|
---|
338 | w_object(value, p);
|
---|
339 | Py_DECREF(value);
|
---|
340 | }
|
---|
341 | Py_DECREF(it);
|
---|
342 | if (PyErr_Occurred()) {
|
---|
343 | p->depth--;
|
---|
344 | p->error = 1;
|
---|
345 | return;
|
---|
346 | }
|
---|
347 | }
|
---|
348 | else if (PyCode_Check(v)) {
|
---|
349 | PyCodeObject *co = (PyCodeObject *)v;
|
---|
350 | w_byte(TYPE_CODE, p);
|
---|
351 | w_long(co->co_argcount, p);
|
---|
352 | w_long(co->co_nlocals, p);
|
---|
353 | w_long(co->co_stacksize, p);
|
---|
354 | w_long(co->co_flags, p);
|
---|
355 | w_object(co->co_code, p);
|
---|
356 | w_object(co->co_consts, p);
|
---|
357 | w_object(co->co_names, p);
|
---|
358 | w_object(co->co_varnames, p);
|
---|
359 | w_object(co->co_freevars, p);
|
---|
360 | w_object(co->co_cellvars, p);
|
---|
361 | w_object(co->co_filename, p);
|
---|
362 | w_object(co->co_name, p);
|
---|
363 | w_long(co->co_firstlineno, p);
|
---|
364 | w_object(co->co_lnotab, p);
|
---|
365 | }
|
---|
366 | else if (PyObject_CheckReadBuffer(v)) {
|
---|
367 | /* Write unknown buffer-style objects as a string */
|
---|
368 | char *s;
|
---|
369 | PyBufferProcs *pb = v->ob_type->tp_as_buffer;
|
---|
370 | w_byte(TYPE_STRING, p);
|
---|
371 | n = (*pb->bf_getreadbuffer)(v, 0, (void **)&s);
|
---|
372 | if (n > INT_MAX) {
|
---|
373 | p->depth--;
|
---|
374 | p->error = 1;
|
---|
375 | return;
|
---|
376 | }
|
---|
377 | w_long((long)n, p);
|
---|
378 | w_string(s, (int)n, p);
|
---|
379 | }
|
---|
380 | else {
|
---|
381 | w_byte(TYPE_UNKNOWN, p);
|
---|
382 | p->error = 1;
|
---|
383 | }
|
---|
384 | exit:
|
---|
385 | p->depth--;
|
---|
386 | }
|
---|
387 |
|
---|
388 | /* version currently has no effect for writing longs. */
|
---|
389 | void
|
---|
390 | PyMarshal_WriteLongToFile(long x, FILE *fp, int version)
|
---|
391 | {
|
---|
392 | WFILE wf;
|
---|
393 | wf.fp = fp;
|
---|
394 | wf.error = 0;
|
---|
395 | wf.depth = 0;
|
---|
396 | wf.strings = NULL;
|
---|
397 | wf.version = version;
|
---|
398 | w_long(x, &wf);
|
---|
399 | }
|
---|
400 |
|
---|
401 | void
|
---|
402 | PyMarshal_WriteObjectToFile(PyObject *x, FILE *fp, int version)
|
---|
403 | {
|
---|
404 | WFILE wf;
|
---|
405 | wf.fp = fp;
|
---|
406 | wf.error = 0;
|
---|
407 | wf.depth = 0;
|
---|
408 | wf.strings = (version > 0) ? PyDict_New() : NULL;
|
---|
409 | wf.version = version;
|
---|
410 | w_object(x, &wf);
|
---|
411 | Py_XDECREF(wf.strings);
|
---|
412 | }
|
---|
413 |
|
---|
414 | typedef WFILE RFILE; /* Same struct with different invariants */
|
---|
415 |
|
---|
416 | #define rs_byte(p) (((p)->ptr != (p)->end) ? (unsigned char)*(p)->ptr++ : EOF)
|
---|
417 |
|
---|
418 | #define r_byte(p) ((p)->fp ? getc((p)->fp) : rs_byte(p))
|
---|
419 |
|
---|
420 | static int
|
---|
421 | r_string(char *s, int n, RFILE *p)
|
---|
422 | {
|
---|
423 | if (p->fp != NULL)
|
---|
424 | /* The result fits into int because it must be <=n. */
|
---|
425 | return (int)fread(s, 1, n, p->fp);
|
---|
426 | if (p->end - p->ptr < n)
|
---|
427 | n = (int)(p->end - p->ptr);
|
---|
428 | memcpy(s, p->ptr, n);
|
---|
429 | p->ptr += n;
|
---|
430 | return n;
|
---|
431 | }
|
---|
432 |
|
---|
433 | static int
|
---|
434 | r_short(RFILE *p)
|
---|
435 | {
|
---|
436 | register short x;
|
---|
437 | x = r_byte(p);
|
---|
438 | x |= r_byte(p) << 8;
|
---|
439 | /* Sign-extension, in case short greater than 16 bits */
|
---|
440 | x |= -(x & 0x8000);
|
---|
441 | return x;
|
---|
442 | }
|
---|
443 |
|
---|
444 | static long
|
---|
445 | r_long(RFILE *p)
|
---|
446 | {
|
---|
447 | register long x;
|
---|
448 | register FILE *fp = p->fp;
|
---|
449 | if (fp) {
|
---|
450 | x = getc(fp);
|
---|
451 | x |= (long)getc(fp) << 8;
|
---|
452 | x |= (long)getc(fp) << 16;
|
---|
453 | x |= (long)getc(fp) << 24;
|
---|
454 | }
|
---|
455 | else {
|
---|
456 | x = rs_byte(p);
|
---|
457 | x |= (long)rs_byte(p) << 8;
|
---|
458 | x |= (long)rs_byte(p) << 16;
|
---|
459 | x |= (long)rs_byte(p) << 24;
|
---|
460 | }
|
---|
461 | #if SIZEOF_LONG > 4
|
---|
462 | /* Sign extension for 64-bit machines */
|
---|
463 | x |= -(x & 0x80000000L);
|
---|
464 | #endif
|
---|
465 | return x;
|
---|
466 | }
|
---|
467 |
|
---|
468 | /* r_long64 deals with the TYPE_INT64 code. On a machine with
|
---|
469 | sizeof(long) > 4, it returns a Python int object, else a Python long
|
---|
470 | object. Note that w_long64 writes out TYPE_INT if 32 bits is enough,
|
---|
471 | so there's no inefficiency here in returning a PyLong on 32-bit boxes
|
---|
472 | for everything written via TYPE_INT64 (i.e., if an int is written via
|
---|
473 | TYPE_INT64, it *needs* more than 32 bits).
|
---|
474 | */
|
---|
475 | static PyObject *
|
---|
476 | r_long64(RFILE *p)
|
---|
477 | {
|
---|
478 | long lo4 = r_long(p);
|
---|
479 | long hi4 = r_long(p);
|
---|
480 | #if SIZEOF_LONG > 4
|
---|
481 | long x = (hi4 << 32) | (lo4 & 0xFFFFFFFFL);
|
---|
482 | return PyInt_FromLong(x);
|
---|
483 | #else
|
---|
484 | unsigned char buf[8];
|
---|
485 | int one = 1;
|
---|
486 | int is_little_endian = (int)*(char*)&one;
|
---|
487 | if (is_little_endian) {
|
---|
488 | memcpy(buf, &lo4, 4);
|
---|
489 | memcpy(buf+4, &hi4, 4);
|
---|
490 | }
|
---|
491 | else {
|
---|
492 | memcpy(buf, &hi4, 4);
|
---|
493 | memcpy(buf+4, &lo4, 4);
|
---|
494 | }
|
---|
495 | return _PyLong_FromByteArray(buf, 8, is_little_endian, 1);
|
---|
496 | #endif
|
---|
497 | }
|
---|
498 |
|
---|
499 | static PyObject *
|
---|
500 | r_object(RFILE *p)
|
---|
501 | {
|
---|
502 | /* NULL is a valid return value, it does not necessarily means that
|
---|
503 | an exception is set. */
|
---|
504 | PyObject *v, *v2, *v3;
|
---|
505 | long i, n;
|
---|
506 | int type = r_byte(p);
|
---|
507 |
|
---|
508 | switch (type) {
|
---|
509 |
|
---|
510 | case EOF:
|
---|
511 | PyErr_SetString(PyExc_EOFError,
|
---|
512 | "EOF read where object expected");
|
---|
513 | return NULL;
|
---|
514 |
|
---|
515 | case TYPE_NULL:
|
---|
516 | return NULL;
|
---|
517 |
|
---|
518 | case TYPE_NONE:
|
---|
519 | Py_INCREF(Py_None);
|
---|
520 | return Py_None;
|
---|
521 |
|
---|
522 | case TYPE_STOPITER:
|
---|
523 | Py_INCREF(PyExc_StopIteration);
|
---|
524 | return PyExc_StopIteration;
|
---|
525 |
|
---|
526 | case TYPE_ELLIPSIS:
|
---|
527 | Py_INCREF(Py_Ellipsis);
|
---|
528 | return Py_Ellipsis;
|
---|
529 |
|
---|
530 | case TYPE_FALSE:
|
---|
531 | Py_INCREF(Py_False);
|
---|
532 | return Py_False;
|
---|
533 |
|
---|
534 | case TYPE_TRUE:
|
---|
535 | Py_INCREF(Py_True);
|
---|
536 | return Py_True;
|
---|
537 |
|
---|
538 | case TYPE_INT:
|
---|
539 | return PyInt_FromLong(r_long(p));
|
---|
540 |
|
---|
541 | case TYPE_INT64:
|
---|
542 | return r_long64(p);
|
---|
543 |
|
---|
544 | case TYPE_LONG:
|
---|
545 | {
|
---|
546 | int size;
|
---|
547 | PyLongObject *ob;
|
---|
548 | n = r_long(p);
|
---|
549 | size = n<0 ? -n : n;
|
---|
550 | ob = _PyLong_New(size);
|
---|
551 | if (ob == NULL)
|
---|
552 | return NULL;
|
---|
553 | ob->ob_size = n;
|
---|
554 | for (i = 0; i < size; i++) {
|
---|
555 | int digit = r_short(p);
|
---|
556 | if (digit < 0) {
|
---|
557 | Py_DECREF(ob);
|
---|
558 | PyErr_SetString(PyExc_ValueError,
|
---|
559 | "bad marshal data");
|
---|
560 | return NULL;
|
---|
561 | }
|
---|
562 | ob->ob_digit[i] = digit;
|
---|
563 | }
|
---|
564 | return (PyObject *)ob;
|
---|
565 | }
|
---|
566 |
|
---|
567 | case TYPE_FLOAT:
|
---|
568 | {
|
---|
569 | char buf[256];
|
---|
570 | double dx;
|
---|
571 | n = r_byte(p);
|
---|
572 | if (n == EOF || r_string(buf, (int)n, p) != n) {
|
---|
573 | PyErr_SetString(PyExc_EOFError,
|
---|
574 | "EOF read where object expected");
|
---|
575 | return NULL;
|
---|
576 | }
|
---|
577 | buf[n] = '\0';
|
---|
578 | PyFPE_START_PROTECT("atof", return 0)
|
---|
579 | dx = PyOS_ascii_atof(buf);
|
---|
580 | PyFPE_END_PROTECT(dx)
|
---|
581 | return PyFloat_FromDouble(dx);
|
---|
582 | }
|
---|
583 |
|
---|
584 | case TYPE_BINARY_FLOAT:
|
---|
585 | {
|
---|
586 | unsigned char buf[8];
|
---|
587 | double x;
|
---|
588 | if (r_string((char*)buf, 8, p) != 8) {
|
---|
589 | PyErr_SetString(PyExc_EOFError,
|
---|
590 | "EOF read where object expected");
|
---|
591 | return NULL;
|
---|
592 | }
|
---|
593 | x = _PyFloat_Unpack8(buf, 1);
|
---|
594 | if (x == -1.0 && PyErr_Occurred()) {
|
---|
595 | return NULL;
|
---|
596 | }
|
---|
597 | return PyFloat_FromDouble(x);
|
---|
598 | }
|
---|
599 |
|
---|
600 | #ifndef WITHOUT_COMPLEX
|
---|
601 | case TYPE_COMPLEX:
|
---|
602 | {
|
---|
603 | char buf[256];
|
---|
604 | Py_complex c;
|
---|
605 | n = r_byte(p);
|
---|
606 | if (n == EOF || r_string(buf, (int)n, p) != n) {
|
---|
607 | PyErr_SetString(PyExc_EOFError,
|
---|
608 | "EOF read where object expected");
|
---|
609 | return NULL;
|
---|
610 | }
|
---|
611 | buf[n] = '\0';
|
---|
612 | PyFPE_START_PROTECT("atof", return 0)
|
---|
613 | c.real = PyOS_ascii_atof(buf);
|
---|
614 | PyFPE_END_PROTECT(c)
|
---|
615 | n = r_byte(p);
|
---|
616 | if (n == EOF || r_string(buf, (int)n, p) != n) {
|
---|
617 | PyErr_SetString(PyExc_EOFError,
|
---|
618 | "EOF read where object expected");
|
---|
619 | return NULL;
|
---|
620 | }
|
---|
621 | buf[n] = '\0';
|
---|
622 | PyFPE_START_PROTECT("atof", return 0)
|
---|
623 | c.imag = PyOS_ascii_atof(buf);
|
---|
624 | PyFPE_END_PROTECT(c)
|
---|
625 | return PyComplex_FromCComplex(c);
|
---|
626 | }
|
---|
627 |
|
---|
628 | case TYPE_BINARY_COMPLEX:
|
---|
629 | {
|
---|
630 | unsigned char buf[8];
|
---|
631 | Py_complex c;
|
---|
632 | if (r_string((char*)buf, 8, p) != 8) {
|
---|
633 | PyErr_SetString(PyExc_EOFError,
|
---|
634 | "EOF read where object expected");
|
---|
635 | return NULL;
|
---|
636 | }
|
---|
637 | c.real = _PyFloat_Unpack8(buf, 1);
|
---|
638 | if (c.real == -1.0 && PyErr_Occurred()) {
|
---|
639 | return NULL;
|
---|
640 | }
|
---|
641 | if (r_string((char*)buf, 8, p) != 8) {
|
---|
642 | PyErr_SetString(PyExc_EOFError,
|
---|
643 | "EOF read where object expected");
|
---|
644 | return NULL;
|
---|
645 | }
|
---|
646 | c.imag = _PyFloat_Unpack8(buf, 1);
|
---|
647 | if (c.imag == -1.0 && PyErr_Occurred()) {
|
---|
648 | return NULL;
|
---|
649 | }
|
---|
650 | return PyComplex_FromCComplex(c);
|
---|
651 | }
|
---|
652 | #endif
|
---|
653 |
|
---|
654 | case TYPE_INTERNED:
|
---|
655 | case TYPE_STRING:
|
---|
656 | n = r_long(p);
|
---|
657 | if (n < 0) {
|
---|
658 | PyErr_SetString(PyExc_ValueError, "bad marshal data");
|
---|
659 | return NULL;
|
---|
660 | }
|
---|
661 | v = PyString_FromStringAndSize((char *)NULL, n);
|
---|
662 | if (v == NULL)
|
---|
663 | return v;
|
---|
664 | if (r_string(PyString_AS_STRING(v), (int)n, p) != n) {
|
---|
665 | Py_DECREF(v);
|
---|
666 | PyErr_SetString(PyExc_EOFError,
|
---|
667 | "EOF read where object expected");
|
---|
668 | return NULL;
|
---|
669 | }
|
---|
670 | if (type == TYPE_INTERNED) {
|
---|
671 | PyString_InternInPlace(&v);
|
---|
672 | PyList_Append(p->strings, v);
|
---|
673 | }
|
---|
674 | return v;
|
---|
675 |
|
---|
676 | case TYPE_STRINGREF:
|
---|
677 | n = r_long(p);
|
---|
678 | if (n < 0 || n >= PyList_GET_SIZE(p->strings)) {
|
---|
679 | PyErr_SetString(PyExc_ValueError, "bad marshal data");
|
---|
680 | return NULL;
|
---|
681 | }
|
---|
682 | v = PyList_GET_ITEM(p->strings, n);
|
---|
683 | Py_INCREF(v);
|
---|
684 | return v;
|
---|
685 |
|
---|
686 | #ifdef Py_USING_UNICODE
|
---|
687 | case TYPE_UNICODE:
|
---|
688 | {
|
---|
689 | char *buffer;
|
---|
690 |
|
---|
691 | n = r_long(p);
|
---|
692 | if (n < 0) {
|
---|
693 | PyErr_SetString(PyExc_ValueError, "bad marshal data");
|
---|
694 | return NULL;
|
---|
695 | }
|
---|
696 | buffer = PyMem_NEW(char, n);
|
---|
697 | if (buffer == NULL)
|
---|
698 | return PyErr_NoMemory();
|
---|
699 | if (r_string(buffer, (int)n, p) != n) {
|
---|
700 | PyMem_DEL(buffer);
|
---|
701 | PyErr_SetString(PyExc_EOFError,
|
---|
702 | "EOF read where object expected");
|
---|
703 | return NULL;
|
---|
704 | }
|
---|
705 | v = PyUnicode_DecodeUTF8(buffer, n, NULL);
|
---|
706 | PyMem_DEL(buffer);
|
---|
707 | return v;
|
---|
708 | }
|
---|
709 | #endif
|
---|
710 |
|
---|
711 | case TYPE_TUPLE:
|
---|
712 | n = r_long(p);
|
---|
713 | if (n < 0) {
|
---|
714 | PyErr_SetString(PyExc_ValueError, "bad marshal data");
|
---|
715 | return NULL;
|
---|
716 | }
|
---|
717 | v = PyTuple_New((int)n);
|
---|
718 | if (v == NULL)
|
---|
719 | return v;
|
---|
720 | for (i = 0; i < n; i++) {
|
---|
721 | v2 = r_object(p);
|
---|
722 | if ( v2 == NULL ) {
|
---|
723 | if (!PyErr_Occurred())
|
---|
724 | PyErr_SetString(PyExc_TypeError,
|
---|
725 | "NULL object in marshal data");
|
---|
726 | Py_DECREF(v);
|
---|
727 | v = NULL;
|
---|
728 | break;
|
---|
729 | }
|
---|
730 | PyTuple_SET_ITEM(v, (int)i, v2);
|
---|
731 | }
|
---|
732 | return v;
|
---|
733 |
|
---|
734 | case TYPE_LIST:
|
---|
735 | n = r_long(p);
|
---|
736 | if (n < 0) {
|
---|
737 | PyErr_SetString(PyExc_ValueError, "bad marshal data");
|
---|
738 | return NULL;
|
---|
739 | }
|
---|
740 | v = PyList_New((int)n);
|
---|
741 | if (v == NULL)
|
---|
742 | return v;
|
---|
743 | for (i = 0; i < n; i++) {
|
---|
744 | v2 = r_object(p);
|
---|
745 | if ( v2 == NULL ) {
|
---|
746 | if (!PyErr_Occurred())
|
---|
747 | PyErr_SetString(PyExc_TypeError,
|
---|
748 | "NULL object in marshal data");
|
---|
749 | Py_DECREF(v);
|
---|
750 | v = NULL;
|
---|
751 | break;
|
---|
752 | }
|
---|
753 | PyList_SetItem(v, (int)i, v2);
|
---|
754 | }
|
---|
755 | return v;
|
---|
756 |
|
---|
757 | case TYPE_DICT:
|
---|
758 | v = PyDict_New();
|
---|
759 | if (v == NULL)
|
---|
760 | return NULL;
|
---|
761 | for (;;) {
|
---|
762 | PyObject *key, *val;
|
---|
763 | key = r_object(p);
|
---|
764 | if (key == NULL)
|
---|
765 | break;
|
---|
766 | val = r_object(p);
|
---|
767 | if (val != NULL)
|
---|
768 | PyDict_SetItem(v, key, val);
|
---|
769 | Py_DECREF(key);
|
---|
770 | Py_XDECREF(val);
|
---|
771 | }
|
---|
772 | if (PyErr_Occurred()) {
|
---|
773 | Py_DECREF(v);
|
---|
774 | v = NULL;
|
---|
775 | }
|
---|
776 | return v;
|
---|
777 |
|
---|
778 | case TYPE_SET:
|
---|
779 | case TYPE_FROZENSET:
|
---|
780 | n = r_long(p);
|
---|
781 | if (n < 0) {
|
---|
782 | PyErr_SetString(PyExc_ValueError, "bad marshal data");
|
---|
783 | return NULL;
|
---|
784 | }
|
---|
785 | v = PyTuple_New((int)n);
|
---|
786 | if (v == NULL)
|
---|
787 | return v;
|
---|
788 | for (i = 0; i < n; i++) {
|
---|
789 | v2 = r_object(p);
|
---|
790 | if ( v2 == NULL ) {
|
---|
791 | if (!PyErr_Occurred())
|
---|
792 | PyErr_SetString(PyExc_TypeError,
|
---|
793 | "NULL object in marshal data");
|
---|
794 | Py_DECREF(v);
|
---|
795 | v = NULL;
|
---|
796 | break;
|
---|
797 | }
|
---|
798 | PyTuple_SET_ITEM(v, (int)i, v2);
|
---|
799 | }
|
---|
800 | if (v == NULL)
|
---|
801 | return v;
|
---|
802 | if (type == TYPE_SET)
|
---|
803 | v3 = PySet_New(v);
|
---|
804 | else
|
---|
805 | v3 = PyFrozenSet_New(v);
|
---|
806 | Py_DECREF(v);
|
---|
807 | return v3;
|
---|
808 |
|
---|
809 | case TYPE_CODE:
|
---|
810 | if (PyEval_GetRestricted()) {
|
---|
811 | PyErr_SetString(PyExc_RuntimeError,
|
---|
812 | "cannot unmarshal code objects in "
|
---|
813 | "restricted execution mode");
|
---|
814 | return NULL;
|
---|
815 | }
|
---|
816 | else {
|
---|
817 | int argcount;
|
---|
818 | int nlocals;
|
---|
819 | int stacksize;
|
---|
820 | int flags;
|
---|
821 | PyObject *code = NULL;
|
---|
822 | PyObject *consts = NULL;
|
---|
823 | PyObject *names = NULL;
|
---|
824 | PyObject *varnames = NULL;
|
---|
825 | PyObject *freevars = NULL;
|
---|
826 | PyObject *cellvars = NULL;
|
---|
827 | PyObject *filename = NULL;
|
---|
828 | PyObject *name = NULL;
|
---|
829 | int firstlineno;
|
---|
830 | PyObject *lnotab = NULL;
|
---|
831 |
|
---|
832 | v = NULL;
|
---|
833 |
|
---|
834 | argcount = r_long(p);
|
---|
835 | nlocals = r_long(p);
|
---|
836 | stacksize = r_long(p);
|
---|
837 | flags = r_long(p);
|
---|
838 | code = r_object(p);
|
---|
839 | if (code == NULL)
|
---|
840 | goto code_error;
|
---|
841 | consts = r_object(p);
|
---|
842 | if (consts == NULL)
|
---|
843 | goto code_error;
|
---|
844 | names = r_object(p);
|
---|
845 | if (names == NULL)
|
---|
846 | goto code_error;
|
---|
847 | varnames = r_object(p);
|
---|
848 | if (varnames == NULL)
|
---|
849 | goto code_error;
|
---|
850 | freevars = r_object(p);
|
---|
851 | if (freevars == NULL)
|
---|
852 | goto code_error;
|
---|
853 | cellvars = r_object(p);
|
---|
854 | if (cellvars == NULL)
|
---|
855 | goto code_error;
|
---|
856 | filename = r_object(p);
|
---|
857 | if (filename == NULL)
|
---|
858 | goto code_error;
|
---|
859 | name = r_object(p);
|
---|
860 | if (name == NULL)
|
---|
861 | goto code_error;
|
---|
862 | firstlineno = r_long(p);
|
---|
863 | lnotab = r_object(p);
|
---|
864 | if (lnotab == NULL)
|
---|
865 | goto code_error;
|
---|
866 |
|
---|
867 | v = (PyObject *) PyCode_New(
|
---|
868 | argcount, nlocals, stacksize, flags,
|
---|
869 | code, consts, names, varnames,
|
---|
870 | freevars, cellvars, filename, name,
|
---|
871 | firstlineno, lnotab);
|
---|
872 |
|
---|
873 | code_error:
|
---|
874 | Py_XDECREF(code);
|
---|
875 | Py_XDECREF(consts);
|
---|
876 | Py_XDECREF(names);
|
---|
877 | Py_XDECREF(varnames);
|
---|
878 | Py_XDECREF(freevars);
|
---|
879 | Py_XDECREF(cellvars);
|
---|
880 | Py_XDECREF(filename);
|
---|
881 | Py_XDECREF(name);
|
---|
882 | Py_XDECREF(lnotab);
|
---|
883 |
|
---|
884 | }
|
---|
885 | return v;
|
---|
886 |
|
---|
887 | default:
|
---|
888 | /* Bogus data got written, which isn't ideal.
|
---|
889 | This will let you keep working and recover. */
|
---|
890 | PyErr_SetString(PyExc_ValueError, "bad marshal data");
|
---|
891 | return NULL;
|
---|
892 |
|
---|
893 | }
|
---|
894 | }
|
---|
895 |
|
---|
896 | static PyObject *
|
---|
897 | read_object(RFILE *p)
|
---|
898 | {
|
---|
899 | PyObject *v;
|
---|
900 | if (PyErr_Occurred()) {
|
---|
901 | fprintf(stderr, "XXX readobject called with exception set\n");
|
---|
902 | return NULL;
|
---|
903 | }
|
---|
904 | v = r_object(p);
|
---|
905 | if (v == NULL && !PyErr_Occurred())
|
---|
906 | PyErr_SetString(PyExc_TypeError, "NULL object in marshal data");
|
---|
907 | return v;
|
---|
908 | }
|
---|
909 |
|
---|
910 | int
|
---|
911 | PyMarshal_ReadShortFromFile(FILE *fp)
|
---|
912 | {
|
---|
913 | RFILE rf;
|
---|
914 | assert(fp);
|
---|
915 | rf.fp = fp;
|
---|
916 | rf.strings = NULL;
|
---|
917 | rf.end = rf.ptr = NULL;
|
---|
918 | return r_short(&rf);
|
---|
919 | }
|
---|
920 |
|
---|
921 | long
|
---|
922 | PyMarshal_ReadLongFromFile(FILE *fp)
|
---|
923 | {
|
---|
924 | RFILE rf;
|
---|
925 | rf.fp = fp;
|
---|
926 | rf.strings = NULL;
|
---|
927 | return r_long(&rf);
|
---|
928 | }
|
---|
929 |
|
---|
930 | #ifdef HAVE_FSTAT
|
---|
931 | /* Return size of file in bytes; < 0 if unknown. */
|
---|
932 | static off_t
|
---|
933 | getfilesize(FILE *fp)
|
---|
934 | {
|
---|
935 | struct stat st;
|
---|
936 | if (fstat(fileno(fp), &st) != 0)
|
---|
937 | return -1;
|
---|
938 | else
|
---|
939 | return st.st_size;
|
---|
940 | }
|
---|
941 | #endif
|
---|
942 |
|
---|
943 | /* If we can get the size of the file up-front, and it's reasonably small,
|
---|
944 | * read it in one gulp and delegate to ...FromString() instead. Much quicker
|
---|
945 | * than reading a byte at a time from file; speeds .pyc imports.
|
---|
946 | * CAUTION: since this may read the entire remainder of the file, don't
|
---|
947 | * call it unless you know you're done with the file.
|
---|
948 | */
|
---|
949 | PyObject *
|
---|
950 | PyMarshal_ReadLastObjectFromFile(FILE *fp)
|
---|
951 | {
|
---|
952 | /* 75% of 2.1's .pyc files can exploit SMALL_FILE_LIMIT.
|
---|
953 | * REASONABLE_FILE_LIMIT is by defn something big enough for Tkinter.pyc.
|
---|
954 | */
|
---|
955 | #define SMALL_FILE_LIMIT (1L << 14)
|
---|
956 | #define REASONABLE_FILE_LIMIT (1L << 18)
|
---|
957 | #ifdef HAVE_FSTAT
|
---|
958 | off_t filesize;
|
---|
959 | #endif
|
---|
960 | #ifdef HAVE_FSTAT
|
---|
961 | filesize = getfilesize(fp);
|
---|
962 | if (filesize > 0) {
|
---|
963 | char buf[SMALL_FILE_LIMIT];
|
---|
964 | char* pBuf = NULL;
|
---|
965 | if (filesize <= SMALL_FILE_LIMIT)
|
---|
966 | pBuf = buf;
|
---|
967 | else if (filesize <= REASONABLE_FILE_LIMIT)
|
---|
968 | pBuf = (char *)PyMem_MALLOC(filesize);
|
---|
969 | if (pBuf != NULL) {
|
---|
970 | PyObject* v;
|
---|
971 | size_t n;
|
---|
972 | /* filesize must fit into an int, because it
|
---|
973 | is smaller than REASONABLE_FILE_LIMIT */
|
---|
974 | n = fread(pBuf, 1, (int)filesize, fp);
|
---|
975 | v = PyMarshal_ReadObjectFromString(pBuf, n);
|
---|
976 | if (pBuf != buf)
|
---|
977 | PyMem_FREE(pBuf);
|
---|
978 | return v;
|
---|
979 | }
|
---|
980 |
|
---|
981 | }
|
---|
982 | #endif
|
---|
983 | /* We don't have fstat, or we do but the file is larger than
|
---|
984 | * REASONABLE_FILE_LIMIT or malloc failed -- read a byte at a time.
|
---|
985 | */
|
---|
986 | return PyMarshal_ReadObjectFromFile(fp);
|
---|
987 |
|
---|
988 | #undef SMALL_FILE_LIMIT
|
---|
989 | #undef REASONABLE_FILE_LIMIT
|
---|
990 | }
|
---|
991 |
|
---|
992 | PyObject *
|
---|
993 | PyMarshal_ReadObjectFromFile(FILE *fp)
|
---|
994 | {
|
---|
995 | RFILE rf;
|
---|
996 | PyObject *result;
|
---|
997 | rf.fp = fp;
|
---|
998 | rf.strings = PyList_New(0);
|
---|
999 | result = r_object(&rf);
|
---|
1000 | Py_DECREF(rf.strings);
|
---|
1001 | return result;
|
---|
1002 | }
|
---|
1003 |
|
---|
1004 | PyObject *
|
---|
1005 | PyMarshal_ReadObjectFromString(char *str, Py_ssize_t len)
|
---|
1006 | {
|
---|
1007 | RFILE rf;
|
---|
1008 | PyObject *result;
|
---|
1009 | rf.fp = NULL;
|
---|
1010 | rf.ptr = str;
|
---|
1011 | rf.end = str + len;
|
---|
1012 | rf.strings = PyList_New(0);
|
---|
1013 | result = r_object(&rf);
|
---|
1014 | Py_DECREF(rf.strings);
|
---|
1015 | return result;
|
---|
1016 | }
|
---|
1017 |
|
---|
1018 | PyObject *
|
---|
1019 | PyMarshal_WriteObjectToString(PyObject *x, int version)
|
---|
1020 | {
|
---|
1021 | WFILE wf;
|
---|
1022 | wf.fp = NULL;
|
---|
1023 | wf.str = PyString_FromStringAndSize((char *)NULL, 50);
|
---|
1024 | if (wf.str == NULL)
|
---|
1025 | return NULL;
|
---|
1026 | wf.ptr = PyString_AS_STRING((PyStringObject *)wf.str);
|
---|
1027 | wf.end = wf.ptr + PyString_Size(wf.str);
|
---|
1028 | wf.error = 0;
|
---|
1029 | wf.depth = 0;
|
---|
1030 | wf.version = version;
|
---|
1031 | wf.strings = (version > 0) ? PyDict_New() : NULL;
|
---|
1032 | w_object(x, &wf);
|
---|
1033 | Py_XDECREF(wf.strings);
|
---|
1034 | if (wf.str != NULL)
|
---|
1035 | _PyString_Resize(&wf.str,
|
---|
1036 | (int) (wf.ptr -
|
---|
1037 | PyString_AS_STRING((PyStringObject *)wf.str)));
|
---|
1038 | if (wf.error) {
|
---|
1039 | Py_XDECREF(wf.str);
|
---|
1040 | PyErr_SetString(PyExc_ValueError,
|
---|
1041 | (wf.error==1)?"unmarshallable object"
|
---|
1042 | :"object too deeply nested to marshal");
|
---|
1043 | return NULL;
|
---|
1044 | }
|
---|
1045 | return wf.str;
|
---|
1046 | }
|
---|
1047 |
|
---|
1048 | /* And an interface for Python programs... */
|
---|
1049 |
|
---|
1050 | static PyObject *
|
---|
1051 | marshal_dump(PyObject *self, PyObject *args)
|
---|
1052 | {
|
---|
1053 | WFILE wf;
|
---|
1054 | PyObject *x;
|
---|
1055 | PyObject *f;
|
---|
1056 | int version = Py_MARSHAL_VERSION;
|
---|
1057 | if (!PyArg_ParseTuple(args, "OO|i:dump", &x, &f, &version))
|
---|
1058 | return NULL;
|
---|
1059 | if (!PyFile_Check(f)) {
|
---|
1060 | PyErr_SetString(PyExc_TypeError,
|
---|
1061 | "marshal.dump() 2nd arg must be file");
|
---|
1062 | return NULL;
|
---|
1063 | }
|
---|
1064 | wf.fp = PyFile_AsFile(f);
|
---|
1065 | wf.str = NULL;
|
---|
1066 | wf.ptr = wf.end = NULL;
|
---|
1067 | wf.error = 0;
|
---|
1068 | wf.depth = 0;
|
---|
1069 | wf.strings = (version > 0) ? PyDict_New() : 0;
|
---|
1070 | wf.version = version;
|
---|
1071 | w_object(x, &wf);
|
---|
1072 | Py_XDECREF(wf.strings);
|
---|
1073 | if (wf.error) {
|
---|
1074 | PyErr_SetString(PyExc_ValueError,
|
---|
1075 | (wf.error==1)?"unmarshallable object"
|
---|
1076 | :"object too deeply nested to marshal");
|
---|
1077 | return NULL;
|
---|
1078 | }
|
---|
1079 | Py_INCREF(Py_None);
|
---|
1080 | return Py_None;
|
---|
1081 | }
|
---|
1082 |
|
---|
1083 | static PyObject *
|
---|
1084 | marshal_load(PyObject *self, PyObject *f)
|
---|
1085 | {
|
---|
1086 | RFILE rf;
|
---|
1087 | PyObject *result;
|
---|
1088 | if (!PyFile_Check(f)) {
|
---|
1089 | PyErr_SetString(PyExc_TypeError,
|
---|
1090 | "marshal.load() arg must be file");
|
---|
1091 | return NULL;
|
---|
1092 | }
|
---|
1093 | rf.fp = PyFile_AsFile(f);
|
---|
1094 | rf.strings = PyList_New(0);
|
---|
1095 | result = read_object(&rf);
|
---|
1096 | Py_DECREF(rf.strings);
|
---|
1097 | return result;
|
---|
1098 | }
|
---|
1099 |
|
---|
1100 | static PyObject *
|
---|
1101 | marshal_dumps(PyObject *self, PyObject *args)
|
---|
1102 | {
|
---|
1103 | PyObject *x;
|
---|
1104 | int version = Py_MARSHAL_VERSION;
|
---|
1105 | if (!PyArg_ParseTuple(args, "O|i:dumps", &x, &version))
|
---|
1106 | return NULL;
|
---|
1107 | return PyMarshal_WriteObjectToString(x, version);
|
---|
1108 | }
|
---|
1109 |
|
---|
1110 | static PyObject *
|
---|
1111 | marshal_loads(PyObject *self, PyObject *args)
|
---|
1112 | {
|
---|
1113 | RFILE rf;
|
---|
1114 | char *s;
|
---|
1115 | Py_ssize_t n;
|
---|
1116 | PyObject* result;
|
---|
1117 | if (!PyArg_ParseTuple(args, "s#:loads", &s, &n))
|
---|
1118 | return NULL;
|
---|
1119 | rf.fp = NULL;
|
---|
1120 | rf.ptr = s;
|
---|
1121 | rf.end = s + n;
|
---|
1122 | rf.strings = PyList_New(0);
|
---|
1123 | result = read_object(&rf);
|
---|
1124 | Py_DECREF(rf.strings);
|
---|
1125 | return result;
|
---|
1126 | }
|
---|
1127 |
|
---|
1128 | static PyMethodDef marshal_methods[] = {
|
---|
1129 | {"dump", marshal_dump, METH_VARARGS},
|
---|
1130 | {"load", marshal_load, METH_O},
|
---|
1131 | {"dumps", marshal_dumps, METH_VARARGS},
|
---|
1132 | {"loads", marshal_loads, METH_VARARGS},
|
---|
1133 | {NULL, NULL} /* sentinel */
|
---|
1134 | };
|
---|
1135 |
|
---|
1136 | PyMODINIT_FUNC
|
---|
1137 | PyMarshal_Init(void)
|
---|
1138 | {
|
---|
1139 | PyObject *mod = Py_InitModule("marshal", marshal_methods);
|
---|
1140 | if (mod == NULL)
|
---|
1141 | return;
|
---|
1142 | PyModule_AddIntConstant(mod, "version", Py_MARSHAL_VERSION);
|
---|
1143 | }
|
---|