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