Mercurial > cpython-lru_cache-weakref
annotate Python/ast.c @ 36000:cc3261597136 legacy-trunk
SF #1370197, memory leak - ast_error_finish (in error conditions).
| author | Neal Norwitz <nnorwitz@gmail.com> |
|---|---|
| date | Sun, 11 Dec 2005 20:08:33 +0000 |
| parents | c89707daa011 |
| children | 6321669f2f4d |
| rev | line source |
|---|---|
| 35742 | 1 /* |
| 2 * This file includes functions to transform a concrete syntax tree (CST) to | |
| 3 * an abstract syntax tree (AST). The main function is PyAST_FromNode(). | |
| 4 * | |
| 5 */ | |
| 6 #include "Python.h" | |
| 7 #include "Python-ast.h" | |
| 8 #include "grammar.h" | |
| 9 #include "node.h" | |
| 10 #include "ast.h" | |
| 11 #include "token.h" | |
| 12 #include "parsetok.h" | |
| 13 #include "graminit.h" | |
| 14 | |
| 15 #include <assert.h> | |
| 16 | |
| 17 /* XXX TO DO | |
| 18 - re-indent this file (should be done) | |
| 19 - internal error checking (freeing memory, etc.) | |
| 20 - syntax errors | |
| 21 */ | |
| 22 | |
|
35874
44d63eedfaca
Add a note about how to do the memory deallocation a bit.
Neal Norwitz <nnorwitz@gmail.com>
parents:
35873
diff
changeset
|
23 /* |
|
44d63eedfaca
Add a note about how to do the memory deallocation a bit.
Neal Norwitz <nnorwitz@gmail.com>
parents:
35873
diff
changeset
|
24 Note: |
|
44d63eedfaca
Add a note about how to do the memory deallocation a bit.
Neal Norwitz <nnorwitz@gmail.com>
parents:
35873
diff
changeset
|
25 |
|
44d63eedfaca
Add a note about how to do the memory deallocation a bit.
Neal Norwitz <nnorwitz@gmail.com>
parents:
35873
diff
changeset
|
26 You should rarely need to use the asdl_seq_free() in this file. |
|
44d63eedfaca
Add a note about how to do the memory deallocation a bit.
Neal Norwitz <nnorwitz@gmail.com>
parents:
35873
diff
changeset
|
27 If you use asdl_seq_free(), you will leak any objects held in the seq. |
|
44d63eedfaca
Add a note about how to do the memory deallocation a bit.
Neal Norwitz <nnorwitz@gmail.com>
parents:
35873
diff
changeset
|
28 If there is an appropriate asdl_*_seq_free() function, use it. |
|
44d63eedfaca
Add a note about how to do the memory deallocation a bit.
Neal Norwitz <nnorwitz@gmail.com>
parents:
35873
diff
changeset
|
29 If there isn't an asdl_*_seq_free() function for you, you will |
|
44d63eedfaca
Add a note about how to do the memory deallocation a bit.
Neal Norwitz <nnorwitz@gmail.com>
parents:
35873
diff
changeset
|
30 need to loop over the data in the sequence and free it. |
|
44d63eedfaca
Add a note about how to do the memory deallocation a bit.
Neal Norwitz <nnorwitz@gmail.com>
parents:
35873
diff
changeset
|
31 |
|
44d63eedfaca
Add a note about how to do the memory deallocation a bit.
Neal Norwitz <nnorwitz@gmail.com>
parents:
35873
diff
changeset
|
32 asdl_seq* seq; |
|
44d63eedfaca
Add a note about how to do the memory deallocation a bit.
Neal Norwitz <nnorwitz@gmail.com>
parents:
35873
diff
changeset
|
33 int i; |
|
44d63eedfaca
Add a note about how to do the memory deallocation a bit.
Neal Norwitz <nnorwitz@gmail.com>
parents:
35873
diff
changeset
|
34 |
|
44d63eedfaca
Add a note about how to do the memory deallocation a bit.
Neal Norwitz <nnorwitz@gmail.com>
parents:
35873
diff
changeset
|
35 for (i = 0; i < asdl_seq_LEN(seq); i++) |
|
44d63eedfaca
Add a note about how to do the memory deallocation a bit.
Neal Norwitz <nnorwitz@gmail.com>
parents:
35873
diff
changeset
|
36 free_***(asdl_seq_GET(seq, i)); |
| 35879 | 37 asdl_seq_free(seq); / * ok * / |
|
35874
44d63eedfaca
Add a note about how to do the memory deallocation a bit.
Neal Norwitz <nnorwitz@gmail.com>
parents:
35873
diff
changeset
|
38 |
|
44d63eedfaca
Add a note about how to do the memory deallocation a bit.
Neal Norwitz <nnorwitz@gmail.com>
parents:
35873
diff
changeset
|
39 Almost all of the ast functions return a seq of expr, so you should |
|
44d63eedfaca
Add a note about how to do the memory deallocation a bit.
Neal Norwitz <nnorwitz@gmail.com>
parents:
35873
diff
changeset
|
40 use asdl_expr_seq_free(). The exception is ast_for_suite() which |
|
44d63eedfaca
Add a note about how to do the memory deallocation a bit.
Neal Norwitz <nnorwitz@gmail.com>
parents:
35873
diff
changeset
|
41 returns a seq of stmt's, so use asdl_stmt_seq_free() to free it. |
| 35879 | 42 |
| 43 If asdl_seq_free is appropriate, you should mark it with an ok comment. | |
| 44 | |
| 45 There are still many memory problems in this file even though | |
| 46 it runs clean in valgrind, save one problem that may have existed | |
| 47 before the AST. | |
| 48 | |
| 49 Any code which does something like this: | |
| 50 | |
| 51 return ASTconstruct(local, LINENO(n)); | |
| 52 | |
| 53 will leak memory. The problem is if ASTconstruct (e.g., TryFinally) | |
| 54 cannot allocate memory, local will be leaked. | |
| 55 | |
| 56 There was discussion on python-dev to replace the entire allocation | |
| 57 scheme in this file with arenas. Basically rather than allocate | |
| 58 memory in little blocks with malloc(), we allocate one big honking | |
| 59 hunk and deref everything into this block. We would still need | |
| 60 another block or technique to handle the PyObject*s. | |
| 61 | |
| 62 http://mail.python.org/pipermail/python-dev/2005-November/058138.html | |
|
35874
44d63eedfaca
Add a note about how to do the memory deallocation a bit.
Neal Norwitz <nnorwitz@gmail.com>
parents:
35873
diff
changeset
|
63 */ |
| 35742 | 64 |
| 65 /* Data structure used internally */ | |
| 66 struct compiling { | |
| 67 char *c_encoding; /* source encoding */ | |
| 68 }; | |
| 69 | |
| 70 static asdl_seq *seq_for_testlist(struct compiling *, const node *); | |
| 71 static expr_ty ast_for_expr(struct compiling *, const node *); | |
| 72 static stmt_ty ast_for_stmt(struct compiling *, const node *); | |
| 73 static asdl_seq *ast_for_suite(struct compiling *, const node *); | |
| 74 static asdl_seq *ast_for_exprlist(struct compiling *, const node *, int); | |
|
35798
857f68fba275
Write a separate ast_for_testlist_gexp() function instead of overloading
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35771
diff
changeset
|
75 static expr_ty ast_for_testlist(struct compiling *, const node *); |
|
857f68fba275
Write a separate ast_for_testlist_gexp() function instead of overloading
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35771
diff
changeset
|
76 static expr_ty ast_for_testlist_gexp(struct compiling *, const node *); |
| 35742 | 77 |
| 78 /* Note different signature for ast_for_call */ | |
| 79 static expr_ty ast_for_call(struct compiling *, const node *, expr_ty); | |
| 80 | |
| 81 static PyObject *parsenumber(const char *); | |
| 82 static PyObject *parsestr(const char *s, const char *encoding); | |
| 83 static PyObject *parsestrplus(struct compiling *, const node *n); | |
| 84 | |
| 85 #ifndef LINENO | |
| 86 #define LINENO(n) ((n)->n_lineno) | |
| 87 #endif | |
| 88 | |
| 89 #define NEW_IDENTIFIER(n) PyString_InternFromString(STR(n)) | |
| 90 | |
| 91 static void | |
| 92 asdl_stmt_seq_free(asdl_seq* seq) | |
| 93 { | |
| 94 int n, i; | |
| 95 | |
| 96 if (!seq) | |
| 97 return; | |
|
35873
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
98 |
| 35742 | 99 n = asdl_seq_LEN(seq); |
| 100 for (i = 0; i < n; i++) | |
| 101 free_stmt(asdl_seq_GET(seq, i)); | |
|
35873
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
102 asdl_seq_free(seq); /* ok */ |
| 35742 | 103 } |
| 104 | |
| 105 static void | |
| 106 asdl_expr_seq_free(asdl_seq* seq) | |
| 107 { | |
| 108 int n, i; | |
| 109 | |
| 110 if (!seq) | |
| 111 return; | |
|
35873
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
112 |
| 35742 | 113 n = asdl_seq_LEN(seq); |
| 114 for (i = 0; i < n; i++) | |
| 115 free_expr(asdl_seq_GET(seq, i)); | |
|
35873
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
116 asdl_seq_free(seq); /* ok */ |
|
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
117 } |
|
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
118 |
|
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
119 static void |
|
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
120 asdl_alias_seq_free(asdl_seq* seq) |
|
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
121 { |
|
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
122 int n, i; |
|
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
123 |
|
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
124 if (!seq) |
|
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
125 return; |
|
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
126 |
|
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
127 n = asdl_seq_LEN(seq); |
|
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
128 for (i = 0; i < n; i++) |
|
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
129 free_alias(asdl_seq_GET(seq, i)); |
|
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
130 asdl_seq_free(seq); /* ok */ |
|
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
131 } |
|
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
132 |
|
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
133 static void |
|
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
134 asdl_comprehension_seq_free(asdl_seq* seq) |
|
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
135 { |
|
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
136 int n, i; |
|
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
137 |
|
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
138 if (!seq) |
|
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
139 return; |
|
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
140 |
|
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
141 n = asdl_seq_LEN(seq); |
|
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
142 for (i = 0; i < n; i++) |
|
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
143 free_comprehension(asdl_seq_GET(seq, i)); |
|
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
144 asdl_seq_free(seq); /* ok */ |
| 35742 | 145 } |
| 146 | |
| 147 /* This routine provides an invalid object for the syntax error. | |
| 148 The outermost routine must unpack this error and create the | |
| 149 proper object. We do this so that we don't have to pass | |
| 150 the filename to everything function. | |
| 151 | |
| 152 XXX Maybe we should just pass the filename... | |
| 153 */ | |
| 154 | |
| 155 static int | |
| 156 ast_error(const node *n, const char *errstr) | |
| 157 { | |
| 158 PyObject *u = Py_BuildValue("zi", errstr, LINENO(n)); | |
| 159 if (!u) | |
| 160 return 0; | |
| 161 PyErr_SetObject(PyExc_SyntaxError, u); | |
| 162 Py_DECREF(u); | |
| 163 return 0; | |
| 164 } | |
| 165 | |
| 166 static void | |
| 167 ast_error_finish(const char *filename) | |
| 168 { | |
| 169 PyObject *type, *value, *tback, *errstr, *loc, *tmp; | |
| 170 int lineno; | |
| 171 | |
| 172 assert(PyErr_Occurred()); | |
| 173 if (!PyErr_ExceptionMatches(PyExc_SyntaxError)) | |
| 174 return; | |
| 175 | |
| 176 PyErr_Fetch(&type, &value, &tback); | |
| 177 errstr = PyTuple_GetItem(value, 0); | |
| 178 if (!errstr) | |
| 179 return; | |
| 180 Py_INCREF(errstr); | |
| 181 lineno = PyInt_AsLong(PyTuple_GetItem(value, 1)); | |
|
36000
cc3261597136
SF #1370197, memory leak - ast_error_finish (in error conditions).
Neal Norwitz <nnorwitz@gmail.com>
parents:
35990
diff
changeset
|
182 if (lineno == -1) { |
|
cc3261597136
SF #1370197, memory leak - ast_error_finish (in error conditions).
Neal Norwitz <nnorwitz@gmail.com>
parents:
35990
diff
changeset
|
183 Py_DECREF(errstr); |
| 35742 | 184 return; |
|
36000
cc3261597136
SF #1370197, memory leak - ast_error_finish (in error conditions).
Neal Norwitz <nnorwitz@gmail.com>
parents:
35990
diff
changeset
|
185 } |
| 35742 | 186 Py_DECREF(value); |
| 187 | |
| 188 loc = PyErr_ProgramText(filename, lineno); | |
| 189 if (!loc) { | |
| 190 Py_INCREF(Py_None); | |
| 191 loc = Py_None; | |
| 192 } | |
| 193 tmp = Py_BuildValue("(ziOO)", filename, lineno, Py_None, loc); | |
| 194 Py_DECREF(loc); | |
|
36000
cc3261597136
SF #1370197, memory leak - ast_error_finish (in error conditions).
Neal Norwitz <nnorwitz@gmail.com>
parents:
35990
diff
changeset
|
195 if (!tmp) { |
|
cc3261597136
SF #1370197, memory leak - ast_error_finish (in error conditions).
Neal Norwitz <nnorwitz@gmail.com>
parents:
35990
diff
changeset
|
196 Py_DECREF(errstr); |
| 35742 | 197 return; |
|
36000
cc3261597136
SF #1370197, memory leak - ast_error_finish (in error conditions).
Neal Norwitz <nnorwitz@gmail.com>
parents:
35990
diff
changeset
|
198 } |
| 35742 | 199 value = Py_BuildValue("(OO)", errstr, tmp); |
| 200 Py_DECREF(errstr); | |
| 201 Py_DECREF(tmp); | |
| 202 if (!value) | |
| 203 return; | |
| 204 PyErr_Restore(type, value, tback); | |
| 205 } | |
| 206 | |
| 207 /* num_stmts() returns number of contained statements. | |
| 208 | |
| 209 Use this routine to determine how big a sequence is needed for | |
| 210 the statements in a parse tree. Its raison d'etre is this bit of | |
| 211 grammar: | |
| 212 | |
| 213 stmt: simple_stmt | compound_stmt | |
| 214 simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE | |
| 215 | |
| 216 A simple_stmt can contain multiple small_stmt elements joined | |
| 217 by semicolons. If the arg is a simple_stmt, the number of | |
| 218 small_stmt elements is returned. | |
| 219 */ | |
| 220 | |
| 221 static int | |
| 222 num_stmts(const node *n) | |
| 223 { | |
| 224 int i, l; | |
| 225 node *ch; | |
| 226 | |
| 227 switch (TYPE(n)) { | |
| 228 case single_input: | |
| 229 if (TYPE(CHILD(n, 0)) == NEWLINE) | |
| 230 return 0; | |
| 231 else | |
| 232 return num_stmts(CHILD(n, 0)); | |
| 233 case file_input: | |
| 234 l = 0; | |
| 235 for (i = 0; i < NCH(n); i++) { | |
| 236 ch = CHILD(n, i); | |
| 237 if (TYPE(ch) == stmt) | |
| 238 l += num_stmts(ch); | |
| 239 } | |
| 240 return l; | |
| 241 case stmt: | |
| 242 return num_stmts(CHILD(n, 0)); | |
| 243 case compound_stmt: | |
| 244 return 1; | |
| 245 case simple_stmt: | |
| 246 return NCH(n) / 2; /* Divide by 2 to remove count of semi-colons */ | |
| 247 case suite: | |
| 248 if (NCH(n) == 1) | |
| 249 return num_stmts(CHILD(n, 0)); | |
| 250 else { | |
| 251 l = 0; | |
| 252 for (i = 2; i < (NCH(n) - 1); i++) | |
| 253 l += num_stmts(CHILD(n, i)); | |
| 254 return l; | |
| 255 } | |
| 256 default: { | |
| 257 char buf[128]; | |
| 258 | |
| 259 sprintf(buf, "Non-statement found: %d %d\n", | |
| 260 TYPE(n), NCH(n)); | |
| 261 Py_FatalError(buf); | |
| 262 } | |
| 263 } | |
| 264 assert(0); | |
| 265 return 0; | |
| 266 } | |
| 267 | |
| 268 /* Transform the CST rooted at node * to the appropriate AST | |
| 269 */ | |
| 270 | |
| 271 mod_ty | |
| 272 PyAST_FromNode(const node *n, PyCompilerFlags *flags, const char *filename) | |
| 273 { | |
| 274 int i, j, num; | |
| 275 asdl_seq *stmts = NULL; | |
| 276 stmt_ty s; | |
| 277 node *ch; | |
| 278 struct compiling c; | |
| 279 | |
| 280 if (flags && flags->cf_flags & PyCF_SOURCE_IS_UTF8) { | |
| 281 c.c_encoding = "utf-8"; | |
| 282 } else if (TYPE(n) == encoding_decl) { | |
| 283 c.c_encoding = STR(n); | |
| 284 n = CHILD(n, 0); | |
| 285 } else { | |
| 286 c.c_encoding = NULL; | |
| 287 } | |
| 288 | |
| 289 switch (TYPE(n)) { | |
| 290 case file_input: | |
| 291 stmts = asdl_seq_new(num_stmts(n)); | |
| 292 if (!stmts) | |
| 293 return NULL; | |
| 294 for (i = 0; i < NCH(n) - 1; i++) { | |
| 295 ch = CHILD(n, i); | |
| 296 if (TYPE(ch) == NEWLINE) | |
| 297 continue; | |
| 298 REQ(ch, stmt); | |
| 299 num = num_stmts(ch); | |
| 300 if (num == 1) { | |
| 301 s = ast_for_stmt(&c, ch); | |
| 302 if (!s) | |
| 303 goto error; | |
| 304 asdl_seq_APPEND(stmts, s); | |
| 305 } | |
| 306 else { | |
| 307 ch = CHILD(ch, 0); | |
| 308 REQ(ch, simple_stmt); | |
| 309 for (j = 0; j < num; j++) { | |
| 310 s = ast_for_stmt(&c, CHILD(ch, j * 2)); | |
| 311 if (!s) | |
| 312 goto error; | |
| 313 asdl_seq_APPEND(stmts, s); | |
| 314 } | |
| 315 } | |
| 316 } | |
| 317 return Module(stmts); | |
| 318 case eval_input: { | |
| 319 expr_ty testlist_ast; | |
| 320 | |
| 321 /* XXX Why not gen_for here? */ | |
|
35798
857f68fba275
Write a separate ast_for_testlist_gexp() function instead of overloading
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35771
diff
changeset
|
322 testlist_ast = ast_for_testlist(&c, CHILD(n, 0)); |
| 35742 | 323 if (!testlist_ast) |
| 324 goto error; | |
| 325 return Expression(testlist_ast); | |
| 326 } | |
| 327 case single_input: | |
| 328 if (TYPE(CHILD(n, 0)) == NEWLINE) { | |
| 329 stmts = asdl_seq_new(1); | |
| 330 if (!stmts) | |
| 331 goto error; | |
| 332 asdl_seq_SET(stmts, 0, Pass(n->n_lineno)); | |
| 333 return Interactive(stmts); | |
| 334 } | |
| 335 else { | |
| 336 n = CHILD(n, 0); | |
| 337 num = num_stmts(n); | |
| 338 stmts = asdl_seq_new(num); | |
| 339 if (!stmts) | |
| 340 goto error; | |
| 341 if (num == 1) { | |
| 342 stmt_ty s = ast_for_stmt(&c, n); | |
| 343 if (!s) | |
| 344 goto error; | |
| 345 asdl_seq_SET(stmts, 0, s); | |
| 346 } | |
| 347 else { | |
| 348 /* Only a simple_stmt can contain multiple statements. */ | |
| 349 REQ(n, simple_stmt); | |
| 350 for (i = 0; i < NCH(n); i += 2) { | |
| 351 stmt_ty s; | |
| 352 if (TYPE(CHILD(n, i)) == NEWLINE) | |
| 353 break; | |
| 354 s = ast_for_stmt(&c, CHILD(n, i)); | |
| 355 if (!s) | |
| 356 goto error; | |
| 357 asdl_seq_SET(stmts, i / 2, s); | |
| 358 } | |
| 359 } | |
| 360 | |
| 361 return Interactive(stmts); | |
| 362 } | |
| 363 default: | |
| 364 goto error; | |
| 365 } | |
| 366 error: | |
| 367 if (stmts) | |
| 368 asdl_stmt_seq_free(stmts); | |
| 369 ast_error_finish(filename); | |
| 370 return NULL; | |
| 371 } | |
| 372 | |
| 373 /* Return the AST repr. of the operator represented as syntax (|, ^, etc.) | |
| 374 */ | |
| 375 | |
| 376 static operator_ty | |
| 377 get_operator(const node *n) | |
| 378 { | |
| 379 switch (TYPE(n)) { | |
| 380 case VBAR: | |
| 381 return BitOr; | |
| 382 case CIRCUMFLEX: | |
| 383 return BitXor; | |
| 384 case AMPER: | |
| 385 return BitAnd; | |
| 386 case LEFTSHIFT: | |
| 387 return LShift; | |
| 388 case RIGHTSHIFT: | |
| 389 return RShift; | |
| 390 case PLUS: | |
| 391 return Add; | |
| 392 case MINUS: | |
| 393 return Sub; | |
| 394 case STAR: | |
| 395 return Mult; | |
| 396 case SLASH: | |
| 397 return Div; | |
| 398 case DOUBLESLASH: | |
| 399 return FloorDiv; | |
| 400 case PERCENT: | |
| 401 return Mod; | |
| 402 default: | |
| 403 return 0; | |
| 404 } | |
| 405 } | |
| 406 | |
| 407 /* Set the context ctx for expr_ty e returning 0 on success, -1 on error. | |
| 408 | |
| 409 Only sets context for expr kinds that "can appear in assignment context" | |
| 410 (according to ../Parser/Python.asdl). For other expr kinds, it sets | |
| 411 an appropriate syntax error and returns false. | |
| 412 | |
| 413 If e is a sequential type, items in sequence will also have their context | |
| 414 set. | |
| 415 | |
| 416 */ | |
| 417 | |
| 418 static int | |
| 419 set_context(expr_ty e, expr_context_ty ctx, const node *n) | |
| 420 { | |
| 421 asdl_seq *s = NULL; | |
| 422 | |
| 423 switch (e->kind) { | |
| 424 case Attribute_kind: | |
| 425 if (ctx == Store && | |
| 426 !strcmp(PyString_AS_STRING(e->v.Attribute.attr), "None")) { | |
| 427 return ast_error(n, "assignment to None"); | |
| 428 } | |
| 429 e->v.Attribute.ctx = ctx; | |
| 430 break; | |
| 431 case Subscript_kind: | |
| 432 e->v.Subscript.ctx = ctx; | |
| 433 break; | |
| 434 case Name_kind: | |
| 435 if (ctx == Store && | |
| 436 !strcmp(PyString_AS_STRING(e->v.Name.id), "None")) { | |
| 437 return ast_error(n, "assignment to None"); | |
| 438 } | |
| 439 e->v.Name.ctx = ctx; | |
| 440 break; | |
| 441 case List_kind: | |
| 442 e->v.List.ctx = ctx; | |
| 443 s = e->v.List.elts; | |
| 444 break; | |
| 445 case Tuple_kind: | |
| 446 if (asdl_seq_LEN(e->v.Tuple.elts) == 0) | |
| 447 return ast_error(n, "can't assign to ()"); | |
| 448 e->v.Tuple.ctx = ctx; | |
| 449 s = e->v.Tuple.elts; | |
| 450 break; | |
| 451 case Call_kind: | |
| 452 if (ctx == Store) | |
| 453 return ast_error(n, "can't assign to function call"); | |
| 454 else if (ctx == Del) | |
| 455 return ast_error(n, "can't delete function call"); | |
| 456 else | |
| 457 return ast_error(n, "unexpected operation on function call"); | |
| 458 break; | |
| 459 case BinOp_kind: | |
| 460 return ast_error(n, "can't assign to operator"); | |
| 461 case GeneratorExp_kind: | |
| 462 return ast_error(n, "assignment to generator expression " | |
| 463 "not possible"); | |
| 464 case Num_kind: | |
| 465 case Str_kind: | |
| 466 return ast_error(n, "can't assign to literal"); | |
| 467 default: { | |
| 468 char buf[300]; | |
| 469 PyOS_snprintf(buf, sizeof(buf), | |
| 470 "unexpected expression in assignment %d (line %d)", | |
| 471 e->kind, e->lineno); | |
| 472 return ast_error(n, buf); | |
| 473 } | |
| 474 } | |
| 475 /* If the LHS is a list or tuple, we need to set the assignment | |
| 476 context for all the tuple elements. | |
| 477 */ | |
| 478 if (s) { | |
| 479 int i; | |
| 480 | |
| 481 for (i = 0; i < asdl_seq_LEN(s); i++) { | |
| 482 if (!set_context(asdl_seq_GET(s, i), ctx, n)) | |
| 483 return 0; | |
| 484 } | |
| 485 } | |
| 486 return 1; | |
| 487 } | |
| 488 | |
| 489 static operator_ty | |
| 490 ast_for_augassign(const node *n) | |
| 491 { | |
| 492 REQ(n, augassign); | |
| 493 n = CHILD(n, 0); | |
| 494 switch (STR(n)[0]) { | |
| 495 case '+': | |
| 496 return Add; | |
| 497 case '-': | |
| 498 return Sub; | |
| 499 case '/': | |
| 500 if (STR(n)[1] == '/') | |
| 501 return FloorDiv; | |
| 502 else | |
| 503 return Div; | |
| 504 case '%': | |
| 505 return Mod; | |
| 506 case '<': | |
| 507 return LShift; | |
| 508 case '>': | |
| 509 return RShift; | |
| 510 case '&': | |
| 511 return BitAnd; | |
| 512 case '^': | |
| 513 return BitXor; | |
| 514 case '|': | |
| 515 return BitOr; | |
| 516 case '*': | |
| 517 if (STR(n)[1] == '*') | |
| 518 return Pow; | |
| 519 else | |
| 520 return Mult; | |
| 521 default: | |
|
35870
f531c05cd240
Convert all internal errors from Exception to SystemError
Neal Norwitz <nnorwitz@gmail.com>
parents:
35868
diff
changeset
|
522 PyErr_Format(PyExc_SystemError, "invalid augassign: %s", STR(n)); |
| 35742 | 523 return 0; |
| 524 } | |
| 525 } | |
| 526 | |
| 527 static cmpop_ty | |
| 528 ast_for_comp_op(const node *n) | |
| 529 { | |
| 530 /* comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is' | |
| 531 |'is' 'not' | |
| 532 */ | |
| 533 REQ(n, comp_op); | |
| 534 if (NCH(n) == 1) { | |
| 535 n = CHILD(n, 0); | |
| 536 switch (TYPE(n)) { | |
| 537 case LESS: | |
| 538 return Lt; | |
| 539 case GREATER: | |
| 540 return Gt; | |
| 541 case EQEQUAL: /* == */ | |
| 542 return Eq; | |
| 543 case LESSEQUAL: | |
| 544 return LtE; | |
| 545 case GREATEREQUAL: | |
| 546 return GtE; | |
| 547 case NOTEQUAL: | |
| 548 return NotEq; | |
| 549 case NAME: | |
| 550 if (strcmp(STR(n), "in") == 0) | |
| 551 return In; | |
| 552 if (strcmp(STR(n), "is") == 0) | |
| 553 return Is; | |
| 554 default: | |
|
35870
f531c05cd240
Convert all internal errors from Exception to SystemError
Neal Norwitz <nnorwitz@gmail.com>
parents:
35868
diff
changeset
|
555 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s", |
| 35742 | 556 STR(n)); |
| 557 return 0; | |
| 558 } | |
| 559 } | |
| 560 else if (NCH(n) == 2) { | |
| 561 /* handle "not in" and "is not" */ | |
| 562 switch (TYPE(CHILD(n, 0))) { | |
| 563 case NAME: | |
| 564 if (strcmp(STR(CHILD(n, 1)), "in") == 0) | |
| 565 return NotIn; | |
| 566 if (strcmp(STR(CHILD(n, 0)), "is") == 0) | |
| 567 return IsNot; | |
| 568 default: | |
|
35870
f531c05cd240
Convert all internal errors from Exception to SystemError
Neal Norwitz <nnorwitz@gmail.com>
parents:
35868
diff
changeset
|
569 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s %s", |
| 35742 | 570 STR(CHILD(n, 0)), STR(CHILD(n, 1))); |
| 571 return 0; | |
| 572 } | |
| 573 } | |
|
35870
f531c05cd240
Convert all internal errors from Exception to SystemError
Neal Norwitz <nnorwitz@gmail.com>
parents:
35868
diff
changeset
|
574 PyErr_Format(PyExc_SystemError, "invalid comp_op: has %d children", |
| 35742 | 575 NCH(n)); |
| 576 return 0; | |
| 577 } | |
| 578 | |
| 579 static asdl_seq * | |
| 580 seq_for_testlist(struct compiling *c, const node *n) | |
| 581 { | |
| 582 /* testlist: test (',' test)* [','] */ | |
|
35759
d91ed7107fd2
ANSI-C-ify the placement of local var declarations.
Armin Rigo <arigo@tunes.org>
parents:
35748
diff
changeset
|
583 asdl_seq *seq; |
|
d91ed7107fd2
ANSI-C-ify the placement of local var declarations.
Armin Rigo <arigo@tunes.org>
parents:
35748
diff
changeset
|
584 expr_ty expression; |
|
d91ed7107fd2
ANSI-C-ify the placement of local var declarations.
Armin Rigo <arigo@tunes.org>
parents:
35748
diff
changeset
|
585 int i; |
| 35742 | 586 assert(TYPE(n) == testlist |
| 587 || TYPE(n) == listmaker | |
| 588 || TYPE(n) == testlist_gexp | |
| 589 || TYPE(n) == testlist_safe | |
| 590 ); | |
| 591 | |
| 592 seq = asdl_seq_new((NCH(n) + 1) / 2); | |
| 593 if (!seq) | |
| 594 return NULL; | |
| 595 | |
| 596 for (i = 0; i < NCH(n); i += 2) { | |
| 597 REQ(CHILD(n, i), test); | |
| 598 | |
| 599 expression = ast_for_expr(c, CHILD(n, i)); | |
| 600 if (!expression) { | |
|
35873
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
601 asdl_expr_seq_free(seq); |
| 35742 | 602 return NULL; |
| 603 } | |
| 604 | |
| 605 assert(i / 2 < seq->size); | |
| 606 asdl_seq_SET(seq, i / 2, expression); | |
| 607 } | |
| 608 return seq; | |
| 609 } | |
| 610 | |
| 611 static expr_ty | |
| 612 compiler_complex_args(const node *n) | |
| 613 { | |
| 614 int i, len = (NCH(n) + 1) / 2; | |
| 615 expr_ty result; | |
| 616 asdl_seq *args = asdl_seq_new(len); | |
| 617 if (!args) | |
| 618 return NULL; | |
| 619 | |
| 620 REQ(n, fplist); | |
| 621 | |
| 622 for (i = 0; i < len; i++) { | |
| 623 const node *child = CHILD(CHILD(n, 2*i), 0); | |
| 624 expr_ty arg; | |
| 625 if (TYPE(child) == NAME) { | |
| 626 if (!strcmp(STR(child), "None")) { | |
| 627 ast_error(child, "assignment to None"); | |
| 628 return NULL; | |
| 629 } | |
| 630 arg = Name(NEW_IDENTIFIER(child), Store, LINENO(child)); | |
| 631 } | |
| 632 else | |
| 633 arg = compiler_complex_args(CHILD(CHILD(n, 2*i), 1)); | |
| 634 set_context(arg, Store, n); | |
| 635 asdl_seq_SET(args, i, arg); | |
| 636 } | |
| 637 | |
| 638 result = Tuple(args, Store, LINENO(n)); | |
| 639 set_context(result, Store, n); | |
| 640 return result; | |
| 641 } | |
| 642 | |
| 643 /* Create AST for argument list. | |
| 644 | |
| 645 XXX TO DO: | |
| 646 - check for invalid argument lists like normal after default | |
| 647 */ | |
| 648 | |
| 649 static arguments_ty | |
| 650 ast_for_arguments(struct compiling *c, const node *n) | |
| 651 { | |
| 652 /* parameters: '(' [varargslist] ')' | |
| 653 varargslist: (fpdef ['=' test] ',')* ('*' NAME [',' '**' NAME] | |
| 654 | '**' NAME) | fpdef ['=' test] (',' fpdef ['=' test])* [','] | |
| 655 */ | |
| 656 int i, n_args = 0, n_defaults = 0, found_default = 0; | |
| 657 asdl_seq *args, *defaults; | |
| 658 identifier vararg = NULL, kwarg = NULL; | |
| 659 node *ch; | |
| 660 | |
| 661 if (TYPE(n) == parameters) { | |
| 662 if (NCH(n) == 2) /* () as argument list */ | |
| 663 return arguments(NULL, NULL, NULL, NULL); | |
| 664 n = CHILD(n, 1); | |
| 665 } | |
| 666 REQ(n, varargslist); | |
| 667 | |
| 668 /* first count the number of normal args & defaults */ | |
| 669 for (i = 0; i < NCH(n); i++) { | |
| 670 ch = CHILD(n, i); | |
| 671 if (TYPE(ch) == fpdef) { | |
| 672 n_args++; | |
| 673 } | |
| 674 if (TYPE(ch) == EQUAL) | |
| 675 n_defaults++; | |
| 676 } | |
| 677 args = (n_args ? asdl_seq_new(n_args) : NULL); | |
| 678 if (!args && n_args) | |
| 679 return NULL; /* Don't need to go to NULL; nothing allocated */ | |
| 680 defaults = (n_defaults ? asdl_seq_new(n_defaults) : NULL); | |
| 681 if (!defaults && n_defaults) | |
| 682 goto error; | |
| 683 | |
| 684 /* fpdef: NAME | '(' fplist ')' | |
| 685 fplist: fpdef (',' fpdef)* [','] | |
| 686 */ | |
| 687 i = 0; | |
| 688 while (i < NCH(n)) { | |
| 689 ch = CHILD(n, i); | |
| 690 switch (TYPE(ch)) { | |
| 691 case fpdef: | |
| 692 /* XXX Need to worry about checking if TYPE(CHILD(n, i+1)) is | |
| 693 anything other than EQUAL or a comma? */ | |
| 694 /* XXX Should NCH(n) check be made a separate check? */ | |
| 695 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) { | |
| 696 asdl_seq_APPEND(defaults, | |
| 697 ast_for_expr(c, CHILD(n, i + 2))); | |
| 698 i += 2; | |
| 699 found_default = 1; | |
| 700 } | |
| 701 else if (found_default) { | |
| 702 ast_error(n, | |
| 703 "non-default argument follows default argument"); | |
| 704 goto error; | |
| 705 } | |
| 706 | |
| 707 if (NCH(ch) == 3) { | |
| 708 asdl_seq_APPEND(args, | |
| 709 compiler_complex_args(CHILD(ch, 1))); | |
| 710 } | |
| 711 else if (TYPE(CHILD(ch, 0)) == NAME) { | |
|
35759
d91ed7107fd2
ANSI-C-ify the placement of local var declarations.
Armin Rigo <arigo@tunes.org>
parents:
35748
diff
changeset
|
712 expr_ty name; |
| 35742 | 713 if (!strcmp(STR(CHILD(ch, 0)), "None")) { |
| 714 ast_error(CHILD(ch, 0), "assignment to None"); | |
| 715 goto error; | |
| 716 } | |
|
35759
d91ed7107fd2
ANSI-C-ify the placement of local var declarations.
Armin Rigo <arigo@tunes.org>
parents:
35748
diff
changeset
|
717 name = Name(NEW_IDENTIFIER(CHILD(ch, 0)), |
|
d91ed7107fd2
ANSI-C-ify the placement of local var declarations.
Armin Rigo <arigo@tunes.org>
parents:
35748
diff
changeset
|
718 Param, LINENO(ch)); |
| 35742 | 719 if (!name) |
| 720 goto error; | |
| 721 asdl_seq_APPEND(args, name); | |
| 722 | |
| 723 } | |
| 724 i += 2; /* the name and the comma */ | |
| 725 break; | |
| 726 case STAR: | |
| 727 if (!strcmp(STR(CHILD(n, i+1)), "None")) { | |
| 728 ast_error(CHILD(n, i+1), "assignment to None"); | |
| 729 goto error; | |
| 730 } | |
| 731 vararg = NEW_IDENTIFIER(CHILD(n, i+1)); | |
| 732 i += 3; | |
| 733 break; | |
| 734 case DOUBLESTAR: | |
| 735 if (!strcmp(STR(CHILD(n, i+1)), "None")) { | |
| 736 ast_error(CHILD(n, i+1), "assignment to None"); | |
| 737 goto error; | |
| 738 } | |
| 739 kwarg = NEW_IDENTIFIER(CHILD(n, i+1)); | |
| 740 i += 3; | |
| 741 break; | |
| 742 default: | |
|
35870
f531c05cd240
Convert all internal errors from Exception to SystemError
Neal Norwitz <nnorwitz@gmail.com>
parents:
35868
diff
changeset
|
743 PyErr_Format(PyExc_SystemError, |
| 35742 | 744 "unexpected node in varargslist: %d @ %d", |
| 745 TYPE(ch), i); | |
| 746 goto error; | |
| 747 } | |
| 748 } | |
| 749 | |
| 750 return arguments(args, vararg, kwarg, defaults); | |
| 751 | |
| 752 error: | |
|
35873
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
753 Py_XDECREF(vararg); |
|
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
754 Py_XDECREF(kwarg); |
| 35742 | 755 if (args) |
|
35873
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
756 asdl_expr_seq_free(args); |
| 35742 | 757 if (defaults) |
|
35873
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
758 asdl_expr_seq_free(defaults); |
| 35742 | 759 return NULL; |
| 760 } | |
| 761 | |
| 762 static expr_ty | |
| 763 ast_for_dotted_name(struct compiling *c, const node *n) | |
| 764 { | |
| 765 expr_ty e = NULL; | |
| 766 expr_ty attrib = NULL; | |
| 767 identifier id = NULL; | |
| 768 int i; | |
| 769 | |
| 770 REQ(n, dotted_name); | |
| 771 | |
| 772 id = NEW_IDENTIFIER(CHILD(n, 0)); | |
| 773 if (!id) | |
| 774 goto error; | |
| 775 e = Name(id, Load, LINENO(n)); | |
| 776 if (!e) | |
| 777 goto error; | |
| 778 id = NULL; | |
| 779 | |
| 780 for (i = 2; i < NCH(n); i+=2) { | |
| 781 id = NEW_IDENTIFIER(CHILD(n, i)); | |
| 782 if (!id) | |
| 783 goto error; | |
| 784 attrib = Attribute(e, id, Load, LINENO(CHILD(n, i))); | |
| 785 if (!attrib) | |
| 786 goto error; | |
| 787 e = attrib; | |
| 788 attrib = NULL; | |
| 789 } | |
| 790 | |
| 791 return e; | |
| 792 | |
| 793 error: | |
| 794 Py_XDECREF(id); | |
| 795 free_expr(e); | |
| 796 return NULL; | |
| 797 } | |
| 798 | |
| 799 static expr_ty | |
| 800 ast_for_decorator(struct compiling *c, const node *n) | |
| 801 { | |
| 802 /* decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE */ | |
| 803 expr_ty d = NULL; | |
| 804 expr_ty name_expr = NULL; | |
| 805 | |
| 806 REQ(n, decorator); | |
| 807 | |
| 808 if ((NCH(n) < 3 && NCH(n) != 5 && NCH(n) != 6) | |
| 809 || TYPE(CHILD(n, 0)) != AT || TYPE(RCHILD(n, -1)) != NEWLINE) { | |
| 810 ast_error(n, "Invalid decorator node"); | |
| 811 goto error; | |
| 812 } | |
| 813 | |
| 814 name_expr = ast_for_dotted_name(c, CHILD(n, 1)); | |
| 815 if (!name_expr) | |
| 816 goto error; | |
| 817 | |
| 818 if (NCH(n) == 3) { /* No arguments */ | |
| 819 d = name_expr; | |
| 820 name_expr = NULL; | |
| 821 } | |
| 822 else if (NCH(n) == 5) { /* Call with no arguments */ | |
| 823 d = Call(name_expr, NULL, NULL, NULL, NULL, LINENO(n)); | |
| 824 if (!d) | |
| 825 goto error; | |
| 826 name_expr = NULL; | |
| 827 } | |
| 828 else { | |
| 829 d = ast_for_call(c, CHILD(n, 3), name_expr); | |
| 830 if (!d) | |
| 831 goto error; | |
| 832 name_expr = NULL; | |
| 833 } | |
| 834 | |
| 835 return d; | |
| 836 | |
| 837 error: | |
| 838 free_expr(name_expr); | |
| 839 free_expr(d); | |
| 840 return NULL; | |
| 841 } | |
| 842 | |
| 843 static asdl_seq* | |
| 844 ast_for_decorators(struct compiling *c, const node *n) | |
| 845 { | |
| 846 asdl_seq* decorator_seq = NULL; | |
|
35873
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
847 expr_ty d; |
| 35742 | 848 int i; |
| 849 | |
| 850 REQ(n, decorators); | |
| 851 | |
| 852 decorator_seq = asdl_seq_new(NCH(n)); | |
| 853 if (!decorator_seq) | |
| 854 return NULL; | |
| 855 | |
| 856 for (i = 0; i < NCH(n); i++) { | |
| 857 d = ast_for_decorator(c, CHILD(n, i)); | |
| 858 if (!d) | |
| 859 goto error; | |
| 860 asdl_seq_APPEND(decorator_seq, d); | |
| 861 } | |
| 862 return decorator_seq; | |
| 863 error: | |
| 864 asdl_expr_seq_free(decorator_seq); | |
| 865 return NULL; | |
| 866 } | |
| 867 | |
| 868 static stmt_ty | |
| 869 ast_for_funcdef(struct compiling *c, const node *n) | |
| 870 { | |
| 871 /* funcdef: 'def' [decorators] NAME parameters ':' suite */ | |
| 872 identifier name = NULL; | |
| 873 arguments_ty args = NULL; | |
| 874 asdl_seq *body = NULL; | |
| 875 asdl_seq *decorator_seq = NULL; | |
| 876 int name_i; | |
| 877 | |
| 878 REQ(n, funcdef); | |
| 879 | |
| 880 if (NCH(n) == 6) { /* decorators are present */ | |
| 881 decorator_seq = ast_for_decorators(c, CHILD(n, 0)); | |
| 882 if (!decorator_seq) | |
| 883 goto error; | |
| 884 name_i = 2; | |
| 885 } | |
| 886 else { | |
| 887 name_i = 1; | |
| 888 } | |
| 889 | |
| 890 name = NEW_IDENTIFIER(CHILD(n, name_i)); | |
| 891 if (!name) | |
| 892 goto error; | |
| 893 else if (!strcmp(STR(CHILD(n, name_i)), "None")) { | |
|
35873
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
894 ast_error(CHILD(n, name_i), "assignment to None"); |
|
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
895 goto error; |
| 35742 | 896 } |
| 897 args = ast_for_arguments(c, CHILD(n, name_i + 1)); | |
| 898 if (!args) | |
| 899 goto error; | |
| 900 body = ast_for_suite(c, CHILD(n, name_i + 3)); | |
| 901 if (!body) | |
| 902 goto error; | |
| 903 | |
| 904 return FunctionDef(name, args, body, decorator_seq, LINENO(n)); | |
| 905 | |
| 906 error: | |
| 907 asdl_stmt_seq_free(body); | |
| 908 asdl_expr_seq_free(decorator_seq); | |
| 909 free_arguments(args); | |
| 910 Py_XDECREF(name); | |
| 911 return NULL; | |
| 912 } | |
| 913 | |
| 914 static expr_ty | |
| 915 ast_for_lambdef(struct compiling *c, const node *n) | |
| 916 { | |
| 917 /* lambdef: 'lambda' [varargslist] ':' test */ | |
| 918 arguments_ty args; | |
| 919 expr_ty expression; | |
| 920 | |
| 921 if (NCH(n) == 3) { | |
| 922 args = arguments(NULL, NULL, NULL, NULL); | |
| 923 if (!args) | |
| 924 return NULL; | |
| 925 expression = ast_for_expr(c, CHILD(n, 2)); | |
| 926 if (!expression) { | |
| 927 free_arguments(args); | |
| 928 return NULL; | |
| 929 } | |
| 930 } | |
| 931 else { | |
| 932 args = ast_for_arguments(c, CHILD(n, 1)); | |
| 933 if (!args) | |
| 934 return NULL; | |
| 935 expression = ast_for_expr(c, CHILD(n, 3)); | |
| 936 if (!expression) { | |
| 937 free_arguments(args); | |
| 938 return NULL; | |
| 939 } | |
| 940 } | |
| 941 | |
| 942 return Lambda(args, expression, LINENO(n)); | |
| 943 } | |
| 944 | |
| 945 /* Count the number of 'for' loop in a list comprehension. | |
| 946 | |
| 947 Helper for ast_for_listcomp(). | |
| 948 */ | |
| 949 | |
| 950 static int | |
| 951 count_list_fors(const node *n) | |
| 952 { | |
| 953 int n_fors = 0; | |
| 954 node *ch = CHILD(n, 1); | |
| 955 | |
| 956 count_list_for: | |
| 957 n_fors++; | |
| 958 REQ(ch, list_for); | |
| 959 if (NCH(ch) == 5) | |
| 960 ch = CHILD(ch, 4); | |
| 961 else | |
| 962 return n_fors; | |
| 963 count_list_iter: | |
| 964 REQ(ch, list_iter); | |
| 965 ch = CHILD(ch, 0); | |
| 966 if (TYPE(ch) == list_for) | |
| 967 goto count_list_for; | |
| 968 else if (TYPE(ch) == list_if) { | |
| 969 if (NCH(ch) == 3) { | |
| 970 ch = CHILD(ch, 2); | |
| 971 goto count_list_iter; | |
| 972 } | |
| 973 else | |
| 974 return n_fors; | |
| 975 } | |
| 976 else { | |
| 977 /* Should never be reached */ | |
|
35867
f4842f42700b
remove useless debug print helper. fix a couple of exceptions
Neal Norwitz <nnorwitz@gmail.com>
parents:
35799
diff
changeset
|
978 PyErr_SetString(PyExc_SystemError, "logic error in count_list_fors"); |
| 35742 | 979 return -1; |
| 980 } | |
| 981 } | |
| 982 | |
| 983 /* Count the number of 'if' statements in a list comprehension. | |
| 984 | |
| 985 Helper for ast_for_listcomp(). | |
| 986 */ | |
| 987 | |
| 988 static int | |
| 989 count_list_ifs(const node *n) | |
| 990 { | |
| 991 int n_ifs = 0; | |
| 992 | |
| 993 count_list_iter: | |
| 994 REQ(n, list_iter); | |
| 995 if (TYPE(CHILD(n, 0)) == list_for) | |
| 996 return n_ifs; | |
| 997 n = CHILD(n, 0); | |
| 998 REQ(n, list_if); | |
| 999 n_ifs++; | |
| 1000 if (NCH(n) == 2) | |
| 1001 return n_ifs; | |
| 1002 n = CHILD(n, 2); | |
| 1003 goto count_list_iter; | |
| 1004 } | |
| 1005 | |
| 1006 static expr_ty | |
| 1007 ast_for_listcomp(struct compiling *c, const node *n) | |
| 1008 { | |
| 1009 /* listmaker: test ( list_for | (',' test)* [','] ) | |
| 1010 list_for: 'for' exprlist 'in' testlist_safe [list_iter] | |
| 1011 list_iter: list_for | list_if | |
| 1012 list_if: 'if' test [list_iter] | |
| 1013 testlist_safe: test [(',' test)+ [',']] | |
| 1014 */ | |
| 1015 expr_ty elt; | |
| 1016 asdl_seq *listcomps; | |
| 1017 int i, n_fors; | |
| 1018 node *ch; | |
| 1019 | |
| 1020 REQ(n, listmaker); | |
| 1021 assert(NCH(n) > 1); | |
| 1022 | |
| 1023 elt = ast_for_expr(c, CHILD(n, 0)); | |
| 1024 if (!elt) | |
| 1025 return NULL; | |
| 1026 | |
| 1027 n_fors = count_list_fors(n); | |
| 1028 if (n_fors == -1) | |
| 1029 return NULL; | |
| 1030 | |
| 1031 listcomps = asdl_seq_new(n_fors); | |
| 1032 if (!listcomps) { | |
| 1033 free_expr(elt); | |
| 1034 return NULL; | |
| 1035 } | |
| 1036 | |
| 1037 ch = CHILD(n, 1); | |
| 1038 for (i = 0; i < n_fors; i++) { | |
| 1039 comprehension_ty lc; | |
| 1040 asdl_seq *t; | |
| 1041 expr_ty expression; | |
| 1042 | |
| 1043 REQ(ch, list_for); | |
| 1044 | |
| 1045 t = ast_for_exprlist(c, CHILD(ch, 1), Store); | |
| 1046 if (!t) { | |
|
35873
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
1047 asdl_comprehension_seq_free(listcomps); |
| 35742 | 1048 free_expr(elt); |
| 1049 return NULL; | |
| 1050 } | |
|
35798
857f68fba275
Write a separate ast_for_testlist_gexp() function instead of overloading
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35771
diff
changeset
|
1051 expression = ast_for_testlist(c, CHILD(ch, 3)); |
| 35742 | 1052 if (!expression) { |
|
35873
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
1053 asdl_expr_seq_free(t); |
|
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
1054 asdl_comprehension_seq_free(listcomps); |
| 35742 | 1055 free_expr(elt); |
| 1056 return NULL; | |
| 1057 } | |
| 1058 | |
|
35895
fe4c627b2096
Fix a few more memory leaks
Neal Norwitz <nnorwitz@gmail.com>
parents:
35879
diff
changeset
|
1059 if (asdl_seq_LEN(t) == 1) { |
| 35742 | 1060 lc = comprehension(asdl_seq_GET(t, 0), expression, NULL); |
|
35895
fe4c627b2096
Fix a few more memory leaks
Neal Norwitz <nnorwitz@gmail.com>
parents:
35879
diff
changeset
|
1061 /* only free the sequence since we grabbed element 0 above */ |
|
fe4c627b2096
Fix a few more memory leaks
Neal Norwitz <nnorwitz@gmail.com>
parents:
35879
diff
changeset
|
1062 if (lc) |
|
fe4c627b2096
Fix a few more memory leaks
Neal Norwitz <nnorwitz@gmail.com>
parents:
35879
diff
changeset
|
1063 asdl_seq_free(t); /* ok */ |
|
fe4c627b2096
Fix a few more memory leaks
Neal Norwitz <nnorwitz@gmail.com>
parents:
35879
diff
changeset
|
1064 } |
| 35742 | 1065 else |
| 1066 lc = comprehension(Tuple(t, Store, LINENO(ch)), expression, NULL); | |
| 1067 | |
| 1068 if (!lc) { | |
|
35873
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
1069 asdl_expr_seq_free(t); |
|
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
1070 asdl_comprehension_seq_free(listcomps); |
| 35742 | 1071 free_expr(expression); |
| 1072 free_expr(elt); | |
| 1073 return NULL; | |
| 1074 } | |
| 1075 | |
| 1076 if (NCH(ch) == 5) { | |
| 1077 int j, n_ifs; | |
| 1078 asdl_seq *ifs; | |
| 1079 | |
| 1080 ch = CHILD(ch, 4); | |
| 1081 n_ifs = count_list_ifs(ch); | |
| 1082 if (n_ifs == -1) { | |
|
35873
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
1083 free_comprehension(lc); |
|
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
1084 asdl_comprehension_seq_free(listcomps); |
| 35742 | 1085 free_expr(elt); |
| 1086 return NULL; | |
| 1087 } | |
| 1088 | |
| 1089 ifs = asdl_seq_new(n_ifs); | |
| 1090 if (!ifs) { | |
|
35873
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
1091 free_comprehension(lc); |
|
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
1092 asdl_comprehension_seq_free(listcomps); |
| 35742 | 1093 free_expr(elt); |
| 1094 return NULL; | |
| 1095 } | |
| 1096 | |
| 1097 for (j = 0; j < n_ifs; j++) { | |
| 1098 REQ(ch, list_iter); | |
| 1099 | |
| 1100 ch = CHILD(ch, 0); | |
| 1101 REQ(ch, list_if); | |
| 1102 | |
| 1103 asdl_seq_APPEND(ifs, ast_for_expr(c, CHILD(ch, 1))); | |
| 1104 if (NCH(ch) == 3) | |
| 1105 ch = CHILD(ch, 2); | |
| 1106 } | |
| 1107 /* on exit, must guarantee that ch is a list_for */ | |
| 1108 if (TYPE(ch) == list_iter) | |
| 1109 ch = CHILD(ch, 0); | |
| 1110 lc->ifs = ifs; | |
| 1111 } | |
| 1112 asdl_seq_APPEND(listcomps, lc); | |
| 1113 } | |
| 1114 | |
| 1115 return ListComp(elt, listcomps, LINENO(n)); | |
| 1116 } | |
| 1117 | |
| 1118 /* | |
| 1119 Count the number of 'for' loops in a generator expression. | |
| 1120 | |
| 1121 Helper for ast_for_genexp(). | |
| 1122 */ | |
| 1123 | |
| 1124 static int | |
| 1125 count_gen_fors(const node *n) | |
| 1126 { | |
| 1127 int n_fors = 0; | |
| 1128 node *ch = CHILD(n, 1); | |
| 1129 | |
| 1130 count_gen_for: | |
| 1131 n_fors++; | |
| 1132 REQ(ch, gen_for); | |
| 1133 if (NCH(ch) == 5) | |
| 1134 ch = CHILD(ch, 4); | |
| 1135 else | |
| 1136 return n_fors; | |
| 1137 count_gen_iter: | |
| 1138 REQ(ch, gen_iter); | |
| 1139 ch = CHILD(ch, 0); | |
| 1140 if (TYPE(ch) == gen_for) | |
| 1141 goto count_gen_for; | |
| 1142 else if (TYPE(ch) == gen_if) { | |
| 1143 if (NCH(ch) == 3) { | |
| 1144 ch = CHILD(ch, 2); | |
| 1145 goto count_gen_iter; | |
| 1146 } | |
| 1147 else | |
| 1148 return n_fors; | |
| 1149 } | |
| 1150 else { | |
| 1151 /* Should never be reached */ | |
|
35867
f4842f42700b
remove useless debug print helper. fix a couple of exceptions
Neal Norwitz <nnorwitz@gmail.com>
parents:
35799
diff
changeset
|
1152 PyErr_SetString(PyExc_SystemError, |
|
f4842f42700b
remove useless debug print helper. fix a couple of exceptions
Neal Norwitz <nnorwitz@gmail.com>
parents:
35799
diff
changeset
|
1153 "logic error in count_gen_fors"); |
| 35742 | 1154 return -1; |
| 1155 } | |
| 1156 } | |
| 1157 | |
| 1158 /* Count the number of 'if' statements in a generator expression. | |
| 1159 | |
| 1160 Helper for ast_for_genexp(). | |
| 1161 */ | |
| 1162 | |
| 1163 static int | |
| 1164 count_gen_ifs(const node *n) | |
| 1165 { | |
| 1166 int n_ifs = 0; | |
| 1167 | |
| 1168 while (1) { | |
| 1169 REQ(n, gen_iter); | |
| 1170 if (TYPE(CHILD(n, 0)) == gen_for) | |
| 1171 return n_ifs; | |
| 1172 n = CHILD(n, 0); | |
| 1173 REQ(n, gen_if); | |
| 1174 n_ifs++; | |
| 1175 if (NCH(n) == 2) | |
| 1176 return n_ifs; | |
| 1177 n = CHILD(n, 2); | |
| 1178 } | |
| 1179 } | |
| 1180 | |
| 1181 static expr_ty | |
| 1182 ast_for_genexp(struct compiling *c, const node *n) | |
| 1183 { | |
| 1184 /* testlist_gexp: test ( gen_for | (',' test)* [','] ) | |
| 1185 argument: [test '='] test [gen_for] # Really [keyword '='] test */ | |
| 1186 expr_ty elt; | |
| 1187 asdl_seq *genexps; | |
| 1188 int i, n_fors; | |
| 1189 node *ch; | |
| 1190 | |
| 1191 assert(TYPE(n) == (testlist_gexp) || TYPE(n) == (argument)); | |
| 1192 assert(NCH(n) > 1); | |
| 1193 | |
| 1194 elt = ast_for_expr(c, CHILD(n, 0)); | |
| 1195 if (!elt) | |
| 1196 return NULL; | |
| 1197 | |
| 1198 n_fors = count_gen_fors(n); | |
| 1199 if (n_fors == -1) | |
| 1200 return NULL; | |
| 1201 | |
| 1202 genexps = asdl_seq_new(n_fors); | |
| 1203 if (!genexps) { | |
| 1204 free_expr(elt); | |
| 1205 return NULL; | |
| 1206 } | |
| 1207 | |
| 1208 ch = CHILD(n, 1); | |
| 1209 for (i = 0; i < n_fors; i++) { | |
| 1210 comprehension_ty ge; | |
| 1211 asdl_seq *t; | |
| 1212 expr_ty expression; | |
| 1213 | |
| 1214 REQ(ch, gen_for); | |
| 1215 | |
| 1216 t = ast_for_exprlist(c, CHILD(ch, 1), Store); | |
| 1217 if (!t) { | |
|
35873
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
1218 asdl_comprehension_seq_free(genexps); |
|
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
1219 asdl_expr_seq_free(t); |
| 35742 | 1220 free_expr(elt); |
| 1221 return NULL; | |
| 1222 } | |
|
35798
857f68fba275
Write a separate ast_for_testlist_gexp() function instead of overloading
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35771
diff
changeset
|
1223 expression = ast_for_expr(c, CHILD(ch, 3)); |
| 35742 | 1224 if (!expression) { |
|
35873
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
1225 asdl_comprehension_seq_free(genexps); |
|
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
1226 asdl_expr_seq_free(t); |
| 35742 | 1227 free_expr(elt); |
| 1228 return NULL; | |
| 1229 } | |
| 1230 | |
|
35895
fe4c627b2096
Fix a few more memory leaks
Neal Norwitz <nnorwitz@gmail.com>
parents:
35879
diff
changeset
|
1231 if (asdl_seq_LEN(t) == 1) { |
| 35742 | 1232 ge = comprehension(asdl_seq_GET(t, 0), expression, |
| 1233 NULL); | |
|
35895
fe4c627b2096
Fix a few more memory leaks
Neal Norwitz <nnorwitz@gmail.com>
parents:
35879
diff
changeset
|
1234 /* only free the sequence since we grabbed element 0 above */ |
|
fe4c627b2096
Fix a few more memory leaks
Neal Norwitz <nnorwitz@gmail.com>
parents:
35879
diff
changeset
|
1235 if (ge) |
|
fe4c627b2096
Fix a few more memory leaks
Neal Norwitz <nnorwitz@gmail.com>
parents:
35879
diff
changeset
|
1236 asdl_seq_free(t); /* ok */ |
|
fe4c627b2096
Fix a few more memory leaks
Neal Norwitz <nnorwitz@gmail.com>
parents:
35879
diff
changeset
|
1237 } |
| 35742 | 1238 else |
| 1239 ge = comprehension(Tuple(t, Store, LINENO(ch)), | |
| 1240 expression, NULL); | |
| 1241 | |
| 1242 if (!ge) { | |
|
35873
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
1243 asdl_comprehension_seq_free(genexps); |
|
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
1244 asdl_expr_seq_free(t); |
| 35742 | 1245 free_expr(elt); |
| 1246 return NULL; | |
| 1247 } | |
| 1248 | |
| 1249 if (NCH(ch) == 5) { | |
| 1250 int j, n_ifs; | |
| 1251 asdl_seq *ifs; | |
| 1252 | |
| 1253 ch = CHILD(ch, 4); | |
| 1254 n_ifs = count_gen_ifs(ch); | |
| 1255 if (n_ifs == -1) { | |
|
35873
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
1256 asdl_comprehension_seq_free(genexps); |
|
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
1257 free_comprehension(ge); |
| 35742 | 1258 free_expr(elt); |
| 1259 return NULL; | |
| 1260 } | |
| 1261 | |
| 1262 ifs = asdl_seq_new(n_ifs); | |
| 1263 if (!ifs) { | |
|
35873
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
1264 asdl_comprehension_seq_free(genexps); |
|
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
1265 free_comprehension(ge); |
| 35742 | 1266 free_expr(elt); |
| 1267 return NULL; | |
| 1268 } | |
| 1269 | |
| 1270 for (j = 0; j < n_ifs; j++) { | |
|
35798
857f68fba275
Write a separate ast_for_testlist_gexp() function instead of overloading
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35771
diff
changeset
|
1271 expr_ty expression; |
| 35742 | 1272 REQ(ch, gen_iter); |
| 1273 ch = CHILD(ch, 0); | |
| 1274 REQ(ch, gen_if); | |
| 1275 | |
|
35798
857f68fba275
Write a separate ast_for_testlist_gexp() function instead of overloading
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35771
diff
changeset
|
1276 expression = ast_for_expr(c, CHILD(ch, 1)); |
|
857f68fba275
Write a separate ast_for_testlist_gexp() function instead of overloading
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35771
diff
changeset
|
1277 if (!expression) { |
|
35873
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
1278 asdl_expr_seq_free(ifs); |
|
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
1279 asdl_comprehension_seq_free(genexps); |
|
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
1280 free_comprehension(ge); |
|
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
1281 free_expr(elt); |
|
35798
857f68fba275
Write a separate ast_for_testlist_gexp() function instead of overloading
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35771
diff
changeset
|
1282 return NULL; |
|
857f68fba275
Write a separate ast_for_testlist_gexp() function instead of overloading
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35771
diff
changeset
|
1283 } |
|
857f68fba275
Write a separate ast_for_testlist_gexp() function instead of overloading
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35771
diff
changeset
|
1284 asdl_seq_APPEND(ifs, expression); |
| 35742 | 1285 if (NCH(ch) == 3) |
| 1286 ch = CHILD(ch, 2); | |
| 1287 } | |
| 1288 /* on exit, must guarantee that ch is a gen_for */ | |
| 1289 if (TYPE(ch) == gen_iter) | |
| 1290 ch = CHILD(ch, 0); | |
| 1291 ge->ifs = ifs; | |
| 1292 } | |
| 1293 asdl_seq_APPEND(genexps, ge); | |
| 1294 } | |
| 1295 | |
| 1296 return GeneratorExp(elt, genexps, LINENO(n)); | |
| 1297 } | |
| 1298 | |
| 1299 static expr_ty | |
| 1300 ast_for_atom(struct compiling *c, const node *n) | |
| 1301 { | |
| 1302 /* atom: '(' [yield_expr|testlist_gexp] ')' | '[' [listmaker] ']' | |
| 1303 | '{' [dictmaker] '}' | '`' testlist '`' | NAME | NUMBER | STRING+ | |
| 1304 */ | |
| 1305 node *ch = CHILD(n, 0); | |
| 1306 | |
| 1307 switch (TYPE(ch)) { | |
| 1308 case NAME: | |
| 1309 /* All names start in Load context, but may later be | |
| 1310 changed. */ | |
| 1311 return Name(NEW_IDENTIFIER(ch), Load, LINENO(n)); | |
| 1312 case STRING: { | |
| 1313 PyObject *str = parsestrplus(c, n); | |
| 1314 | |
| 1315 if (!str) | |
| 1316 return NULL; | |
| 1317 | |
| 1318 return Str(str, LINENO(n)); | |
| 1319 } | |
| 1320 case NUMBER: { | |
| 1321 PyObject *pynum = parsenumber(STR(ch)); | |
| 1322 | |
| 1323 if (!pynum) | |
| 1324 return NULL; | |
| 1325 | |
| 1326 return Num(pynum, LINENO(n)); | |
| 1327 } | |
| 1328 case LPAR: /* some parenthesized expressions */ | |
| 1329 ch = CHILD(n, 1); | |
| 1330 | |
| 1331 if (TYPE(ch) == RPAR) | |
| 1332 return Tuple(NULL, Load, LINENO(n)); | |
| 1333 | |
| 1334 if (TYPE(ch) == yield_expr) | |
| 1335 return ast_for_expr(c, ch); | |
| 1336 | |
| 1337 if ((NCH(ch) > 1) && (TYPE(CHILD(ch, 1)) == gen_for)) | |
| 1338 return ast_for_genexp(c, ch); | |
| 1339 | |
|
35798
857f68fba275
Write a separate ast_for_testlist_gexp() function instead of overloading
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35771
diff
changeset
|
1340 return ast_for_testlist_gexp(c, ch); |
| 35742 | 1341 case LSQB: /* list (or list comprehension) */ |
| 1342 ch = CHILD(n, 1); | |
| 1343 | |
| 1344 if (TYPE(ch) == RSQB) | |
| 1345 return List(NULL, Load, LINENO(n)); | |
| 1346 | |
| 1347 REQ(ch, listmaker); | |
| 1348 if (NCH(ch) == 1 || TYPE(CHILD(ch, 1)) == COMMA) { | |
| 1349 asdl_seq *elts = seq_for_testlist(c, ch); | |
| 1350 | |
| 1351 if (!elts) | |
| 1352 return NULL; | |
| 1353 | |
| 1354 return List(elts, Load, LINENO(n)); | |
| 1355 } | |
| 1356 else | |
| 1357 return ast_for_listcomp(c, ch); | |
| 1358 case LBRACE: { | |
| 1359 /* dictmaker: test ':' test (',' test ':' test)* [','] */ | |
| 1360 int i, size; | |
| 1361 asdl_seq *keys, *values; | |
| 1362 | |
| 1363 ch = CHILD(n, 1); | |
| 1364 size = (NCH(ch) + 1) / 4; /* +1 in case no trailing comma */ | |
| 1365 keys = asdl_seq_new(size); | |
| 1366 if (!keys) | |
| 1367 return NULL; | |
| 1368 | |
| 1369 values = asdl_seq_new(size); | |
| 1370 if (!values) { | |
|
35873
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
1371 asdl_seq_free(keys); /* ok */ |
| 35742 | 1372 return NULL; |
| 1373 } | |
| 1374 | |
| 1375 for (i = 0; i < NCH(ch); i += 4) { | |
| 1376 expr_ty expression; | |
| 1377 | |
| 1378 expression = ast_for_expr(c, CHILD(ch, i)); | |
|
35873
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
1379 if (!expression) { |
|
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
1380 asdl_expr_seq_free(keys); |
|
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
1381 asdl_expr_seq_free(values); |
| 35742 | 1382 return NULL; |
|
35873
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
1383 } |
| 35742 | 1384 |
| 1385 asdl_seq_SET(keys, i / 4, expression); | |
| 1386 | |
| 1387 expression = ast_for_expr(c, CHILD(ch, i + 2)); | |
|
35873
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
1388 if (!expression) { |
|
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
1389 asdl_expr_seq_free(keys); |
|
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
1390 asdl_expr_seq_free(values); |
| 35742 | 1391 return NULL; |
|
35873
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
1392 } |
|
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
1393 |
| 35742 | 1394 asdl_seq_SET(values, i / 4, expression); |
| 1395 } | |
| 1396 return Dict(keys, values, LINENO(n)); | |
| 1397 } | |
| 1398 case BACKQUOTE: { /* repr */ | |
|
35798
857f68fba275
Write a separate ast_for_testlist_gexp() function instead of overloading
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35771
diff
changeset
|
1399 expr_ty expression = ast_for_testlist(c, CHILD(n, 1)); |
| 35742 | 1400 |
| 1401 if (!expression) | |
| 1402 return NULL; | |
| 1403 | |
| 1404 return Repr(expression, LINENO(n)); | |
| 1405 } | |
| 1406 default: | |
|
35870
f531c05cd240
Convert all internal errors from Exception to SystemError
Neal Norwitz <nnorwitz@gmail.com>
parents:
35868
diff
changeset
|
1407 PyErr_Format(PyExc_SystemError, "unhandled atom %d", TYPE(ch)); |
| 35742 | 1408 return NULL; |
| 1409 } | |
| 1410 } | |
| 1411 | |
| 1412 static slice_ty | |
| 1413 ast_for_slice(struct compiling *c, const node *n) | |
| 1414 { | |
| 1415 node *ch; | |
| 1416 expr_ty lower = NULL, upper = NULL, step = NULL; | |
| 1417 | |
| 1418 REQ(n, subscript); | |
| 1419 | |
| 1420 /* | |
| 1421 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop] | |
| 1422 sliceop: ':' [test] | |
| 1423 */ | |
| 1424 ch = CHILD(n, 0); | |
| 1425 if (TYPE(ch) == DOT) | |
| 1426 return Ellipsis(); | |
| 1427 | |
| 1428 if (NCH(n) == 1 && TYPE(ch) == test) { | |
| 1429 /* 'step' variable hold no significance in terms of being used over | |
| 1430 other vars */ | |
| 1431 step = ast_for_expr(c, ch); | |
| 1432 if (!step) | |
| 1433 return NULL; | |
| 1434 | |
| 1435 return Index(step); | |
| 1436 } | |
| 1437 | |
| 1438 if (TYPE(ch) == test) { | |
| 1439 lower = ast_for_expr(c, ch); | |
| 1440 if (!lower) | |
| 1441 return NULL; | |
| 1442 } | |
| 1443 | |
| 1444 /* If there's an upper bound it's in the second or third position. */ | |
| 1445 if (TYPE(ch) == COLON) { | |
| 1446 if (NCH(n) > 1) { | |
| 1447 node *n2 = CHILD(n, 1); | |
| 1448 | |
| 1449 if (TYPE(n2) == test) { | |
| 1450 upper = ast_for_expr(c, n2); | |
| 1451 if (!upper) | |
| 1452 return NULL; | |
| 1453 } | |
| 1454 } | |
| 1455 } else if (NCH(n) > 2) { | |
| 1456 node *n2 = CHILD(n, 2); | |
| 1457 | |
| 1458 if (TYPE(n2) == test) { | |
| 1459 upper = ast_for_expr(c, n2); | |
| 1460 if (!upper) | |
| 1461 return NULL; | |
| 1462 } | |
| 1463 } | |
| 1464 | |
| 1465 ch = CHILD(n, NCH(n) - 1); | |
| 1466 if (TYPE(ch) == sliceop) { | |
| 1467 if (NCH(ch) == 1) | |
| 1468 /* XXX: If only 1 child, then should just be a colon. Should we | |
| 1469 just skip assigning and just get to the return? */ | |
| 1470 ch = CHILD(ch, 0); | |
| 1471 else | |
| 1472 ch = CHILD(ch, 1); | |
| 1473 if (TYPE(ch) == test) { | |
| 1474 step = ast_for_expr(c, ch); | |
| 1475 if (!step) | |
| 1476 return NULL; | |
| 1477 } | |
| 1478 } | |
| 1479 | |
| 1480 return Slice(lower, upper, step); | |
| 1481 } | |
| 1482 | |
| 1483 static expr_ty | |
| 1484 ast_for_binop(struct compiling *c, const node *n) | |
| 1485 { | |
| 1486 /* Must account for a sequence of expressions. | |
| 1487 How should A op B op C by represented? | |
| 1488 BinOp(BinOp(A, op, B), op, C). | |
| 1489 */ | |
| 1490 | |
| 1491 int i, nops; | |
| 1492 expr_ty expr1, expr2, result; | |
| 1493 operator_ty operator; | |
| 1494 | |
| 1495 expr1 = ast_for_expr(c, CHILD(n, 0)); | |
| 1496 if (!expr1) | |
| 1497 return NULL; | |
| 1498 | |
| 1499 expr2 = ast_for_expr(c, CHILD(n, 2)); | |
| 1500 if (!expr2) | |
| 1501 return NULL; | |
| 1502 | |
| 1503 operator = get_operator(CHILD(n, 1)); | |
| 1504 if (!operator) | |
| 1505 return NULL; | |
| 1506 | |
| 1507 result = BinOp(expr1, operator, expr2, LINENO(n)); | |
| 1508 if (!result) | |
| 1509 return NULL; | |
| 1510 | |
| 1511 nops = (NCH(n) - 1) / 2; | |
| 1512 for (i = 1; i < nops; i++) { | |
| 1513 expr_ty tmp_result, tmp; | |
| 1514 const node* next_oper = CHILD(n, i * 2 + 1); | |
| 1515 | |
| 1516 operator = get_operator(next_oper); | |
| 1517 if (!operator) | |
| 1518 return NULL; | |
| 1519 | |
| 1520 tmp = ast_for_expr(c, CHILD(n, i * 2 + 2)); | |
| 1521 if (!tmp) | |
| 1522 return NULL; | |
| 1523 | |
| 1524 tmp_result = BinOp(result, operator, tmp, | |
| 1525 LINENO(next_oper)); | |
| 1526 if (!tmp) | |
| 1527 return NULL; | |
| 1528 result = tmp_result; | |
| 1529 } | |
| 1530 return result; | |
| 1531 } | |
| 1532 | |
|
35799
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1533 static expr_ty |
|
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1534 ast_for_trailer(struct compiling *c, const node *n, expr_ty left_expr) |
|
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1535 { |
|
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1536 /* trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME */ |
|
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1537 expr_ty e; |
|
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1538 REQ(n, trailer); |
|
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1539 if (TYPE(CHILD(n, 0)) == LPAR) { |
|
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1540 if (NCH(n) == 2) |
|
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1541 e = Call(left_expr, NULL, NULL, NULL, NULL, LINENO(n)); |
|
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1542 else |
|
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1543 e = ast_for_call(c, CHILD(n, 1), left_expr); |
|
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1544 } |
|
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1545 else if (TYPE(CHILD(n, 0)) == LSQB) { |
|
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1546 REQ(CHILD(n, 2), RSQB); |
|
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1547 n = CHILD(n, 1); |
|
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1548 if (NCH(n) <= 2) { |
|
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1549 slice_ty slc = ast_for_slice(c, CHILD(n, 0)); |
|
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1550 if (!slc) |
|
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1551 return NULL; |
|
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1552 e = Subscript(left_expr, slc, Load, LINENO(n)); |
|
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1553 if (!e) { |
|
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1554 free_slice(slc); |
|
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1555 return NULL; |
|
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1556 } |
|
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1557 } |
|
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1558 else { |
|
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1559 int j; |
|
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1560 slice_ty slc; |
|
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1561 asdl_seq *slices = asdl_seq_new((NCH(n) + 1) / 2); |
|
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1562 if (!slices) |
|
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1563 return NULL; |
|
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1564 for (j = 0; j < NCH(n); j += 2) { |
|
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1565 slc = ast_for_slice(c, CHILD(n, j)); |
|
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1566 if (!slc) { |
|
35873
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
1567 for (j = j / 2; j >= 0; j--) |
|
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
1568 free_slice(asdl_seq_GET(slices, j)); |
|
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
1569 asdl_seq_free(slices); /* ok */ |
|
35799
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1570 return NULL; |
|
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1571 } |
|
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1572 asdl_seq_SET(slices, j / 2, slc); |
|
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1573 } |
|
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1574 e = Subscript(left_expr, ExtSlice(slices), Load, LINENO(n)); |
|
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1575 if (!e) { |
|
35873
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
1576 for (j = 0; j < asdl_seq_LEN(slices); j++) |
|
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
1577 free_slice(asdl_seq_GET(slices, j)); |
|
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
1578 asdl_seq_free(slices); /* ok */ |
|
35799
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1579 return NULL; |
|
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1580 } |
|
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1581 } |
|
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1582 } |
|
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1583 else { |
|
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1584 assert(TYPE(CHILD(n, 0)) == DOT); |
|
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1585 e = Attribute(left_expr, NEW_IDENTIFIER(CHILD(n, 1)), Load, LINENO(n)); |
|
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1586 } |
|
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1587 return e; |
|
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1588 } |
|
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1589 |
|
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1590 static expr_ty |
|
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1591 ast_for_power(struct compiling *c, const node *n) |
|
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1592 { |
|
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1593 /* power: atom trailer* ('**' factor)* |
|
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1594 */ |
|
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1595 int i; |
|
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1596 expr_ty e, tmp; |
|
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1597 REQ(n, power); |
|
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1598 e = ast_for_atom(c, CHILD(n, 0)); |
|
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1599 if (!e) |
|
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1600 return NULL; |
|
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1601 if (NCH(n) == 1) |
|
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1602 return e; |
|
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1603 for (i = 1; i < NCH(n); i++) { |
|
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1604 node *ch = CHILD(n, i); |
|
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1605 if (TYPE(ch) != trailer) |
|
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1606 break; |
|
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1607 tmp = ast_for_trailer(c, ch, e); |
|
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1608 if (!tmp) { |
|
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1609 free_expr(e); |
|
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1610 return NULL; |
|
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1611 } |
|
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1612 e = tmp; |
|
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1613 } |
|
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1614 if (TYPE(CHILD(n, NCH(n) - 1)) == factor) { |
|
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1615 expr_ty f = ast_for_expr(c, CHILD(n, NCH(n) - 1)); |
|
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1616 if (!f) { |
|
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1617 free_expr(e); |
|
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1618 return NULL; |
|
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1619 } |
|
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1620 tmp = BinOp(e, Pow, f, LINENO(n)); |
|
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1621 if (!tmp) { |
|
35875
086caccfff0e
Fix another memory leak or two (one real, one potential)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35874
diff
changeset
|
1622 free_expr(f); |
|
35799
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1623 free_expr(e); |
|
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1624 return NULL; |
|
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1625 } |
|
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1626 e = tmp; |
|
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1627 } |
|
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1628 return e; |
|
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1629 } |
|
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1630 |
| 35742 | 1631 /* Do not name a variable 'expr'! Will cause a compile error. |
| 1632 */ | |
| 1633 | |
| 1634 static expr_ty | |
| 1635 ast_for_expr(struct compiling *c, const node *n) | |
| 1636 { | |
| 1637 /* handle the full range of simple expressions | |
| 1638 test: and_test ('or' and_test)* | lambdef | |
| 1639 and_test: not_test ('and' not_test)* | |
| 1640 not_test: 'not' not_test | comparison | |
| 1641 comparison: expr (comp_op expr)* | |
| 1642 expr: xor_expr ('|' xor_expr)* | |
| 1643 xor_expr: and_expr ('^' and_expr)* | |
| 1644 and_expr: shift_expr ('&' shift_expr)* | |
| 1645 shift_expr: arith_expr (('<<'|'>>') arith_expr)* | |
| 1646 arith_expr: term (('+'|'-') term)* | |
| 1647 term: factor (('*'|'/'|'%'|'//') factor)* | |
| 1648 factor: ('+'|'-'|'~') factor | power | |
| 1649 power: atom trailer* ('**' factor)* | |
| 1650 */ | |
| 1651 | |
| 1652 asdl_seq *seq; | |
| 1653 int i; | |
| 1654 | |
| 1655 loop: | |
| 1656 switch (TYPE(n)) { | |
| 1657 case test: | |
| 1658 if (TYPE(CHILD(n, 0)) == lambdef) | |
| 1659 return ast_for_lambdef(c, CHILD(n, 0)); | |
| 1660 /* Fall through to and_test */ | |
| 1661 case and_test: | |
| 1662 if (NCH(n) == 1) { | |
| 1663 n = CHILD(n, 0); | |
| 1664 goto loop; | |
| 1665 } | |
| 1666 seq = asdl_seq_new((NCH(n) + 1) / 2); | |
| 1667 if (!seq) | |
| 1668 return NULL; | |
| 1669 for (i = 0; i < NCH(n); i += 2) { | |
| 1670 expr_ty e = ast_for_expr(c, CHILD(n, i)); | |
| 1671 if (!e) | |
| 1672 return NULL; | |
| 1673 asdl_seq_SET(seq, i / 2, e); | |
| 1674 } | |
| 1675 if (!strcmp(STR(CHILD(n, 1)), "and")) | |
| 1676 return BoolOp(And, seq, LINENO(n)); | |
| 1677 else { | |
| 1678 assert(!strcmp(STR(CHILD(n, 1)), "or")); | |
| 1679 return BoolOp(Or, seq, LINENO(n)); | |
| 1680 } | |
| 1681 break; | |
| 1682 case not_test: | |
| 1683 if (NCH(n) == 1) { | |
| 1684 n = CHILD(n, 0); | |
| 1685 goto loop; | |
| 1686 } | |
| 1687 else { | |
| 1688 expr_ty expression = ast_for_expr(c, CHILD(n, 1)); | |
| 1689 if (!expression) | |
| 1690 return NULL; | |
| 1691 | |
| 1692 return UnaryOp(Not, expression, LINENO(n)); | |
| 1693 } | |
| 1694 case comparison: | |
| 1695 if (NCH(n) == 1) { | |
| 1696 n = CHILD(n, 0); | |
| 1697 goto loop; | |
| 1698 } | |
| 1699 else { | |
| 1700 expr_ty expression; | |
| 1701 asdl_seq *ops, *cmps; | |
| 1702 ops = asdl_seq_new(NCH(n) / 2); | |
| 1703 if (!ops) | |
| 1704 return NULL; | |
| 1705 cmps = asdl_seq_new(NCH(n) / 2); | |
| 1706 if (!cmps) { | |
|
35873
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
1707 asdl_seq_free(ops); /* ok */ |
| 35742 | 1708 return NULL; |
| 1709 } | |
| 1710 for (i = 1; i < NCH(n); i += 2) { | |
| 1711 /* XXX cmpop_ty is just an enum */ | |
| 1712 cmpop_ty operator; | |
| 1713 | |
| 1714 operator = ast_for_comp_op(CHILD(n, i)); | |
|
35873
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
1715 if (!operator) { |
|
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
1716 asdl_expr_seq_free(ops); |
|
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
1717 asdl_expr_seq_free(cmps); |
| 35742 | 1718 return NULL; |
|
35873
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
1719 } |
| 35742 | 1720 |
| 1721 expression = ast_for_expr(c, CHILD(n, i + 1)); | |
|
35873
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
1722 if (!expression) { |
|
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
1723 asdl_expr_seq_free(ops); |
|
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
1724 asdl_expr_seq_free(cmps); |
| 35742 | 1725 return NULL; |
|
35873
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
1726 } |
| 35742 | 1727 |
| 1728 asdl_seq_SET(ops, i / 2, (void *)operator); | |
| 1729 asdl_seq_SET(cmps, i / 2, expression); | |
| 1730 } | |
| 1731 expression = ast_for_expr(c, CHILD(n, 0)); | |
|
35873
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
1732 if (!expression) { |
|
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
1733 asdl_expr_seq_free(ops); |
|
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
1734 asdl_expr_seq_free(cmps); |
| 35742 | 1735 return NULL; |
|
35873
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
1736 } |
| 35742 | 1737 |
| 1738 return Compare(expression, ops, cmps, LINENO(n)); | |
| 1739 } | |
| 1740 break; | |
| 1741 | |
| 1742 /* The next five cases all handle BinOps. The main body of code | |
| 1743 is the same in each case, but the switch turned inside out to | |
| 1744 reuse the code for each type of operator. | |
| 1745 */ | |
| 1746 case expr: | |
| 1747 case xor_expr: | |
| 1748 case and_expr: | |
| 1749 case shift_expr: | |
| 1750 case arith_expr: | |
| 1751 case term: | |
| 1752 if (NCH(n) == 1) { | |
| 1753 n = CHILD(n, 0); | |
| 1754 goto loop; | |
| 1755 } | |
| 1756 return ast_for_binop(c, n); | |
| 1757 case yield_expr: { | |
| 1758 expr_ty exp = NULL; | |
| 1759 if (NCH(n) == 2) { | |
|
35798
857f68fba275
Write a separate ast_for_testlist_gexp() function instead of overloading
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35771
diff
changeset
|
1760 exp = ast_for_testlist(c, CHILD(n, 1)); |
| 35742 | 1761 if (!exp) |
| 1762 return NULL; | |
| 1763 } | |
| 1764 return Yield(exp, LINENO(n)); | |
| 1765 } | |
| 1766 case factor: { | |
| 1767 expr_ty expression; | |
| 1768 | |
| 1769 if (NCH(n) == 1) { | |
| 1770 n = CHILD(n, 0); | |
| 1771 goto loop; | |
| 1772 } | |
| 1773 | |
| 1774 expression = ast_for_expr(c, CHILD(n, 1)); | |
| 1775 if (!expression) | |
| 1776 return NULL; | |
| 1777 | |
| 1778 switch (TYPE(CHILD(n, 0))) { | |
| 1779 case PLUS: | |
| 1780 return UnaryOp(UAdd, expression, LINENO(n)); | |
| 1781 case MINUS: | |
| 1782 return UnaryOp(USub, expression, LINENO(n)); | |
| 1783 case TILDE: | |
| 1784 return UnaryOp(Invert, expression, LINENO(n)); | |
| 1785 } | |
|
35873
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
1786 PyErr_Format(PyExc_SystemError, "unhandled factor: %d", |
|
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
1787 TYPE(CHILD(n, 0))); |
| 35742 | 1788 break; |
| 1789 } | |
|
35799
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1790 case power: |
|
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1791 return ast_for_power(c, n); |
| 35742 | 1792 default: |
|
35870
f531c05cd240
Convert all internal errors from Exception to SystemError
Neal Norwitz <nnorwitz@gmail.com>
parents:
35868
diff
changeset
|
1793 PyErr_Format(PyExc_SystemError, "unhandled expr: %d", TYPE(n)); |
| 35742 | 1794 return NULL; |
| 1795 } | |
| 1796 /* should never get here */ | |
| 1797 return NULL; | |
| 1798 } | |
| 1799 | |
| 1800 static expr_ty | |
| 1801 ast_for_call(struct compiling *c, const node *n, expr_ty func) | |
| 1802 { | |
| 1803 /* | |
| 1804 arglist: (argument ',')* (argument [',']| '*' test [',' '**' test] | |
| 1805 | '**' test) | |
| 1806 argument: [test '='] test [gen_for] # Really [keyword '='] test | |
| 1807 */ | |
| 1808 | |
| 1809 int i, nargs, nkeywords, ngens; | |
| 1810 asdl_seq *args = NULL; | |
| 1811 asdl_seq *keywords = NULL; | |
| 1812 expr_ty vararg = NULL, kwarg = NULL; | |
| 1813 | |
| 1814 REQ(n, arglist); | |
| 1815 | |
| 1816 nargs = 0; | |
| 1817 nkeywords = 0; | |
| 1818 ngens = 0; | |
| 1819 for (i = 0; i < NCH(n); i++) { | |
| 1820 node *ch = CHILD(n, i); | |
| 1821 if (TYPE(ch) == argument) { | |
| 1822 if (NCH(ch) == 1) | |
| 1823 nargs++; | |
| 1824 else if (TYPE(CHILD(ch, 1)) == gen_for) | |
| 1825 ngens++; | |
| 1826 else | |
| 1827 nkeywords++; | |
| 1828 } | |
| 1829 } | |
| 1830 if (ngens > 1 || (ngens && (nargs || nkeywords))) { | |
| 1831 ast_error(n, "Generator expression must be parenthesised " | |
| 1832 "if not sole argument"); | |
| 1833 return NULL; | |
| 1834 } | |
| 1835 | |
| 1836 if (nargs + nkeywords + ngens > 255) { | |
| 1837 ast_error(n, "more than 255 arguments"); | |
| 1838 return NULL; | |
| 1839 } | |
| 1840 | |
| 1841 args = asdl_seq_new(nargs + ngens); | |
| 1842 if (!args) | |
| 1843 goto error; | |
| 1844 keywords = asdl_seq_new(nkeywords); | |
| 1845 if (!keywords) | |
| 1846 goto error; | |
| 1847 nargs = 0; | |
| 1848 nkeywords = 0; | |
| 1849 for (i = 0; i < NCH(n); i++) { | |
| 1850 node *ch = CHILD(n, i); | |
| 1851 if (TYPE(ch) == argument) { | |
| 1852 expr_ty e; | |
| 1853 if (NCH(ch) == 1) { | |
| 1854 e = ast_for_expr(c, CHILD(ch, 0)); | |
| 1855 if (!e) | |
| 1856 goto error; | |
| 1857 asdl_seq_SET(args, nargs++, e); | |
| 1858 } | |
| 1859 else if (TYPE(CHILD(ch, 1)) == gen_for) { | |
| 1860 e = ast_for_genexp(c, ch); | |
| 1861 if (!e) | |
| 1862 goto error; | |
| 1863 asdl_seq_SET(args, nargs++, e); | |
| 1864 } | |
| 1865 else { | |
| 1866 keyword_ty kw; | |
| 1867 identifier key; | |
| 1868 | |
| 1869 /* CHILD(ch, 0) is test, but must be an identifier? */ | |
| 1870 e = ast_for_expr(c, CHILD(ch, 0)); | |
| 1871 if (!e) | |
| 1872 goto error; | |
| 1873 /* f(lambda x: x[0] = 3) ends up getting parsed with | |
| 1874 * LHS test = lambda x: x[0], and RHS test = 3. | |
| 1875 * SF bug 132313 points out that complaining about a keyword | |
| 1876 * then is very confusing. | |
| 1877 */ | |
| 1878 if (e->kind == Lambda_kind) { | |
| 1879 ast_error(CHILD(ch, 0), "lambda cannot contain assignment"); | |
| 1880 goto error; | |
| 1881 } else if (e->kind != Name_kind) { | |
| 1882 ast_error(CHILD(ch, 0), "keyword can't be an expression"); | |
| 1883 goto error; | |
| 1884 } | |
| 1885 key = e->v.Name.id; | |
|
35873
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
1886 free(e); /* XXX: is free correct here? */ |
| 35742 | 1887 e = ast_for_expr(c, CHILD(ch, 2)); |
| 1888 if (!e) | |
| 1889 goto error; | |
| 1890 kw = keyword(key, e); | |
| 1891 if (!kw) | |
| 1892 goto error; | |
| 1893 asdl_seq_SET(keywords, nkeywords++, kw); | |
| 1894 } | |
| 1895 } | |
| 1896 else if (TYPE(ch) == STAR) { | |
| 1897 vararg = ast_for_expr(c, CHILD(n, i+1)); | |
| 1898 i++; | |
| 1899 } | |
| 1900 else if (TYPE(ch) == DOUBLESTAR) { | |
| 1901 kwarg = ast_for_expr(c, CHILD(n, i+1)); | |
| 1902 i++; | |
| 1903 } | |
| 1904 } | |
| 1905 | |
| 1906 return Call(func, args, keywords, vararg, kwarg, LINENO(n)); | |
| 1907 | |
| 1908 error: | |
|
35873
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
1909 free_expr(vararg); |
|
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
1910 free_expr(kwarg); |
| 35742 | 1911 if (args) |
|
35873
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
1912 asdl_expr_seq_free(args); |
|
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
1913 if (keywords) { |
|
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
1914 for (i = 0; i < asdl_seq_LEN(keywords); i++) |
|
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
1915 free_keyword(asdl_seq_GET(keywords, i)); |
|
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
1916 asdl_seq_free(keywords); /* ok */ |
|
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
1917 } |
| 35742 | 1918 return NULL; |
| 1919 } | |
| 1920 | |
| 1921 static expr_ty | |
|
35798
857f68fba275
Write a separate ast_for_testlist_gexp() function instead of overloading
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35771
diff
changeset
|
1922 ast_for_testlist(struct compiling *c, const node* n) |
| 35742 | 1923 { |
|
35798
857f68fba275
Write a separate ast_for_testlist_gexp() function instead of overloading
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35771
diff
changeset
|
1924 /* testlist_gexp: test (',' test)* [','] */ |
|
857f68fba275
Write a separate ast_for_testlist_gexp() function instead of overloading
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35771
diff
changeset
|
1925 /* testlist: test (',' test)* [','] */ |
|
857f68fba275
Write a separate ast_for_testlist_gexp() function instead of overloading
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35771
diff
changeset
|
1926 /* testlist_safe: test (',' test)+ [','] */ |
|
857f68fba275
Write a separate ast_for_testlist_gexp() function instead of overloading
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35771
diff
changeset
|
1927 /* testlist1: test (',' test)* */ |
| 35742 | 1928 assert(NCH(n) > 0); |
|
35798
857f68fba275
Write a separate ast_for_testlist_gexp() function instead of overloading
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35771
diff
changeset
|
1929 if (TYPE(n) == testlist_gexp) { |
|
857f68fba275
Write a separate ast_for_testlist_gexp() function instead of overloading
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35771
diff
changeset
|
1930 if (NCH(n) > 1) |
|
857f68fba275
Write a separate ast_for_testlist_gexp() function instead of overloading
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35771
diff
changeset
|
1931 assert(TYPE(CHILD(n, 1)) != gen_for); |
|
857f68fba275
Write a separate ast_for_testlist_gexp() function instead of overloading
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35771
diff
changeset
|
1932 } |
|
857f68fba275
Write a separate ast_for_testlist_gexp() function instead of overloading
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35771
diff
changeset
|
1933 else { |
|
857f68fba275
Write a separate ast_for_testlist_gexp() function instead of overloading
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35771
diff
changeset
|
1934 assert(TYPE(n) == testlist || |
|
857f68fba275
Write a separate ast_for_testlist_gexp() function instead of overloading
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35771
diff
changeset
|
1935 TYPE(n) == testlist_safe || |
|
857f68fba275
Write a separate ast_for_testlist_gexp() function instead of overloading
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35771
diff
changeset
|
1936 TYPE(n) == testlist1); |
|
857f68fba275
Write a separate ast_for_testlist_gexp() function instead of overloading
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35771
diff
changeset
|
1937 } |
| 35742 | 1938 if (NCH(n) == 1) |
| 1939 return ast_for_expr(c, CHILD(n, 0)); | |
| 1940 else { | |
| 1941 asdl_seq *tmp = seq_for_testlist(c, n); | |
| 1942 if (!tmp) | |
| 1943 return NULL; | |
| 1944 return Tuple(tmp, Load, LINENO(n)); | |
| 1945 } | |
|
35798
857f68fba275
Write a separate ast_for_testlist_gexp() function instead of overloading
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35771
diff
changeset
|
1946 } |
|
857f68fba275
Write a separate ast_for_testlist_gexp() function instead of overloading
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35771
diff
changeset
|
1947 |
|
857f68fba275
Write a separate ast_for_testlist_gexp() function instead of overloading
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35771
diff
changeset
|
1948 static expr_ty |
|
857f68fba275
Write a separate ast_for_testlist_gexp() function instead of overloading
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35771
diff
changeset
|
1949 ast_for_testlist_gexp(struct compiling *c, const node* n) |
|
857f68fba275
Write a separate ast_for_testlist_gexp() function instead of overloading
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35771
diff
changeset
|
1950 { |
|
857f68fba275
Write a separate ast_for_testlist_gexp() function instead of overloading
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35771
diff
changeset
|
1951 /* testlist_gexp: test ( gen_for | (',' test)* [','] ) */ |
|
857f68fba275
Write a separate ast_for_testlist_gexp() function instead of overloading
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35771
diff
changeset
|
1952 /* argument: test [ gen_for ] */ |
|
857f68fba275
Write a separate ast_for_testlist_gexp() function instead of overloading
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35771
diff
changeset
|
1953 assert(TYPE(n) == testlist_gexp || TYPE(n) == argument); |
|
857f68fba275
Write a separate ast_for_testlist_gexp() function instead of overloading
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35771
diff
changeset
|
1954 if (NCH(n) > 1 && TYPE(CHILD(n, 1)) == gen_for) { |
|
857f68fba275
Write a separate ast_for_testlist_gexp() function instead of overloading
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35771
diff
changeset
|
1955 return ast_for_genexp(c, n); |
|
857f68fba275
Write a separate ast_for_testlist_gexp() function instead of overloading
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35771
diff
changeset
|
1956 } |
|
857f68fba275
Write a separate ast_for_testlist_gexp() function instead of overloading
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35771
diff
changeset
|
1957 else |
|
857f68fba275
Write a separate ast_for_testlist_gexp() function instead of overloading
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35771
diff
changeset
|
1958 return ast_for_testlist(c, n); |
|
857f68fba275
Write a separate ast_for_testlist_gexp() function instead of overloading
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35771
diff
changeset
|
1959 } |
|
857f68fba275
Write a separate ast_for_testlist_gexp() function instead of overloading
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35771
diff
changeset
|
1960 |
|
857f68fba275
Write a separate ast_for_testlist_gexp() function instead of overloading
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35771
diff
changeset
|
1961 /* like ast_for_testlist() but returns a sequence */ |
|
857f68fba275
Write a separate ast_for_testlist_gexp() function instead of overloading
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35771
diff
changeset
|
1962 static asdl_seq* |
|
857f68fba275
Write a separate ast_for_testlist_gexp() function instead of overloading
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35771
diff
changeset
|
1963 ast_for_class_bases(struct compiling *c, const node* n) |
|
857f68fba275
Write a separate ast_for_testlist_gexp() function instead of overloading
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35771
diff
changeset
|
1964 { |
|
857f68fba275
Write a separate ast_for_testlist_gexp() function instead of overloading
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35771
diff
changeset
|
1965 /* testlist: test (',' test)* [','] */ |
|
857f68fba275
Write a separate ast_for_testlist_gexp() function instead of overloading
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35771
diff
changeset
|
1966 assert(NCH(n) > 0); |
|
857f68fba275
Write a separate ast_for_testlist_gexp() function instead of overloading
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35771
diff
changeset
|
1967 REQ(n, testlist); |
|
857f68fba275
Write a separate ast_for_testlist_gexp() function instead of overloading
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35771
diff
changeset
|
1968 if (NCH(n) == 1) { |
|
857f68fba275
Write a separate ast_for_testlist_gexp() function instead of overloading
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35771
diff
changeset
|
1969 expr_ty base; |
|
857f68fba275
Write a separate ast_for_testlist_gexp() function instead of overloading
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35771
diff
changeset
|
1970 asdl_seq *bases = asdl_seq_new(1); |
|
857f68fba275
Write a separate ast_for_testlist_gexp() function instead of overloading
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35771
diff
changeset
|
1971 if (!bases) |
|
857f68fba275
Write a separate ast_for_testlist_gexp() function instead of overloading
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35771
diff
changeset
|
1972 return NULL; |
|
857f68fba275
Write a separate ast_for_testlist_gexp() function instead of overloading
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35771
diff
changeset
|
1973 base = ast_for_expr(c, CHILD(n, 0)); |
|
857f68fba275
Write a separate ast_for_testlist_gexp() function instead of overloading
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35771
diff
changeset
|
1974 if (!base) { |
|
35873
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
1975 asdl_seq_free(bases); /* ok */ |
|
35798
857f68fba275
Write a separate ast_for_testlist_gexp() function instead of overloading
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35771
diff
changeset
|
1976 return NULL; |
|
857f68fba275
Write a separate ast_for_testlist_gexp() function instead of overloading
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35771
diff
changeset
|
1977 } |
|
857f68fba275
Write a separate ast_for_testlist_gexp() function instead of overloading
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35771
diff
changeset
|
1978 asdl_seq_SET(bases, 0, base); |
|
857f68fba275
Write a separate ast_for_testlist_gexp() function instead of overloading
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35771
diff
changeset
|
1979 return bases; |
|
857f68fba275
Write a separate ast_for_testlist_gexp() function instead of overloading
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35771
diff
changeset
|
1980 } |
|
857f68fba275
Write a separate ast_for_testlist_gexp() function instead of overloading
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35771
diff
changeset
|
1981 else { |
|
857f68fba275
Write a separate ast_for_testlist_gexp() function instead of overloading
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35771
diff
changeset
|
1982 return seq_for_testlist(c, n); |
|
857f68fba275
Write a separate ast_for_testlist_gexp() function instead of overloading
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35771
diff
changeset
|
1983 } |
| 35742 | 1984 } |
| 1985 | |
| 1986 static stmt_ty | |
| 1987 ast_for_expr_stmt(struct compiling *c, const node *n) | |
| 1988 { | |
| 1989 REQ(n, expr_stmt); | |
| 1990 /* expr_stmt: testlist (augassign (yield_expr|testlist) | |
| 1991 | ('=' (yield_expr|testlist))*) | |
| 1992 testlist: test (',' test)* [','] | |
| 1993 augassign: '+=' | '-=' | '*=' | '/=' | '%=' | '&=' | '|=' | '^=' | |
| 1994 | '<<=' | '>>=' | '**=' | '//=' | |
| 1995 test: ... here starts the operator precendence dance | |
| 1996 */ | |
| 1997 | |
| 1998 if (NCH(n) == 1) { | |
|
35798
857f68fba275
Write a separate ast_for_testlist_gexp() function instead of overloading
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35771
diff
changeset
|
1999 expr_ty e = ast_for_testlist(c, CHILD(n, 0)); |
| 35742 | 2000 if (!e) |
| 2001 return NULL; | |
| 2002 | |
| 2003 return Expr(e, LINENO(n)); | |
| 2004 } | |
| 2005 else if (TYPE(CHILD(n, 1)) == augassign) { | |
| 2006 expr_ty expr1, expr2; | |
| 2007 operator_ty operator; | |
| 2008 node *ch = CHILD(n, 0); | |
| 2009 | |
| 2010 if (TYPE(ch) == testlist) | |
|
35798
857f68fba275
Write a separate ast_for_testlist_gexp() function instead of overloading
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35771
diff
changeset
|
2011 expr1 = ast_for_testlist(c, ch); |
| 35742 | 2012 else |
| 2013 expr1 = Yield(ast_for_expr(c, CHILD(ch, 0)), LINENO(ch)); | |
| 2014 | |
| 2015 if (!expr1) | |
| 2016 return NULL; | |
| 2017 if (expr1->kind == GeneratorExp_kind) { | |
|
35868
37da969ad116
Fix memory leak with bad generator expression
Neal Norwitz <nnorwitz@gmail.com>
parents:
35867
diff
changeset
|
2018 free_expr(expr1); |
| 35742 | 2019 ast_error(ch, "augmented assignment to generator " |
| 2020 "expression not possible"); | |
| 2021 return NULL; | |
| 2022 } | |
| 2023 if (expr1->kind == Name_kind) { | |
| 2024 char *var_name = PyString_AS_STRING(expr1->v.Name.id); | |
| 2025 if (var_name[0] == 'N' && !strcmp(var_name, "None")) { | |
|
35868
37da969ad116
Fix memory leak with bad generator expression
Neal Norwitz <nnorwitz@gmail.com>
parents:
35867
diff
changeset
|
2026 free_expr(expr1); |
| 35742 | 2027 ast_error(ch, "assignment to None"); |
| 2028 return NULL; | |
| 2029 } | |
| 2030 } | |
| 2031 | |
| 2032 ch = CHILD(n, 2); | |
| 2033 if (TYPE(ch) == testlist) | |
|
35798
857f68fba275
Write a separate ast_for_testlist_gexp() function instead of overloading
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35771
diff
changeset
|
2034 expr2 = ast_for_testlist(c, ch); |
| 35742 | 2035 else |
| 2036 expr2 = Yield(ast_for_expr(c, ch), LINENO(ch)); | |
|
35868
37da969ad116
Fix memory leak with bad generator expression
Neal Norwitz <nnorwitz@gmail.com>
parents:
35867
diff
changeset
|
2037 if (!expr2) { |
|
37da969ad116
Fix memory leak with bad generator expression
Neal Norwitz <nnorwitz@gmail.com>
parents:
35867
diff
changeset
|
2038 free_expr(expr1); |
| 35742 | 2039 return NULL; |
|
35868
37da969ad116
Fix memory leak with bad generator expression
Neal Norwitz <nnorwitz@gmail.com>
parents:
35867
diff
changeset
|
2040 } |
| 35742 | 2041 |
| 2042 operator = ast_for_augassign(CHILD(n, 1)); | |
|
35868
37da969ad116
Fix memory leak with bad generator expression
Neal Norwitz <nnorwitz@gmail.com>
parents:
35867
diff
changeset
|
2043 if (!operator) { |
|
37da969ad116
Fix memory leak with bad generator expression
Neal Norwitz <nnorwitz@gmail.com>
parents:
35867
diff
changeset
|
2044 free_expr(expr1); |
|
37da969ad116
Fix memory leak with bad generator expression
Neal Norwitz <nnorwitz@gmail.com>
parents:
35867
diff
changeset
|
2045 free_expr(expr2); |
| 35742 | 2046 return NULL; |
|
35868
37da969ad116
Fix memory leak with bad generator expression
Neal Norwitz <nnorwitz@gmail.com>
parents:
35867
diff
changeset
|
2047 } |
| 35742 | 2048 |
| 2049 return AugAssign(expr1, operator, expr2, LINENO(n)); | |
| 2050 } | |
| 2051 else { | |
| 2052 int i; | |
| 2053 asdl_seq *targets; | |
| 2054 node *value; | |
| 2055 expr_ty expression; | |
| 2056 | |
| 2057 /* a normal assignment */ | |
| 2058 REQ(CHILD(n, 1), EQUAL); | |
| 2059 targets = asdl_seq_new(NCH(n) / 2); | |
| 2060 if (!targets) | |
| 2061 return NULL; | |
| 2062 for (i = 0; i < NCH(n) - 2; i += 2) { | |
|
35759
d91ed7107fd2
ANSI-C-ify the placement of local var declarations.
Armin Rigo <arigo@tunes.org>
parents:
35748
diff
changeset
|
2063 expr_ty e; |
| 35742 | 2064 node *ch = CHILD(n, i); |
| 2065 if (TYPE(ch) == yield_expr) { | |
| 2066 ast_error(ch, "assignment to yield expression not possible"); | |
| 2067 goto error; | |
| 2068 } | |
|
35798
857f68fba275
Write a separate ast_for_testlist_gexp() function instead of overloading
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35771
diff
changeset
|
2069 e = ast_for_testlist(c, ch); |
| 35742 | 2070 |
| 2071 /* set context to assign */ | |
| 2072 if (!e) | |
| 2073 goto error; | |
| 2074 | |
| 2075 if (!set_context(e, Store, CHILD(n, i))) { | |
| 2076 free_expr(e); | |
| 2077 goto error; | |
| 2078 } | |
| 2079 | |
| 2080 asdl_seq_SET(targets, i / 2, e); | |
| 2081 } | |
| 2082 value = CHILD(n, NCH(n) - 1); | |
| 2083 if (TYPE(value) == testlist) | |
|
35798
857f68fba275
Write a separate ast_for_testlist_gexp() function instead of overloading
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35771
diff
changeset
|
2084 expression = ast_for_testlist(c, value); |
| 35742 | 2085 else |
| 2086 expression = ast_for_expr(c, value); | |
| 2087 if (!expression) | |
|
35868
37da969ad116
Fix memory leak with bad generator expression
Neal Norwitz <nnorwitz@gmail.com>
parents:
35867
diff
changeset
|
2088 goto error; |
| 35742 | 2089 return Assign(targets, expression, LINENO(n)); |
| 2090 error: | |
|
35873
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
2091 asdl_expr_seq_free(targets); |
| 35742 | 2092 } |
| 2093 return NULL; | |
| 2094 } | |
| 2095 | |
| 2096 static stmt_ty | |
| 2097 ast_for_print_stmt(struct compiling *c, const node *n) | |
| 2098 { | |
| 2099 /* print_stmt: 'print' ( [ test (',' test)* [','] ] | |
| 2100 | '>>' test [ (',' test)+ [','] ] ) | |
| 2101 */ | |
| 2102 expr_ty dest = NULL, expression; | |
| 2103 asdl_seq *seq; | |
| 2104 bool nl; | |
| 2105 int i, start = 1; | |
| 2106 | |
| 2107 REQ(n, print_stmt); | |
| 2108 if (NCH(n) >= 2 && TYPE(CHILD(n, 1)) == RIGHTSHIFT) { | |
| 2109 dest = ast_for_expr(c, CHILD(n, 2)); | |
| 2110 if (!dest) | |
| 2111 return NULL; | |
| 2112 start = 4; | |
| 2113 } | |
| 2114 seq = asdl_seq_new((NCH(n) + 1 - start) / 2); | |
| 2115 if (!seq) | |
| 2116 return NULL; | |
| 2117 for (i = start; i < NCH(n); i += 2) { | |
| 2118 expression = ast_for_expr(c, CHILD(n, i)); | |
| 2119 if (!expression) { | |
|
35873
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
2120 free_expr(dest); |
|
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
2121 asdl_expr_seq_free(seq); |
| 35742 | 2122 return NULL; |
| 2123 } | |
| 2124 | |
| 2125 asdl_seq_APPEND(seq, expression); | |
| 2126 } | |
| 2127 nl = (TYPE(CHILD(n, NCH(n) - 1)) == COMMA) ? false : true; | |
| 2128 return Print(dest, seq, nl, LINENO(n)); | |
| 2129 } | |
| 2130 | |
| 2131 static asdl_seq * | |
| 2132 ast_for_exprlist(struct compiling *c, const node *n, int context) | |
| 2133 { | |
| 2134 asdl_seq *seq; | |
| 2135 int i; | |
| 2136 expr_ty e; | |
| 2137 | |
| 2138 REQ(n, exprlist); | |
| 2139 | |
| 2140 seq = asdl_seq_new((NCH(n) + 1) / 2); | |
| 2141 if (!seq) | |
| 2142 return NULL; | |
| 2143 for (i = 0; i < NCH(n); i += 2) { | |
| 2144 e = ast_for_expr(c, CHILD(n, i)); | |
|
35873
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
2145 if (!e) |
|
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
2146 goto error; |
|
35875
086caccfff0e
Fix another memory leak or two (one real, one potential)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35874
diff
changeset
|
2147 asdl_seq_SET(seq, i / 2, e); |
| 35742 | 2148 if (context) { |
| 2149 if (!set_context(e, context, CHILD(n, i))) | |
|
35873
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
2150 goto error; |
| 35742 | 2151 } |
| 2152 } | |
| 2153 return seq; | |
|
35873
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
2154 |
|
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
2155 error: |
|
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
2156 asdl_expr_seq_free(seq); |
|
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
2157 return NULL; |
| 35742 | 2158 } |
| 2159 | |
| 2160 static stmt_ty | |
| 2161 ast_for_del_stmt(struct compiling *c, const node *n) | |
| 2162 { | |
| 2163 asdl_seq *expr_list; | |
| 2164 | |
| 2165 /* del_stmt: 'del' exprlist */ | |
| 2166 REQ(n, del_stmt); | |
| 2167 | |
| 2168 expr_list = ast_for_exprlist(c, CHILD(n, 1), Del); | |
| 2169 if (!expr_list) | |
| 2170 return NULL; | |
| 2171 return Delete(expr_list, LINENO(n)); | |
| 2172 } | |
| 2173 | |
| 2174 static stmt_ty | |
| 2175 ast_for_flow_stmt(struct compiling *c, const node *n) | |
| 2176 { | |
| 2177 /* | |
| 2178 flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt | |
| 2179 | yield_stmt | |
| 2180 break_stmt: 'break' | |
| 2181 continue_stmt: 'continue' | |
| 2182 return_stmt: 'return' [testlist] | |
| 2183 yield_stmt: yield_expr | |
| 2184 yield_expr: 'yield' testlist | |
| 2185 raise_stmt: 'raise' [test [',' test [',' test]]] | |
| 2186 */ | |
| 2187 node *ch; | |
| 2188 | |
| 2189 REQ(n, flow_stmt); | |
| 2190 ch = CHILD(n, 0); | |
| 2191 switch (TYPE(ch)) { | |
| 2192 case break_stmt: | |
| 2193 return Break(LINENO(n)); | |
| 2194 case continue_stmt: | |
| 2195 return Continue(LINENO(n)); | |
| 2196 case yield_stmt: { /* will reduce to yield_expr */ | |
| 2197 expr_ty exp = ast_for_expr(c, CHILD(ch, 0)); | |
| 2198 if (!exp) | |
| 2199 return NULL; | |
| 2200 return Expr(exp, LINENO(n)); | |
| 2201 } | |
| 2202 case return_stmt: | |
| 2203 if (NCH(ch) == 1) | |
| 2204 return Return(NULL, LINENO(n)); | |
| 2205 else { | |
|
35798
857f68fba275
Write a separate ast_for_testlist_gexp() function instead of overloading
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35771
diff
changeset
|
2206 expr_ty expression = ast_for_testlist(c, CHILD(ch, 1)); |
| 35742 | 2207 if (!expression) |
| 2208 return NULL; | |
| 2209 return Return(expression, LINENO(n)); | |
| 2210 } | |
| 2211 case raise_stmt: | |
| 2212 if (NCH(ch) == 1) | |
| 2213 return Raise(NULL, NULL, NULL, LINENO(n)); | |
| 2214 else if (NCH(ch) == 2) { | |
| 2215 expr_ty expression = ast_for_expr(c, CHILD(ch, 1)); | |
| 2216 if (!expression) | |
| 2217 return NULL; | |
| 2218 return Raise(expression, NULL, NULL, LINENO(n)); | |
| 2219 } | |
| 2220 else if (NCH(ch) == 4) { | |
| 2221 expr_ty expr1, expr2; | |
| 2222 | |
| 2223 expr1 = ast_for_expr(c, CHILD(ch, 1)); | |
| 2224 if (!expr1) | |
| 2225 return NULL; | |
| 2226 expr2 = ast_for_expr(c, CHILD(ch, 3)); | |
| 2227 if (!expr2) | |
| 2228 return NULL; | |
| 2229 | |
| 2230 return Raise(expr1, expr2, NULL, LINENO(n)); | |
| 2231 } | |
| 2232 else if (NCH(ch) == 6) { | |
| 2233 expr_ty expr1, expr2, expr3; | |
| 2234 | |
| 2235 expr1 = ast_for_expr(c, CHILD(ch, 1)); | |
| 2236 if (!expr1) | |
| 2237 return NULL; | |
| 2238 expr2 = ast_for_expr(c, CHILD(ch, 3)); | |
| 2239 if (!expr2) | |
| 2240 return NULL; | |
| 2241 expr3 = ast_for_expr(c, CHILD(ch, 5)); | |
| 2242 if (!expr3) | |
| 2243 return NULL; | |
| 2244 | |
| 2245 return Raise(expr1, expr2, expr3, LINENO(n)); | |
| 2246 } | |
| 2247 default: | |
|
35870
f531c05cd240
Convert all internal errors from Exception to SystemError
Neal Norwitz <nnorwitz@gmail.com>
parents:
35868
diff
changeset
|
2248 PyErr_Format(PyExc_SystemError, |
| 35742 | 2249 "unexpected flow_stmt: %d", TYPE(ch)); |
| 2250 return NULL; | |
| 2251 } | |
| 2252 } | |
| 2253 | |
| 2254 static alias_ty | |
| 2255 alias_for_import_name(const node *n) | |
| 2256 { | |
| 2257 /* | |
| 2258 import_as_name: NAME [NAME NAME] | |
| 2259 dotted_as_name: dotted_name [NAME NAME] | |
| 2260 dotted_name: NAME ('.' NAME)* | |
| 2261 */ | |
| 2262 loop: | |
| 2263 switch (TYPE(n)) { | |
| 2264 case import_as_name: | |
| 2265 if (NCH(n) == 3) | |
| 2266 return alias(NEW_IDENTIFIER(CHILD(n, 0)), | |
| 2267 NEW_IDENTIFIER(CHILD(n, 2))); | |
| 2268 else | |
| 2269 return alias(NEW_IDENTIFIER(CHILD(n, 0)), | |
| 2270 NULL); | |
| 2271 break; | |
| 2272 case dotted_as_name: | |
| 2273 if (NCH(n) == 1) { | |
| 2274 n = CHILD(n, 0); | |
| 2275 goto loop; | |
| 2276 } | |
| 2277 else { | |
| 2278 alias_ty a = alias_for_import_name(CHILD(n, 0)); | |
| 2279 assert(!a->asname); | |
| 2280 a->asname = NEW_IDENTIFIER(CHILD(n, 2)); | |
| 2281 return a; | |
| 2282 } | |
| 2283 break; | |
| 2284 case dotted_name: | |
| 2285 if (NCH(n) == 1) | |
| 2286 return alias(NEW_IDENTIFIER(CHILD(n, 0)), NULL); | |
| 2287 else { | |
| 2288 /* Create a string of the form "a.b.c" */ | |
| 2289 int i, len; | |
| 2290 PyObject *str; | |
| 2291 char *s; | |
| 2292 | |
| 2293 len = 0; | |
| 2294 for (i = 0; i < NCH(n); i += 2) | |
| 2295 /* length of string plus one for the dot */ | |
| 2296 len += strlen(STR(CHILD(n, i))) + 1; | |
| 2297 len--; /* the last name doesn't have a dot */ | |
| 2298 str = PyString_FromStringAndSize(NULL, len); | |
| 2299 if (!str) | |
| 2300 return NULL; | |
| 2301 s = PyString_AS_STRING(str); | |
| 2302 if (!s) | |
| 2303 return NULL; | |
| 2304 for (i = 0; i < NCH(n); i += 2) { | |
| 2305 char *sch = STR(CHILD(n, i)); | |
| 2306 strcpy(s, STR(CHILD(n, i))); | |
| 2307 s += strlen(sch); | |
| 2308 *s++ = '.'; | |
| 2309 } | |
| 2310 --s; | |
| 2311 *s = '\0'; | |
| 2312 PyString_InternInPlace(&str); | |
| 2313 return alias(str, NULL); | |
| 2314 } | |
| 2315 break; | |
| 2316 case STAR: | |
| 2317 return alias(PyString_InternFromString("*"), NULL); | |
| 2318 default: | |
|
35870
f531c05cd240
Convert all internal errors from Exception to SystemError
Neal Norwitz <nnorwitz@gmail.com>
parents:
35868
diff
changeset
|
2319 PyErr_Format(PyExc_SystemError, |
| 35742 | 2320 "unexpected import name: %d", TYPE(n)); |
| 2321 return NULL; | |
| 2322 } | |
| 2323 return NULL; | |
| 2324 } | |
| 2325 | |
| 2326 static stmt_ty | |
| 2327 ast_for_import_stmt(struct compiling *c, const node *n) | |
| 2328 { | |
| 2329 /* | |
| 2330 import_stmt: import_name | import_from | |
| 2331 import_name: 'import' dotted_as_names | |
| 2332 import_from: 'from' dotted_name 'import' ('*' | | |
| 2333 '(' import_as_names ')' | | |
| 2334 import_as_names) | |
| 2335 */ | |
| 2336 int i; | |
| 2337 asdl_seq *aliases; | |
| 2338 | |
| 2339 REQ(n, import_stmt); | |
| 2340 n = CHILD(n, 0); | |
| 2341 if (STR(CHILD(n, 0))[0] == 'i') { /* import */ | |
| 2342 n = CHILD(n, 1); | |
|
35771
044d8e0f65f2
Add an assert.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35759
diff
changeset
|
2343 REQ(n, dotted_as_names); |
| 35742 | 2344 aliases = asdl_seq_new((NCH(n) + 1) / 2); |
| 2345 if (!aliases) | |
| 2346 return NULL; | |
| 2347 for (i = 0; i < NCH(n); i += 2) { | |
| 2348 alias_ty import_alias = alias_for_import_name(CHILD(n, i)); | |
| 2349 if (!import_alias) { | |
|
35873
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
2350 asdl_alias_seq_free(aliases); |
| 35742 | 2351 return NULL; |
| 2352 } | |
| 2353 asdl_seq_SET(aliases, i / 2, import_alias); | |
| 2354 } | |
| 2355 return Import(aliases, LINENO(n)); | |
| 2356 } | |
| 2357 else if (STR(CHILD(n, 0))[0] == 'f') { /* from */ | |
| 2358 stmt_ty import; | |
| 2359 int n_children; | |
| 2360 const char *from_modules; | |
| 2361 int lineno = LINENO(n); | |
| 2362 alias_ty mod = alias_for_import_name(CHILD(n, 1)); | |
| 2363 if (!mod) | |
| 2364 return NULL; | |
| 2365 | |
| 2366 /* XXX this needs to be cleaned up */ | |
| 2367 | |
| 2368 from_modules = STR(CHILD(n, 3)); | |
| 2369 if (!from_modules) { | |
| 2370 n = CHILD(n, 3); /* from ... import x, y, z */ | |
| 2371 if (NCH(n) % 2 == 0) { | |
| 2372 /* it ends with a comma, not valid but the parser allows it */ | |
|
35873
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
2373 free_alias(mod); |
| 35742 | 2374 ast_error(n, "trailing comma not allowed without" |
| 2375 " surrounding parentheses"); | |
| 2376 return NULL; | |
| 2377 } | |
| 2378 } | |
| 2379 else if (from_modules[0] == '*') { | |
| 2380 n = CHILD(n, 3); /* from ... import * */ | |
| 2381 } | |
| 2382 else if (from_modules[0] == '(') | |
| 2383 n = CHILD(n, 4); /* from ... import (x, y, z) */ | |
|
35873
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
2384 else { |
|
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
2385 /* XXX: don't we need to call ast_error(n, "..."); */ |
|
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
2386 free_alias(mod); |
| 35742 | 2387 return NULL; |
|
35873
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
2388 } |
| 35742 | 2389 |
| 2390 n_children = NCH(n); | |
| 2391 if (from_modules && from_modules[0] == '*') | |
| 2392 n_children = 1; | |
| 2393 | |
| 2394 aliases = asdl_seq_new((n_children + 1) / 2); | |
| 2395 if (!aliases) { | |
| 2396 free_alias(mod); | |
| 2397 return NULL; | |
| 2398 } | |
| 2399 | |
| 2400 /* handle "from ... import *" special b/c there's no children */ | |
| 2401 if (from_modules && from_modules[0] == '*') { | |
| 2402 alias_ty import_alias = alias_for_import_name(n); | |
| 2403 if (!import_alias) { | |
|
35873
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
2404 asdl_alias_seq_free(aliases); |
| 35742 | 2405 free_alias(mod); |
| 2406 return NULL; | |
| 2407 } | |
| 2408 asdl_seq_APPEND(aliases, import_alias); | |
| 2409 } | |
| 2410 | |
| 2411 for (i = 0; i < NCH(n); i += 2) { | |
| 2412 alias_ty import_alias = alias_for_import_name(CHILD(n, i)); | |
| 2413 if (!import_alias) { | |
|
35873
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
2414 asdl_alias_seq_free(aliases); |
| 35742 | 2415 free_alias(mod); |
| 2416 return NULL; | |
| 2417 } | |
| 2418 asdl_seq_APPEND(aliases, import_alias); | |
| 2419 } | |
| 2420 Py_INCREF(mod->name); | |
| 2421 import = ImportFrom(mod->name, aliases, lineno); | |
| 2422 free_alias(mod); | |
| 2423 return import; | |
| 2424 } | |
|
35870
f531c05cd240
Convert all internal errors from Exception to SystemError
Neal Norwitz <nnorwitz@gmail.com>
parents:
35868
diff
changeset
|
2425 PyErr_Format(PyExc_SystemError, |
| 35742 | 2426 "unknown import statement: starts with command '%s'", |
| 2427 STR(CHILD(n, 0))); | |
| 2428 return NULL; | |
| 2429 } | |
| 2430 | |
| 2431 static stmt_ty | |
| 2432 ast_for_global_stmt(struct compiling *c, const node *n) | |
| 2433 { | |
| 2434 /* global_stmt: 'global' NAME (',' NAME)* */ | |
| 2435 identifier name; | |
| 2436 asdl_seq *s; | |
| 2437 int i; | |
| 2438 | |
| 2439 REQ(n, global_stmt); | |
| 2440 s = asdl_seq_new(NCH(n) / 2); | |
| 2441 if (!s) | |
| 2442 return NULL; | |
| 2443 for (i = 1; i < NCH(n); i += 2) { | |
| 2444 name = NEW_IDENTIFIER(CHILD(n, i)); | |
| 2445 if (!name) { | |
|
35873
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
2446 for (i = i / 2; i > 0; i--) |
|
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
2447 Py_XDECREF((identifier) asdl_seq_GET(s, i)); |
|
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
2448 asdl_seq_free(s); /* ok */ |
| 35742 | 2449 return NULL; |
| 2450 } | |
| 2451 asdl_seq_SET(s, i / 2, name); | |
| 2452 } | |
| 2453 return Global(s, LINENO(n)); | |
| 2454 } | |
| 2455 | |
| 2456 static stmt_ty | |
| 2457 ast_for_exec_stmt(struct compiling *c, const node *n) | |
| 2458 { | |
| 2459 expr_ty expr1, globals = NULL, locals = NULL; | |
| 2460 int n_children = NCH(n); | |
| 2461 if (n_children != 2 && n_children != 4 && n_children != 6) { | |
|
35870
f531c05cd240
Convert all internal errors from Exception to SystemError
Neal Norwitz <nnorwitz@gmail.com>
parents:
35868
diff
changeset
|
2462 PyErr_Format(PyExc_SystemError, |
| 35742 | 2463 "poorly formed 'exec' statement: %d parts to statement", |
| 2464 n_children); | |
| 2465 return NULL; | |
| 2466 } | |
| 2467 | |
| 2468 /* exec_stmt: 'exec' expr ['in' test [',' test]] */ | |
| 2469 REQ(n, exec_stmt); | |
| 2470 expr1 = ast_for_expr(c, CHILD(n, 1)); | |
| 2471 if (!expr1) | |
| 2472 return NULL; | |
| 2473 if (n_children >= 4) { | |
| 2474 globals = ast_for_expr(c, CHILD(n, 3)); | |
| 2475 if (!globals) | |
| 2476 return NULL; | |
| 2477 } | |
| 2478 if (n_children == 6) { | |
| 2479 locals = ast_for_expr(c, CHILD(n, 5)); | |
| 2480 if (!locals) | |
| 2481 return NULL; | |
| 2482 } | |
| 2483 | |
| 2484 return Exec(expr1, globals, locals, LINENO(n)); | |
| 2485 } | |
| 2486 | |
| 2487 static stmt_ty | |
| 2488 ast_for_assert_stmt(struct compiling *c, const node *n) | |
| 2489 { | |
| 2490 /* assert_stmt: 'assert' test [',' test] */ | |
| 2491 REQ(n, assert_stmt); | |
| 2492 if (NCH(n) == 2) { | |
| 2493 expr_ty expression = ast_for_expr(c, CHILD(n, 1)); | |
| 2494 if (!expression) | |
| 2495 return NULL; | |
| 2496 return Assert(expression, NULL, LINENO(n)); | |
| 2497 } | |
| 2498 else if (NCH(n) == 4) { | |
| 2499 expr_ty expr1, expr2; | |
| 2500 | |
| 2501 expr1 = ast_for_expr(c, CHILD(n, 1)); | |
| 2502 if (!expr1) | |
| 2503 return NULL; | |
| 2504 expr2 = ast_for_expr(c, CHILD(n, 3)); | |
| 2505 if (!expr2) | |
| 2506 return NULL; | |
| 2507 | |
| 2508 return Assert(expr1, expr2, LINENO(n)); | |
| 2509 } | |
|
35870
f531c05cd240
Convert all internal errors from Exception to SystemError
Neal Norwitz <nnorwitz@gmail.com>
parents:
35868
diff
changeset
|
2510 PyErr_Format(PyExc_SystemError, |
| 35742 | 2511 "improper number of parts to 'assert' statement: %d", |
| 2512 NCH(n)); | |
| 2513 return NULL; | |
| 2514 } | |
| 2515 | |
| 2516 static asdl_seq * | |
| 2517 ast_for_suite(struct compiling *c, const node *n) | |
| 2518 { | |
| 2519 /* suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT */ | |
| 2520 asdl_seq *seq = NULL; | |
| 2521 stmt_ty s; | |
| 2522 int i, total, num, end, pos = 0; | |
| 2523 node *ch; | |
| 2524 | |
| 2525 REQ(n, suite); | |
| 2526 | |
| 2527 total = num_stmts(n); | |
| 2528 seq = asdl_seq_new(total); | |
| 2529 if (!seq) | |
| 2530 return NULL; | |
| 2531 if (TYPE(CHILD(n, 0)) == simple_stmt) { | |
| 2532 n = CHILD(n, 0); | |
| 2533 /* simple_stmt always ends with a NEWLINE, | |
| 2534 and may have a trailing SEMI | |
| 2535 */ | |
| 2536 end = NCH(n) - 1; | |
| 2537 if (TYPE(CHILD(n, end - 1)) == SEMI) | |
| 2538 end--; | |
| 2539 /* loop by 2 to skip semi-colons */ | |
| 2540 for (i = 0; i < end; i += 2) { | |
| 2541 ch = CHILD(n, i); | |
| 2542 s = ast_for_stmt(c, ch); | |
| 2543 if (!s) | |
| 2544 goto error; | |
| 2545 asdl_seq_SET(seq, pos++, s); | |
| 2546 } | |
| 2547 } | |
| 2548 else { | |
| 2549 for (i = 2; i < (NCH(n) - 1); i++) { | |
| 2550 ch = CHILD(n, i); | |
| 2551 REQ(ch, stmt); | |
| 2552 num = num_stmts(ch); | |
| 2553 if (num == 1) { | |
| 2554 /* small_stmt or compound_stmt with only one child */ | |
| 2555 s = ast_for_stmt(c, ch); | |
| 2556 if (!s) | |
| 2557 goto error; | |
| 2558 asdl_seq_SET(seq, pos++, s); | |
| 2559 } | |
| 2560 else { | |
| 2561 int j; | |
| 2562 ch = CHILD(ch, 0); | |
| 2563 REQ(ch, simple_stmt); | |
| 2564 for (j = 0; j < NCH(ch); j += 2) { | |
| 2565 s = ast_for_stmt(c, CHILD(ch, j)); | |
| 2566 if (!s) | |
| 2567 goto error; | |
| 2568 asdl_seq_SET(seq, pos++, s); | |
| 2569 } | |
| 2570 } | |
| 2571 } | |
| 2572 } | |
| 2573 assert(pos == seq->size); | |
| 2574 return seq; | |
| 2575 error: | |
| 2576 if (seq) | |
|
35873
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
2577 asdl_stmt_seq_free(seq); |
| 35742 | 2578 return NULL; |
| 2579 } | |
| 2580 | |
| 2581 static stmt_ty | |
| 2582 ast_for_if_stmt(struct compiling *c, const node *n) | |
| 2583 { | |
| 2584 /* if_stmt: 'if' test ':' suite ('elif' test ':' suite)* | |
| 2585 ['else' ':' suite] | |
| 2586 */ | |
| 2587 char *s; | |
| 2588 | |
| 2589 REQ(n, if_stmt); | |
| 2590 | |
| 2591 if (NCH(n) == 4) { | |
| 2592 expr_ty expression; | |
| 2593 asdl_seq *suite_seq; | |
| 2594 | |
| 2595 expression = ast_for_expr(c, CHILD(n, 1)); | |
| 2596 if (!expression) | |
| 2597 return NULL; | |
| 2598 suite_seq = ast_for_suite(c, CHILD(n, 3)); | |
|
35873
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
2599 if (!suite_seq) { |
|
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
2600 free_expr(expression); |
| 35742 | 2601 return NULL; |
|
35873
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
2602 } |
| 35742 | 2603 |
| 2604 return If(expression, suite_seq, NULL, LINENO(n)); | |
| 2605 } | |
| 2606 s = STR(CHILD(n, 4)); | |
| 2607 /* s[2], the third character in the string, will be | |
| 2608 's' for el_s_e, or | |
| 2609 'i' for el_i_f | |
| 2610 */ | |
| 2611 if (s[2] == 's') { | |
| 2612 expr_ty expression; | |
| 2613 asdl_seq *seq1, *seq2; | |
| 2614 | |
| 2615 expression = ast_for_expr(c, CHILD(n, 1)); | |
| 2616 if (!expression) | |
| 2617 return NULL; | |
| 2618 seq1 = ast_for_suite(c, CHILD(n, 3)); | |
|
35873
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
2619 if (!seq1) { |
|
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
2620 free_expr(expression); |
| 35742 | 2621 return NULL; |
|
35873
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
2622 } |
| 35742 | 2623 seq2 = ast_for_suite(c, CHILD(n, 6)); |
|
35873
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
2624 if (!seq2) { |
|
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
2625 asdl_stmt_seq_free(seq1); |
|
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
2626 free_expr(expression); |
| 35742 | 2627 return NULL; |
|
35873
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
2628 } |
| 35742 | 2629 |
| 2630 return If(expression, seq1, seq2, LINENO(n)); | |
| 2631 } | |
| 2632 else if (s[2] == 'i') { | |
| 2633 int i, n_elif, has_else = 0; | |
| 2634 asdl_seq *orelse = NULL; | |
| 2635 n_elif = NCH(n) - 4; | |
| 2636 /* must reference the child n_elif+1 since 'else' token is third, | |
| 2637 not fourth, child from the end. */ | |
| 2638 if (TYPE(CHILD(n, (n_elif + 1))) == NAME | |
| 2639 && STR(CHILD(n, (n_elif + 1)))[2] == 's') { | |
| 2640 has_else = 1; | |
| 2641 n_elif -= 3; | |
| 2642 } | |
| 2643 n_elif /= 4; | |
| 2644 | |
| 2645 if (has_else) { | |
| 2646 expr_ty expression; | |
| 2647 asdl_seq *seq1, *seq2; | |
| 2648 | |
| 2649 orelse = asdl_seq_new(1); | |
| 2650 if (!orelse) | |
| 2651 return NULL; | |
| 2652 expression = ast_for_expr(c, CHILD(n, NCH(n) - 6)); | |
| 2653 if (!expression) { | |
|
35873
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
2654 asdl_seq_free(orelse); /* ok */ |
| 35742 | 2655 return NULL; |
| 2656 } | |
| 2657 seq1 = ast_for_suite(c, CHILD(n, NCH(n) - 4)); | |
| 2658 if (!seq1) { | |
|
35873
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
2659 free_expr(expression); |
|
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
2660 asdl_seq_free(orelse); /* ok */ |
| 35742 | 2661 return NULL; |
| 2662 } | |
| 2663 seq2 = ast_for_suite(c, CHILD(n, NCH(n) - 1)); | |
| 2664 if (!seq2) { | |
|
35873
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
2665 free_expr(expression); |
|
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
2666 asdl_stmt_seq_free(seq1); |
|
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
2667 asdl_seq_free(orelse); /* ok */ |
| 35742 | 2668 return NULL; |
| 2669 } | |
| 2670 | |
| 2671 asdl_seq_SET(orelse, 0, If(expression, seq1, seq2, | |
| 2672 LINENO(CHILD(n, NCH(n) - 6)))); | |
| 2673 /* the just-created orelse handled the last elif */ | |
| 2674 n_elif--; | |
| 2675 } | |
| 2676 else | |
| 2677 orelse = NULL; | |
| 2678 | |
| 2679 for (i = 0; i < n_elif; i++) { | |
| 2680 int off = 5 + (n_elif - i - 1) * 4; | |
| 2681 expr_ty expression; | |
| 2682 asdl_seq *suite_seq; | |
| 2683 asdl_seq *new = asdl_seq_new(1); | |
|
35873
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
2684 if (!new) { |
|
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
2685 asdl_stmt_seq_free(orelse); |
| 35742 | 2686 return NULL; |
|
35873
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
2687 } |
| 35742 | 2688 expression = ast_for_expr(c, CHILD(n, off)); |
| 2689 if (!expression) { | |
|
35873
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
2690 asdl_stmt_seq_free(orelse); |
|
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
2691 asdl_seq_free(new); /* ok */ |
| 35742 | 2692 return NULL; |
| 2693 } | |
| 2694 suite_seq = ast_for_suite(c, CHILD(n, off + 2)); | |
| 2695 if (!suite_seq) { | |
|
35873
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
2696 asdl_stmt_seq_free(orelse); |
|
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
2697 free_expr(expression); |
|
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
2698 asdl_seq_free(new); /* ok */ |
| 35742 | 2699 return NULL; |
| 2700 } | |
| 2701 | |
| 2702 asdl_seq_SET(new, 0, | |
| 2703 If(expression, suite_seq, orelse, | |
| 2704 LINENO(CHILD(n, off)))); | |
| 2705 orelse = new; | |
| 2706 } | |
| 2707 return If(ast_for_expr(c, CHILD(n, 1)), | |
| 2708 ast_for_suite(c, CHILD(n, 3)), | |
| 2709 orelse, LINENO(n)); | |
| 2710 } | |
| 2711 else { | |
|
35870
f531c05cd240
Convert all internal errors from Exception to SystemError
Neal Norwitz <nnorwitz@gmail.com>
parents:
35868
diff
changeset
|
2712 PyErr_Format(PyExc_SystemError, |
| 35742 | 2713 "unexpected token in 'if' statement: %s", s); |
| 2714 return NULL; | |
| 2715 } | |
| 2716 } | |
| 2717 | |
| 2718 static stmt_ty | |
| 2719 ast_for_while_stmt(struct compiling *c, const node *n) | |
| 2720 { | |
| 2721 /* while_stmt: 'while' test ':' suite ['else' ':' suite] */ | |
| 2722 REQ(n, while_stmt); | |
| 2723 | |
| 2724 if (NCH(n) == 4) { | |
| 2725 expr_ty expression; | |
| 2726 asdl_seq *suite_seq; | |
| 2727 | |
| 2728 expression = ast_for_expr(c, CHILD(n, 1)); | |
| 2729 if (!expression) | |
| 2730 return NULL; | |
| 2731 suite_seq = ast_for_suite(c, CHILD(n, 3)); | |
|
35873
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
2732 if (!suite_seq) { |
|
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
2733 free_expr(expression); |
| 35742 | 2734 return NULL; |
|
35873
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
2735 } |
| 35742 | 2736 return While(expression, suite_seq, NULL, LINENO(n)); |
| 2737 } | |
| 2738 else if (NCH(n) == 7) { | |
| 2739 expr_ty expression; | |
| 2740 asdl_seq *seq1, *seq2; | |
| 2741 | |
| 2742 expression = ast_for_expr(c, CHILD(n, 1)); | |
| 2743 if (!expression) | |
| 2744 return NULL; | |
| 2745 seq1 = ast_for_suite(c, CHILD(n, 3)); | |
|
35873
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
2746 if (!seq1) { |
|
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
2747 free_expr(expression); |
| 35742 | 2748 return NULL; |
|
35873
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
2749 } |
| 35742 | 2750 seq2 = ast_for_suite(c, CHILD(n, 6)); |
|
35873
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
2751 if (!seq2) { |
|
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
2752 asdl_stmt_seq_free(seq1); |
|
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
2753 free_expr(expression); |
| 35742 | 2754 return NULL; |
|
35873
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
2755 } |
| 35742 | 2756 |
| 2757 return While(expression, seq1, seq2, LINENO(n)); | |
| 2758 } | |
| 2759 else { | |
|
35870
f531c05cd240
Convert all internal errors from Exception to SystemError
Neal Norwitz <nnorwitz@gmail.com>
parents:
35868
diff
changeset
|
2760 PyErr_Format(PyExc_SystemError, |
| 35742 | 2761 "wrong number of tokens for 'while' statement: %d", |
| 2762 NCH(n)); | |
| 2763 return NULL; | |
| 2764 } | |
| 2765 } | |
| 2766 | |
| 2767 static stmt_ty | |
| 2768 ast_for_for_stmt(struct compiling *c, const node *n) | |
| 2769 { | |
| 2770 asdl_seq *_target = NULL, *seq = NULL, *suite_seq = NULL; | |
| 2771 expr_ty expression; | |
| 2772 expr_ty target; | |
| 2773 /* for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite] */ | |
| 2774 REQ(n, for_stmt); | |
| 2775 | |
| 2776 if (NCH(n) == 9) { | |
| 2777 seq = ast_for_suite(c, CHILD(n, 8)); | |
| 2778 if (!seq) | |
| 2779 return NULL; | |
| 2780 } | |
| 2781 | |
| 2782 _target = ast_for_exprlist(c, CHILD(n, 1), Store); | |
|
35873
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
2783 if (!_target) { |
|
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
2784 asdl_stmt_seq_free(seq); |
| 35742 | 2785 return NULL; |
|
35873
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
2786 } |
| 35742 | 2787 if (asdl_seq_LEN(_target) == 1) { |
| 2788 target = asdl_seq_GET(_target, 0); | |
|
35873
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
2789 asdl_seq_free(_target); /* ok */ |
| 35742 | 2790 } |
| 2791 else | |
| 2792 target = Tuple(_target, Store, LINENO(n)); | |
| 2793 | |
|
35798
857f68fba275
Write a separate ast_for_testlist_gexp() function instead of overloading
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35771
diff
changeset
|
2794 expression = ast_for_testlist(c, CHILD(n, 3)); |
|
35873
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
2795 if (!expression) { |
|
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
2796 free_expr(target); |
|
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
2797 asdl_stmt_seq_free(seq); |
| 35742 | 2798 return NULL; |
|
35873
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
2799 } |
| 35742 | 2800 suite_seq = ast_for_suite(c, CHILD(n, 5)); |
|
35873
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
2801 if (!suite_seq) { |
|
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
2802 free_expr(target); |
|
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
2803 free_expr(expression); |
|
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
2804 asdl_stmt_seq_free(seq); |
| 35742 | 2805 return NULL; |
|
35873
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
2806 } |
| 35742 | 2807 |
| 2808 return For(target, expression, suite_seq, seq, LINENO(n)); | |
| 2809 } | |
| 2810 | |
| 2811 static excepthandler_ty | |
| 2812 ast_for_except_clause(struct compiling *c, const node *exc, node *body) | |
| 2813 { | |
| 2814 /* except_clause: 'except' [test [',' test]] */ | |
| 2815 REQ(exc, except_clause); | |
| 2816 REQ(body, suite); | |
| 2817 | |
| 2818 if (NCH(exc) == 1) { | |
| 2819 asdl_seq *suite_seq = ast_for_suite(c, body); | |
| 2820 if (!suite_seq) | |
| 2821 return NULL; | |
| 2822 | |
| 2823 return excepthandler(NULL, NULL, suite_seq); | |
| 2824 } | |
| 2825 else if (NCH(exc) == 2) { | |
| 2826 expr_ty expression; | |
| 2827 asdl_seq *suite_seq; | |
| 2828 | |
| 2829 expression = ast_for_expr(c, CHILD(exc, 1)); | |
| 2830 if (!expression) | |
| 2831 return NULL; | |
| 2832 suite_seq = ast_for_suite(c, body); | |
|
35873
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
2833 if (!suite_seq) { |
|
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
2834 free_expr(expression); |
| 35742 | 2835 return NULL; |
|
35873
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
2836 } |
| 35742 | 2837 |
| 2838 return excepthandler(expression, NULL, suite_seq); | |
| 2839 } | |
| 2840 else if (NCH(exc) == 4) { | |
| 2841 asdl_seq *suite_seq; | |
| 2842 expr_ty expression; | |
| 2843 expr_ty e = ast_for_expr(c, CHILD(exc, 3)); | |
| 2844 if (!e) | |
| 2845 return NULL; | |
|
35873
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
2846 if (!set_context(e, Store, CHILD(exc, 3))) { |
|
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
2847 free_expr(e); |
| 35742 | 2848 return NULL; |
|
35873
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
2849 } |
| 35742 | 2850 expression = ast_for_expr(c, CHILD(exc, 1)); |
|
35873
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
2851 if (!expression) { |
|
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
2852 free_expr(e); |
| 35742 | 2853 return NULL; |
|
35873
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
2854 } |
| 35742 | 2855 suite_seq = ast_for_suite(c, body); |
|
35873
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
2856 if (!suite_seq) { |
|
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
2857 free_expr(expression); |
|
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
2858 free_expr(e); |
| 35742 | 2859 return NULL; |
|
35873
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
2860 } |
| 35742 | 2861 |
| 2862 return excepthandler(expression, e, suite_seq); | |
| 2863 } | |
| 2864 else { | |
|
35870
f531c05cd240
Convert all internal errors from Exception to SystemError
Neal Norwitz <nnorwitz@gmail.com>
parents:
35868
diff
changeset
|
2865 PyErr_Format(PyExc_SystemError, |
| 35742 | 2866 "wrong number of children for 'except' clause: %d", |
| 2867 NCH(exc)); | |
| 2868 return NULL; | |
| 2869 } | |
| 2870 } | |
| 2871 | |
| 2872 static stmt_ty | |
| 2873 ast_for_try_stmt(struct compiling *c, const node *n) | |
| 2874 { | |
| 2875 REQ(n, try_stmt); | |
| 2876 | |
| 2877 if (TYPE(CHILD(n, 3)) == NAME) {/* must be 'finally' */ | |
| 2878 /* try_stmt: 'try' ':' suite 'finally' ':' suite) */ | |
| 2879 asdl_seq *s1, *s2; | |
| 2880 s1 = ast_for_suite(c, CHILD(n, 2)); | |
| 2881 if (!s1) | |
| 2882 return NULL; | |
| 2883 s2 = ast_for_suite(c, CHILD(n, 5)); | |
|
35873
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
2884 if (!s2) { |
|
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
2885 asdl_stmt_seq_free(s1); |
| 35742 | 2886 return NULL; |
|
35873
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
2887 } |
| 35742 | 2888 |
| 2889 return TryFinally(s1, s2, LINENO(n)); | |
| 2890 } | |
| 2891 else if (TYPE(CHILD(n, 3)) == except_clause) { | |
| 2892 /* try_stmt: ('try' ':' suite (except_clause ':' suite)+ | |
| 2893 ['else' ':' suite] | |
| 2894 */ | |
| 2895 asdl_seq *suite_seq1, *suite_seq2; | |
| 2896 asdl_seq *handlers; | |
| 2897 int i, has_else = 0, n_except = NCH(n) - 3; | |
| 2898 if (TYPE(CHILD(n, NCH(n) - 3)) == NAME) { | |
| 2899 has_else = 1; | |
| 2900 n_except -= 3; | |
| 2901 } | |
| 2902 n_except /= 3; | |
| 2903 handlers = asdl_seq_new(n_except); | |
| 2904 if (!handlers) | |
| 2905 return NULL; | |
| 2906 for (i = 0; i < n_except; i++) { | |
| 2907 excepthandler_ty e = ast_for_except_clause(c, | |
| 2908 CHILD(n, 3 + i * 3), | |
| 2909 CHILD(n, 5 + i * 3)); | |
|
35873
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
2910 if (!e) { |
|
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
2911 for ( ; i >= 0; i--) |
|
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
2912 free_excepthandler(asdl_seq_GET(handlers, i)); |
|
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
2913 asdl_seq_free(handlers); /* ok */ |
| 35742 | 2914 return NULL; |
|
35873
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
2915 } |
| 35742 | 2916 asdl_seq_SET(handlers, i, e); |
| 2917 } | |
| 2918 | |
| 2919 suite_seq1 = ast_for_suite(c, CHILD(n, 2)); | |
|
35873
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
2920 if (!suite_seq1) { |
|
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
2921 for (i = 0; i < asdl_seq_LEN(handlers); i++) |
|
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
2922 free_excepthandler(asdl_seq_GET(handlers, i)); |
|
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
2923 asdl_seq_free(handlers); /* ok */ |
| 35742 | 2924 return NULL; |
|
35873
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
2925 } |
| 35742 | 2926 if (has_else) { |
| 2927 suite_seq2 = ast_for_suite(c, CHILD(n, NCH(n) - 1)); | |
|
35873
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
2928 if (!suite_seq2) { |
|
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
2929 for (i = 0; i < asdl_seq_LEN(handlers); i++) |
|
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
2930 free_excepthandler(asdl_seq_GET(handlers, i)); |
|
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
2931 asdl_seq_free(handlers); /* ok */ |
|
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
2932 asdl_stmt_seq_free(suite_seq1); |
| 35742 | 2933 return NULL; |
|
35873
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
2934 } |
| 35742 | 2935 } |
| 2936 else | |
| 2937 suite_seq2 = NULL; | |
| 2938 | |
| 2939 return TryExcept(suite_seq1, handlers, suite_seq2, LINENO(n)); | |
| 2940 } | |
| 2941 else { | |
|
35867
f4842f42700b
remove useless debug print helper. fix a couple of exceptions
Neal Norwitz <nnorwitz@gmail.com>
parents:
35799
diff
changeset
|
2942 ast_error(n, "malformed 'try' statement"); |
| 35742 | 2943 return NULL; |
| 2944 } | |
| 2945 } | |
| 2946 | |
| 2947 static stmt_ty | |
| 2948 ast_for_classdef(struct compiling *c, const node *n) | |
| 2949 { | |
| 2950 /* classdef: 'class' NAME ['(' testlist ')'] ':' suite */ | |
| 2951 asdl_seq *bases, *s; | |
| 2952 | |
| 2953 REQ(n, classdef); | |
| 2954 | |
| 2955 if (!strcmp(STR(CHILD(n, 1)), "None")) { | |
| 2956 ast_error(n, "assignment to None"); | |
| 2957 return NULL; | |
| 2958 } | |
| 2959 | |
| 2960 if (NCH(n) == 4) { | |
| 2961 s = ast_for_suite(c, CHILD(n, 3)); | |
| 2962 if (!s) | |
| 2963 return NULL; | |
| 2964 return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), NULL, s, LINENO(n)); | |
| 2965 } | |
| 2966 /* check for empty base list */ | |
| 2967 if (TYPE(CHILD(n,3)) == RPAR) { | |
| 2968 s = ast_for_suite(c, CHILD(n,5)); | |
| 2969 if (!s) | |
| 2970 return NULL; | |
| 2971 return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), NULL, s, LINENO(n)); | |
| 2972 } | |
| 2973 | |
| 2974 /* else handle the base class list */ | |
|
35798
857f68fba275
Write a separate ast_for_testlist_gexp() function instead of overloading
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35771
diff
changeset
|
2975 bases = ast_for_class_bases(c, CHILD(n, 3)); |
|
857f68fba275
Write a separate ast_for_testlist_gexp() function instead of overloading
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35771
diff
changeset
|
2976 if (!bases) |
| 35742 | 2977 return NULL; |
| 2978 | |
| 2979 s = ast_for_suite(c, CHILD(n, 6)); | |
| 2980 if (!s) { | |
|
35873
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
2981 asdl_expr_seq_free(bases); |
| 35742 | 2982 return NULL; |
| 2983 } | |
| 2984 return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), bases, s, LINENO(n)); | |
| 2985 } | |
| 2986 | |
| 2987 static stmt_ty | |
| 2988 ast_for_stmt(struct compiling *c, const node *n) | |
| 2989 { | |
| 2990 if (TYPE(n) == stmt) { | |
| 2991 assert(NCH(n) == 1); | |
| 2992 n = CHILD(n, 0); | |
| 2993 } | |
| 2994 if (TYPE(n) == simple_stmt) { | |
| 2995 assert(num_stmts(n) == 1); | |
| 2996 n = CHILD(n, 0); | |
| 2997 } | |
| 2998 if (TYPE(n) == small_stmt) { | |
| 2999 REQ(n, small_stmt); | |
| 3000 n = CHILD(n, 0); | |
| 3001 /* small_stmt: expr_stmt | print_stmt | del_stmt | pass_stmt | |
| 3002 | flow_stmt | import_stmt | global_stmt | exec_stmt | |
| 3003 | assert_stmt | |
| 3004 */ | |
| 3005 switch (TYPE(n)) { | |
| 3006 case expr_stmt: | |
| 3007 return ast_for_expr_stmt(c, n); | |
| 3008 case print_stmt: | |
| 3009 return ast_for_print_stmt(c, n); | |
| 3010 case del_stmt: | |
| 3011 return ast_for_del_stmt(c, n); | |
| 3012 case pass_stmt: | |
| 3013 return Pass(LINENO(n)); | |
| 3014 case flow_stmt: | |
| 3015 return ast_for_flow_stmt(c, n); | |
| 3016 case import_stmt: | |
| 3017 return ast_for_import_stmt(c, n); | |
| 3018 case global_stmt: | |
| 3019 return ast_for_global_stmt(c, n); | |
| 3020 case exec_stmt: | |
| 3021 return ast_for_exec_stmt(c, n); | |
| 3022 case assert_stmt: | |
| 3023 return ast_for_assert_stmt(c, n); | |
| 3024 default: | |
|
35870
f531c05cd240
Convert all internal errors from Exception to SystemError
Neal Norwitz <nnorwitz@gmail.com>
parents:
35868
diff
changeset
|
3025 PyErr_Format(PyExc_SystemError, |
| 35742 | 3026 "unhandled small_stmt: TYPE=%d NCH=%d\n", |
| 3027 TYPE(n), NCH(n)); | |
| 3028 return NULL; | |
| 3029 } | |
| 3030 } | |
| 3031 else { | |
| 3032 /* compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | |
| 3033 | funcdef | classdef | |
| 3034 */ | |
| 3035 node *ch = CHILD(n, 0); | |
| 3036 REQ(n, compound_stmt); | |
| 3037 switch (TYPE(ch)) { | |
| 3038 case if_stmt: | |
| 3039 return ast_for_if_stmt(c, ch); | |
| 3040 case while_stmt: | |
| 3041 return ast_for_while_stmt(c, ch); | |
| 3042 case for_stmt: | |
| 3043 return ast_for_for_stmt(c, ch); | |
| 3044 case try_stmt: | |
| 3045 return ast_for_try_stmt(c, ch); | |
| 3046 case funcdef: | |
| 3047 return ast_for_funcdef(c, ch); | |
| 3048 case classdef: | |
| 3049 return ast_for_classdef(c, ch); | |
| 3050 default: | |
|
35870
f531c05cd240
Convert all internal errors from Exception to SystemError
Neal Norwitz <nnorwitz@gmail.com>
parents:
35868
diff
changeset
|
3051 PyErr_Format(PyExc_SystemError, |
| 35742 | 3052 "unhandled small_stmt: TYPE=%d NCH=%d\n", |
| 3053 TYPE(n), NCH(n)); | |
| 3054 return NULL; | |
| 3055 } | |
| 3056 } | |
| 3057 } | |
| 3058 | |
| 3059 static PyObject * | |
| 3060 parsenumber(const char *s) | |
| 3061 { | |
| 3062 const char *end; | |
| 3063 long x; | |
| 3064 double dx; | |
| 3065 #ifndef WITHOUT_COMPLEX | |
| 3066 Py_complex c; | |
| 3067 int imflag; | |
| 3068 #endif | |
| 3069 | |
| 3070 errno = 0; | |
| 3071 end = s + strlen(s) - 1; | |
| 3072 #ifndef WITHOUT_COMPLEX | |
| 3073 imflag = *end == 'j' || *end == 'J'; | |
| 3074 #endif | |
| 3075 if (*end == 'l' || *end == 'L') | |
| 3076 return PyLong_FromString((char *)s, (char **)0, 0); | |
| 3077 if (s[0] == '0') { | |
| 3078 x = (long) PyOS_strtoul((char *)s, (char **)&end, 0); | |
| 3079 if (x < 0 && errno == 0) { | |
| 3080 return PyLong_FromString((char *)s, | |
| 3081 (char **)0, | |
| 3082 0); | |
| 3083 } | |
| 3084 } | |
| 3085 else | |
| 3086 x = PyOS_strtol((char *)s, (char **)&end, 0); | |
| 3087 if (*end == '\0') { | |
| 3088 if (errno != 0) | |
| 3089 return PyLong_FromString((char *)s, (char **)0, 0); | |
| 3090 return PyInt_FromLong(x); | |
| 3091 } | |
| 3092 /* XXX Huge floats may silently fail */ | |
| 3093 #ifndef WITHOUT_COMPLEX | |
| 3094 if (imflag) { | |
| 3095 c.real = 0.; | |
| 3096 PyFPE_START_PROTECT("atof", return 0) | |
| 3097 c.imag = atof(s); | |
| 3098 PyFPE_END_PROTECT(c) | |
| 3099 return PyComplex_FromCComplex(c); | |
| 3100 } | |
| 3101 else | |
| 3102 #endif | |
| 3103 { | |
| 3104 PyFPE_START_PROTECT("atof", return 0) | |
| 3105 dx = atof(s); | |
| 3106 PyFPE_END_PROTECT(dx) | |
| 3107 return PyFloat_FromDouble(dx); | |
| 3108 } | |
| 3109 } | |
| 3110 | |
| 3111 static PyObject * | |
| 3112 decode_utf8(const char **sPtr, const char *end, char* encoding) | |
| 3113 { | |
| 3114 #ifndef Py_USING_UNICODE | |
| 3115 Py_FatalError("decode_utf8 should not be called in this build."); | |
| 3116 return NULL; | |
| 3117 #else | |
| 3118 PyObject *u, *v; | |
| 3119 char *s, *t; | |
| 3120 t = s = (char *)*sPtr; | |
| 3121 /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */ | |
| 3122 while (s < end && (*s & 0x80)) s++; | |
| 3123 *sPtr = s; | |
| 3124 u = PyUnicode_DecodeUTF8(t, s - t, NULL); | |
| 3125 if (u == NULL) | |
| 3126 return NULL; | |
| 3127 v = PyUnicode_AsEncodedString(u, encoding, NULL); | |
| 3128 Py_DECREF(u); | |
| 3129 return v; | |
| 3130 #endif | |
| 3131 } | |
| 3132 | |
| 3133 static PyObject * | |
| 3134 decode_unicode(const char *s, size_t len, int rawmode, const char *encoding) | |
| 3135 { | |
| 3136 PyObject *v, *u; | |
| 3137 char *buf; | |
| 3138 char *p; | |
| 3139 const char *end; | |
| 3140 if (encoding == NULL) { | |
| 3141 buf = (char *)s; | |
| 3142 u = NULL; | |
| 3143 } else if (strcmp(encoding, "iso-8859-1") == 0) { | |
| 3144 buf = (char *)s; | |
| 3145 u = NULL; | |
| 3146 } else { | |
| 3147 /* "\XX" may become "\u005c\uHHLL" (12 bytes) */ | |
| 3148 u = PyString_FromStringAndSize((char *)NULL, len * 4); | |
| 3149 if (u == NULL) | |
| 3150 return NULL; | |
| 3151 p = buf = PyString_AsString(u); | |
| 3152 end = s + len; | |
| 3153 while (s < end) { | |
| 3154 if (*s == '\\') { | |
| 3155 *p++ = *s++; | |
| 3156 if (*s & 0x80) { | |
| 3157 strcpy(p, "u005c"); | |
| 3158 p += 5; | |
| 3159 } | |
| 3160 } | |
| 3161 if (*s & 0x80) { /* XXX inefficient */ | |
| 3162 PyObject *w; | |
| 3163 char *r; | |
| 3164 int rn, i; | |
| 3165 w = decode_utf8(&s, end, "utf-16-be"); | |
| 3166 if (w == NULL) { | |
| 3167 Py_DECREF(u); | |
| 3168 return NULL; | |
| 3169 } | |
| 3170 r = PyString_AsString(w); | |
| 3171 rn = PyString_Size(w); | |
| 3172 assert(rn % 2 == 0); | |
| 3173 for (i = 0; i < rn; i += 2) { | |
| 3174 sprintf(p, "\\u%02x%02x", | |
| 3175 r[i + 0] & 0xFF, | |
| 3176 r[i + 1] & 0xFF); | |
| 3177 p += 6; | |
| 3178 } | |
| 3179 Py_DECREF(w); | |
| 3180 } else { | |
| 3181 *p++ = *s++; | |
| 3182 } | |
| 3183 } | |
| 3184 len = p - buf; | |
| 3185 s = buf; | |
| 3186 } | |
| 3187 if (rawmode) | |
| 3188 v = PyUnicode_DecodeRawUnicodeEscape(s, len, NULL); | |
| 3189 else | |
| 3190 v = PyUnicode_DecodeUnicodeEscape(s, len, NULL); | |
| 3191 Py_XDECREF(u); | |
| 3192 return v; | |
| 3193 } | |
| 3194 | |
| 3195 /* s is a Python string literal, including the bracketing quote characters, | |
| 3196 * and r &/or u prefixes (if any), and embedded escape sequences (if any). | |
| 3197 * parsestr parses it, and returns the decoded Python string object. | |
| 3198 */ | |
| 3199 static PyObject * | |
| 3200 parsestr(const char *s, const char *encoding) | |
| 3201 { | |
| 3202 PyObject *v; | |
| 3203 size_t len; | |
| 3204 int quote = *s; | |
| 3205 int rawmode = 0; | |
| 3206 int need_encoding; | |
| 3207 int unicode = 0; | |
| 3208 | |
| 3209 if (isalpha(quote) || quote == '_') { | |
| 3210 if (quote == 'u' || quote == 'U') { | |
| 3211 quote = *++s; | |
| 3212 unicode = 1; | |
| 3213 } | |
| 3214 if (quote == 'r' || quote == 'R') { | |
| 3215 quote = *++s; | |
| 3216 rawmode = 1; | |
| 3217 } | |
| 3218 } | |
| 3219 if (quote != '\'' && quote != '\"') { | |
| 3220 PyErr_BadInternalCall(); | |
| 3221 return NULL; | |
| 3222 } | |
| 3223 s++; | |
| 3224 len = strlen(s); | |
| 3225 if (len > INT_MAX) { | |
| 3226 PyErr_SetString(PyExc_OverflowError, | |
| 3227 "string to parse is too long"); | |
| 3228 return NULL; | |
| 3229 } | |
| 3230 if (s[--len] != quote) { | |
| 3231 PyErr_BadInternalCall(); | |
| 3232 return NULL; | |
| 3233 } | |
| 3234 if (len >= 4 && s[0] == quote && s[1] == quote) { | |
| 3235 s += 2; | |
| 3236 len -= 2; | |
| 3237 if (s[--len] != quote || s[--len] != quote) { | |
| 3238 PyErr_BadInternalCall(); | |
| 3239 return NULL; | |
| 3240 } | |
| 3241 } | |
| 3242 #ifdef Py_USING_UNICODE | |
| 3243 if (unicode || Py_UnicodeFlag) { | |
| 3244 return decode_unicode(s, len, rawmode, encoding); | |
| 3245 } | |
| 3246 #endif | |
| 3247 need_encoding = (encoding != NULL && | |
| 3248 strcmp(encoding, "utf-8") != 0 && | |
| 3249 strcmp(encoding, "iso-8859-1") != 0); | |
| 3250 if (rawmode || strchr(s, '\\') == NULL) { | |
| 3251 if (need_encoding) { | |
| 3252 #ifndef Py_USING_UNICODE | |
| 3253 /* This should not happen - we never see any other | |
| 3254 encoding. */ | |
| 3255 Py_FatalError("cannot deal with encodings in this build."); | |
| 3256 #else | |
| 3257 PyObject* u = PyUnicode_DecodeUTF8(s, len, NULL); | |
| 3258 if (u == NULL) | |
| 3259 return NULL; | |
| 3260 v = PyUnicode_AsEncodedString(u, encoding, NULL); | |
| 3261 Py_DECREF(u); | |
| 3262 return v; | |
| 3263 #endif | |
| 3264 } else { | |
| 3265 return PyString_FromStringAndSize(s, len); | |
| 3266 } | |
| 3267 } | |
| 3268 | |
| 3269 v = PyString_DecodeEscape(s, len, NULL, unicode, | |
| 3270 need_encoding ? encoding : NULL); | |
| 3271 return v; | |
| 3272 } | |
| 3273 | |
| 3274 /* Build a Python string object out of a STRING atom. This takes care of | |
| 3275 * compile-time literal catenation, calling parsestr() on each piece, and | |
| 3276 * pasting the intermediate results together. | |
| 3277 */ | |
| 3278 static PyObject * | |
| 3279 parsestrplus(struct compiling *c, const node *n) | |
| 3280 { | |
| 3281 PyObject *v; | |
| 3282 int i; | |
| 3283 REQ(CHILD(n, 0), STRING); | |
| 3284 if ((v = parsestr(STR(CHILD(n, 0)), c->c_encoding)) != NULL) { | |
| 3285 /* String literal concatenation */ | |
| 3286 for (i = 1; i < NCH(n); i++) { | |
| 3287 PyObject *s; | |
| 3288 s = parsestr(STR(CHILD(n, i)), c->c_encoding); | |
| 3289 if (s == NULL) | |
| 3290 goto onError; | |
| 3291 if (PyString_Check(v) && PyString_Check(s)) { | |
| 3292 PyString_ConcatAndDel(&v, s); | |
| 3293 if (v == NULL) | |
| 3294 goto onError; | |
| 3295 } | |
| 3296 #ifdef Py_USING_UNICODE | |
| 3297 else { | |
| 3298 PyObject *temp; | |
| 3299 temp = PyUnicode_Concat(v, s); | |
| 3300 Py_DECREF(s); | |
| 3301 if (temp == NULL) | |
| 3302 goto onError; | |
| 3303 Py_DECREF(v); | |
| 3304 v = temp; | |
| 3305 } | |
| 3306 #endif | |
| 3307 } | |
| 3308 } | |
| 3309 return v; | |
| 3310 | |
| 3311 onError: | |
| 3312 Py_XDECREF(v); | |
| 3313 return NULL; | |
| 3314 } |
