/home/mdboom/Work/builds/cpython/Python/marshal.c
Line | Count | Source (jump to first uncovered line) |
1 | |
2 | /* Write Python objects to files and read them back. |
3 | This is primarily intended for writing and reading compiled Python code, |
4 | even though dicts, lists, sets and frozensets, not commonly seen in |
5 | code objects, are supported. |
6 | Version 3 of this protocol properly supports circular links |
7 | and sharing. */ |
8 | |
9 | #define PY_SSIZE_T_CLEAN |
10 | |
11 | #include "Python.h" |
12 | #include "pycore_call.h" // _PyObject_CallNoArgs() |
13 | #include "pycore_code.h" // _PyCode_New() |
14 | #include "pycore_hashtable.h" // _Py_hashtable_t |
15 | #include "marshal.h" // Py_MARSHAL_VERSION |
16 | |
17 | /*[clinic input] |
18 | module marshal |
19 | [clinic start generated code]*/ |
20 | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=c982b7930dee17db]*/ |
21 | |
22 | #include "clinic/marshal.c.h" |
23 | |
24 | /* High water mark to determine when the marshalled object is dangerously deep |
25 | * and risks coring the interpreter. When the object stack gets this deep, |
26 | * raise an exception instead of continuing. |
27 | * On Windows debug builds, reduce this value. |
28 | * |
29 | * BUG: https://bugs.python.org/issue33720 |
30 | * On Windows PGO builds, the r_object function overallocates its stack and |
31 | * can cause a stack overflow. We reduce the maximum depth for all Windows |
32 | * releases to protect against this. |
33 | * #if defined(MS_WINDOWS) && defined(_DEBUG) |
34 | */ |
35 | #if defined(MS_WINDOWS) |
36 | #define MAX_MARSHAL_STACK_DEPTH 1000 |
37 | #else |
38 | #define MAX_MARSHAL_STACK_DEPTH 2000 |
39 | #endif |
40 | |
41 | #define TYPE_NULL '0' |
42 | #define TYPE_NONE 'N' |
43 | #define TYPE_FALSE 'F' |
44 | #define TYPE_TRUE 'T' |
45 | #define TYPE_STOPITER 'S' |
46 | #define TYPE_ELLIPSIS '.' |
47 | #define TYPE_INT 'i' |
48 | /* TYPE_INT64 is not generated anymore. |
49 | Supported for backward compatibility only. */ |
50 | #define TYPE_INT64 'I' |
51 | #define TYPE_FLOAT 'f' |
52 | #define TYPE_BINARY_FLOAT 'g' |
53 | #define TYPE_COMPLEX 'x' |
54 | #define TYPE_BINARY_COMPLEX 'y' |
55 | #define TYPE_LONG 'l' |
56 | #define TYPE_STRING 's' |
57 | #define TYPE_INTERNED 't' |
58 | #define TYPE_REF 'r' |
59 | #define TYPE_TUPLE '(' |
60 | #define TYPE_LIST '[' |
61 | #define TYPE_DICT '{' |
62 | #define TYPE_CODE 'c' |
63 | #define TYPE_UNICODE 'u' |
64 | #define TYPE_UNKNOWN '?' |
65 | #define TYPE_SET '<' |
66 | #define TYPE_FROZENSET '>' |
67 | #define FLAG_REF '\x80' /* with a type, add obj to index */ |
68 | |
69 | #define TYPE_ASCII 'a' |
70 | #define TYPE_ASCII_INTERNED 'A' |
71 | #define TYPE_SMALL_TUPLE ')' |
72 | #define TYPE_SHORT_ASCII 'z' |
73 | #define TYPE_SHORT_ASCII_INTERNED 'Z' |
74 | |
75 | #define WFERR_OK 0 |
76 | #define WFERR_UNMARSHALLABLE 1 |
77 | #define WFERR_NESTEDTOODEEP 2 |
78 | #define WFERR_NOMEMORY 3 |
79 | |
80 | typedef struct { |
81 | FILE *fp; |
82 | int error; /* see WFERR_* values */ |
83 | int depth; |
84 | PyObject *str; |
85 | char *ptr; |
86 | const char *end; |
87 | char *buf; |
88 | _Py_hashtable_t *hashtable; |
89 | int version; |
90 | } WFILE; |
91 | |
92 | #define w_byte(c, p) do { \ |
93 | if ((p)->ptr != (p)->end || w_reserve((p), 1)396 ) \ |
94 | *(p)->ptr++ = (c); \ |
95 | } while(0) |
96 | |
97 | static void |
98 | w_flush(WFILE *p) |
99 | { |
100 | assert(p->fp != NULL); |
101 | fwrite(p->buf, 1, p->ptr - p->buf, p->fp); |
102 | p->ptr = p->buf; |
103 | } |
104 | |
105 | static int |
106 | w_reserve(WFILE *p, Py_ssize_t needed) |
107 | { |
108 | Py_ssize_t pos, size, delta; |
109 | if (p->ptr == NULL) Branch (109:9): [True: 0, False: 1.01k]
|
110 | return 0; /* An error already occurred */ |
111 | if (p->fp != NULL) { Branch (111:9): [True: 0, False: 1.01k]
|
112 | w_flush(p); |
113 | return needed <= p->end - p->ptr; |
114 | } |
115 | assert(p->str != NULL); |
116 | pos = p->ptr - p->buf; |
117 | size = PyBytes_GET_SIZE(p->str); |
118 | if (size > 16*1024*1024) Branch (118:9): [True: 0, False: 1.01k]
|
119 | delta = (size >> 3); /* 12.5% overallocation */ |
120 | else |
121 | delta = size + 1024; |
122 | delta = Py_MAX(delta, needed); |
123 | if (delta > PY_SSIZE_T_MAX - size) { Branch (123:9): [True: 0, False: 1.01k]
|
124 | p->error = WFERR_NOMEMORY; |
125 | return 0; |
126 | } |
127 | size += delta; |
128 | if (_PyBytes_Resize(&p->str, size) != 0) { Branch (128:9): [True: 0, False: 1.01k]
|
129 | p->end = p->ptr = p->buf = NULL; |
130 | return 0; |
131 | } |
132 | else { |
133 | p->buf = PyBytes_AS_STRING(p->str); |
134 | p->ptr = p->buf + pos; |
135 | p->end = p->buf + size; |
136 | return 1; |
137 | } |
138 | } |
139 | |
140 | static void |
141 | w_string(const void *s, Py_ssize_t n, WFILE *p) |
142 | { |
143 | Py_ssize_t m; |
144 | if (!n || p->ptr == NULL20.0k ) Branch (144:9): [True: 1.35k, False: 20.0k]
Branch (144:15): [True: 0, False: 20.0k]
|
145 | return; |
146 | m = p->end - p->ptr; |
147 | if (p->fp != NULL) { Branch (147:9): [True: 30, False: 20.0k]
|
148 | if (n <= m) { Branch (148:13): [True: 25, False: 5]
|
149 | memcpy(p->ptr, s, n); |
150 | p->ptr += n; |
151 | } |
152 | else { |
153 | w_flush(p); |
154 | fwrite(s, 1, n, p->fp); |
155 | } |
156 | } |
157 | else { |
158 | if (n <= m || w_reserve(p, n - m)615 ) { Branch (158:13): [True: 19.4k, False: 615]
Branch (158:23): [True: 615, False: 0]
|
159 | memcpy(p->ptr, s, n); |
160 | p->ptr += n; |
161 | } |
162 | } |
163 | } |
164 | |
165 | static void |
166 | w_short(int x, WFILE *p) |
167 | { |
168 | w_byte((char)( x & 0xff), p); |
169 | w_byte((char)((x>> 8) & 0xff), p); |
170 | } |
171 | |
172 | static void |
173 | w_long(long x, WFILE *p) |
174 | { |
175 | w_byte((char)( x & 0xff), p); |
176 | w_byte((char)((x>> 8) & 0xff), p); |
177 | w_byte((char)((x>>16) & 0xff), p); |
178 | w_byte((char)((x>>24) & 0xff), p); |
179 | } |
180 | |
181 | #define SIZE32_MAX 0x7FFFFFFF |
182 | |
183 | #if SIZEOF_SIZE_T > 4 |
184 | # define W_SIZE(n, p) do { \ |
185 | if ((n) > SIZE32_MAX) { \ |
186 | (p)->depth--; \ |
187 | (p)->error = WFERR_UNMARSHALLABLE; \ |
188 | return; \ |
189 | } \ |
190 | w_long((long)(n), p); \ |
191 | } while(0) |
192 | #else |
193 | # define W_SIZE w_long |
194 | #endif |
195 | |
196 | static void |
197 | w_pstring(const void *s, Py_ssize_t n, WFILE *p) |
198 | { |
199 | W_SIZE(n, p); |
200 | w_string(s, n, p); |
201 | } |
202 | |
203 | static void |
204 | w_short_pstring(const void *s, Py_ssize_t n, WFILE *p) |
205 | { |
206 | w_byte(Py_SAFE_DOWNCAST(n, Py_ssize_t, unsigned char), p); |
207 | w_string(s, n, p); |
208 | } |
209 | |
210 | /* We assume that Python ints are stored internally in base some power of |
211 | 2**15; for the sake of portability we'll always read and write them in base |
212 | exactly 2**15. */ |
213 | |
214 | #define PyLong_MARSHAL_SHIFT 15 |
215 | #define PyLong_MARSHAL_BASE ((short)1 << PyLong_MARSHAL_SHIFT) |
216 | #define PyLong_MARSHAL_MASK (PyLong_MARSHAL_BASE - 1) |
217 | #if PyLong_SHIFT % PyLong_MARSHAL_SHIFT != 0 |
218 | #error "PyLong_SHIFT must be a multiple of PyLong_MARSHAL_SHIFT" |
219 | #endif |
220 | #define PyLong_MARSHAL_RATIO (PyLong_SHIFT / PyLong_MARSHAL_SHIFT) |
221 | |
222 | #define W_TYPE(t, p) do { \ |
223 | w_byte((t) | flag, (p)); \ |
224 | } while(0) |
225 | |
226 | static void |
227 | w_PyLong(const PyLongObject *ob, char flag, WFILE *p) |
228 | { |
229 | Py_ssize_t i, j, n, l; |
230 | digit d; |
231 | |
232 | W_TYPE(TYPE_LONG, p); |
233 | if (Py_SIZE(ob) == 0) { Branch (233:9): [True: 0, False: 451]
|
234 | w_long((long)0, p); |
235 | return; |
236 | } |
237 | |
238 | /* set l to number of base PyLong_MARSHAL_BASE digits */ |
239 | n = Py_ABS(Py_SIZE(ob)); |
240 | l = (n-1) * PyLong_MARSHAL_RATIO; |
241 | d = ob->ob_digit[n-1]; |
242 | assert(d != 0); /* a PyLong is always normalized */ |
243 | do { |
244 | d >>= PyLong_MARSHAL_SHIFT; |
245 | l++; |
246 | } while (d != 0); Branch (246:14): [True: 180, False: 451]
|
247 | if (l > SIZE32_MAX) { Branch (247:9): [True: 0, False: 451]
|
248 | p->depth--; |
249 | p->error = WFERR_UNMARSHALLABLE; |
250 | return; |
251 | } |
252 | w_long((long)(Py_SIZE(ob) > 0 ? l241 : -l210 ), p); Branch (252:19): [True: 241, False: 210]
|
253 | |
254 | for (i=0; i < n-1; i++934 ) { Branch (254:15): [True: 934, False: 451]
|
255 | d = ob->ob_digit[i]; |
256 | for (j=0; j < PyLong_MARSHAL_RATIO; j++1.86k ) { Branch (256:19): [True: 1.86k, False: 934]
|
257 | w_short(d & PyLong_MARSHAL_MASK, p); |
258 | d >>= PyLong_MARSHAL_SHIFT; |
259 | } |
260 | assert (d == 0); |
261 | } |
262 | d = ob->ob_digit[n-1]; |
263 | do { |
264 | w_short(d & PyLong_MARSHAL_MASK, p); |
265 | d >>= PyLong_MARSHAL_SHIFT; |
266 | } while (d != 0); Branch (266:14): [True: 180, False: 451]
|
267 | } |
268 | |
269 | static void |
270 | w_float_bin(double v, WFILE *p) |
271 | { |
272 | char buf[8]; |
273 | if (PyFloat_Pack8(v, buf, 1) < 0) { Branch (273:9): [True: 0, False: 1.36k]
|
274 | p->error = WFERR_UNMARSHALLABLE; |
275 | return; |
276 | } |
277 | w_string(buf, 8, p); |
278 | } |
279 | |
280 | static void |
281 | w_float_str(double v, WFILE *p) |
282 | { |
283 | char *buf = PyOS_double_to_string(v, 'g', 17, 0, NULL); |
284 | if (!buf) { Branch (284:9): [True: 0, False: 461]
|
285 | p->error = WFERR_NOMEMORY; |
286 | return; |
287 | } |
288 | w_short_pstring(buf, strlen(buf), p); |
289 | PyMem_Free(buf); |
290 | } |
291 | |
292 | static int |
293 | w_ref(PyObject *v, char *flag, WFILE *p) |
294 | { |
295 | _Py_hashtable_entry_t *entry; |
296 | int w; |
297 | |
298 | if (p->version < 3 || p->hashtable == NULL107k ) Branch (298:9): [True: 11.4k, False: 107k]
Branch (298:27): [True: 0, False: 107k]
|
299 | return 0; /* not writing object references */ |
300 | |
301 | /* If it has only one reference, it definitely isn't shared. |
302 | * But we use TYPE_REF always for interned string, to PYC file stable |
303 | * as possible. |
304 | */ |
305 | if (Py_REFCNT(v) == 1 && Branch (305:9): [True: 10.7k, False: 96.6k]
|
306 | !(10.7k PyUnicode_CheckExact10.7k (v) && PyUnicode_CHECK_INTERNED837 (v))) { |
307 | return 0; |
308 | } |
309 | |
310 | entry = _Py_hashtable_get_entry(p->hashtable, v); |
311 | if (entry != NULL) { Branch (311:9): [True: 83.2k, False: 13.4k]
|
312 | /* write the reference index to the stream */ |
313 | w = (int)(uintptr_t)entry->value; |
314 | /* we don't store "long" indices in the dict */ |
315 | assert(0 <= w && w <= 0x7fffffff); |
316 | w_byte(TYPE_REF, p); |
317 | w_long(w, p); |
318 | return 1; |
319 | } else { |
320 | size_t s = p->hashtable->nentries; |
321 | /* we don't support long indices */ |
322 | if (s >= 0x7fffffff) { Branch (322:13): [True: 0, False: 13.4k]
|
323 | PyErr_SetString(PyExc_ValueError, "too many objects"); |
324 | goto err; |
325 | } |
326 | w = (int)s; |
327 | Py_INCREF(v); |
328 | if (_Py_hashtable_set(p->hashtable, v, (void *)(uintptr_t)w) < 0) { Branch (328:13): [True: 0, False: 13.4k]
|
329 | Py_DECREF(v); |
330 | goto err; |
331 | } |
332 | *flag |= FLAG_REF; |
333 | return 0; |
334 | } |
335 | err: |
336 | p->error = WFERR_UNMARSHALLABLE; |
337 | return 1; |
338 | } |
339 | |
340 | static void |
341 | w_complex_object(PyObject *v, char flag, WFILE *p); |
342 | |
343 | static void |
344 | w_object(PyObject *v, WFILE *p) |
345 | { |
346 | char flag = '\0'; |
347 | |
348 | p->depth++; |
349 | |
350 | if (p->depth > MAX_MARSHAL_STACK_DEPTH) { Branch (350:9): [True: 1, False: 120k]
|
351 | p->error = WFERR_NESTEDTOODEEP; |
352 | } |
353 | else if (v == NULL) { Branch (353:14): [True: 102, False: 120k]
|
354 | w_byte(TYPE_NULL, p); |
355 | } |
356 | else if (v == Py_None) { Branch (356:14): [True: 1.76k, False: 118k]
|
357 | w_byte(TYPE_NONE, p); |
358 | } |
359 | else if (v == PyExc_StopIteration) { Branch (359:14): [True: 1, False: 118k]
|
360 | w_byte(TYPE_STOPITER, p); |
361 | } |
362 | else if (v == Py_Ellipsis) { Branch (362:14): [True: 0, False: 118k]
|
363 | w_byte(TYPE_ELLIPSIS, p); |
364 | } |
365 | else if (v == Py_False) { Branch (365:14): [True: 67, False: 118k]
|
366 | w_byte(TYPE_FALSE, p); |
367 | } |
368 | else if (v == Py_True) { Branch (368:14): [True: 44, False: 118k]
|
369 | w_byte(TYPE_TRUE, p); |
370 | } |
371 | else if (!w_ref(v, &flag, p)) Branch (371:14): [True: 35.6k, False: 83.2k]
|
372 | w_complex_object(v, flag, p); |
373 | |
374 | p->depth--; |
375 | } |
376 | |
377 | static void |
378 | w_complex_object(PyObject *v, char flag, WFILE *p) |
379 | { |
380 | Py_ssize_t i, n; |
381 | |
382 | if (PyLong_CheckExact(v)) { |
383 | int overflow; |
384 | long x = PyLong_AsLongAndOverflow(v, &overflow); |
385 | if (overflow) { Branch (385:13): [True: 253, False: 1.97k]
|
386 | w_PyLong((PyLongObject *)v, flag, p); |
387 | } |
388 | else { |
389 | #if SIZEOF_LONG > 4 |
390 | long y = Py_ARITHMETIC_RIGHT_SHIFT(long, x, 31); |
391 | if (y && y != -1292 ) { Branch (391:17): [True: 292, False: 1.68k]
Branch (391:22): [True: 198, False: 94]
|
392 | /* Too large for TYPE_INT */ |
393 | w_PyLong((PyLongObject*)v, flag, p); |
394 | } |
395 | else |
396 | #endif |
397 | { |
398 | W_TYPE(TYPE_INT, p); |
399 | w_long(x, p); |
400 | } |
401 | } |
402 | } |
403 | else if (PyFloat_CheckExact(v)) { |
404 | if (p->version > 1) { Branch (404:13): [True: 1.30k, False: 433]
|
405 | W_TYPE(TYPE_BINARY_FLOAT, p); |
406 | w_float_bin(PyFloat_AS_DOUBLE(v), p); |
407 | } |
408 | else { |
409 | W_TYPE(TYPE_FLOAT, p); |
410 | w_float_str(PyFloat_AS_DOUBLE(v), p); |
411 | } |
412 | } |
413 | else if (PyComplex_CheckExact(v)) { |
414 | if (p->version > 1) { Branch (414:13): [True: 32, False: 14]
|
415 | W_TYPE(TYPE_BINARY_COMPLEX, p); |
416 | w_float_bin(PyComplex_RealAsDouble(v), p); |
417 | w_float_bin(PyComplex_ImagAsDouble(v), p); |
418 | } |
419 | else { |
420 | W_TYPE(TYPE_COMPLEX, p); |
421 | w_float_str(PyComplex_RealAsDouble(v), p); |
422 | w_float_str(PyComplex_ImagAsDouble(v), p); |
423 | } |
424 | } |
425 | else if (PyBytes_CheckExact(v)) { |
426 | W_TYPE(TYPE_STRING, p); |
427 | w_pstring(PyBytes_AS_STRING(v), PyBytes_GET_SIZE(v), p); |
428 | } |
429 | else if (PyUnicode_CheckExact(v)) { |
430 | if (p->version >= 4 && PyUnicode_IS_ASCII6.16k (v)) { Branch (430:13): [True: 6.16k, False: 7.04k]
|
431 | int is_short = PyUnicode_GET_LENGTH(v) < 256; |
432 | if (is_short) { Branch (432:17): [True: 5.97k, False: 10]
|
433 | if (PyUnicode_CHECK_INTERNED(v)) |
434 | W_TYPE(TYPE_SHORT_ASCII_INTERNED, p); |
435 | else |
436 | W_TYPE(TYPE_SHORT_ASCII, p); |
437 | w_short_pstring(PyUnicode_1BYTE_DATA(v), |
438 | PyUnicode_GET_LENGTH(v), p); |
439 | } |
440 | else { |
441 | if (PyUnicode_CHECK_INTERNED(v)) |
442 | W_TYPE(TYPE_ASCII_INTERNED, p); |
443 | else |
444 | W_TYPE(TYPE_ASCII, p); |
445 | w_pstring(PyUnicode_1BYTE_DATA(v), |
446 | PyUnicode_GET_LENGTH(v), p); |
447 | } |
448 | } |
449 | else { |
450 | PyObject *utf8; |
451 | utf8 = PyUnicode_AsEncodedString(v, "utf8", "surrogatepass"); |
452 | if (utf8 == NULL) { Branch (452:17): [True: 0, False: 7.21k]
|
453 | p->depth--; |
454 | p->error = WFERR_UNMARSHALLABLE; |
455 | return; |
456 | } |
457 | if (p->version >= 3 && PyUnicode_CHECK_INTERNED1.01k (v)) Branch (457:17): [True: 1.01k, False: 6.20k]
|
458 | W_TYPE(TYPE_INTERNED, p); |
459 | else |
460 | W_TYPE(TYPE_UNICODE, p); |
461 | w_pstring(PyBytes_AS_STRING(utf8), PyBytes_GET_SIZE(utf8), p); |
462 | Py_DECREF(utf8); |
463 | } |
464 | } |
465 | else if (PyTuple_CheckExact(v)) { |
466 | n = PyTuple_GET_SIZE(v); |
467 | if (p->version >= 4 && n < 2563.76k ) { Branch (467:13): [True: 3.76k, False: 2.12k]
Branch (467:32): [True: 3.76k, False: 2]
|
468 | W_TYPE(TYPE_SMALL_TUPLE, p); |
469 | w_byte((unsigned char)n, p); |
470 | } |
471 | else { |
472 | W_TYPE(TYPE_TUPLE, p); |
473 | W_SIZE(n, p); |
474 | } |
475 | for (i = 0; 5.88k i < n; i++88.8k ) { Branch (475:21): [True: 88.8k, False: 5.88k]
|
476 | w_object(PyTuple_GET_ITEM(v, i), p); |
477 | } |
478 | } |
479 | else if (PyList_CheckExact(v)) { |
480 | W_TYPE(TYPE_LIST, p); |
481 | n = PyList_GET_SIZE(v); |
482 | W_SIZE(n, p); |
483 | for (i = 0; 4.04k i < n; i++9.08k ) { Branch (483:21): [True: 9.08k, False: 4.04k]
|
484 | w_object(PyList_GET_ITEM(v, i), p); |
485 | } |
486 | } |
487 | else if (PyDict_CheckExact(v)) { |
488 | Py_ssize_t pos; |
489 | PyObject *key, *value; |
490 | W_TYPE(TYPE_DICT, p); |
491 | /* This one is NULL object terminated! */ |
492 | pos = 0; |
493 | while (PyDict_Next(v, &pos, &key, &value)) { Branch (493:16): [True: 245, False: 102]
|
494 | w_object(key, p); |
495 | w_object(value, p); |
496 | } |
497 | w_object((PyObject *)NULL, p); |
498 | } |
499 | else if (PyAnySet_CheckExact(v)) { |
500 | PyObject *value; |
501 | Py_ssize_t pos = 0; |
502 | Py_hash_t hash; |
503 | |
504 | if (PyFrozenSet_CheckExact(v)) |
505 | W_TYPE(TYPE_FROZENSET, p); |
506 | else |
507 | W_TYPE(TYPE_SET, p); |
508 | n = PySet_GET_SIZE(v); |
509 | W_SIZE(n, p); |
510 | // bpo-37596: To support reproducible builds, sets and frozensets need |
511 | // to have their elements serialized in a consistent order (even when |
512 | // they have been scrambled by hash randomization). To ensure this, we |
513 | // use an order equivalent to sorted(v, key=marshal.dumps): |
514 | PyObject *pairs = PyList_New(n); |
515 | if (pairs == NULL) { Branch (515:13): [True: 0, False: 140]
|
516 | p->error = WFERR_NOMEMORY; |
517 | return; |
518 | } |
519 | Py_ssize_t i = 0; |
520 | while (_PySet_NextEntry(v, &pos, &value, &hash)) { Branch (520:16): [True: 304, False: 140]
|
521 | PyObject *dump = PyMarshal_WriteObjectToString(value, p->version); |
522 | if (dump == NULL) { Branch (522:17): [True: 0, False: 304]
|
523 | p->error = WFERR_UNMARSHALLABLE; |
524 | Py_DECREF(pairs); |
525 | return; |
526 | } |
527 | PyObject *pair = PyTuple_Pack(2, dump, value); |
528 | Py_DECREF(dump); |
529 | if (pair == NULL) { Branch (529:17): [True: 0, False: 304]
|
530 | p->error = WFERR_NOMEMORY; |
531 | Py_DECREF(pairs); |
532 | return; |
533 | } |
534 | PyList_SET_ITEM(pairs, i++, pair); |
535 | } |
536 | assert(i == n); |
537 | if (PyList_Sort(pairs)) { Branch (537:13): [True: 0, False: 140]
|
538 | p->error = WFERR_NOMEMORY; |
539 | Py_DECREF(pairs); |
540 | return; |
541 | } |
542 | for (Py_ssize_t i = 0; 140 i < n; i++304 ) { Branch (542:32): [True: 304, False: 140]
|
543 | PyObject *pair = PyList_GET_ITEM(pairs, i); |
544 | value = PyTuple_GET_ITEM(pair, 1); |
545 | w_object(value, p); |
546 | } |
547 | Py_DECREF(pairs); |
548 | } |
549 | else if (PyCode_Check(v)) { |
550 | PyCodeObject *co = (PyCodeObject *)v; |
551 | PyObject *co_code = _PyCode_GetCode(co); |
552 | if (co_code == NULL) { Branch (552:13): [True: 0, False: 1.87k]
|
553 | p->error = WFERR_NOMEMORY; |
554 | return; |
555 | } |
556 | W_TYPE(TYPE_CODE, p); |
557 | w_long(co->co_argcount, p); |
558 | w_long(co->co_posonlyargcount, p); |
559 | w_long(co->co_kwonlyargcount, p); |
560 | w_long(co->co_stacksize, p); |
561 | w_long(co->co_flags, p); |
562 | w_object(co_code, p); |
563 | w_object(co->co_consts, p); |
564 | w_object(co->co_names, p); |
565 | w_object(co->co_localsplusnames, p); |
566 | w_object(co->co_localspluskinds, p); |
567 | w_object(co->co_filename, p); |
568 | w_object(co->co_name, p); |
569 | w_object(co->co_qualname, p); |
570 | w_long(co->co_firstlineno, p); |
571 | w_object(co->co_linetable, p); |
572 | w_object(co->co_exceptiontable, p); |
573 | Py_DECREF(co_code); |
574 | } |
575 | else if (PyObject_CheckBuffer(v)) { Branch (575:14): [True: 7, False: 8]
|
576 | /* Write unknown bytes-like objects as a bytes object */ |
577 | Py_buffer view; |
578 | if (PyObject_GetBuffer(v, &view, PyBUF_SIMPLE) != 0) { Branch (578:13): [True: 0, False: 7]
|
579 | w_byte(TYPE_UNKNOWN, p); |
580 | p->depth--; |
581 | p->error = WFERR_UNMARSHALLABLE; |
582 | return; |
583 | } |
584 | W_TYPE(TYPE_STRING, p); |
585 | w_pstring(view.buf, view.len, p); |
586 | PyBuffer_Release(&view); |
587 | } |
588 | else { |
589 | W_TYPE(TYPE_UNKNOWN, p); |
590 | p->error = WFERR_UNMARSHALLABLE; |
591 | } |
592 | } |
593 | |
594 | static void |
595 | w_decref_entry(void *key) |
596 | { |
597 | PyObject *entry_key = (PyObject *)key; |
598 | Py_XDECREF(entry_key); |
599 | } |
600 | |
601 | static int |
602 | w_init_refs(WFILE *wf, int version) |
603 | { |
604 | if (version >= 3) { Branch (604:9): [True: 2.72k, False: 553]
|
605 | wf->hashtable = _Py_hashtable_new_full(_Py_hashtable_hash_ptr, |
606 | _Py_hashtable_compare_direct, |
607 | w_decref_entry, NULL, NULL); |
608 | if (wf->hashtable == NULL) { Branch (608:13): [True: 0, False: 2.72k]
|
609 | PyErr_NoMemory(); |
610 | return -1; |
611 | } |
612 | } |
613 | return 0; |
614 | } |
615 | |
616 | static void |
617 | w_clear_refs(WFILE *wf) |
618 | { |
619 | if (wf->hashtable != NULL) { Branch (619:9): [True: 2.72k, False: 553]
|
620 | _Py_hashtable_destroy(wf->hashtable); |
621 | } |
622 | } |
623 | |
624 | /* version currently has no effect for writing ints. */ |
625 | void |
626 | PyMarshal_WriteLongToFile(long x, FILE *fp, int version) |
627 | { |
628 | char buf[4]; |
629 | WFILE wf; |
630 | memset(&wf, 0, sizeof(wf)); |
631 | wf.fp = fp; |
632 | wf.ptr = wf.buf = buf; |
633 | wf.end = wf.ptr + sizeof(buf); |
634 | wf.error = WFERR_OK; |
635 | wf.version = version; |
636 | w_long(x, &wf); |
637 | w_flush(&wf); |
638 | } |
639 | |
640 | void |
641 | PyMarshal_WriteObjectToFile(PyObject *x, FILE *fp, int version) |
642 | { |
643 | char buf[BUFSIZ]; |
644 | WFILE wf; |
645 | if (PySys_Audit("marshal.dumps", "Oi", x, version) < 0) { Branch (645:9): [True: 0, False: 5]
|
646 | return; /* caller must check PyErr_Occurred() */ |
647 | } |
648 | memset(&wf, 0, sizeof(wf)); |
649 | wf.fp = fp; |
650 | wf.ptr = wf.buf = buf; |
651 | wf.end = wf.ptr + sizeof(buf); |
652 | wf.error = WFERR_OK; |
653 | wf.version = version; |
654 | if (w_init_refs(&wf, version)) { Branch (654:9): [True: 0, False: 5]
|
655 | return; /* caller must check PyErr_Occurred() */ |
656 | } |
657 | w_object(x, &wf); |
658 | w_clear_refs(&wf); |
659 | w_flush(&wf); |
660 | } |
661 | |
662 | typedef struct { |
663 | FILE *fp; |
664 | int depth; |
665 | PyObject *readable; /* Stream-like object being read from */ |
666 | const char *ptr; |
667 | const char *end; |
668 | char *buf; |
669 | Py_ssize_t buf_size; |
670 | PyObject *refs; /* a list */ |
671 | } RFILE; |
672 | |
673 | static const char * |
674 | r_string(Py_ssize_t n, RFILE *p) |
675 | { |
676 | Py_ssize_t read = -1; |
677 | |
678 | if (p->ptr != NULL) { Branch (678:9): [True: 5.63M, False: 120k]
|
679 | /* Fast path for loads() */ |
680 | const char *res = p->ptr; |
681 | Py_ssize_t left = p->end - p->ptr; |
682 | if (left < n) { Branch (682:13): [True: 51, False: 5.63M]
|
683 | PyErr_SetString(PyExc_EOFError, |
684 | "marshal data too short"); |
685 | return NULL; |
686 | } |
687 | p->ptr += n; |
688 | return res; |
689 | } |
690 | if (p->buf == NULL) { Branch (690:9): [True: 1.03k, False: 119k]
|
691 | p->buf = PyMem_Malloc(n); |
692 | if (p->buf == NULL) { Branch (692:13): [True: 0, False: 1.03k]
|
693 | PyErr_NoMemory(); |
694 | return NULL; |
695 | } |
696 | p->buf_size = n; |
697 | } |
698 | else if (p->buf_size < n) { Branch (698:14): [True: 1.11k, False: 118k]
|
699 | char *tmp = PyMem_Realloc(p->buf, n); |
700 | if (tmp == NULL) { Branch (700:13): [True: 0, False: 1.11k]
|
701 | PyErr_NoMemory(); |
702 | return NULL; |
703 | } |
704 | p->buf = tmp; |
705 | p->buf_size = n; |
706 | } |
707 | |
708 | if (!p->readable) { Branch (708:9): [True: 52, False: 120k]
|
709 | assert(p->fp != NULL); |
710 | read = fread(p->buf, 1, n, p->fp); |
711 | } |
712 | else { |
713 | PyObject *res, *mview; |
714 | Py_buffer buf; |
715 | |
716 | if (PyBuffer_FillInfo(&buf, NULL, p->buf, n, 0, PyBUF_CONTIG) == -1) Branch (716:13): [True: 0, False: 120k]
|
717 | return NULL; |
718 | mview = PyMemoryView_FromBuffer(&buf); |
719 | if (mview == NULL) Branch (719:13): [True: 0, False: 120k]
|
720 | return NULL; |
721 | |
722 | res = _PyObject_CallMethod(p->readable, &_Py_ID(readinto), "N", mview); |
723 | if (res != NULL) { Branch (723:13): [True: 120k, False: 0]
|
724 | read = PyNumber_AsSsize_t(res, PyExc_ValueError); |
725 | Py_DECREF(res); |
726 | } |
727 | } |
728 | if (read != n) { Branch (728:9): [True: 10, False: 120k]
|
729 | if (!PyErr_Occurred()) { Branch (729:13): [True: 10, False: 0]
|
730 | if (read > n) Branch (730:17): [True: 4, False: 6]
|
731 | PyErr_Format(PyExc_ValueError, |
732 | "read() returned too much data: " |
733 | "%zd bytes requested, %zd returned", |
734 | n, read); |
735 | else |
736 | PyErr_SetString(PyExc_EOFError, |
737 | "EOF read where not expected"); |
738 | } |
739 | return NULL; |
740 | } |
741 | return p->buf; |
742 | } |
743 | |
744 | static int |
745 | r_byte(RFILE *p) |
746 | { |
747 | int c = EOF; |
748 | |
749 | if (p->ptr != NULL) { Branch (749:9): [True: 6.24M, False: 56.8k]
|
750 | if (p->ptr < p->end) Branch (750:13): [True: 6.24M, False: 34]
|
751 | c = (unsigned char) *p->ptr++; |
752 | return c; |
753 | } |
754 | if (!p->readable) { Branch (754:9): [True: 44, False: 56.8k]
|
755 | assert(p->fp); |
756 | c = getc(p->fp); |
757 | } |
758 | else { |
759 | const char *ptr = r_string(1, p); |
760 | if (ptr != NULL) Branch (760:13): [True: 56.8k, False: 0]
|
761 | c = *(const unsigned char *) ptr; |
762 | } |
763 | return c; |
764 | } |
765 | |
766 | static int |
767 | r_short(RFILE *p) |
768 | { |
769 | short x = -1; |
770 | const unsigned char *buffer; |
771 | |
772 | buffer = (const unsigned char *) r_string(2, p); |
773 | if (buffer != NULL) { Branch (773:9): [True: 6.07k, False: 2]
|
774 | x = buffer[0]; |
775 | x |= buffer[1] << 8; |
776 | /* Sign-extension, in case short greater than 16 bits */ |
777 | x |= -(x & 0x8000); |
778 | } |
779 | return x; |
780 | } |
781 | |
782 | static long |
783 | r_long(RFILE *p) |
784 | { |
785 | long x = -1; |
786 | const unsigned char *buffer; |
787 | |
788 | buffer = (const unsigned char *) r_string(4, p); |
789 | if (buffer != NULL) { Branch (789:9): [True: 3.97M, False: 39]
|
790 | x = buffer[0]; |
791 | x |= (long)buffer[1] << 8; |
792 | x |= (long)buffer[2] << 16; |
793 | x |= (long)buffer[3] << 24; |
794 | #if SIZEOF_LONG > 4 |
795 | /* Sign extension for 64-bit machines */ |
796 | x |= -(x & 0x80000000L); |
797 | #endif |
798 | } |
799 | return x; |
800 | } |
801 | |
802 | /* r_long64 deals with the TYPE_INT64 code. */ |
803 | static PyObject * |
804 | r_long64(RFILE *p) |
805 | { |
806 | const unsigned char *buffer = (const unsigned char *) r_string(8, p); |
807 | if (buffer == NULL) { Branch (807:9): [True: 2, False: 258]
|
808 | return NULL; |
809 | } |
810 | return _PyLong_FromByteArray(buffer, 8, |
811 | 1 /* little endian */, |
812 | 1 /* signed */); |
813 | } |
814 | |
815 | static PyObject * |
816 | r_PyLong(RFILE *p) |
817 | { |
818 | PyLongObject *ob; |
819 | long n, size, i; |
820 | int j, md, shorts_in_top_digit; |
821 | digit d; |
822 | |
823 | n = r_long(p); |
824 | if (PyErr_Occurred()) Branch (824:9): [True: 2, False: 1.28k]
|
825 | return NULL; |
826 | if (n == 0) Branch (826:9): [True: 0, False: 1.28k]
|
827 | return (PyObject *)_PyLong_New(0); |
828 | if (n < -SIZE32_MAX || n > SIZE32_MAX) { Branch (828:9): [True: 0, False: 1.28k]
Branch (828:28): [True: 0, False: 1.28k]
|
829 | PyErr_SetString(PyExc_ValueError, |
830 | "bad marshal data (long size out of range)"); |
831 | return NULL; |
832 | } |
833 | |
834 | size = 1 + (Py_ABS(n) - 1) / PyLong_MARSHAL_RATIO; |
835 | shorts_in_top_digit = 1 + (Py_ABS(n) - 1) % PyLong_MARSHAL_RATIO; |
836 | ob = _PyLong_New(size); |
837 | if (ob == NULL) Branch (837:9): [True: 0, False: 1.28k]
|
838 | return NULL; |
839 | |
840 | Py_SET_SIZE(ob, n > 0 ? size : -size); |
841 | |
842 | for (i = 0; i < size-1; i++2.22k ) { Branch (842:17): [True: 2.22k, False: 1.28k]
|
843 | d = 0; |
844 | for (j=0; j < PyLong_MARSHAL_RATIO; j++4.45k ) { Branch (844:19): [True: 4.45k, False: 2.22k]
|
845 | md = r_short(p); |
846 | if (PyErr_Occurred()) { Branch (846:17): [True: 0, False: 4.45k]
|
847 | Py_DECREF(ob); |
848 | return NULL; |
849 | } |
850 | if (md < 0 || md > PyLong_MARSHAL_BASE) Branch (850:17): [True: 0, False: 4.45k]
Branch (850:27): [True: 0, False: 4.45k]
|
851 | goto bad_digit; |
852 | d += (digit)md << j*PyLong_MARSHAL_SHIFT; |
853 | } |
854 | ob->ob_digit[i] = d; |
855 | } |
856 | |
857 | d = 0; |
858 | for (j=0; j < shorts_in_top_digit; j++1.61k ) { Branch (858:15): [True: 1.61k, False: 1.28k]
|
859 | md = r_short(p); |
860 | if (PyErr_Occurred()) { Branch (860:13): [True: 1, False: 1.61k]
|
861 | Py_DECREF(ob); |
862 | return NULL; |
863 | } |
864 | if (md < 0 || md > PyLong_MARSHAL_BASE) Branch (864:13): [True: 0, False: 1.61k]
Branch (864:23): [True: 0, False: 1.61k]
|
865 | goto bad_digit; |
866 | /* topmost marshal digit should be nonzero */ |
867 | if (md == 0 && j == shorts_in_top_digit - 135 ) { Branch (867:13): [True: 35, False: 1.57k]
Branch (867:24): [True: 1, False: 34]
|
868 | Py_DECREF(ob); |
869 | PyErr_SetString(PyExc_ValueError, |
870 | "bad marshal data (unnormalized long data)"); |
871 | return NULL; |
872 | } |
873 | d += (digit)md << j*PyLong_MARSHAL_SHIFT; |
874 | } |
875 | if (PyErr_Occurred()) { Branch (875:9): [True: 0, False: 1.28k]
|
876 | Py_DECREF(ob); |
877 | return NULL; |
878 | } |
879 | /* top digit should be nonzero, else the resulting PyLong won't be |
880 | normalized */ |
881 | ob->ob_digit[size-1] = d; |
882 | return (PyObject *)ob; |
883 | bad_digit: |
884 | Py_DECREF(ob); |
885 | PyErr_SetString(PyExc_ValueError, |
886 | "bad marshal data (digit out of range in long)"); |
887 | return NULL; |
888 | } |
889 | |
890 | static double |
891 | r_float_bin(RFILE *p) |
892 | { |
893 | const char *buf = r_string(8, p); |
894 | if (buf == NULL) Branch (894:9): [True: 6, False: 16.0k]
|
895 | return -1; |
896 | return PyFloat_Unpack8(buf, 1); |
897 | } |
898 | |
899 | /* Issue #33720: Disable inlining for reducing the C stack consumption |
900 | on PGO builds. */ |
901 | Py_NO_INLINE static double |
902 | r_float_str(RFILE *p) |
903 | { |
904 | int n; |
905 | char buf[256]; |
906 | const char *ptr; |
907 | n = r_byte(p); |
908 | if (n == EOF) { Branch (908:9): [True: 5, False: 461]
|
909 | PyErr_SetString(PyExc_EOFError, |
910 | "EOF read where object expected"); |
911 | return -1; |
912 | } |
913 | ptr = r_string(n, p); |
914 | if (ptr == NULL) { Branch (914:9): [True: 0, False: 461]
|
915 | return -1; |
916 | } |
917 | memcpy(buf, ptr, n); |
918 | buf[n] = '\0'; |
919 | return PyOS_string_to_double(buf, NULL, NULL); |
920 | } |
921 | |
922 | /* allocate the reflist index for a new object. Return -1 on failure */ |
923 | static Py_ssize_t |
924 | r_ref_reserve(int flag, RFILE *p) |
925 | { |
926 | if (flag) { /* currently only FLAG_REF is defined */ Branch (926:9): [True: 4.92k, False: 188k]
|
927 | Py_ssize_t idx = PyList_GET_SIZE(p->refs); |
928 | if (idx >= 0x7ffffffe) { Branch (928:13): [True: 0, False: 4.92k]
|
929 | PyErr_SetString(PyExc_ValueError, "bad marshal data (index list too large)"); |
930 | return -1; |
931 | } |
932 | if (PyList_Append(p->refs, Py_None) < 0) Branch (932:13): [True: 0, False: 4.92k]
|
933 | return -1; |
934 | return idx; |
935 | } else |
936 | return 0; |
937 | } |
938 | |
939 | /* insert the new object 'o' to the reflist at previously |
940 | * allocated index 'idx'. |
941 | * 'o' can be NULL, in which case nothing is done. |
942 | * if 'o' was non-NULL, and the function succeeds, 'o' is returned. |
943 | * if 'o' was non-NULL, and the function fails, 'o' is released and |
944 | * NULL returned. This simplifies error checking at the call site since |
945 | * a single test for NULL for the function result is enough. |
946 | */ |
947 | static PyObject * |
948 | r_ref_insert(PyObject *o, Py_ssize_t idx, int flag, RFILE *p) |
949 | { |
950 | if (o != NULL && flag191k ) { /* currently only FLAG_REF is defined */ Branch (950:9): [True: 191k, False: 2.00k]
Branch (950:22): [True: 4.92k, False: 186k]
|
951 | PyObject *tmp = PyList_GET_ITEM(p->refs, idx); |
952 | Py_INCREF(o); |
953 | PyList_SET_ITEM(p->refs, idx, o); |
954 | Py_DECREF(tmp); |
955 | } |
956 | return o; |
957 | } |
958 | |
959 | /* combination of both above, used when an object can be |
960 | * created whenever it is seen in the file, as opposed to |
961 | * after having loaded its sub-objects. |
962 | */ |
963 | static PyObject * |
964 | r_ref(PyObject *o, int flag, RFILE *p) |
965 | { |
966 | assert(flag & FLAG_REF); |
967 | if (o == NULL) Branch (967:9): [True: 3, False: 1.14M]
|
968 | return NULL; |
969 | if (PyList_Append(p->refs, o) < 0) { Branch (969:9): [True: 0, False: 1.14M]
|
970 | Py_DECREF(o); /* release the new object */ |
971 | return NULL; |
972 | } |
973 | return o; |
974 | } |
975 | |
976 | static PyObject * |
977 | r_object(RFILE *p) |
978 | { |
979 | /* NULL is a valid return value, it does not necessarily means that |
980 | an exception is set. */ |
981 | PyObject *v, *v2; |
982 | Py_ssize_t idx = 0; |
983 | long i, n; |
984 | int type, code = r_byte(p); |
985 | int flag, is_interned = 0; |
986 | PyObject *retval = NULL; |
987 | |
988 | if (code == EOF) { Branch (988:9): [True: 20, False: 4.74M]
|
989 | PyErr_SetString(PyExc_EOFError, |
990 | "EOF read where object expected"); |
991 | return NULL; |
992 | } |
993 | |
994 | p->depth++; |
995 | |
996 | if (p->depth > MAX_MARSHAL_STACK_DEPTH) { Branch (996:9): [True: 5, False: 4.74M]
|
997 | p->depth--; |
998 | PyErr_SetString(PyExc_ValueError, "recursion limit exceeded"); |
999 | return NULL; |
1000 | } |
1001 | |
1002 | flag = code & FLAG_REF; |
1003 | type = code & ~FLAG_REF; |
1004 | |
1005 | #define R_REF(O) do2.24M {\ |
1006 | if (flag) \ |
1007 | O = r_ref(O, flag, p)1.14M ;\ |
1008 | } while (0) |
1009 | |
1010 | switch (type) { |
1011 | |
1012 | case TYPE_NULL: Branch (1012:5): [True: 2.18k, False: 4.74M]
|
1013 | break; |
1014 | |
1015 | case TYPE_NONE: Branch (1015:5): [True: 122k, False: 4.62M]
|
1016 | Py_INCREF(Py_None); |
1017 | retval = Py_None; |
1018 | break; |
1019 | |
1020 | case TYPE_STOPITER: Branch (1020:5): [True: 3, False: 4.74M]
|
1021 | Py_INCREF(PyExc_StopIteration); |
1022 | retval = PyExc_StopIteration; |
1023 | break; |
1024 | |
1025 | case TYPE_ELLIPSIS: Branch (1025:5): [True: 459, False: 4.74M]
|
1026 | Py_INCREF(Py_Ellipsis); |
1027 | retval = Py_Ellipsis; |
1028 | break; |
1029 | |
1030 | case TYPE_FALSE: Branch (1030:5): [True: 11.8k, False: 4.73M]
|
1031 | Py_INCREF(Py_False); |
1032 | retval = Py_False; |
1033 | break; |
1034 | |
1035 | case TYPE_TRUE: Branch (1035:5): [True: 12.3k, False: 4.73M]
|
1036 | Py_INCREF(Py_True); |
1037 | retval = Py_True; |
1038 | break; |
1039 | |
1040 | case TYPE_INT: Branch (1040:5): [True: 61.6k, False: 4.68M]
|
1041 | n = r_long(p); |
1042 | retval = PyErr_Occurred() ? NULL : PyLong_FromLong(n)61.6k ; Branch (1042:18): [True: 2, False: 61.6k]
|
1043 | R_REF(retval); |
1044 | break; |
1045 | |
1046 | case TYPE_INT64: Branch (1046:5): [True: 260, False: 4.74M]
|
1047 | retval = r_long64(p); |
1048 | R_REF(retval); |
1049 | break; |
1050 | |
1051 | case TYPE_LONG: Branch (1051:5): [True: 1.28k, False: 4.74M]
|
1052 | retval = r_PyLong(p); |
1053 | R_REF(retval); |
1054 | break; |
1055 | |
1056 | case TYPE_FLOAT: Branch (1056:5): [True: 436, False: 4.74M]
|
1057 | { |
1058 | double x = r_float_str(p); |
1059 | if (x == -1.0 && PyErr_Occurred()3 ) Branch (1059:17): [True: 3, False: 433]
Branch (1059:30): [True: 3, False: 0]
|
1060 | break; |
1061 | retval = PyFloat_FromDouble(x); |
1062 | R_REF(retval); |
1063 | break; |
1064 | } |
1065 | |
1066 | case TYPE_BINARY_FLOAT: Branch (1066:5): [True: 15.5k, False: 4.72M]
|
1067 | { |
1068 | double x = r_float_bin(p); |
1069 | if (x == -1.0 && PyErr_Occurred()51 ) Branch (1069:17): [True: 51, False: 15.5k]
Branch (1069:30): [True: 3, False: 48]
|
1070 | break; |
1071 | retval = PyFloat_FromDouble(x); |
1072 | R_REF(retval); |
1073 | break; |
1074 | } |
1075 | |
1076 | case TYPE_COMPLEX: Branch (1076:5): [True: 16, False: 4.74M]
|
1077 | { |
1078 | Py_complex c; |
1079 | c.real = r_float_str(p); |
1080 | if (c.real == -1.0 && PyErr_Occurred()2 ) Branch (1080:17): [True: 2, False: 14]
Branch (1080:35): [True: 2, False: 0]
|
1081 | break; |
1082 | c.imag = r_float_str(p); |
1083 | if (c.imag == -1.0 && PyErr_Occurred()0 ) Branch (1083:17): [True: 0, False: 14]
Branch (1083:35): [True: 0, False: 0]
|
1084 | break; |
1085 | retval = PyComplex_FromCComplex(c); |
1086 | R_REF(retval); |
1087 | break; |
1088 | } |
1089 | |
1090 | case TYPE_BINARY_COMPLEX: Branch (1090:5): [True: 269, False: 4.74M]
|
1091 | { |
1092 | Py_complex c; |
1093 | c.real = r_float_bin(p); |
1094 | if (c.real == -1.0 && PyErr_Occurred()10 ) Branch (1094:17): [True: 10, False: 259]
Branch (1094:35): [True: 3, False: 7]
|
1095 | break; |
1096 | c.imag = r_float_bin(p); |
1097 | if (c.imag == -1.0 && PyErr_Occurred()12 ) Branch (1097:17): [True: 12, False: 254]
Branch (1097:35): [True: 0, False: 12]
|
1098 | break; |
1099 | retval = PyComplex_FromCComplex(c); |
1100 | R_REF(retval); |
1101 | break; |
1102 | } |
1103 | |
1104 | case TYPE_STRING: Branch (1104:5): [True: 557k, False: 4.18M]
|
1105 | { |
1106 | const char *ptr; |
1107 | n = r_long(p); |
1108 | if (PyErr_Occurred()) Branch (1108:17): [True: 2, False: 557k]
|
1109 | break; |
1110 | if (n < 0 || n > SIZE32_MAX) { Branch (1110:17): [True: 0, False: 557k]
Branch (1110:26): [True: 0, False: 557k]
|
1111 | PyErr_SetString(PyExc_ValueError, "bad marshal data (bytes object size out of range)"); |
1112 | break; |
1113 | } |
1114 | v = PyBytes_FromStringAndSize((char *)NULL, n); |
1115 | if (v == NULL) Branch (1115:17): [True: 0, False: 557k]
|
1116 | break; |
1117 | ptr = r_string(n, p); |
1118 | if (ptr == NULL) { Branch (1118:17): [True: 1, False: 557k]
|
1119 | Py_DECREF(v); |
1120 | break; |
1121 | } |
1122 | memcpy(PyBytes_AS_STRING(v), ptr, n); |
1123 | retval = v; |
1124 | R_REF(retval); |
1125 | break; |
1126 | } |
1127 | |
1128 | case TYPE_ASCII_INTERNED: Branch (1128:5): [True: 37, False: 4.74M]
|
1129 | is_interned = 1; |
1130 | /* fall through */ |
1131 | case TYPE_ASCII: Branch (1131:5): [True: 12.0k, False: 4.73M]
|
1132 | n = r_long(p); |
1133 | if (PyErr_Occurred()) Branch (1133:13): [True: 4, False: 12.1k]
|
1134 | break; |
1135 | if (n < 0 || n > SIZE32_MAX) { Branch (1135:13): [True: 0, False: 12.1k]
Branch (1135:22): [True: 0, False: 12.1k]
|
1136 | PyErr_SetString(PyExc_ValueError, "bad marshal data (string size out of range)"); |
1137 | break; |
1138 | } |
1139 | goto _read_ascii; |
1140 | |
1141 | case TYPE_SHORT_ASCII_INTERNED: Branch (1141:5): [True: 788k, False: 3.95M]
|
1142 | is_interned = 1; |
1143 | /* fall through */ |
1144 | case TYPE_SHORT_ASCII: Branch (1144:5): [True: 324k, False: 4.42M]
|
1145 | n = r_byte(p); |
1146 | if (n == EOF) { Branch (1146:13): [True: 6, False: 1.11M]
|
1147 | PyErr_SetString(PyExc_EOFError, |
1148 | "EOF read where object expected"); |
1149 | break; |
1150 | } |
1151 | _read_ascii: |
1152 | { |
1153 | const char *ptr; |
1154 | ptr = r_string(n, p); |
1155 | if (ptr == NULL) Branch (1155:17): [True: 11, False: 1.12M]
|
1156 | break; |
1157 | v = PyUnicode_FromKindAndData(PyUnicode_1BYTE_KIND, ptr, n); |
1158 | if (v == NULL) Branch (1158:17): [True: 0, False: 1.12M]
|
1159 | break; |
1160 | if (is_interned) Branch (1160:17): [True: 788k, False: 336k]
|
1161 | PyUnicode_InternInPlace(&v); |
1162 | retval = v; |
1163 | R_REF(retval); |
1164 | break; |
1165 | } |
1166 | |
1167 | case TYPE_INTERNED: Branch (1167:5): [True: 625, False: 4.74M]
|
1168 | is_interned = 1; |
1169 | /* fall through */ |
1170 | case TYPE_UNICODE: Branch (1170:5): [True: 20.9k, False: 4.72M]
|
1171 | { |
1172 | const char *buffer; |
1173 | |
1174 | n = r_long(p); |
1175 | if (PyErr_Occurred()) Branch (1175:13): [True: 4, False: 21.6k]
|
1176 | break; |
1177 | if (n < 0 || n > SIZE32_MAX) { Branch (1177:13): [True: 0, False: 21.6k]
Branch (1177:22): [True: 0, False: 21.6k]
|
1178 | PyErr_SetString(PyExc_ValueError, "bad marshal data (string size out of range)"); |
1179 | break; |
1180 | } |
1181 | if (n != 0) { Branch (1181:13): [True: 21.6k, False: 12]
|
1182 | buffer = r_string(n, p); |
1183 | if (buffer == NULL) Branch (1183:17): [True: 0, False: 21.6k]
|
1184 | break; |
1185 | v = PyUnicode_DecodeUTF8(buffer, n, "surrogatepass"); |
1186 | } |
1187 | else { |
1188 | v = PyUnicode_New(0, 0); |
1189 | } |
1190 | if (v == NULL) Branch (1190:13): [True: 0, False: 21.6k]
|
1191 | break; |
1192 | if (is_interned) Branch (1192:13): [True: 623, False: 20.9k]
|
1193 | PyUnicode_InternInPlace(&v); |
1194 | retval = v; |
1195 | R_REF(retval); |
1196 | break; |
1197 | } |
1198 | |
1199 | case TYPE_SMALL_TUPLE: Branch (1199:5): [True: 441k, False: 4.30M]
|
1200 | n = (unsigned char) r_byte(p); |
1201 | if (PyErr_Occurred()) Branch (1201:13): [True: 0, False: 441k]
|
1202 | break; |
1203 | goto _read_tuple; |
1204 | case 15.0k TYPE_TUPLE15.0k : Branch (1204:5): [True: 15.0k, False: 4.72M]
|
1205 | n = r_long(p); |
1206 | if (PyErr_Occurred()) Branch (1206:13): [True: 10, False: 15.0k]
|
1207 | break; |
1208 | if (n < 0 || n > SIZE32_MAX) { Branch (1208:13): [True: 0, False: 15.0k]
Branch (1208:22): [True: 0, False: 15.0k]
|
1209 | PyErr_SetString(PyExc_ValueError, "bad marshal data (tuple size out of range)"); |
1210 | break; |
1211 | } |
1212 | _read_tuple: |
1213 | v = PyTuple_New(n); |
1214 | R_REF(v); |
1215 | if (v == NULL) Branch (1215:13): [True: 0, False: 456k]
|
1216 | break; |
1217 | |
1218 | for (i = 0; 456k i < n; i++2.80M ) { Branch (1218:21): [True: 2.80M, False: 452k]
|
1219 | v2 = r_object(p); |
1220 | if ( v2 == NULL ) { Branch (1220:18): [True: 4.02k, False: 2.80M]
|
1221 | if (!PyErr_Occurred()) Branch (1221:21): [True: 0, False: 4.02k]
|
1222 | PyErr_SetString(PyExc_TypeError, |
1223 | "NULL object in marshal data for tuple"); |
1224 | Py_DECREF(v); |
1225 | v = NULL; |
1226 | break; |
1227 | } |
1228 | PyTuple_SET_ITEM(v, i, v2); |
1229 | } |
1230 | retval = v; |
1231 | break; |
1232 | |
1233 | case TYPE_LIST: Branch (1233:5): [True: 4.14k, False: 4.74M]
|
1234 | n = r_long(p); |
1235 | if (PyErr_Occurred()) Branch (1235:13): [True: 2, False: 4.13k]
|
1236 | break; |
1237 | if (n < 0 || n > SIZE32_MAX) { Branch (1237:13): [True: 0, False: 4.13k]
Branch (1237:22): [True: 0, False: 4.13k]
|
1238 | PyErr_SetString(PyExc_ValueError, "bad marshal data (list size out of range)"); |
1239 | break; |
1240 | } |
1241 | v = PyList_New(n); |
1242 | R_REF(v); |
1243 | if (v == NULL) Branch (1243:13): [True: 0, False: 4.13k]
|
1244 | break; |
1245 | for (i = 0; 4.13k i < n; i++4.18k ) { Branch (1245:21): [True: 6.18k, False: 2.13k]
|
1246 | v2 = r_object(p); |
1247 | if ( v2 == NULL ) { Branch (1247:18): [True: 2.00k, False: 4.18k]
|
1248 | if (!PyErr_Occurred()) Branch (1248:21): [True: 0, False: 2.00k]
|
1249 | PyErr_SetString(PyExc_TypeError, |
1250 | "NULL object in marshal data for list"); |
1251 | Py_DECREF(v); |
1252 | v = NULL; |
1253 | break; |
1254 | } |
1255 | PyList_SET_ITEM(v, i, v2); |
1256 | } |
1257 | retval = v; |
1258 | break; |
1259 | |
1260 | case TYPE_DICT: Branch (1260:5): [True: 4.18k, False: 4.74M]
|
1261 | v = PyDict_New(); |
1262 | R_REF(v); |
1263 | if (v == NULL) Branch (1263:13): [True: 0, False: 4.18k]
|
1264 | break; |
1265 | for (;;)4.18k { |
1266 | PyObject *key, *val; |
1267 | key = r_object(p); |
1268 | if (key == NULL) Branch (1268:17): [True: 2.18k, False: 7.50k]
|
1269 | break; |
1270 | val = r_object(p); |
1271 | if (val == NULL) { Branch (1271:17): [True: 1.99k, False: 5.50k]
|
1272 | Py_DECREF(key); |
1273 | break; |
1274 | } |
1275 | if (PyDict_SetItem(v, key, val) < 0) { Branch (1275:17): [True: 0, False: 5.50k]
|
1276 | Py_DECREF(key); |
1277 | Py_DECREF(val); |
1278 | break; |
1279 | } |
1280 | Py_DECREF(key); |
1281 | Py_DECREF(val); |
1282 | } |
1283 | if (PyErr_Occurred()) { Branch (1283:13): [True: 2.00k, False: 2.18k]
|
1284 | Py_DECREF(v); |
1285 | v = NULL; |
1286 | } |
1287 | retval = v; |
1288 | break; |
1289 | |
1290 | case TYPE_SET: Branch (1290:5): [True: 38, False: 4.74M]
|
1291 | case TYPE_FROZENSET: Branch (1291:5): [True: 2.62k, False: 4.74M]
|
1292 | n = r_long(p); |
1293 | if (PyErr_Occurred()) Branch (1293:13): [True: 8, False: 2.65k]
|
1294 | break; |
1295 | if (n < 0 || n > SIZE32_MAX) { Branch (1295:13): [True: 0, False: 2.65k]
Branch (1295:22): [True: 0, False: 2.65k]
|
1296 | PyErr_SetString(PyExc_ValueError, "bad marshal data (set size out of range)"); |
1297 | break; |
1298 | } |
1299 | |
1300 | if (n == 0 && type == 0 TYPE_FROZENSET0 ) { Branch (1300:13): [True: 0, False: 2.65k]
Branch (1300:23): [True: 0, False: 0]
|
1301 | /* call frozenset() to get the empty frozenset singleton */ |
1302 | v = _PyObject_CallNoArgs((PyObject*)&PyFrozenSet_Type); |
1303 | if (v == NULL) Branch (1303:17): [True: 0, False: 0]
|
1304 | break; |
1305 | R_REF(v); |
1306 | retval = v; |
1307 | } |
1308 | else { |
1309 | v = (type == TYPE_SET) ? PySet_New(NULL)36 : PyFrozenSet_New(NULL)2.61k ; Branch (1309:17): [True: 36, False: 2.61k]
|
1310 | if (type == TYPE_SET) { Branch (1310:17): [True: 36, False: 2.61k]
|
1311 | R_REF(v); |
1312 | } else { |
1313 | /* must use delayed registration of frozensets because they must |
1314 | * be init with a refcount of 1 |
1315 | */ |
1316 | idx = r_ref_reserve(flag, p); |
1317 | if (idx < 0) Branch (1317:21): [True: 0, False: 2.61k]
|
1318 | Py_CLEAR(v); /* signal error */ |
1319 | } |
1320 | if (v == NULL) Branch (1320:17): [True: 0, False: 2.65k]
|
1321 | break; |
1322 | |
1323 | for (i = 0; 2.65k i < n; i++2.74k ) { Branch (1323:25): [True: 4.74k, False: 646]
|
1324 | v2 = r_object(p); |
1325 | if ( v2 == NULL ) { Branch (1325:22): [True: 2.00k, False: 2.74k]
|
1326 | if (!PyErr_Occurred()) Branch (1326:25): [True: 0, False: 2.00k]
|
1327 | PyErr_SetString(PyExc_TypeError, |
1328 | "NULL object in marshal data for set"); |
1329 | Py_DECREF(v); |
1330 | v = NULL; |
1331 | break; |
1332 | } |
1333 | if (PySet_Add(v, v2) == -1) { Branch (1333:21): [True: 0, False: 2.74k]
|
1334 | Py_DECREF(v); |
1335 | Py_DECREF(v2); |
1336 | v = NULL; |
1337 | break; |
1338 | } |
1339 | Py_DECREF(v2); |
1340 | } |
1341 | if (type != TYPE_SET) Branch (1341:17): [True: 2.61k, False: 36]
|
1342 | v = r_ref_insert(v, idx, flag, p); |
1343 | retval = v; |
1344 | } |
1345 | break; |
1346 | |
1347 | case TYPE_CODE: Branch (1347:5): [True: 190k, False: 4.55M]
|
1348 | { |
1349 | int argcount; |
1350 | int posonlyargcount; |
1351 | int kwonlyargcount; |
1352 | int stacksize; |
1353 | int flags; |
1354 | PyObject *code = NULL; |
1355 | PyObject *consts = NULL; |
1356 | PyObject *names = NULL; |
1357 | PyObject *localsplusnames = NULL; |
1358 | PyObject *localspluskinds = NULL; |
1359 | PyObject *filename = NULL; |
1360 | PyObject *name = NULL; |
1361 | PyObject *qualname = NULL; |
1362 | int firstlineno; |
1363 | PyObject* linetable = NULL; |
1364 | PyObject *exceptiontable = NULL; |
1365 | |
1366 | idx = r_ref_reserve(flag, p); |
1367 | if (idx < 0) Branch (1367:17): [True: 0, False: 190k]
|
1368 | break; |
1369 | |
1370 | v = NULL; |
1371 | |
1372 | /* XXX ignore long->int overflows for now */ |
1373 | argcount = (int)r_long(p); |
1374 | if (PyErr_Occurred()) Branch (1374:17): [True: 2, False: 190k]
|
1375 | goto code_error; |
1376 | posonlyargcount = (int)r_long(p); |
1377 | if (PyErr_Occurred()) { Branch (1377:17): [True: 0, False: 190k]
|
1378 | goto code_error; |
1379 | } |
1380 | kwonlyargcount = (int)r_long(p); |
1381 | if (PyErr_Occurred()) Branch (1381:17): [True: 0, False: 190k]
|
1382 | goto code_error; |
1383 | stacksize = (int)r_long(p); |
1384 | if (PyErr_Occurred()) Branch (1384:17): [True: 0, False: 190k]
|
1385 | goto code_error; |
1386 | flags = (int)r_long(p); |
1387 | if (PyErr_Occurred()) Branch (1387:17): [True: 0, False: 190k]
|
1388 | goto code_error; |
1389 | code = r_object(p); |
1390 | if (code == NULL) Branch (1390:17): [True: 0, False: 190k]
|
1391 | goto code_error; |
1392 | consts = r_object(p); |
1393 | if (consts == NULL) Branch (1393:17): [True: 0, False: 190k]
|
1394 | goto code_error; |
1395 | names = r_object(p); |
1396 | if (names == NULL) Branch (1396:17): [True: 0, False: 190k]
|
1397 | goto code_error; |
1398 | localsplusnames = r_object(p); |
1399 | if (localsplusnames == NULL) Branch (1399:17): [True: 0, False: 190k]
|
1400 | goto code_error; |
1401 | localspluskinds = r_object(p); |
1402 | if (localspluskinds == NULL) Branch (1402:17): [True: 0, False: 190k]
|
1403 | goto code_error; |
1404 | filename = r_object(p); |
1405 | if (filename == NULL) Branch (1405:17): [True: 0, False: 190k]
|
1406 | goto code_error; |
1407 | name = r_object(p); |
1408 | if (name == NULL) Branch (1408:17): [True: 0, False: 190k]
|
1409 | goto code_error; |
1410 | qualname = r_object(p); |
1411 | if (qualname == NULL) Branch (1411:17): [True: 0, False: 190k]
|
1412 | goto code_error; |
1413 | firstlineno = (int)r_long(p); |
1414 | if (firstlineno == -1 && PyErr_Occurred()0 ) Branch (1414:17): [True: 0, False: 190k]
Branch (1414:38): [True: 0, False: 0]
|
1415 | break; |
1416 | linetable = r_object(p); |
1417 | if (linetable == NULL) Branch (1417:17): [True: 0, False: 190k]
|
1418 | goto code_error; |
1419 | exceptiontable = r_object(p); |
1420 | if (exceptiontable == NULL) Branch (1420:17): [True: 0, False: 190k]
|
1421 | goto code_error; |
1422 | |
1423 | struct _PyCodeConstructor con = { |
1424 | .filename = filename, |
1425 | .name = name, |
1426 | .qualname = qualname, |
1427 | .flags = flags, |
1428 | |
1429 | .code = code, |
1430 | .firstlineno = firstlineno, |
1431 | .linetable = linetable, |
1432 | |
1433 | .consts = consts, |
1434 | .names = names, |
1435 | |
1436 | .localsplusnames = localsplusnames, |
1437 | .localspluskinds = localspluskinds, |
1438 | |
1439 | .argcount = argcount, |
1440 | .posonlyargcount = posonlyargcount, |
1441 | .kwonlyargcount = kwonlyargcount, |
1442 | |
1443 | .stacksize = stacksize, |
1444 | |
1445 | .exceptiontable = exceptiontable, |
1446 | }; |
1447 | |
1448 | if (_PyCode_Validate(&con) < 0) { Branch (1448:17): [True: 0, False: 190k]
|
1449 | goto code_error; |
1450 | } |
1451 | |
1452 | v = (PyObject *)_PyCode_New(&con); |
1453 | if (v == NULL) { Branch (1453:17): [True: 0, False: 190k]
|
1454 | goto code_error; |
1455 | } |
1456 | |
1457 | v = r_ref_insert(v, idx, flag, p); |
1458 | |
1459 | code_error: |
1460 | Py_XDECREF(code); |
1461 | Py_XDECREF(consts); |
1462 | Py_XDECREF(names); |
1463 | Py_XDECREF(localsplusnames); |
1464 | Py_XDECREF(localspluskinds); |
1465 | Py_XDECREF(filename); |
1466 | Py_XDECREF(name); |
1467 | Py_XDECREF(qualname); |
1468 | Py_XDECREF(linetable); |
1469 | Py_XDECREF(exceptiontable); |
1470 | } |
1471 | retval = v; |
1472 | break; |
1473 | |
1474 | case TYPE_REF: Branch (1474:5): [True: 2.15M, False: 2.59M]
|
1475 | n = r_long(p); |
1476 | if (n < 0 || n >= 2.15M PyList_GET_SIZE2.15M (p->refs)) { Branch (1476:13): [True: 2, False: 2.15M]
Branch (1476:22): [True: 0, False: 2.15M]
|
1477 | if (n == -1 && PyErr_Occurred()) Branch (1477:17): [True: 2, False: 0]
Branch (1477:28): [True: 2, False: 0]
|
1478 | break; |
1479 | PyErr_SetString(PyExc_ValueError, "bad marshal data (invalid reference)"); |
1480 | break; |
1481 | } |
1482 | v = PyList_GET_ITEM(p->refs, n); |
1483 | if (v == Py_None) { Branch (1483:13): [True: 0, False: 2.15M]
|
1484 | PyErr_SetString(PyExc_ValueError, "bad marshal data (invalid reference)"); |
1485 | break; |
1486 | } |
1487 | Py_INCREF(v); |
1488 | retval = v; |
1489 | break; |
1490 | |
1491 | default: Branch (1491:5): [True: 200, False: 4.74M]
|
1492 | /* Bogus data got written, which isn't ideal. |
1493 | This will let you keep working and recover. */ |
1494 | PyErr_SetString(PyExc_ValueError, "bad marshal data (unknown type code)"); |
1495 | break; |
1496 | |
1497 | } |
1498 | p->depth--; |
1499 | return retval; |
1500 | } |
1501 | |
1502 | static PyObject * |
1503 | read_object(RFILE *p) |
1504 | { |
1505 | PyObject *v; |
1506 | if (PyErr_Occurred()) { Branch (1506:9): [True: 0, False: 7.83k]
|
1507 | fprintf(stderr, "XXX readobject called with exception set\n"); |
1508 | return NULL; |
1509 | } |
1510 | if (p->ptr && p->end6.80k ) { Branch (1510:9): [True: 6.80k, False: 1.03k]
Branch (1510:19): [True: 6.80k, False: 0]
|
1511 | if (PySys_Audit("marshal.loads", "y#", p->ptr, (Py_ssize_t)(p->end - p->ptr)) < 0) { Branch (1511:13): [True: 0, False: 6.80k]
|
1512 | return NULL; |
1513 | } |
1514 | } else if (1.03k p->fp1.03k || p->readable1.02k ) { Branch (1514:16): [True: 10, False: 1.02k]
Branch (1514:25): [True: 1.02k, False: 0]
|
1515 | if (PySys_Audit("marshal.load", NULL) < 0) { Branch (1515:13): [True: 0, False: 1.03k]
|
1516 | return NULL; |
1517 | } |
1518 | } |
1519 | v = r_object(p); |
1520 | if (v == NULL && !PyErr_Occurred()299 ) Branch (1520:9): [True: 299, False: 7.53k]
Branch (1520:22): [True: 3, False: 296]
|
1521 | PyErr_SetString(PyExc_TypeError, "NULL object in marshal data for object"); |
1522 | return v; |
1523 | } |
1524 | |
1525 | int |
1526 | PyMarshal_ReadShortFromFile(FILE *fp) |
1527 | { |
1528 | RFILE rf; |
1529 | int res; |
1530 | assert(fp); |
1531 | rf.readable = NULL; |
1532 | rf.fp = fp; |
1533 | rf.end = rf.ptr = NULL; |
1534 | rf.buf = NULL; |
1535 | res = r_short(&rf); |
1536 | if (rf.buf != NULL) Branch (1536:9): [True: 2, False: 0]
|
1537 | PyMem_Free(rf.buf); |
1538 | return res; |
1539 | } |
1540 | |
1541 | long |
1542 | PyMarshal_ReadLongFromFile(FILE *fp) |
1543 | { |
1544 | RFILE rf; |
1545 | long res; |
1546 | rf.fp = fp; |
1547 | rf.readable = NULL; |
1548 | rf.ptr = rf.end = NULL; |
1549 | rf.buf = NULL; |
1550 | res = r_long(&rf); |
1551 | if (rf.buf != NULL) Branch (1551:9): [True: 2, False: 0]
|
1552 | PyMem_Free(rf.buf); |
1553 | return res; |
1554 | } |
1555 | |
1556 | /* Return size of file in bytes; < 0 if unknown or INT_MAX if too big */ |
1557 | static off_t |
1558 | getfilesize(FILE *fp) |
1559 | { |
1560 | struct _Py_stat_struct st; |
1561 | if (_Py_fstat_noraise(fileno(fp), &st) != 0) Branch (1561:9): [True: 0, False: 10]
|
1562 | return -1; |
1563 | #if SIZEOF_OFF_T == 4 |
1564 | else if (st.st_size >= INT_MAX) |
1565 | return (off_t)INT_MAX; |
1566 | #endif |
1567 | else |
1568 | return (off_t)st.st_size; |
1569 | } |
1570 | |
1571 | /* If we can get the size of the file up-front, and it's reasonably small, |
1572 | * read it in one gulp and delegate to ...FromString() instead. Much quicker |
1573 | * than reading a byte at a time from file; speeds .pyc imports. |
1574 | * CAUTION: since this may read the entire remainder of the file, don't |
1575 | * call it unless you know you're done with the file. |
1576 | */ |
1577 | PyObject * |
1578 | PyMarshal_ReadLastObjectFromFile(FILE *fp) |
1579 | { |
1580 | /* REASONABLE_FILE_LIMIT is by defn something big enough for Tkinter.pyc. */ |
1581 | #define REASONABLE_FILE_LIMIT (1L << 18) |
1582 | off_t filesize; |
1583 | filesize = getfilesize(fp); |
1584 | if (filesize > 0 && filesize <= REASONABLE_FILE_LIMIT) { Branch (1584:9): [True: 10, False: 0]
Branch (1584:25): [True: 10, False: 0]
|
1585 | char* pBuf = (char *)PyMem_Malloc(filesize); |
1586 | if (pBuf != NULL) { Branch (1586:13): [True: 10, False: 0]
|
1587 | size_t n = fread(pBuf, 1, (size_t)filesize, fp); |
1588 | PyObject* v = PyMarshal_ReadObjectFromString(pBuf, n); |
1589 | PyMem_Free(pBuf); |
1590 | return v; |
1591 | } |
1592 | |
1593 | } |
1594 | /* We don't have fstat, or we do but the file is larger than |
1595 | * REASONABLE_FILE_LIMIT or malloc failed -- read a byte at a time. |
1596 | */ |
1597 | return PyMarshal_ReadObjectFromFile(fp); |
1598 | |
1599 | #undef REASONABLE_FILE_LIMIT |
1600 | } |
1601 | |
1602 | PyObject * |
1603 | PyMarshal_ReadObjectFromFile(FILE *fp) |
1604 | { |
1605 | RFILE rf; |
1606 | PyObject *result; |
1607 | rf.fp = fp; |
1608 | rf.readable = NULL; |
1609 | rf.depth = 0; |
1610 | rf.ptr = rf.end = NULL; |
1611 | rf.buf = NULL; |
1612 | rf.refs = PyList_New(0); |
1613 | if (rf.refs == NULL) Branch (1613:9): [True: 0, False: 10]
|
1614 | return NULL; |
1615 | result = read_object(&rf); |
1616 | Py_DECREF(rf.refs); |
1617 | if (rf.buf != NULL) Branch (1617:9): [True: 9, False: 1]
|
1618 | PyMem_Free(rf.buf); |
1619 | return result; |
1620 | } |
1621 | |
1622 | PyObject * |
1623 | PyMarshal_ReadObjectFromString(const char *str, Py_ssize_t len) |
1624 | { |
1625 | RFILE rf; |
1626 | PyObject *result; |
1627 | rf.fp = NULL; |
1628 | rf.readable = NULL; |
1629 | rf.ptr = str; |
1630 | rf.end = str + len; |
1631 | rf.buf = NULL; |
1632 | rf.depth = 0; |
1633 | rf.refs = PyList_New(0); |
1634 | if (rf.refs == NULL) Branch (1634:9): [True: 0, False: 290]
|
1635 | return NULL; |
1636 | result = read_object(&rf); |
1637 | Py_DECREF(rf.refs); |
1638 | if (rf.buf != NULL) Branch (1638:9): [True: 0, False: 290]
|
1639 | PyMem_Free(rf.buf); |
1640 | return result; |
1641 | } |
1642 | |
1643 | PyObject * |
1644 | PyMarshal_WriteObjectToString(PyObject *x, int version) |
1645 | { |
1646 | WFILE wf; |
1647 | |
1648 | if (PySys_Audit("marshal.dumps", "Oi", x, version) < 0) { Branch (1648:9): [True: 0, False: 3.27k]
|
1649 | return NULL; |
1650 | } |
1651 | memset(&wf, 0, sizeof(wf)); |
1652 | wf.str = PyBytes_FromStringAndSize((char *)NULL, 50); |
1653 | if (wf.str == NULL) Branch (1653:9): [True: 0, False: 3.27k]
|
1654 | return NULL; |
1655 | wf.ptr = wf.buf = PyBytes_AS_STRING(wf.str); |
1656 | wf.end = wf.ptr + PyBytes_GET_SIZE(wf.str); |
1657 | wf.error = WFERR_OK; |
1658 | wf.version = version; |
1659 | if (w_init_refs(&wf, version)) { Branch (1659:9): [True: 0, False: 3.27k]
|
1660 | Py_DECREF(wf.str); |
1661 | return NULL; |
1662 | } |
1663 | w_object(x, &wf); |
1664 | w_clear_refs(&wf); |
1665 | if (wf.str != NULL) { Branch (1665:9): [True: 3.27k, False: 0]
|
1666 | const char *base = PyBytes_AS_STRING(wf.str); |
1667 | if (_PyBytes_Resize(&wf.str, (Py_ssize_t)(wf.ptr - base)) < 0) Branch (1667:13): [True: 0, False: 3.27k]
|
1668 | return NULL; |
1669 | } |
1670 | if (wf.error != WFERR_OK) { Branch (1670:9): [True: 9, False: 3.26k]
|
1671 | Py_XDECREF(wf.str); |
1672 | if (wf.error == WFERR_NOMEMORY) Branch (1672:13): [True: 0, False: 9]
|
1673 | PyErr_NoMemory(); |
1674 | else |
1675 | PyErr_SetString(PyExc_ValueError, |
1676 | (wf.error==WFERR_UNMARSHALLABLE)?"unmarshallable object"8 Branch (1676:15): [True: 8, False: 1]
|
1677 | :"object too deeply nested to marshal"1 ); |
1678 | return NULL; |
1679 | } |
1680 | return wf.str; |
1681 | } |
1682 | |
1683 | /* And an interface for Python programs... */ |
1684 | /*[clinic input] |
1685 | marshal.dump |
1686 | |
1687 | value: object |
1688 | Must be a supported type. |
1689 | file: object |
1690 | Must be a writeable binary file. |
1691 | version: int(c_default="Py_MARSHAL_VERSION") = version |
1692 | Indicates the data format that dump should use. |
1693 | / |
1694 | |
1695 | Write the value on the open file. |
1696 | |
1697 | If the value has (or contains an object that has) an unsupported type, a |
1698 | ValueError exception is raised - but garbage data will also be written |
1699 | to the file. The object will not be properly read back by load(). |
1700 | [clinic start generated code]*/ |
1701 | |
1702 | static PyObject * |
1703 | marshal_dump_impl(PyObject *module, PyObject *value, PyObject *file, |
1704 | int version) |
1705 | /*[clinic end generated code: output=aaee62c7028a7cb2 input=6c7a3c23c6fef556]*/ |
1706 | { |
1707 | /* XXX Quick hack -- need to do this differently */ |
1708 | PyObject *s; |
1709 | PyObject *res; |
1710 | |
1711 | s = PyMarshal_WriteObjectToString(value, version); |
1712 | if (s == NULL) Branch (1712:9): [True: 0, False: 999]
|
1713 | return NULL; |
1714 | res = _PyObject_CallMethodOneArg(file, &_Py_ID(write), s); |
1715 | Py_DECREF(s); |
1716 | return res; |
1717 | } |
1718 | |
1719 | /*[clinic input] |
1720 | marshal.load |
1721 | |
1722 | file: object |
1723 | Must be readable binary file. |
1724 | / |
1725 | |
1726 | Read one value from the open file and return it. |
1727 | |
1728 | If no valid value is read (e.g. because the data has a different Python |
1729 | version's incompatible marshal format), raise EOFError, ValueError or |
1730 | TypeError. |
1731 | |
1732 | Note: If an object containing an unsupported type was marshalled with |
1733 | dump(), load() will substitute None for the unmarshallable type. |
1734 | [clinic start generated code]*/ |
1735 | |
1736 | static PyObject * |
1737 | marshal_load(PyObject *module, PyObject *file) |
1738 | /*[clinic end generated code: output=f8e5c33233566344 input=c85c2b594cd8124a]*/ |
1739 | { |
1740 | PyObject *data, *result; |
1741 | RFILE rf; |
1742 | |
1743 | /* |
1744 | * Make a call to the read method, but read zero bytes. |
1745 | * This is to ensure that the object passed in at least |
1746 | * has a read method which returns bytes. |
1747 | * This can be removed if we guarantee good error handling |
1748 | * for r_string() |
1749 | */ |
1750 | data = _PyObject_CallMethod(file, &_Py_ID(read), "i", 0); |
1751 | if (data == NULL) Branch (1751:9): [True: 0, False: 1.02k]
|
1752 | return NULL; |
1753 | if (!PyBytes_Check(data)) { Branch (1753:9): [True: 0, False: 1.02k]
|
1754 | PyErr_Format(PyExc_TypeError, |
1755 | "file.read() returned not bytes but %.100s", |
1756 | Py_TYPE(data)->tp_name); |
1757 | result = NULL; |
1758 | } |
1759 | else { |
1760 | rf.depth = 0; |
1761 | rf.fp = NULL; |
1762 | rf.readable = file; |
1763 | rf.ptr = rf.end = NULL; |
1764 | rf.buf = NULL; |
1765 | if ((rf.refs = PyList_New(0)) != NULL) { Branch (1765:13): [True: 1.02k, False: 0]
|
1766 | result = read_object(&rf); |
1767 | Py_DECREF(rf.refs); |
1768 | if (rf.buf != NULL) Branch (1768:17): [True: 1.02k, False: 0]
|
1769 | PyMem_Free(rf.buf); |
1770 | } else |
1771 | result = NULL; |
1772 | } |
1773 | Py_DECREF(data); |
1774 | return result; |
1775 | } |
1776 | |
1777 | /*[clinic input] |
1778 | marshal.dumps |
1779 | |
1780 | value: object |
1781 | Must be a supported type. |
1782 | version: int(c_default="Py_MARSHAL_VERSION") = version |
1783 | Indicates the data format that dumps should use. |
1784 | / |
1785 | |
1786 | Return the bytes object that would be written to a file by dump(value, file). |
1787 | |
1788 | Raise a ValueError exception if value has (or contains an object that has) an |
1789 | unsupported type. |
1790 | [clinic start generated code]*/ |
1791 | |
1792 | static PyObject * |
1793 | marshal_dumps_impl(PyObject *module, PyObject *value, int version) |
1794 | /*[clinic end generated code: output=9c200f98d7256cad input=a2139ea8608e9b27]*/ |
1795 | { |
1796 | return PyMarshal_WriteObjectToString(value, version); |
1797 | } |
1798 | |
1799 | /*[clinic input] |
1800 | marshal.loads |
1801 | |
1802 | bytes: Py_buffer |
1803 | / |
1804 | |
1805 | Convert the bytes-like object to a value. |
1806 | |
1807 | If no valid value is found, raise EOFError, ValueError or TypeError. Extra |
1808 | bytes in the input are ignored. |
1809 | [clinic start generated code]*/ |
1810 | |
1811 | static PyObject * |
1812 | marshal_loads_impl(PyObject *module, Py_buffer *bytes) |
1813 | /*[clinic end generated code: output=9fc65985c93d1bb1 input=6f426518459c8495]*/ |
1814 | { |
1815 | RFILE rf; |
1816 | char *s = bytes->buf; |
1817 | Py_ssize_t n = bytes->len; |
1818 | PyObject* result; |
1819 | rf.fp = NULL; |
1820 | rf.readable = NULL; |
1821 | rf.ptr = s; |
1822 | rf.end = s + n; |
1823 | rf.depth = 0; |
1824 | if ((rf.refs = PyList_New(0)) == NULL) Branch (1824:9): [True: 0, False: 6.51k]
|
1825 | return NULL; |
1826 | result = read_object(&rf); |
1827 | Py_DECREF(rf.refs); |
1828 | return result; |
1829 | } |
1830 | |
1831 | static PyMethodDef marshal_methods[] = { |
1832 | MARSHAL_DUMP_METHODDEF |
1833 | MARSHAL_LOAD_METHODDEF |
1834 | MARSHAL_DUMPS_METHODDEF |
1835 | MARSHAL_LOADS_METHODDEF |
1836 | {NULL, NULL} /* sentinel */ |
1837 | }; |
1838 | |
1839 | |
1840 | PyDoc_STRVAR(module_doc, |
1841 | "This module contains functions that can read and write Python values in\n\ |
1842 | a binary format. The format is specific to Python, but independent of\n\ |
1843 | machine architecture issues.\n\ |
1844 | \n\ |
1845 | Not all Python object types are supported; in general, only objects\n\ |
1846 | whose value is independent from a particular invocation of Python can be\n\ |
1847 | written and read by this module. The following types are supported:\n\ |
1848 | None, integers, floating point numbers, strings, bytes, bytearrays,\n\ |
1849 | tuples, lists, sets, dictionaries, and code objects, where it\n\ |
1850 | should be understood that tuples, lists and dictionaries are only\n\ |
1851 | supported as long as the values contained therein are themselves\n\ |
1852 | supported; and recursive lists and dictionaries should not be written\n\ |
1853 | (they will cause infinite loops).\n\ |
1854 | \n\ |
1855 | Variables:\n\ |
1856 | \n\ |
1857 | version -- indicates the format that the module uses. Version 0 is the\n\ |
1858 | historical format, version 1 shares interned strings and version 2\n\ |
1859 | uses a binary format for floating point numbers.\n\ |
1860 | Version 3 shares common object references (New in version 3.4).\n\ |
1861 | \n\ |
1862 | Functions:\n\ |
1863 | \n\ |
1864 | dump() -- write value to a file\n\ |
1865 | load() -- read value from a file\n\ |
1866 | dumps() -- marshal value as a bytes object\n\ |
1867 | loads() -- read value from a bytes-like object"); |
1868 | |
1869 | |
1870 | static int |
1871 | marshal_module_exec(PyObject *mod) |
1872 | { |
1873 | if (PyModule_AddIntConstant(mod, "version", Py_MARSHAL_VERSION) < 0) { Branch (1873:9): [True: 0, False: 281]
|
1874 | return -1; |
1875 | } |
1876 | return 0; |
1877 | } |
1878 | |
1879 | static PyModuleDef_Slot marshalmodule_slots[] = { |
1880 | {Py_mod_exec, marshal_module_exec}, |
1881 | {0, NULL} |
1882 | }; |
1883 | |
1884 | static struct PyModuleDef marshalmodule = { |
1885 | PyModuleDef_HEAD_INIT, |
1886 | .m_name = "marshal", |
1887 | .m_doc = module_doc, |
1888 | .m_methods = marshal_methods, |
1889 | .m_slots = marshalmodule_slots, |
1890 | }; |
1891 | |
1892 | PyMODINIT_FUNC |
1893 | PyMarshal_Init(void) |
1894 | { |
1895 | return PyModuleDef_Init(&marshalmodule); |
1896 | } |