Mercurial > cpython-lru_cache-weakref
annotate Python/marshal.c @ 49955:878d40468c4e legacy-trunk
Issue #4384: Added logging integration with warnings module using captureWarnings(). This change includes a NullHandler which does nothing; it will be of use to library developers who want to avoid the "No handlers could be found for logger XXX" message which can appear if the library user doesn't configure logging.
| author | Vinay Sajip <vinay_sajip@yahoo.co.uk> |
|---|---|
| date | Wed, 03 Dec 2008 23:22:58 +0000 |
| parents | 4650eefb5838 |
| children | 4ebe6b260c55 beaf00c98b71 |
| rev | line source |
|---|---|
| 267 | 1 |
| 2 /* Write Python objects to files and read them back. | |
| 3 This is intended for writing and reading compiled Python code only; | |
| 4 a true persistent storage facility would be much harder, since | |
| 5 it would have to take circular links and sharing into account. */ | |
| 6 | |
|
36792
688795873ff1
Make Py_ssize_t clean.
Thomas Wouters <thomas@python.org>
parents:
36784
diff
changeset
|
7 #define PY_SSIZE_T_CLEAN |
|
688795873ff1
Make Py_ssize_t clean.
Thomas Wouters <thomas@python.org>
parents:
36784
diff
changeset
|
8 |
|
5373
5880384d7c71
Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
9 #include "Python.h" |
| 267 | 10 #include "longintrepr.h" |
|
35742
1c4d192f1568
Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
35346
diff
changeset
|
11 #include "code.h" |
| 267 | 12 #include "marshal.h" |
| 13 | |
|
13070
7392e71f6cce
Michael Hudson <mwh21@cam.ac.uk>:
Fred Drake <fdrake@acm.org>
parents:
12576
diff
changeset
|
14 /* High water mark to determine when the marshalled object is dangerously deep |
|
7392e71f6cce
Michael Hudson <mwh21@cam.ac.uk>:
Fred Drake <fdrake@acm.org>
parents:
12576
diff
changeset
|
15 * and risks coring the interpreter. When the object stack gets this deep, |
|
7392e71f6cce
Michael Hudson <mwh21@cam.ac.uk>:
Fred Drake <fdrake@acm.org>
parents:
12576
diff
changeset
|
16 * raise an exception instead of continuing. |
|
7392e71f6cce
Michael Hudson <mwh21@cam.ac.uk>:
Fred Drake <fdrake@acm.org>
parents:
12576
diff
changeset
|
17 */ |
|
41748
6f0dba807734
Last try for tweaking the max stack depth. 5000 was the original value,
Neal Norwitz <nnorwitz@gmail.com>
parents:
41747
diff
changeset
|
18 #define MAX_MARSHAL_STACK_DEPTH 2000 |
|
13070
7392e71f6cce
Michael Hudson <mwh21@cam.ac.uk>:
Fred Drake <fdrake@acm.org>
parents:
12576
diff
changeset
|
19 |
| 35008 | 20 #define TYPE_NULL '0' |
| 21 #define TYPE_NONE 'N' | |
| 22 #define TYPE_FALSE 'F' | |
| 23 #define TYPE_TRUE 'T' | |
| 24 #define TYPE_STOPITER 'S' | |
| 25 #define TYPE_ELLIPSIS '.' | |
| 26 #define TYPE_INT 'i' | |
| 27 #define TYPE_INT64 'I' | |
| 28 #define TYPE_FLOAT 'f' | |
| 29 #define TYPE_BINARY_FLOAT 'g' | |
| 30 #define TYPE_COMPLEX 'x' | |
| 31 #define TYPE_BINARY_COMPLEX 'y' | |
| 32 #define TYPE_LONG 'l' | |
| 33 #define TYPE_STRING 's' | |
| 34 #define TYPE_INTERNED 't' | |
| 35 #define TYPE_STRINGREF 'R' | |
| 36 #define TYPE_TUPLE '(' | |
| 37 #define TYPE_LIST '[' | |
| 38 #define TYPE_DICT '{' | |
| 39 #define TYPE_CODE 'c' | |
| 40 #define TYPE_UNICODE 'u' | |
| 41 #define TYPE_UNKNOWN '?' | |
| 42 #define TYPE_SET '<' | |
| 43 #define TYPE_FROZENSET '>' | |
| 267 | 44 |
|
1191
38f0054f44a7
Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
45 typedef struct { |
|
38f0054f44a7
Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
46 FILE *fp; |
|
3731
de1cb33219e3
Jeremy's patches for more robust handling of unmarshallable types.
Guido van Rossum <guido@python.org>
parents:
3620
diff
changeset
|
47 int error; |
|
13070
7392e71f6cce
Michael Hudson <mwh21@cam.ac.uk>:
Fred Drake <fdrake@acm.org>
parents:
12576
diff
changeset
|
48 int depth; |
|
1191
38f0054f44a7
Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
49 /* If fp == NULL, the following are valid: */ |
|
5373
5880384d7c71
Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
50 PyObject *str; |
|
1191
38f0054f44a7
Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
51 char *ptr; |
|
38f0054f44a7
Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
52 char *end; |
|
32422
3df36e2e5ddc
Patch #923098: Share interned strings in marshal.
Martin v. Löwis <martin@v.loewis.de>
parents:
32343
diff
changeset
|
53 PyObject *strings; /* dict on marshal, list on unmarshal */ |
| 35008 | 54 int version; |
|
1191
38f0054f44a7
Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
55 } WFILE; |
|
38f0054f44a7
Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
56 |
|
38f0054f44a7
Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
57 #define w_byte(c, p) if (((p)->fp)) putc((c), (p)->fp); \ |
|
38f0054f44a7
Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
58 else if ((p)->ptr != (p)->end) *(p)->ptr++ = (c); \ |
|
38f0054f44a7
Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
59 else w_more(c, p) |
| 267 | 60 |
|
1191
38f0054f44a7
Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
61 static void |
|
13672
bfc5b2a8c11f
-- changed w_more to take an integer instead of a char
Fredrik Lundh <fredrik@pythonware.com>
parents:
13658
diff
changeset
|
62 w_more(int c, WFILE *p) |
|
1191
38f0054f44a7
Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
63 { |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36324
diff
changeset
|
64 Py_ssize_t size, newsize; |
|
1191
38f0054f44a7
Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
65 if (p->str == NULL) |
|
38f0054f44a7
Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
66 return; /* An error already occurred */ |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47294
diff
changeset
|
67 size = PyString_Size(p->str); |
|
46880
4fb6c0c169ff
#1792: Improve performance of marshal.dumps() on large objects by increasing
Andrew M. Kuchling <amk@amk.ca>
parents:
45081
diff
changeset
|
68 newsize = size + size + 1024; |
|
4fb6c0c169ff
#1792: Improve performance of marshal.dumps() on large objects by increasing
Andrew M. Kuchling <amk@amk.ca>
parents:
45081
diff
changeset
|
69 if (newsize > 32*1024*1024) { |
|
47730
4650eefb5838
Issue 3116: fix quadratic behavior in marshal.dumps().
Raymond Hettinger <python@rcn.com>
parents:
47527
diff
changeset
|
70 newsize = size + (size >> 3); /* 12.5% overallocation */ |
|
46880
4fb6c0c169ff
#1792: Improve performance of marshal.dumps() on large objects by increasing
Andrew M. Kuchling <amk@amk.ca>
parents:
45081
diff
changeset
|
71 } |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47294
diff
changeset
|
72 if (_PyString_Resize(&p->str, newsize) != 0) { |
|
1191
38f0054f44a7
Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
73 p->ptr = p->end = NULL; |
|
38f0054f44a7
Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
74 } |
|
38f0054f44a7
Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
75 else { |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47294
diff
changeset
|
76 p->ptr = PyString_AS_STRING((PyStringObject *)p->str) + size; |
|
5373
5880384d7c71
Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
77 p->end = |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47294
diff
changeset
|
78 PyString_AS_STRING((PyStringObject *)p->str) + newsize; |
|
13674
c90d94d287d7
Included assert.h in Python.h -- it's absurd that this basic tool of
Tim Peters <tim.peters@gmail.com>
parents:
13672
diff
changeset
|
79 *p->ptr++ = Py_SAFE_DOWNCAST(c, int, char); |
|
1191
38f0054f44a7
Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
80 } |
|
38f0054f44a7
Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
81 } |
|
38f0054f44a7
Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
82 |
|
38f0054f44a7
Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
83 static void |
|
13658
f79434ee41fa
Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents:
13447
diff
changeset
|
84 w_string(char *s, int n, WFILE *p) |
|
1191
38f0054f44a7
Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
85 { |
|
38f0054f44a7
Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
86 if (p->fp != NULL) { |
|
38f0054f44a7
Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
87 fwrite(s, 1, n, p->fp); |
|
38f0054f44a7
Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
88 } |
|
38f0054f44a7
Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
89 else { |
|
38f0054f44a7
Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
90 while (--n >= 0) { |
|
38f0054f44a7
Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
91 w_byte(*s, p); |
|
38f0054f44a7
Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
92 s++; |
|
38f0054f44a7
Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
93 } |
|
38f0054f44a7
Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
94 } |
|
38f0054f44a7
Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
95 } |
|
38f0054f44a7
Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
96 |
|
38f0054f44a7
Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
97 static void |
|
13658
f79434ee41fa
Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents:
13447
diff
changeset
|
98 w_short(int x, WFILE *p) |
|
1191
38f0054f44a7
Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
99 { |
|
24434
9ef82377669d
Fix SF 588452: debug build crashes on marshal.dumps([128] * 1000).
Thomas Heller <theller@ctypes.org>
parents:
23851
diff
changeset
|
100 w_byte((char)( x & 0xff), p); |
|
9ef82377669d
Fix SF 588452: debug build crashes on marshal.dumps([128] * 1000).
Thomas Heller <theller@ctypes.org>
parents:
23851
diff
changeset
|
101 w_byte((char)((x>> 8) & 0xff), p); |
|
1191
38f0054f44a7
Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
102 } |
|
38f0054f44a7
Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
103 |
|
38f0054f44a7
Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
104 static void |
|
13658
f79434ee41fa
Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents:
13447
diff
changeset
|
105 w_long(long x, WFILE *p) |
|
1191
38f0054f44a7
Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
106 { |
|
24435
7dc3f4077e59
Whitespace normalization.
Thomas Heller <theller@ctypes.org>
parents:
24434
diff
changeset
|
107 w_byte((char)( x & 0xff), p); |
|
7dc3f4077e59
Whitespace normalization.
Thomas Heller <theller@ctypes.org>
parents:
24434
diff
changeset
|
108 w_byte((char)((x>> 8) & 0xff), p); |
|
7dc3f4077e59
Whitespace normalization.
Thomas Heller <theller@ctypes.org>
parents:
24434
diff
changeset
|
109 w_byte((char)((x>>16) & 0xff), p); |
|
7dc3f4077e59
Whitespace normalization.
Thomas Heller <theller@ctypes.org>
parents:
24434
diff
changeset
|
110 w_byte((char)((x>>24) & 0xff), p); |
|
1191
38f0054f44a7
Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
111 } |
|
38f0054f44a7
Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
112 |
|
4656
186c2ad4abf3
Better way to handle 64-bit ints, keeping gcc -Wall happy.
Guido van Rossum <guido@python.org>
parents:
4592
diff
changeset
|
113 #if SIZEOF_LONG > 4 |
|
1191
38f0054f44a7
Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
114 static void |
|
13658
f79434ee41fa
Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents:
13447
diff
changeset
|
115 w_long64(long x, WFILE *p) |
|
4592
3bf9e2beaf8c
Tentative changes to make this work better on 64-bit machines.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
116 { |
|
3bf9e2beaf8c
Tentative changes to make this work better on 64-bit machines.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
117 w_long(x, p); |
|
4656
186c2ad4abf3
Better way to handle 64-bit ints, keeping gcc -Wall happy.
Guido van Rossum <guido@python.org>
parents:
4592
diff
changeset
|
118 w_long(x>>32, p); |
|
4592
3bf9e2beaf8c
Tentative changes to make this work better on 64-bit machines.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
119 } |
|
4656
186c2ad4abf3
Better way to handle 64-bit ints, keeping gcc -Wall happy.
Guido van Rossum <guido@python.org>
parents:
4592
diff
changeset
|
120 #endif |
|
4592
3bf9e2beaf8c
Tentative changes to make this work better on 64-bit machines.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
121 |
|
3bf9e2beaf8c
Tentative changes to make this work better on 64-bit machines.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
122 static void |
|
13658
f79434ee41fa
Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents:
13447
diff
changeset
|
123 w_object(PyObject *v, WFILE *p) |
| 267 | 124 { |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36324
diff
changeset
|
125 Py_ssize_t i, n; |
|
13070
7392e71f6cce
Michael Hudson <mwh21@cam.ac.uk>:
Fred Drake <fdrake@acm.org>
parents:
12576
diff
changeset
|
126 |
|
7392e71f6cce
Michael Hudson <mwh21@cam.ac.uk>:
Fred Drake <fdrake@acm.org>
parents:
12576
diff
changeset
|
127 p->depth++; |
|
16311
8c41f3861aad
It's unclear whether PyMarshal_XXX() are part of the public or private API.
Tim Peters <tim.peters@gmail.com>
parents:
16257
diff
changeset
|
128 |
|
13070
7392e71f6cce
Michael Hudson <mwh21@cam.ac.uk>:
Fred Drake <fdrake@acm.org>
parents:
12576
diff
changeset
|
129 if (p->depth > MAX_MARSHAL_STACK_DEPTH) { |
|
7392e71f6cce
Michael Hudson <mwh21@cam.ac.uk>:
Fred Drake <fdrake@acm.org>
parents:
12576
diff
changeset
|
130 p->error = 2; |
|
16311
8c41f3861aad
It's unclear whether PyMarshal_XXX() are part of the public or private API.
Tim Peters <tim.peters@gmail.com>
parents:
16257
diff
changeset
|
131 } |
|
13070
7392e71f6cce
Michael Hudson <mwh21@cam.ac.uk>:
Fred Drake <fdrake@acm.org>
parents:
12576
diff
changeset
|
132 else if (v == NULL) { |
|
1191
38f0054f44a7
Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
133 w_byte(TYPE_NULL, p); |
|
7982
a909a2869ac5
Make new gcc -Wall happy
Guido van Rossum <guido@python.org>
parents:
5905
diff
changeset
|
134 } |
|
a909a2869ac5
Make new gcc -Wall happy
Guido van Rossum <guido@python.org>
parents:
5905
diff
changeset
|
135 else if (v == Py_None) { |
|
1191
38f0054f44a7
Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
136 w_byte(TYPE_NONE, p); |
|
7982
a909a2869ac5
Make new gcc -Wall happy
Guido van Rossum <guido@python.org>
parents:
5905
diff
changeset
|
137 } |
|
18148
7bc473b2c654
Merging the gen-branch into the main line, at Guido's direction. Yay!
Tim Peters <tim.peters@gmail.com>
parents:
17707
diff
changeset
|
138 else if (v == PyExc_StopIteration) { |
|
7bc473b2c654
Merging the gen-branch into the main line, at Guido's direction. Yay!
Tim Peters <tim.peters@gmail.com>
parents:
17707
diff
changeset
|
139 w_byte(TYPE_STOPITER, p); |
|
7bc473b2c654
Merging the gen-branch into the main line, at Guido's direction. Yay!
Tim Peters <tim.peters@gmail.com>
parents:
17707
diff
changeset
|
140 } |
|
7982
a909a2869ac5
Make new gcc -Wall happy
Guido van Rossum <guido@python.org>
parents:
5905
diff
changeset
|
141 else if (v == Py_Ellipsis) { |
|
a909a2869ac5
Make new gcc -Wall happy
Guido van Rossum <guido@python.org>
parents:
5905
diff
changeset
|
142 w_byte(TYPE_ELLIPSIS, p); |
|
a909a2869ac5
Make new gcc -Wall happy
Guido van Rossum <guido@python.org>
parents:
5905
diff
changeset
|
143 } |
|
22785
aa8d59823add
Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents:
22705
diff
changeset
|
144 else if (v == Py_False) { |
|
aa8d59823add
Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents:
22705
diff
changeset
|
145 w_byte(TYPE_FALSE, p); |
|
aa8d59823add
Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents:
22705
diff
changeset
|
146 } |
|
aa8d59823add
Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents:
22705
diff
changeset
|
147 else if (v == Py_True) { |
|
aa8d59823add
Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents:
22705
diff
changeset
|
148 w_byte(TYPE_TRUE, p); |
|
aa8d59823add
Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents:
22705
diff
changeset
|
149 } |
|
43781
da58cf9bcb01
Fix marshal's incorrect handling of subclasses of builtin types (backport candidate).
Raymond Hettinger <python@rcn.com>
parents:
43536
diff
changeset
|
150 else if (PyInt_CheckExact(v)) { |
|
5373
5880384d7c71
Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
151 long x = PyInt_AS_LONG((PyIntObject *)v); |
|
4656
186c2ad4abf3
Better way to handle 64-bit ints, keeping gcc -Wall happy.
Guido van Rossum <guido@python.org>
parents:
4592
diff
changeset
|
152 #if SIZEOF_LONG > 4 |
|
17368
bb75d93f6a31
test_pickle works on sizeof(long)==8 boxes again.
Tim Peters <tim.peters@gmail.com>
parents:
16311
diff
changeset
|
153 long y = Py_ARITHMETIC_RIGHT_SHIFT(long, x, 31); |
|
4592
3bf9e2beaf8c
Tentative changes to make this work better on 64-bit machines.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
154 if (y && y != -1) { |
|
3bf9e2beaf8c
Tentative changes to make this work better on 64-bit machines.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
155 w_byte(TYPE_INT64, p); |
|
3bf9e2beaf8c
Tentative changes to make this work better on 64-bit machines.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
156 w_long64(x, p); |
|
3bf9e2beaf8c
Tentative changes to make this work better on 64-bit machines.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
157 } |
|
4656
186c2ad4abf3
Better way to handle 64-bit ints, keeping gcc -Wall happy.
Guido van Rossum <guido@python.org>
parents:
4592
diff
changeset
|
158 else |
|
186c2ad4abf3
Better way to handle 64-bit ints, keeping gcc -Wall happy.
Guido van Rossum <guido@python.org>
parents:
4592
diff
changeset
|
159 #endif |
|
186c2ad4abf3
Better way to handle 64-bit ints, keeping gcc -Wall happy.
Guido van Rossum <guido@python.org>
parents:
4592
diff
changeset
|
160 { |
|
4592
3bf9e2beaf8c
Tentative changes to make this work better on 64-bit machines.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
161 w_byte(TYPE_INT, p); |
|
3bf9e2beaf8c
Tentative changes to make this work better on 64-bit machines.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
162 w_long(x, p); |
|
3bf9e2beaf8c
Tentative changes to make this work better on 64-bit machines.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
163 } |
|
1191
38f0054f44a7
Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
164 } |
|
43781
da58cf9bcb01
Fix marshal's incorrect handling of subclasses of builtin types (backport candidate).
Raymond Hettinger <python@rcn.com>
parents:
43536
diff
changeset
|
165 else if (PyLong_CheckExact(v)) { |
|
5373
5880384d7c71
Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
166 PyLongObject *ob = (PyLongObject *)v; |
|
1191
38f0054f44a7
Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
167 w_byte(TYPE_LONG, p); |
|
38f0054f44a7
Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
168 n = ob->ob_size; |
|
38f0054f44a7
Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
169 w_long((long)n, p); |
|
38f0054f44a7
Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
170 if (n < 0) |
|
38f0054f44a7
Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
171 n = -n; |
|
38f0054f44a7
Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
172 for (i = 0; i < n; i++) |
|
38f0054f44a7
Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
173 w_short(ob->ob_digit[i], p); |
|
38f0054f44a7
Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
174 } |
|
43781
da58cf9bcb01
Fix marshal's incorrect handling of subclasses of builtin types (backport candidate).
Raymond Hettinger <python@rcn.com>
parents:
43536
diff
changeset
|
175 else if (PyFloat_CheckExact(v)) { |
| 35008 | 176 if (p->version > 1) { |
|
35126
70681267a74a
Fix signedness of various char variables to stop causing a warning under gcc 4.
Brett Cannon <bcannon@gmail.com>
parents:
35071
diff
changeset
|
177 unsigned char buf[8]; |
| 35008 | 178 if (_PyFloat_Pack8(PyFloat_AsDouble(v), |
| 179 buf, 1) < 0) { | |
| 180 p->error = 1; | |
| 181 return; | |
| 182 } | |
| 183 w_byte(TYPE_BINARY_FLOAT, p); | |
|
35126
70681267a74a
Fix signedness of various char variables to stop causing a warning under gcc 4.
Brett Cannon <bcannon@gmail.com>
parents:
35071
diff
changeset
|
184 w_string((char*)buf, 8, p); |
| 35008 | 185 } |
| 186 else { | |
| 187 char buf[256]; /* Plenty to format any double */ | |
| 188 PyFloat_AsReprString(buf, (PyFloatObject *)v); | |
|
36522
2791676b3825
Move cast to suppress warning.
Martin v. Löwis <martin@v.loewis.de>
parents:
36501
diff
changeset
|
189 n = strlen(buf); |
| 35008 | 190 w_byte(TYPE_FLOAT, p); |
|
36522
2791676b3825
Move cast to suppress warning.
Martin v. Löwis <martin@v.loewis.de>
parents:
36501
diff
changeset
|
191 w_byte((int)n, p); |
|
36866
9eb24f92a784
Change int to Py_ssize_t in several places.
Martin v. Löwis <martin@v.loewis.de>
parents:
36792
diff
changeset
|
192 w_string(buf, (int)n, p); |
| 35008 | 193 } |
|
1191
38f0054f44a7
Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
194 } |
|
3323
6098aaa8ae7f
changes for complex numbers
Guido van Rossum <guido@python.org>
parents:
3234
diff
changeset
|
195 #ifndef WITHOUT_COMPLEX |
|
43781
da58cf9bcb01
Fix marshal's incorrect handling of subclasses of builtin types (backport candidate).
Raymond Hettinger <python@rcn.com>
parents:
43536
diff
changeset
|
196 else if (PyComplex_CheckExact(v)) { |
| 35008 | 197 if (p->version > 1) { |
|
35126
70681267a74a
Fix signedness of various char variables to stop causing a warning under gcc 4.
Brett Cannon <bcannon@gmail.com>
parents:
35071
diff
changeset
|
198 unsigned char buf[8]; |
| 35008 | 199 if (_PyFloat_Pack8(PyComplex_RealAsDouble(v), |
| 200 buf, 1) < 0) { | |
| 201 p->error = 1; | |
| 202 return; | |
| 203 } | |
| 204 w_byte(TYPE_BINARY_COMPLEX, p); | |
|
35126
70681267a74a
Fix signedness of various char variables to stop causing a warning under gcc 4.
Brett Cannon <bcannon@gmail.com>
parents:
35071
diff
changeset
|
205 w_string((char*)buf, 8, p); |
| 35008 | 206 if (_PyFloat_Pack8(PyComplex_ImagAsDouble(v), |
| 207 buf, 1) < 0) { | |
| 208 p->error = 1; | |
| 209 return; | |
| 210 } | |
|
35126
70681267a74a
Fix signedness of various char variables to stop causing a warning under gcc 4.
Brett Cannon <bcannon@gmail.com>
parents:
35071
diff
changeset
|
211 w_string((char*)buf, 8, p); |
| 35008 | 212 } |
| 213 else { | |
| 214 char buf[256]; /* Plenty to format any double */ | |
| 215 PyFloatObject *temp; | |
| 216 w_byte(TYPE_COMPLEX, p); | |
| 217 temp = (PyFloatObject*)PyFloat_FromDouble( | |
| 218 PyComplex_RealAsDouble(v)); | |
|
39448
2179d9f14278
This code is actually not used unless WITHOUT_COMPLEX is defined.
Neal Norwitz <nnorwitz@gmail.com>
parents:
38490
diff
changeset
|
219 if (!temp) { |
|
2179d9f14278
This code is actually not used unless WITHOUT_COMPLEX is defined.
Neal Norwitz <nnorwitz@gmail.com>
parents:
38490
diff
changeset
|
220 p->error = 1; |
|
2179d9f14278
This code is actually not used unless WITHOUT_COMPLEX is defined.
Neal Norwitz <nnorwitz@gmail.com>
parents:
38490
diff
changeset
|
221 return; |
|
2179d9f14278
This code is actually not used unless WITHOUT_COMPLEX is defined.
Neal Norwitz <nnorwitz@gmail.com>
parents:
38490
diff
changeset
|
222 } |
| 35008 | 223 PyFloat_AsReprString(buf, temp); |
| 224 Py_DECREF(temp); | |
|
36866
9eb24f92a784
Change int to Py_ssize_t in several places.
Martin v. Löwis <martin@v.loewis.de>
parents:
36792
diff
changeset
|
225 n = strlen(buf); |
|
9eb24f92a784
Change int to Py_ssize_t in several places.
Martin v. Löwis <martin@v.loewis.de>
parents:
36792
diff
changeset
|
226 w_byte((int)n, p); |
|
9eb24f92a784
Change int to Py_ssize_t in several places.
Martin v. Löwis <martin@v.loewis.de>
parents:
36792
diff
changeset
|
227 w_string(buf, (int)n, p); |
| 35008 | 228 temp = (PyFloatObject*)PyFloat_FromDouble( |
| 229 PyComplex_ImagAsDouble(v)); | |
|
39448
2179d9f14278
This code is actually not used unless WITHOUT_COMPLEX is defined.
Neal Norwitz <nnorwitz@gmail.com>
parents:
38490
diff
changeset
|
230 if (!temp) { |
|
2179d9f14278
This code is actually not used unless WITHOUT_COMPLEX is defined.
Neal Norwitz <nnorwitz@gmail.com>
parents:
38490
diff
changeset
|
231 p->error = 1; |
|
2179d9f14278
This code is actually not used unless WITHOUT_COMPLEX is defined.
Neal Norwitz <nnorwitz@gmail.com>
parents:
38490
diff
changeset
|
232 return; |
|
2179d9f14278
This code is actually not used unless WITHOUT_COMPLEX is defined.
Neal Norwitz <nnorwitz@gmail.com>
parents:
38490
diff
changeset
|
233 } |
| 35008 | 234 PyFloat_AsReprString(buf, temp); |
| 235 Py_DECREF(temp); | |
|
36866
9eb24f92a784
Change int to Py_ssize_t in several places.
Martin v. Löwis <martin@v.loewis.de>
parents:
36792
diff
changeset
|
236 n = strlen(buf); |
|
9eb24f92a784
Change int to Py_ssize_t in several places.
Martin v. Löwis <martin@v.loewis.de>
parents:
36792
diff
changeset
|
237 w_byte((int)n, p); |
|
9eb24f92a784
Change int to Py_ssize_t in several places.
Martin v. Löwis <martin@v.loewis.de>
parents:
36792
diff
changeset
|
238 w_string(buf, (int)n, p); |
| 35008 | 239 } |
|
3323
6098aaa8ae7f
changes for complex numbers
Guido van Rossum <guido@python.org>
parents:
3234
diff
changeset
|
240 } |
|
6098aaa8ae7f
changes for complex numbers
Guido van Rossum <guido@python.org>
parents:
3234
diff
changeset
|
241 #endif |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47294
diff
changeset
|
242 else if (PyString_CheckExact(v)) { |
|
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47294
diff
changeset
|
243 if (p->strings && PyString_CHECK_INTERNED(v)) { |
|
32422
3df36e2e5ddc
Patch #923098: Share interned strings in marshal.
Martin v. Löwis <martin@v.loewis.de>
parents:
32343
diff
changeset
|
244 PyObject *o = PyDict_GetItem(p->strings, v); |
|
3df36e2e5ddc
Patch #923098: Share interned strings in marshal.
Martin v. Löwis <martin@v.loewis.de>
parents:
32343
diff
changeset
|
245 if (o) { |
|
3df36e2e5ddc
Patch #923098: Share interned strings in marshal.
Martin v. Löwis <martin@v.loewis.de>
parents:
32343
diff
changeset
|
246 long w = PyInt_AsLong(o); |
|
3df36e2e5ddc
Patch #923098: Share interned strings in marshal.
Martin v. Löwis <martin@v.loewis.de>
parents:
32343
diff
changeset
|
247 w_byte(TYPE_STRINGREF, p); |
|
3df36e2e5ddc
Patch #923098: Share interned strings in marshal.
Martin v. Löwis <martin@v.loewis.de>
parents:
32343
diff
changeset
|
248 w_long(w, p); |
|
3df36e2e5ddc
Patch #923098: Share interned strings in marshal.
Martin v. Löwis <martin@v.loewis.de>
parents:
32343
diff
changeset
|
249 goto exit; |
|
3df36e2e5ddc
Patch #923098: Share interned strings in marshal.
Martin v. Löwis <martin@v.loewis.de>
parents:
32343
diff
changeset
|
250 } |
|
3df36e2e5ddc
Patch #923098: Share interned strings in marshal.
Martin v. Löwis <martin@v.loewis.de>
parents:
32343
diff
changeset
|
251 else { |
|
41741
6f1293ab3ac4
Fix bug in marshal where bad data would cause a segfault due to
Neal Norwitz <nnorwitz@gmail.com>
parents:
39995
diff
changeset
|
252 int ok; |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36324
diff
changeset
|
253 o = PyInt_FromSsize_t(PyDict_Size(p->strings)); |
|
41741
6f1293ab3ac4
Fix bug in marshal where bad data would cause a segfault due to
Neal Norwitz <nnorwitz@gmail.com>
parents:
39995
diff
changeset
|
254 ok = o && |
|
6f1293ab3ac4
Fix bug in marshal where bad data would cause a segfault due to
Neal Norwitz <nnorwitz@gmail.com>
parents:
39995
diff
changeset
|
255 PyDict_SetItem(p->strings, v, o) >= 0; |
|
6f1293ab3ac4
Fix bug in marshal where bad data would cause a segfault due to
Neal Norwitz <nnorwitz@gmail.com>
parents:
39995
diff
changeset
|
256 Py_XDECREF(o); |
|
6f1293ab3ac4
Fix bug in marshal where bad data would cause a segfault due to
Neal Norwitz <nnorwitz@gmail.com>
parents:
39995
diff
changeset
|
257 if (!ok) { |
|
6f1293ab3ac4
Fix bug in marshal where bad data would cause a segfault due to
Neal Norwitz <nnorwitz@gmail.com>
parents:
39995
diff
changeset
|
258 p->depth--; |
|
6f1293ab3ac4
Fix bug in marshal where bad data would cause a segfault due to
Neal Norwitz <nnorwitz@gmail.com>
parents:
39995
diff
changeset
|
259 p->error = 1; |
|
6f1293ab3ac4
Fix bug in marshal where bad data would cause a segfault due to
Neal Norwitz <nnorwitz@gmail.com>
parents:
39995
diff
changeset
|
260 return; |
|
6f1293ab3ac4
Fix bug in marshal where bad data would cause a segfault due to
Neal Norwitz <nnorwitz@gmail.com>
parents:
39995
diff
changeset
|
261 } |
|
32422
3df36e2e5ddc
Patch #923098: Share interned strings in marshal.
Martin v. Löwis <martin@v.loewis.de>
parents:
32343
diff
changeset
|
262 w_byte(TYPE_INTERNED, p); |
|
3df36e2e5ddc
Patch #923098: Share interned strings in marshal.
Martin v. Löwis <martin@v.loewis.de>
parents:
32343
diff
changeset
|
263 } |
|
3df36e2e5ddc
Patch #923098: Share interned strings in marshal.
Martin v. Löwis <martin@v.loewis.de>
parents:
32343
diff
changeset
|
264 } |
|
3df36e2e5ddc
Patch #923098: Share interned strings in marshal.
Martin v. Löwis <martin@v.loewis.de>
parents:
32343
diff
changeset
|
265 else { |
|
3df36e2e5ddc
Patch #923098: Share interned strings in marshal.
Martin v. Löwis <martin@v.loewis.de>
parents:
32343
diff
changeset
|
266 w_byte(TYPE_STRING, p); |
|
3df36e2e5ddc
Patch #923098: Share interned strings in marshal.
Martin v. Löwis <martin@v.loewis.de>
parents:
32343
diff
changeset
|
267 } |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47294
diff
changeset
|
268 n = PyString_GET_SIZE(v); |
|
36866
9eb24f92a784
Change int to Py_ssize_t in several places.
Martin v. Löwis <martin@v.loewis.de>
parents:
36792
diff
changeset
|
269 if (n > INT_MAX) { |
|
9eb24f92a784
Change int to Py_ssize_t in several places.
Martin v. Löwis <martin@v.loewis.de>
parents:
36792
diff
changeset
|
270 /* huge strings are not supported */ |
|
9eb24f92a784
Change int to Py_ssize_t in several places.
Martin v. Löwis <martin@v.loewis.de>
parents:
36792
diff
changeset
|
271 p->depth--; |
|
9eb24f92a784
Change int to Py_ssize_t in several places.
Martin v. Löwis <martin@v.loewis.de>
parents:
36792
diff
changeset
|
272 p->error = 1; |
|
9eb24f92a784
Change int to Py_ssize_t in several places.
Martin v. Löwis <martin@v.loewis.de>
parents:
36792
diff
changeset
|
273 return; |
|
9eb24f92a784
Change int to Py_ssize_t in several places.
Martin v. Löwis <martin@v.loewis.de>
parents:
36792
diff
changeset
|
274 } |
|
2368
663aa059e29f
hope to fix DEC Alpha gcc problems by declaring ints instead of longs
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
275 w_long((long)n, p); |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47294
diff
changeset
|
276 w_string(PyString_AS_STRING(v), (int)n, p); |
|
11976
513354d87a85
Marc-Andre Lemburg: support marshalling Unicode objects (code 'u').
Guido van Rossum <guido@python.org>
parents:
9300
diff
changeset
|
277 } |
|
19343
7257e87e0720
Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents:
18148
diff
changeset
|
278 #ifdef Py_USING_UNICODE |
|
43781
da58cf9bcb01
Fix marshal's incorrect handling of subclasses of builtin types (backport candidate).
Raymond Hettinger <python@rcn.com>
parents:
43536
diff
changeset
|
279 else if (PyUnicode_CheckExact(v)) { |
|
11976
513354d87a85
Marc-Andre Lemburg: support marshalling Unicode objects (code 'u').
Guido van Rossum <guido@python.org>
parents:
9300
diff
changeset
|
280 PyObject *utf8; |
|
513354d87a85
Marc-Andre Lemburg: support marshalling Unicode objects (code 'u').
Guido van Rossum <guido@python.org>
parents:
9300
diff
changeset
|
281 utf8 = PyUnicode_AsUTF8String(v); |
|
513354d87a85
Marc-Andre Lemburg: support marshalling Unicode objects (code 'u').
Guido van Rossum <guido@python.org>
parents:
9300
diff
changeset
|
282 if (utf8 == NULL) { |
|
13106
a6725acdf33b
Urmpf. Quality control on this patch lapsed a bit. :-(
Guido van Rossum <guido@python.org>
parents:
13070
diff
changeset
|
283 p->depth--; |
|
a6725acdf33b
Urmpf. Quality control on this patch lapsed a bit. :-(
Guido van Rossum <guido@python.org>
parents:
13070
diff
changeset
|
284 p->error = 1; |
|
a6725acdf33b
Urmpf. Quality control on this patch lapsed a bit. :-(
Guido van Rossum <guido@python.org>
parents:
13070
diff
changeset
|
285 return; |
|
11976
513354d87a85
Marc-Andre Lemburg: support marshalling Unicode objects (code 'u').
Guido van Rossum <guido@python.org>
parents:
9300
diff
changeset
|
286 } |
|
513354d87a85
Marc-Andre Lemburg: support marshalling Unicode objects (code 'u').
Guido van Rossum <guido@python.org>
parents:
9300
diff
changeset
|
287 w_byte(TYPE_UNICODE, p); |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47294
diff
changeset
|
288 n = PyString_GET_SIZE(utf8); |
|
36866
9eb24f92a784
Change int to Py_ssize_t in several places.
Martin v. Löwis <martin@v.loewis.de>
parents:
36792
diff
changeset
|
289 if (n > INT_MAX) { |
|
9eb24f92a784
Change int to Py_ssize_t in several places.
Martin v. Löwis <martin@v.loewis.de>
parents:
36792
diff
changeset
|
290 p->depth--; |
|
9eb24f92a784
Change int to Py_ssize_t in several places.
Martin v. Löwis <martin@v.loewis.de>
parents:
36792
diff
changeset
|
291 p->error = 1; |
|
9eb24f92a784
Change int to Py_ssize_t in several places.
Martin v. Löwis <martin@v.loewis.de>
parents:
36792
diff
changeset
|
292 return; |
|
9eb24f92a784
Change int to Py_ssize_t in several places.
Martin v. Löwis <martin@v.loewis.de>
parents:
36792
diff
changeset
|
293 } |
|
11976
513354d87a85
Marc-Andre Lemburg: support marshalling Unicode objects (code 'u').
Guido van Rossum <guido@python.org>
parents:
9300
diff
changeset
|
294 w_long((long)n, p); |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47294
diff
changeset
|
295 w_string(PyString_AS_STRING(utf8), (int)n, p); |
|
11976
513354d87a85
Marc-Andre Lemburg: support marshalling Unicode objects (code 'u').
Guido van Rossum <guido@python.org>
parents:
9300
diff
changeset
|
296 Py_DECREF(utf8); |
|
1191
38f0054f44a7
Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
297 } |
|
19343
7257e87e0720
Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents:
18148
diff
changeset
|
298 #endif |
|
43781
da58cf9bcb01
Fix marshal's incorrect handling of subclasses of builtin types (backport candidate).
Raymond Hettinger <python@rcn.com>
parents:
43536
diff
changeset
|
299 else if (PyTuple_CheckExact(v)) { |
|
1191
38f0054f44a7
Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
300 w_byte(TYPE_TUPLE, p); |
|
5373
5880384d7c71
Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
301 n = PyTuple_Size(v); |
|
2368
663aa059e29f
hope to fix DEC Alpha gcc problems by declaring ints instead of longs
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
302 w_long((long)n, p); |
|
1191
38f0054f44a7
Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
303 for (i = 0; i < n; i++) { |
|
5373
5880384d7c71
Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
304 w_object(PyTuple_GET_ITEM(v, i), p); |
|
1191
38f0054f44a7
Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
305 } |
|
38f0054f44a7
Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
306 } |
|
43781
da58cf9bcb01
Fix marshal's incorrect handling of subclasses of builtin types (backport candidate).
Raymond Hettinger <python@rcn.com>
parents:
43536
diff
changeset
|
307 else if (PyList_CheckExact(v)) { |
|
1191
38f0054f44a7
Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
308 w_byte(TYPE_LIST, p); |
|
11976
513354d87a85
Marc-Andre Lemburg: support marshalling Unicode objects (code 'u').
Guido van Rossum <guido@python.org>
parents:
9300
diff
changeset
|
309 n = PyList_GET_SIZE(v); |
|
2368
663aa059e29f
hope to fix DEC Alpha gcc problems by declaring ints instead of longs
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
310 w_long((long)n, p); |
|
1191
38f0054f44a7
Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
311 for (i = 0; i < n; i++) { |
|
11976
513354d87a85
Marc-Andre Lemburg: support marshalling Unicode objects (code 'u').
Guido van Rossum <guido@python.org>
parents:
9300
diff
changeset
|
312 w_object(PyList_GET_ITEM(v, i), p); |
|
1191
38f0054f44a7
Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
313 } |
|
38f0054f44a7
Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
314 } |
|
43781
da58cf9bcb01
Fix marshal's incorrect handling of subclasses of builtin types (backport candidate).
Raymond Hettinger <python@rcn.com>
parents:
43536
diff
changeset
|
315 else if (PyDict_CheckExact(v)) { |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36324
diff
changeset
|
316 Py_ssize_t pos; |
|
5373
5880384d7c71
Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
317 PyObject *key, *value; |
|
1191
38f0054f44a7
Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
318 w_byte(TYPE_DICT, p); |
|
38f0054f44a7
Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
319 /* This one is NULL object terminated! */ |
|
1288
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1270
diff
changeset
|
320 pos = 0; |
|
5373
5880384d7c71
Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
321 while (PyDict_Next(v, &pos, &key, &value)) { |
|
1288
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1270
diff
changeset
|
322 w_object(key, p); |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1270
diff
changeset
|
323 w_object(value, p); |
|
1191
38f0054f44a7
Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
324 } |
|
5373
5880384d7c71
Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
325 w_object((PyObject *)NULL, p); |
|
1191
38f0054f44a7
Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
326 } |
|
43781
da58cf9bcb01
Fix marshal's incorrect handling of subclasses of builtin types (backport candidate).
Raymond Hettinger <python@rcn.com>
parents:
43536
diff
changeset
|
327 else if (PyAnySet_CheckExact(v)) { |
|
34394
b480abaf1f6f
SF 1098985: set objects cannot be marshalled
Raymond Hettinger <python@rcn.com>
parents:
34205
diff
changeset
|
328 PyObject *value, *it; |
|
b480abaf1f6f
SF 1098985: set objects cannot be marshalled
Raymond Hettinger <python@rcn.com>
parents:
34205
diff
changeset
|
329 |
|
b480abaf1f6f
SF 1098985: set objects cannot be marshalled
Raymond Hettinger <python@rcn.com>
parents:
34205
diff
changeset
|
330 if (PyObject_TypeCheck(v, &PySet_Type)) |
|
b480abaf1f6f
SF 1098985: set objects cannot be marshalled
Raymond Hettinger <python@rcn.com>
parents:
34205
diff
changeset
|
331 w_byte(TYPE_SET, p); |
|
b480abaf1f6f
SF 1098985: set objects cannot be marshalled
Raymond Hettinger <python@rcn.com>
parents:
34205
diff
changeset
|
332 else |
|
b480abaf1f6f
SF 1098985: set objects cannot be marshalled
Raymond Hettinger <python@rcn.com>
parents:
34205
diff
changeset
|
333 w_byte(TYPE_FROZENSET, p); |
|
b480abaf1f6f
SF 1098985: set objects cannot be marshalled
Raymond Hettinger <python@rcn.com>
parents:
34205
diff
changeset
|
334 n = PyObject_Size(v); |
|
b480abaf1f6f
SF 1098985: set objects cannot be marshalled
Raymond Hettinger <python@rcn.com>
parents:
34205
diff
changeset
|
335 if (n == -1) { |
|
b480abaf1f6f
SF 1098985: set objects cannot be marshalled
Raymond Hettinger <python@rcn.com>
parents:
34205
diff
changeset
|
336 p->depth--; |
|
b480abaf1f6f
SF 1098985: set objects cannot be marshalled
Raymond Hettinger <python@rcn.com>
parents:
34205
diff
changeset
|
337 p->error = 1; |
|
b480abaf1f6f
SF 1098985: set objects cannot be marshalled
Raymond Hettinger <python@rcn.com>
parents:
34205
diff
changeset
|
338 return; |
|
b480abaf1f6f
SF 1098985: set objects cannot be marshalled
Raymond Hettinger <python@rcn.com>
parents:
34205
diff
changeset
|
339 } |
|
b480abaf1f6f
SF 1098985: set objects cannot be marshalled
Raymond Hettinger <python@rcn.com>
parents:
34205
diff
changeset
|
340 w_long((long)n, p); |
|
b480abaf1f6f
SF 1098985: set objects cannot be marshalled
Raymond Hettinger <python@rcn.com>
parents:
34205
diff
changeset
|
341 it = PyObject_GetIter(v); |
|
b480abaf1f6f
SF 1098985: set objects cannot be marshalled
Raymond Hettinger <python@rcn.com>
parents:
34205
diff
changeset
|
342 if (it == NULL) { |
|
b480abaf1f6f
SF 1098985: set objects cannot be marshalled
Raymond Hettinger <python@rcn.com>
parents:
34205
diff
changeset
|
343 p->depth--; |
|
b480abaf1f6f
SF 1098985: set objects cannot be marshalled
Raymond Hettinger <python@rcn.com>
parents:
34205
diff
changeset
|
344 p->error = 1; |
|
b480abaf1f6f
SF 1098985: set objects cannot be marshalled
Raymond Hettinger <python@rcn.com>
parents:
34205
diff
changeset
|
345 return; |
|
b480abaf1f6f
SF 1098985: set objects cannot be marshalled
Raymond Hettinger <python@rcn.com>
parents:
34205
diff
changeset
|
346 } |
|
b480abaf1f6f
SF 1098985: set objects cannot be marshalled
Raymond Hettinger <python@rcn.com>
parents:
34205
diff
changeset
|
347 while ((value = PyIter_Next(it)) != NULL) { |
|
b480abaf1f6f
SF 1098985: set objects cannot be marshalled
Raymond Hettinger <python@rcn.com>
parents:
34205
diff
changeset
|
348 w_object(value, p); |
|
b480abaf1f6f
SF 1098985: set objects cannot be marshalled
Raymond Hettinger <python@rcn.com>
parents:
34205
diff
changeset
|
349 Py_DECREF(value); |
|
b480abaf1f6f
SF 1098985: set objects cannot be marshalled
Raymond Hettinger <python@rcn.com>
parents:
34205
diff
changeset
|
350 } |
|
b480abaf1f6f
SF 1098985: set objects cannot be marshalled
Raymond Hettinger <python@rcn.com>
parents:
34205
diff
changeset
|
351 Py_DECREF(it); |
|
b480abaf1f6f
SF 1098985: set objects cannot be marshalled
Raymond Hettinger <python@rcn.com>
parents:
34205
diff
changeset
|
352 if (PyErr_Occurred()) { |
|
b480abaf1f6f
SF 1098985: set objects cannot be marshalled
Raymond Hettinger <python@rcn.com>
parents:
34205
diff
changeset
|
353 p->depth--; |
|
b480abaf1f6f
SF 1098985: set objects cannot be marshalled
Raymond Hettinger <python@rcn.com>
parents:
34205
diff
changeset
|
354 p->error = 1; |
|
b480abaf1f6f
SF 1098985: set objects cannot be marshalled
Raymond Hettinger <python@rcn.com>
parents:
34205
diff
changeset
|
355 return; |
|
b480abaf1f6f
SF 1098985: set objects cannot be marshalled
Raymond Hettinger <python@rcn.com>
parents:
34205
diff
changeset
|
356 } |
|
b480abaf1f6f
SF 1098985: set objects cannot be marshalled
Raymond Hettinger <python@rcn.com>
parents:
34205
diff
changeset
|
357 } |
|
5373
5880384d7c71
Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
358 else if (PyCode_Check(v)) { |
|
5880384d7c71
Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
359 PyCodeObject *co = (PyCodeObject *)v; |
|
1191
38f0054f44a7
Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
360 w_byte(TYPE_CODE, p); |
|
23851
d6ae544acd24
Fix SF bug # 561858 Assertion with very long lists
Neal Norwitz <nnorwitz@gmail.com>
parents:
22785
diff
changeset
|
361 w_long(co->co_argcount, p); |
|
d6ae544acd24
Fix SF bug # 561858 Assertion with very long lists
Neal Norwitz <nnorwitz@gmail.com>
parents:
22785
diff
changeset
|
362 w_long(co->co_nlocals, p); |
|
d6ae544acd24
Fix SF bug # 561858 Assertion with very long lists
Neal Norwitz <nnorwitz@gmail.com>
parents:
22785
diff
changeset
|
363 w_long(co->co_stacksize, p); |
|
d6ae544acd24
Fix SF bug # 561858 Assertion with very long lists
Neal Norwitz <nnorwitz@gmail.com>
parents:
22785
diff
changeset
|
364 w_long(co->co_flags, p); |
|
9293
0a67db47e1d0
Changes to support other object types besides strings
Guido van Rossum <guido@python.org>
parents:
8465
diff
changeset
|
365 w_object(co->co_code, p); |
|
1191
38f0054f44a7
Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
366 w_object(co->co_consts, p); |
|
38f0054f44a7
Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
367 w_object(co->co_names, p); |
|
2844
0261bf5b3819
keyword arguments and faster calls
Guido van Rossum <guido@python.org>
parents:
2503
diff
changeset
|
368 w_object(co->co_varnames, p); |
|
16257
fd8c7203251f
PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15979
diff
changeset
|
369 w_object(co->co_freevars, p); |
|
fd8c7203251f
PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15979
diff
changeset
|
370 w_object(co->co_cellvars, p); |
|
1191
38f0054f44a7
Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
371 w_object(co->co_filename, p); |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1191
diff
changeset
|
372 w_object(co->co_name, p); |
|
23851
d6ae544acd24
Fix SF bug # 561858 Assertion with very long lists
Neal Norwitz <nnorwitz@gmail.com>
parents:
22785
diff
changeset
|
373 w_long(co->co_firstlineno, p); |
|
5042
9b7e0cf51990
Marshal the line number table of code objects.
Guido van Rossum <guido@python.org>
parents:
4995
diff
changeset
|
374 w_object(co->co_lnotab, p); |
|
1191
38f0054f44a7
Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
375 } |
|
20947
c555f1e7c4be
Use PyObject_CheckReadBuffer().
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20552
diff
changeset
|
376 else if (PyObject_CheckReadBuffer(v)) { |
|
9293
0a67db47e1d0
Changes to support other object types besides strings
Guido van Rossum <guido@python.org>
parents:
8465
diff
changeset
|
377 /* Write unknown buffer-style objects as a string */ |
|
0a67db47e1d0
Changes to support other object types besides strings
Guido van Rossum <guido@python.org>
parents:
8465
diff
changeset
|
378 char *s; |
|
20947
c555f1e7c4be
Use PyObject_CheckReadBuffer().
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20552
diff
changeset
|
379 PyBufferProcs *pb = v->ob_type->tp_as_buffer; |
|
9293
0a67db47e1d0
Changes to support other object types besides strings
Guido van Rossum <guido@python.org>
parents:
8465
diff
changeset
|
380 w_byte(TYPE_STRING, p); |
|
0a67db47e1d0
Changes to support other object types besides strings
Guido van Rossum <guido@python.org>
parents:
8465
diff
changeset
|
381 n = (*pb->bf_getreadbuffer)(v, 0, (void **)&s); |
|
36866
9eb24f92a784
Change int to Py_ssize_t in several places.
Martin v. Löwis <martin@v.loewis.de>
parents:
36792
diff
changeset
|
382 if (n > INT_MAX) { |
|
9eb24f92a784
Change int to Py_ssize_t in several places.
Martin v. Löwis <martin@v.loewis.de>
parents:
36792
diff
changeset
|
383 p->depth--; |
|
9eb24f92a784
Change int to Py_ssize_t in several places.
Martin v. Löwis <martin@v.loewis.de>
parents:
36792
diff
changeset
|
384 p->error = 1; |
|
9eb24f92a784
Change int to Py_ssize_t in several places.
Martin v. Löwis <martin@v.loewis.de>
parents:
36792
diff
changeset
|
385 return; |
|
9eb24f92a784
Change int to Py_ssize_t in several places.
Martin v. Löwis <martin@v.loewis.de>
parents:
36792
diff
changeset
|
386 } |
|
9293
0a67db47e1d0
Changes to support other object types besides strings
Guido van Rossum <guido@python.org>
parents:
8465
diff
changeset
|
387 w_long((long)n, p); |
|
36866
9eb24f92a784
Change int to Py_ssize_t in several places.
Martin v. Löwis <martin@v.loewis.de>
parents:
36792
diff
changeset
|
388 w_string(s, (int)n, p); |
|
9293
0a67db47e1d0
Changes to support other object types besides strings
Guido van Rossum <guido@python.org>
parents:
8465
diff
changeset
|
389 } |
|
1191
38f0054f44a7
Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
390 else { |
|
38f0054f44a7
Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
391 w_byte(TYPE_UNKNOWN, p); |
|
3731
de1cb33219e3
Jeremy's patches for more robust handling of unmarshallable types.
Guido van Rossum <guido@python.org>
parents:
3620
diff
changeset
|
392 p->error = 1; |
|
1191
38f0054f44a7
Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
393 } |
|
32422
3df36e2e5ddc
Patch #923098: Share interned strings in marshal.
Martin v. Löwis <martin@v.loewis.de>
parents:
32343
diff
changeset
|
394 exit: |
|
13106
a6725acdf33b
Urmpf. Quality control on this patch lapsed a bit. :-(
Guido van Rossum <guido@python.org>
parents:
13070
diff
changeset
|
395 p->depth--; |
| 267 | 396 } |
| 397 | |
|
32422
3df36e2e5ddc
Patch #923098: Share interned strings in marshal.
Martin v. Löwis <martin@v.loewis.de>
parents:
32343
diff
changeset
|
398 /* version currently has no effect for writing longs. */ |
| 267 | 399 void |
|
32422
3df36e2e5ddc
Patch #923098: Share interned strings in marshal.
Martin v. Löwis <martin@v.loewis.de>
parents:
32343
diff
changeset
|
400 PyMarshal_WriteLongToFile(long x, FILE *fp, int version) |
| 267 | 401 { |
|
1191
38f0054f44a7
Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
402 WFILE wf; |
|
38f0054f44a7
Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
403 wf.fp = fp; |
|
3731
de1cb33219e3
Jeremy's patches for more robust handling of unmarshallable types.
Guido van Rossum <guido@python.org>
parents:
3620
diff
changeset
|
404 wf.error = 0; |
|
13070
7392e71f6cce
Michael Hudson <mwh21@cam.ac.uk>:
Fred Drake <fdrake@acm.org>
parents:
12576
diff
changeset
|
405 wf.depth = 0; |
|
32422
3df36e2e5ddc
Patch #923098: Share interned strings in marshal.
Martin v. Löwis <martin@v.loewis.de>
parents:
32343
diff
changeset
|
406 wf.strings = NULL; |
| 35008 | 407 wf.version = version; |
|
1191
38f0054f44a7
Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
408 w_long(x, &wf); |
| 267 | 409 } |
| 410 | |
| 411 void | |
|
32422
3df36e2e5ddc
Patch #923098: Share interned strings in marshal.
Martin v. Löwis <martin@v.loewis.de>
parents:
32343
diff
changeset
|
412 PyMarshal_WriteObjectToFile(PyObject *x, FILE *fp, int version) |
| 267 | 413 { |
|
1191
38f0054f44a7
Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
414 WFILE wf; |
|
38f0054f44a7
Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
415 wf.fp = fp; |
|
3731
de1cb33219e3
Jeremy's patches for more robust handling of unmarshallable types.
Guido van Rossum <guido@python.org>
parents:
3620
diff
changeset
|
416 wf.error = 0; |
|
13106
a6725acdf33b
Urmpf. Quality control on this patch lapsed a bit. :-(
Guido van Rossum <guido@python.org>
parents:
13070
diff
changeset
|
417 wf.depth = 0; |
|
32422
3df36e2e5ddc
Patch #923098: Share interned strings in marshal.
Martin v. Löwis <martin@v.loewis.de>
parents:
32343
diff
changeset
|
418 wf.strings = (version > 0) ? PyDict_New() : NULL; |
| 35008 | 419 wf.version = version; |
|
1191
38f0054f44a7
Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
420 w_object(x, &wf); |
|
32422
3df36e2e5ddc
Patch #923098: Share interned strings in marshal.
Martin v. Löwis <martin@v.loewis.de>
parents:
32343
diff
changeset
|
421 Py_XDECREF(wf.strings); |
| 267 | 422 } |
| 423 | |
|
1191
38f0054f44a7
Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
424 typedef WFILE RFILE; /* Same struct with different invariants */ |
|
38f0054f44a7
Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
425 |
|
41741
6f1293ab3ac4
Fix bug in marshal where bad data would cause a segfault due to
Neal Norwitz <nnorwitz@gmail.com>
parents:
39995
diff
changeset
|
426 #define rs_byte(p) (((p)->ptr < (p)->end) ? (unsigned char)*(p)->ptr++ : EOF) |
|
2503
4dd1af0ddef0
various tuple related optimizations; remove unused b/w compat code from ceval.c
Guido van Rossum <guido@python.org>
parents:
2368
diff
changeset
|
427 |
|
4dd1af0ddef0
various tuple related optimizations; remove unused b/w compat code from ceval.c
Guido van Rossum <guido@python.org>
parents:
2368
diff
changeset
|
428 #define r_byte(p) ((p)->fp ? getc((p)->fp) : rs_byte(p)) |
| 267 | 429 |
|
1191
38f0054f44a7
Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
430 static int |
|
13658
f79434ee41fa
Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents:
13447
diff
changeset
|
431 r_string(char *s, int n, RFILE *p) |
|
1191
38f0054f44a7
Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
432 { |
|
38f0054f44a7
Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
433 if (p->fp != NULL) |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36324
diff
changeset
|
434 /* The result fits into int because it must be <=n. */ |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36324
diff
changeset
|
435 return (int)fread(s, 1, n, p->fp); |
|
1191
38f0054f44a7
Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
436 if (p->end - p->ptr < n) |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36324
diff
changeset
|
437 n = (int)(p->end - p->ptr); |
|
1191
38f0054f44a7
Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
438 memcpy(s, p->ptr, n); |
|
38f0054f44a7
Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
439 p->ptr += n; |
|
38f0054f44a7
Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
440 return n; |
|
38f0054f44a7
Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
441 } |
|
38f0054f44a7
Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
442 |
|
38f0054f44a7
Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
443 static int |
|
13658
f79434ee41fa
Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents:
13447
diff
changeset
|
444 r_short(RFILE *p) |
| 267 | 445 { |
| 446 register short x; | |
|
1191
38f0054f44a7
Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
447 x = r_byte(p); |
|
38f0054f44a7
Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
448 x |= r_byte(p) << 8; |
| 14678 | 449 /* Sign-extension, in case short greater than 16 bits */ |
| 450 x |= -(x & 0x8000); | |
| 267 | 451 return x; |
| 452 } | |
| 453 | |
|
1191
38f0054f44a7
Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
454 static long |
|
13658
f79434ee41fa
Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents:
13447
diff
changeset
|
455 r_long(RFILE *p) |
| 267 | 456 { |
| 457 register long x; | |
|
2503
4dd1af0ddef0
various tuple related optimizations; remove unused b/w compat code from ceval.c
Guido van Rossum <guido@python.org>
parents:
2368
diff
changeset
|
458 register FILE *fp = p->fp; |
|
4dd1af0ddef0
various tuple related optimizations; remove unused b/w compat code from ceval.c
Guido van Rossum <guido@python.org>
parents:
2368
diff
changeset
|
459 if (fp) { |
|
4dd1af0ddef0
various tuple related optimizations; remove unused b/w compat code from ceval.c
Guido van Rossum <guido@python.org>
parents:
2368
diff
changeset
|
460 x = getc(fp); |
|
4dd1af0ddef0
various tuple related optimizations; remove unused b/w compat code from ceval.c
Guido van Rossum <guido@python.org>
parents:
2368
diff
changeset
|
461 x |= (long)getc(fp) << 8; |
|
4dd1af0ddef0
various tuple related optimizations; remove unused b/w compat code from ceval.c
Guido van Rossum <guido@python.org>
parents:
2368
diff
changeset
|
462 x |= (long)getc(fp) << 16; |
|
4dd1af0ddef0
various tuple related optimizations; remove unused b/w compat code from ceval.c
Guido van Rossum <guido@python.org>
parents:
2368
diff
changeset
|
463 x |= (long)getc(fp) << 24; |
|
4dd1af0ddef0
various tuple related optimizations; remove unused b/w compat code from ceval.c
Guido van Rossum <guido@python.org>
parents:
2368
diff
changeset
|
464 } |
|
4dd1af0ddef0
various tuple related optimizations; remove unused b/w compat code from ceval.c
Guido van Rossum <guido@python.org>
parents:
2368
diff
changeset
|
465 else { |
|
4dd1af0ddef0
various tuple related optimizations; remove unused b/w compat code from ceval.c
Guido van Rossum <guido@python.org>
parents:
2368
diff
changeset
|
466 x = rs_byte(p); |
|
4dd1af0ddef0
various tuple related optimizations; remove unused b/w compat code from ceval.c
Guido van Rossum <guido@python.org>
parents:
2368
diff
changeset
|
467 x |= (long)rs_byte(p) << 8; |
|
4dd1af0ddef0
various tuple related optimizations; remove unused b/w compat code from ceval.c
Guido van Rossum <guido@python.org>
parents:
2368
diff
changeset
|
468 x |= (long)rs_byte(p) << 16; |
|
4dd1af0ddef0
various tuple related optimizations; remove unused b/w compat code from ceval.c
Guido van Rossum <guido@python.org>
parents:
2368
diff
changeset
|
469 x |= (long)rs_byte(p) << 24; |
|
4dd1af0ddef0
various tuple related optimizations; remove unused b/w compat code from ceval.c
Guido van Rossum <guido@python.org>
parents:
2368
diff
changeset
|
470 } |
|
4656
186c2ad4abf3
Better way to handle 64-bit ints, keeping gcc -Wall happy.
Guido van Rossum <guido@python.org>
parents:
4592
diff
changeset
|
471 #if SIZEOF_LONG > 4 |
|
4592
3bf9e2beaf8c
Tentative changes to make this work better on 64-bit machines.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
472 /* Sign extension for 64-bit machines */ |
| 14678 | 473 x |= -(x & 0x80000000L); |
|
4656
186c2ad4abf3
Better way to handle 64-bit ints, keeping gcc -Wall happy.
Guido van Rossum <guido@python.org>
parents:
4592
diff
changeset
|
474 #endif |
|
4592
3bf9e2beaf8c
Tentative changes to make this work better on 64-bit machines.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
475 return x; |
|
3bf9e2beaf8c
Tentative changes to make this work better on 64-bit machines.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
476 } |
|
3bf9e2beaf8c
Tentative changes to make this work better on 64-bit machines.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
477 |
|
19547
7e628f60245e
marshal.c r_long64: When reading a TYPE_INT64 value on a box with 32-bit
Tim Peters <tim.peters@gmail.com>
parents:
19343
diff
changeset
|
478 /* r_long64 deals with the TYPE_INT64 code. On a machine with |
|
7e628f60245e
marshal.c r_long64: When reading a TYPE_INT64 value on a box with 32-bit
Tim Peters <tim.peters@gmail.com>
parents:
19343
diff
changeset
|
479 sizeof(long) > 4, it returns a Python int object, else a Python long |
|
7e628f60245e
marshal.c r_long64: When reading a TYPE_INT64 value on a box with 32-bit
Tim Peters <tim.peters@gmail.com>
parents:
19343
diff
changeset
|
480 object. Note that w_long64 writes out TYPE_INT if 32 bits is enough, |
|
7e628f60245e
marshal.c r_long64: When reading a TYPE_INT64 value on a box with 32-bit
Tim Peters <tim.peters@gmail.com>
parents:
19343
diff
changeset
|
481 so there's no inefficiency here in returning a PyLong on 32-bit boxes |
|
7e628f60245e
marshal.c r_long64: When reading a TYPE_INT64 value on a box with 32-bit
Tim Peters <tim.peters@gmail.com>
parents:
19343
diff
changeset
|
482 for everything written via TYPE_INT64 (i.e., if an int is written via |
|
7e628f60245e
marshal.c r_long64: When reading a TYPE_INT64 value on a box with 32-bit
Tim Peters <tim.peters@gmail.com>
parents:
19343
diff
changeset
|
483 TYPE_INT64, it *needs* more than 32 bits). |
|
7e628f60245e
marshal.c r_long64: When reading a TYPE_INT64 value on a box with 32-bit
Tim Peters <tim.peters@gmail.com>
parents:
19343
diff
changeset
|
484 */ |
|
7e628f60245e
marshal.c r_long64: When reading a TYPE_INT64 value on a box with 32-bit
Tim Peters <tim.peters@gmail.com>
parents:
19343
diff
changeset
|
485 static PyObject * |
|
13658
f79434ee41fa
Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents:
13447
diff
changeset
|
486 r_long64(RFILE *p) |
|
4592
3bf9e2beaf8c
Tentative changes to make this work better on 64-bit machines.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
487 { |
|
19547
7e628f60245e
marshal.c r_long64: When reading a TYPE_INT64 value on a box with 32-bit
Tim Peters <tim.peters@gmail.com>
parents:
19343
diff
changeset
|
488 long lo4 = r_long(p); |
|
7e628f60245e
marshal.c r_long64: When reading a TYPE_INT64 value on a box with 32-bit
Tim Peters <tim.peters@gmail.com>
parents:
19343
diff
changeset
|
489 long hi4 = r_long(p); |
|
4656
186c2ad4abf3
Better way to handle 64-bit ints, keeping gcc -Wall happy.
Guido van Rossum <guido@python.org>
parents:
4592
diff
changeset
|
490 #if SIZEOF_LONG > 4 |
|
19547
7e628f60245e
marshal.c r_long64: When reading a TYPE_INT64 value on a box with 32-bit
Tim Peters <tim.peters@gmail.com>
parents:
19343
diff
changeset
|
491 long x = (hi4 << 32) | (lo4 & 0xFFFFFFFFL); |
|
7e628f60245e
marshal.c r_long64: When reading a TYPE_INT64 value on a box with 32-bit
Tim Peters <tim.peters@gmail.com>
parents:
19343
diff
changeset
|
492 return PyInt_FromLong(x); |
|
4656
186c2ad4abf3
Better way to handle 64-bit ints, keeping gcc -Wall happy.
Guido van Rossum <guido@python.org>
parents:
4592
diff
changeset
|
493 #else |
|
19547
7e628f60245e
marshal.c r_long64: When reading a TYPE_INT64 value on a box with 32-bit
Tim Peters <tim.peters@gmail.com>
parents:
19343
diff
changeset
|
494 unsigned char buf[8]; |
|
7e628f60245e
marshal.c r_long64: When reading a TYPE_INT64 value on a box with 32-bit
Tim Peters <tim.peters@gmail.com>
parents:
19343
diff
changeset
|
495 int one = 1; |
|
7e628f60245e
marshal.c r_long64: When reading a TYPE_INT64 value on a box with 32-bit
Tim Peters <tim.peters@gmail.com>
parents:
19343
diff
changeset
|
496 int is_little_endian = (int)*(char*)&one; |
|
7e628f60245e
marshal.c r_long64: When reading a TYPE_INT64 value on a box with 32-bit
Tim Peters <tim.peters@gmail.com>
parents:
19343
diff
changeset
|
497 if (is_little_endian) { |
|
7e628f60245e
marshal.c r_long64: When reading a TYPE_INT64 value on a box with 32-bit
Tim Peters <tim.peters@gmail.com>
parents:
19343
diff
changeset
|
498 memcpy(buf, &lo4, 4); |
|
7e628f60245e
marshal.c r_long64: When reading a TYPE_INT64 value on a box with 32-bit
Tim Peters <tim.peters@gmail.com>
parents:
19343
diff
changeset
|
499 memcpy(buf+4, &hi4, 4); |
|
4592
3bf9e2beaf8c
Tentative changes to make this work better on 64-bit machines.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
500 } |
|
19547
7e628f60245e
marshal.c r_long64: When reading a TYPE_INT64 value on a box with 32-bit
Tim Peters <tim.peters@gmail.com>
parents:
19343
diff
changeset
|
501 else { |
|
7e628f60245e
marshal.c r_long64: When reading a TYPE_INT64 value on a box with 32-bit
Tim Peters <tim.peters@gmail.com>
parents:
19343
diff
changeset
|
502 memcpy(buf, &hi4, 4); |
|
7e628f60245e
marshal.c r_long64: When reading a TYPE_INT64 value on a box with 32-bit
Tim Peters <tim.peters@gmail.com>
parents:
19343
diff
changeset
|
503 memcpy(buf+4, &lo4, 4); |
|
7e628f60245e
marshal.c r_long64: When reading a TYPE_INT64 value on a box with 32-bit
Tim Peters <tim.peters@gmail.com>
parents:
19343
diff
changeset
|
504 } |
|
7e628f60245e
marshal.c r_long64: When reading a TYPE_INT64 value on a box with 32-bit
Tim Peters <tim.peters@gmail.com>
parents:
19343
diff
changeset
|
505 return _PyLong_FromByteArray(buf, 8, is_little_endian, 1); |
|
4656
186c2ad4abf3
Better way to handle 64-bit ints, keeping gcc -Wall happy.
Guido van Rossum <guido@python.org>
parents:
4592
diff
changeset
|
506 #endif |
| 267 | 507 } |
| 508 | |
|
5373
5880384d7c71
Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
509 static PyObject * |
|
13658
f79434ee41fa
Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents:
13447
diff
changeset
|
510 r_object(RFILE *p) |
| 267 | 511 { |
|
31824
331de8b9d1ed
Marshal clean-up (SF patch #873224)
Armin Rigo <arigo@tunes.org>
parents:
30311
diff
changeset
|
512 /* NULL is a valid return value, it does not necessarily means that |
|
331de8b9d1ed
Marshal clean-up (SF patch #873224)
Armin Rigo <arigo@tunes.org>
parents:
30311
diff
changeset
|
513 an exception is set. */ |
|
45054
53b90f31efd6
Removed unused var
Christian Heimes <christian@cheimes.de>
parents:
45039
diff
changeset
|
514 PyObject *v, *v2; |
| 267 | 515 long i, n; |
|
1191
38f0054f44a7
Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
516 int type = r_byte(p); |
|
41741
6f1293ab3ac4
Fix bug in marshal where bad data would cause a segfault due to
Neal Norwitz <nnorwitz@gmail.com>
parents:
39995
diff
changeset
|
517 PyObject *retval; |
|
6f1293ab3ac4
Fix bug in marshal where bad data would cause a segfault due to
Neal Norwitz <nnorwitz@gmail.com>
parents:
39995
diff
changeset
|
518 |
|
6f1293ab3ac4
Fix bug in marshal where bad data would cause a segfault due to
Neal Norwitz <nnorwitz@gmail.com>
parents:
39995
diff
changeset
|
519 p->depth++; |
|
6f1293ab3ac4
Fix bug in marshal where bad data would cause a segfault due to
Neal Norwitz <nnorwitz@gmail.com>
parents:
39995
diff
changeset
|
520 |
|
6f1293ab3ac4
Fix bug in marshal where bad data would cause a segfault due to
Neal Norwitz <nnorwitz@gmail.com>
parents:
39995
diff
changeset
|
521 if (p->depth > MAX_MARSHAL_STACK_DEPTH) { |
|
6f1293ab3ac4
Fix bug in marshal where bad data would cause a segfault due to
Neal Norwitz <nnorwitz@gmail.com>
parents:
39995
diff
changeset
|
522 p->depth--; |
|
6f1293ab3ac4
Fix bug in marshal where bad data would cause a segfault due to
Neal Norwitz <nnorwitz@gmail.com>
parents:
39995
diff
changeset
|
523 PyErr_SetString(PyExc_ValueError, "recursion limit exceeded"); |
|
6f1293ab3ac4
Fix bug in marshal where bad data would cause a segfault due to
Neal Norwitz <nnorwitz@gmail.com>
parents:
39995
diff
changeset
|
524 return NULL; |
|
6f1293ab3ac4
Fix bug in marshal where bad data would cause a segfault due to
Neal Norwitz <nnorwitz@gmail.com>
parents:
39995
diff
changeset
|
525 } |
|
16311
8c41f3861aad
It's unclear whether PyMarshal_XXX() are part of the public or private API.
Tim Peters <tim.peters@gmail.com>
parents:
16257
diff
changeset
|
526 |
| 267 | 527 switch (type) { |
|
16311
8c41f3861aad
It's unclear whether PyMarshal_XXX() are part of the public or private API.
Tim Peters <tim.peters@gmail.com>
parents:
16257
diff
changeset
|
528 |
| 267 | 529 case EOF: |
|
5373
5880384d7c71
Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
530 PyErr_SetString(PyExc_EOFError, |
|
5880384d7c71
Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
531 "EOF read where object expected"); |
|
41741
6f1293ab3ac4
Fix bug in marshal where bad data would cause a segfault due to
Neal Norwitz <nnorwitz@gmail.com>
parents:
39995
diff
changeset
|
532 retval = NULL; |
|
6f1293ab3ac4
Fix bug in marshal where bad data would cause a segfault due to
Neal Norwitz <nnorwitz@gmail.com>
parents:
39995
diff
changeset
|
533 break; |
|
16311
8c41f3861aad
It's unclear whether PyMarshal_XXX() are part of the public or private API.
Tim Peters <tim.peters@gmail.com>
parents:
16257
diff
changeset
|
534 |
| 267 | 535 case TYPE_NULL: |
|
41741
6f1293ab3ac4
Fix bug in marshal where bad data would cause a segfault due to
Neal Norwitz <nnorwitz@gmail.com>
parents:
39995
diff
changeset
|
536 retval = NULL; |
|
6f1293ab3ac4
Fix bug in marshal where bad data would cause a segfault due to
Neal Norwitz <nnorwitz@gmail.com>
parents:
39995
diff
changeset
|
537 break; |
|
16311
8c41f3861aad
It's unclear whether PyMarshal_XXX() are part of the public or private API.
Tim Peters <tim.peters@gmail.com>
parents:
16257
diff
changeset
|
538 |
| 267 | 539 case TYPE_NONE: |
|
5373
5880384d7c71
Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
540 Py_INCREF(Py_None); |
|
41741
6f1293ab3ac4
Fix bug in marshal where bad data would cause a segfault due to
Neal Norwitz <nnorwitz@gmail.com>
parents:
39995
diff
changeset
|
541 retval = Py_None; |
|
6f1293ab3ac4
Fix bug in marshal where bad data would cause a segfault due to
Neal Norwitz <nnorwitz@gmail.com>
parents:
39995
diff
changeset
|
542 break; |
|
16311
8c41f3861aad
It's unclear whether PyMarshal_XXX() are part of the public or private API.
Tim Peters <tim.peters@gmail.com>
parents:
16257
diff
changeset
|
543 |
|
18148
7bc473b2c654
Merging the gen-branch into the main line, at Guido's direction. Yay!
Tim Peters <tim.peters@gmail.com>
parents:
17707
diff
changeset
|
544 case TYPE_STOPITER: |
|
7bc473b2c654
Merging the gen-branch into the main line, at Guido's direction. Yay!
Tim Peters <tim.peters@gmail.com>
parents:
17707
diff
changeset
|
545 Py_INCREF(PyExc_StopIteration); |
|
41741
6f1293ab3ac4
Fix bug in marshal where bad data would cause a segfault due to
Neal Norwitz <nnorwitz@gmail.com>
parents:
39995
diff
changeset
|
546 retval = PyExc_StopIteration; |
|
6f1293ab3ac4
Fix bug in marshal where bad data would cause a segfault due to
Neal Norwitz <nnorwitz@gmail.com>
parents:
39995
diff
changeset
|
547 break; |
|
18148
7bc473b2c654
Merging the gen-branch into the main line, at Guido's direction. Yay!
Tim Peters <tim.peters@gmail.com>
parents:
17707
diff
changeset
|
548 |
|
4403
3b3f7c3f6da2
Ellipses -> Ellipsis rename (the dictionary really says that it should
Guido van Rossum <guido@python.org>
parents:
4039
diff
changeset
|
549 case TYPE_ELLIPSIS: |
|
5373
5880384d7c71
Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
550 Py_INCREF(Py_Ellipsis); |
|
41741
6f1293ab3ac4
Fix bug in marshal where bad data would cause a segfault due to
Neal Norwitz <nnorwitz@gmail.com>
parents:
39995
diff
changeset
|
551 retval = Py_Ellipsis; |
|
6f1293ab3ac4
Fix bug in marshal where bad data would cause a segfault due to
Neal Norwitz <nnorwitz@gmail.com>
parents:
39995
diff
changeset
|
552 break; |
|
16311
8c41f3861aad
It's unclear whether PyMarshal_XXX() are part of the public or private API.
Tim Peters <tim.peters@gmail.com>
parents:
16257
diff
changeset
|
553 |
|
22785
aa8d59823add
Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents:
22705
diff
changeset
|
554 case TYPE_FALSE: |
|
aa8d59823add
Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents:
22705
diff
changeset
|
555 Py_INCREF(Py_False); |
|
41741
6f1293ab3ac4
Fix bug in marshal where bad data would cause a segfault due to
Neal Norwitz <nnorwitz@gmail.com>
parents:
39995
diff
changeset
|
556 retval = Py_False; |
|
6f1293ab3ac4
Fix bug in marshal where bad data would cause a segfault due to
Neal Norwitz <nnorwitz@gmail.com>
parents:
39995
diff
changeset
|
557 break; |
|
22785
aa8d59823add
Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents:
22705
diff
changeset
|
558 |
|
aa8d59823add
Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents:
22705
diff
changeset
|
559 case TYPE_TRUE: |
|
aa8d59823add
Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents:
22705
diff
changeset
|
560 Py_INCREF(Py_True); |
|
41741
6f1293ab3ac4
Fix bug in marshal where bad data would cause a segfault due to
Neal Norwitz <nnorwitz@gmail.com>
parents:
39995
diff
changeset
|
561 retval = Py_True; |
|
6f1293ab3ac4
Fix bug in marshal where bad data would cause a segfault due to
Neal Norwitz <nnorwitz@gmail.com>
parents:
39995
diff
changeset
|
562 break; |
|
22785
aa8d59823add
Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents:
22705
diff
changeset
|
563 |
| 267 | 564 case TYPE_INT: |
|
41741
6f1293ab3ac4
Fix bug in marshal where bad data would cause a segfault due to
Neal Norwitz <nnorwitz@gmail.com>
parents:
39995
diff
changeset
|
565 retval = PyInt_FromLong(r_long(p)); |
|
6f1293ab3ac4
Fix bug in marshal where bad data would cause a segfault due to
Neal Norwitz <nnorwitz@gmail.com>
parents:
39995
diff
changeset
|
566 break; |
|
16311
8c41f3861aad
It's unclear whether PyMarshal_XXX() are part of the public or private API.
Tim Peters <tim.peters@gmail.com>
parents:
16257
diff
changeset
|
567 |
|
4592
3bf9e2beaf8c
Tentative changes to make this work better on 64-bit machines.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
568 case TYPE_INT64: |
|
41741
6f1293ab3ac4
Fix bug in marshal where bad data would cause a segfault due to
Neal Norwitz <nnorwitz@gmail.com>
parents:
39995
diff
changeset
|
569 retval = r_long64(p); |
|
6f1293ab3ac4
Fix bug in marshal where bad data would cause a segfault due to
Neal Norwitz <nnorwitz@gmail.com>
parents:
39995
diff
changeset
|
570 break; |
|
16311
8c41f3861aad
It's unclear whether PyMarshal_XXX() are part of the public or private API.
Tim Peters <tim.peters@gmail.com>
parents:
16257
diff
changeset
|
571 |
| 267 | 572 case TYPE_LONG: |
| 573 { | |
| 574 int size; | |
|
5373
5880384d7c71
Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
575 PyLongObject *ob; |
|
1191
38f0054f44a7
Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
576 n = r_long(p); |
|
39995
6ba402b60041
Forward-port of r52136,52138: a review of overflow-detecting code.
Armin Rigo <arigo@tunes.org>
parents:
39448
diff
changeset
|
577 if (n < -INT_MAX || n > INT_MAX) { |
|
6ba402b60041
Forward-port of r52136,52138: a review of overflow-detecting code.
Armin Rigo <arigo@tunes.org>
parents:
39448
diff
changeset
|
578 PyErr_SetString(PyExc_ValueError, |
|
6ba402b60041
Forward-port of r52136,52138: a review of overflow-detecting code.
Armin Rigo <arigo@tunes.org>
parents:
39448
diff
changeset
|
579 "bad marshal data"); |
|
41741
6f1293ab3ac4
Fix bug in marshal where bad data would cause a segfault due to
Neal Norwitz <nnorwitz@gmail.com>
parents:
39995
diff
changeset
|
580 retval = NULL; |
|
6f1293ab3ac4
Fix bug in marshal where bad data would cause a segfault due to
Neal Norwitz <nnorwitz@gmail.com>
parents:
39995
diff
changeset
|
581 break; |
|
39995
6ba402b60041
Forward-port of r52136,52138: a review of overflow-detecting code.
Armin Rigo <arigo@tunes.org>
parents:
39448
diff
changeset
|
582 } |
| 267 | 583 size = n<0 ? -n : n; |
|
5373
5880384d7c71
Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
584 ob = _PyLong_New(size); |
|
41741
6f1293ab3ac4
Fix bug in marshal where bad data would cause a segfault due to
Neal Norwitz <nnorwitz@gmail.com>
parents:
39995
diff
changeset
|
585 if (ob == NULL) { |
|
6f1293ab3ac4
Fix bug in marshal where bad data would cause a segfault due to
Neal Norwitz <nnorwitz@gmail.com>
parents:
39995
diff
changeset
|
586 retval = NULL; |
|
6f1293ab3ac4
Fix bug in marshal where bad data would cause a segfault due to
Neal Norwitz <nnorwitz@gmail.com>
parents:
39995
diff
changeset
|
587 break; |
|
6f1293ab3ac4
Fix bug in marshal where bad data would cause a segfault due to
Neal Norwitz <nnorwitz@gmail.com>
parents:
39995
diff
changeset
|
588 } |
| 267 | 589 ob->ob_size = n; |
|
31824
331de8b9d1ed
Marshal clean-up (SF patch #873224)
Armin Rigo <arigo@tunes.org>
parents:
30311
diff
changeset
|
590 for (i = 0; i < size; i++) { |
|
331de8b9d1ed
Marshal clean-up (SF patch #873224)
Armin Rigo <arigo@tunes.org>
parents:
30311
diff
changeset
|
591 int digit = r_short(p); |
|
331de8b9d1ed
Marshal clean-up (SF patch #873224)
Armin Rigo <arigo@tunes.org>
parents:
30311
diff
changeset
|
592 if (digit < 0) { |
|
331de8b9d1ed
Marshal clean-up (SF patch #873224)
Armin Rigo <arigo@tunes.org>
parents:
30311
diff
changeset
|
593 Py_DECREF(ob); |
|
331de8b9d1ed
Marshal clean-up (SF patch #873224)
Armin Rigo <arigo@tunes.org>
parents:
30311
diff
changeset
|
594 PyErr_SetString(PyExc_ValueError, |
|
331de8b9d1ed
Marshal clean-up (SF patch #873224)
Armin Rigo <arigo@tunes.org>
parents:
30311
diff
changeset
|
595 "bad marshal data"); |
|
41741
6f1293ab3ac4
Fix bug in marshal where bad data would cause a segfault due to
Neal Norwitz <nnorwitz@gmail.com>
parents:
39995
diff
changeset
|
596 ob = NULL; |
|
6f1293ab3ac4
Fix bug in marshal where bad data would cause a segfault due to
Neal Norwitz <nnorwitz@gmail.com>
parents:
39995
diff
changeset
|
597 break; |
|
31824
331de8b9d1ed
Marshal clean-up (SF patch #873224)
Armin Rigo <arigo@tunes.org>
parents:
30311
diff
changeset
|
598 } |
|
41741
6f1293ab3ac4
Fix bug in marshal where bad data would cause a segfault due to
Neal Norwitz <nnorwitz@gmail.com>
parents:
39995
diff
changeset
|
599 if (ob != NULL) |
|
6f1293ab3ac4
Fix bug in marshal where bad data would cause a segfault due to
Neal Norwitz <nnorwitz@gmail.com>
parents:
39995
diff
changeset
|
600 ob->ob_digit[i] = digit; |
|
31824
331de8b9d1ed
Marshal clean-up (SF patch #873224)
Armin Rigo <arigo@tunes.org>
parents:
30311
diff
changeset
|
601 } |
|
41741
6f1293ab3ac4
Fix bug in marshal where bad data would cause a segfault due to
Neal Norwitz <nnorwitz@gmail.com>
parents:
39995
diff
changeset
|
602 retval = (PyObject *)ob; |
|
6f1293ab3ac4
Fix bug in marshal where bad data would cause a segfault due to
Neal Norwitz <nnorwitz@gmail.com>
parents:
39995
diff
changeset
|
603 break; |
| 267 | 604 } |
|
16311
8c41f3861aad
It's unclear whether PyMarshal_XXX() are part of the public or private API.
Tim Peters <tim.peters@gmail.com>
parents:
16257
diff
changeset
|
605 |
| 267 | 606 case TYPE_FLOAT: |
| 607 { | |
| 608 char buf[256]; | |
|
5119
bda01a374762
Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
5115
diff
changeset
|
609 double dx; |
|
1191
38f0054f44a7
Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
610 n = r_byte(p); |
|
31824
331de8b9d1ed
Marshal clean-up (SF patch #873224)
Armin Rigo <arigo@tunes.org>
parents:
30311
diff
changeset
|
611 if (n == EOF || r_string(buf, (int)n, p) != n) { |
|
5373
5880384d7c71
Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
612 PyErr_SetString(PyExc_EOFError, |
| 267 | 613 "EOF read where object expected"); |
|
41741
6f1293ab3ac4
Fix bug in marshal where bad data would cause a segfault due to
Neal Norwitz <nnorwitz@gmail.com>
parents:
39995
diff
changeset
|
614 retval = NULL; |
|
6f1293ab3ac4
Fix bug in marshal where bad data would cause a segfault due to
Neal Norwitz <nnorwitz@gmail.com>
parents:
39995
diff
changeset
|
615 break; |
| 267 | 616 } |
| 617 buf[n] = '\0'; | |
|
41741
6f1293ab3ac4
Fix bug in marshal where bad data would cause a segfault due to
Neal Norwitz <nnorwitz@gmail.com>
parents:
39995
diff
changeset
|
618 retval = NULL; |
|
6f1293ab3ac4
Fix bug in marshal where bad data would cause a segfault due to
Neal Norwitz <nnorwitz@gmail.com>
parents:
39995
diff
changeset
|
619 PyFPE_START_PROTECT("atof", break) |
|
32305
13656d58d450
Patch #774665: Make Python LC_NUMERIC agnostic.
Martin v. Löwis <martin@v.loewis.de>
parents:
31824
diff
changeset
|
620 dx = PyOS_ascii_atof(buf); |
|
5197
74c12704a0bd
New form of PyFPE_END_PROTECT macro.
Guido van Rossum <guido@python.org>
parents:
5119
diff
changeset
|
621 PyFPE_END_PROTECT(dx) |
|
41741
6f1293ab3ac4
Fix bug in marshal where bad data would cause a segfault due to
Neal Norwitz <nnorwitz@gmail.com>
parents:
39995
diff
changeset
|
622 retval = PyFloat_FromDouble(dx); |
|
6f1293ab3ac4
Fix bug in marshal where bad data would cause a segfault due to
Neal Norwitz <nnorwitz@gmail.com>
parents:
39995
diff
changeset
|
623 break; |
| 267 | 624 } |
|
16311
8c41f3861aad
It's unclear whether PyMarshal_XXX() are part of the public or private API.
Tim Peters <tim.peters@gmail.com>
parents:
16257
diff
changeset
|
625 |
| 35008 | 626 case TYPE_BINARY_FLOAT: |
| 627 { | |
|
35126
70681267a74a
Fix signedness of various char variables to stop causing a warning under gcc 4.
Brett Cannon <bcannon@gmail.com>
parents:
35071
diff
changeset
|
628 unsigned char buf[8]; |
| 35008 | 629 double x; |
|
35126
70681267a74a
Fix signedness of various char variables to stop causing a warning under gcc 4.
Brett Cannon <bcannon@gmail.com>
parents:
35071
diff
changeset
|
630 if (r_string((char*)buf, 8, p) != 8) { |
| 35008 | 631 PyErr_SetString(PyExc_EOFError, |
| 632 "EOF read where object expected"); | |
|
41741
6f1293ab3ac4
Fix bug in marshal where bad data would cause a segfault due to
Neal Norwitz <nnorwitz@gmail.com>
parents:
39995
diff
changeset
|
633 retval = NULL; |
|
6f1293ab3ac4
Fix bug in marshal where bad data would cause a segfault due to
Neal Norwitz <nnorwitz@gmail.com>
parents:
39995
diff
changeset
|
634 break; |
| 35008 | 635 } |
| 636 x = _PyFloat_Unpack8(buf, 1); | |
| 637 if (x == -1.0 && PyErr_Occurred()) { | |
|
41741
6f1293ab3ac4
Fix bug in marshal where bad data would cause a segfault due to
Neal Norwitz <nnorwitz@gmail.com>
parents:
39995
diff
changeset
|
638 retval = NULL; |
|
6f1293ab3ac4
Fix bug in marshal where bad data would cause a segfault due to
Neal Norwitz <nnorwitz@gmail.com>
parents:
39995
diff
changeset
|
639 break; |
| 35008 | 640 } |
|
41741
6f1293ab3ac4
Fix bug in marshal where bad data would cause a segfault due to
Neal Norwitz <nnorwitz@gmail.com>
parents:
39995
diff
changeset
|
641 retval = PyFloat_FromDouble(x); |
|
6f1293ab3ac4
Fix bug in marshal where bad data would cause a segfault due to
Neal Norwitz <nnorwitz@gmail.com>
parents:
39995
diff
changeset
|
642 break; |
| 35008 | 643 } |
| 644 | |
|
3323
6098aaa8ae7f
changes for complex numbers
Guido van Rossum <guido@python.org>
parents:
3234
diff
changeset
|
645 #ifndef WITHOUT_COMPLEX |
|
6098aaa8ae7f
changes for complex numbers
Guido van Rossum <guido@python.org>
parents:
3234
diff
changeset
|
646 case TYPE_COMPLEX: |
|
6098aaa8ae7f
changes for complex numbers
Guido van Rossum <guido@python.org>
parents:
3234
diff
changeset
|
647 { |
|
6098aaa8ae7f
changes for complex numbers
Guido van Rossum <guido@python.org>
parents:
3234
diff
changeset
|
648 char buf[256]; |
|
3766
516490c8192c
Py_complex; and WITHOUT_COMPLEX added to getargs.c
Guido van Rossum <guido@python.org>
parents:
3731
diff
changeset
|
649 Py_complex c; |
|
3323
6098aaa8ae7f
changes for complex numbers
Guido van Rossum <guido@python.org>
parents:
3234
diff
changeset
|
650 n = r_byte(p); |
|
31824
331de8b9d1ed
Marshal clean-up (SF patch #873224)
Armin Rigo <arigo@tunes.org>
parents:
30311
diff
changeset
|
651 if (n == EOF || r_string(buf, (int)n, p) != n) { |
|
5373
5880384d7c71
Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
652 PyErr_SetString(PyExc_EOFError, |
|
3323
6098aaa8ae7f
changes for complex numbers
Guido van Rossum <guido@python.org>
parents:
3234
diff
changeset
|
653 "EOF read where object expected"); |
|
41741
6f1293ab3ac4
Fix bug in marshal where bad data would cause a segfault due to
Neal Norwitz <nnorwitz@gmail.com>
parents:
39995
diff
changeset
|
654 retval = NULL; |
|
6f1293ab3ac4
Fix bug in marshal where bad data would cause a segfault due to
Neal Norwitz <nnorwitz@gmail.com>
parents:
39995
diff
changeset
|
655 break; |
|
3323
6098aaa8ae7f
changes for complex numbers
Guido van Rossum <guido@python.org>
parents:
3234
diff
changeset
|
656 } |
|
6098aaa8ae7f
changes for complex numbers
Guido van Rossum <guido@python.org>
parents:
3234
diff
changeset
|
657 buf[n] = '\0'; |
|
41741
6f1293ab3ac4
Fix bug in marshal where bad data would cause a segfault due to
Neal Norwitz <nnorwitz@gmail.com>
parents:
39995
diff
changeset
|
658 retval = NULL; |
|
6f1293ab3ac4
Fix bug in marshal where bad data would cause a segfault due to
Neal Norwitz <nnorwitz@gmail.com>
parents:
39995
diff
changeset
|
659 PyFPE_START_PROTECT("atof", break;) |
|
32305
13656d58d450
Patch #774665: Make Python LC_NUMERIC agnostic.
Martin v. Löwis <martin@v.loewis.de>
parents:
31824
diff
changeset
|
660 c.real = PyOS_ascii_atof(buf); |
|
5197
74c12704a0bd
New form of PyFPE_END_PROTECT macro.
Guido van Rossum <guido@python.org>
parents:
5119
diff
changeset
|
661 PyFPE_END_PROTECT(c) |
|
3323
6098aaa8ae7f
changes for complex numbers
Guido van Rossum <guido@python.org>
parents:
3234
diff
changeset
|
662 n = r_byte(p); |
|
31824
331de8b9d1ed
Marshal clean-up (SF patch #873224)
Armin Rigo <arigo@tunes.org>
parents:
30311
diff
changeset
|
663 if (n == EOF || r_string(buf, (int)n, p) != n) { |
|
5373
5880384d7c71
Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
664 PyErr_SetString(PyExc_EOFError, |
|
3323
6098aaa8ae7f
changes for complex numbers
Guido van Rossum <guido@python.org>
parents:
3234
diff
changeset
|
665 "EOF read where object expected"); |
|
41741
6f1293ab3ac4
Fix bug in marshal where bad data would cause a segfault due to
Neal Norwitz <nnorwitz@gmail.com>
parents:
39995
diff
changeset
|
666 retval = NULL; |
|
6f1293ab3ac4
Fix bug in marshal where bad data would cause a segfault due to
Neal Norwitz <nnorwitz@gmail.com>
parents:
39995
diff
changeset
|
667 break; |
|
3323
6098aaa8ae7f
changes for complex numbers
Guido van Rossum <guido@python.org>
parents:
3234
diff
changeset
|
668 } |
|
6098aaa8ae7f
changes for complex numbers
Guido van Rossum <guido@python.org>
parents:
3234
diff
changeset
|
669 buf[n] = '\0'; |
|
41741
6f1293ab3ac4
Fix bug in marshal where bad data would cause a segfault due to
Neal Norwitz <nnorwitz@gmail.com>
parents:
39995
diff
changeset
|
670 PyFPE_START_PROTECT("atof", break) |
|
32305
13656d58d450
Patch #774665: Make Python LC_NUMERIC agnostic.
Martin v. Löwis <martin@v.loewis.de>
parents:
31824
diff
changeset
|
671 c.imag = PyOS_ascii_atof(buf); |
|
5197
74c12704a0bd
New form of PyFPE_END_PROTECT macro.
Guido van Rossum <guido@python.org>
parents:
5119
diff
changeset
|
672 PyFPE_END_PROTECT(c) |
|
41741
6f1293ab3ac4
Fix bug in marshal where bad data would cause a segfault due to
Neal Norwitz <nnorwitz@gmail.com>
parents:
39995
diff
changeset
|
673 retval = PyComplex_FromCComplex(c); |
|
6f1293ab3ac4
Fix bug in marshal where bad data would cause a segfault due to
Neal Norwitz <nnorwitz@gmail.com>
parents:
39995
diff
changeset
|
674 break; |
|
3323
6098aaa8ae7f
changes for complex numbers
Guido van Rossum <guido@python.org>
parents:
3234
diff
changeset
|
675 } |
| 35008 | 676 |
| 677 case TYPE_BINARY_COMPLEX: | |
| 678 { | |
|
35126
70681267a74a
Fix signedness of various char variables to stop causing a warning under gcc 4.
Brett Cannon <bcannon@gmail.com>
parents:
35071
diff
changeset
|
679 unsigned char buf[8]; |
| 35008 | 680 Py_complex c; |
|
35126
70681267a74a
Fix signedness of various char variables to stop causing a warning under gcc 4.
Brett Cannon <bcannon@gmail.com>
parents:
35071
diff
changeset
|
681 if (r_string((char*)buf, 8, p) != 8) { |
| 35008 | 682 PyErr_SetString(PyExc_EOFError, |
| 683 "EOF read where object expected"); | |
|
41741
6f1293ab3ac4
Fix bug in marshal where bad data would cause a segfault due to
Neal Norwitz <nnorwitz@gmail.com>
parents:
39995
diff
changeset
|
684 retval = NULL; |
|
6f1293ab3ac4
Fix bug in marshal where bad data would cause a segfault due to
Neal Norwitz <nnorwitz@gmail.com>
parents:
39995
diff
changeset
|
685 break; |
| 35008 | 686 } |
| 687 c.real = _PyFloat_Unpack8(buf, 1); | |
| 688 if (c.real == -1.0 && PyErr_Occurred()) { | |
|
41741
6f1293ab3ac4
Fix bug in marshal where bad data would cause a segfault due to
Neal Norwitz <nnorwitz@gmail.com>
parents:
39995
diff
changeset
|
689 retval = NULL; |
|
6f1293ab3ac4
Fix bug in marshal where bad data would cause a segfault due to
Neal Norwitz <nnorwitz@gmail.com>
parents:
39995
diff
changeset
|
690 break; |
| 35008 | 691 } |
|
35126
70681267a74a
Fix signedness of various char variables to stop causing a warning under gcc 4.
Brett Cannon <bcannon@gmail.com>
parents:
35071
diff
changeset
|
692 if (r_string((char*)buf, 8, p) != 8) { |
| 35008 | 693 PyErr_SetString(PyExc_EOFError, |
| 694 "EOF read where object expected"); | |
|
41741
6f1293ab3ac4
Fix bug in marshal where bad data would cause a segfault due to
Neal Norwitz <nnorwitz@gmail.com>
parents:
39995
diff
changeset
|
695 retval = NULL; |
|
6f1293ab3ac4
Fix bug in marshal where bad data would cause a segfault due to
Neal Norwitz <nnorwitz@gmail.com>
parents:
39995
diff
changeset
|
696 break; |
| 35008 | 697 } |
| 698 c.imag = _PyFloat_Unpack8(buf, 1); | |
| 699 if (c.imag == -1.0 && PyErr_Occurred()) { | |
|
41741
6f1293ab3ac4
Fix bug in marshal where bad data would cause a segfault due to
Neal Norwitz <nnorwitz@gmail.com>
parents:
39995
diff
changeset
|
700 retval = NULL; |
|
6f1293ab3ac4
Fix bug in marshal where bad data would cause a segfault due to
Neal Norwitz <nnorwitz@gmail.com>
parents:
39995
diff
changeset
|
701 break; |
| 35008 | 702 } |
|
41741
6f1293ab3ac4
Fix bug in marshal where bad data would cause a segfault due to
Neal Norwitz <nnorwitz@gmail.com>
parents:
39995
diff
changeset
|
703 retval = PyComplex_FromCComplex(c); |
|
6f1293ab3ac4
Fix bug in marshal where bad data would cause a segfault due to
Neal Norwitz <nnorwitz@gmail.com>
parents:
39995
diff
changeset
|
704 break; |
| 35008 | 705 } |
|
3323
6098aaa8ae7f
changes for complex numbers
Guido van Rossum <guido@python.org>
parents:
3234
diff
changeset
|
706 #endif |
|
16311
8c41f3861aad
It's unclear whether PyMarshal_XXX() are part of the public or private API.
Tim Peters <tim.peters@gmail.com>
parents:
16257
diff
changeset
|
707 |
|
32422
3df36e2e5ddc
Patch #923098: Share interned strings in marshal.
Martin v. Löwis <martin@v.loewis.de>
parents:
32343
diff
changeset
|
708 case TYPE_INTERNED: |
| 267 | 709 case TYPE_STRING: |
|
1191
38f0054f44a7
Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
710 n = r_long(p); |
|
39995
6ba402b60041
Forward-port of r52136,52138: a review of overflow-detecting code.
Armin Rigo <arigo@tunes.org>
parents:
39448
diff
changeset
|
711 if (n < 0 || n > INT_MAX) { |
|
8465
0e6fbbe3b63c
When unmarshalling, add test for negative lengths on strings, tuples
Guido van Rossum <guido@python.org>
parents:
8329
diff
changeset
|
712 PyErr_SetString(PyExc_ValueError, "bad marshal data"); |
|
41741
6f1293ab3ac4
Fix bug in marshal where bad data would cause a segfault due to
Neal Norwitz <nnorwitz@gmail.com>
parents:
39995
diff
changeset
|
713 retval = NULL; |
|
6f1293ab3ac4
Fix bug in marshal where bad data would cause a segfault due to
Neal Norwitz <nnorwitz@gmail.com>
parents:
39995
diff
changeset
|
714 break; |
|
8465
0e6fbbe3b63c
When unmarshalling, add test for negative lengths on strings, tuples
Guido van Rossum <guido@python.org>
parents:
8329
diff
changeset
|
715 } |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47294
diff
changeset
|
716 v = PyString_FromStringAndSize((char *)NULL, n); |
|
41741
6f1293ab3ac4
Fix bug in marshal where bad data would cause a segfault due to
Neal Norwitz <nnorwitz@gmail.com>
parents:
39995
diff
changeset
|
717 if (v == NULL) { |
|
6f1293ab3ac4
Fix bug in marshal where bad data would cause a segfault due to
Neal Norwitz <nnorwitz@gmail.com>
parents:
39995
diff
changeset
|
718 retval = NULL; |
|
6f1293ab3ac4
Fix bug in marshal where bad data would cause a segfault due to
Neal Norwitz <nnorwitz@gmail.com>
parents:
39995
diff
changeset
|
719 break; |
|
6f1293ab3ac4
Fix bug in marshal where bad data would cause a segfault due to
Neal Norwitz <nnorwitz@gmail.com>
parents:
39995
diff
changeset
|
720 } |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47294
diff
changeset
|
721 if (r_string(PyString_AS_STRING(v), (int)n, p) != n) { |
|
35010
8e9682fd57f8
Fix a couple of crashing-on-malformed data marshal bugs.
Michael W. Hudson <mwh@python.net>
parents:
35008
diff
changeset
|
722 Py_DECREF(v); |
|
8e9682fd57f8
Fix a couple of crashing-on-malformed data marshal bugs.
Michael W. Hudson <mwh@python.net>
parents:
35008
diff
changeset
|
723 PyErr_SetString(PyExc_EOFError, |
| 267 | 724 "EOF read where object expected"); |
|
41741
6f1293ab3ac4
Fix bug in marshal where bad data would cause a segfault due to
Neal Norwitz <nnorwitz@gmail.com>
parents:
39995
diff
changeset
|
725 retval = NULL; |
|
6f1293ab3ac4
Fix bug in marshal where bad data would cause a segfault due to
Neal Norwitz <nnorwitz@gmail.com>
parents:
39995
diff
changeset
|
726 break; |
| 267 | 727 } |
|
32422
3df36e2e5ddc
Patch #923098: Share interned strings in marshal.
Martin v. Löwis <martin@v.loewis.de>
parents:
32343
diff
changeset
|
728 if (type == TYPE_INTERNED) { |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47294
diff
changeset
|
729 PyString_InternInPlace(&v); |
|
41741
6f1293ab3ac4
Fix bug in marshal where bad data would cause a segfault due to
Neal Norwitz <nnorwitz@gmail.com>
parents:
39995
diff
changeset
|
730 if (PyList_Append(p->strings, v) < 0) { |
|
6f1293ab3ac4
Fix bug in marshal where bad data would cause a segfault due to
Neal Norwitz <nnorwitz@gmail.com>
parents:
39995
diff
changeset
|
731 retval = NULL; |
|
6f1293ab3ac4
Fix bug in marshal where bad data would cause a segfault due to
Neal Norwitz <nnorwitz@gmail.com>
parents:
39995
diff
changeset
|
732 break; |
|
6f1293ab3ac4
Fix bug in marshal where bad data would cause a segfault due to
Neal Norwitz <nnorwitz@gmail.com>
parents:
39995
diff
changeset
|
733 } |
|
32422
3df36e2e5ddc
Patch #923098: Share interned strings in marshal.
Martin v. Löwis <martin@v.loewis.de>
parents:
32343
diff
changeset
|
734 } |
|
41741
6f1293ab3ac4
Fix bug in marshal where bad data would cause a segfault due to
Neal Norwitz <nnorwitz@gmail.com>
parents:
39995
diff
changeset
|
735 retval = v; |
|
6f1293ab3ac4
Fix bug in marshal where bad data would cause a segfault due to
Neal Norwitz <nnorwitz@gmail.com>
parents:
39995
diff
changeset
|
736 break; |
|
32422
3df36e2e5ddc
Patch #923098: Share interned strings in marshal.
Martin v. Löwis <martin@v.loewis.de>
parents:
32343
diff
changeset
|
737 |
|
3df36e2e5ddc
Patch #923098: Share interned strings in marshal.
Martin v. Löwis <martin@v.loewis.de>
parents:
32343
diff
changeset
|
738 case TYPE_STRINGREF: |
|
3df36e2e5ddc
Patch #923098: Share interned strings in marshal.
Martin v. Löwis <martin@v.loewis.de>
parents:
32343
diff
changeset
|
739 n = r_long(p); |
| 35071 | 740 if (n < 0 || n >= PyList_GET_SIZE(p->strings)) { |
| 741 PyErr_SetString(PyExc_ValueError, "bad marshal data"); | |
|
41741
6f1293ab3ac4
Fix bug in marshal where bad data would cause a segfault due to
Neal Norwitz <nnorwitz@gmail.com>
parents:
39995
diff
changeset
|
742 retval = NULL; |
|
6f1293ab3ac4
Fix bug in marshal where bad data would cause a segfault due to
Neal Norwitz <nnorwitz@gmail.com>
parents:
39995
diff
changeset
|
743 break; |
| 35071 | 744 } |
|
32422
3df36e2e5ddc
Patch #923098: Share interned strings in marshal.
Martin v. Löwis <martin@v.loewis.de>
parents:
32343
diff
changeset
|
745 v = PyList_GET_ITEM(p->strings, n); |
|
3df36e2e5ddc
Patch #923098: Share interned strings in marshal.
Martin v. Löwis <martin@v.loewis.de>
parents:
32343
diff
changeset
|
746 Py_INCREF(v); |
|
41741
6f1293ab3ac4
Fix bug in marshal where bad data would cause a segfault due to
Neal Norwitz <nnorwitz@gmail.com>
parents:
39995
diff
changeset
|
747 retval = v; |
|
6f1293ab3ac4
Fix bug in marshal where bad data would cause a segfault due to
Neal Norwitz <nnorwitz@gmail.com>
parents:
39995
diff
changeset
|
748 break; |
|
16311
8c41f3861aad
It's unclear whether PyMarshal_XXX() are part of the public or private API.
Tim Peters <tim.peters@gmail.com>
parents:
16257
diff
changeset
|
749 |
|
19343
7257e87e0720
Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents:
18148
diff
changeset
|
750 #ifdef Py_USING_UNICODE |
|
11976
513354d87a85
Marc-Andre Lemburg: support marshalling Unicode objects (code 'u').
Guido van Rossum <guido@python.org>
parents:
9300
diff
changeset
|
751 case TYPE_UNICODE: |
|
513354d87a85
Marc-Andre Lemburg: support marshalling Unicode objects (code 'u').
Guido van Rossum <guido@python.org>
parents:
9300
diff
changeset
|
752 { |
|
513354d87a85
Marc-Andre Lemburg: support marshalling Unicode objects (code 'u').
Guido van Rossum <guido@python.org>
parents:
9300
diff
changeset
|
753 char *buffer; |
|
513354d87a85
Marc-Andre Lemburg: support marshalling Unicode objects (code 'u').
Guido van Rossum <guido@python.org>
parents:
9300
diff
changeset
|
754 |
|
513354d87a85
Marc-Andre Lemburg: support marshalling Unicode objects (code 'u').
Guido van Rossum <guido@python.org>
parents:
9300
diff
changeset
|
755 n = r_long(p); |
|
39995
6ba402b60041
Forward-port of r52136,52138: a review of overflow-detecting code.
Armin Rigo <arigo@tunes.org>
parents:
39448
diff
changeset
|
756 if (n < 0 || n > INT_MAX) { |
|
11976
513354d87a85
Marc-Andre Lemburg: support marshalling Unicode objects (code 'u').
Guido van Rossum <guido@python.org>
parents:
9300
diff
changeset
|
757 PyErr_SetString(PyExc_ValueError, "bad marshal data"); |
|
41741
6f1293ab3ac4
Fix bug in marshal where bad data would cause a segfault due to
Neal Norwitz <nnorwitz@gmail.com>
parents:
39995
diff
changeset
|
758 retval = NULL; |
|
6f1293ab3ac4
Fix bug in marshal where bad data would cause a segfault due to
Neal Norwitz <nnorwitz@gmail.com>
parents:
39995
diff
changeset
|
759 break; |
|
11976
513354d87a85
Marc-Andre Lemburg: support marshalling Unicode objects (code 'u').
Guido van Rossum <guido@python.org>
parents:
9300
diff
changeset
|
760 } |
|
12576
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12135
diff
changeset
|
761 buffer = PyMem_NEW(char, n); |
|
41741
6f1293ab3ac4
Fix bug in marshal where bad data would cause a segfault due to
Neal Norwitz <nnorwitz@gmail.com>
parents:
39995
diff
changeset
|
762 if (buffer == NULL) { |
|
6f1293ab3ac4
Fix bug in marshal where bad data would cause a segfault due to
Neal Norwitz <nnorwitz@gmail.com>
parents:
39995
diff
changeset
|
763 retval = PyErr_NoMemory(); |
|
6f1293ab3ac4
Fix bug in marshal where bad data would cause a segfault due to
Neal Norwitz <nnorwitz@gmail.com>
parents:
39995
diff
changeset
|
764 break; |
|
6f1293ab3ac4
Fix bug in marshal where bad data would cause a segfault due to
Neal Norwitz <nnorwitz@gmail.com>
parents:
39995
diff
changeset
|
765 } |
|
11976
513354d87a85
Marc-Andre Lemburg: support marshalling Unicode objects (code 'u').
Guido van Rossum <guido@python.org>
parents:
9300
diff
changeset
|
766 if (r_string(buffer, (int)n, p) != n) { |
|
12576
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12135
diff
changeset
|
767 PyMem_DEL(buffer); |
|
11976
513354d87a85
Marc-Andre Lemburg: support marshalling Unicode objects (code 'u').
Guido van Rossum <guido@python.org>
parents:
9300
diff
changeset
|
768 PyErr_SetString(PyExc_EOFError, |
|
513354d87a85
Marc-Andre Lemburg: support marshalling Unicode objects (code 'u').
Guido van Rossum <guido@python.org>
parents:
9300
diff
changeset
|
769 "EOF read where object expected"); |
|
41741
6f1293ab3ac4
Fix bug in marshal where bad data would cause a segfault due to
Neal Norwitz <nnorwitz@gmail.com>
parents:
39995
diff
changeset
|
770 retval = NULL; |
|
6f1293ab3ac4
Fix bug in marshal where bad data would cause a segfault due to
Neal Norwitz <nnorwitz@gmail.com>
parents:
39995
diff
changeset
|
771 break; |
|
11976
513354d87a85
Marc-Andre Lemburg: support marshalling Unicode objects (code 'u').
Guido van Rossum <guido@python.org>
parents:
9300
diff
changeset
|
772 } |
|
513354d87a85
Marc-Andre Lemburg: support marshalling Unicode objects (code 'u').
Guido van Rossum <guido@python.org>
parents:
9300
diff
changeset
|
773 v = PyUnicode_DecodeUTF8(buffer, n, NULL); |
|
12576
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12135
diff
changeset
|
774 PyMem_DEL(buffer); |
|
41741
6f1293ab3ac4
Fix bug in marshal where bad data would cause a segfault due to
Neal Norwitz <nnorwitz@gmail.com>
parents:
39995
diff
changeset
|
775 retval = v; |
|
6f1293ab3ac4
Fix bug in marshal where bad data would cause a segfault due to
Neal Norwitz <nnorwitz@gmail.com>
parents:
39995
diff
changeset
|
776 break; |
|
11976
513354d87a85
Marc-Andre Lemburg: support marshalling Unicode objects (code 'u').
Guido van Rossum <guido@python.org>
parents:
9300
diff
changeset
|
777 } |
|
19343
7257e87e0720
Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents:
18148
diff
changeset
|
778 #endif |
|
16311
8c41f3861aad
It's unclear whether PyMarshal_XXX() are part of the public or private API.
Tim Peters <tim.peters@gmail.com>
parents:
16257
diff
changeset
|
779 |
| 267 | 780 case TYPE_TUPLE: |
|
1191
38f0054f44a7
Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
781 n = r_long(p); |
|
39995
6ba402b60041
Forward-port of r52136,52138: a review of overflow-detecting code.
Armin Rigo <arigo@tunes.org>
parents:
39448
diff
changeset
|
782 if (n < 0 || n > INT_MAX) { |
|
8465
0e6fbbe3b63c
When unmarshalling, add test for negative lengths on strings, tuples
Guido van Rossum <guido@python.org>
parents:
8329
diff
changeset
|
783 PyErr_SetString(PyExc_ValueError, "bad marshal data"); |
|
41741
6f1293ab3ac4
Fix bug in marshal where bad data would cause a segfault due to
Neal Norwitz <nnorwitz@gmail.com>
parents:
39995
diff
changeset
|
784 retval = NULL; |
|
6f1293ab3ac4
Fix bug in marshal where bad data would cause a segfault due to
Neal Norwitz <nnorwitz@gmail.com>
parents:
39995
diff
changeset
|
785 break; |
|
8465
0e6fbbe3b63c
When unmarshalling, add test for negative lengths on strings, tuples
Guido van Rossum <guido@python.org>
parents:
8329
diff
changeset
|
786 } |
|
5373
5880384d7c71
Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
787 v = PyTuple_New((int)n); |
|
41741
6f1293ab3ac4
Fix bug in marshal where bad data would cause a segfault due to
Neal Norwitz <nnorwitz@gmail.com>
parents:
39995
diff
changeset
|
788 if (v == NULL) { |
|
6f1293ab3ac4
Fix bug in marshal where bad data would cause a segfault due to
Neal Norwitz <nnorwitz@gmail.com>
parents:
39995
diff
changeset
|
789 retval = NULL; |
|
6f1293ab3ac4
Fix bug in marshal where bad data would cause a segfault due to
Neal Norwitz <nnorwitz@gmail.com>
parents:
39995
diff
changeset
|
790 break; |
|
6f1293ab3ac4
Fix bug in marshal where bad data would cause a segfault due to
Neal Norwitz <nnorwitz@gmail.com>
parents:
39995
diff
changeset
|
791 } |
|
3234
aafe7d634985
Made a bit more robust against out-of-memory situations
Jack Jansen <jack.jansen@cwi.nl>
parents:
3012
diff
changeset
|
792 for (i = 0; i < n; i++) { |
|
aafe7d634985
Made a bit more robust against out-of-memory situations
Jack Jansen <jack.jansen@cwi.nl>
parents:
3012
diff
changeset
|
793 v2 = r_object(p); |
|
aafe7d634985
Made a bit more robust against out-of-memory situations
Jack Jansen <jack.jansen@cwi.nl>
parents:
3012
diff
changeset
|
794 if ( v2 == NULL ) { |
|
31824
331de8b9d1ed
Marshal clean-up (SF patch #873224)
Armin Rigo <arigo@tunes.org>
parents:
30311
diff
changeset
|
795 if (!PyErr_Occurred()) |
|
331de8b9d1ed
Marshal clean-up (SF patch #873224)
Armin Rigo <arigo@tunes.org>
parents:
30311
diff
changeset
|
796 PyErr_SetString(PyExc_TypeError, |
|
331de8b9d1ed
Marshal clean-up (SF patch #873224)
Armin Rigo <arigo@tunes.org>
parents:
30311
diff
changeset
|
797 "NULL object in marshal data"); |
|
5373
5880384d7c71
Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
798 Py_DECREF(v); |
|
3234
aafe7d634985
Made a bit more robust against out-of-memory situations
Jack Jansen <jack.jansen@cwi.nl>
parents:
3012
diff
changeset
|
799 v = NULL; |
|
aafe7d634985
Made a bit more robust against out-of-memory situations
Jack Jansen <jack.jansen@cwi.nl>
parents:
3012
diff
changeset
|
800 break; |
|
aafe7d634985
Made a bit more robust against out-of-memory situations
Jack Jansen <jack.jansen@cwi.nl>
parents:
3012
diff
changeset
|
801 } |
|
5373
5880384d7c71
Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
802 PyTuple_SET_ITEM(v, (int)i, v2); |
|
3234
aafe7d634985
Made a bit more robust against out-of-memory situations
Jack Jansen <jack.jansen@cwi.nl>
parents:
3012
diff
changeset
|
803 } |
|
41741
6f1293ab3ac4
Fix bug in marshal where bad data would cause a segfault due to
Neal Norwitz <nnorwitz@gmail.com>
parents:
39995
diff
changeset
|
804 retval = v; |
|
6f1293ab3ac4
Fix bug in marshal where bad data would cause a segfault due to
Neal Norwitz <nnorwitz@gmail.com>
parents:
39995
diff
changeset
|
805 break; |
|
16311
8c41f3861aad
It's unclear whether PyMarshal_XXX() are part of the public or private API.
Tim Peters <tim.peters@gmail.com>
parents:
16257
diff
changeset
|
806 |
| 267 | 807 case TYPE_LIST: |
|
1191
38f0054f44a7
Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
808 n = r_long(p); |
|
39995
6ba402b60041
Forward-port of r52136,52138: a review of overflow-detecting code.
Armin Rigo <arigo@tunes.org>
parents:
39448
diff
changeset
|
809 if (n < 0 || n > INT_MAX) { |
|
8465
0e6fbbe3b63c
When unmarshalling, add test for negative lengths on strings, tuples
Guido van Rossum <guido@python.org>
parents:
8329
diff
changeset
|
810 PyErr_SetString(PyExc_ValueError, "bad marshal data"); |
|
41741
6f1293ab3ac4
Fix bug in marshal where bad data would cause a segfault due to
Neal Norwitz <nnorwitz@gmail.com>
parents:
39995
diff
changeset
|
811 retval = NULL; |
|
6f1293ab3ac4
Fix bug in marshal where bad data would cause a segfault due to
Neal Norwitz <nnorwitz@gmail.com>
parents:
39995
diff
changeset
|
812 break; |
|
8465
0e6fbbe3b63c
When unmarshalling, add test for negative lengths on strings, tuples
Guido van Rossum <guido@python.org>
parents:
8329
diff
changeset
|
813 } |
|
5373
5880384d7c71
Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
814 v = PyList_New((int)n); |
|
41741
6f1293ab3ac4
Fix bug in marshal where bad data would cause a segfault due to
Neal Norwitz <nnorwitz@gmail.com>
parents:
39995
diff
changeset
|
815 if (v == NULL) { |
|
6f1293ab3ac4
Fix bug in marshal where bad data would cause a segfault due to
Neal Norwitz <nnorwitz@gmail.com>
parents:
39995
diff
changeset
|
816 retval = NULL; |
|
6f1293ab3ac4
Fix bug in marshal where bad data would cause a segfault due to
Neal Norwitz <nnorwitz@gmail.com>
parents:
39995
diff
changeset
|
817 break; |
|
6f1293ab3ac4
Fix bug in marshal where bad data would cause a segfault due to
Neal Norwitz <nnorwitz@gmail.com>
parents:
39995
diff
changeset
|
818 } |
|
3234
aafe7d634985
Made a bit more robust against out-of-memory situations
Jack Jansen <jack.jansen@cwi.nl>
parents:
3012
diff
changeset
|
819 for (i = 0; i < n; i++) { |
|
aafe7d634985
Made a bit more robust against out-of-memory situations
Jack Jansen <jack.jansen@cwi.nl>
parents:
3012
diff
changeset
|
820 v2 = r_object(p); |
|
aafe7d634985
Made a bit more robust against out-of-memory situations
Jack Jansen <jack.jansen@cwi.nl>
parents:
3012
diff
changeset
|
821 if ( v2 == NULL ) { |
|
31824
331de8b9d1ed
Marshal clean-up (SF patch #873224)
Armin Rigo <arigo@tunes.org>
parents:
30311
diff
changeset
|
822 if (!PyErr_Occurred()) |
|
331de8b9d1ed
Marshal clean-up (SF patch #873224)
Armin Rigo <arigo@tunes.org>
parents:
30311
diff
changeset
|
823 PyErr_SetString(PyExc_TypeError, |
|
331de8b9d1ed
Marshal clean-up (SF patch #873224)
Armin Rigo <arigo@tunes.org>
parents:
30311
diff
changeset
|
824 "NULL object in marshal data"); |
|
5373
5880384d7c71
Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
825 Py_DECREF(v); |
|
3234
aafe7d634985
Made a bit more robust against out-of-memory situations
Jack Jansen <jack.jansen@cwi.nl>
parents:
3012
diff
changeset
|
826 v = NULL; |
|
aafe7d634985
Made a bit more robust against out-of-memory situations
Jack Jansen <jack.jansen@cwi.nl>
parents:
3012
diff
changeset
|
827 break; |
|
aafe7d634985
Made a bit more robust against out-of-memory situations
Jack Jansen <jack.jansen@cwi.nl>
parents:
3012
diff
changeset
|
828 } |
|
41741
6f1293ab3ac4
Fix bug in marshal where bad data would cause a segfault due to
Neal Norwitz <nnorwitz@gmail.com>
parents:
39995
diff
changeset
|
829 PyList_SET_ITEM(v, (int)i, v2); |
|
3234
aafe7d634985
Made a bit more robust against out-of-memory situations
Jack Jansen <jack.jansen@cwi.nl>
parents:
3012
diff
changeset
|
830 } |
|
41741
6f1293ab3ac4
Fix bug in marshal where bad data would cause a segfault due to
Neal Norwitz <nnorwitz@gmail.com>
parents:
39995
diff
changeset
|
831 retval = v; |
|
6f1293ab3ac4
Fix bug in marshal where bad data would cause a segfault due to
Neal Norwitz <nnorwitz@gmail.com>
parents:
39995
diff
changeset
|
832 break; |
|
16311
8c41f3861aad
It's unclear whether PyMarshal_XXX() are part of the public or private API.
Tim Peters <tim.peters@gmail.com>
parents:
16257
diff
changeset
|
833 |
|
279
9c924f9c3b16
Add marshalling for dictionaries.
Guido van Rossum <guido@python.org>
parents:
272
diff
changeset
|
834 case TYPE_DICT: |
|
5373
5880384d7c71
Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
835 v = PyDict_New(); |
|
41741
6f1293ab3ac4
Fix bug in marshal where bad data would cause a segfault due to
Neal Norwitz <nnorwitz@gmail.com>
parents:
39995
diff
changeset
|
836 if (v == NULL) { |
|
6f1293ab3ac4
Fix bug in marshal where bad data would cause a segfault due to
Neal Norwitz <nnorwitz@gmail.com>
parents:
39995
diff
changeset
|
837 retval = NULL; |
|
6f1293ab3ac4
Fix bug in marshal where bad data would cause a segfault due to
Neal Norwitz <nnorwitz@gmail.com>
parents:
39995
diff
changeset
|
838 break; |
|
6f1293ab3ac4
Fix bug in marshal where bad data would cause a segfault due to
Neal Norwitz <nnorwitz@gmail.com>
parents:
39995
diff
changeset
|
839 } |
|
279
9c924f9c3b16
Add marshalling for dictionaries.
Guido van Rossum <guido@python.org>
parents:
272
diff
changeset
|
840 for (;;) { |
|
5373
5880384d7c71
Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
841 PyObject *key, *val; |
|
1191
38f0054f44a7
Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
842 key = r_object(p); |
|
279
9c924f9c3b16
Add marshalling for dictionaries.
Guido van Rossum <guido@python.org>
parents:
272
diff
changeset
|
843 if (key == NULL) |
|
31824
331de8b9d1ed
Marshal clean-up (SF patch #873224)
Armin Rigo <arigo@tunes.org>
parents:
30311
diff
changeset
|
844 break; |
|
1191
38f0054f44a7
Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
845 val = r_object(p); |
|
3731
de1cb33219e3
Jeremy's patches for more robust handling of unmarshallable types.
Guido van Rossum <guido@python.org>
parents:
3620
diff
changeset
|
846 if (val != NULL) |
|
5373
5880384d7c71
Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
847 PyDict_SetItem(v, key, val); |
|
5880384d7c71
Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
848 Py_DECREF(key); |
|
5880384d7c71
Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
849 Py_XDECREF(val); |
|
279
9c924f9c3b16
Add marshalling for dictionaries.
Guido van Rossum <guido@python.org>
parents:
272
diff
changeset
|
850 } |
|
31824
331de8b9d1ed
Marshal clean-up (SF patch #873224)
Armin Rigo <arigo@tunes.org>
parents:
30311
diff
changeset
|
851 if (PyErr_Occurred()) { |
|
331de8b9d1ed
Marshal clean-up (SF patch #873224)
Armin Rigo <arigo@tunes.org>
parents:
30311
diff
changeset
|
852 Py_DECREF(v); |
|
331de8b9d1ed
Marshal clean-up (SF patch #873224)
Armin Rigo <arigo@tunes.org>
parents:
30311
diff
changeset
|
853 v = NULL; |
|
331de8b9d1ed
Marshal clean-up (SF patch #873224)
Armin Rigo <arigo@tunes.org>
parents:
30311
diff
changeset
|
854 } |
|
41741
6f1293ab3ac4
Fix bug in marshal where bad data would cause a segfault due to
Neal Norwitz <nnorwitz@gmail.com>
parents:
39995
diff
changeset
|
855 retval = v; |
|
6f1293ab3ac4
Fix bug in marshal where bad data would cause a segfault due to
Neal Norwitz <nnorwitz@gmail.com>
parents:
39995
diff
changeset
|
856 break; |
|
16311
8c41f3861aad
It's unclear whether PyMarshal_XXX() are part of the public or private API.
Tim Peters <tim.peters@gmail.com>
parents:
16257
diff
changeset
|
857 |
|
34394
b480abaf1f6f
SF 1098985: set objects cannot be marshalled
Raymond Hettinger <python@rcn.com>
parents:
34205
diff
changeset
|
858 case TYPE_SET: |
|
b480abaf1f6f
SF 1098985: set objects cannot be marshalled
Raymond Hettinger <python@rcn.com>
parents:
34205
diff
changeset
|
859 case TYPE_FROZENSET: |
|
b480abaf1f6f
SF 1098985: set objects cannot be marshalled
Raymond Hettinger <python@rcn.com>
parents:
34205
diff
changeset
|
860 n = r_long(p); |
|
41741
6f1293ab3ac4
Fix bug in marshal where bad data would cause a segfault due to
Neal Norwitz <nnorwitz@gmail.com>
parents:
39995
diff
changeset
|
861 if (n < 0 || n > INT_MAX) { |
|
34394
b480abaf1f6f
SF 1098985: set objects cannot be marshalled
Raymond Hettinger <python@rcn.com>
parents:
34205
diff
changeset
|
862 PyErr_SetString(PyExc_ValueError, "bad marshal data"); |
|
41741
6f1293ab3ac4
Fix bug in marshal where bad data would cause a segfault due to
Neal Norwitz <nnorwitz@gmail.com>
parents:
39995
diff
changeset
|
863 retval = NULL; |
|
6f1293ab3ac4
Fix bug in marshal where bad data would cause a segfault due to
Neal Norwitz <nnorwitz@gmail.com>
parents:
39995
diff
changeset
|
864 break; |
|
34394
b480abaf1f6f
SF 1098985: set objects cannot be marshalled
Raymond Hettinger <python@rcn.com>
parents:
34205
diff
changeset
|
865 } |
|
45036
1e583eb71b3f
Let marshal built-up sets and frozensets one element at a time (without creating an intermediate tuple).
Raymond Hettinger <python@rcn.com>
parents:
44964
diff
changeset
|
866 v = (type == TYPE_SET) ? PySet_New(NULL) : PyFrozenSet_New(NULL); |
|
41741
6f1293ab3ac4
Fix bug in marshal where bad data would cause a segfault due to
Neal Norwitz <nnorwitz@gmail.com>
parents:
39995
diff
changeset
|
867 if (v == NULL) { |
|
6f1293ab3ac4
Fix bug in marshal where bad data would cause a segfault due to
Neal Norwitz <nnorwitz@gmail.com>
parents:
39995
diff
changeset
|
868 retval = NULL; |
|
6f1293ab3ac4
Fix bug in marshal where bad data would cause a segfault due to
Neal Norwitz <nnorwitz@gmail.com>
parents:
39995
diff
changeset
|
869 break; |
|
6f1293ab3ac4
Fix bug in marshal where bad data would cause a segfault due to
Neal Norwitz <nnorwitz@gmail.com>
parents:
39995
diff
changeset
|
870 } |
|
34394
b480abaf1f6f
SF 1098985: set objects cannot be marshalled
Raymond Hettinger <python@rcn.com>
parents:
34205
diff
changeset
|
871 for (i = 0; i < n; i++) { |
|
b480abaf1f6f
SF 1098985: set objects cannot be marshalled
Raymond Hettinger <python@rcn.com>
parents:
34205
diff
changeset
|
872 v2 = r_object(p); |
|
b480abaf1f6f
SF 1098985: set objects cannot be marshalled
Raymond Hettinger <python@rcn.com>
parents:
34205
diff
changeset
|
873 if ( v2 == NULL ) { |
|
b480abaf1f6f
SF 1098985: set objects cannot be marshalled
Raymond Hettinger <python@rcn.com>
parents:
34205
diff
changeset
|
874 if (!PyErr_Occurred()) |
|
b480abaf1f6f
SF 1098985: set objects cannot be marshalled
Raymond Hettinger <python@rcn.com>
parents:
34205
diff
changeset
|
875 PyErr_SetString(PyExc_TypeError, |
|
b480abaf1f6f
SF 1098985: set objects cannot be marshalled
Raymond Hettinger <python@rcn.com>
parents:
34205
diff
changeset
|
876 "NULL object in marshal data"); |
|
b480abaf1f6f
SF 1098985: set objects cannot be marshalled
Raymond Hettinger <python@rcn.com>
parents:
34205
diff
changeset
|
877 Py_DECREF(v); |
|
b480abaf1f6f
SF 1098985: set objects cannot be marshalled
Raymond Hettinger <python@rcn.com>
parents:
34205
diff
changeset
|
878 v = NULL; |
|
b480abaf1f6f
SF 1098985: set objects cannot be marshalled
Raymond Hettinger <python@rcn.com>
parents:
34205
diff
changeset
|
879 break; |
|
b480abaf1f6f
SF 1098985: set objects cannot be marshalled
Raymond Hettinger <python@rcn.com>
parents:
34205
diff
changeset
|
880 } |
|
45036
1e583eb71b3f
Let marshal built-up sets and frozensets one element at a time (without creating an intermediate tuple).
Raymond Hettinger <python@rcn.com>
parents:
44964
diff
changeset
|
881 if (PySet_Add(v, v2) == -1) { |
|
1e583eb71b3f
Let marshal built-up sets and frozensets one element at a time (without creating an intermediate tuple).
Raymond Hettinger <python@rcn.com>
parents:
44964
diff
changeset
|
882 Py_DECREF(v); |
|
1e583eb71b3f
Let marshal built-up sets and frozensets one element at a time (without creating an intermediate tuple).
Raymond Hettinger <python@rcn.com>
parents:
44964
diff
changeset
|
883 Py_DECREF(v2); |
|
1e583eb71b3f
Let marshal built-up sets and frozensets one element at a time (without creating an intermediate tuple).
Raymond Hettinger <python@rcn.com>
parents:
44964
diff
changeset
|
884 v = NULL; |
|
1e583eb71b3f
Let marshal built-up sets and frozensets one element at a time (without creating an intermediate tuple).
Raymond Hettinger <python@rcn.com>
parents:
44964
diff
changeset
|
885 break; |
|
1e583eb71b3f
Let marshal built-up sets and frozensets one element at a time (without creating an intermediate tuple).
Raymond Hettinger <python@rcn.com>
parents:
44964
diff
changeset
|
886 } |
| 45081 | 887 Py_DECREF(v2); |
|
41741
6f1293ab3ac4
Fix bug in marshal where bad data would cause a segfault due to
Neal Norwitz <nnorwitz@gmail.com>
parents:
39995
diff
changeset
|
888 } |
|
45039
7a6a80e84a8a
Removed unnecessary conditional (spotted by Neal Norwitz).
Raymond Hettinger <python@rcn.com>
parents:
45036
diff
changeset
|
889 retval = v; |
|
41741
6f1293ab3ac4
Fix bug in marshal where bad data would cause a segfault due to
Neal Norwitz <nnorwitz@gmail.com>
parents:
39995
diff
changeset
|
890 break; |
|
34394
b480abaf1f6f
SF 1098985: set objects cannot be marshalled
Raymond Hettinger <python@rcn.com>
parents:
34205
diff
changeset
|
891 |
| 267 | 892 case TYPE_CODE: |
|
19605
10bf4bed5096
fix for part of bug #453523: disable unmarshalling of code objects in
Michael W. Hudson <mwh@python.net>
parents:
19547
diff
changeset
|
893 if (PyEval_GetRestricted()) { |
|
10bf4bed5096
fix for part of bug #453523: disable unmarshalling of code objects in
Michael W. Hudson <mwh@python.net>
parents:
19547
diff
changeset
|
894 PyErr_SetString(PyExc_RuntimeError, |
|
10bf4bed5096
fix for part of bug #453523: disable unmarshalling of code objects in
Michael W. Hudson <mwh@python.net>
parents:
19547
diff
changeset
|
895 "cannot unmarshal code objects in " |
|
10bf4bed5096
fix for part of bug #453523: disable unmarshalling of code objects in
Michael W. Hudson <mwh@python.net>
parents:
19547
diff
changeset
|
896 "restricted execution mode"); |
|
41741
6f1293ab3ac4
Fix bug in marshal where bad data would cause a segfault due to
Neal Norwitz <nnorwitz@gmail.com>
parents:
39995
diff
changeset
|
897 retval = NULL; |
|
6f1293ab3ac4
Fix bug in marshal where bad data would cause a segfault due to
Neal Norwitz <nnorwitz@gmail.com>
parents:
39995
diff
changeset
|
898 break; |
|
19605
10bf4bed5096
fix for part of bug #453523: disable unmarshalling of code objects in
Michael W. Hudson <mwh@python.net>
parents:
19547
diff
changeset
|
899 } |
|
10bf4bed5096
fix for part of bug #453523: disable unmarshalling of code objects in
Michael W. Hudson <mwh@python.net>
parents:
19547
diff
changeset
|
900 else { |
| 35008 | 901 int argcount; |
| 902 int nlocals; | |
| 903 int stacksize; | |
| 904 int flags; | |
| 905 PyObject *code = NULL; | |
| 906 PyObject *consts = NULL; | |
| 907 PyObject *names = NULL; | |
| 908 PyObject *varnames = NULL; | |
| 909 PyObject *freevars = NULL; | |
| 910 PyObject *cellvars = NULL; | |
| 911 PyObject *filename = NULL; | |
| 912 PyObject *name = NULL; | |
| 913 int firstlineno; | |
| 914 PyObject *lnotab = NULL; | |
| 915 | |
| 916 v = NULL; | |
|
16311
8c41f3861aad
It's unclear whether PyMarshal_XXX() are part of the public or private API.
Tim Peters <tim.peters@gmail.com>
parents:
16257
diff
changeset
|
917 |
|
39995
6ba402b60041
Forward-port of r52136,52138: a review of overflow-detecting code.
Armin Rigo <arigo@tunes.org>
parents:
39448
diff
changeset
|
918 /* XXX ignore long->int overflows for now */ |
|
6ba402b60041
Forward-port of r52136,52138: a review of overflow-detecting code.
Armin Rigo <arigo@tunes.org>
parents:
39448
diff
changeset
|
919 argcount = (int)r_long(p); |
|
6ba402b60041
Forward-port of r52136,52138: a review of overflow-detecting code.
Armin Rigo <arigo@tunes.org>
parents:
39448
diff
changeset
|
920 nlocals = (int)r_long(p); |
|
6ba402b60041
Forward-port of r52136,52138: a review of overflow-detecting code.
Armin Rigo <arigo@tunes.org>
parents:
39448
diff
changeset
|
921 stacksize = (int)r_long(p); |
|
6ba402b60041
Forward-port of r52136,52138: a review of overflow-detecting code.
Armin Rigo <arigo@tunes.org>
parents:
39448
diff
changeset
|
922 flags = (int)r_long(p); |
| 35008 | 923 code = r_object(p); |
| 924 if (code == NULL) | |
| 925 goto code_error; | |
| 926 consts = r_object(p); | |
| 927 if (consts == NULL) | |
| 928 goto code_error; | |
| 929 names = r_object(p); | |
| 930 if (names == NULL) | |
| 931 goto code_error; | |
| 932 varnames = r_object(p); | |
| 933 if (varnames == NULL) | |
| 934 goto code_error; | |
| 935 freevars = r_object(p); | |
| 936 if (freevars == NULL) | |
| 937 goto code_error; | |
| 938 cellvars = r_object(p); | |
| 939 if (cellvars == NULL) | |
| 940 goto code_error; | |
| 941 filename = r_object(p); | |
| 942 if (filename == NULL) | |
| 943 goto code_error; | |
| 944 name = r_object(p); | |
| 945 if (name == NULL) | |
| 946 goto code_error; | |
|
39995
6ba402b60041
Forward-port of r52136,52138: a review of overflow-detecting code.
Armin Rigo <arigo@tunes.org>
parents:
39448
diff
changeset
|
947 firstlineno = (int)r_long(p); |
| 35008 | 948 lnotab = r_object(p); |
| 949 if (lnotab == NULL) | |
| 950 goto code_error; | |
| 951 | |
| 952 v = (PyObject *) PyCode_New( | |
|
16311
8c41f3861aad
It's unclear whether PyMarshal_XXX() are part of the public or private API.
Tim Peters <tim.peters@gmail.com>
parents:
16257
diff
changeset
|
953 argcount, nlocals, stacksize, flags, |
|
2844
0261bf5b3819
keyword arguments and faster calls
Guido van Rossum <guido@python.org>
parents:
2503
diff
changeset
|
954 code, consts, names, varnames, |
|
16311
8c41f3861aad
It's unclear whether PyMarshal_XXX() are part of the public or private API.
Tim Peters <tim.peters@gmail.com>
parents:
16257
diff
changeset
|
955 freevars, cellvars, filename, name, |
|
8c41f3861aad
It's unclear whether PyMarshal_XXX() are part of the public or private API.
Tim Peters <tim.peters@gmail.com>
parents:
16257
diff
changeset
|
956 firstlineno, lnotab); |
| 35008 | 957 |
| 958 code_error: | |
|
5373
5880384d7c71
Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
959 Py_XDECREF(code); |
|
5880384d7c71
Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
960 Py_XDECREF(consts); |
|
5880384d7c71
Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
961 Py_XDECREF(names); |
|
5880384d7c71
Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
962 Py_XDECREF(varnames); |
|
16257
fd8c7203251f
PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15979
diff
changeset
|
963 Py_XDECREF(freevars); |
|
fd8c7203251f
PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15979
diff
changeset
|
964 Py_XDECREF(cellvars); |
|
5373
5880384d7c71
Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
965 Py_XDECREF(filename); |
|
5880384d7c71
Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
966 Py_XDECREF(name); |
|
5905
c2423843a26f
Plugged a leak. (The same as the one plugged in compile.c -- forgot
Guido van Rossum <guido@python.org>
parents:
5608
diff
changeset
|
967 Py_XDECREF(lnotab); |
| 267 | 968 |
| 969 } | |
|
41741
6f1293ab3ac4
Fix bug in marshal where bad data would cause a segfault due to
Neal Norwitz <nnorwitz@gmail.com>
parents:
39995
diff
changeset
|
970 retval = v; |
|
6f1293ab3ac4
Fix bug in marshal where bad data would cause a segfault due to
Neal Norwitz <nnorwitz@gmail.com>
parents:
39995
diff
changeset
|
971 break; |
|
16311
8c41f3861aad
It's unclear whether PyMarshal_XXX() are part of the public or private API.
Tim Peters <tim.peters@gmail.com>
parents:
16257
diff
changeset
|
972 |
| 267 | 973 default: |
|
3731
de1cb33219e3
Jeremy's patches for more robust handling of unmarshallable types.
Guido van Rossum <guido@python.org>
parents:
3620
diff
changeset
|
974 /* Bogus data got written, which isn't ideal. |
|
de1cb33219e3
Jeremy's patches for more robust handling of unmarshallable types.
Guido van Rossum <guido@python.org>
parents:
3620
diff
changeset
|
975 This will let you keep working and recover. */ |
|
8465
0e6fbbe3b63c
When unmarshalling, add test for negative lengths on strings, tuples
Guido van Rossum <guido@python.org>
parents:
8329
diff
changeset
|
976 PyErr_SetString(PyExc_ValueError, "bad marshal data"); |
|
41741
6f1293ab3ac4
Fix bug in marshal where bad data would cause a segfault due to
Neal Norwitz <nnorwitz@gmail.com>
parents:
39995
diff
changeset
|
977 retval = NULL; |
|
6f1293ab3ac4
Fix bug in marshal where bad data would cause a segfault due to
Neal Norwitz <nnorwitz@gmail.com>
parents:
39995
diff
changeset
|
978 break; |
|
16311
8c41f3861aad
It's unclear whether PyMarshal_XXX() are part of the public or private API.
Tim Peters <tim.peters@gmail.com>
parents:
16257
diff
changeset
|
979 |
| 267 | 980 } |
|
41741
6f1293ab3ac4
Fix bug in marshal where bad data would cause a segfault due to
Neal Norwitz <nnorwitz@gmail.com>
parents:
39995
diff
changeset
|
981 p->depth--; |
|
6f1293ab3ac4
Fix bug in marshal where bad data would cause a segfault due to
Neal Norwitz <nnorwitz@gmail.com>
parents:
39995
diff
changeset
|
982 return retval; |
| 267 | 983 } |
| 984 | |
|
32343
54b257e55f99
Make private function static
Neal Norwitz <nnorwitz@gmail.com>
parents:
32305
diff
changeset
|
985 static PyObject * |
|
31824
331de8b9d1ed
Marshal clean-up (SF patch #873224)
Armin Rigo <arigo@tunes.org>
parents:
30311
diff
changeset
|
986 read_object(RFILE *p) |
|
331de8b9d1ed
Marshal clean-up (SF patch #873224)
Armin Rigo <arigo@tunes.org>
parents:
30311
diff
changeset
|
987 { |
|
331de8b9d1ed
Marshal clean-up (SF patch #873224)
Armin Rigo <arigo@tunes.org>
parents:
30311
diff
changeset
|
988 PyObject *v; |
|
331de8b9d1ed
Marshal clean-up (SF patch #873224)
Armin Rigo <arigo@tunes.org>
parents:
30311
diff
changeset
|
989 if (PyErr_Occurred()) { |
|
331de8b9d1ed
Marshal clean-up (SF patch #873224)
Armin Rigo <arigo@tunes.org>
parents:
30311
diff
changeset
|
990 fprintf(stderr, "XXX readobject called with exception set\n"); |
|
331de8b9d1ed
Marshal clean-up (SF patch #873224)
Armin Rigo <arigo@tunes.org>
parents:
30311
diff
changeset
|
991 return NULL; |
|
331de8b9d1ed
Marshal clean-up (SF patch #873224)
Armin Rigo <arigo@tunes.org>
parents:
30311
diff
changeset
|
992 } |
|
331de8b9d1ed
Marshal clean-up (SF patch #873224)
Armin Rigo <arigo@tunes.org>
parents:
30311
diff
changeset
|
993 v = r_object(p); |
|
331de8b9d1ed
Marshal clean-up (SF patch #873224)
Armin Rigo <arigo@tunes.org>
parents:
30311
diff
changeset
|
994 if (v == NULL && !PyErr_Occurred()) |
|
331de8b9d1ed
Marshal clean-up (SF patch #873224)
Armin Rigo <arigo@tunes.org>
parents:
30311
diff
changeset
|
995 PyErr_SetString(PyExc_TypeError, "NULL object in marshal data"); |
|
331de8b9d1ed
Marshal clean-up (SF patch #873224)
Armin Rigo <arigo@tunes.org>
parents:
30311
diff
changeset
|
996 return v; |
|
331de8b9d1ed
Marshal clean-up (SF patch #873224)
Armin Rigo <arigo@tunes.org>
parents:
30311
diff
changeset
|
997 } |
|
331de8b9d1ed
Marshal clean-up (SF patch #873224)
Armin Rigo <arigo@tunes.org>
parents:
30311
diff
changeset
|
998 |
|
20552
65dd6157f999
SF patch #470393 (Jim Ahlstrom): Add missing marshal function
Guido van Rossum <guido@python.org>
parents:
19605
diff
changeset
|
999 int |
|
65dd6157f999
SF patch #470393 (Jim Ahlstrom): Add missing marshal function
Guido van Rossum <guido@python.org>
parents:
19605
diff
changeset
|
1000 PyMarshal_ReadShortFromFile(FILE *fp) |
|
65dd6157f999
SF patch #470393 (Jim Ahlstrom): Add missing marshal function
Guido van Rossum <guido@python.org>
parents:
19605
diff
changeset
|
1001 { |
|
65dd6157f999
SF patch #470393 (Jim Ahlstrom): Add missing marshal function
Guido van Rossum <guido@python.org>
parents:
19605
diff
changeset
|
1002 RFILE rf; |
|
36783
965bb4a54096
Fix gcc (4.0.x) warning about use of uninitialized variables.
Thomas Wouters <thomas@python.org>
parents:
36522
diff
changeset
|
1003 assert(fp); |
|
20552
65dd6157f999
SF patch #470393 (Jim Ahlstrom): Add missing marshal function
Guido van Rossum <guido@python.org>
parents:
19605
diff
changeset
|
1004 rf.fp = fp; |
|
36784
d853bc06847e
Fix incompatible assignment warning from previous checkin.
Thomas Wouters <thomas@python.org>
parents:
36783
diff
changeset
|
1005 rf.strings = NULL; |
|
d853bc06847e
Fix incompatible assignment warning from previous checkin.
Thomas Wouters <thomas@python.org>
parents:
36783
diff
changeset
|
1006 rf.end = rf.ptr = NULL; |
|
20552
65dd6157f999
SF patch #470393 (Jim Ahlstrom): Add missing marshal function
Guido van Rossum <guido@python.org>
parents:
19605
diff
changeset
|
1007 return r_short(&rf); |
|
65dd6157f999
SF patch #470393 (Jim Ahlstrom): Add missing marshal function
Guido van Rossum <guido@python.org>
parents:
19605
diff
changeset
|
1008 } |
|
65dd6157f999
SF patch #470393 (Jim Ahlstrom): Add missing marshal function
Guido van Rossum <guido@python.org>
parents:
19605
diff
changeset
|
1009 |
|
1191
38f0054f44a7
Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
1010 long |
|
13658
f79434ee41fa
Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents:
13447
diff
changeset
|
1011 PyMarshal_ReadLongFromFile(FILE *fp) |
|
1191
38f0054f44a7
Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
1012 { |
|
38f0054f44a7
Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
1013 RFILE rf; |
|
38f0054f44a7
Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
1014 rf.fp = fp; |
|
32422
3df36e2e5ddc
Patch #923098: Share interned strings in marshal.
Martin v. Löwis <martin@v.loewis.de>
parents:
32343
diff
changeset
|
1015 rf.strings = NULL; |
|
43536
cda0b625275a
Fix Coverity 185-186: If the passed in FILE is NULL, uninitialized memory
Neal Norwitz <nnorwitz@gmail.com>
parents:
41748
diff
changeset
|
1016 rf.ptr = rf.end = NULL; |
|
1191
38f0054f44a7
Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
1017 return r_long(&rf); |
|
38f0054f44a7
Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
1018 } |
|
38f0054f44a7
Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
1019 |
|
15979
a36a4932e399
Variant of SF patch 103252: Startup optimize: read *.pyc as string, not with getc().
Tim Peters <tim.peters@gmail.com>
parents:
15408
diff
changeset
|
1020 #ifdef HAVE_FSTAT |
|
a36a4932e399
Variant of SF patch 103252: Startup optimize: read *.pyc as string, not with getc().
Tim Peters <tim.peters@gmail.com>
parents:
15408
diff
changeset
|
1021 /* Return size of file in bytes; < 0 if unknown. */ |
|
a36a4932e399
Variant of SF patch 103252: Startup optimize: read *.pyc as string, not with getc().
Tim Peters <tim.peters@gmail.com>
parents:
15408
diff
changeset
|
1022 static off_t |
|
a36a4932e399
Variant of SF patch 103252: Startup optimize: read *.pyc as string, not with getc().
Tim Peters <tim.peters@gmail.com>
parents:
15408
diff
changeset
|
1023 getfilesize(FILE *fp) |
|
a36a4932e399
Variant of SF patch 103252: Startup optimize: read *.pyc as string, not with getc().
Tim Peters <tim.peters@gmail.com>
parents:
15408
diff
changeset
|
1024 { |
|
a36a4932e399
Variant of SF patch 103252: Startup optimize: read *.pyc as string, not with getc().
Tim Peters <tim.peters@gmail.com>
parents:
15408
diff
changeset
|
1025 struct stat st; |
|
a36a4932e399
Variant of SF patch 103252: Startup optimize: read *.pyc as string, not with getc().
Tim Peters <tim.peters@gmail.com>
parents:
15408
diff
changeset
|
1026 if (fstat(fileno(fp), &st) != 0) |
|
a36a4932e399
Variant of SF patch 103252: Startup optimize: read *.pyc as string, not with getc().
Tim Peters <tim.peters@gmail.com>
parents:
15408
diff
changeset
|
1027 return -1; |
|
a36a4932e399
Variant of SF patch 103252: Startup optimize: read *.pyc as string, not with getc().
Tim Peters <tim.peters@gmail.com>
parents:
15408
diff
changeset
|
1028 else |
|
a36a4932e399
Variant of SF patch 103252: Startup optimize: read *.pyc as string, not with getc().
Tim Peters <tim.peters@gmail.com>
parents:
15408
diff
changeset
|
1029 return st.st_size; |
|
a36a4932e399
Variant of SF patch 103252: Startup optimize: read *.pyc as string, not with getc().
Tim Peters <tim.peters@gmail.com>
parents:
15408
diff
changeset
|
1030 } |
|
a36a4932e399
Variant of SF patch 103252: Startup optimize: read *.pyc as string, not with getc().
Tim Peters <tim.peters@gmail.com>
parents:
15408
diff
changeset
|
1031 #endif |
|
16311
8c41f3861aad
It's unclear whether PyMarshal_XXX() are part of the public or private API.
Tim Peters <tim.peters@gmail.com>
parents:
16257
diff
changeset
|
1032 |
|
15979
a36a4932e399
Variant of SF patch 103252: Startup optimize: read *.pyc as string, not with getc().
Tim Peters <tim.peters@gmail.com>
parents:
15408
diff
changeset
|
1033 /* If we can get the size of the file up-front, and it's reasonably small, |
|
a36a4932e399
Variant of SF patch 103252: Startup optimize: read *.pyc as string, not with getc().
Tim Peters <tim.peters@gmail.com>
parents:
15408
diff
changeset
|
1034 * read it in one gulp and delegate to ...FromString() instead. Much quicker |
|
a36a4932e399
Variant of SF patch 103252: Startup optimize: read *.pyc as string, not with getc().
Tim Peters <tim.peters@gmail.com>
parents:
15408
diff
changeset
|
1035 * than reading a byte at a time from file; speeds .pyc imports. |
|
16311
8c41f3861aad
It's unclear whether PyMarshal_XXX() are part of the public or private API.
Tim Peters <tim.peters@gmail.com>
parents:
16257
diff
changeset
|
1036 * CAUTION: since this may read the entire remainder of the file, don't |
|
8c41f3861aad
It's unclear whether PyMarshal_XXX() are part of the public or private API.
Tim Peters <tim.peters@gmail.com>
parents:
16257
diff
changeset
|
1037 * call it unless you know you're done with the file. |
|
15979
a36a4932e399
Variant of SF patch 103252: Startup optimize: read *.pyc as string, not with getc().
Tim Peters <tim.peters@gmail.com>
parents:
15408
diff
changeset
|
1038 */ |
|
5373
5880384d7c71
Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
1039 PyObject * |
|
16311
8c41f3861aad
It's unclear whether PyMarshal_XXX() are part of the public or private API.
Tim Peters <tim.peters@gmail.com>
parents:
16257
diff
changeset
|
1040 PyMarshal_ReadLastObjectFromFile(FILE *fp) |
|
1191
38f0054f44a7
Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
1041 { |
|
15979
a36a4932e399
Variant of SF patch 103252: Startup optimize: read *.pyc as string, not with getc().
Tim Peters <tim.peters@gmail.com>
parents:
15408
diff
changeset
|
1042 /* 75% of 2.1's .pyc files can exploit SMALL_FILE_LIMIT. |
|
a36a4932e399
Variant of SF patch 103252: Startup optimize: read *.pyc as string, not with getc().
Tim Peters <tim.peters@gmail.com>
parents:
15408
diff
changeset
|
1043 * REASONABLE_FILE_LIMIT is by defn something big enough for Tkinter.pyc. |
|
a36a4932e399
Variant of SF patch 103252: Startup optimize: read *.pyc as string, not with getc().
Tim Peters <tim.peters@gmail.com>
parents:
15408
diff
changeset
|
1044 */ |
|
a36a4932e399
Variant of SF patch 103252: Startup optimize: read *.pyc as string, not with getc().
Tim Peters <tim.peters@gmail.com>
parents:
15408
diff
changeset
|
1045 #define SMALL_FILE_LIMIT (1L << 14) |
|
a36a4932e399
Variant of SF patch 103252: Startup optimize: read *.pyc as string, not with getc().
Tim Peters <tim.peters@gmail.com>
parents:
15408
diff
changeset
|
1046 #define REASONABLE_FILE_LIMIT (1L << 18) |
|
a36a4932e399
Variant of SF patch 103252: Startup optimize: read *.pyc as string, not with getc().
Tim Peters <tim.peters@gmail.com>
parents:
15408
diff
changeset
|
1047 #ifdef HAVE_FSTAT |
|
a36a4932e399
Variant of SF patch 103252: Startup optimize: read *.pyc as string, not with getc().
Tim Peters <tim.peters@gmail.com>
parents:
15408
diff
changeset
|
1048 off_t filesize; |
|
a36a4932e399
Variant of SF patch 103252: Startup optimize: read *.pyc as string, not with getc().
Tim Peters <tim.peters@gmail.com>
parents:
15408
diff
changeset
|
1049 #endif |
|
a36a4932e399
Variant of SF patch 103252: Startup optimize: read *.pyc as string, not with getc().
Tim Peters <tim.peters@gmail.com>
parents:
15408
diff
changeset
|
1050 #ifdef HAVE_FSTAT |
|
a36a4932e399
Variant of SF patch 103252: Startup optimize: read *.pyc as string, not with getc().
Tim Peters <tim.peters@gmail.com>
parents:
15408
diff
changeset
|
1051 filesize = getfilesize(fp); |
|
a36a4932e399
Variant of SF patch 103252: Startup optimize: read *.pyc as string, not with getc().
Tim Peters <tim.peters@gmail.com>
parents:
15408
diff
changeset
|
1052 if (filesize > 0) { |
|
a36a4932e399
Variant of SF patch 103252: Startup optimize: read *.pyc as string, not with getc().
Tim Peters <tim.peters@gmail.com>
parents:
15408
diff
changeset
|
1053 char buf[SMALL_FILE_LIMIT]; |
|
a36a4932e399
Variant of SF patch 103252: Startup optimize: read *.pyc as string, not with getc().
Tim Peters <tim.peters@gmail.com>
parents:
15408
diff
changeset
|
1054 char* pBuf = NULL; |
|
a36a4932e399
Variant of SF patch 103252: Startup optimize: read *.pyc as string, not with getc().
Tim Peters <tim.peters@gmail.com>
parents:
15408
diff
changeset
|
1055 if (filesize <= SMALL_FILE_LIMIT) |
|
a36a4932e399
Variant of SF patch 103252: Startup optimize: read *.pyc as string, not with getc().
Tim Peters <tim.peters@gmail.com>
parents:
15408
diff
changeset
|
1056 pBuf = buf; |
|
a36a4932e399
Variant of SF patch 103252: Startup optimize: read *.pyc as string, not with getc().
Tim Peters <tim.peters@gmail.com>
parents:
15408
diff
changeset
|
1057 else if (filesize <= REASONABLE_FILE_LIMIT) |
|
a36a4932e399
Variant of SF patch 103252: Startup optimize: read *.pyc as string, not with getc().
Tim Peters <tim.peters@gmail.com>
parents:
15408
diff
changeset
|
1058 pBuf = (char *)PyMem_MALLOC(filesize); |
|
a36a4932e399
Variant of SF patch 103252: Startup optimize: read *.pyc as string, not with getc().
Tim Peters <tim.peters@gmail.com>
parents:
15408
diff
changeset
|
1059 if (pBuf != NULL) { |
|
a36a4932e399
Variant of SF patch 103252: Startup optimize: read *.pyc as string, not with getc().
Tim Peters <tim.peters@gmail.com>
parents:
15408
diff
changeset
|
1060 PyObject* v; |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36324
diff
changeset
|
1061 size_t n; |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36324
diff
changeset
|
1062 /* filesize must fit into an int, because it |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36324
diff
changeset
|
1063 is smaller than REASONABLE_FILE_LIMIT */ |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36324
diff
changeset
|
1064 n = fread(pBuf, 1, (int)filesize, fp); |
|
15979
a36a4932e399
Variant of SF patch 103252: Startup optimize: read *.pyc as string, not with getc().
Tim Peters <tim.peters@gmail.com>
parents:
15408
diff
changeset
|
1065 v = PyMarshal_ReadObjectFromString(pBuf, n); |
|
a36a4932e399
Variant of SF patch 103252: Startup optimize: read *.pyc as string, not with getc().
Tim Peters <tim.peters@gmail.com>
parents:
15408
diff
changeset
|
1066 if (pBuf != buf) |
|
a36a4932e399
Variant of SF patch 103252: Startup optimize: read *.pyc as string, not with getc().
Tim Peters <tim.peters@gmail.com>
parents:
15408
diff
changeset
|
1067 PyMem_FREE(pBuf); |
|
a36a4932e399
Variant of SF patch 103252: Startup optimize: read *.pyc as string, not with getc().
Tim Peters <tim.peters@gmail.com>
parents:
15408
diff
changeset
|
1068 return v; |
|
a36a4932e399
Variant of SF patch 103252: Startup optimize: read *.pyc as string, not with getc().
Tim Peters <tim.peters@gmail.com>
parents:
15408
diff
changeset
|
1069 } |
|
16311
8c41f3861aad
It's unclear whether PyMarshal_XXX() are part of the public or private API.
Tim Peters <tim.peters@gmail.com>
parents:
16257
diff
changeset
|
1070 |
|
15979
a36a4932e399
Variant of SF patch 103252: Startup optimize: read *.pyc as string, not with getc().
Tim Peters <tim.peters@gmail.com>
parents:
15408
diff
changeset
|
1071 } |
|
a36a4932e399
Variant of SF patch 103252: Startup optimize: read *.pyc as string, not with getc().
Tim Peters <tim.peters@gmail.com>
parents:
15408
diff
changeset
|
1072 #endif |
|
16311
8c41f3861aad
It's unclear whether PyMarshal_XXX() are part of the public or private API.
Tim Peters <tim.peters@gmail.com>
parents:
16257
diff
changeset
|
1073 /* We don't have fstat, or we do but the file is larger than |
|
8c41f3861aad
It's unclear whether PyMarshal_XXX() are part of the public or private API.
Tim Peters <tim.peters@gmail.com>
parents:
16257
diff
changeset
|
1074 * REASONABLE_FILE_LIMIT or malloc failed -- read a byte at a time. |
|
8c41f3861aad
It's unclear whether PyMarshal_XXX() are part of the public or private API.
Tim Peters <tim.peters@gmail.com>
parents:
16257
diff
changeset
|
1075 */ |
|
8c41f3861aad
It's unclear whether PyMarshal_XXX() are part of the public or private API.
Tim Peters <tim.peters@gmail.com>
parents:
16257
diff
changeset
|
1076 return PyMarshal_ReadObjectFromFile(fp); |
|
8c41f3861aad
It's unclear whether PyMarshal_XXX() are part of the public or private API.
Tim Peters <tim.peters@gmail.com>
parents:
16257
diff
changeset
|
1077 |
|
8c41f3861aad
It's unclear whether PyMarshal_XXX() are part of the public or private API.
Tim Peters <tim.peters@gmail.com>
parents:
16257
diff
changeset
|
1078 #undef SMALL_FILE_LIMIT |
|
8c41f3861aad
It's unclear whether PyMarshal_XXX() are part of the public or private API.
Tim Peters <tim.peters@gmail.com>
parents:
16257
diff
changeset
|
1079 #undef REASONABLE_FILE_LIMIT |
|
8c41f3861aad
It's unclear whether PyMarshal_XXX() are part of the public or private API.
Tim Peters <tim.peters@gmail.com>
parents:
16257
diff
changeset
|
1080 } |
|
8c41f3861aad
It's unclear whether PyMarshal_XXX() are part of the public or private API.
Tim Peters <tim.peters@gmail.com>
parents:
16257
diff
changeset
|
1081 |
|
8c41f3861aad
It's unclear whether PyMarshal_XXX() are part of the public or private API.
Tim Peters <tim.peters@gmail.com>
parents:
16257
diff
changeset
|
1082 PyObject * |
|
8c41f3861aad
It's unclear whether PyMarshal_XXX() are part of the public or private API.
Tim Peters <tim.peters@gmail.com>
parents:
16257
diff
changeset
|
1083 PyMarshal_ReadObjectFromFile(FILE *fp) |
|
8c41f3861aad
It's unclear whether PyMarshal_XXX() are part of the public or private API.
Tim Peters <tim.peters@gmail.com>
parents:
16257
diff
changeset
|
1084 { |
|
8c41f3861aad
It's unclear whether PyMarshal_XXX() are part of the public or private API.
Tim Peters <tim.peters@gmail.com>
parents:
16257
diff
changeset
|
1085 RFILE rf; |
|
32422
3df36e2e5ddc
Patch #923098: Share interned strings in marshal.
Martin v. Löwis <martin@v.loewis.de>
parents:
32343
diff
changeset
|
1086 PyObject *result; |
|
1191
38f0054f44a7
Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
1087 rf.fp = fp; |
|
32422
3df36e2e5ddc
Patch #923098: Share interned strings in marshal.
Martin v. Löwis <martin@v.loewis.de>
parents:
32343
diff
changeset
|
1088 rf.strings = PyList_New(0); |
|
41741
6f1293ab3ac4
Fix bug in marshal where bad data would cause a segfault due to
Neal Norwitz <nnorwitz@gmail.com>
parents:
39995
diff
changeset
|
1089 rf.depth = 0; |
|
43536
cda0b625275a
Fix Coverity 185-186: If the passed in FILE is NULL, uninitialized memory
Neal Norwitz <nnorwitz@gmail.com>
parents:
41748
diff
changeset
|
1090 rf.ptr = rf.end = NULL; |
|
32422
3df36e2e5ddc
Patch #923098: Share interned strings in marshal.
Martin v. Löwis <martin@v.loewis.de>
parents:
32343
diff
changeset
|
1091 result = r_object(&rf); |
|
3df36e2e5ddc
Patch #923098: Share interned strings in marshal.
Martin v. Löwis <martin@v.loewis.de>
parents:
32343
diff
changeset
|
1092 Py_DECREF(rf.strings); |
|
3df36e2e5ddc
Patch #923098: Share interned strings in marshal.
Martin v. Löwis <martin@v.loewis.de>
parents:
32343
diff
changeset
|
1093 return result; |
|
1191
38f0054f44a7
Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
1094 } |
|
38f0054f44a7
Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
1095 |
|
5373
5880384d7c71
Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
1096 PyObject * |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36324
diff
changeset
|
1097 PyMarshal_ReadObjectFromString(char *str, Py_ssize_t len) |
|
1270
14369a5e6167
Support for frozen scripts; added -i option.
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
1098 { |
|
14369a5e6167
Support for frozen scripts; added -i option.
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
1099 RFILE rf; |
|
32422
3df36e2e5ddc
Patch #923098: Share interned strings in marshal.
Martin v. Löwis <martin@v.loewis.de>
parents:
32343
diff
changeset
|
1100 PyObject *result; |
|
1270
14369a5e6167
Support for frozen scripts; added -i option.
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
1101 rf.fp = NULL; |
|
14369a5e6167
Support for frozen scripts; added -i option.
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
1102 rf.ptr = str; |
|
14369a5e6167
Support for frozen scripts; added -i option.
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
1103 rf.end = str + len; |
|
32422
3df36e2e5ddc
Patch #923098: Share interned strings in marshal.
Martin v. Löwis <martin@v.loewis.de>
parents:
32343
diff
changeset
|
1104 rf.strings = PyList_New(0); |
|
41741
6f1293ab3ac4
Fix bug in marshal where bad data would cause a segfault due to
Neal Norwitz <nnorwitz@gmail.com>
parents:
39995
diff
changeset
|
1105 rf.depth = 0; |
|
32422
3df36e2e5ddc
Patch #923098: Share interned strings in marshal.
Martin v. Löwis <martin@v.loewis.de>
parents:
32343
diff
changeset
|
1106 result = r_object(&rf); |
|
3df36e2e5ddc
Patch #923098: Share interned strings in marshal.
Martin v. Löwis <martin@v.loewis.de>
parents:
32343
diff
changeset
|
1107 Py_DECREF(rf.strings); |
|
3df36e2e5ddc
Patch #923098: Share interned strings in marshal.
Martin v. Löwis <martin@v.loewis.de>
parents:
32343
diff
changeset
|
1108 return result; |
|
1270
14369a5e6167
Support for frozen scripts; added -i option.
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
1109 } |
|
14369a5e6167
Support for frozen scripts; added -i option.
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
1110 |
|
5373
5880384d7c71
Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
1111 PyObject * |
|
32422
3df36e2e5ddc
Patch #923098: Share interned strings in marshal.
Martin v. Löwis <martin@v.loewis.de>
parents:
32343
diff
changeset
|
1112 PyMarshal_WriteObjectToString(PyObject *x, int version) |
|
4039
23f280ab5409
Added *PyMarshal_WriteObjectToString() (moved the relevant code there
Guido van Rossum <guido@python.org>
parents:
3839
diff
changeset
|
1113 { |
|
23f280ab5409
Added *PyMarshal_WriteObjectToString() (moved the relevant code there
Guido van Rossum <guido@python.org>
parents:
3839
diff
changeset
|
1114 WFILE wf; |
|
23f280ab5409
Added *PyMarshal_WriteObjectToString() (moved the relevant code there
Guido van Rossum <guido@python.org>
parents:
3839
diff
changeset
|
1115 wf.fp = NULL; |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47294
diff
changeset
|
1116 wf.str = PyString_FromStringAndSize((char *)NULL, 50); |
|
4039
23f280ab5409
Added *PyMarshal_WriteObjectToString() (moved the relevant code there
Guido van Rossum <guido@python.org>
parents:
3839
diff
changeset
|
1117 if (wf.str == NULL) |
|
23f280ab5409
Added *PyMarshal_WriteObjectToString() (moved the relevant code there
Guido van Rossum <guido@python.org>
parents:
3839
diff
changeset
|
1118 return NULL; |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47294
diff
changeset
|
1119 wf.ptr = PyString_AS_STRING((PyStringObject *)wf.str); |
|
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47294
diff
changeset
|
1120 wf.end = wf.ptr + PyString_Size(wf.str); |
|
4039
23f280ab5409
Added *PyMarshal_WriteObjectToString() (moved the relevant code there
Guido van Rossum <guido@python.org>
parents:
3839
diff
changeset
|
1121 wf.error = 0; |
|
13070
7392e71f6cce
Michael Hudson <mwh21@cam.ac.uk>:
Fred Drake <fdrake@acm.org>
parents:
12576
diff
changeset
|
1122 wf.depth = 0; |
| 35008 | 1123 wf.version = version; |
|
32422
3df36e2e5ddc
Patch #923098: Share interned strings in marshal.
Martin v. Löwis <martin@v.loewis.de>
parents:
32343
diff
changeset
|
1124 wf.strings = (version > 0) ? PyDict_New() : NULL; |
|
4039
23f280ab5409
Added *PyMarshal_WriteObjectToString() (moved the relevant code there
Guido van Rossum <guido@python.org>
parents:
3839
diff
changeset
|
1125 w_object(x, &wf); |
|
32422
3df36e2e5ddc
Patch #923098: Share interned strings in marshal.
Martin v. Löwis <martin@v.loewis.de>
parents:
32343
diff
changeset
|
1126 Py_XDECREF(wf.strings); |
|
39995
6ba402b60041
Forward-port of r52136,52138: a review of overflow-detecting code.
Armin Rigo <arigo@tunes.org>
parents:
39448
diff
changeset
|
1127 if (wf.str != NULL) { |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47294
diff
changeset
|
1128 char *base = PyString_AS_STRING((PyStringObject *)wf.str); |
|
39995
6ba402b60041
Forward-port of r52136,52138: a review of overflow-detecting code.
Armin Rigo <arigo@tunes.org>
parents:
39448
diff
changeset
|
1129 if (wf.ptr - base > PY_SSIZE_T_MAX) { |
|
6ba402b60041
Forward-port of r52136,52138: a review of overflow-detecting code.
Armin Rigo <arigo@tunes.org>
parents:
39448
diff
changeset
|
1130 Py_DECREF(wf.str); |
|
6ba402b60041
Forward-port of r52136,52138: a review of overflow-detecting code.
Armin Rigo <arigo@tunes.org>
parents:
39448
diff
changeset
|
1131 PyErr_SetString(PyExc_OverflowError, |
|
6ba402b60041
Forward-port of r52136,52138: a review of overflow-detecting code.
Armin Rigo <arigo@tunes.org>
parents:
39448
diff
changeset
|
1132 "too much marshall data for a string"); |
|
6ba402b60041
Forward-port of r52136,52138: a review of overflow-detecting code.
Armin Rigo <arigo@tunes.org>
parents:
39448
diff
changeset
|
1133 return NULL; |
|
6ba402b60041
Forward-port of r52136,52138: a review of overflow-detecting code.
Armin Rigo <arigo@tunes.org>
parents:
39448
diff
changeset
|
1134 } |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47294
diff
changeset
|
1135 _PyString_Resize(&wf.str, (Py_ssize_t)(wf.ptr - base)); |
|
39995
6ba402b60041
Forward-port of r52136,52138: a review of overflow-detecting code.
Armin Rigo <arigo@tunes.org>
parents:
39448
diff
changeset
|
1136 } |
|
4039
23f280ab5409
Added *PyMarshal_WriteObjectToString() (moved the relevant code there
Guido van Rossum <guido@python.org>
parents:
3839
diff
changeset
|
1137 if (wf.error) { |
|
5373
5880384d7c71
Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
1138 Py_XDECREF(wf.str); |
|
16311
8c41f3861aad
It's unclear whether PyMarshal_XXX() are part of the public or private API.
Tim Peters <tim.peters@gmail.com>
parents:
16257
diff
changeset
|
1139 PyErr_SetString(PyExc_ValueError, |
|
13070
7392e71f6cce
Michael Hudson <mwh21@cam.ac.uk>:
Fred Drake <fdrake@acm.org>
parents:
12576
diff
changeset
|
1140 (wf.error==1)?"unmarshallable object" |
|
7392e71f6cce
Michael Hudson <mwh21@cam.ac.uk>:
Fred Drake <fdrake@acm.org>
parents:
12576
diff
changeset
|
1141 :"object too deeply nested to marshal"); |
|
4039
23f280ab5409
Added *PyMarshal_WriteObjectToString() (moved the relevant code there
Guido van Rossum <guido@python.org>
parents:
3839
diff
changeset
|
1142 return NULL; |
|
23f280ab5409
Added *PyMarshal_WriteObjectToString() (moved the relevant code there
Guido van Rossum <guido@python.org>
parents:
3839
diff
changeset
|
1143 } |
|
23f280ab5409
Added *PyMarshal_WriteObjectToString() (moved the relevant code there
Guido van Rossum <guido@python.org>
parents:
3839
diff
changeset
|
1144 return wf.str; |
|
23f280ab5409
Added *PyMarshal_WriteObjectToString() (moved the relevant code there
Guido van Rossum <guido@python.org>
parents:
3839
diff
changeset
|
1145 } |
|
23f280ab5409
Added *PyMarshal_WriteObjectToString() (moved the relevant code there
Guido van Rossum <guido@python.org>
parents:
3839
diff
changeset
|
1146 |
|
279
9c924f9c3b16
Add marshalling for dictionaries.
Guido van Rossum <guido@python.org>
parents:
272
diff
changeset
|
1147 /* And an interface for Python programs... */ |
| 267 | 1148 |
|
5373
5880384d7c71
Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
1149 static PyObject * |
|
13658
f79434ee41fa
Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents:
13447
diff
changeset
|
1150 marshal_dump(PyObject *self, PyObject *args) |
| 267 | 1151 { |
|
1191
38f0054f44a7
Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
1152 WFILE wf; |
|
5373
5880384d7c71
Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
1153 PyObject *x; |
|
5880384d7c71
Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
1154 PyObject *f; |
|
32422
3df36e2e5ddc
Patch #923098: Share interned strings in marshal.
Martin v. Löwis <martin@v.loewis.de>
parents:
32343
diff
changeset
|
1155 int version = Py_MARSHAL_VERSION; |
|
3df36e2e5ddc
Patch #923098: Share interned strings in marshal.
Martin v. Löwis <martin@v.loewis.de>
parents:
32343
diff
changeset
|
1156 if (!PyArg_ParseTuple(args, "OO|i:dump", &x, &f, &version)) |
|
1191
38f0054f44a7
Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
1157 return NULL; |
|
5373
5880384d7c71
Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
1158 if (!PyFile_Check(f)) { |
|
5880384d7c71
Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
1159 PyErr_SetString(PyExc_TypeError, |
|
5880384d7c71
Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
1160 "marshal.dump() 2nd arg must be file"); |
| 267 | 1161 return NULL; |
| 1162 } | |
|
5373
5880384d7c71
Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
1163 wf.fp = PyFile_AsFile(f); |
|
1191
38f0054f44a7
Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
1164 wf.str = NULL; |
|
38f0054f44a7
Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
1165 wf.ptr = wf.end = NULL; |
|
3731
de1cb33219e3
Jeremy's patches for more robust handling of unmarshallable types.
Guido van Rossum <guido@python.org>
parents:
3620
diff
changeset
|
1166 wf.error = 0; |
|
13070
7392e71f6cce
Michael Hudson <mwh21@cam.ac.uk>:
Fred Drake <fdrake@acm.org>
parents:
12576
diff
changeset
|
1167 wf.depth = 0; |
|
32422
3df36e2e5ddc
Patch #923098: Share interned strings in marshal.
Martin v. Löwis <martin@v.loewis.de>
parents:
32343
diff
changeset
|
1168 wf.strings = (version > 0) ? PyDict_New() : 0; |
|
35877
40f3d79b2125
version was not initialized properly
Neal Norwitz <nnorwitz@gmail.com>
parents:
35766
diff
changeset
|
1169 wf.version = version; |
|
1191
38f0054f44a7
Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
1170 w_object(x, &wf); |
|
32422
3df36e2e5ddc
Patch #923098: Share interned strings in marshal.
Martin v. Löwis <martin@v.loewis.de>
parents:
32343
diff
changeset
|
1171 Py_XDECREF(wf.strings); |
|
3731
de1cb33219e3
Jeremy's patches for more robust handling of unmarshallable types.
Guido van Rossum <guido@python.org>
parents:
3620
diff
changeset
|
1172 if (wf.error) { |
|
16311
8c41f3861aad
It's unclear whether PyMarshal_XXX() are part of the public or private API.
Tim Peters <tim.peters@gmail.com>
parents:
16257
diff
changeset
|
1173 PyErr_SetString(PyExc_ValueError, |
|
13070
7392e71f6cce
Michael Hudson <mwh21@cam.ac.uk>:
Fred Drake <fdrake@acm.org>
parents:
12576
diff
changeset
|
1174 (wf.error==1)?"unmarshallable object" |
|
7392e71f6cce
Michael Hudson <mwh21@cam.ac.uk>:
Fred Drake <fdrake@acm.org>
parents:
12576
diff
changeset
|
1175 :"object too deeply nested to marshal"); |
|
3731
de1cb33219e3
Jeremy's patches for more robust handling of unmarshallable types.
Guido van Rossum <guido@python.org>
parents:
3620
diff
changeset
|
1176 return NULL; |
|
de1cb33219e3
Jeremy's patches for more robust handling of unmarshallable types.
Guido van Rossum <guido@python.org>
parents:
3620
diff
changeset
|
1177 } |
|
5373
5880384d7c71
Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
1178 Py_INCREF(Py_None); |
|
5880384d7c71
Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
1179 return Py_None; |
| 267 | 1180 } |
| 1181 | |
|
5373
5880384d7c71
Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
1182 static PyObject * |
|
38490
a5298f371be7
Convert more modules to METH_VARARGS.
Georg Brandl <georg@python.org>
parents:
36866
diff
changeset
|
1183 marshal_load(PyObject *self, PyObject *f) |
| 267 | 1184 { |
|
1191
38f0054f44a7
Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
1185 RFILE rf; |
|
38490
a5298f371be7
Convert more modules to METH_VARARGS.
Georg Brandl <georg@python.org>
parents:
36866
diff
changeset
|
1186 PyObject *result; |
|
5373
5880384d7c71
Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
1187 if (!PyFile_Check(f)) { |
|
5880384d7c71
Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
1188 PyErr_SetString(PyExc_TypeError, |
|
5880384d7c71
Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
1189 "marshal.load() arg must be file"); |
| 267 | 1190 return NULL; |
| 1191 } | |
|
5373
5880384d7c71
Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
1192 rf.fp = PyFile_AsFile(f); |
|
32422
3df36e2e5ddc
Patch #923098: Share interned strings in marshal.
Martin v. Löwis <martin@v.loewis.de>
parents:
32343
diff
changeset
|
1193 rf.strings = PyList_New(0); |
|
41741
6f1293ab3ac4
Fix bug in marshal where bad data would cause a segfault due to
Neal Norwitz <nnorwitz@gmail.com>
parents:
39995
diff
changeset
|
1194 rf.depth = 0; |
|
32422
3df36e2e5ddc
Patch #923098: Share interned strings in marshal.
Martin v. Löwis <martin@v.loewis.de>
parents:
32343
diff
changeset
|
1195 result = read_object(&rf); |
|
3df36e2e5ddc
Patch #923098: Share interned strings in marshal.
Martin v. Löwis <martin@v.loewis.de>
parents:
32343
diff
changeset
|
1196 Py_DECREF(rf.strings); |
|
3df36e2e5ddc
Patch #923098: Share interned strings in marshal.
Martin v. Löwis <martin@v.loewis.de>
parents:
32343
diff
changeset
|
1197 return result; |
|
1191
38f0054f44a7
Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
1198 } |
|
38f0054f44a7
Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
1199 |
|
5373
5880384d7c71
Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
1200 static PyObject * |
|
13658
f79434ee41fa
Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents:
13447
diff
changeset
|
1201 marshal_dumps(PyObject *self, PyObject *args) |
|
1191
38f0054f44a7
Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
1202 { |
|
5373
5880384d7c71
Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
1203 PyObject *x; |
|
32422
3df36e2e5ddc
Patch #923098: Share interned strings in marshal.
Martin v. Löwis <martin@v.loewis.de>
parents:
32343
diff
changeset
|
1204 int version = Py_MARSHAL_VERSION; |
|
34205
ea340006c1f3
Back-ported: marshal.dumps() with the new optional argument 'version' just
Armin Rigo <arigo@tunes.org>
parents:
32422
diff
changeset
|
1205 if (!PyArg_ParseTuple(args, "O|i:dumps", &x, &version)) |
|
1191
38f0054f44a7
Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
1206 return NULL; |
|
32422
3df36e2e5ddc
Patch #923098: Share interned strings in marshal.
Martin v. Löwis <martin@v.loewis.de>
parents:
32343
diff
changeset
|
1207 return PyMarshal_WriteObjectToString(x, version); |
|
1191
38f0054f44a7
Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
1208 } |
|
38f0054f44a7
Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
1209 |
|
5373
5880384d7c71
Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
1210 static PyObject * |
|
13658
f79434ee41fa
Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents:
13447
diff
changeset
|
1211 marshal_loads(PyObject *self, PyObject *args) |
|
1191
38f0054f44a7
Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
1212 { |
|
38f0054f44a7
Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
1213 RFILE rf; |
|
38f0054f44a7
Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
1214 char *s; |
|
36792
688795873ff1
Make Py_ssize_t clean.
Thomas Wouters <thomas@python.org>
parents:
36784
diff
changeset
|
1215 Py_ssize_t n; |
|
32422
3df36e2e5ddc
Patch #923098: Share interned strings in marshal.
Martin v. Löwis <martin@v.loewis.de>
parents:
32343
diff
changeset
|
1216 PyObject* result; |
|
35070
1332f2e705b2
Remove extraneous format character from PyArg_ParseTuple call in
Michael W. Hudson <mwh@python.net>
parents:
35010
diff
changeset
|
1217 if (!PyArg_ParseTuple(args, "s#:loads", &s, &n)) |
|
1191
38f0054f44a7
Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
1218 return NULL; |
|
38f0054f44a7
Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
1219 rf.fp = NULL; |
|
38f0054f44a7
Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
1220 rf.ptr = s; |
|
38f0054f44a7
Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
1221 rf.end = s + n; |
|
32422
3df36e2e5ddc
Patch #923098: Share interned strings in marshal.
Martin v. Löwis <martin@v.loewis.de>
parents:
32343
diff
changeset
|
1222 rf.strings = PyList_New(0); |
|
41741
6f1293ab3ac4
Fix bug in marshal where bad data would cause a segfault due to
Neal Norwitz <nnorwitz@gmail.com>
parents:
39995
diff
changeset
|
1223 rf.depth = 0; |
|
32422
3df36e2e5ddc
Patch #923098: Share interned strings in marshal.
Martin v. Löwis <martin@v.loewis.de>
parents:
32343
diff
changeset
|
1224 result = read_object(&rf); |
|
3df36e2e5ddc
Patch #923098: Share interned strings in marshal.
Martin v. Löwis <martin@v.loewis.de>
parents:
32343
diff
changeset
|
1225 Py_DECREF(rf.strings); |
|
3df36e2e5ddc
Patch #923098: Share interned strings in marshal.
Martin v. Löwis <martin@v.loewis.de>
parents:
32343
diff
changeset
|
1226 return result; |
| 267 | 1227 } |
| 1228 | |
|
5373
5880384d7c71
Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
1229 static PyMethodDef marshal_methods[] = { |
|
22705
8703d2052339
Use symbolic METH_VARARGS instead of 1 for ml_flags
Neal Norwitz <nnorwitz@gmail.com>
parents:
20947
diff
changeset
|
1230 {"dump", marshal_dump, METH_VARARGS}, |
|
38490
a5298f371be7
Convert more modules to METH_VARARGS.
Georg Brandl <georg@python.org>
parents:
36866
diff
changeset
|
1231 {"load", marshal_load, METH_O}, |
|
22705
8703d2052339
Use symbolic METH_VARARGS instead of 1 for ml_flags
Neal Norwitz <nnorwitz@gmail.com>
parents:
20947
diff
changeset
|
1232 {"dumps", marshal_dumps, METH_VARARGS}, |
|
8703d2052339
Use symbolic METH_VARARGS instead of 1 for ml_flags
Neal Norwitz <nnorwitz@gmail.com>
parents:
20947
diff
changeset
|
1233 {"loads", marshal_loads, METH_VARARGS}, |
| 267 | 1234 {NULL, NULL} /* sentinel */ |
| 1235 }; | |
| 1236 | |
|
30311
f003e48f562d
Bug #794140: cygwin builds do not embed
Jason Tishler <jason@tishler.net>
parents:
24435
diff
changeset
|
1237 PyMODINIT_FUNC |
|
13658
f79434ee41fa
Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents:
13447
diff
changeset
|
1238 PyMarshal_Init(void) |
| 267 | 1239 { |
|
32422
3df36e2e5ddc
Patch #923098: Share interned strings in marshal.
Martin v. Löwis <martin@v.loewis.de>
parents:
32343
diff
changeset
|
1240 PyObject *mod = Py_InitModule("marshal", marshal_methods); |
|
36324
1af7f719ed28
Check return result from Py_InitModule*(). This API can fail.
Neal Norwitz <nnorwitz@gmail.com>
parents:
35877
diff
changeset
|
1241 if (mod == NULL) |
|
1af7f719ed28
Check return result from Py_InitModule*(). This API can fail.
Neal Norwitz <nnorwitz@gmail.com>
parents:
35877
diff
changeset
|
1242 return; |
|
32422
3df36e2e5ddc
Patch #923098: Share interned strings in marshal.
Martin v. Löwis <martin@v.loewis.de>
parents:
32343
diff
changeset
|
1243 PyModule_AddIntConstant(mod, "version", Py_MARSHAL_VERSION); |
| 267 | 1244 } |
