]>
Commit | Line | Data |
---|---|---|
3ec97ca4 DM |
1 | \r |
2 | /* Write Python objects to files and read them back.\r | |
3 | This is intended for writing and reading compiled Python code only;\r | |
4 | a true persistent storage facility would be much harder, since\r | |
5 | it would have to take circular links and sharing into account. */\r | |
6 | \r | |
7 | #define PY_SSIZE_T_CLEAN\r | |
8 | \r | |
9 | #include "Python.h"\r | |
10 | #include "longintrepr.h"\r | |
11 | #include "code.h"\r | |
12 | #include "marshal.h"\r | |
13 | \r | |
14 | #define ABS(x) ((x) < 0 ? -(x) : (x))\r | |
15 | \r | |
16 | /* High water mark to determine when the marshalled object is dangerously deep\r | |
17 | * and risks coring the interpreter. When the object stack gets this deep,\r | |
18 | * raise an exception instead of continuing.\r | |
19 | */\r | |
20 | #define MAX_MARSHAL_STACK_DEPTH 2000\r | |
21 | \r | |
22 | #define TYPE_NULL '0'\r | |
23 | #define TYPE_NONE 'N'\r | |
24 | #define TYPE_FALSE 'F'\r | |
25 | #define TYPE_TRUE 'T'\r | |
26 | #define TYPE_STOPITER 'S'\r | |
27 | #define TYPE_ELLIPSIS '.'\r | |
28 | #define TYPE_INT 'i'\r | |
29 | #define TYPE_INT64 'I'\r | |
30 | #define TYPE_FLOAT 'f'\r | |
31 | #define TYPE_BINARY_FLOAT 'g'\r | |
32 | #define TYPE_COMPLEX 'x'\r | |
33 | #define TYPE_BINARY_COMPLEX 'y'\r | |
34 | #define TYPE_LONG 'l'\r | |
35 | #define TYPE_STRING 's'\r | |
36 | #define TYPE_INTERNED 't'\r | |
37 | #define TYPE_STRINGREF 'R'\r | |
38 | #define TYPE_TUPLE '('\r | |
39 | #define TYPE_LIST '['\r | |
40 | #define TYPE_DICT '{'\r | |
41 | #define TYPE_CODE 'c'\r | |
42 | #define TYPE_UNICODE 'u'\r | |
43 | #define TYPE_UNKNOWN '?'\r | |
44 | #define TYPE_SET '<'\r | |
45 | #define TYPE_FROZENSET '>'\r | |
46 | \r | |
47 | #define WFERR_OK 0\r | |
48 | #define WFERR_UNMARSHALLABLE 1\r | |
49 | #define WFERR_NESTEDTOODEEP 2\r | |
50 | #define WFERR_NOMEMORY 3\r | |
51 | \r | |
52 | typedef struct {\r | |
53 | FILE *fp;\r | |
54 | int error; /* see WFERR_* values */\r | |
55 | int depth;\r | |
56 | /* If fp == NULL, the following are valid: */\r | |
57 | PyObject *str;\r | |
58 | char *ptr;\r | |
59 | char *end;\r | |
60 | PyObject *strings; /* dict on marshal, list on unmarshal */\r | |
61 | int version;\r | |
62 | } WFILE;\r | |
63 | \r | |
64 | #define w_byte(c, p) if (((p)->fp)) putc((c), (p)->fp); \\r | |
65 | else if ((p)->ptr != (p)->end) *(p)->ptr++ = (c); \\r | |
66 | else w_more(c, p)\r | |
67 | \r | |
68 | static void\r | |
69 | w_more(int c, WFILE *p)\r | |
70 | {\r | |
71 | Py_ssize_t size, newsize;\r | |
72 | if (p->str == NULL)\r | |
73 | return; /* An error already occurred */\r | |
74 | size = PyString_Size(p->str);\r | |
75 | newsize = size + size + 1024;\r | |
76 | if (newsize > 32*1024*1024) {\r | |
77 | newsize = size + (size >> 3); /* 12.5% overallocation */\r | |
78 | }\r | |
79 | if (_PyString_Resize(&p->str, newsize) != 0) {\r | |
80 | p->ptr = p->end = NULL;\r | |
81 | }\r | |
82 | else {\r | |
83 | p->ptr = PyString_AS_STRING((PyStringObject *)p->str) + size;\r | |
84 | p->end =\r | |
85 | PyString_AS_STRING((PyStringObject *)p->str) + newsize;\r | |
86 | *p->ptr++ = Py_SAFE_DOWNCAST(c, int, char);\r | |
87 | }\r | |
88 | }\r | |
89 | \r | |
90 | static void\r | |
91 | w_string(const char *s, Py_ssize_t n, WFILE *p)\r | |
92 | {\r | |
93 | if (p->fp != NULL) {\r | |
94 | fwrite(s, 1, n, p->fp);\r | |
95 | }\r | |
96 | else {\r | |
97 | while (--n >= 0) {\r | |
98 | w_byte(*s, p);\r | |
99 | s++;\r | |
100 | }\r | |
101 | }\r | |
102 | }\r | |
103 | \r | |
104 | static void\r | |
105 | w_short(int x, WFILE *p)\r | |
106 | {\r | |
107 | w_byte((char)( x & 0xff), p);\r | |
108 | w_byte((char)((x>> 8) & 0xff), p);\r | |
109 | }\r | |
110 | \r | |
111 | static void\r | |
112 | w_long(long x, WFILE *p)\r | |
113 | {\r | |
114 | w_byte((char)( x & 0xff), p);\r | |
115 | w_byte((char)((x>> 8) & 0xff), p);\r | |
116 | w_byte((char)((x>>16) & 0xff), p);\r | |
117 | w_byte((char)((x>>24) & 0xff), p);\r | |
118 | }\r | |
119 | \r | |
120 | #if SIZEOF_LONG > 4\r | |
121 | static void\r | |
122 | w_long64(long x, WFILE *p)\r | |
123 | {\r | |
124 | w_long(x, p);\r | |
125 | w_long(x>>32, p);\r | |
126 | }\r | |
127 | #endif\r | |
128 | \r | |
129 | #define SIZE32_MAX 0x7FFFFFFF\r | |
130 | \r | |
131 | #if SIZEOF_SIZE_T > 4\r | |
132 | # define W_SIZE(n, p) do { \\r | |
133 | if ((n) > SIZE32_MAX) { \\r | |
134 | (p)->depth--; \\r | |
135 | (p)->error = WFERR_UNMARSHALLABLE; \\r | |
136 | return; \\r | |
137 | } \\r | |
138 | w_long((long)(n), p); \\r | |
139 | } while(0)\r | |
140 | #else\r | |
141 | # define W_SIZE w_long\r | |
142 | #endif\r | |
143 | \r | |
144 | static void\r | |
145 | w_pstring(const char *s, Py_ssize_t n, WFILE *p)\r | |
146 | {\r | |
147 | W_SIZE(n, p);\r | |
148 | w_string(s, n, p);\r | |
149 | }\r | |
150 | \r | |
151 | /* We assume that Python longs are stored internally in base some power of\r | |
152 | 2**15; for the sake of portability we'll always read and write them in base\r | |
153 | exactly 2**15. */\r | |
154 | \r | |
155 | #define PyLong_MARSHAL_SHIFT 15\r | |
156 | #define PyLong_MARSHAL_BASE ((short)1 << PyLong_MARSHAL_SHIFT)\r | |
157 | #define PyLong_MARSHAL_MASK (PyLong_MARSHAL_BASE - 1)\r | |
158 | #if PyLong_SHIFT % PyLong_MARSHAL_SHIFT != 0\r | |
159 | #error "PyLong_SHIFT must be a multiple of PyLong_MARSHAL_SHIFT"\r | |
160 | #endif\r | |
161 | #define PyLong_MARSHAL_RATIO (PyLong_SHIFT / PyLong_MARSHAL_SHIFT)\r | |
162 | \r | |
163 | static void\r | |
164 | w_PyLong(const PyLongObject *ob, WFILE *p)\r | |
165 | {\r | |
166 | Py_ssize_t i, j, n, l;\r | |
167 | digit d;\r | |
168 | \r | |
169 | w_byte(TYPE_LONG, p);\r | |
170 | if (Py_SIZE(ob) == 0) {\r | |
171 | w_long((long)0, p);\r | |
172 | return;\r | |
173 | }\r | |
174 | \r | |
175 | /* set l to number of base PyLong_MARSHAL_BASE digits */\r | |
176 | n = ABS(Py_SIZE(ob));\r | |
177 | l = (n-1) * PyLong_MARSHAL_RATIO;\r | |
178 | d = ob->ob_digit[n-1];\r | |
179 | assert(d != 0); /* a PyLong is always normalized */\r | |
180 | do {\r | |
181 | d >>= PyLong_MARSHAL_SHIFT;\r | |
182 | l++;\r | |
183 | } while (d != 0);\r | |
184 | if (l > SIZE32_MAX) {\r | |
185 | p->depth--;\r | |
186 | p->error = WFERR_UNMARSHALLABLE;\r | |
187 | return;\r | |
188 | }\r | |
189 | w_long((long)(Py_SIZE(ob) > 0 ? l : -l), p);\r | |
190 | \r | |
191 | for (i=0; i < n-1; i++) {\r | |
192 | d = ob->ob_digit[i];\r | |
193 | for (j=0; j < PyLong_MARSHAL_RATIO; j++) {\r | |
194 | w_short(d & PyLong_MARSHAL_MASK, p);\r | |
195 | d >>= PyLong_MARSHAL_SHIFT;\r | |
196 | }\r | |
197 | assert (d == 0);\r | |
198 | }\r | |
199 | d = ob->ob_digit[n-1];\r | |
200 | do {\r | |
201 | w_short(d & PyLong_MARSHAL_MASK, p);\r | |
202 | d >>= PyLong_MARSHAL_SHIFT;\r | |
203 | } while (d != 0);\r | |
204 | }\r | |
205 | \r | |
206 | static void\r | |
207 | w_object(PyObject *v, WFILE *p)\r | |
208 | {\r | |
209 | Py_ssize_t i, n;\r | |
210 | \r | |
211 | p->depth++;\r | |
212 | \r | |
213 | if (p->depth > MAX_MARSHAL_STACK_DEPTH) {\r | |
214 | p->error = WFERR_NESTEDTOODEEP;\r | |
215 | }\r | |
216 | else if (v == NULL) {\r | |
217 | w_byte(TYPE_NULL, p);\r | |
218 | }\r | |
219 | else if (v == Py_None) {\r | |
220 | w_byte(TYPE_NONE, p);\r | |
221 | }\r | |
222 | else if (v == PyExc_StopIteration) {\r | |
223 | w_byte(TYPE_STOPITER, p);\r | |
224 | }\r | |
225 | else if (v == Py_Ellipsis) {\r | |
226 | w_byte(TYPE_ELLIPSIS, p);\r | |
227 | }\r | |
228 | else if (v == Py_False) {\r | |
229 | w_byte(TYPE_FALSE, p);\r | |
230 | }\r | |
231 | else if (v == Py_True) {\r | |
232 | w_byte(TYPE_TRUE, p);\r | |
233 | }\r | |
234 | else if (PyInt_CheckExact(v)) {\r | |
235 | long x = PyInt_AS_LONG((PyIntObject *)v);\r | |
236 | #if SIZEOF_LONG > 4\r | |
237 | long y = Py_ARITHMETIC_RIGHT_SHIFT(long, x, 31);\r | |
238 | if (y && y != -1) {\r | |
239 | w_byte(TYPE_INT64, p);\r | |
240 | w_long64(x, p);\r | |
241 | }\r | |
242 | else\r | |
243 | #endif\r | |
244 | {\r | |
245 | w_byte(TYPE_INT, p);\r | |
246 | w_long(x, p);\r | |
247 | }\r | |
248 | }\r | |
249 | else if (PyLong_CheckExact(v)) {\r | |
250 | PyLongObject *ob = (PyLongObject *)v;\r | |
251 | w_PyLong(ob, p);\r | |
252 | }\r | |
253 | else if (PyFloat_CheckExact(v)) {\r | |
254 | if (p->version > 1) {\r | |
255 | unsigned char buf[8];\r | |
256 | if (_PyFloat_Pack8(PyFloat_AsDouble(v),\r | |
257 | buf, 1) < 0) {\r | |
258 | p->error = WFERR_UNMARSHALLABLE;\r | |
259 | return;\r | |
260 | }\r | |
261 | w_byte(TYPE_BINARY_FLOAT, p);\r | |
262 | w_string((char*)buf, 8, p);\r | |
263 | }\r | |
264 | else {\r | |
265 | char *buf = PyOS_double_to_string(PyFloat_AS_DOUBLE(v),\r | |
266 | 'g', 17, 0, NULL);\r | |
267 | if (!buf) {\r | |
268 | p->error = WFERR_NOMEMORY;\r | |
269 | return;\r | |
270 | }\r | |
271 | n = strlen(buf);\r | |
272 | w_byte(TYPE_FLOAT, p);\r | |
273 | w_byte((int)n, p);\r | |
274 | w_string(buf, n, p);\r | |
275 | PyMem_Free(buf);\r | |
276 | }\r | |
277 | }\r | |
278 | #ifndef WITHOUT_COMPLEX\r | |
279 | else if (PyComplex_CheckExact(v)) {\r | |
280 | if (p->version > 1) {\r | |
281 | unsigned char buf[8];\r | |
282 | if (_PyFloat_Pack8(PyComplex_RealAsDouble(v),\r | |
283 | buf, 1) < 0) {\r | |
284 | p->error = WFERR_UNMARSHALLABLE;\r | |
285 | return;\r | |
286 | }\r | |
287 | w_byte(TYPE_BINARY_COMPLEX, p);\r | |
288 | w_string((char*)buf, 8, p);\r | |
289 | if (_PyFloat_Pack8(PyComplex_ImagAsDouble(v),\r | |
290 | buf, 1) < 0) {\r | |
291 | p->error = WFERR_UNMARSHALLABLE;\r | |
292 | return;\r | |
293 | }\r | |
294 | w_string((char*)buf, 8, p);\r | |
295 | }\r | |
296 | else {\r | |
297 | char *buf;\r | |
298 | w_byte(TYPE_COMPLEX, p);\r | |
299 | buf = PyOS_double_to_string(PyComplex_RealAsDouble(v),\r | |
300 | 'g', 17, 0, NULL);\r | |
301 | if (!buf) {\r | |
302 | p->error = WFERR_NOMEMORY;\r | |
303 | return;\r | |
304 | }\r | |
305 | n = strlen(buf);\r | |
306 | w_byte((int)n, p);\r | |
307 | w_string(buf, n, p);\r | |
308 | PyMem_Free(buf);\r | |
309 | buf = PyOS_double_to_string(PyComplex_ImagAsDouble(v),\r | |
310 | 'g', 17, 0, NULL);\r | |
311 | if (!buf) {\r | |
312 | p->error = WFERR_NOMEMORY;\r | |
313 | return;\r | |
314 | }\r | |
315 | n = strlen(buf);\r | |
316 | w_byte((int)n, p);\r | |
317 | w_string(buf, n, p);\r | |
318 | PyMem_Free(buf);\r | |
319 | }\r | |
320 | }\r | |
321 | #endif\r | |
322 | else if (PyString_CheckExact(v)) {\r | |
323 | if (p->strings && PyString_CHECK_INTERNED(v)) {\r | |
324 | PyObject *o = PyDict_GetItem(p->strings, v);\r | |
325 | if (o) {\r | |
326 | long w = PyInt_AsLong(o);\r | |
327 | w_byte(TYPE_STRINGREF, p);\r | |
328 | w_long(w, p);\r | |
329 | goto exit;\r | |
330 | }\r | |
331 | else {\r | |
332 | int ok;\r | |
333 | o = PyInt_FromSsize_t(PyDict_Size(p->strings));\r | |
334 | ok = o &&\r | |
335 | PyDict_SetItem(p->strings, v, o) >= 0;\r | |
336 | Py_XDECREF(o);\r | |
337 | if (!ok) {\r | |
338 | p->depth--;\r | |
339 | p->error = WFERR_UNMARSHALLABLE;\r | |
340 | return;\r | |
341 | }\r | |
342 | w_byte(TYPE_INTERNED, p);\r | |
343 | }\r | |
344 | }\r | |
345 | else {\r | |
346 | w_byte(TYPE_STRING, p);\r | |
347 | }\r | |
348 | w_pstring(PyBytes_AS_STRING(v), PyString_GET_SIZE(v), p);\r | |
349 | }\r | |
350 | #ifdef Py_USING_UNICODE\r | |
351 | else if (PyUnicode_CheckExact(v)) {\r | |
352 | PyObject *utf8;\r | |
353 | utf8 = PyUnicode_AsUTF8String(v);\r | |
354 | if (utf8 == NULL) {\r | |
355 | p->depth--;\r | |
356 | p->error = WFERR_UNMARSHALLABLE;\r | |
357 | return;\r | |
358 | }\r | |
359 | w_byte(TYPE_UNICODE, p);\r | |
360 | w_pstring(PyString_AS_STRING(utf8), PyString_GET_SIZE(utf8), p);\r | |
361 | Py_DECREF(utf8);\r | |
362 | }\r | |
363 | #endif\r | |
364 | else if (PyTuple_CheckExact(v)) {\r | |
365 | w_byte(TYPE_TUPLE, p);\r | |
366 | n = PyTuple_Size(v);\r | |
367 | W_SIZE(n, p);\r | |
368 | for (i = 0; i < n; i++) {\r | |
369 | w_object(PyTuple_GET_ITEM(v, i), p);\r | |
370 | }\r | |
371 | }\r | |
372 | else if (PyList_CheckExact(v)) {\r | |
373 | w_byte(TYPE_LIST, p);\r | |
374 | n = PyList_GET_SIZE(v);\r | |
375 | W_SIZE(n, p);\r | |
376 | for (i = 0; i < n; i++) {\r | |
377 | w_object(PyList_GET_ITEM(v, i), p);\r | |
378 | }\r | |
379 | }\r | |
380 | else if (PyDict_CheckExact(v)) {\r | |
381 | Py_ssize_t pos;\r | |
382 | PyObject *key, *value;\r | |
383 | w_byte(TYPE_DICT, p);\r | |
384 | /* This one is NULL object terminated! */\r | |
385 | pos = 0;\r | |
386 | while (PyDict_Next(v, &pos, &key, &value)) {\r | |
387 | w_object(key, p);\r | |
388 | w_object(value, p);\r | |
389 | }\r | |
390 | w_object((PyObject *)NULL, p);\r | |
391 | }\r | |
392 | else if (PyAnySet_CheckExact(v)) {\r | |
393 | PyObject *value, *it;\r | |
394 | \r | |
395 | if (PyObject_TypeCheck(v, &PySet_Type))\r | |
396 | w_byte(TYPE_SET, p);\r | |
397 | else\r | |
398 | w_byte(TYPE_FROZENSET, p);\r | |
399 | n = PyObject_Size(v);\r | |
400 | if (n == -1) {\r | |
401 | p->depth--;\r | |
402 | p->error = WFERR_UNMARSHALLABLE;\r | |
403 | return;\r | |
404 | }\r | |
405 | W_SIZE(n, p);\r | |
406 | it = PyObject_GetIter(v);\r | |
407 | if (it == NULL) {\r | |
408 | p->depth--;\r | |
409 | p->error = WFERR_UNMARSHALLABLE;\r | |
410 | return;\r | |
411 | }\r | |
412 | while ((value = PyIter_Next(it)) != NULL) {\r | |
413 | w_object(value, p);\r | |
414 | Py_DECREF(value);\r | |
415 | }\r | |
416 | Py_DECREF(it);\r | |
417 | if (PyErr_Occurred()) {\r | |
418 | p->depth--;\r | |
419 | p->error = WFERR_UNMARSHALLABLE;\r | |
420 | return;\r | |
421 | }\r | |
422 | }\r | |
423 | else if (PyCode_Check(v)) {\r | |
424 | PyCodeObject *co = (PyCodeObject *)v;\r | |
425 | w_byte(TYPE_CODE, p);\r | |
426 | w_long(co->co_argcount, p);\r | |
427 | w_long(co->co_nlocals, p);\r | |
428 | w_long(co->co_stacksize, p);\r | |
429 | w_long(co->co_flags, p);\r | |
430 | w_object(co->co_code, p);\r | |
431 | w_object(co->co_consts, p);\r | |
432 | w_object(co->co_names, p);\r | |
433 | w_object(co->co_varnames, p);\r | |
434 | w_object(co->co_freevars, p);\r | |
435 | w_object(co->co_cellvars, p);\r | |
436 | w_object(co->co_filename, p);\r | |
437 | w_object(co->co_name, p);\r | |
438 | w_long(co->co_firstlineno, p);\r | |
439 | w_object(co->co_lnotab, p);\r | |
440 | }\r | |
441 | else if (PyObject_CheckReadBuffer(v)) {\r | |
442 | /* Write unknown buffer-style objects as a string */\r | |
443 | char *s;\r | |
444 | PyBufferProcs *pb = v->ob_type->tp_as_buffer;\r | |
445 | w_byte(TYPE_STRING, p);\r | |
446 | n = (*pb->bf_getreadbuffer)(v, 0, (void **)&s);\r | |
447 | w_pstring(s, n, p);\r | |
448 | }\r | |
449 | else {\r | |
450 | w_byte(TYPE_UNKNOWN, p);\r | |
451 | p->error = WFERR_UNMARSHALLABLE;\r | |
452 | }\r | |
453 | exit:\r | |
454 | p->depth--;\r | |
455 | }\r | |
456 | \r | |
457 | /* version currently has no effect for writing longs. */\r | |
458 | void\r | |
459 | PyMarshal_WriteLongToFile(long x, FILE *fp, int version)\r | |
460 | {\r | |
461 | WFILE wf;\r | |
462 | wf.fp = fp;\r | |
463 | wf.error = WFERR_OK;\r | |
464 | wf.depth = 0;\r | |
465 | wf.strings = NULL;\r | |
466 | wf.version = version;\r | |
467 | w_long(x, &wf);\r | |
468 | }\r | |
469 | \r | |
470 | void\r | |
471 | PyMarshal_WriteObjectToFile(PyObject *x, FILE *fp, int version)\r | |
472 | {\r | |
473 | WFILE wf;\r | |
474 | wf.fp = fp;\r | |
475 | wf.error = WFERR_OK;\r | |
476 | wf.depth = 0;\r | |
477 | wf.strings = (version > 0) ? PyDict_New() : NULL;\r | |
478 | wf.version = version;\r | |
479 | w_object(x, &wf);\r | |
480 | Py_XDECREF(wf.strings);\r | |
481 | }\r | |
482 | \r | |
483 | typedef WFILE RFILE; /* Same struct with different invariants */\r | |
484 | \r | |
485 | #define rs_byte(p) (((p)->ptr < (p)->end) ? (unsigned char)*(p)->ptr++ : EOF)\r | |
486 | \r | |
487 | #define r_byte(p) ((p)->fp ? getc((p)->fp) : rs_byte(p))\r | |
488 | \r | |
489 | static Py_ssize_t\r | |
490 | r_string(char *s, Py_ssize_t n, RFILE *p)\r | |
491 | {\r | |
492 | if (p->fp != NULL)\r | |
493 | /* The result fits into int because it must be <=n. */\r | |
494 | return fread(s, 1, n, p->fp);\r | |
495 | if (p->end - p->ptr < n)\r | |
496 | n = p->end - p->ptr;\r | |
497 | memcpy(s, p->ptr, n);\r | |
498 | p->ptr += n;\r | |
499 | return n;\r | |
500 | }\r | |
501 | \r | |
502 | static int\r | |
503 | r_short(RFILE *p)\r | |
504 | {\r | |
505 | register short x;\r | |
506 | x = r_byte(p);\r | |
507 | x |= r_byte(p) << 8;\r | |
508 | /* Sign-extension, in case short greater than 16 bits */\r | |
509 | x |= -(x & 0x8000);\r | |
510 | return x;\r | |
511 | }\r | |
512 | \r | |
513 | static long\r | |
514 | r_long(RFILE *p)\r | |
515 | {\r | |
516 | register long x;\r | |
517 | register FILE *fp = p->fp;\r | |
518 | if (fp) {\r | |
519 | x = getc(fp);\r | |
520 | x |= (long)getc(fp) << 8;\r | |
521 | x |= (long)getc(fp) << 16;\r | |
522 | x |= (long)getc(fp) << 24;\r | |
523 | }\r | |
524 | else {\r | |
525 | x = rs_byte(p);\r | |
526 | x |= (long)rs_byte(p) << 8;\r | |
527 | x |= (long)rs_byte(p) << 16;\r | |
528 | x |= (long)rs_byte(p) << 24;\r | |
529 | }\r | |
530 | #if SIZEOF_LONG > 4\r | |
531 | /* Sign extension for 64-bit machines */\r | |
532 | x |= -(x & 0x80000000L);\r | |
533 | #endif\r | |
534 | return x;\r | |
535 | }\r | |
536 | \r | |
537 | /* r_long64 deals with the TYPE_INT64 code. On a machine with\r | |
538 | sizeof(long) > 4, it returns a Python int object, else a Python long\r | |
539 | object. Note that w_long64 writes out TYPE_INT if 32 bits is enough,\r | |
540 | so there's no inefficiency here in returning a PyLong on 32-bit boxes\r | |
541 | for everything written via TYPE_INT64 (i.e., if an int is written via\r | |
542 | TYPE_INT64, it *needs* more than 32 bits).\r | |
543 | */\r | |
544 | static PyObject *\r | |
545 | r_long64(RFILE *p)\r | |
546 | {\r | |
547 | long lo4 = r_long(p);\r | |
548 | long hi4 = r_long(p);\r | |
549 | #if SIZEOF_LONG > 4\r | |
550 | long x = (hi4 << 32) | (lo4 & 0xFFFFFFFFL);\r | |
551 | return PyInt_FromLong(x);\r | |
552 | #else\r | |
553 | unsigned char buf[8];\r | |
554 | int one = 1;\r | |
555 | int is_little_endian = (int)*(char*)&one;\r | |
556 | if (is_little_endian) {\r | |
557 | memcpy(buf, &lo4, 4);\r | |
558 | memcpy(buf+4, &hi4, 4);\r | |
559 | }\r | |
560 | else {\r | |
561 | memcpy(buf, &hi4, 4);\r | |
562 | memcpy(buf+4, &lo4, 4);\r | |
563 | }\r | |
564 | return _PyLong_FromByteArray(buf, 8, is_little_endian, 1);\r | |
565 | #endif\r | |
566 | }\r | |
567 | \r | |
568 | static PyObject *\r | |
569 | r_PyLong(RFILE *p)\r | |
570 | {\r | |
571 | PyLongObject *ob;\r | |
572 | long n, size, i;\r | |
573 | int j, md, shorts_in_top_digit;\r | |
574 | digit d;\r | |
575 | \r | |
576 | n = r_long(p);\r | |
577 | if (n == 0)\r | |
578 | return (PyObject *)_PyLong_New(0);\r | |
579 | if (n < -SIZE32_MAX || n > SIZE32_MAX) {\r | |
580 | PyErr_SetString(PyExc_ValueError,\r | |
581 | "bad marshal data (long size out of range)");\r | |
582 | return NULL;\r | |
583 | }\r | |
584 | \r | |
585 | size = 1 + (ABS(n) - 1) / PyLong_MARSHAL_RATIO;\r | |
586 | shorts_in_top_digit = 1 + (ABS(n) - 1) % PyLong_MARSHAL_RATIO;\r | |
587 | ob = _PyLong_New(size);\r | |
588 | if (ob == NULL)\r | |
589 | return NULL;\r | |
590 | Py_SIZE(ob) = n > 0 ? size : -size;\r | |
591 | \r | |
592 | for (i = 0; i < size-1; i++) {\r | |
593 | d = 0;\r | |
594 | for (j=0; j < PyLong_MARSHAL_RATIO; j++) {\r | |
595 | md = r_short(p);\r | |
596 | if (md < 0 || md > PyLong_MARSHAL_BASE)\r | |
597 | goto bad_digit;\r | |
598 | d += (digit)md << j*PyLong_MARSHAL_SHIFT;\r | |
599 | }\r | |
600 | ob->ob_digit[i] = d;\r | |
601 | }\r | |
602 | d = 0;\r | |
603 | for (j=0; j < shorts_in_top_digit; j++) {\r | |
604 | md = r_short(p);\r | |
605 | if (md < 0 || md > PyLong_MARSHAL_BASE)\r | |
606 | goto bad_digit;\r | |
607 | /* topmost marshal digit should be nonzero */\r | |
608 | if (md == 0 && j == shorts_in_top_digit - 1) {\r | |
609 | Py_DECREF(ob);\r | |
610 | PyErr_SetString(PyExc_ValueError,\r | |
611 | "bad marshal data (unnormalized long data)");\r | |
612 | return NULL;\r | |
613 | }\r | |
614 | d += (digit)md << j*PyLong_MARSHAL_SHIFT;\r | |
615 | }\r | |
616 | /* top digit should be nonzero, else the resulting PyLong won't be\r | |
617 | normalized */\r | |
618 | ob->ob_digit[size-1] = d;\r | |
619 | return (PyObject *)ob;\r | |
620 | bad_digit:\r | |
621 | Py_DECREF(ob);\r | |
622 | PyErr_SetString(PyExc_ValueError,\r | |
623 | "bad marshal data (digit out of range in long)");\r | |
624 | return NULL;\r | |
625 | }\r | |
626 | \r | |
627 | \r | |
628 | static PyObject *\r | |
629 | r_object(RFILE *p)\r | |
630 | {\r | |
631 | /* NULL is a valid return value, it does not necessarily means that\r | |
632 | an exception is set. */\r | |
633 | PyObject *v, *v2;\r | |
634 | long i, n;\r | |
635 | int type = r_byte(p);\r | |
636 | PyObject *retval;\r | |
637 | \r | |
638 | p->depth++;\r | |
639 | \r | |
640 | if (p->depth > MAX_MARSHAL_STACK_DEPTH) {\r | |
641 | p->depth--;\r | |
642 | PyErr_SetString(PyExc_ValueError, "recursion limit exceeded");\r | |
643 | return NULL;\r | |
644 | }\r | |
645 | \r | |
646 | switch (type) {\r | |
647 | \r | |
648 | case EOF:\r | |
649 | PyErr_SetString(PyExc_EOFError,\r | |
650 | "EOF read where object expected");\r | |
651 | retval = NULL;\r | |
652 | break;\r | |
653 | \r | |
654 | case TYPE_NULL:\r | |
655 | retval = NULL;\r | |
656 | break;\r | |
657 | \r | |
658 | case TYPE_NONE:\r | |
659 | Py_INCREF(Py_None);\r | |
660 | retval = Py_None;\r | |
661 | break;\r | |
662 | \r | |
663 | case TYPE_STOPITER:\r | |
664 | Py_INCREF(PyExc_StopIteration);\r | |
665 | retval = PyExc_StopIteration;\r | |
666 | break;\r | |
667 | \r | |
668 | case TYPE_ELLIPSIS:\r | |
669 | Py_INCREF(Py_Ellipsis);\r | |
670 | retval = Py_Ellipsis;\r | |
671 | break;\r | |
672 | \r | |
673 | case TYPE_FALSE:\r | |
674 | Py_INCREF(Py_False);\r | |
675 | retval = Py_False;\r | |
676 | break;\r | |
677 | \r | |
678 | case TYPE_TRUE:\r | |
679 | Py_INCREF(Py_True);\r | |
680 | retval = Py_True;\r | |
681 | break;\r | |
682 | \r | |
683 | case TYPE_INT:\r | |
684 | retval = PyInt_FromLong(r_long(p));\r | |
685 | break;\r | |
686 | \r | |
687 | case TYPE_INT64:\r | |
688 | retval = r_long64(p);\r | |
689 | break;\r | |
690 | \r | |
691 | case TYPE_LONG:\r | |
692 | retval = r_PyLong(p);\r | |
693 | break;\r | |
694 | \r | |
695 | case TYPE_FLOAT:\r | |
696 | {\r | |
697 | char buf[256];\r | |
698 | double dx;\r | |
699 | n = r_byte(p);\r | |
700 | if (n == EOF || r_string(buf, n, p) != n) {\r | |
701 | PyErr_SetString(PyExc_EOFError,\r | |
702 | "EOF read where object expected");\r | |
703 | retval = NULL;\r | |
704 | break;\r | |
705 | }\r | |
706 | buf[n] = '\0';\r | |
707 | dx = PyOS_string_to_double(buf, NULL, NULL);\r | |
708 | if (dx == -1.0 && PyErr_Occurred()) {\r | |
709 | retval = NULL;\r | |
710 | break;\r | |
711 | }\r | |
712 | retval = PyFloat_FromDouble(dx);\r | |
713 | break;\r | |
714 | }\r | |
715 | \r | |
716 | case TYPE_BINARY_FLOAT:\r | |
717 | {\r | |
718 | unsigned char buf[8];\r | |
719 | double x;\r | |
720 | if (r_string((char*)buf, 8, p) != 8) {\r | |
721 | PyErr_SetString(PyExc_EOFError,\r | |
722 | "EOF read where object expected");\r | |
723 | retval = NULL;\r | |
724 | break;\r | |
725 | }\r | |
726 | x = _PyFloat_Unpack8(buf, 1);\r | |
727 | if (x == -1.0 && PyErr_Occurred()) {\r | |
728 | retval = NULL;\r | |
729 | break;\r | |
730 | }\r | |
731 | retval = PyFloat_FromDouble(x);\r | |
732 | break;\r | |
733 | }\r | |
734 | \r | |
735 | #ifndef WITHOUT_COMPLEX\r | |
736 | case TYPE_COMPLEX:\r | |
737 | {\r | |
738 | char buf[256];\r | |
739 | Py_complex c;\r | |
740 | n = r_byte(p);\r | |
741 | if (n == EOF || r_string(buf, n, p) != n) {\r | |
742 | PyErr_SetString(PyExc_EOFError,\r | |
743 | "EOF read where object expected");\r | |
744 | retval = NULL;\r | |
745 | break;\r | |
746 | }\r | |
747 | buf[n] = '\0';\r | |
748 | c.real = PyOS_string_to_double(buf, NULL, NULL);\r | |
749 | if (c.real == -1.0 && PyErr_Occurred()) {\r | |
750 | retval = NULL;\r | |
751 | break;\r | |
752 | }\r | |
753 | n = r_byte(p);\r | |
754 | if (n == EOF || r_string(buf, n, p) != n) {\r | |
755 | PyErr_SetString(PyExc_EOFError,\r | |
756 | "EOF read where object expected");\r | |
757 | retval = NULL;\r | |
758 | break;\r | |
759 | }\r | |
760 | buf[n] = '\0';\r | |
761 | c.imag = PyOS_string_to_double(buf, NULL, NULL);\r | |
762 | if (c.imag == -1.0 && PyErr_Occurred()) {\r | |
763 | retval = NULL;\r | |
764 | break;\r | |
765 | }\r | |
766 | retval = PyComplex_FromCComplex(c);\r | |
767 | break;\r | |
768 | }\r | |
769 | \r | |
770 | case TYPE_BINARY_COMPLEX:\r | |
771 | {\r | |
772 | unsigned char buf[8];\r | |
773 | Py_complex c;\r | |
774 | if (r_string((char*)buf, 8, p) != 8) {\r | |
775 | PyErr_SetString(PyExc_EOFError,\r | |
776 | "EOF read where object expected");\r | |
777 | retval = NULL;\r | |
778 | break;\r | |
779 | }\r | |
780 | c.real = _PyFloat_Unpack8(buf, 1);\r | |
781 | if (c.real == -1.0 && PyErr_Occurred()) {\r | |
782 | retval = NULL;\r | |
783 | break;\r | |
784 | }\r | |
785 | if (r_string((char*)buf, 8, p) != 8) {\r | |
786 | PyErr_SetString(PyExc_EOFError,\r | |
787 | "EOF read where object expected");\r | |
788 | retval = NULL;\r | |
789 | break;\r | |
790 | }\r | |
791 | c.imag = _PyFloat_Unpack8(buf, 1);\r | |
792 | if (c.imag == -1.0 && PyErr_Occurred()) {\r | |
793 | retval = NULL;\r | |
794 | break;\r | |
795 | }\r | |
796 | retval = PyComplex_FromCComplex(c);\r | |
797 | break;\r | |
798 | }\r | |
799 | #endif\r | |
800 | \r | |
801 | case TYPE_INTERNED:\r | |
802 | case TYPE_STRING:\r | |
803 | n = r_long(p);\r | |
804 | if (n < 0 || n > SIZE32_MAX) {\r | |
805 | PyErr_SetString(PyExc_ValueError, "bad marshal data (string size out of range)");\r | |
806 | retval = NULL;\r | |
807 | break;\r | |
808 | }\r | |
809 | v = PyString_FromStringAndSize((char *)NULL, n);\r | |
810 | if (v == NULL) {\r | |
811 | retval = NULL;\r | |
812 | break;\r | |
813 | }\r | |
814 | if (r_string(PyString_AS_STRING(v), n, p) != n) {\r | |
815 | Py_DECREF(v);\r | |
816 | PyErr_SetString(PyExc_EOFError,\r | |
817 | "EOF read where object expected");\r | |
818 | retval = NULL;\r | |
819 | break;\r | |
820 | }\r | |
821 | if (type == TYPE_INTERNED) {\r | |
822 | PyString_InternInPlace(&v);\r | |
823 | if (PyList_Append(p->strings, v) < 0) {\r | |
824 | retval = NULL;\r | |
825 | break;\r | |
826 | }\r | |
827 | }\r | |
828 | retval = v;\r | |
829 | break;\r | |
830 | \r | |
831 | case TYPE_STRINGREF:\r | |
832 | n = r_long(p);\r | |
833 | if (n < 0 || n >= PyList_GET_SIZE(p->strings)) {\r | |
834 | PyErr_SetString(PyExc_ValueError, "bad marshal data (string ref out of range)");\r | |
835 | retval = NULL;\r | |
836 | break;\r | |
837 | }\r | |
838 | v = PyList_GET_ITEM(p->strings, n);\r | |
839 | Py_INCREF(v);\r | |
840 | retval = v;\r | |
841 | break;\r | |
842 | \r | |
843 | #ifdef Py_USING_UNICODE\r | |
844 | case TYPE_UNICODE:\r | |
845 | {\r | |
846 | char *buffer;\r | |
847 | \r | |
848 | n = r_long(p);\r | |
849 | if (n < 0 || n > SIZE32_MAX) {\r | |
850 | PyErr_SetString(PyExc_ValueError, "bad marshal data (unicode size out of range)");\r | |
851 | retval = NULL;\r | |
852 | break;\r | |
853 | }\r | |
854 | buffer = PyMem_NEW(char, n);\r | |
855 | if (buffer == NULL) {\r | |
856 | retval = PyErr_NoMemory();\r | |
857 | break;\r | |
858 | }\r | |
859 | if (r_string(buffer, n, p) != n) {\r | |
860 | PyMem_DEL(buffer);\r | |
861 | PyErr_SetString(PyExc_EOFError,\r | |
862 | "EOF read where object expected");\r | |
863 | retval = NULL;\r | |
864 | break;\r | |
865 | }\r | |
866 | v = PyUnicode_DecodeUTF8(buffer, n, NULL);\r | |
867 | PyMem_DEL(buffer);\r | |
868 | retval = v;\r | |
869 | break;\r | |
870 | }\r | |
871 | #endif\r | |
872 | \r | |
873 | case TYPE_TUPLE:\r | |
874 | n = r_long(p);\r | |
875 | if (n < 0 || n > SIZE32_MAX) {\r | |
876 | PyErr_SetString(PyExc_ValueError, "bad marshal data (tuple size out of range)");\r | |
877 | retval = NULL;\r | |
878 | break;\r | |
879 | }\r | |
880 | v = PyTuple_New(n);\r | |
881 | if (v == NULL) {\r | |
882 | retval = NULL;\r | |
883 | break;\r | |
884 | }\r | |
885 | for (i = 0; i < n; i++) {\r | |
886 | v2 = r_object(p);\r | |
887 | if ( v2 == NULL ) {\r | |
888 | if (!PyErr_Occurred())\r | |
889 | PyErr_SetString(PyExc_TypeError,\r | |
890 | "NULL object in marshal data for tuple");\r | |
891 | Py_DECREF(v);\r | |
892 | v = NULL;\r | |
893 | break;\r | |
894 | }\r | |
895 | PyTuple_SET_ITEM(v, i, v2);\r | |
896 | }\r | |
897 | retval = v;\r | |
898 | break;\r | |
899 | \r | |
900 | case TYPE_LIST:\r | |
901 | n = r_long(p);\r | |
902 | if (n < 0 || n > SIZE32_MAX) {\r | |
903 | PyErr_SetString(PyExc_ValueError, "bad marshal data (list size out of range)");\r | |
904 | retval = NULL;\r | |
905 | break;\r | |
906 | }\r | |
907 | v = PyList_New(n);\r | |
908 | if (v == NULL) {\r | |
909 | retval = NULL;\r | |
910 | break;\r | |
911 | }\r | |
912 | for (i = 0; i < n; i++) {\r | |
913 | v2 = r_object(p);\r | |
914 | if ( v2 == NULL ) {\r | |
915 | if (!PyErr_Occurred())\r | |
916 | PyErr_SetString(PyExc_TypeError,\r | |
917 | "NULL object in marshal data for list");\r | |
918 | Py_DECREF(v);\r | |
919 | v = NULL;\r | |
920 | break;\r | |
921 | }\r | |
922 | PyList_SET_ITEM(v, i, v2);\r | |
923 | }\r | |
924 | retval = v;\r | |
925 | break;\r | |
926 | \r | |
927 | case TYPE_DICT:\r | |
928 | v = PyDict_New();\r | |
929 | if (v == NULL) {\r | |
930 | retval = NULL;\r | |
931 | break;\r | |
932 | }\r | |
933 | for (;;) {\r | |
934 | PyObject *key, *val;\r | |
935 | key = r_object(p);\r | |
936 | if (key == NULL)\r | |
937 | break;\r | |
938 | val = r_object(p);\r | |
939 | if (val != NULL)\r | |
940 | PyDict_SetItem(v, key, val);\r | |
941 | Py_DECREF(key);\r | |
942 | Py_XDECREF(val);\r | |
943 | }\r | |
944 | if (PyErr_Occurred()) {\r | |
945 | Py_DECREF(v);\r | |
946 | v = NULL;\r | |
947 | }\r | |
948 | retval = v;\r | |
949 | break;\r | |
950 | \r | |
951 | case TYPE_SET:\r | |
952 | case TYPE_FROZENSET:\r | |
953 | n = r_long(p);\r | |
954 | if (n < 0 || n > SIZE32_MAX) {\r | |
955 | PyErr_SetString(PyExc_ValueError, "bad marshal data (set size out of range)");\r | |
956 | retval = NULL;\r | |
957 | break;\r | |
958 | }\r | |
959 | v = (type == TYPE_SET) ? PySet_New(NULL) : PyFrozenSet_New(NULL);\r | |
960 | if (v == NULL) {\r | |
961 | retval = NULL;\r | |
962 | break;\r | |
963 | }\r | |
964 | for (i = 0; i < n; i++) {\r | |
965 | v2 = r_object(p);\r | |
966 | if ( v2 == NULL ) {\r | |
967 | if (!PyErr_Occurred())\r | |
968 | PyErr_SetString(PyExc_TypeError,\r | |
969 | "NULL object in marshal data for set");\r | |
970 | Py_DECREF(v);\r | |
971 | v = NULL;\r | |
972 | break;\r | |
973 | }\r | |
974 | if (PySet_Add(v, v2) == -1) {\r | |
975 | Py_DECREF(v);\r | |
976 | Py_DECREF(v2);\r | |
977 | v = NULL;\r | |
978 | break;\r | |
979 | }\r | |
980 | Py_DECREF(v2);\r | |
981 | }\r | |
982 | retval = v;\r | |
983 | break;\r | |
984 | \r | |
985 | case TYPE_CODE:\r | |
986 | if (PyEval_GetRestricted()) {\r | |
987 | PyErr_SetString(PyExc_RuntimeError,\r | |
988 | "cannot unmarshal code objects in "\r | |
989 | "restricted execution mode");\r | |
990 | retval = NULL;\r | |
991 | break;\r | |
992 | }\r | |
993 | else {\r | |
994 | int argcount;\r | |
995 | int nlocals;\r | |
996 | int stacksize;\r | |
997 | int flags;\r | |
998 | PyObject *code = NULL;\r | |
999 | PyObject *consts = NULL;\r | |
1000 | PyObject *names = NULL;\r | |
1001 | PyObject *varnames = NULL;\r | |
1002 | PyObject *freevars = NULL;\r | |
1003 | PyObject *cellvars = NULL;\r | |
1004 | PyObject *filename = NULL;\r | |
1005 | PyObject *name = NULL;\r | |
1006 | int firstlineno;\r | |
1007 | PyObject *lnotab = NULL;\r | |
1008 | \r | |
1009 | v = NULL;\r | |
1010 | \r | |
1011 | /* XXX ignore long->int overflows for now */\r | |
1012 | argcount = (int)r_long(p);\r | |
1013 | nlocals = (int)r_long(p);\r | |
1014 | stacksize = (int)r_long(p);\r | |
1015 | flags = (int)r_long(p);\r | |
1016 | code = r_object(p);\r | |
1017 | if (code == NULL)\r | |
1018 | goto code_error;\r | |
1019 | consts = r_object(p);\r | |
1020 | if (consts == NULL)\r | |
1021 | goto code_error;\r | |
1022 | names = r_object(p);\r | |
1023 | if (names == NULL)\r | |
1024 | goto code_error;\r | |
1025 | varnames = r_object(p);\r | |
1026 | if (varnames == NULL)\r | |
1027 | goto code_error;\r | |
1028 | freevars = r_object(p);\r | |
1029 | if (freevars == NULL)\r | |
1030 | goto code_error;\r | |
1031 | cellvars = r_object(p);\r | |
1032 | if (cellvars == NULL)\r | |
1033 | goto code_error;\r | |
1034 | filename = r_object(p);\r | |
1035 | if (filename == NULL)\r | |
1036 | goto code_error;\r | |
1037 | name = r_object(p);\r | |
1038 | if (name == NULL)\r | |
1039 | goto code_error;\r | |
1040 | firstlineno = (int)r_long(p);\r | |
1041 | lnotab = r_object(p);\r | |
1042 | if (lnotab == NULL)\r | |
1043 | goto code_error;\r | |
1044 | \r | |
1045 | v = (PyObject *) PyCode_New(\r | |
1046 | argcount, nlocals, stacksize, flags,\r | |
1047 | code, consts, names, varnames,\r | |
1048 | freevars, cellvars, filename, name,\r | |
1049 | firstlineno, lnotab);\r | |
1050 | \r | |
1051 | code_error:\r | |
1052 | Py_XDECREF(code);\r | |
1053 | Py_XDECREF(consts);\r | |
1054 | Py_XDECREF(names);\r | |
1055 | Py_XDECREF(varnames);\r | |
1056 | Py_XDECREF(freevars);\r | |
1057 | Py_XDECREF(cellvars);\r | |
1058 | Py_XDECREF(filename);\r | |
1059 | Py_XDECREF(name);\r | |
1060 | Py_XDECREF(lnotab);\r | |
1061 | \r | |
1062 | }\r | |
1063 | retval = v;\r | |
1064 | break;\r | |
1065 | \r | |
1066 | default:\r | |
1067 | /* Bogus data got written, which isn't ideal.\r | |
1068 | This will let you keep working and recover. */\r | |
1069 | PyErr_SetString(PyExc_ValueError, "bad marshal data (unknown type code)");\r | |
1070 | retval = NULL;\r | |
1071 | break;\r | |
1072 | \r | |
1073 | }\r | |
1074 | p->depth--;\r | |
1075 | return retval;\r | |
1076 | }\r | |
1077 | \r | |
1078 | static PyObject *\r | |
1079 | read_object(RFILE *p)\r | |
1080 | {\r | |
1081 | PyObject *v;\r | |
1082 | if (PyErr_Occurred()) {\r | |
1083 | fprintf(stderr, "XXX readobject called with exception set\n");\r | |
1084 | return NULL;\r | |
1085 | }\r | |
1086 | v = r_object(p);\r | |
1087 | if (v == NULL && !PyErr_Occurred())\r | |
1088 | PyErr_SetString(PyExc_TypeError, "NULL object in marshal data for object");\r | |
1089 | return v;\r | |
1090 | }\r | |
1091 | \r | |
1092 | int\r | |
1093 | PyMarshal_ReadShortFromFile(FILE *fp)\r | |
1094 | {\r | |
1095 | RFILE rf;\r | |
1096 | assert(fp);\r | |
1097 | rf.fp = fp;\r | |
1098 | rf.strings = NULL;\r | |
1099 | rf.end = rf.ptr = NULL;\r | |
1100 | return r_short(&rf);\r | |
1101 | }\r | |
1102 | \r | |
1103 | long\r | |
1104 | PyMarshal_ReadLongFromFile(FILE *fp)\r | |
1105 | {\r | |
1106 | RFILE rf;\r | |
1107 | rf.fp = fp;\r | |
1108 | rf.strings = NULL;\r | |
1109 | rf.ptr = rf.end = NULL;\r | |
1110 | return r_long(&rf);\r | |
1111 | }\r | |
1112 | \r | |
1113 | #ifdef HAVE_FSTAT\r | |
1114 | /* Return size of file in bytes; < 0 if unknown. */\r | |
1115 | static off_t\r | |
1116 | getfilesize(FILE *fp)\r | |
1117 | {\r | |
1118 | struct stat st;\r | |
1119 | if (fstat(fileno(fp), &st) != 0)\r | |
1120 | return -1;\r | |
1121 | else\r | |
1122 | return st.st_size;\r | |
1123 | }\r | |
1124 | #endif\r | |
1125 | \r | |
1126 | /* If we can get the size of the file up-front, and it's reasonably small,\r | |
1127 | * read it in one gulp and delegate to ...FromString() instead. Much quicker\r | |
1128 | * than reading a byte at a time from file; speeds .pyc imports.\r | |
1129 | * CAUTION: since this may read the entire remainder of the file, don't\r | |
1130 | * call it unless you know you're done with the file.\r | |
1131 | */\r | |
1132 | PyObject *\r | |
1133 | PyMarshal_ReadLastObjectFromFile(FILE *fp)\r | |
1134 | {\r | |
1135 | /* REASONABLE_FILE_LIMIT is by defn something big enough for Tkinter.pyc. */\r | |
1136 | #define REASONABLE_FILE_LIMIT (1L << 18)\r | |
1137 | #ifdef HAVE_FSTAT\r | |
1138 | off_t filesize;\r | |
1139 | filesize = getfilesize(fp);\r | |
1140 | if (filesize > 0 && filesize <= REASONABLE_FILE_LIMIT) {\r | |
1141 | char* pBuf = (char *)PyMem_MALLOC(filesize);\r | |
1142 | if (pBuf != NULL) {\r | |
1143 | size_t n = fread(pBuf, 1, (size_t)filesize, fp);\r | |
1144 | PyObject* v = PyMarshal_ReadObjectFromString(pBuf, n);\r | |
1145 | PyMem_FREE(pBuf);\r | |
1146 | return v;\r | |
1147 | }\r | |
1148 | \r | |
1149 | }\r | |
1150 | #endif\r | |
1151 | /* We don't have fstat, or we do but the file is larger than\r | |
1152 | * REASONABLE_FILE_LIMIT or malloc failed -- read a byte at a time.\r | |
1153 | */\r | |
1154 | return PyMarshal_ReadObjectFromFile(fp);\r | |
1155 | \r | |
1156 | #undef REASONABLE_FILE_LIMIT\r | |
1157 | }\r | |
1158 | \r | |
1159 | PyObject *\r | |
1160 | PyMarshal_ReadObjectFromFile(FILE *fp)\r | |
1161 | {\r | |
1162 | RFILE rf;\r | |
1163 | PyObject *result;\r | |
1164 | rf.fp = fp;\r | |
1165 | rf.strings = PyList_New(0);\r | |
1166 | rf.depth = 0;\r | |
1167 | rf.ptr = rf.end = NULL;\r | |
1168 | result = r_object(&rf);\r | |
1169 | Py_DECREF(rf.strings);\r | |
1170 | return result;\r | |
1171 | }\r | |
1172 | \r | |
1173 | PyObject *\r | |
1174 | PyMarshal_ReadObjectFromString(char *str, Py_ssize_t len)\r | |
1175 | {\r | |
1176 | RFILE rf;\r | |
1177 | PyObject *result;\r | |
1178 | rf.fp = NULL;\r | |
1179 | rf.ptr = str;\r | |
1180 | rf.end = str + len;\r | |
1181 | rf.strings = PyList_New(0);\r | |
1182 | rf.depth = 0;\r | |
1183 | result = r_object(&rf);\r | |
1184 | Py_DECREF(rf.strings);\r | |
1185 | return result;\r | |
1186 | }\r | |
1187 | \r | |
1188 | static void\r | |
1189 | set_error(int error)\r | |
1190 | {\r | |
1191 | switch (error) {\r | |
1192 | case WFERR_NOMEMORY:\r | |
1193 | PyErr_NoMemory();\r | |
1194 | break;\r | |
1195 | case WFERR_UNMARSHALLABLE:\r | |
1196 | PyErr_SetString(PyExc_ValueError, "unmarshallable object");\r | |
1197 | break;\r | |
1198 | case WFERR_NESTEDTOODEEP:\r | |
1199 | default:\r | |
1200 | PyErr_SetString(PyExc_ValueError,\r | |
1201 | "object too deeply nested to marshal");\r | |
1202 | break;\r | |
1203 | }\r | |
1204 | }\r | |
1205 | \r | |
1206 | PyObject *\r | |
1207 | PyMarshal_WriteObjectToString(PyObject *x, int version)\r | |
1208 | {\r | |
1209 | WFILE wf;\r | |
1210 | wf.fp = NULL;\r | |
1211 | wf.str = PyString_FromStringAndSize((char *)NULL, 50);\r | |
1212 | if (wf.str == NULL)\r | |
1213 | return NULL;\r | |
1214 | wf.ptr = PyString_AS_STRING((PyStringObject *)wf.str);\r | |
1215 | wf.end = wf.ptr + PyString_Size(wf.str);\r | |
1216 | wf.error = WFERR_OK;\r | |
1217 | wf.depth = 0;\r | |
1218 | wf.version = version;\r | |
1219 | wf.strings = (version > 0) ? PyDict_New() : NULL;\r | |
1220 | w_object(x, &wf);\r | |
1221 | Py_XDECREF(wf.strings);\r | |
1222 | if (wf.str != NULL) {\r | |
1223 | char *base = PyString_AS_STRING((PyStringObject *)wf.str);\r | |
1224 | if (wf.ptr - base > PY_SSIZE_T_MAX) {\r | |
1225 | Py_DECREF(wf.str);\r | |
1226 | PyErr_SetString(PyExc_OverflowError,\r | |
1227 | "too much marshall data for a string");\r | |
1228 | return NULL;\r | |
1229 | }\r | |
1230 | if (_PyString_Resize(&wf.str, (Py_ssize_t)(wf.ptr - base)))\r | |
1231 | return NULL;\r | |
1232 | }\r | |
1233 | if (wf.error != WFERR_OK) {\r | |
1234 | Py_XDECREF(wf.str);\r | |
1235 | set_error(wf.error);\r | |
1236 | return NULL;\r | |
1237 | }\r | |
1238 | return wf.str;\r | |
1239 | }\r | |
1240 | \r | |
1241 | /* And an interface for Python programs... */\r | |
1242 | \r | |
1243 | static PyObject *\r | |
1244 | marshal_dump(PyObject *self, PyObject *args)\r | |
1245 | {\r | |
1246 | WFILE wf;\r | |
1247 | PyObject *x;\r | |
1248 | PyObject *f;\r | |
1249 | int version = Py_MARSHAL_VERSION;\r | |
1250 | if (!PyArg_ParseTuple(args, "OO|i:dump", &x, &f, &version))\r | |
1251 | return NULL;\r | |
1252 | if (!PyFile_Check(f)) {\r | |
1253 | PyErr_SetString(PyExc_TypeError,\r | |
1254 | "marshal.dump() 2nd arg must be file");\r | |
1255 | return NULL;\r | |
1256 | }\r | |
1257 | wf.fp = PyFile_AsFile(f);\r | |
1258 | wf.str = NULL;\r | |
1259 | wf.ptr = wf.end = NULL;\r | |
1260 | wf.error = WFERR_OK;\r | |
1261 | wf.depth = 0;\r | |
1262 | wf.strings = (version > 0) ? PyDict_New() : 0;\r | |
1263 | wf.version = version;\r | |
1264 | w_object(x, &wf);\r | |
1265 | Py_XDECREF(wf.strings);\r | |
1266 | if (wf.error != WFERR_OK) {\r | |
1267 | set_error(wf.error);\r | |
1268 | return NULL;\r | |
1269 | }\r | |
1270 | Py_INCREF(Py_None);\r | |
1271 | return Py_None;\r | |
1272 | }\r | |
1273 | \r | |
1274 | PyDoc_STRVAR(dump_doc,\r | |
1275 | "dump(value, file[, version])\n\\r | |
1276 | \n\\r | |
1277 | Write the value on the open file. The value must be a supported type.\n\\r | |
1278 | The file must be an open file object such as sys.stdout or returned by\n\\r | |
1279 | open() or os.popen(). It must be opened in binary mode ('wb' or 'w+b').\n\\r | |
1280 | \n\\r | |
1281 | If the value has (or contains an object that has) an unsupported type, a\n\\r | |
1282 | ValueError exception is raised — but garbage data will also be written\n\\r | |
1283 | to the file. The object will not be properly read back by load()\n\\r | |
1284 | \n\\r | |
1285 | New in version 2.4: The version argument indicates the data format that\n\\r | |
1286 | dump should use.");\r | |
1287 | \r | |
1288 | static PyObject *\r | |
1289 | marshal_load(PyObject *self, PyObject *f)\r | |
1290 | {\r | |
1291 | RFILE rf;\r | |
1292 | PyObject *result;\r | |
1293 | if (!PyFile_Check(f)) {\r | |
1294 | PyErr_SetString(PyExc_TypeError,\r | |
1295 | "marshal.load() arg must be file");\r | |
1296 | return NULL;\r | |
1297 | }\r | |
1298 | rf.fp = PyFile_AsFile(f);\r | |
1299 | rf.strings = PyList_New(0);\r | |
1300 | rf.depth = 0;\r | |
1301 | result = read_object(&rf);\r | |
1302 | Py_DECREF(rf.strings);\r | |
1303 | return result;\r | |
1304 | }\r | |
1305 | \r | |
1306 | PyDoc_STRVAR(load_doc,\r | |
1307 | "load(file)\n\\r | |
1308 | \n\\r | |
1309 | Read one value from the open file and return it. If no valid value is\n\\r | |
1310 | read (e.g. because the data has a different Python version’s\n\\r | |
1311 | incompatible marshal format), raise EOFError, ValueError or TypeError.\n\\r | |
1312 | The file must be an open file object opened in binary mode ('rb' or\n\\r | |
1313 | 'r+b').\n\\r | |
1314 | \n\\r | |
1315 | Note: If an object containing an unsupported type was marshalled with\n\\r | |
1316 | dump(), load() will substitute None for the unmarshallable type.");\r | |
1317 | \r | |
1318 | \r | |
1319 | static PyObject *\r | |
1320 | marshal_dumps(PyObject *self, PyObject *args)\r | |
1321 | {\r | |
1322 | PyObject *x;\r | |
1323 | int version = Py_MARSHAL_VERSION;\r | |
1324 | if (!PyArg_ParseTuple(args, "O|i:dumps", &x, &version))\r | |
1325 | return NULL;\r | |
1326 | return PyMarshal_WriteObjectToString(x, version);\r | |
1327 | }\r | |
1328 | \r | |
1329 | PyDoc_STRVAR(dumps_doc,\r | |
1330 | "dumps(value[, version])\n\\r | |
1331 | \n\\r | |
1332 | Return the string that would be written to a file by dump(value, file).\n\\r | |
1333 | The value must be a supported type. Raise a ValueError exception if\n\\r | |
1334 | value has (or contains an object that has) an unsupported type.\n\\r | |
1335 | \n\\r | |
1336 | New in version 2.4: The version argument indicates the data format that\n\\r | |
1337 | dumps should use.");\r | |
1338 | \r | |
1339 | \r | |
1340 | static PyObject *\r | |
1341 | marshal_loads(PyObject *self, PyObject *args)\r | |
1342 | {\r | |
1343 | RFILE rf;\r | |
1344 | char *s;\r | |
1345 | Py_ssize_t n;\r | |
1346 | PyObject* result;\r | |
1347 | if (!PyArg_ParseTuple(args, "s#:loads", &s, &n))\r | |
1348 | return NULL;\r | |
1349 | rf.fp = NULL;\r | |
1350 | rf.ptr = s;\r | |
1351 | rf.end = s + n;\r | |
1352 | rf.strings = PyList_New(0);\r | |
1353 | rf.depth = 0;\r | |
1354 | result = read_object(&rf);\r | |
1355 | Py_DECREF(rf.strings);\r | |
1356 | return result;\r | |
1357 | }\r | |
1358 | \r | |
1359 | PyDoc_STRVAR(loads_doc,\r | |
1360 | "loads(string)\n\\r | |
1361 | \n\\r | |
1362 | Convert the string to a value. If no valid value is found, raise\n\\r | |
1363 | EOFError, ValueError or TypeError. Extra characters in the string are\n\\r | |
1364 | ignored.");\r | |
1365 | \r | |
1366 | static PyMethodDef marshal_methods[] = {\r | |
1367 | {"dump", marshal_dump, METH_VARARGS, dump_doc},\r | |
1368 | {"load", marshal_load, METH_O, load_doc},\r | |
1369 | {"dumps", marshal_dumps, METH_VARARGS, dumps_doc},\r | |
1370 | {"loads", marshal_loads, METH_VARARGS, loads_doc},\r | |
1371 | {NULL, NULL} /* sentinel */\r | |
1372 | };\r | |
1373 | \r | |
1374 | PyDoc_STRVAR(marshal_doc,\r | |
1375 | "This module contains functions that can read and write Python values in\n\\r | |
1376 | a binary format. The format is specific to Python, but independent of\n\\r | |
1377 | machine architecture issues.\n\\r | |
1378 | \n\\r | |
1379 | Not all Python object types are supported; in general, only objects\n\\r | |
1380 | whose value is independent from a particular invocation of Python can be\n\\r | |
1381 | written and read by this module. The following types are supported:\n\\r | |
1382 | None, integers, long integers, floating point numbers, strings, Unicode\n\\r | |
1383 | objects, tuples, lists, sets, dictionaries, and code objects, where it\n\\r | |
1384 | should be understood that tuples, lists and dictionaries are only\n\\r | |
1385 | supported as long as the values contained therein are themselves\n\\r | |
1386 | supported; and recursive lists and dictionaries should not be written\n\\r | |
1387 | (they will cause infinite loops).\n\\r | |
1388 | \n\\r | |
1389 | Variables:\n\\r | |
1390 | \n\\r | |
1391 | version -- indicates the format that the module uses. Version 0 is the\n\\r | |
1392 | historical format, version 1 (added in Python 2.4) shares interned\n\\r | |
1393 | strings and version 2 (added in Python 2.5) uses a binary format for\n\\r | |
1394 | floating point numbers. (New in version 2.4)\n\\r | |
1395 | \n\\r | |
1396 | Functions:\n\\r | |
1397 | \n\\r | |
1398 | dump() -- write value to a file\n\\r | |
1399 | load() -- read value from a file\n\\r | |
1400 | dumps() -- write value to a string\n\\r | |
1401 | loads() -- read value from a string");\r | |
1402 | \r | |
1403 | \r | |
1404 | PyMODINIT_FUNC\r | |
1405 | PyMarshal_Init(void)\r | |
1406 | {\r | |
1407 | PyObject *mod = Py_InitModule3("marshal", marshal_methods,\r | |
1408 | marshal_doc);\r | |
1409 | if (mod == NULL)\r | |
1410 | return;\r | |
1411 | PyModule_AddIntConstant(mod, "version", Py_MARSHAL_VERSION);\r | |
1412 | }\r |