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