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