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