Mercurial > cpython-lru_cache-weakref
annotate Python/ast.c @ 67965:a222a015e28d 3.2 v3.2
Make a 3.2 maintenance branch.
| author | Georg Brandl <georg@python.org> |
|---|---|
| date | Sun, 20 Feb 2011 10:31:59 +0000 |
| parents | 724920a015f7 |
| children | 3c9232a684ae e2f96bd29252 |
| rev | line source |
|---|---|
| 35742 | 1 /* |
| 2 * This file includes functions to transform a concrete syntax tree (CST) to | |
| 3 * an abstract syntax tree (AST). The main function is PyAST_FromNode(). | |
| 4 * | |
| 5 */ | |
| 6 #include "Python.h" | |
| 7 #include "Python-ast.h" | |
| 8 #include "grammar.h" | |
| 9 #include "node.h" | |
| 10 #include "ast.h" | |
| 11 #include "token.h" | |
| 12 #include "parsetok.h" | |
| 13 #include "graminit.h" | |
| 14 | |
| 15 #include <assert.h> | |
| 16 | |
| 17 /* Data structure used internally */ | |
| 18 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
|
19 char *c_encoding; /* source encoding */ |
|
ea083efdc96c
Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents:
36001
diff
changeset
|
20 PyArena *c_arena; /* arena for allocating memeory */ |
|
44025
ebc7389297d6
Merged revisions 59107-59186 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
43961
diff
changeset
|
21 const char *c_filename; /* filename */ |
| 35742 | 22 }; |
| 23 | |
| 24 static asdl_seq *seq_for_testlist(struct compiling *, const node *); | |
| 25 static expr_ty ast_for_expr(struct compiling *, const node *); | |
| 26 static stmt_ty ast_for_stmt(struct compiling *, const node *); | |
| 27 static asdl_seq *ast_for_suite(struct compiling *, const node *); | |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
28 static asdl_seq *ast_for_exprlist(struct compiling *, const node *, |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
29 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
|
30 static expr_ty ast_for_testlist(struct compiling *, const node *); |
|
41824
24b60622b944
Merged revisions 55407-55513 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41717
diff
changeset
|
31 static stmt_ty ast_for_classdef(struct compiling *, const node *, asdl_seq *); |
| 35742 | 32 |
| 33 /* Note different signature for ast_for_call */ | |
| 34 static expr_ty ast_for_call(struct compiling *, const node *, expr_ty); | |
| 35 | |
|
46664
031dfe455585
Merged revisions 62425-62429,62434-62436,62441,62444,62446-62448,62450-62455,62463,62465-62466,62469,62474,62476-62478,62480,62485,62492,62497-62498,62500,62507,62513-62514,62516,62521,62531,62535,62545-62546,62548-62551,62553-62559,62569,62574,62577,62593,62595,62604-62606,62608,62616,62626-62627,62636,62638,62644-62645,62647-62648,62651-62653,62656,62661,62663,62680,62686-62687,62696,62699-62703,62711 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
46306
diff
changeset
|
36 static PyObject *parsenumber(struct compiling *, const char *); |
|
46120
78504d604b44
Merged revisions 61952-61953 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45931
diff
changeset
|
37 static PyObject *parsestr(struct compiling *, const node *n, int *bytesmode); |
| 40894 | 38 static PyObject *parsestrplus(struct compiling *, const node *n, |
| 39 int *bytesmode); | |
| 35742 | 40 |
| 41 #ifndef LINENO | |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
42 #define LINENO(n) ((n)->n_lineno) |
| 35742 | 43 #endif |
| 44 | |
|
41464
060c79f6d919
Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents:
41235
diff
changeset
|
45 #define COMP_GENEXP 0 |
|
060c79f6d919
Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents:
41235
diff
changeset
|
46 #define COMP_LISTCOMP 1 |
|
060c79f6d919
Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents:
41235
diff
changeset
|
47 #define COMP_SETCOMP 2 |
|
060c79f6d919
Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents:
41235
diff
changeset
|
48 |
|
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
|
49 static identifier |
|
42597
20477cbfd996
Implement PEP 3131. Add isidentifier to str.
Martin v. Löwis <martin@v.loewis.de>
parents:
42549
diff
changeset
|
50 new_identifier(const char* n, PyArena *arena) |
|
20477cbfd996
Implement PEP 3131. Add isidentifier to str.
Martin v. Löwis <martin@v.loewis.de>
parents:
42549
diff
changeset
|
51 { |
|
42013
cd125fe83051
Make identifiers str (not str8) objects throughout.
Martin v. Löwis <martin@v.loewis.de>
parents:
41969
diff
changeset
|
52 PyObject* id = PyUnicode_DecodeUTF8(n, strlen(n), NULL); |
|
49847
6c0871f0c7a6
Make ast.c compile on Windows again.
Thomas Heller <theller@ctypes.org>
parents:
49844
diff
changeset
|
53 Py_UNICODE *u; |
|
49844
40b7277e665f
Merged revisions 67373 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
49797
diff
changeset
|
54 if (!id) |
|
40b7277e665f
Merged revisions 67373 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
49797
diff
changeset
|
55 return NULL; |
|
49847
6c0871f0c7a6
Make ast.c compile on Windows again.
Thomas Heller <theller@ctypes.org>
parents:
49844
diff
changeset
|
56 u = PyUnicode_AS_UNICODE(id); |
|
42597
20477cbfd996
Implement PEP 3131. Add isidentifier to str.
Martin v. Löwis <martin@v.loewis.de>
parents:
42549
diff
changeset
|
57 /* Check whether there are non-ASCII characters in the |
|
20477cbfd996
Implement PEP 3131. Add isidentifier to str.
Martin v. Löwis <martin@v.loewis.de>
parents:
42549
diff
changeset
|
58 identifier; if so, normalize to NFKC. */ |
|
20477cbfd996
Implement PEP 3131. Add isidentifier to str.
Martin v. Löwis <martin@v.loewis.de>
parents:
42549
diff
changeset
|
59 for (; *u; u++) { |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
56951
diff
changeset
|
60 if (*u >= 128) { |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
56951
diff
changeset
|
61 PyObject *m = PyImport_ImportModuleNoBlock("unicodedata"); |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
56951
diff
changeset
|
62 PyObject *id2; |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
56951
diff
changeset
|
63 if (!m) |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
56951
diff
changeset
|
64 return NULL; |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
56951
diff
changeset
|
65 id2 = PyObject_CallMethod(m, "normalize", "sO", "NFKC", id); |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
56951
diff
changeset
|
66 Py_DECREF(m); |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
56951
diff
changeset
|
67 if (!id2) |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
56951
diff
changeset
|
68 return NULL; |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
56951
diff
changeset
|
69 Py_DECREF(id); |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
56951
diff
changeset
|
70 id = id2; |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
56951
diff
changeset
|
71 break; |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
56951
diff
changeset
|
72 } |
|
42597
20477cbfd996
Implement PEP 3131. Add isidentifier to str.
Martin v. Löwis <martin@v.loewis.de>
parents:
42549
diff
changeset
|
73 } |
|
42013
cd125fe83051
Make identifiers str (not str8) objects throughout.
Martin v. Löwis <martin@v.loewis.de>
parents:
41969
diff
changeset
|
74 PyUnicode_InternInPlace(&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
|
75 PyArena_AddPyObject(arena, id); |
|
ea083efdc96c
Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents:
36001
diff
changeset
|
76 return id; |
| 35742 | 77 } |
| 78 | |
|
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
|
79 #define NEW_IDENTIFIER(n) new_identifier(STR(n), c->c_arena) |
| 35742 | 80 |
| 81 /* This routine provides an invalid object for the syntax error. | |
| 82 The outermost routine must unpack this error and create the | |
| 83 proper object. We do this so that we don't have to pass | |
| 84 the filename to everything function. | |
| 85 | |
| 86 XXX Maybe we should just pass the filename... | |
| 87 */ | |
| 88 | |
| 89 static int | |
| 90 ast_error(const node *n, const char *errstr) | |
| 91 { | |
|
64861
861c35cef7aa
add column offset to all syntax errors
Benjamin Peterson <benjamin@python.org>
parents:
64184
diff
changeset
|
92 PyObject *u = Py_BuildValue("zii", errstr, LINENO(n), n->n_col_offset); |
| 35742 | 93 if (!u) |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
94 return 0; |
| 35742 | 95 PyErr_SetObject(PyExc_SyntaxError, u); |
| 96 Py_DECREF(u); | |
| 97 return 0; | |
| 98 } | |
| 99 | |
| 100 static void | |
| 101 ast_error_finish(const char *filename) | |
| 102 { | |
|
64861
861c35cef7aa
add column offset to all syntax errors
Benjamin Peterson <benjamin@python.org>
parents:
64184
diff
changeset
|
103 PyObject *type, *value, *tback, *errstr, *offset, *loc, *tmp; |
|
65399
55ed5c524428
Issue #9713, #10114: Parser functions (eg. PyParser_ASTFromFile) expects
Victor Stinner <victor.stinner@haypocalc.com>
parents:
64861
diff
changeset
|
104 PyObject *filename_obj; |
|
36231
d3f4c467e439
Fix icc warnings: conversion from "long" to "int" may lose significant bits
Neal Norwitz <nnorwitz@gmail.com>
parents:
36224
diff
changeset
|
105 long lineno; |
| 35742 | 106 |
| 107 assert(PyErr_Occurred()); | |
| 108 if (!PyErr_ExceptionMatches(PyExc_SyntaxError)) | |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
109 return; |
| 35742 | 110 |
| 111 PyErr_Fetch(&type, &value, &tback); | |
| 112 errstr = PyTuple_GetItem(value, 0); | |
| 113 if (!errstr) | |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
114 return; |
| 35742 | 115 Py_INCREF(errstr); |
|
44099
f324631462a2
Cleanup: Replaced most PyInt_ aliases with PyLong_ and disabled the aliases in intobject.h
Christian Heimes <christian@cheimes.de>
parents:
44025
diff
changeset
|
116 lineno = PyLong_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
|
117 if (lineno == -1) { |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
118 Py_DECREF(errstr); |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
119 return; |
|
36000
cc3261597136
SF #1370197, memory leak - ast_error_finish (in error conditions).
Neal Norwitz <nnorwitz@gmail.com>
parents:
35990
diff
changeset
|
120 } |
|
64861
861c35cef7aa
add column offset to all syntax errors
Benjamin Peterson <benjamin@python.org>
parents:
64184
diff
changeset
|
121 offset = PyTuple_GetItem(value, 2); |
|
861c35cef7aa
add column offset to all syntax errors
Benjamin Peterson <benjamin@python.org>
parents:
64184
diff
changeset
|
122 if (!offset) { |
|
861c35cef7aa
add column offset to all syntax errors
Benjamin Peterson <benjamin@python.org>
parents:
64184
diff
changeset
|
123 Py_DECREF(errstr); |
|
861c35cef7aa
add column offset to all syntax errors
Benjamin Peterson <benjamin@python.org>
parents:
64184
diff
changeset
|
124 return; |
|
861c35cef7aa
add column offset to all syntax errors
Benjamin Peterson <benjamin@python.org>
parents:
64184
diff
changeset
|
125 } |
| 35742 | 126 Py_DECREF(value); |
| 127 | |
| 128 loc = PyErr_ProgramText(filename, lineno); | |
| 129 if (!loc) { | |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
130 Py_INCREF(Py_None); |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
131 loc = Py_None; |
| 35742 | 132 } |
|
65400
9ed68dd6b71f
Fix ast_error_finish() and err_input(): filename can be NULL
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65399
diff
changeset
|
133 if (filename != NULL) |
|
9ed68dd6b71f
Fix ast_error_finish() and err_input(): filename can be NULL
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65399
diff
changeset
|
134 filename_obj = PyUnicode_DecodeFSDefault(filename); |
|
9ed68dd6b71f
Fix ast_error_finish() and err_input(): filename can be NULL
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65399
diff
changeset
|
135 else { |
|
9ed68dd6b71f
Fix ast_error_finish() and err_input(): filename can be NULL
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65399
diff
changeset
|
136 Py_INCREF(Py_None); |
|
9ed68dd6b71f
Fix ast_error_finish() and err_input(): filename can be NULL
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65399
diff
changeset
|
137 filename_obj = Py_None; |
|
9ed68dd6b71f
Fix ast_error_finish() and err_input(): filename can be NULL
Victor Stinner <victor.stinner@haypocalc.com>
parents:
65399
diff
changeset
|
138 } |
|
65399
55ed5c524428
Issue #9713, #10114: Parser functions (eg. PyParser_ASTFromFile) expects
Victor Stinner <victor.stinner@haypocalc.com>
parents:
64861
diff
changeset
|
139 if (filename_obj != NULL) |
|
55ed5c524428
Issue #9713, #10114: Parser functions (eg. PyParser_ASTFromFile) expects
Victor Stinner <victor.stinner@haypocalc.com>
parents:
64861
diff
changeset
|
140 tmp = Py_BuildValue("(NlOO)", filename_obj, lineno, offset, loc); |
|
55ed5c524428
Issue #9713, #10114: Parser functions (eg. PyParser_ASTFromFile) expects
Victor Stinner <victor.stinner@haypocalc.com>
parents:
64861
diff
changeset
|
141 else |
|
55ed5c524428
Issue #9713, #10114: Parser functions (eg. PyParser_ASTFromFile) expects
Victor Stinner <victor.stinner@haypocalc.com>
parents:
64861
diff
changeset
|
142 tmp = NULL; |
| 35742 | 143 Py_DECREF(loc); |
|
36000
cc3261597136
SF #1370197, memory leak - ast_error_finish (in error conditions).
Neal Norwitz <nnorwitz@gmail.com>
parents:
35990
diff
changeset
|
144 if (!tmp) { |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
145 Py_DECREF(errstr); |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
146 return; |
|
36000
cc3261597136
SF #1370197, memory leak - ast_error_finish (in error conditions).
Neal Norwitz <nnorwitz@gmail.com>
parents:
35990
diff
changeset
|
147 } |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37879
diff
changeset
|
148 value = PyTuple_Pack(2, errstr, tmp); |
| 35742 | 149 Py_DECREF(errstr); |
| 150 Py_DECREF(tmp); | |
| 151 if (!value) | |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
152 return; |
| 35742 | 153 PyErr_Restore(type, value, tback); |
| 154 } | |
| 155 | |
| 156 /* num_stmts() returns number of contained statements. | |
| 157 | |
| 158 Use this routine to determine how big a sequence is needed for | |
| 159 the statements in a parse tree. Its raison d'etre is this bit of | |
| 160 grammar: | |
| 161 | |
| 162 stmt: simple_stmt | compound_stmt | |
| 163 simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE | |
| 164 | |
| 165 A simple_stmt can contain multiple small_stmt elements joined | |
| 166 by semicolons. If the arg is a simple_stmt, the number of | |
| 167 small_stmt elements is returned. | |
| 168 */ | |
| 169 | |
| 170 static int | |
| 171 num_stmts(const node *n) | |
| 172 { | |
| 173 int i, l; | |
| 174 node *ch; | |
| 175 | |
| 176 switch (TYPE(n)) { | |
| 177 case single_input: | |
| 178 if (TYPE(CHILD(n, 0)) == NEWLINE) | |
| 179 return 0; | |
| 180 else | |
| 181 return num_stmts(CHILD(n, 0)); | |
| 182 case file_input: | |
| 183 l = 0; | |
| 184 for (i = 0; i < NCH(n); i++) { | |
| 185 ch = CHILD(n, i); | |
| 186 if (TYPE(ch) == stmt) | |
| 187 l += num_stmts(ch); | |
| 188 } | |
| 189 return l; | |
| 190 case stmt: | |
| 191 return num_stmts(CHILD(n, 0)); | |
| 192 case compound_stmt: | |
| 193 return 1; | |
| 194 case simple_stmt: | |
| 195 return NCH(n) / 2; /* Divide by 2 to remove count of semi-colons */ | |
| 196 case suite: | |
| 197 if (NCH(n) == 1) | |
| 198 return num_stmts(CHILD(n, 0)); | |
| 199 else { | |
| 200 l = 0; | |
| 201 for (i = 2; i < (NCH(n) - 1); i++) | |
| 202 l += num_stmts(CHILD(n, i)); | |
| 203 return l; | |
| 204 } | |
| 205 default: { | |
| 206 char buf[128]; | |
| 207 | |
|
50886
581d8602f3f7
Merged revisions 68746 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
50035
diff
changeset
|
208 sprintf(buf, "Non-statement found: %d %d", |
| 35742 | 209 TYPE(n), NCH(n)); |
| 210 Py_FatalError(buf); | |
| 211 } | |
| 212 } | |
| 213 assert(0); | |
| 214 return 0; | |
| 215 } | |
| 216 | |
| 217 /* Transform the CST rooted at node * to the appropriate AST | |
| 218 */ | |
| 219 | |
| 220 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
|
221 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
|
222 PyArena *arena) |
| 35742 | 223 { |
|
36718
45e6f901902c
Remove asdl_seq_APPEND() and simplify asdl seq implementation.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36712
diff
changeset
|
224 int i, j, k, num; |
| 35742 | 225 asdl_seq *stmts = NULL; |
| 226 stmt_ty s; | |
| 227 node *ch; | |
| 228 struct compiling c; | |
| 229 | |
| 230 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
|
231 c.c_encoding = "utf-8"; |
|
37879
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36877
diff
changeset
|
232 if (TYPE(n) == encoding_decl) { |
|
42054
063530fcc32e
Minimal changes to make the "freeze" tool work again.
Guido van Rossum <guido@python.org>
parents:
42013
diff
changeset
|
233 #if 0 |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
234 ast_error(n, "encoding declaration in Unicode string"); |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
235 goto error; |
|
42054
063530fcc32e
Minimal changes to make the "freeze" tool work again.
Guido van Rossum <guido@python.org>
parents:
42013
diff
changeset
|
236 #endif |
|
063530fcc32e
Minimal changes to make the "freeze" tool work again.
Guido van Rossum <guido@python.org>
parents:
42013
diff
changeset
|
237 n = CHILD(n, 0); |
|
37879
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36877
diff
changeset
|
238 } |
| 35742 | 239 } else if (TYPE(n) == encoding_decl) { |
| 240 c.c_encoding = STR(n); | |
| 241 n = CHILD(n, 0); | |
| 242 } else { | |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
56951
diff
changeset
|
243 /* PEP 3120 */ |
| 42403 | 244 c.c_encoding = "utf-8"; |
| 35742 | 245 } |
|
36065
ea083efdc96c
Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents:
36001
diff
changeset
|
246 c.c_arena = arena; |
|
44025
ebc7389297d6
Merged revisions 59107-59186 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
43961
diff
changeset
|
247 c.c_filename = filename; |
| 35742 | 248 |
|
36718
45e6f901902c
Remove asdl_seq_APPEND() and simplify asdl seq implementation.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36712
diff
changeset
|
249 k = 0; |
| 35742 | 250 switch (TYPE(n)) { |
| 251 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
|
252 stmts = asdl_seq_new(num_stmts(n), arena); |
| 35742 | 253 if (!stmts) |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
254 return NULL; |
| 35742 | 255 for (i = 0; i < NCH(n) - 1; i++) { |
| 256 ch = CHILD(n, i); | |
| 257 if (TYPE(ch) == NEWLINE) | |
| 258 continue; | |
| 259 REQ(ch, stmt); | |
| 260 num = num_stmts(ch); | |
| 261 if (num == 1) { | |
| 262 s = ast_for_stmt(&c, ch); | |
| 263 if (!s) | |
| 264 goto error; | |
|
36718
45e6f901902c
Remove asdl_seq_APPEND() and simplify asdl seq implementation.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36712
diff
changeset
|
265 asdl_seq_SET(stmts, k++, s); |
| 35742 | 266 } |
| 267 else { | |
| 268 ch = CHILD(ch, 0); | |
| 269 REQ(ch, simple_stmt); | |
| 270 for (j = 0; j < num; j++) { | |
| 271 s = ast_for_stmt(&c, CHILD(ch, j * 2)); | |
| 272 if (!s) | |
| 273 goto error; | |
|
36718
45e6f901902c
Remove asdl_seq_APPEND() and simplify asdl seq implementation.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36712
diff
changeset
|
274 asdl_seq_SET(stmts, k++, s); |
| 35742 | 275 } |
| 276 } | |
| 277 } | |
|
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 Module(stmts, arena); |
| 35742 | 279 case eval_input: { |
| 280 expr_ty testlist_ast; | |
| 281 | |
|
41464
060c79f6d919
Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents:
41235
diff
changeset
|
282 /* 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
|
283 testlist_ast = ast_for_testlist(&c, CHILD(n, 0)); |
| 35742 | 284 if (!testlist_ast) |
| 285 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
|
286 return Expression(testlist_ast, arena); |
| 35742 | 287 } |
| 288 case single_input: | |
| 289 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
|
290 stmts = asdl_seq_new(1, arena); |
| 35742 | 291 if (!stmts) |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
292 goto error; |
|
37879
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36877
diff
changeset
|
293 asdl_seq_SET(stmts, 0, Pass(n->n_lineno, n->n_col_offset, |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36877
diff
changeset
|
294 arena)); |
|
41716
00c02c4488ce
Merged revisions 55325-55327 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41699
diff
changeset
|
295 if (!asdl_seq_GET(stmts, 0)) |
|
00c02c4488ce
Merged revisions 55325-55327 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41699
diff
changeset
|
296 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
|
297 return Interactive(stmts, arena); |
| 35742 | 298 } |
| 299 else { | |
| 300 n = CHILD(n, 0); | |
| 301 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
|
302 stmts = asdl_seq_new(num, arena); |
| 35742 | 303 if (!stmts) |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
304 goto error; |
| 35742 | 305 if (num == 1) { |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
306 s = ast_for_stmt(&c, n); |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
307 if (!s) |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
308 goto error; |
| 35742 | 309 asdl_seq_SET(stmts, 0, s); |
| 310 } | |
| 311 else { | |
| 312 /* Only a simple_stmt can contain multiple statements. */ | |
| 313 REQ(n, simple_stmt); | |
| 314 for (i = 0; i < NCH(n); i += 2) { | |
| 315 if (TYPE(CHILD(n, i)) == NEWLINE) | |
| 316 break; | |
| 317 s = ast_for_stmt(&c, CHILD(n, i)); | |
| 318 if (!s) | |
| 319 goto error; | |
| 320 asdl_seq_SET(stmts, i / 2, s); | |
| 321 } | |
| 322 } | |
| 323 | |
|
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
|
324 return Interactive(stmts, arena); |
| 35742 | 325 } |
| 326 default: | |
|
41716
00c02c4488ce
Merged revisions 55325-55327 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41699
diff
changeset
|
327 PyErr_Format(PyExc_SystemError, |
|
00c02c4488ce
Merged revisions 55325-55327 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41699
diff
changeset
|
328 "invalid node %d for PyAST_FromNode", TYPE(n)); |
| 35742 | 329 goto error; |
| 330 } | |
| 331 error: | |
| 332 ast_error_finish(filename); | |
| 333 return NULL; | |
| 334 } | |
| 335 | |
| 336 /* Return the AST repr. of the operator represented as syntax (|, ^, etc.) | |
| 337 */ | |
| 338 | |
| 339 static operator_ty | |
| 340 get_operator(const node *n) | |
| 341 { | |
| 342 switch (TYPE(n)) { | |
| 343 case VBAR: | |
| 344 return BitOr; | |
| 345 case CIRCUMFLEX: | |
| 346 return BitXor; | |
| 347 case AMPER: | |
| 348 return BitAnd; | |
| 349 case LEFTSHIFT: | |
| 350 return LShift; | |
| 351 case RIGHTSHIFT: | |
| 352 return RShift; | |
| 353 case PLUS: | |
| 354 return Add; | |
| 355 case MINUS: | |
| 356 return Sub; | |
| 357 case STAR: | |
| 358 return Mult; | |
| 359 case SLASH: | |
| 360 return Div; | |
| 361 case DOUBLESLASH: | |
| 362 return FloorDiv; | |
| 363 case PERCENT: | |
| 364 return Mod; | |
| 365 default: | |
|
37879
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36877
diff
changeset
|
366 return (operator_ty)0; |
| 35742 | 367 } |
| 368 } | |
| 369 | |
|
41969
9d02a4adf574
Merged revisions 55631-55794 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41824
diff
changeset
|
370 static const char* FORBIDDEN[] = { |
|
9d02a4adf574
Merged revisions 55631-55794 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41824
diff
changeset
|
371 "None", |
|
9d02a4adf574
Merged revisions 55631-55794 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41824
diff
changeset
|
372 "True", |
|
9d02a4adf574
Merged revisions 55631-55794 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41824
diff
changeset
|
373 "False", |
|
9d02a4adf574
Merged revisions 55631-55794 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41824
diff
changeset
|
374 NULL, |
|
9d02a4adf574
Merged revisions 55631-55794 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41824
diff
changeset
|
375 }; |
|
9d02a4adf574
Merged revisions 55631-55794 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41824
diff
changeset
|
376 |
|
9d02a4adf574
Merged revisions 55631-55794 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41824
diff
changeset
|
377 static int |
|
54954
c45e331238ae
In most cases, the parser will protect True, False, and None from being assign to.
Benjamin Peterson <benjamin@python.org>
parents:
54918
diff
changeset
|
378 forbidden_name(identifier name, const node *n, int full_checks) |
|
41969
9d02a4adf574
Merged revisions 55631-55794 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41824
diff
changeset
|
379 { |
|
54918
0704f15b4682
Merged revisions 73376,73393,73398,73400,73404-73405,73409,73419-73421,73432,73457,73460,73485-73486,73488-73489,73501-73502,73513-73514 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
54703
diff
changeset
|
380 assert(PyUnicode_Check(name)); |
|
54954
c45e331238ae
In most cases, the parser will protect True, False, and None from being assign to.
Benjamin Peterson <benjamin@python.org>
parents:
54918
diff
changeset
|
381 if (PyUnicode_CompareWithASCIIString(name, "__debug__") == 0) { |
|
c45e331238ae
In most cases, the parser will protect True, False, and None from being assign to.
Benjamin Peterson <benjamin@python.org>
parents:
54918
diff
changeset
|
382 ast_error(n, "assignment to keyword"); |
|
c45e331238ae
In most cases, the parser will protect True, False, and None from being assign to.
Benjamin Peterson <benjamin@python.org>
parents:
54918
diff
changeset
|
383 return 1; |
|
c45e331238ae
In most cases, the parser will protect True, False, and None from being assign to.
Benjamin Peterson <benjamin@python.org>
parents:
54918
diff
changeset
|
384 } |
|
c45e331238ae
In most cases, the parser will protect True, False, and None from being assign to.
Benjamin Peterson <benjamin@python.org>
parents:
54918
diff
changeset
|
385 if (full_checks) { |
|
c45e331238ae
In most cases, the parser will protect True, False, and None from being assign to.
Benjamin Peterson <benjamin@python.org>
parents:
54918
diff
changeset
|
386 const char **p; |
|
c45e331238ae
In most cases, the parser will protect True, False, and None from being assign to.
Benjamin Peterson <benjamin@python.org>
parents:
54918
diff
changeset
|
387 for (p = FORBIDDEN; *p; p++) { |
|
c45e331238ae
In most cases, the parser will protect True, False, and None from being assign to.
Benjamin Peterson <benjamin@python.org>
parents:
54918
diff
changeset
|
388 if (PyUnicode_CompareWithASCIIString(name, *p) == 0) { |
|
c45e331238ae
In most cases, the parser will protect True, False, and None from being assign to.
Benjamin Peterson <benjamin@python.org>
parents:
54918
diff
changeset
|
389 ast_error(n, "assignment to keyword"); |
|
c45e331238ae
In most cases, the parser will protect True, False, and None from being assign to.
Benjamin Peterson <benjamin@python.org>
parents:
54918
diff
changeset
|
390 return 1; |
|
c45e331238ae
In most cases, the parser will protect True, False, and None from being assign to.
Benjamin Peterson <benjamin@python.org>
parents:
54918
diff
changeset
|
391 } |
|
41969
9d02a4adf574
Merged revisions 55631-55794 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41824
diff
changeset
|
392 } |
|
9d02a4adf574
Merged revisions 55631-55794 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41824
diff
changeset
|
393 } |
|
9d02a4adf574
Merged revisions 55631-55794 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41824
diff
changeset
|
394 return 0; |
|
9d02a4adf574
Merged revisions 55631-55794 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41824
diff
changeset
|
395 } |
|
9d02a4adf574
Merged revisions 55631-55794 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41824
diff
changeset
|
396 |
|
36718
45e6f901902c
Remove asdl_seq_APPEND() and simplify asdl seq implementation.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36712
diff
changeset
|
397 /* Set the context ctx for expr_ty e, recursively traversing e. |
| 35742 | 398 |
| 399 Only sets context for expr kinds that "can appear in assignment context" | |
| 400 (according to ../Parser/Python.asdl). For other expr kinds, it sets | |
| 401 an appropriate syntax error and returns false. | |
| 402 */ | |
| 403 | |
| 404 static int | |
|
46664
031dfe455585
Merged revisions 62425-62429,62434-62436,62441,62444,62446-62448,62450-62455,62463,62465-62466,62469,62474,62476-62478,62480,62485,62492,62497-62498,62500,62507,62513-62514,62516,62521,62531,62535,62545-62546,62548-62551,62553-62559,62569,62574,62577,62593,62595,62604-62606,62608,62616,62626-62627,62636,62638,62644-62645,62647-62648,62651-62653,62656,62661,62663,62680,62686-62687,62696,62699-62703,62711 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
46306
diff
changeset
|
405 set_context(struct compiling *c, expr_ty e, expr_context_ty ctx, const node *n) |
| 35742 | 406 { |
| 407 asdl_seq *s = NULL; | |
|
36418
a625e8876407
Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36242
diff
changeset
|
408 /* 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
|
409 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
|
410 */ |
|
a625e8876407
Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36242
diff
changeset
|
411 const char* expr_name = NULL; |
|
a625e8876407
Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36242
diff
changeset
|
412 |
|
a625e8876407
Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36242
diff
changeset
|
413 /* 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
|
414 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
|
415 a little more complex than necessary as a result. It also means |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
416 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
|
417 Consider restructuring so that augmented assignment uses |
|
36695
b6d2f6fd3e11
PEP 343 -- the with-statement.
Guido van Rossum <guido@python.org>
parents:
36686
diff
changeset
|
418 set_context(), too. |
|
36418
a625e8876407
Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36242
diff
changeset
|
419 */ |
|
a625e8876407
Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36242
diff
changeset
|
420 assert(ctx != AugStore && ctx != AugLoad); |
| 35742 | 421 |
| 422 switch (e->kind) { | |
| 423 case Attribute_kind: | |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
424 e->v.Attribute.ctx = ctx; |
|
54954
c45e331238ae
In most cases, the parser will protect True, False, and None from being assign to.
Benjamin Peterson <benjamin@python.org>
parents:
54918
diff
changeset
|
425 if (ctx == Store && forbidden_name(e->v.Attribute.attr, n, 1)) |
|
c45e331238ae
In most cases, the parser will protect True, False, and None from being assign to.
Benjamin Peterson <benjamin@python.org>
parents:
54918
diff
changeset
|
426 return 0; |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
427 break; |
| 35742 | 428 case Subscript_kind: |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
429 e->v.Subscript.ctx = ctx; |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
430 break; |
|
41699
e16c8e5c5ca4
Merged revisions 55225-55227,55229-55269 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41698
diff
changeset
|
431 case Starred_kind: |
|
e16c8e5c5ca4
Merged revisions 55225-55227,55229-55269 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41698
diff
changeset
|
432 e->v.Starred.ctx = ctx; |
|
46664
031dfe455585
Merged revisions 62425-62429,62434-62436,62441,62444,62446-62448,62450-62455,62463,62465-62466,62469,62474,62476-62478,62480,62485,62492,62497-62498,62500,62507,62513-62514,62516,62521,62531,62535,62545-62546,62548-62551,62553-62559,62569,62574,62577,62593,62595,62604-62606,62608,62616,62626-62627,62636,62638,62644-62645,62647-62648,62651-62653,62656,62661,62663,62680,62686-62687,62696,62699-62703,62711 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
46306
diff
changeset
|
433 if (!set_context(c, e->v.Starred.value, ctx, n)) |
|
41699
e16c8e5c5ca4
Merged revisions 55225-55227,55229-55269 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41698
diff
changeset
|
434 return 0; |
|
e16c8e5c5ca4
Merged revisions 55225-55227,55229-55269 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41698
diff
changeset
|
435 break; |
| 35742 | 436 case Name_kind: |
|
41969
9d02a4adf574
Merged revisions 55631-55794 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41824
diff
changeset
|
437 if (ctx == Store) { |
|
54954
c45e331238ae
In most cases, the parser will protect True, False, and None from being assign to.
Benjamin Peterson <benjamin@python.org>
parents:
54918
diff
changeset
|
438 if (forbidden_name(e->v.Name.id, n, 1)) |
|
41969
9d02a4adf574
Merged revisions 55631-55794 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41824
diff
changeset
|
439 return 0; /* forbidden_name() calls ast_error() */ |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
440 } |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
441 e->v.Name.ctx = ctx; |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
442 break; |
| 35742 | 443 case List_kind: |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
444 e->v.List.ctx = ctx; |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
445 s = e->v.List.elts; |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
446 break; |
| 35742 | 447 case Tuple_kind: |
|
54918
0704f15b4682
Merged revisions 73376,73393,73398,73400,73404-73405,73409,73419-73421,73432,73457,73460,73485-73486,73488-73489,73501-73502,73513-73514 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
54703
diff
changeset
|
448 if (asdl_seq_LEN(e->v.Tuple.elts)) { |
|
0704f15b4682
Merged revisions 73376,73393,73398,73400,73404-73405,73409,73419-73421,73432,73457,73460,73485-73486,73488-73489,73501-73502,73513-73514 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
54703
diff
changeset
|
449 e->v.Tuple.ctx = ctx; |
|
0704f15b4682
Merged revisions 73376,73393,73398,73400,73404-73405,73409,73419-73421,73432,73457,73460,73485-73486,73488-73489,73501-73502,73513-73514 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
54703
diff
changeset
|
450 s = e->v.Tuple.elts; |
|
0704f15b4682
Merged revisions 73376,73393,73398,73400,73404-73405,73409,73419-73421,73432,73457,73460,73485-73486,73488-73489,73501-73502,73513-73514 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
54703
diff
changeset
|
451 } |
|
0704f15b4682
Merged revisions 73376,73393,73398,73400,73404-73405,73409,73419-73421,73432,73457,73460,73485-73486,73488-73489,73501-73502,73513-73514 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
54703
diff
changeset
|
452 else { |
|
0704f15b4682
Merged revisions 73376,73393,73398,73400,73404-73405,73409,73419-73421,73432,73457,73460,73485-73486,73488-73489,73501-73502,73513-73514 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
54703
diff
changeset
|
453 expr_name = "()"; |
|
0704f15b4682
Merged revisions 73376,73393,73398,73400,73404-73405,73409,73419-73421,73432,73457,73460,73485-73486,73488-73489,73501-73502,73513-73514 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
54703
diff
changeset
|
454 } |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
455 break; |
|
36418
a625e8876407
Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36242
diff
changeset
|
456 case Lambda_kind: |
|
a625e8876407
Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36242
diff
changeset
|
457 expr_name = "lambda"; |
|
a625e8876407
Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36242
diff
changeset
|
458 break; |
| 35742 | 459 case Call_kind: |
|
36418
a625e8876407
Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36242
diff
changeset
|
460 expr_name = "function call"; |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
461 break; |
|
36418
a625e8876407
Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36242
diff
changeset
|
462 case BoolOp_kind: |
| 35742 | 463 case BinOp_kind: |
|
36418
a625e8876407
Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36242
diff
changeset
|
464 case UnaryOp_kind: |
|
a625e8876407
Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36242
diff
changeset
|
465 expr_name = "operator"; |
|
a625e8876407
Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36242
diff
changeset
|
466 break; |
| 35742 | 467 case GeneratorExp_kind: |
|
36418
a625e8876407
Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36242
diff
changeset
|
468 expr_name = "generator expression"; |
|
a625e8876407
Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36242
diff
changeset
|
469 break; |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
470 case Yield_kind: |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
471 expr_name = "yield expression"; |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
472 break; |
|
36418
a625e8876407
Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36242
diff
changeset
|
473 case ListComp_kind: |
|
a625e8876407
Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36242
diff
changeset
|
474 expr_name = "list comprehension"; |
|
a625e8876407
Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36242
diff
changeset
|
475 break; |
|
41699
e16c8e5c5ca4
Merged revisions 55225-55227,55229-55269 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41698
diff
changeset
|
476 case SetComp_kind: |
|
e16c8e5c5ca4
Merged revisions 55225-55227,55229-55269 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41698
diff
changeset
|
477 expr_name = "set comprehension"; |
|
e16c8e5c5ca4
Merged revisions 55225-55227,55229-55269 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41698
diff
changeset
|
478 break; |
|
42193
090588a17490
Merged revisions 56154-56264 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42084
diff
changeset
|
479 case DictComp_kind: |
|
090588a17490
Merged revisions 56154-56264 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42084
diff
changeset
|
480 expr_name = "dict comprehension"; |
|
090588a17490
Merged revisions 56154-56264 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42084
diff
changeset
|
481 break; |
|
36418
a625e8876407
Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36242
diff
changeset
|
482 case Dict_kind: |
|
40529
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
483 case Set_kind: |
| 35742 | 484 case Num_kind: |
| 485 case Str_kind: | |
|
36418
a625e8876407
Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36242
diff
changeset
|
486 expr_name = "literal"; |
|
a625e8876407
Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36242
diff
changeset
|
487 break; |
|
40529
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
488 case Ellipsis_kind: |
|
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
489 expr_name = "Ellipsis"; |
|
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
490 break; |
|
36418
a625e8876407
Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36242
diff
changeset
|
491 case Compare_kind: |
|
a625e8876407
Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36242
diff
changeset
|
492 expr_name = "comparison"; |
|
a625e8876407
Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36242
diff
changeset
|
493 break; |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37879
diff
changeset
|
494 case IfExp_kind: |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37879
diff
changeset
|
495 expr_name = "conditional expression"; |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37879
diff
changeset
|
496 break; |
|
36418
a625e8876407
Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36242
diff
changeset
|
497 default: |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
56951
diff
changeset
|
498 PyErr_Format(PyExc_SystemError, |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
56951
diff
changeset
|
499 "unexpected expression in assignment %d (line %d)", |
|
36418
a625e8876407
Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36242
diff
changeset
|
500 e->kind, e->lineno); |
|
a625e8876407
Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36242
diff
changeset
|
501 return 0; |
| 35742 | 502 } |
|
36418
a625e8876407
Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36242
diff
changeset
|
503 /* Check for error string set by switch */ |
|
a625e8876407
Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36242
diff
changeset
|
504 if (expr_name) { |
|
a625e8876407
Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36242
diff
changeset
|
505 char buf[300]; |
|
a625e8876407
Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36242
diff
changeset
|
506 PyOS_snprintf(buf, sizeof(buf), |
|
a625e8876407
Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36242
diff
changeset
|
507 "can't %s %s", |
|
a625e8876407
Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36242
diff
changeset
|
508 ctx == Store ? "assign to" : "delete", |
|
a625e8876407
Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36242
diff
changeset
|
509 expr_name); |
|
a625e8876407
Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36242
diff
changeset
|
510 return ast_error(n, buf); |
|
a625e8876407
Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36242
diff
changeset
|
511 } |
|
a625e8876407
Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36242
diff
changeset
|
512 |
| 35742 | 513 /* If the LHS is a list or tuple, we need to set the assignment |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
56951
diff
changeset
|
514 context for all the contained elements. |
| 35742 | 515 */ |
| 516 if (s) { | |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
517 int i; |
| 35742 | 518 |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
519 for (i = 0; i < asdl_seq_LEN(s); i++) { |
|
46664
031dfe455585
Merged revisions 62425-62429,62434-62436,62441,62444,62446-62448,62450-62455,62463,62465-62466,62469,62474,62476-62478,62480,62485,62492,62497-62498,62500,62507,62513-62514,62516,62521,62531,62535,62545-62546,62548-62551,62553-62559,62569,62574,62577,62593,62595,62604-62606,62608,62616,62626-62627,62636,62638,62644-62645,62647-62648,62651-62653,62656,62661,62663,62680,62686-62687,62696,62699-62703,62711 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
46306
diff
changeset
|
520 if (!set_context(c, (expr_ty)asdl_seq_GET(s, i), ctx, n)) |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
521 return 0; |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
522 } |
| 35742 | 523 } |
| 524 return 1; | |
| 525 } | |
| 526 | |
| 527 static operator_ty | |
|
46664
031dfe455585
Merged revisions 62425-62429,62434-62436,62441,62444,62446-62448,62450-62455,62463,62465-62466,62469,62474,62476-62478,62480,62485,62492,62497-62498,62500,62507,62513-62514,62516,62521,62531,62535,62545-62546,62548-62551,62553-62559,62569,62574,62577,62593,62595,62604-62606,62608,62616,62626-62627,62636,62638,62644-62645,62647-62648,62651-62653,62656,62661,62663,62680,62686-62687,62696,62699-62703,62711 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
46306
diff
changeset
|
528 ast_for_augassign(struct compiling *c, const node *n) |
| 35742 | 529 { |
| 530 REQ(n, augassign); | |
| 531 n = CHILD(n, 0); | |
| 532 switch (STR(n)[0]) { | |
| 533 case '+': | |
| 534 return Add; | |
| 535 case '-': | |
| 536 return Sub; | |
| 537 case '/': | |
| 538 if (STR(n)[1] == '/') | |
| 539 return FloorDiv; | |
| 540 else | |
| 541 return Div; | |
| 542 case '%': | |
| 543 return Mod; | |
| 544 case '<': | |
| 545 return LShift; | |
| 546 case '>': | |
| 547 return RShift; | |
| 548 case '&': | |
| 549 return BitAnd; | |
| 550 case '^': | |
| 551 return BitXor; | |
| 552 case '|': | |
| 553 return BitOr; | |
| 554 case '*': | |
| 555 if (STR(n)[1] == '*') | |
| 556 return Pow; | |
| 557 else | |
| 558 return Mult; | |
| 559 default: | |
|
35870
f531c05cd240
Convert all internal errors from Exception to SystemError
Neal Norwitz <nnorwitz@gmail.com>
parents:
35868
diff
changeset
|
560 PyErr_Format(PyExc_SystemError, "invalid augassign: %s", STR(n)); |
|
37879
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36877
diff
changeset
|
561 return (operator_ty)0; |
| 35742 | 562 } |
| 563 } | |
| 564 | |
| 565 static cmpop_ty | |
|
46664
031dfe455585
Merged revisions 62425-62429,62434-62436,62441,62444,62446-62448,62450-62455,62463,62465-62466,62469,62474,62476-62478,62480,62485,62492,62497-62498,62500,62507,62513-62514,62516,62521,62531,62535,62545-62546,62548-62551,62553-62559,62569,62574,62577,62593,62595,62604-62606,62608,62616,62626-62627,62636,62638,62644-62645,62647-62648,62651-62653,62656,62661,62663,62680,62686-62687,62696,62699-62703,62711 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
46306
diff
changeset
|
566 ast_for_comp_op(struct compiling *c, const node *n) |
| 35742 | 567 { |
|
39648
efe3f2c0da5e
Killed the <> operator. You must now use !=.
Guido van Rossum <guido@python.org>
parents:
39610
diff
changeset
|
568 /* comp_op: '<'|'>'|'=='|'>='|'<='|'!='|'in'|'not' 'in'|'is' |
| 35742 | 569 |'is' 'not' |
| 570 */ | |
| 571 REQ(n, comp_op); | |
| 572 if (NCH(n) == 1) { | |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
573 n = CHILD(n, 0); |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
574 switch (TYPE(n)) { |
| 35742 | 575 case LESS: |
| 576 return Lt; | |
| 577 case GREATER: | |
| 578 return Gt; | |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
579 case EQEQUAL: /* == */ |
| 35742 | 580 return Eq; |
| 581 case LESSEQUAL: | |
| 582 return LtE; | |
| 583 case GREATEREQUAL: | |
| 584 return GtE; | |
| 585 case NOTEQUAL: | |
| 586 return NotEq; | |
| 587 case NAME: | |
| 588 if (strcmp(STR(n), "in") == 0) | |
| 589 return In; | |
| 590 if (strcmp(STR(n), "is") == 0) | |
| 591 return Is; | |
| 592 default: | |
|
35870
f531c05cd240
Convert all internal errors from Exception to SystemError
Neal Norwitz <nnorwitz@gmail.com>
parents:
35868
diff
changeset
|
593 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s", |
| 35742 | 594 STR(n)); |
|
37879
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36877
diff
changeset
|
595 return (cmpop_ty)0; |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
596 } |
| 35742 | 597 } |
| 598 else if (NCH(n) == 2) { | |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
599 /* handle "not in" and "is not" */ |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
600 switch (TYPE(CHILD(n, 0))) { |
| 35742 | 601 case NAME: |
| 602 if (strcmp(STR(CHILD(n, 1)), "in") == 0) | |
| 603 return NotIn; | |
| 604 if (strcmp(STR(CHILD(n, 0)), "is") == 0) | |
| 605 return IsNot; | |
| 606 default: | |
|
35870
f531c05cd240
Convert all internal errors from Exception to SystemError
Neal Norwitz <nnorwitz@gmail.com>
parents:
35868
diff
changeset
|
607 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s %s", |
| 35742 | 608 STR(CHILD(n, 0)), STR(CHILD(n, 1))); |
|
37879
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36877
diff
changeset
|
609 return (cmpop_ty)0; |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
610 } |
| 35742 | 611 } |
|
35870
f531c05cd240
Convert all internal errors from Exception to SystemError
Neal Norwitz <nnorwitz@gmail.com>
parents:
35868
diff
changeset
|
612 PyErr_Format(PyExc_SystemError, "invalid comp_op: has %d children", |
| 35742 | 613 NCH(n)); |
|
37879
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36877
diff
changeset
|
614 return (cmpop_ty)0; |
| 35742 | 615 } |
| 616 | |
| 617 static asdl_seq * | |
| 618 seq_for_testlist(struct compiling *c, const node *n) | |
| 619 { | |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
56951
diff
changeset
|
620 /* testlist: test (',' test)* [','] |
|
56237
7ee140b7ef93
fix an ambiguity in the grammar from the implementation of extended unpacking
Benjamin Peterson <benjamin@python.org>
parents:
55912
diff
changeset
|
621 testlist_star_expr: test|star_expr (',' test|star_expr)* [','] |
|
7ee140b7ef93
fix an ambiguity in the grammar from the implementation of extended unpacking
Benjamin Peterson <benjamin@python.org>
parents:
55912
diff
changeset
|
622 */ |
|
35759
d91ed7107fd2
ANSI-C-ify the placement of local var declarations.
Armin Rigo <arigo@tunes.org>
parents:
35748
diff
changeset
|
623 asdl_seq *seq; |
|
d91ed7107fd2
ANSI-C-ify the placement of local var declarations.
Armin Rigo <arigo@tunes.org>
parents:
35748
diff
changeset
|
624 expr_ty expression; |
|
d91ed7107fd2
ANSI-C-ify the placement of local var declarations.
Armin Rigo <arigo@tunes.org>
parents:
35748
diff
changeset
|
625 int i; |
|
56237
7ee140b7ef93
fix an ambiguity in the grammar from the implementation of extended unpacking
Benjamin Peterson <benjamin@python.org>
parents:
55912
diff
changeset
|
626 assert(TYPE(n) == testlist || TYPE(n) == testlist_star_expr || TYPE(n) == testlist_comp); |
| 35742 | 627 |
|
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
|
628 seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena); |
| 35742 | 629 if (!seq) |
| 630 return NULL; | |
| 631 | |
| 632 for (i = 0; i < NCH(n); i += 2) { | |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
56951
diff
changeset
|
633 const node *ch = CHILD(n, i); |
|
56237
7ee140b7ef93
fix an ambiguity in the grammar from the implementation of extended unpacking
Benjamin Peterson <benjamin@python.org>
parents:
55912
diff
changeset
|
634 assert(TYPE(ch) == test || TYPE(ch) == test_nocond || TYPE(ch) == star_expr); |
| 35742 | 635 |
|
56237
7ee140b7ef93
fix an ambiguity in the grammar from the implementation of extended unpacking
Benjamin Peterson <benjamin@python.org>
parents:
55912
diff
changeset
|
636 expression = ast_for_expr(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
|
637 if (!expression) |
| 35742 | 638 return NULL; |
| 639 | |
| 640 assert(i / 2 < seq->size); | |
| 641 asdl_seq_SET(seq, i / 2, expression); | |
| 642 } | |
| 643 return seq; | |
| 644 } | |
| 645 | |
|
40529
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
646 static arg_ty |
|
41717
9ad84070b22d
Merged revisions 55328-55341 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41716
diff
changeset
|
647 compiler_arg(struct compiling *c, const node *n) |
|
40529
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
648 { |
|
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
649 identifier name; |
|
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
650 expr_ty annotation = NULL; |
|
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
651 node *ch; |
|
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
652 |
|
41717
9ad84070b22d
Merged revisions 55328-55341 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41716
diff
changeset
|
653 assert(TYPE(n) == tfpdef || TYPE(n) == vfpdef); |
|
40529
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
654 ch = CHILD(n, 0); |
|
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
655 name = NEW_IDENTIFIER(ch); |
|
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
656 if (!name) |
|
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
657 return NULL; |
|
54954
c45e331238ae
In most cases, the parser will protect True, False, and None from being assign to.
Benjamin Peterson <benjamin@python.org>
parents:
54918
diff
changeset
|
658 if (forbidden_name(name, ch, 0)) |
|
c45e331238ae
In most cases, the parser will protect True, False, and None from being assign to.
Benjamin Peterson <benjamin@python.org>
parents:
54918
diff
changeset
|
659 return NULL; |
|
40529
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
660 |
|
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
661 if (NCH(n) == 3 && TYPE(CHILD(n, 1)) == COLON) { |
|
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
662 annotation = ast_for_expr(c, CHILD(n, 2)); |
|
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
663 if (!annotation) |
|
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
664 return NULL; |
|
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
665 } |
|
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
666 |
|
41717
9ad84070b22d
Merged revisions 55328-55341 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41716
diff
changeset
|
667 return arg(name, annotation, c->c_arena); |
|
46664
031dfe455585
Merged revisions 62425-62429,62434-62436,62441,62444,62446-62448,62450-62455,62463,62465-62466,62469,62474,62476-62478,62480,62485,62492,62497-62498,62500,62507,62513-62514,62516,62521,62531,62535,62545-62546,62548-62551,62553-62559,62569,62574,62577,62593,62595,62604-62606,62608,62616,62626-62627,62636,62638,62644-62645,62647-62648,62651-62653,62656,62661,62663,62680,62686-62687,62696,62699-62703,62711 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
46306
diff
changeset
|
668 #if 0 |
|
031dfe455585
Merged revisions 62425-62429,62434-62436,62441,62444,62446-62448,62450-62455,62463,62465-62466,62469,62474,62476-62478,62480,62485,62492,62497-62498,62500,62507,62513-62514,62516,62521,62531,62535,62545-62546,62548-62551,62553-62559,62569,62574,62577,62593,62595,62604-62606,62608,62616,62626-62627,62636,62638,62644-62645,62647-62648,62651-62653,62656,62661,62663,62680,62686-62687,62696,62699-62703,62711 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
46306
diff
changeset
|
669 result = Tuple(args, Store, LINENO(n), n->n_col_offset, c->c_arena); |
|
031dfe455585
Merged revisions 62425-62429,62434-62436,62441,62444,62446-62448,62450-62455,62463,62465-62466,62469,62474,62476-62478,62480,62485,62492,62497-62498,62500,62507,62513-62514,62516,62521,62531,62535,62545-62546,62548-62551,62553-62559,62569,62574,62577,62593,62595,62604-62606,62608,62616,62626-62627,62636,62638,62644-62645,62647-62648,62651-62653,62656,62661,62663,62680,62686-62687,62696,62699-62703,62711 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
46306
diff
changeset
|
670 if (!set_context(c, result, Store, n)) |
|
031dfe455585
Merged revisions 62425-62429,62434-62436,62441,62444,62446-62448,62450-62455,62463,62465-62466,62469,62474,62476-62478,62480,62485,62492,62497-62498,62500,62507,62513-62514,62516,62521,62531,62535,62545-62546,62548-62551,62553-62559,62569,62574,62577,62593,62595,62604-62606,62608,62616,62626-62627,62636,62638,62644-62645,62647-62648,62651-62653,62656,62661,62663,62680,62686-62687,62696,62699-62703,62711 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
46306
diff
changeset
|
671 return NULL; |
|
031dfe455585
Merged revisions 62425-62429,62434-62436,62441,62444,62446-62448,62450-62455,62463,62465-62466,62469,62474,62476-62478,62480,62485,62492,62497-62498,62500,62507,62513-62514,62516,62521,62531,62535,62545-62546,62548-62551,62553-62559,62569,62574,62577,62593,62595,62604-62606,62608,62616,62626-62627,62636,62638,62644-62645,62647-62648,62651-62653,62656,62661,62663,62680,62686-62687,62696,62699-62703,62711 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
46306
diff
changeset
|
672 return result; |
|
031dfe455585
Merged revisions 62425-62429,62434-62436,62441,62444,62446-62448,62450-62455,62463,62465-62466,62469,62474,62476-62478,62480,62485,62492,62497-62498,62500,62507,62513-62514,62516,62521,62531,62535,62545-62546,62548-62551,62553-62559,62569,62574,62577,62593,62595,62604-62606,62608,62616,62626-62627,62636,62638,62644-62645,62647-62648,62651-62653,62656,62661,62663,62680,62686-62687,62696,62699-62703,62711 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
46306
diff
changeset
|
673 #endif |
| 35742 | 674 } |
| 675 | |
|
40215
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
676 /* returns -1 if failed to handle keyword only arguments |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
677 returns new position to keep processing if successful |
|
41717
9ad84070b22d
Merged revisions 55328-55341 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41716
diff
changeset
|
678 (',' tfpdef ['=' test])* |
|
40215
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
679 ^^^ |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
680 start pointing here |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
681 */ |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
682 static int |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
683 handle_keywordonly_args(struct compiling *c, const node *n, int start, |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
684 asdl_seq *kwonlyargs, asdl_seq *kwdefaults) |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
685 { |
|
49852
30d01a5f517e
check the return value of NEW_IDENTIFIER in some more places
Benjamin Peterson <benjamin@python.org>
parents:
49847
diff
changeset
|
686 PyObject *argname; |
|
40215
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
687 node *ch; |
|
40529
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
688 expr_ty expression, annotation; |
|
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
689 arg_ty arg; |
|
40215
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
690 int i = start; |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
691 int j = 0; /* index for kwdefaults and kwonlyargs */ |
|
44246
aac33ee1a8fc
Issue #1573, second attempt:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
44237
diff
changeset
|
692 |
|
aac33ee1a8fc
Issue #1573, second attempt:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
44237
diff
changeset
|
693 if (kwonlyargs == NULL) { |
|
aac33ee1a8fc
Issue #1573, second attempt:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
44237
diff
changeset
|
694 ast_error(CHILD(n, start), "named arguments must follow bare *"); |
|
aac33ee1a8fc
Issue #1573, second attempt:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
44237
diff
changeset
|
695 return -1; |
|
aac33ee1a8fc
Issue #1573, second attempt:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
44237
diff
changeset
|
696 } |
|
aac33ee1a8fc
Issue #1573, second attempt:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
44237
diff
changeset
|
697 assert(kwdefaults != NULL); |
|
40215
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
698 while (i < NCH(n)) { |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
699 ch = CHILD(n, i); |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
700 switch (TYPE(ch)) { |
|
41717
9ad84070b22d
Merged revisions 55328-55341 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41716
diff
changeset
|
701 case vfpdef: |
|
9ad84070b22d
Merged revisions 55328-55341 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41716
diff
changeset
|
702 case tfpdef: |
|
40215
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
703 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) { |
|
40529
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
704 expression = ast_for_expr(c, CHILD(n, i + 2)); |
|
64178
5fd26f452600
Check the return values for all functions returning an ast node.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
62373
diff
changeset
|
705 if (!expression) |
|
64184
c18eeb969102
Add tests for r84209 (crashes in the Ast builder)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
64178
diff
changeset
|
706 goto error; |
|
40215
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
707 asdl_seq_SET(kwdefaults, j, expression); |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
708 i += 2; /* '=' and test */ |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
709 } |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
710 else { /* setting NULL if no default value exists */ |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
711 asdl_seq_SET(kwdefaults, j, NULL); |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
712 } |
|
40529
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
713 if (NCH(ch) == 3) { |
|
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
714 /* ch is NAME ':' test */ |
|
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
715 annotation = ast_for_expr(c, CHILD(ch, 2)); |
|
64178
5fd26f452600
Check the return values for all functions returning an ast node.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
62373
diff
changeset
|
716 if (!annotation) |
|
40529
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
717 goto error; |
|
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
718 } |
|
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
719 else { |
|
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
720 annotation = NULL; |
|
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
721 } |
|
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
722 ch = CHILD(ch, 0); |
|
49852
30d01a5f517e
check the return value of NEW_IDENTIFIER in some more places
Benjamin Peterson <benjamin@python.org>
parents:
49847
diff
changeset
|
723 argname = NEW_IDENTIFIER(ch); |
|
30d01a5f517e
check the return value of NEW_IDENTIFIER in some more places
Benjamin Peterson <benjamin@python.org>
parents:
49847
diff
changeset
|
724 if (!argname) |
|
40215
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
725 goto error; |
|
54954
c45e331238ae
In most cases, the parser will protect True, False, and None from being assign to.
Benjamin Peterson <benjamin@python.org>
parents:
54918
diff
changeset
|
726 if (forbidden_name(argname, ch, 0)) |
|
c45e331238ae
In most cases, the parser will protect True, False, and None from being assign to.
Benjamin Peterson <benjamin@python.org>
parents:
54918
diff
changeset
|
727 goto error; |
|
49852
30d01a5f517e
check the return value of NEW_IDENTIFIER in some more places
Benjamin Peterson <benjamin@python.org>
parents:
49847
diff
changeset
|
728 arg = arg(argname, annotation, c->c_arena); |
|
30d01a5f517e
check the return value of NEW_IDENTIFIER in some more places
Benjamin Peterson <benjamin@python.org>
parents:
49847
diff
changeset
|
729 if (!arg) |
|
30d01a5f517e
check the return value of NEW_IDENTIFIER in some more places
Benjamin Peterson <benjamin@python.org>
parents:
49847
diff
changeset
|
730 goto error; |
|
40529
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
731 asdl_seq_SET(kwonlyargs, j++, arg); |
|
40215
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
732 i += 2; /* the name and the comma */ |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
733 break; |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
734 case DOUBLESTAR: |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
735 return i; |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
736 default: |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
737 ast_error(ch, "unexpected node"); |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
738 goto error; |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
739 } |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
740 } |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
741 return i; |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
742 error: |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
56951
diff
changeset
|
743 return -1; |
|
40215
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
744 } |
| 35742 | 745 |
|
36718
45e6f901902c
Remove asdl_seq_APPEND() and simplify asdl seq implementation.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36712
diff
changeset
|
746 /* Create AST for argument list. */ |
| 35742 | 747 |
| 748 static arguments_ty | |
| 749 ast_for_arguments(struct compiling *c, const node *n) | |
| 750 { | |
|
40529
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
751 /* This function handles both typedargslist (function definition) |
|
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
752 and varargslist (lambda definition). |
|
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
753 |
|
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
754 parameters: '(' [typedargslist] ')' |
|
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
755 typedargslist: ((tfpdef ['=' test] ',')* |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
56951
diff
changeset
|
756 ('*' [tfpdef] (',' tfpdef ['=' test])* [',' '**' tfpdef] |
|
41717
9ad84070b22d
Merged revisions 55328-55341 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41716
diff
changeset
|
757 | '**' tfpdef) |
|
40529
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
758 | tfpdef ['=' test] (',' tfpdef ['=' test])* [',']) |
|
41717
9ad84070b22d
Merged revisions 55328-55341 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41716
diff
changeset
|
759 tfpdef: NAME [':' test] |
|
40529
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
760 varargslist: ((vfpdef ['=' test] ',')* |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
56951
diff
changeset
|
761 ('*' [vfpdef] (',' vfpdef ['=' test])* [',' '**' vfpdef] |
|
41717
9ad84070b22d
Merged revisions 55328-55341 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41716
diff
changeset
|
762 | '**' vfpdef) |
|
40529
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
763 | vfpdef ['=' test] (',' vfpdef ['=' test])* [',']) |
|
41717
9ad84070b22d
Merged revisions 55328-55341 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41716
diff
changeset
|
764 vfpdef: NAME |
| 35742 | 765 */ |
|
40215
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
766 int i, j, k, nposargs = 0, nkwonlyargs = 0; |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
767 int nposdefaults = 0, found_default = 0; |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
768 asdl_seq *posargs, *posdefaults, *kwonlyargs, *kwdefaults; |
| 35742 | 769 identifier vararg = NULL, kwarg = NULL; |
|
40529
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
770 arg_ty arg; |
|
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
771 expr_ty varargannotation = NULL, kwargannotation = NULL; |
| 35742 | 772 node *ch; |
| 773 | |
| 774 if (TYPE(n) == parameters) { | |
|
40215
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
775 if (NCH(n) == 2) /* () as argument list */ |
|
40529
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
776 return arguments(NULL, NULL, NULL, NULL, NULL, NULL, NULL, |
|
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
777 NULL, c->c_arena); |
|
40215
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
778 n = CHILD(n, 1); |
| 35742 | 779 } |
|
40529
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
780 assert(TYPE(n) == typedargslist || TYPE(n) == varargslist); |
| 35742 | 781 |
|
48515
dbc2b7ad0c22
Fix uninitialized memory read for cases like def(f, *): pass
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
48397
diff
changeset
|
782 /* First count the number of positional args & defaults. The |
|
dbc2b7ad0c22
Fix uninitialized memory read for cases like def(f, *): pass
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
48397
diff
changeset
|
783 variable i is the loop index for this for loop and the next. |
|
dbc2b7ad0c22
Fix uninitialized memory read for cases like def(f, *): pass
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
48397
diff
changeset
|
784 The next loop picks up where the first leaves off. |
|
dbc2b7ad0c22
Fix uninitialized memory read for cases like def(f, *): pass
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
48397
diff
changeset
|
785 */ |
| 35742 | 786 for (i = 0; i < NCH(n); i++) { |
|
40215
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
787 ch = CHILD(n, i); |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
788 if (TYPE(ch) == STAR) { |
|
48515
dbc2b7ad0c22
Fix uninitialized memory read for cases like def(f, *): pass
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
48397
diff
changeset
|
789 /* skip star */ |
|
41717
9ad84070b22d
Merged revisions 55328-55341 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41716
diff
changeset
|
790 i++; |
|
48515
dbc2b7ad0c22
Fix uninitialized memory read for cases like def(f, *): pass
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
48397
diff
changeset
|
791 if (i < NCH(n) && /* skip argument following star */ |
|
dbc2b7ad0c22
Fix uninitialized memory read for cases like def(f, *): pass
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
48397
diff
changeset
|
792 (TYPE(CHILD(n, i)) == tfpdef || |
|
dbc2b7ad0c22
Fix uninitialized memory read for cases like def(f, *): pass
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
48397
diff
changeset
|
793 TYPE(CHILD(n, i)) == vfpdef)) { |
|
dbc2b7ad0c22
Fix uninitialized memory read for cases like def(f, *): pass
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
48397
diff
changeset
|
794 i++; |
|
dbc2b7ad0c22
Fix uninitialized memory read for cases like def(f, *): pass
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
48397
diff
changeset
|
795 } |
|
dbc2b7ad0c22
Fix uninitialized memory read for cases like def(f, *): pass
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
48397
diff
changeset
|
796 break; |
|
40215
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
797 } |
|
41717
9ad84070b22d
Merged revisions 55328-55341 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41716
diff
changeset
|
798 if (TYPE(ch) == DOUBLESTAR) break; |
|
40529
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
799 if (TYPE(ch) == vfpdef || TYPE(ch) == tfpdef) nposargs++; |
|
40215
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
800 if (TYPE(ch) == EQUAL) nposdefaults++; |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
801 } |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
56951
diff
changeset
|
802 /* count the number of keyword only args & |
|
40215
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
803 defaults for keyword only args */ |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
804 for ( ; i < NCH(n); ++i) { |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
805 ch = CHILD(n, i); |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
806 if (TYPE(ch) == DOUBLESTAR) break; |
|
41717
9ad84070b22d
Merged revisions 55328-55341 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41716
diff
changeset
|
807 if (TYPE(ch) == tfpdef || TYPE(ch) == vfpdef) nkwonlyargs++; |
| 35742 | 808 } |
|
40215
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
809 posargs = (nposargs ? asdl_seq_new(nposargs, c->c_arena) : NULL); |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
810 if (!posargs && nposargs) |
|
64178
5fd26f452600
Check the return values for all functions returning an ast node.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
62373
diff
changeset
|
811 return NULL; |
|
40215
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
812 kwonlyargs = (nkwonlyargs ? |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
813 asdl_seq_new(nkwonlyargs, c->c_arena) : NULL); |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
814 if (!kwonlyargs && nkwonlyargs) |
|
64178
5fd26f452600
Check the return values for all functions returning an ast node.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
62373
diff
changeset
|
815 return NULL; |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
56951
diff
changeset
|
816 posdefaults = (nposdefaults ? |
|
40215
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
817 asdl_seq_new(nposdefaults, c->c_arena) : NULL); |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
818 if (!posdefaults && nposdefaults) |
|
64178
5fd26f452600
Check the return values for all functions returning an ast node.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
62373
diff
changeset
|
819 return NULL; |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
56951
diff
changeset
|
820 /* The length of kwonlyargs and kwdefaults are same |
|
40215
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
821 since we set NULL as default for keyword only argument w/o default |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
822 - we have sequence data structure, but no dictionary */ |
|
40529
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
823 kwdefaults = (nkwonlyargs ? |
|
40215
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
824 asdl_seq_new(nkwonlyargs, c->c_arena) : NULL); |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
825 if (!kwdefaults && nkwonlyargs) |
|
64178
5fd26f452600
Check the return values for all functions returning an ast node.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
62373
diff
changeset
|
826 return NULL; |
|
40215
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
827 |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
828 if (nposargs + nkwonlyargs > 255) { |
|
40529
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
829 ast_error(n, "more than 255 arguments"); |
|
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
830 return NULL; |
|
40215
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
831 } |
| 35742 | 832 |
|
41717
9ad84070b22d
Merged revisions 55328-55341 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41716
diff
changeset
|
833 /* tfpdef: NAME [':' test] |
|
9ad84070b22d
Merged revisions 55328-55341 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41716
diff
changeset
|
834 vfpdef: NAME |
| 35742 | 835 */ |
| 836 i = 0; | |
|
36718
45e6f901902c
Remove asdl_seq_APPEND() and simplify asdl seq implementation.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36712
diff
changeset
|
837 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
|
838 k = 0; /* index for args */ |
| 35742 | 839 while (i < NCH(n)) { |
|
40215
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
840 ch = CHILD(n, i); |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
841 switch (TYPE(ch)) { |
|
40529
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
842 case tfpdef: |
|
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
843 case vfpdef: |
| 35742 | 844 /* XXX Need to worry about checking if TYPE(CHILD(n, i+1)) is |
| 845 anything other than EQUAL or a comma? */ | |
| 846 /* XXX Should NCH(n) check be made a separate check? */ | |
| 847 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) { | |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
848 expr_ty expression = ast_for_expr(c, CHILD(n, i + 2)); |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
849 if (!expression) |
|
64178
5fd26f452600
Check the return values for all functions returning an ast node.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
62373
diff
changeset
|
850 return NULL; |
|
40215
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
851 assert(posdefaults != NULL); |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
852 asdl_seq_SET(posdefaults, j++, expression); |
| 35742 | 853 i += 2; |
|
40215
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
854 found_default = 1; |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
855 } |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
856 else if (found_default) { |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
56951
diff
changeset
|
857 ast_error(n, |
|
40215
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
858 "non-default argument follows default argument"); |
|
64178
5fd26f452600
Check the return values for all functions returning an ast node.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
62373
diff
changeset
|
859 return NULL; |
| 35742 | 860 } |
|
41717
9ad84070b22d
Merged revisions 55328-55341 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41716
diff
changeset
|
861 arg = compiler_arg(c, ch); |
|
40529
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
862 if (!arg) |
|
64178
5fd26f452600
Check the return values for all functions returning an ast node.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
62373
diff
changeset
|
863 return NULL; |
|
40529
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
864 asdl_seq_SET(posargs, k++, arg); |
| 35742 | 865 i += 2; /* the name and the comma */ |
| 866 break; | |
| 867 case STAR: | |
|
40215
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
868 if (i+1 >= NCH(n)) { |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
56951
diff
changeset
|
869 ast_error(CHILD(n, i), |
|
44246
aac33ee1a8fc
Issue #1573, second attempt:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
44237
diff
changeset
|
870 "named arguments must follow bare *"); |
|
64178
5fd26f452600
Check the return values for all functions returning an ast node.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
62373
diff
changeset
|
871 return NULL; |
|
40215
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
872 } |
|
41717
9ad84070b22d
Merged revisions 55328-55341 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41716
diff
changeset
|
873 ch = CHILD(n, i+1); /* tfpdef or COMMA */ |
|
40529
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
874 if (TYPE(ch) == COMMA) { |
|
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
875 int res = 0; |
|
40215
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
876 i += 2; /* now follows keyword only arguments */ |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
877 res = handle_keywordonly_args(c, n, i, |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
878 kwonlyargs, kwdefaults); |
|
64178
5fd26f452600
Check the return values for all functions returning an ast node.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
62373
diff
changeset
|
879 if (res == -1) return NULL; |
|
40215
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
880 i = res; /* res has new position to process */ |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
881 } |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
882 else { |
|
40529
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
883 vararg = NEW_IDENTIFIER(CHILD(ch, 0)); |
|
49844
40b7277e665f
Merged revisions 67373 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
49797
diff
changeset
|
884 if (!vararg) |
|
40b7277e665f
Merged revisions 67373 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
49797
diff
changeset
|
885 return NULL; |
|
54954
c45e331238ae
In most cases, the parser will protect True, False, and None from being assign to.
Benjamin Peterson <benjamin@python.org>
parents:
54918
diff
changeset
|
886 if (forbidden_name(vararg, CHILD(ch, 0), 0)) |
|
c45e331238ae
In most cases, the parser will protect True, False, and None from being assign to.
Benjamin Peterson <benjamin@python.org>
parents:
54918
diff
changeset
|
887 return NULL; |
|
40529
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
888 if (NCH(ch) > 1) { |
|
41717
9ad84070b22d
Merged revisions 55328-55341 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41716
diff
changeset
|
889 /* there is an annotation on the vararg */ |
|
9ad84070b22d
Merged revisions 55328-55341 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41716
diff
changeset
|
890 varargannotation = ast_for_expr(c, CHILD(ch, 2)); |
|
64178
5fd26f452600
Check the return values for all functions returning an ast node.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
62373
diff
changeset
|
891 if (!varargannotation) |
|
5fd26f452600
Check the return values for all functions returning an ast node.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
62373
diff
changeset
|
892 return NULL; |
|
40529
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
893 } |
|
40215
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
894 i += 3; |
|
41717
9ad84070b22d
Merged revisions 55328-55341 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41716
diff
changeset
|
895 if (i < NCH(n) && (TYPE(CHILD(n, i)) == tfpdef |
|
9ad84070b22d
Merged revisions 55328-55341 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41716
diff
changeset
|
896 || TYPE(CHILD(n, i)) == vfpdef)) { |
|
40215
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
897 int res = 0; |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
898 res = handle_keywordonly_args(c, n, i, |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
899 kwonlyargs, kwdefaults); |
|
64178
5fd26f452600
Check the return values for all functions returning an ast node.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
62373
diff
changeset
|
900 if (res == -1) return NULL; |
|
40215
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
901 i = res; /* res has new position to process */ |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
902 } |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
903 } |
| 35742 | 904 break; |
| 905 case DOUBLESTAR: | |
|
41717
9ad84070b22d
Merged revisions 55328-55341 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41716
diff
changeset
|
906 ch = CHILD(n, i+1); /* tfpdef */ |
|
9ad84070b22d
Merged revisions 55328-55341 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41716
diff
changeset
|
907 assert(TYPE(ch) == tfpdef || TYPE(ch) == vfpdef); |
|
40529
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
908 kwarg = NEW_IDENTIFIER(CHILD(ch, 0)); |
|
64184
c18eeb969102
Add tests for r84209 (crashes in the Ast builder)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
64178
diff
changeset
|
909 if (!kwarg) |
|
c18eeb969102
Add tests for r84209 (crashes in the Ast builder)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
64178
diff
changeset
|
910 return NULL; |
|
40529
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
911 if (NCH(ch) > 1) { |
|
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
912 /* there is an annotation on the kwarg */ |
|
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
913 kwargannotation = ast_for_expr(c, CHILD(ch, 2)); |
|
64178
5fd26f452600
Check the return values for all functions returning an ast node.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
62373
diff
changeset
|
914 if (!kwargannotation) |
|
5fd26f452600
Check the return values for all functions returning an ast node.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
62373
diff
changeset
|
915 return NULL; |
|
40529
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
916 } |
|
54954
c45e331238ae
In most cases, the parser will protect True, False, and None from being assign to.
Benjamin Peterson <benjamin@python.org>
parents:
54918
diff
changeset
|
917 if (forbidden_name(kwarg, CHILD(ch, 0), 0)) |
|
64178
5fd26f452600
Check the return values for all functions returning an ast node.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
62373
diff
changeset
|
918 return NULL; |
| 35742 | 919 i += 3; |
| 920 break; | |
| 921 default: | |
|
35870
f531c05cd240
Convert all internal errors from Exception to SystemError
Neal Norwitz <nnorwitz@gmail.com>
parents:
35868
diff
changeset
|
922 PyErr_Format(PyExc_SystemError, |
| 35742 | 923 "unexpected node in varargslist: %d @ %d", |
| 924 TYPE(ch), i); | |
|
64178
5fd26f452600
Check the return values for all functions returning an ast node.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
62373
diff
changeset
|
925 return NULL; |
|
40215
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
926 } |
| 35742 | 927 } |
|
40529
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
928 return arguments(posargs, vararg, varargannotation, kwonlyargs, kwarg, |
|
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
929 kwargannotation, posdefaults, kwdefaults, c->c_arena); |
| 35742 | 930 } |
| 931 | |
| 932 static expr_ty | |
| 933 ast_for_dotted_name(struct compiling *c, const node *n) | |
| 934 { | |
|
36072
de16a85c3b4c
Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents:
36066
diff
changeset
|
935 expr_ty e; |
|
de16a85c3b4c
Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents:
36066
diff
changeset
|
936 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
|
937 int lineno, col_offset; |
| 35742 | 938 int i; |
| 939 | |
| 940 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
|
941 |
|
57322cb2ca11
Patch #1440601: Add col_offset attribute to AST nodes.
Martin v. Löwis <martin@v.loewis.de>
parents:
36780
diff
changeset
|
942 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
|
943 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
|
944 |
| 35742 | 945 id = NEW_IDENTIFIER(CHILD(n, 0)); |
| 946 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
|
947 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
|
948 e = Name(id, Load, lineno, col_offset, c->c_arena); |
| 35742 | 949 if (!e) |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
950 return NULL; |
| 35742 | 951 |
| 952 for (i = 2; i < NCH(n); i+=2) { | |
| 953 id = NEW_IDENTIFIER(CHILD(n, i)); | |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
954 if (!id) |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
955 return NULL; |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
956 e = Attribute(e, id, Load, lineno, col_offset, c->c_arena); |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
957 if (!e) |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
958 return NULL; |
| 35742 | 959 } |
| 960 | |
| 961 return e; | |
| 962 } | |
| 963 | |
| 964 static expr_ty | |
| 965 ast_for_decorator(struct compiling *c, const node *n) | |
| 966 { | |
| 967 /* decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE */ | |
| 968 expr_ty d = NULL; | |
|
36072
de16a85c3b4c
Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents:
36066
diff
changeset
|
969 expr_ty name_expr; |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
56951
diff
changeset
|
970 |
| 35742 | 971 REQ(n, decorator); |
|
36418
a625e8876407
Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36242
diff
changeset
|
972 REQ(CHILD(n, 0), AT); |
|
a625e8876407
Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36242
diff
changeset
|
973 REQ(RCHILD(n, -1), NEWLINE); |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
56951
diff
changeset
|
974 |
| 35742 | 975 name_expr = ast_for_dotted_name(c, CHILD(n, 1)); |
| 976 if (!name_expr) | |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
977 return NULL; |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
56951
diff
changeset
|
978 |
| 35742 | 979 if (NCH(n) == 3) { /* No arguments */ |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
980 d = name_expr; |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
981 name_expr = NULL; |
| 35742 | 982 } |
| 983 else if (NCH(n) == 5) { /* Call with no arguments */ | |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
984 d = Call(name_expr, NULL, NULL, NULL, NULL, LINENO(n), |
|
37879
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36877
diff
changeset
|
985 n->n_col_offset, c->c_arena); |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
986 if (!d) |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
987 return NULL; |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
988 name_expr = NULL; |
| 35742 | 989 } |
| 990 else { | |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
991 d = ast_for_call(c, CHILD(n, 3), name_expr); |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
992 if (!d) |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
993 return NULL; |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
994 name_expr = NULL; |
| 35742 | 995 } |
| 996 | |
| 997 return d; | |
| 998 } | |
| 999 | |
| 1000 static asdl_seq* | |
| 1001 ast_for_decorators(struct compiling *c, const node *n) | |
| 1002 { | |
|
36072
de16a85c3b4c
Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents:
36066
diff
changeset
|
1003 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
|
1004 expr_ty d; |
| 35742 | 1005 int i; |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
56951
diff
changeset
|
1006 |
| 35742 | 1007 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
|
1008 decorator_seq = asdl_seq_new(NCH(n), c->c_arena); |
| 35742 | 1009 if (!decorator_seq) |
| 1010 return NULL; | |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
56951
diff
changeset
|
1011 |
| 35742 | 1012 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
|
1013 d = ast_for_decorator(c, CHILD(n, i)); |
|
64178
5fd26f452600
Check the return values for all functions returning an ast node.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
62373
diff
changeset
|
1014 if (!d) |
|
5fd26f452600
Check the return values for all functions returning an ast node.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
62373
diff
changeset
|
1015 return NULL; |
|
5fd26f452600
Check the return values for all functions returning an ast node.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
62373
diff
changeset
|
1016 asdl_seq_SET(decorator_seq, i, d); |
| 35742 | 1017 } |
| 1018 return decorator_seq; | |
| 1019 } | |
| 1020 | |
| 1021 static stmt_ty | |
|
41824
24b60622b944
Merged revisions 55407-55513 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41717
diff
changeset
|
1022 ast_for_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq) |
| 35742 | 1023 { |
|
41824
24b60622b944
Merged revisions 55407-55513 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41717
diff
changeset
|
1024 /* funcdef: 'def' NAME parameters ['->' test] ':' suite */ |
|
36072
de16a85c3b4c
Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents:
36066
diff
changeset
|
1025 identifier name; |
|
de16a85c3b4c
Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents:
36066
diff
changeset
|
1026 arguments_ty args; |
|
de16a85c3b4c
Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents:
36066
diff
changeset
|
1027 asdl_seq *body; |
|
40529
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
1028 expr_ty returns = NULL; |
|
41824
24b60622b944
Merged revisions 55407-55513 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41717
diff
changeset
|
1029 int name_i = 1; |
| 35742 | 1030 |
| 1031 REQ(n, funcdef); | |
| 1032 | |
| 1033 name = NEW_IDENTIFIER(CHILD(n, name_i)); | |
| 1034 if (!name) | |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1035 return NULL; |
|
54954
c45e331238ae
In most cases, the parser will protect True, False, and None from being assign to.
Benjamin Peterson <benjamin@python.org>
parents:
54918
diff
changeset
|
1036 if (forbidden_name(name, CHILD(n, name_i), 0)) |
|
c45e331238ae
In most cases, the parser will protect True, False, and None from being assign to.
Benjamin Peterson <benjamin@python.org>
parents:
54918
diff
changeset
|
1037 return NULL; |
| 35742 | 1038 args = ast_for_arguments(c, CHILD(n, name_i + 1)); |
| 1039 if (!args) | |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1040 return NULL; |
|
40529
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
1041 if (TYPE(CHILD(n, name_i+2)) == RARROW) { |
|
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
1042 returns = ast_for_expr(c, CHILD(n, name_i + 3)); |
|
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
1043 if (!returns) |
|
64178
5fd26f452600
Check the return values for all functions returning an ast node.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
62373
diff
changeset
|
1044 return NULL; |
|
40529
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
1045 name_i += 2; |
|
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
1046 } |
| 35742 | 1047 body = ast_for_suite(c, CHILD(n, name_i + 3)); |
| 1048 if (!body) | |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1049 return NULL; |
| 35742 | 1050 |
|
40529
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
1051 return FunctionDef(name, args, body, decorator_seq, returns, LINENO(n), |
|
37879
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36877
diff
changeset
|
1052 n->n_col_offset, c->c_arena); |
| 35742 | 1053 } |
| 1054 | |
|
41824
24b60622b944
Merged revisions 55407-55513 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41717
diff
changeset
|
1055 static stmt_ty |
|
24b60622b944
Merged revisions 55407-55513 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41717
diff
changeset
|
1056 ast_for_decorated(struct compiling *c, const node *n) |
|
24b60622b944
Merged revisions 55407-55513 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41717
diff
changeset
|
1057 { |
|
24b60622b944
Merged revisions 55407-55513 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41717
diff
changeset
|
1058 /* decorated: decorators (classdef | funcdef) */ |
|
24b60622b944
Merged revisions 55407-55513 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41717
diff
changeset
|
1059 stmt_ty thing = NULL; |
|
24b60622b944
Merged revisions 55407-55513 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41717
diff
changeset
|
1060 asdl_seq *decorator_seq = NULL; |
|
24b60622b944
Merged revisions 55407-55513 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41717
diff
changeset
|
1061 |
|
24b60622b944
Merged revisions 55407-55513 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41717
diff
changeset
|
1062 REQ(n, decorated); |
|
24b60622b944
Merged revisions 55407-55513 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41717
diff
changeset
|
1063 |
|
24b60622b944
Merged revisions 55407-55513 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41717
diff
changeset
|
1064 decorator_seq = ast_for_decorators(c, CHILD(n, 0)); |
|
24b60622b944
Merged revisions 55407-55513 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41717
diff
changeset
|
1065 if (!decorator_seq) |
|
24b60622b944
Merged revisions 55407-55513 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41717
diff
changeset
|
1066 return NULL; |
|
24b60622b944
Merged revisions 55407-55513 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41717
diff
changeset
|
1067 |
|
24b60622b944
Merged revisions 55407-55513 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41717
diff
changeset
|
1068 assert(TYPE(CHILD(n, 1)) == funcdef || |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
56951
diff
changeset
|
1069 TYPE(CHILD(n, 1)) == classdef); |
|
41824
24b60622b944
Merged revisions 55407-55513 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41717
diff
changeset
|
1070 |
|
24b60622b944
Merged revisions 55407-55513 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41717
diff
changeset
|
1071 if (TYPE(CHILD(n, 1)) == funcdef) { |
|
24b60622b944
Merged revisions 55407-55513 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41717
diff
changeset
|
1072 thing = ast_for_funcdef(c, CHILD(n, 1), decorator_seq); |
|
24b60622b944
Merged revisions 55407-55513 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41717
diff
changeset
|
1073 } else if (TYPE(CHILD(n, 1)) == classdef) { |
|
24b60622b944
Merged revisions 55407-55513 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41717
diff
changeset
|
1074 thing = ast_for_classdef(c, CHILD(n, 1), decorator_seq); |
|
24b60622b944
Merged revisions 55407-55513 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41717
diff
changeset
|
1075 } |
|
45448
f1b50aa5d5af
Patch from Georg Brandl: Fix co_lineno of decorated function and class objects. If you see an error in test_inspect please delete all pyc files.
Christian Heimes <christian@cheimes.de>
parents:
44442
diff
changeset
|
1076 /* we count the decorators in when talking about the class' or |
|
f1b50aa5d5af
Patch from Georg Brandl: Fix co_lineno of decorated function and class objects. If you see an error in test_inspect please delete all pyc files.
Christian Heimes <christian@cheimes.de>
parents:
44442
diff
changeset
|
1077 * function's line number */ |
|
f1b50aa5d5af
Patch from Georg Brandl: Fix co_lineno of decorated function and class objects. If you see an error in test_inspect please delete all pyc files.
Christian Heimes <christian@cheimes.de>
parents:
44442
diff
changeset
|
1078 if (thing) { |
|
f1b50aa5d5af
Patch from Georg Brandl: Fix co_lineno of decorated function and class objects. If you see an error in test_inspect please delete all pyc files.
Christian Heimes <christian@cheimes.de>
parents:
44442
diff
changeset
|
1079 thing->lineno = LINENO(n); |
|
f1b50aa5d5af
Patch from Georg Brandl: Fix co_lineno of decorated function and class objects. If you see an error in test_inspect please delete all pyc files.
Christian Heimes <christian@cheimes.de>
parents:
44442
diff
changeset
|
1080 thing->col_offset = n->n_col_offset; |
|
f1b50aa5d5af
Patch from Georg Brandl: Fix co_lineno of decorated function and class objects. If you see an error in test_inspect please delete all pyc files.
Christian Heimes <christian@cheimes.de>
parents:
44442
diff
changeset
|
1081 } |
|
41824
24b60622b944
Merged revisions 55407-55513 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41717
diff
changeset
|
1082 return thing; |
|
24b60622b944
Merged revisions 55407-55513 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41717
diff
changeset
|
1083 } |
|
24b60622b944
Merged revisions 55407-55513 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41717
diff
changeset
|
1084 |
| 35742 | 1085 static expr_ty |
| 1086 ast_for_lambdef(struct compiling *c, const node *n) | |
| 1087 { | |
|
41464
060c79f6d919
Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents:
41235
diff
changeset
|
1088 /* lambdef: 'lambda' [varargslist] ':' test |
|
060c79f6d919
Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents:
41235
diff
changeset
|
1089 lambdef_nocond: 'lambda' [varargslist] ':' test_nocond */ |
| 35742 | 1090 arguments_ty args; |
| 1091 expr_ty expression; | |
| 1092 | |
| 1093 if (NCH(n) == 3) { | |
|
40529
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
1094 args = arguments(NULL, NULL, NULL, NULL, NULL, NULL, NULL, |
|
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
1095 NULL, c->c_arena); |
| 35742 | 1096 if (!args) |
| 1097 return NULL; | |
| 1098 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
|
1099 if (!expression) |
| 35742 | 1100 return NULL; |
| 1101 } | |
| 1102 else { | |
| 1103 args = ast_for_arguments(c, CHILD(n, 1)); | |
| 1104 if (!args) | |
| 1105 return NULL; | |
| 1106 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
|
1107 if (!expression) |
| 35742 | 1108 return NULL; |
| 1109 } | |
| 1110 | |
|
36786
57322cb2ca11
Patch #1440601: Add col_offset attribute to AST nodes.
Martin v. Löwis <martin@v.loewis.de>
parents:
36780
diff
changeset
|
1111 return Lambda(args, expression, LINENO(n), n->n_col_offset, c->c_arena); |
| 35742 | 1112 } |
| 1113 | |
|
36666
6165927431d6
PEP 308 implementation, including minor refdocs and some testcases. It
Thomas Wouters <thomas@python.org>
parents:
36444
diff
changeset
|
1114 static expr_ty |
|
6165927431d6
PEP 308 implementation, including minor refdocs and some testcases. It
Thomas Wouters <thomas@python.org>
parents:
36444
diff
changeset
|
1115 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
|
1116 { |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
56951
diff
changeset
|
1117 /* 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
|
1118 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
|
1119 |
|
36676
67c74efad8c2
Fix old not-reading-pep-308-right artifact.
Thomas Wouters <thomas@python.org>
parents:
36670
diff
changeset
|
1120 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
|
1121 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
|
1122 if (!body) |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1123 return NULL; |
|
36666
6165927431d6
PEP 308 implementation, including minor refdocs and some testcases. It
Thomas Wouters <thomas@python.org>
parents:
36444
diff
changeset
|
1124 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
|
1125 if (!expression) |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1126 return NULL; |
|
36666
6165927431d6
PEP 308 implementation, including minor refdocs and some testcases. It
Thomas Wouters <thomas@python.org>
parents:
36444
diff
changeset
|
1127 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
|
1128 if (!orelse) |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1129 return NULL; |
|
37879
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36877
diff
changeset
|
1130 return IfExp(expression, body, orelse, LINENO(n), n->n_col_offset, |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36877
diff
changeset
|
1131 c->c_arena); |
|
36666
6165927431d6
PEP 308 implementation, including minor refdocs and some testcases. It
Thomas Wouters <thomas@python.org>
parents:
36444
diff
changeset
|
1132 } |
|
6165927431d6
PEP 308 implementation, including minor refdocs and some testcases. It
Thomas Wouters <thomas@python.org>
parents:
36444
diff
changeset
|
1133 |
|
41464
060c79f6d919
Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents:
41235
diff
changeset
|
1134 /* |
|
060c79f6d919
Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents:
41235
diff
changeset
|
1135 Count the number of 'for' loops in a comprehension. |
| 35742 | 1136 |
|
41464
060c79f6d919
Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents:
41235
diff
changeset
|
1137 Helper for ast_for_comprehension(). |
| 35742 | 1138 */ |
| 1139 | |
| 1140 static int | |
|
46664
031dfe455585
Merged revisions 62425-62429,62434-62436,62441,62444,62446-62448,62450-62455,62463,62465-62466,62469,62474,62476-62478,62480,62485,62492,62497-62498,62500,62507,62513-62514,62516,62521,62531,62535,62545-62546,62548-62551,62553-62559,62569,62574,62577,62593,62595,62604-62606,62608,62616,62626-62627,62636,62638,62644-62645,62647-62648,62651-62653,62656,62661,62663,62680,62686-62687,62696,62699-62703,62711 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
46306
diff
changeset
|
1141 count_comp_fors(struct compiling *c, const node *n) |
| 35742 | 1142 { |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
1143 int n_fors = 0; |
| 35742 | 1144 |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
1145 count_comp_for: |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
1146 n_fors++; |
|
42193
090588a17490
Merged revisions 56154-56264 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42084
diff
changeset
|
1147 REQ(n, comp_for); |
|
090588a17490
Merged revisions 56154-56264 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42084
diff
changeset
|
1148 if (NCH(n) == 5) |
|
090588a17490
Merged revisions 56154-56264 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42084
diff
changeset
|
1149 n = CHILD(n, 4); |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
1150 else |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
1151 return n_fors; |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
1152 count_comp_iter: |
|
42193
090588a17490
Merged revisions 56154-56264 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42084
diff
changeset
|
1153 REQ(n, comp_iter); |
|
090588a17490
Merged revisions 56154-56264 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42084
diff
changeset
|
1154 n = CHILD(n, 0); |
|
090588a17490
Merged revisions 56154-56264 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42084
diff
changeset
|
1155 if (TYPE(n) == comp_for) |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
1156 goto count_comp_for; |
|
42193
090588a17490
Merged revisions 56154-56264 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42084
diff
changeset
|
1157 else if (TYPE(n) == comp_if) { |
|
090588a17490
Merged revisions 56154-56264 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42084
diff
changeset
|
1158 if (NCH(n) == 3) { |
|
090588a17490
Merged revisions 56154-56264 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42084
diff
changeset
|
1159 n = CHILD(n, 2); |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
1160 goto count_comp_iter; |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
1161 } |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1162 else |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
1163 return n_fors; |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
1164 } |
|
36072
de16a85c3b4c
Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents:
36066
diff
changeset
|
1165 |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
1166 /* Should never be reached */ |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
1167 PyErr_SetString(PyExc_SystemError, |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
1168 "logic error in count_comp_fors"); |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
1169 return -1; |
| 35742 | 1170 } |
| 1171 | |
|
41464
060c79f6d919
Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents:
41235
diff
changeset
|
1172 /* Count the number of 'if' statements in a comprehension. |
| 35742 | 1173 |
|
41464
060c79f6d919
Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents:
41235
diff
changeset
|
1174 Helper for ast_for_comprehension(). |
| 35742 | 1175 */ |
| 1176 | |
| 1177 static int | |
|
46664
031dfe455585
Merged revisions 62425-62429,62434-62436,62441,62444,62446-62448,62450-62455,62463,62465-62466,62469,62474,62476-62478,62480,62485,62492,62497-62498,62500,62507,62513-62514,62516,62521,62531,62535,62545-62546,62548-62551,62553-62559,62569,62574,62577,62593,62595,62604-62606,62608,62616,62626-62627,62636,62638,62644-62645,62647-62648,62651-62653,62656,62661,62663,62680,62686-62687,62696,62699-62703,62711 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
46306
diff
changeset
|
1178 count_comp_ifs(struct compiling *c, const node *n) |
| 35742 | 1179 { |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
1180 int n_ifs = 0; |
| 35742 | 1181 |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
1182 while (1) { |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
1183 REQ(n, comp_iter); |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
1184 if (TYPE(CHILD(n, 0)) == comp_for) |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
1185 return n_ifs; |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
1186 n = CHILD(n, 0); |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
1187 REQ(n, comp_if); |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
1188 n_ifs++; |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
1189 if (NCH(n) == 2) |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
1190 return n_ifs; |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
1191 n = CHILD(n, 2); |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
1192 } |
| 35742 | 1193 } |
| 1194 | |
|
42193
090588a17490
Merged revisions 56154-56264 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42084
diff
changeset
|
1195 static asdl_seq * |
|
090588a17490
Merged revisions 56154-56264 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42084
diff
changeset
|
1196 ast_for_comprehension(struct compiling *c, const node *n) |
| 35742 | 1197 { |
| 1198 int i, n_fors; | |
|
42193
090588a17490
Merged revisions 56154-56264 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42084
diff
changeset
|
1199 asdl_seq *comps; |
|
090588a17490
Merged revisions 56154-56264 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42084
diff
changeset
|
1200 |
|
46664
031dfe455585
Merged revisions 62425-62429,62434-62436,62441,62444,62446-62448,62450-62455,62463,62465-62466,62469,62474,62476-62478,62480,62485,62492,62497-62498,62500,62507,62513-62514,62516,62521,62531,62535,62545-62546,62548-62551,62553-62559,62569,62574,62577,62593,62595,62604-62606,62608,62616,62626-62627,62636,62638,62644-62645,62647-62648,62651-62653,62656,62661,62663,62680,62686-62687,62696,62699-62703,62711 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
46306
diff
changeset
|
1201 n_fors = count_comp_fors(c, n); |
| 35742 | 1202 if (n_fors == -1) |
| 1203 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
|
1204 |
|
41464
060c79f6d919
Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents:
41235
diff
changeset
|
1205 comps = asdl_seq_new(n_fors, c->c_arena); |
|
060c79f6d919
Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents:
41235
diff
changeset
|
1206 if (!comps) |
| 35742 | 1207 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
|
1208 |
| 35742 | 1209 for (i = 0; i < n_fors; i++) { |
|
41464
060c79f6d919
Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents:
41235
diff
changeset
|
1210 comprehension_ty comp; |
| 35742 | 1211 asdl_seq *t; |
|
55912
d4e3af343e32
Merged revisions 74464 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
54954
diff
changeset
|
1212 expr_ty expression, first; |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1213 node *for_ch; |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
56951
diff
changeset
|
1214 |
|
42193
090588a17490
Merged revisions 56154-56264 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42084
diff
changeset
|
1215 REQ(n, comp_for); |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
56951
diff
changeset
|
1216 |
|
42193
090588a17490
Merged revisions 56154-56264 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42084
diff
changeset
|
1217 for_ch = CHILD(n, 1); |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1218 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
|
1219 if (!t) |
| 35742 | 1220 return NULL; |
|
42193
090588a17490
Merged revisions 56154-56264 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42084
diff
changeset
|
1221 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
|
1222 if (!expression) |
| 35742 | 1223 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
|
1224 |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1225 /* Check the # of children rather than the length of t, since |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1226 (x for x, in ...) has 1 element in t, but still requires a Tuple. */ |
|
55912
d4e3af343e32
Merged revisions 74464 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
54954
diff
changeset
|
1227 first = (expr_ty)asdl_seq_GET(t, 0); |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1228 if (NCH(for_ch) == 1) |
|
55912
d4e3af343e32
Merged revisions 74464 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
54954
diff
changeset
|
1229 comp = comprehension(first, expression, NULL, c->c_arena); |
| 35742 | 1230 else |
|
55912
d4e3af343e32
Merged revisions 74464 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
54954
diff
changeset
|
1231 comp = comprehension(Tuple(t, Store, first->lineno, first->col_offset, |
|
d4e3af343e32
Merged revisions 74464 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
54954
diff
changeset
|
1232 c->c_arena), |
|
d4e3af343e32
Merged revisions 74464 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
54954
diff
changeset
|
1233 expression, NULL, c->c_arena); |
|
41464
060c79f6d919
Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents:
41235
diff
changeset
|
1234 if (!comp) |
| 35742 | 1235 return NULL; |
|
36065
ea083efdc96c
Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents:
36001
diff
changeset
|
1236 |
|
42193
090588a17490
Merged revisions 56154-56264 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42084
diff
changeset
|
1237 if (NCH(n) == 5) { |
| 35742 | 1238 int j, n_ifs; |
| 1239 asdl_seq *ifs; | |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
56951
diff
changeset
|
1240 |
|
42193
090588a17490
Merged revisions 56154-56264 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42084
diff
changeset
|
1241 n = CHILD(n, 4); |
|
46664
031dfe455585
Merged revisions 62425-62429,62434-62436,62441,62444,62446-62448,62450-62455,62463,62465-62466,62469,62474,62476-62478,62480,62485,62492,62497-62498,62500,62507,62513-62514,62516,62521,62531,62535,62545-62546,62548-62551,62553-62559,62569,62574,62577,62593,62595,62604-62606,62608,62616,62626-62627,62636,62638,62644-62645,62647-62648,62651-62653,62656,62661,62663,62680,62686-62687,62696,62699-62703,62711 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
46306
diff
changeset
|
1242 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
|
1243 if (n_ifs == -1) |
| 35742 | 1244 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
|
1245 |
|
ea083efdc96c
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 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
|
1247 if (!ifs) |
| 35742 | 1248 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
|
1249 |
| 35742 | 1250 for (j = 0; j < n_ifs; j++) { |
|
42193
090588a17490
Merged revisions 56154-56264 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42084
diff
changeset
|
1251 REQ(n, comp_iter); |
|
090588a17490
Merged revisions 56154-56264 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42084
diff
changeset
|
1252 n = CHILD(n, 0); |
|
090588a17490
Merged revisions 56154-56264 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42084
diff
changeset
|
1253 REQ(n, comp_if); |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
56951
diff
changeset
|
1254 |
|
42193
090588a17490
Merged revisions 56154-56264 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42084
diff
changeset
|
1255 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
|
1256 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
|
1257 return NULL; |
|
36718
45e6f901902c
Remove asdl_seq_APPEND() and simplify asdl seq implementation.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36712
diff
changeset
|
1258 asdl_seq_SET(ifs, j, expression); |
|
42193
090588a17490
Merged revisions 56154-56264 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42084
diff
changeset
|
1259 if (NCH(n) == 3) |
|
090588a17490
Merged revisions 56154-56264 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42084
diff
changeset
|
1260 n = CHILD(n, 2); |
| 35742 | 1261 } |
|
42193
090588a17490
Merged revisions 56154-56264 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42084
diff
changeset
|
1262 /* on exit, must guarantee that n is a comp_for */ |
|
090588a17490
Merged revisions 56154-56264 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42084
diff
changeset
|
1263 if (TYPE(n) == comp_iter) |
|
090588a17490
Merged revisions 56154-56264 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42084
diff
changeset
|
1264 n = CHILD(n, 0); |
|
41464
060c79f6d919
Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents:
41235
diff
changeset
|
1265 comp->ifs = ifs; |
| 35742 | 1266 } |
|
41464
060c79f6d919
Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents:
41235
diff
changeset
|
1267 asdl_seq_SET(comps, i, comp); |
| 35742 | 1268 } |
|
42193
090588a17490
Merged revisions 56154-56264 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42084
diff
changeset
|
1269 return comps; |
|
090588a17490
Merged revisions 56154-56264 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42084
diff
changeset
|
1270 } |
|
090588a17490
Merged revisions 56154-56264 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42084
diff
changeset
|
1271 |
|
090588a17490
Merged revisions 56154-56264 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42084
diff
changeset
|
1272 static expr_ty |
|
090588a17490
Merged revisions 56154-56264 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42084
diff
changeset
|
1273 ast_for_itercomp(struct compiling *c, const node *n, int type) |
|
090588a17490
Merged revisions 56154-56264 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42084
diff
changeset
|
1274 { |
|
090588a17490
Merged revisions 56154-56264 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42084
diff
changeset
|
1275 /* testlist_comp: test ( comp_for | (',' test)* [','] ) |
|
090588a17490
Merged revisions 56154-56264 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42084
diff
changeset
|
1276 argument: [test '='] test [comp_for] # Really [keyword '='] test */ |
|
090588a17490
Merged revisions 56154-56264 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42084
diff
changeset
|
1277 expr_ty elt; |
|
090588a17490
Merged revisions 56154-56264 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42084
diff
changeset
|
1278 asdl_seq *comps; |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
56951
diff
changeset
|
1279 |
|
42193
090588a17490
Merged revisions 56154-56264 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42084
diff
changeset
|
1280 assert(NCH(n) > 1); |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
56951
diff
changeset
|
1281 |
|
42193
090588a17490
Merged revisions 56154-56264 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42084
diff
changeset
|
1282 elt = ast_for_expr(c, CHILD(n, 0)); |
|
090588a17490
Merged revisions 56154-56264 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42084
diff
changeset
|
1283 if (!elt) |
|
090588a17490
Merged revisions 56154-56264 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42084
diff
changeset
|
1284 return NULL; |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
56951
diff
changeset
|
1285 |
|
42193
090588a17490
Merged revisions 56154-56264 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42084
diff
changeset
|
1286 comps = ast_for_comprehension(c, CHILD(n, 1)); |
|
090588a17490
Merged revisions 56154-56264 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42084
diff
changeset
|
1287 if (!comps) |
|
090588a17490
Merged revisions 56154-56264 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42084
diff
changeset
|
1288 return NULL; |
|
41464
060c79f6d919
Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents:
41235
diff
changeset
|
1289 |
|
060c79f6d919
Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents:
41235
diff
changeset
|
1290 if (type == COMP_GENEXP) |
|
060c79f6d919
Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents:
41235
diff
changeset
|
1291 return GeneratorExp(elt, comps, LINENO(n), n->n_col_offset, c->c_arena); |
|
060c79f6d919
Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents:
41235
diff
changeset
|
1292 else if (type == COMP_LISTCOMP) |
|
060c79f6d919
Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents:
41235
diff
changeset
|
1293 return ListComp(elt, comps, LINENO(n), n->n_col_offset, c->c_arena); |
|
060c79f6d919
Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents:
41235
diff
changeset
|
1294 else if (type == COMP_SETCOMP) |
|
060c79f6d919
Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents:
41235
diff
changeset
|
1295 return SetComp(elt, comps, LINENO(n), n->n_col_offset, c->c_arena); |
|
060c79f6d919
Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents:
41235
diff
changeset
|
1296 else |
|
060c79f6d919
Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents:
41235
diff
changeset
|
1297 /* Should never happen */ |
|
060c79f6d919
Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents:
41235
diff
changeset
|
1298 return NULL; |
| 35742 | 1299 } |
| 1300 | |
| 1301 static expr_ty | |
|
42193
090588a17490
Merged revisions 56154-56264 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42084
diff
changeset
|
1302 ast_for_dictcomp(struct compiling *c, const node *n) |
|
090588a17490
Merged revisions 56154-56264 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42084
diff
changeset
|
1303 { |
|
090588a17490
Merged revisions 56154-56264 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42084
diff
changeset
|
1304 expr_ty key, value; |
|
090588a17490
Merged revisions 56154-56264 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42084
diff
changeset
|
1305 asdl_seq *comps; |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
56951
diff
changeset
|
1306 |
|
42193
090588a17490
Merged revisions 56154-56264 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42084
diff
changeset
|
1307 assert(NCH(n) > 3); |
|
090588a17490
Merged revisions 56154-56264 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42084
diff
changeset
|
1308 REQ(CHILD(n, 1), COLON); |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
56951
diff
changeset
|
1309 |
|
42193
090588a17490
Merged revisions 56154-56264 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42084
diff
changeset
|
1310 key = ast_for_expr(c, CHILD(n, 0)); |
|
090588a17490
Merged revisions 56154-56264 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42084
diff
changeset
|
1311 if (!key) |
|
090588a17490
Merged revisions 56154-56264 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42084
diff
changeset
|
1312 return NULL; |
|
090588a17490
Merged revisions 56154-56264 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42084
diff
changeset
|
1313 value = ast_for_expr(c, CHILD(n, 2)); |
|
090588a17490
Merged revisions 56154-56264 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42084
diff
changeset
|
1314 if (!value) |
|
090588a17490
Merged revisions 56154-56264 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42084
diff
changeset
|
1315 return NULL; |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
56951
diff
changeset
|
1316 |
|
42193
090588a17490
Merged revisions 56154-56264 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42084
diff
changeset
|
1317 comps = ast_for_comprehension(c, CHILD(n, 3)); |
|
090588a17490
Merged revisions 56154-56264 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42084
diff
changeset
|
1318 if (!comps) |
|
090588a17490
Merged revisions 56154-56264 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42084
diff
changeset
|
1319 return NULL; |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
56951
diff
changeset
|
1320 |
|
42193
090588a17490
Merged revisions 56154-56264 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42084
diff
changeset
|
1321 return DictComp(key, value, comps, LINENO(n), n->n_col_offset, c->c_arena); |
|
090588a17490
Merged revisions 56154-56264 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42084
diff
changeset
|
1322 } |
|
090588a17490
Merged revisions 56154-56264 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42084
diff
changeset
|
1323 |
|
090588a17490
Merged revisions 56154-56264 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42084
diff
changeset
|
1324 static expr_ty |
|
41464
060c79f6d919
Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents:
41235
diff
changeset
|
1325 ast_for_genexp(struct compiling *c, const node *n) |
|
060c79f6d919
Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents:
41235
diff
changeset
|
1326 { |
|
060c79f6d919
Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents:
41235
diff
changeset
|
1327 assert(TYPE(n) == (testlist_comp) || TYPE(n) == (argument)); |
|
42193
090588a17490
Merged revisions 56154-56264 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42084
diff
changeset
|
1328 return ast_for_itercomp(c, n, COMP_GENEXP); |
|
41464
060c79f6d919
Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents:
41235
diff
changeset
|
1329 } |
|
060c79f6d919
Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents:
41235
diff
changeset
|
1330 |
|
060c79f6d919
Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents:
41235
diff
changeset
|
1331 static expr_ty |
|
060c79f6d919
Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents:
41235
diff
changeset
|
1332 ast_for_listcomp(struct compiling *c, const node *n) |
|
060c79f6d919
Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents:
41235
diff
changeset
|
1333 { |
|
060c79f6d919
Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents:
41235
diff
changeset
|
1334 assert(TYPE(n) == (testlist_comp)); |
|
42193
090588a17490
Merged revisions 56154-56264 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42084
diff
changeset
|
1335 return ast_for_itercomp(c, n, COMP_LISTCOMP); |
|
41464
060c79f6d919
Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents:
41235
diff
changeset
|
1336 } |
|
060c79f6d919
Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents:
41235
diff
changeset
|
1337 |
|
060c79f6d919
Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents:
41235
diff
changeset
|
1338 static expr_ty |
|
060c79f6d919
Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents:
41235
diff
changeset
|
1339 ast_for_setcomp(struct compiling *c, const node *n) |
|
060c79f6d919
Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents:
41235
diff
changeset
|
1340 { |
|
060c79f6d919
Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents:
41235
diff
changeset
|
1341 assert(TYPE(n) == (dictorsetmaker)); |
|
42193
090588a17490
Merged revisions 56154-56264 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42084
diff
changeset
|
1342 return ast_for_itercomp(c, n, COMP_SETCOMP); |
|
41464
060c79f6d919
Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents:
41235
diff
changeset
|
1343 } |
|
060c79f6d919
Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents:
41235
diff
changeset
|
1344 |
|
060c79f6d919
Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents:
41235
diff
changeset
|
1345 |
|
060c79f6d919
Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents:
41235
diff
changeset
|
1346 static expr_ty |
| 35742 | 1347 ast_for_atom(struct compiling *c, const node *n) |
| 1348 { | |
|
41464
060c79f6d919
Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents:
41235
diff
changeset
|
1349 /* atom: '(' [yield_expr|testlist_comp] ')' | '[' [testlist_comp] ']' |
|
060c79f6d919
Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents:
41235
diff
changeset
|
1350 | '{' [dictmaker|testlist_comp] '}' | NAME | NUMBER | STRING+ |
|
41969
9d02a4adf574
Merged revisions 55631-55794 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41824
diff
changeset
|
1351 | '...' | 'None' | 'True' | 'False' |
| 35742 | 1352 */ |
| 1353 node *ch = CHILD(n, 0); | |
| 40894 | 1354 int bytesmode = 0; |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
56951
diff
changeset
|
1355 |
| 35742 | 1356 switch (TYPE(ch)) { |
|
49844
40b7277e665f
Merged revisions 67373 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
49797
diff
changeset
|
1357 case NAME: { |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1358 /* All names start in Load context, but may later be |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1359 changed. */ |
|
49844
40b7277e665f
Merged revisions 67373 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
49797
diff
changeset
|
1360 PyObject *name = NEW_IDENTIFIER(ch); |
|
40b7277e665f
Merged revisions 67373 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
49797
diff
changeset
|
1361 if (!name) |
|
40b7277e665f
Merged revisions 67373 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
49797
diff
changeset
|
1362 return NULL; |
|
40b7277e665f
Merged revisions 67373 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
49797
diff
changeset
|
1363 return Name(name, Load, LINENO(n), n->n_col_offset, c->c_arena); |
|
40b7277e665f
Merged revisions 67373 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
49797
diff
changeset
|
1364 } |
| 35742 | 1365 case STRING: { |
| 40894 | 1366 PyObject *str = parsestrplus(c, n, &bytesmode); |
|
42309
bd6acfdb47d2
Merged revisions 56301-56442 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42193
diff
changeset
|
1367 if (!str) { |
|
43819
068655054b0d
Minimal change that disables (AFAICT) the interpolation of \u and \U inside
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
1368 if (PyErr_ExceptionMatches(PyExc_UnicodeError)) { |
|
42309
bd6acfdb47d2
Merged revisions 56301-56442 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42193
diff
changeset
|
1369 PyObject *type, *value, *tback, *errstr; |
|
bd6acfdb47d2
Merged revisions 56301-56442 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42193
diff
changeset
|
1370 PyErr_Fetch(&type, &value, &tback); |
|
49797
f403d94157b2
Merged revisions 67320 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
49697
diff
changeset
|
1371 errstr = PyObject_Str(value); |
|
42309
bd6acfdb47d2
Merged revisions 56301-56442 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42193
diff
changeset
|
1372 if (errstr) { |
|
bd6acfdb47d2
Merged revisions 56301-56442 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42193
diff
changeset
|
1373 char *s = ""; |
|
bd6acfdb47d2
Merged revisions 56301-56442 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42193
diff
changeset
|
1374 char buf[128]; |
|
48660
85b67fb5672b
Rename PyUnicode_AsString -> _PyUnicode_AsString and
Marc-André Lemburg <mal@egenix.com>
parents:
48515
diff
changeset
|
1375 s = _PyUnicode_AsString(errstr); |
|
42309
bd6acfdb47d2
Merged revisions 56301-56442 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42193
diff
changeset
|
1376 PyOS_snprintf(buf, sizeof(buf), "(unicode error) %s", s); |
|
bd6acfdb47d2
Merged revisions 56301-56442 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42193
diff
changeset
|
1377 ast_error(n, buf); |
|
49797
f403d94157b2
Merged revisions 67320 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
49697
diff
changeset
|
1378 Py_DECREF(errstr); |
|
42309
bd6acfdb47d2
Merged revisions 56301-56442 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42193
diff
changeset
|
1379 } else { |
|
bd6acfdb47d2
Merged revisions 56301-56442 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42193
diff
changeset
|
1380 ast_error(n, "(unicode error) unknown error"); |
|
bd6acfdb47d2
Merged revisions 56301-56442 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42193
diff
changeset
|
1381 } |
|
bd6acfdb47d2
Merged revisions 56301-56442 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42193
diff
changeset
|
1382 Py_DECREF(type); |
|
bd6acfdb47d2
Merged revisions 56301-56442 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42193
diff
changeset
|
1383 Py_DECREF(value); |
|
bd6acfdb47d2
Merged revisions 56301-56442 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42193
diff
changeset
|
1384 Py_XDECREF(tback); |
|
bd6acfdb47d2
Merged revisions 56301-56442 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42193
diff
changeset
|
1385 } |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1386 return NULL; |
|
42309
bd6acfdb47d2
Merged revisions 56301-56442 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42193
diff
changeset
|
1387 } |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1388 PyArena_AddPyObject(c->c_arena, str); |
| 40894 | 1389 if (bytesmode) |
| 1390 return Bytes(str, LINENO(n), n->n_col_offset, c->c_arena); | |
| 1391 else | |
| 1392 return Str(str, LINENO(n), n->n_col_offset, c->c_arena); | |
| 35742 | 1393 } |
| 1394 case NUMBER: { | |
|
46664
031dfe455585
Merged revisions 62425-62429,62434-62436,62441,62444,62446-62448,62450-62455,62463,62465-62466,62469,62474,62476-62478,62480,62485,62492,62497-62498,62500,62507,62513-62514,62516,62521,62531,62535,62545-62546,62548-62551,62553-62559,62569,62574,62577,62593,62595,62604-62606,62608,62616,62626-62627,62636,62638,62644-62645,62647-62648,62651-62653,62656,62661,62663,62680,62686-62687,62696,62699-62703,62711 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
46306
diff
changeset
|
1395 PyObject *pynum = parsenumber(c, STR(ch)); |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1396 if (!pynum) |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1397 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
|
1398 |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1399 PyArena_AddPyObject(c->c_arena, pynum); |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1400 return Num(pynum, LINENO(n), n->n_col_offset, c->c_arena); |
| 35742 | 1401 } |
|
41230
b1a18c7ed5ca
Make ELLIPSIS a separate token. This makes it a syntax error to write ". . ." for Ellipsis.
Georg Brandl <georg@python.org>
parents:
41226
diff
changeset
|
1402 case ELLIPSIS: /* Ellipsis */ |
|
40529
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
1403 return Ellipsis(LINENO(n), n->n_col_offset, c->c_arena); |
| 35742 | 1404 case LPAR: /* some parenthesized expressions */ |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1405 ch = CHILD(n, 1); |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
56951
diff
changeset
|
1406 |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1407 if (TYPE(ch) == RPAR) |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1408 return Tuple(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena); |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
56951
diff
changeset
|
1409 |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1410 if (TYPE(ch) == yield_expr) |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1411 return ast_for_expr(c, ch); |
|
41464
060c79f6d919
Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents:
41235
diff
changeset
|
1412 |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
56951
diff
changeset
|
1413 /* testlist_comp: test ( comp_for | (',' test)* [','] ) */ |
|
41464
060c79f6d919
Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents:
41235
diff
changeset
|
1414 if ((NCH(ch) > 1) && (TYPE(CHILD(ch, 1)) == comp_for)) |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1415 return ast_for_genexp(c, ch); |
|
54918
0704f15b4682
Merged revisions 73376,73393,73398,73400,73404-73405,73409,73419-73421,73432,73457,73460,73485-73486,73488-73489,73501-73502,73513-73514 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
54703
diff
changeset
|
1416 |
|
41464
060c79f6d919
Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents:
41235
diff
changeset
|
1417 return ast_for_testlist(c, ch); |
| 35742 | 1418 case LSQB: /* list (or list comprehension) */ |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1419 ch = CHILD(n, 1); |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
56951
diff
changeset
|
1420 |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1421 if (TYPE(ch) == RSQB) |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1422 return List(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena); |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
56951
diff
changeset
|
1423 |
|
41464
060c79f6d919
Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents:
41235
diff
changeset
|
1424 REQ(ch, testlist_comp); |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1425 if (NCH(ch) == 1 || TYPE(CHILD(ch, 1)) == COMMA) { |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1426 asdl_seq *elts = seq_for_testlist(c, ch); |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1427 if (!elts) |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1428 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
|
1429 |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1430 return List(elts, Load, LINENO(n), n->n_col_offset, c->c_arena); |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1431 } |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1432 else |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1433 return ast_for_listcomp(c, ch); |
| 35742 | 1434 case LBRACE: { |
|
41464
060c79f6d919
Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents:
41235
diff
changeset
|
1435 /* dictorsetmaker: test ':' test (',' test ':' test)* [','] | |
|
060c79f6d919
Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents:
41235
diff
changeset
|
1436 * test (gen_for | (',' test)* [',']) */ |
|
40529
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
1437 int i, size; |
|
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
1438 asdl_seq *keys, *values; |
|
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
1439 |
|
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
1440 ch = CHILD(n, 1); |
|
41464
060c79f6d919
Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents:
41235
diff
changeset
|
1441 if (TYPE(ch) == RBRACE) { |
|
060c79f6d919
Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents:
41235
diff
changeset
|
1442 /* it's an empty dict */ |
|
060c79f6d919
Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents:
41235
diff
changeset
|
1443 return Dict(NULL, NULL, LINENO(n), n->n_col_offset, c->c_arena); |
|
060c79f6d919
Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents:
41235
diff
changeset
|
1444 } else if (NCH(ch) == 1 || TYPE(CHILD(ch, 1)) == COMMA) { |
|
060c79f6d919
Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents:
41235
diff
changeset
|
1445 /* it's a simple set */ |
|
41482
a5e410639e24
Make it compile with C89.
Guido van Rossum <guido@python.org>
parents:
41464
diff
changeset
|
1446 asdl_seq *elts; |
|
39693
d0b83c4630c0
SF patch 1547796 by Georg Brandl -- set literals.
Guido van Rossum <guido@python.org>
parents:
39678
diff
changeset
|
1447 size = (NCH(ch) + 1) / 2; /* +1 in case no trailing comma */ |
|
41482
a5e410639e24
Make it compile with C89.
Guido van Rossum <guido@python.org>
parents:
41464
diff
changeset
|
1448 elts = asdl_seq_new(size, c->c_arena); |
|
41464
060c79f6d919
Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents:
41235
diff
changeset
|
1449 if (!elts) |
|
39693
d0b83c4630c0
SF patch 1547796 by Georg Brandl -- set literals.
Guido van Rossum <guido@python.org>
parents:
39678
diff
changeset
|
1450 return NULL; |
|
d0b83c4630c0
SF patch 1547796 by Georg Brandl -- set literals.
Guido van Rossum <guido@python.org>
parents:
39678
diff
changeset
|
1451 for (i = 0; i < NCH(ch); i += 2) { |
|
d0b83c4630c0
SF patch 1547796 by Georg Brandl -- set literals.
Guido van Rossum <guido@python.org>
parents:
39678
diff
changeset
|
1452 expr_ty expression; |
|
d0b83c4630c0
SF patch 1547796 by Georg Brandl -- set literals.
Guido van Rossum <guido@python.org>
parents:
39678
diff
changeset
|
1453 expression = ast_for_expr(c, CHILD(ch, i)); |
|
d0b83c4630c0
SF patch 1547796 by Georg Brandl -- set literals.
Guido van Rossum <guido@python.org>
parents:
39678
diff
changeset
|
1454 if (!expression) |
|
d0b83c4630c0
SF patch 1547796 by Georg Brandl -- set literals.
Guido van Rossum <guido@python.org>
parents:
39678
diff
changeset
|
1455 return NULL; |
|
41464
060c79f6d919
Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents:
41235
diff
changeset
|
1456 asdl_seq_SET(elts, i / 2, expression); |
|
39693
d0b83c4630c0
SF patch 1547796 by Georg Brandl -- set literals.
Guido van Rossum <guido@python.org>
parents:
39678
diff
changeset
|
1457 } |
|
41464
060c79f6d919
Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents:
41235
diff
changeset
|
1458 return Set(elts, LINENO(n), n->n_col_offset, c->c_arena); |
|
060c79f6d919
Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents:
41235
diff
changeset
|
1459 } else if (TYPE(CHILD(ch, 1)) == comp_for) { |
|
060c79f6d919
Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents:
41235
diff
changeset
|
1460 /* it's a set comprehension */ |
|
060c79f6d919
Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents:
41235
diff
changeset
|
1461 return ast_for_setcomp(c, ch); |
|
42193
090588a17490
Merged revisions 56154-56264 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42084
diff
changeset
|
1462 } else if (NCH(ch) > 3 && TYPE(CHILD(ch, 3)) == comp_for) { |
|
090588a17490
Merged revisions 56154-56264 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42084
diff
changeset
|
1463 return ast_for_dictcomp(c, ch); |
|
39693
d0b83c4630c0
SF patch 1547796 by Georg Brandl -- set literals.
Guido van Rossum <guido@python.org>
parents:
39678
diff
changeset
|
1464 } else { |
|
d0b83c4630c0
SF patch 1547796 by Georg Brandl -- set literals.
Guido van Rossum <guido@python.org>
parents:
39678
diff
changeset
|
1465 /* it's a dict */ |
|
d0b83c4630c0
SF patch 1547796 by Georg Brandl -- set literals.
Guido van Rossum <guido@python.org>
parents:
39678
diff
changeset
|
1466 size = (NCH(ch) + 1) / 4; /* +1 in case no trailing comma */ |
|
d0b83c4630c0
SF patch 1547796 by Georg Brandl -- set literals.
Guido van Rossum <guido@python.org>
parents:
39678
diff
changeset
|
1467 keys = asdl_seq_new(size, c->c_arena); |
|
d0b83c4630c0
SF patch 1547796 by Georg Brandl -- set literals.
Guido van Rossum <guido@python.org>
parents:
39678
diff
changeset
|
1468 if (!keys) |
|
d0b83c4630c0
SF patch 1547796 by Georg Brandl -- set literals.
Guido van Rossum <guido@python.org>
parents:
39678
diff
changeset
|
1469 return NULL; |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
56951
diff
changeset
|
1470 |
|
39693
d0b83c4630c0
SF patch 1547796 by Georg Brandl -- set literals.
Guido van Rossum <guido@python.org>
parents:
39678
diff
changeset
|
1471 values = asdl_seq_new(size, c->c_arena); |
|
d0b83c4630c0
SF patch 1547796 by Georg Brandl -- set literals.
Guido van Rossum <guido@python.org>
parents:
39678
diff
changeset
|
1472 if (!values) |
|
d0b83c4630c0
SF patch 1547796 by Georg Brandl -- set literals.
Guido van Rossum <guido@python.org>
parents:
39678
diff
changeset
|
1473 return NULL; |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
56951
diff
changeset
|
1474 |
|
39693
d0b83c4630c0
SF patch 1547796 by Georg Brandl -- set literals.
Guido van Rossum <guido@python.org>
parents:
39678
diff
changeset
|
1475 for (i = 0; i < NCH(ch); i += 4) { |
|
d0b83c4630c0
SF patch 1547796 by Georg Brandl -- set literals.
Guido van Rossum <guido@python.org>
parents:
39678
diff
changeset
|
1476 expr_ty expression; |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
56951
diff
changeset
|
1477 |
|
39693
d0b83c4630c0
SF patch 1547796 by Georg Brandl -- set literals.
Guido van Rossum <guido@python.org>
parents:
39678
diff
changeset
|
1478 expression = ast_for_expr(c, CHILD(ch, i)); |
|
d0b83c4630c0
SF patch 1547796 by Georg Brandl -- set literals.
Guido van Rossum <guido@python.org>
parents:
39678
diff
changeset
|
1479 if (!expression) |
|
d0b83c4630c0
SF patch 1547796 by Georg Brandl -- set literals.
Guido van Rossum <guido@python.org>
parents:
39678
diff
changeset
|
1480 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
|
1481 |
|
39693
d0b83c4630c0
SF patch 1547796 by Georg Brandl -- set literals.
Guido van Rossum <guido@python.org>
parents:
39678
diff
changeset
|
1482 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
|
1483 |
|
39693
d0b83c4630c0
SF patch 1547796 by Georg Brandl -- set literals.
Guido van Rossum <guido@python.org>
parents:
39678
diff
changeset
|
1484 expression = ast_for_expr(c, CHILD(ch, i + 2)); |
|
d0b83c4630c0
SF patch 1547796 by Georg Brandl -- set literals.
Guido van Rossum <guido@python.org>
parents:
39678
diff
changeset
|
1485 if (!expression) |
|
d0b83c4630c0
SF patch 1547796 by Georg Brandl -- set literals.
Guido van Rossum <guido@python.org>
parents:
39678
diff
changeset
|
1486 return NULL; |
|
d0b83c4630c0
SF patch 1547796 by Georg Brandl -- set literals.
Guido van Rossum <guido@python.org>
parents:
39678
diff
changeset
|
1487 |
|
d0b83c4630c0
SF patch 1547796 by Georg Brandl -- set literals.
Guido van Rossum <guido@python.org>
parents:
39678
diff
changeset
|
1488 asdl_seq_SET(values, i / 4, expression); |
|
d0b83c4630c0
SF patch 1547796 by Georg Brandl -- set literals.
Guido van Rossum <guido@python.org>
parents:
39678
diff
changeset
|
1489 } |
|
d0b83c4630c0
SF patch 1547796 by Georg Brandl -- set literals.
Guido van Rossum <guido@python.org>
parents:
39678
diff
changeset
|
1490 return Dict(keys, values, LINENO(n), n->n_col_offset, c->c_arena); |
|
d0b83c4630c0
SF patch 1547796 by Georg Brandl -- set literals.
Guido van Rossum <guido@python.org>
parents:
39678
diff
changeset
|
1491 } |
| 35742 | 1492 } |
| 1493 default: | |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1494 PyErr_Format(PyExc_SystemError, "unhandled atom %d", TYPE(ch)); |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1495 return NULL; |
| 35742 | 1496 } |
| 1497 } | |
| 1498 | |
| 1499 static slice_ty | |
| 1500 ast_for_slice(struct compiling *c, const node *n) | |
| 1501 { | |
| 1502 node *ch; | |
| 1503 expr_ty lower = NULL, upper = NULL, step = NULL; | |
| 1504 | |
| 1505 REQ(n, subscript); | |
| 1506 | |
| 1507 /* | |
|
39783
7a0ad110e91e
Patch #1550786: ellipsis literal.
Georg Brandl <georg@python.org>
parents:
39782
diff
changeset
|
1508 subscript: test | [test] ':' [test] [sliceop] |
| 35742 | 1509 sliceop: ':' [test] |
| 1510 */ | |
| 1511 ch = CHILD(n, 0); | |
| 1512 if (NCH(n) == 1 && TYPE(ch) == test) { | |
| 1513 /* 'step' variable hold no significance in terms of being used over | |
| 1514 other vars */ | |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
56951
diff
changeset
|
1515 step = ast_for_expr(c, ch); |
| 35742 | 1516 if (!step) |
| 1517 return NULL; | |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
56951
diff
changeset
|
1518 |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1519 return Index(step, c->c_arena); |
| 35742 | 1520 } |
| 1521 | |
| 1522 if (TYPE(ch) == test) { | |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1523 lower = ast_for_expr(c, ch); |
| 35742 | 1524 if (!lower) |
| 1525 return NULL; | |
| 1526 } | |
| 1527 | |
| 1528 /* If there's an upper bound it's in the second or third position. */ | |
| 1529 if (TYPE(ch) == COLON) { | |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1530 if (NCH(n) > 1) { |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1531 node *n2 = CHILD(n, 1); |
| 35742 | 1532 |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1533 if (TYPE(n2) == test) { |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1534 upper = ast_for_expr(c, n2); |
| 35742 | 1535 if (!upper) |
| 1536 return NULL; | |
| 1537 } | |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1538 } |
| 35742 | 1539 } else if (NCH(n) > 2) { |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1540 node *n2 = CHILD(n, 2); |
| 35742 | 1541 |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1542 if (TYPE(n2) == test) { |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1543 upper = ast_for_expr(c, n2); |
| 35742 | 1544 if (!upper) |
| 1545 return NULL; | |
| 1546 } | |
| 1547 } | |
| 1548 | |
| 1549 ch = CHILD(n, NCH(n) - 1); | |
| 1550 if (TYPE(ch) == sliceop) { | |
|
54918
0704f15b4682
Merged revisions 73376,73393,73398,73400,73404-73405,73409,73419-73421,73432,73457,73460,73485-73486,73488-73489,73501-73502,73513-73514 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
54703
diff
changeset
|
1551 if (NCH(ch) != 1) { |
|
37879
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36877
diff
changeset
|
1552 ch = CHILD(ch, 1); |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36877
diff
changeset
|
1553 if (TYPE(ch) == test) { |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36877
diff
changeset
|
1554 step = ast_for_expr(c, ch); |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36877
diff
changeset
|
1555 if (!step) |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36877
diff
changeset
|
1556 return NULL; |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36877
diff
changeset
|
1557 } |
| 35742 | 1558 } |
| 1559 } | |
| 1560 | |
|
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
|
1561 return Slice(lower, upper, step, c->c_arena); |
| 35742 | 1562 } |
| 1563 | |
| 1564 static expr_ty | |
| 1565 ast_for_binop(struct compiling *c, const node *n) | |
| 1566 { | |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
1567 /* Must account for a sequence of expressions. |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
56951
diff
changeset
|
1568 How should A op B op C by represented? |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
1569 BinOp(BinOp(A, op, B), op, C). |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
1570 */ |
| 35742 | 1571 |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
1572 int i, nops; |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
1573 expr_ty expr1, expr2, result; |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
1574 operator_ty newoperator; |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
1575 |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
1576 expr1 = ast_for_expr(c, CHILD(n, 0)); |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
1577 if (!expr1) |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
1578 return NULL; |
| 35742 | 1579 |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
1580 expr2 = ast_for_expr(c, CHILD(n, 2)); |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
1581 if (!expr2) |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
1582 return NULL; |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
1583 |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
1584 newoperator = get_operator(CHILD(n, 1)); |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
1585 if (!newoperator) |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
1586 return NULL; |
| 35742 | 1587 |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
1588 result = BinOp(expr1, newoperator, expr2, LINENO(n), n->n_col_offset, |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
1589 c->c_arena); |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
1590 if (!result) |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
1591 return NULL; |
| 35742 | 1592 |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
1593 nops = (NCH(n) - 1) / 2; |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
1594 for (i = 1; i < nops; i++) { |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
1595 expr_ty tmp_result, tmp; |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
1596 const node* next_oper = CHILD(n, i * 2 + 1); |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
1597 |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
1598 newoperator = get_operator(next_oper); |
|
37879
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36877
diff
changeset
|
1599 if (!newoperator) |
| 35742 | 1600 return NULL; |
| 1601 | |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
1602 tmp = ast_for_expr(c, CHILD(n, i * 2 + 2)); |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
1603 if (!tmp) |
| 35742 | 1604 return NULL; |
| 1605 | |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
56951
diff
changeset
|
1606 tmp_result = BinOp(result, newoperator, tmp, |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
1607 LINENO(next_oper), next_oper->n_col_offset, |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
1608 c->c_arena); |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
56951
diff
changeset
|
1609 if (!tmp_result) |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
1610 return NULL; |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
1611 result = tmp_result; |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
1612 } |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
1613 return result; |
| 35742 | 1614 } |
| 1615 | |
|
35799
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1616 static expr_ty |
|
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1617 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
|
1618 { |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
56951
diff
changeset
|
1619 /* trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME |
|
36686
8611f39cd82b
Fix parsing of subscriptlist.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36682
diff
changeset
|
1620 subscriptlist: subscript (',' subscript)* [','] |
|
8611f39cd82b
Fix parsing of subscriptlist.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36682
diff
changeset
|
1621 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop] |
|
8611f39cd82b
Fix parsing of subscriptlist.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36682
diff
changeset
|
1622 */ |
|
35799
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1623 REQ(n, trailer); |
|
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1624 if (TYPE(CHILD(n, 0)) == LPAR) { |
|
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1625 if (NCH(n) == 2) |
|
37879
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36877
diff
changeset
|
1626 return Call(left_expr, NULL, NULL, NULL, NULL, LINENO(n), |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36877
diff
changeset
|
1627 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
|
1628 else |
|
36679
e1391910b026
Simplify ast_for_trailer() in anticipation of more changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36676
diff
changeset
|
1629 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
|
1630 } |
|
36679
e1391910b026
Simplify ast_for_trailer() in anticipation of more changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36676
diff
changeset
|
1631 else if (TYPE(CHILD(n, 0)) == DOT ) { |
|
49844
40b7277e665f
Merged revisions 67373 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
49797
diff
changeset
|
1632 PyObject *attr_id = NEW_IDENTIFIER(CHILD(n, 1)); |
|
40b7277e665f
Merged revisions 67373 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
49797
diff
changeset
|
1633 if (!attr_id) |
|
40b7277e665f
Merged revisions 67373 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
49797
diff
changeset
|
1634 return NULL; |
|
40b7277e665f
Merged revisions 67373 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
49797
diff
changeset
|
1635 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
|
1636 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
|
1637 } |
|
e1391910b026
Simplify ast_for_trailer() in anticipation of more changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36676
diff
changeset
|
1638 else { |
|
e1391910b026
Simplify ast_for_trailer() in anticipation of more changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36676
diff
changeset
|
1639 REQ(CHILD(n, 0), LSQB); |
|
35799
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1640 REQ(CHILD(n, 2), RSQB); |
|
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1641 n = CHILD(n, 1); |
|
36686
8611f39cd82b
Fix parsing of subscriptlist.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36682
diff
changeset
|
1642 if (NCH(n) == 1) { |
|
35799
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1643 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
|
1644 if (!slc) |
|
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1645 return NULL; |
|
37879
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36877
diff
changeset
|
1646 return Subscript(left_expr, slc, Load, LINENO(n), n->n_col_offset, |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36877
diff
changeset
|
1647 c->c_arena); |
|
35799
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1648 } |
|
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1649 else { |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
56951
diff
changeset
|
1650 /* 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
|
1651 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
|
1652 no slice features. |
|
8611f39cd82b
Fix parsing of subscriptlist.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36682
diff
changeset
|
1653 */ |
|
35799
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1654 int j; |
|
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1655 slice_ty slc; |
|
36686
8611f39cd82b
Fix parsing of subscriptlist.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36682
diff
changeset
|
1656 expr_ty e; |
|
40941
2ca14d6a369b
Whoops, fix build breakage. There were still a few uses of the bool type.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40894
diff
changeset
|
1657 int simple = 1; |
|
36686
8611f39cd82b
Fix parsing of subscriptlist.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36682
diff
changeset
|
1658 asdl_seq *slices, *elts; |
|
8611f39cd82b
Fix parsing of subscriptlist.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36682
diff
changeset
|
1659 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
|
1660 if (!slices) |
|
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1661 return NULL; |
|
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1662 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
|
1663 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
|
1664 if (!slc) |
|
35799
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1665 return NULL; |
|
36686
8611f39cd82b
Fix parsing of subscriptlist.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36682
diff
changeset
|
1666 if (slc->kind != Index_kind) |
|
40941
2ca14d6a369b
Whoops, fix build breakage. There were still a few uses of the bool type.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40894
diff
changeset
|
1667 simple = 0; |
|
35799
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1668 asdl_seq_SET(slices, j / 2, slc); |
|
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1669 } |
|
36686
8611f39cd82b
Fix parsing of subscriptlist.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36682
diff
changeset
|
1670 if (!simple) { |
|
8611f39cd82b
Fix parsing of subscriptlist.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36682
diff
changeset
|
1671 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
|
1672 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
|
1673 } |
|
8611f39cd82b
Fix parsing of subscriptlist.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36682
diff
changeset
|
1674 /* extract Index values and put them in a Tuple */ |
|
8611f39cd82b
Fix parsing of subscriptlist.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36682
diff
changeset
|
1675 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
|
1676 if (!elts) |
|
e9a7a2784e17
SF #1444030: Fix several potential defects found by Coverity.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
36786
diff
changeset
|
1677 return NULL; |
|
36686
8611f39cd82b
Fix parsing of subscriptlist.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36682
diff
changeset
|
1678 for (j = 0; j < asdl_seq_LEN(slices); ++j) { |
|
8611f39cd82b
Fix parsing of subscriptlist.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36682
diff
changeset
|
1679 slc = (slice_ty)asdl_seq_GET(slices, j); |
|
8611f39cd82b
Fix parsing of subscriptlist.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36682
diff
changeset
|
1680 assert(slc->kind == Index_kind && slc->v.Index.value); |
|
8611f39cd82b
Fix parsing of subscriptlist.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36682
diff
changeset
|
1681 asdl_seq_SET(elts, j, slc->v.Index.value); |
|
8611f39cd82b
Fix parsing of subscriptlist.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36682
diff
changeset
|
1682 } |
|
36786
57322cb2ca11
Patch #1440601: Add col_offset attribute to AST nodes.
Martin v. Löwis <martin@v.loewis.de>
parents:
36780
diff
changeset
|
1683 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
|
1684 if (!e) |
|
8611f39cd82b
Fix parsing of subscriptlist.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36682
diff
changeset
|
1685 return NULL; |
|
8611f39cd82b
Fix parsing of subscriptlist.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36682
diff
changeset
|
1686 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
|
1687 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
|
1688 } |
|
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1689 } |
|
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1690 } |
|
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1691 |
|
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1692 static expr_ty |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
1693 ast_for_factor(struct compiling *c, const node *n) |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
1694 { |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
1695 expr_ty expression; |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
1696 |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
1697 expression = ast_for_expr(c, CHILD(n, 1)); |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
1698 if (!expression) |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
1699 return NULL; |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
1700 |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
1701 switch (TYPE(CHILD(n, 0))) { |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
1702 case PLUS: |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
1703 return UnaryOp(UAdd, expression, LINENO(n), n->n_col_offset, |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
1704 c->c_arena); |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
1705 case MINUS: |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
1706 return UnaryOp(USub, expression, LINENO(n), n->n_col_offset, |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
1707 c->c_arena); |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
1708 case TILDE: |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
1709 return UnaryOp(Invert, expression, LINENO(n), |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
1710 n->n_col_offset, c->c_arena); |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
1711 } |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
1712 PyErr_Format(PyExc_SystemError, "unhandled factor: %d", |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
1713 TYPE(CHILD(n, 0))); |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
1714 return NULL; |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
1715 } |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
1716 |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
1717 static expr_ty |
|
35799
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1718 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
|
1719 { |
|
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1720 /* power: atom trailer* ('**' factor)* |
|
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1721 */ |
|
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1722 int i; |
|
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1723 expr_ty e, tmp; |
|
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1724 REQ(n, power); |
|
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1725 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
|
1726 if (!e) |
|
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1727 return NULL; |
|
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1728 if (NCH(n) == 1) |
|
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1729 return e; |
|
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1730 for (i = 1; i < NCH(n); i++) { |
|
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1731 node *ch = CHILD(n, i); |
|
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1732 if (TYPE(ch) != trailer) |
|
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1733 break; |
|
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1734 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
|
1735 if (!tmp) |
|
35799
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1736 return NULL; |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1737 tmp->lineno = e->lineno; |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1738 tmp->col_offset = e->col_offset; |
|
35799
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1739 e = tmp; |
|
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1740 } |
|
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1741 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
|
1742 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
|
1743 if (!f) |
|
35799
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1744 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
|
1745 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
|
1746 if (!tmp) |
|
35799
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1747 return NULL; |
|
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1748 e = tmp; |
|
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1749 } |
|
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1750 return e; |
|
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1751 } |
|
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1752 |
|
41699
e16c8e5c5ca4
Merged revisions 55225-55227,55229-55269 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41698
diff
changeset
|
1753 static expr_ty |
|
e16c8e5c5ca4
Merged revisions 55225-55227,55229-55269 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41698
diff
changeset
|
1754 ast_for_starred(struct compiling *c, const node *n) |
|
e16c8e5c5ca4
Merged revisions 55225-55227,55229-55269 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41698
diff
changeset
|
1755 { |
|
e16c8e5c5ca4
Merged revisions 55225-55227,55229-55269 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41698
diff
changeset
|
1756 expr_ty tmp; |
|
e16c8e5c5ca4
Merged revisions 55225-55227,55229-55269 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41698
diff
changeset
|
1757 REQ(n, star_expr); |
|
e16c8e5c5ca4
Merged revisions 55225-55227,55229-55269 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41698
diff
changeset
|
1758 |
|
e16c8e5c5ca4
Merged revisions 55225-55227,55229-55269 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41698
diff
changeset
|
1759 tmp = ast_for_expr(c, CHILD(n, 1)); |
|
e16c8e5c5ca4
Merged revisions 55225-55227,55229-55269 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41698
diff
changeset
|
1760 if (!tmp) |
|
e16c8e5c5ca4
Merged revisions 55225-55227,55229-55269 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41698
diff
changeset
|
1761 return NULL; |
|
e16c8e5c5ca4
Merged revisions 55225-55227,55229-55269 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41698
diff
changeset
|
1762 |
|
e16c8e5c5ca4
Merged revisions 55225-55227,55229-55269 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41698
diff
changeset
|
1763 /* The Load context is changed later. */ |
|
e16c8e5c5ca4
Merged revisions 55225-55227,55229-55269 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41698
diff
changeset
|
1764 return Starred(tmp, Load, LINENO(n), n->n_col_offset, c->c_arena); |
|
e16c8e5c5ca4
Merged revisions 55225-55227,55229-55269 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41698
diff
changeset
|
1765 } |
|
e16c8e5c5ca4
Merged revisions 55225-55227,55229-55269 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41698
diff
changeset
|
1766 |
|
e16c8e5c5ca4
Merged revisions 55225-55227,55229-55269 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41698
diff
changeset
|
1767 |
| 35742 | 1768 /* Do not name a variable 'expr'! Will cause a compile error. |
| 1769 */ | |
| 1770 | |
| 1771 static expr_ty | |
| 1772 ast_for_expr(struct compiling *c, const node *n) | |
| 1773 { | |
| 1774 /* 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
|
1775 test: or_test ['if' or_test 'else' test] | lambdef |
|
41464
060c79f6d919
Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents:
41235
diff
changeset
|
1776 test_nocond: or_test | lambdef_nocond |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
56951
diff
changeset
|
1777 or_test: and_test ('or' and_test)* |
| 35742 | 1778 and_test: not_test ('and' not_test)* |
| 1779 not_test: 'not' not_test | comparison | |
| 1780 comparison: expr (comp_op expr)* | |
| 1781 expr: xor_expr ('|' xor_expr)* | |
| 1782 xor_expr: and_expr ('^' and_expr)* | |
| 1783 and_expr: shift_expr ('&' shift_expr)* | |
| 1784 shift_expr: arith_expr (('<<'|'>>') arith_expr)* | |
| 1785 arith_expr: term (('+'|'-') term)* | |
| 1786 term: factor (('*'|'/'|'%'|'//') factor)* | |
| 1787 factor: ('+'|'-'|'~') factor | power | |
| 1788 power: atom trailer* ('**' factor)* | |
| 1789 */ | |
| 1790 | |
| 1791 asdl_seq *seq; | |
| 1792 int i; | |
| 1793 | |
| 1794 loop: | |
| 1795 switch (TYPE(n)) { | |
| 1796 case test: | |
|
41464
060c79f6d919
Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents:
41235
diff
changeset
|
1797 case test_nocond: |
|
36666
6165927431d6
PEP 308 implementation, including minor refdocs and some testcases. It
Thomas Wouters <thomas@python.org>
parents:
36444
diff
changeset
|
1798 if (TYPE(CHILD(n, 0)) == lambdef || |
|
41464
060c79f6d919
Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents:
41235
diff
changeset
|
1799 TYPE(CHILD(n, 0)) == lambdef_nocond) |
| 35742 | 1800 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
|
1801 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
|
1802 return ast_for_ifexpr(c, n); |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1803 /* Fallthrough */ |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1804 case or_test: |
| 35742 | 1805 case and_test: |
| 1806 if (NCH(n) == 1) { | |
| 1807 n = CHILD(n, 0); | |
| 1808 goto loop; | |
| 1809 } | |
|
36065
ea083efdc96c
Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents:
36001
diff
changeset
|
1810 seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena); |
| 35742 | 1811 if (!seq) |
| 1812 return NULL; | |
| 1813 for (i = 0; i < NCH(n); i += 2) { | |
| 1814 expr_ty e = ast_for_expr(c, CHILD(n, i)); | |
| 1815 if (!e) | |
| 1816 return NULL; | |
| 1817 asdl_seq_SET(seq, i / 2, e); | |
| 1818 } | |
| 1819 if (!strcmp(STR(CHILD(n, 1)), "and")) | |
|
37879
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36877
diff
changeset
|
1820 return BoolOp(And, seq, LINENO(n), n->n_col_offset, |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36877
diff
changeset
|
1821 c->c_arena); |
|
36072
de16a85c3b4c
Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents:
36066
diff
changeset
|
1822 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
|
1823 return BoolOp(Or, seq, LINENO(n), n->n_col_offset, c->c_arena); |
| 35742 | 1824 case not_test: |
| 1825 if (NCH(n) == 1) { | |
| 1826 n = CHILD(n, 0); | |
| 1827 goto loop; | |
| 1828 } | |
| 1829 else { | |
| 1830 expr_ty expression = ast_for_expr(c, CHILD(n, 1)); | |
| 1831 if (!expression) | |
| 1832 return NULL; | |
| 1833 | |
|
37879
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36877
diff
changeset
|
1834 return UnaryOp(Not, expression, LINENO(n), n->n_col_offset, |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36877
diff
changeset
|
1835 c->c_arena); |
| 35742 | 1836 } |
| 1837 case comparison: | |
| 1838 if (NCH(n) == 1) { | |
| 1839 n = CHILD(n, 0); | |
| 1840 goto loop; | |
| 1841 } | |
| 1842 else { | |
| 1843 expr_ty expression; | |
|
37879
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36877
diff
changeset
|
1844 asdl_int_seq *ops; |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1845 asdl_seq *cmps; |
|
37879
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36877
diff
changeset
|
1846 ops = asdl_int_seq_new(NCH(n) / 2, c->c_arena); |
| 35742 | 1847 if (!ops) |
| 1848 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
|
1849 cmps = asdl_seq_new(NCH(n) / 2, c->c_arena); |
| 35742 | 1850 if (!cmps) { |
| 1851 return NULL; | |
| 1852 } | |
| 1853 for (i = 1; i < NCH(n); i += 2) { | |
|
37879
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36877
diff
changeset
|
1854 cmpop_ty newoperator; |
| 35742 | 1855 |
|
46664
031dfe455585
Merged revisions 62425-62429,62434-62436,62441,62444,62446-62448,62450-62455,62463,62465-62466,62469,62474,62476-62478,62480,62485,62492,62497-62498,62500,62507,62513-62514,62516,62521,62531,62535,62545-62546,62548-62551,62553-62559,62569,62574,62577,62593,62595,62604-62606,62608,62616,62626-62627,62636,62638,62644-62645,62647-62648,62651-62653,62656,62661,62663,62680,62686-62687,62696,62699-62703,62711 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
46306
diff
changeset
|
1856 newoperator = ast_for_comp_op(c, CHILD(n, i)); |
|
37879
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36877
diff
changeset
|
1857 if (!newoperator) { |
| 35742 | 1858 return NULL; |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1859 } |
| 35742 | 1860 |
| 1861 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
|
1862 if (!expression) { |
| 35742 | 1863 return NULL; |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1864 } |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
56951
diff
changeset
|
1865 |
|
37879
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36877
diff
changeset
|
1866 asdl_seq_SET(ops, i / 2, newoperator); |
| 35742 | 1867 asdl_seq_SET(cmps, i / 2, expression); |
| 1868 } | |
| 1869 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
|
1870 if (!expression) { |
| 35742 | 1871 return NULL; |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1872 } |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
56951
diff
changeset
|
1873 |
|
37879
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36877
diff
changeset
|
1874 return Compare(expression, ops, cmps, LINENO(n), |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36877
diff
changeset
|
1875 n->n_col_offset, c->c_arena); |
| 35742 | 1876 } |
| 1877 break; | |
| 1878 | |
|
41699
e16c8e5c5ca4
Merged revisions 55225-55227,55229-55269 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41698
diff
changeset
|
1879 case star_expr: |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
56951
diff
changeset
|
1880 return ast_for_starred(c, n); |
| 35742 | 1881 /* The next five cases all handle BinOps. The main body of code |
| 1882 is the same in each case, but the switch turned inside out to | |
| 1883 reuse the code for each type of operator. | |
| 1884 */ | |
| 1885 case expr: | |
| 1886 case xor_expr: | |
| 1887 case and_expr: | |
| 1888 case shift_expr: | |
| 1889 case arith_expr: | |
| 1890 case term: | |
| 1891 if (NCH(n) == 1) { | |
| 1892 n = CHILD(n, 0); | |
| 1893 goto loop; | |
| 1894 } | |
| 1895 return ast_for_binop(c, n); | |
| 1896 case yield_expr: { | |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1897 expr_ty exp = NULL; |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1898 if (NCH(n) == 2) { |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1899 exp = ast_for_testlist(c, CHILD(n, 1)); |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1900 if (!exp) |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1901 return NULL; |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1902 } |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1903 return Yield(exp, LINENO(n), n->n_col_offset, c->c_arena); |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1904 } |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
1905 case factor: |
| 35742 | 1906 if (NCH(n) == 1) { |
| 1907 n = CHILD(n, 0); | |
| 1908 goto loop; | |
| 1909 } | |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1910 return ast_for_factor(c, n); |
|
35799
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1911 case power: |
|
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1912 return ast_for_power(c, n); |
| 35742 | 1913 default: |
|
35870
f531c05cd240
Convert all internal errors from Exception to SystemError
Neal Norwitz <nnorwitz@gmail.com>
parents:
35868
diff
changeset
|
1914 PyErr_Format(PyExc_SystemError, "unhandled expr: %d", TYPE(n)); |
| 35742 | 1915 return NULL; |
| 1916 } | |
|
36072
de16a85c3b4c
Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents:
36066
diff
changeset
|
1917 /* should never get here unless if error is set */ |
| 35742 | 1918 return NULL; |
| 1919 } | |
| 1920 | |
| 1921 static expr_ty | |
| 1922 ast_for_call(struct compiling *c, const node *n, expr_ty func) | |
| 1923 { | |
| 1924 /* | |
| 1925 arglist: (argument ',')* (argument [',']| '*' test [',' '**' test] | |
| 1926 | '**' test) | |
|
41464
060c79f6d919
Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents:
41235
diff
changeset
|
1927 argument: [test '='] test [comp_for] # Really [keyword '='] test |
| 35742 | 1928 */ |
| 1929 | |
| 1930 int i, nargs, nkeywords, ngens; | |
|
36072
de16a85c3b4c
Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents:
36066
diff
changeset
|
1931 asdl_seq *args; |
|
de16a85c3b4c
Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents:
36066
diff
changeset
|
1932 asdl_seq *keywords; |
| 35742 | 1933 expr_ty vararg = NULL, kwarg = NULL; |
| 1934 | |
| 1935 REQ(n, arglist); | |
| 1936 | |
| 1937 nargs = 0; | |
| 1938 nkeywords = 0; | |
| 1939 ngens = 0; | |
| 1940 for (i = 0; i < NCH(n); i++) { | |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1941 node *ch = CHILD(n, i); |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1942 if (TYPE(ch) == argument) { |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1943 if (NCH(ch) == 1) |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1944 nargs++; |
|
41464
060c79f6d919
Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents:
41235
diff
changeset
|
1945 else if (TYPE(CHILD(ch, 1)) == comp_for) |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1946 ngens++; |
| 35742 | 1947 else |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1948 nkeywords++; |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1949 } |
| 35742 | 1950 } |
| 1951 if (ngens > 1 || (ngens && (nargs || nkeywords))) { | |
|
36418
a625e8876407
Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36242
diff
changeset
|
1952 ast_error(n, "Generator expression must be parenthesized " |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1953 "if not sole argument"); |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1954 return NULL; |
| 35742 | 1955 } |
| 1956 | |
| 1957 if (nargs + nkeywords + ngens > 255) { | |
| 1958 ast_error(n, "more than 255 arguments"); | |
| 1959 return NULL; | |
| 1960 } | |
| 1961 | |
|
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
|
1962 args = asdl_seq_new(nargs + ngens, c->c_arena); |
| 35742 | 1963 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
|
1964 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
|
1965 keywords = asdl_seq_new(nkeywords, c->c_arena); |
| 35742 | 1966 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
|
1967 return NULL; |
| 35742 | 1968 nargs = 0; |
| 1969 nkeywords = 0; | |
| 1970 for (i = 0; i < NCH(n); i++) { | |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1971 node *ch = CHILD(n, i); |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1972 if (TYPE(ch) == argument) { |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1973 expr_ty e; |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1974 if (NCH(ch) == 1) { |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1975 if (nkeywords) { |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1976 ast_error(CHILD(ch, 0), |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1977 "non-keyword arg after keyword arg"); |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1978 return NULL; |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1979 } |
|
48777
6abada05c313
allow keyword args after *args in a function call
Benjamin Peterson <benjamin@python.org>
parents:
48660
diff
changeset
|
1980 if (vararg) { |
|
6abada05c313
allow keyword args after *args in a function call
Benjamin Peterson <benjamin@python.org>
parents:
48660
diff
changeset
|
1981 ast_error(CHILD(ch, 0), |
|
6abada05c313
allow keyword args after *args in a function call
Benjamin Peterson <benjamin@python.org>
parents:
48660
diff
changeset
|
1982 "only named arguments may follow *expression"); |
|
6abada05c313
allow keyword args after *args in a function call
Benjamin Peterson <benjamin@python.org>
parents:
48660
diff
changeset
|
1983 return NULL; |
|
6abada05c313
allow keyword args after *args in a function call
Benjamin Peterson <benjamin@python.org>
parents:
48660
diff
changeset
|
1984 } |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1985 e = ast_for_expr(c, CHILD(ch, 0)); |
| 35742 | 1986 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
|
1987 return NULL; |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1988 asdl_seq_SET(args, nargs++, e); |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
56951
diff
changeset
|
1989 } |
|
41464
060c79f6d919
Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents:
41235
diff
changeset
|
1990 else if (TYPE(CHILD(ch, 1)) == comp_for) { |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1991 e = ast_for_genexp(c, ch); |
| 35742 | 1992 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
|
1993 return NULL; |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1994 asdl_seq_SET(args, nargs++, e); |
| 35742 | 1995 } |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1996 else { |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1997 keyword_ty kw; |
|
48397
42309437f1ca
Merged revisions 64622 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
48330
diff
changeset
|
1998 identifier key, tmp; |
|
42309437f1ca
Merged revisions 64622 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
48330
diff
changeset
|
1999 int k; |
| 35742 | 2000 |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
56951
diff
changeset
|
2001 /* CHILD(ch, 0) is test, but must be an identifier? */ |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2002 e = ast_for_expr(c, CHILD(ch, 0)); |
| 35742 | 2003 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
|
2004 return NULL; |
| 35742 | 2005 /* f(lambda x: x[0] = 3) ends up getting parsed with |
| 2006 * LHS test = lambda x: x[0], and RHS test = 3. | |
| 2007 * SF bug 132313 points out that complaining about a keyword | |
| 2008 * then is very confusing. | |
| 2009 */ | |
| 2010 if (e->kind == Lambda_kind) { | |
| 2011 ast_error(CHILD(ch, 0), "lambda cannot contain assignment"); | |
|
36065
ea083efdc96c
Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents:
36001
diff
changeset
|
2012 return NULL; |
| 35742 | 2013 } else if (e->kind != Name_kind) { |
| 2014 ast_error(CHILD(ch, 0), "keyword can't be an expression"); | |
|
36065
ea083efdc96c
Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents:
36001
diff
changeset
|
2015 return NULL; |
|
54954
c45e331238ae
In most cases, the parser will protect True, False, and None from being assign to.
Benjamin Peterson <benjamin@python.org>
parents:
54918
diff
changeset
|
2016 } else if (forbidden_name(e->v.Name.id, ch, 1)) { |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
56951
diff
changeset
|
2017 return NULL; |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
56951
diff
changeset
|
2018 } |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2019 key = e->v.Name.id; |
|
48397
42309437f1ca
Merged revisions 64622 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
48330
diff
changeset
|
2020 for (k = 0; k < nkeywords; k++) { |
|
42309437f1ca
Merged revisions 64622 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
48330
diff
changeset
|
2021 tmp = ((keyword_ty)asdl_seq_GET(keywords, k))->arg; |
|
42309437f1ca
Merged revisions 64622 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
48330
diff
changeset
|
2022 if (!PyUnicode_Compare(tmp, key)) { |
|
42309437f1ca
Merged revisions 64622 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
48330
diff
changeset
|
2023 ast_error(CHILD(ch, 0), "keyword argument repeated"); |
|
42309437f1ca
Merged revisions 64622 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
48330
diff
changeset
|
2024 return NULL; |
|
42309437f1ca
Merged revisions 64622 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
48330
diff
changeset
|
2025 } |
|
42309437f1ca
Merged revisions 64622 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
48330
diff
changeset
|
2026 } |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2027 e = ast_for_expr(c, CHILD(ch, 2)); |
| 35742 | 2028 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
|
2029 return NULL; |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2030 kw = keyword(key, e, c->c_arena); |
| 35742 | 2031 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
|
2032 return NULL; |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2033 asdl_seq_SET(keywords, nkeywords++, kw); |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2034 } |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2035 } |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2036 else if (TYPE(ch) == STAR) { |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2037 vararg = ast_for_expr(c, CHILD(n, i+1)); |
|
45773
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45448
diff
changeset
|
2038 if (!vararg) |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45448
diff
changeset
|
2039 return NULL; |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2040 i++; |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2041 } |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2042 else if (TYPE(ch) == DOUBLESTAR) { |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2043 kwarg = ast_for_expr(c, CHILD(n, i+1)); |
|
45773
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45448
diff
changeset
|
2044 if (!kwarg) |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45448
diff
changeset
|
2045 return NULL; |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2046 i++; |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2047 } |
| 35742 | 2048 } |
| 2049 | |
|
36786
57322cb2ca11
Patch #1440601: Add col_offset attribute to AST nodes.
Martin v. Löwis <martin@v.loewis.de>
parents:
36780
diff
changeset
|
2050 return Call(func, args, keywords, vararg, kwarg, func->lineno, func->col_offset, c->c_arena); |
| 35742 | 2051 } |
| 2052 | |
| 2053 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
|
2054 ast_for_testlist(struct compiling *c, const node* n) |
| 35742 | 2055 { |
|
41464
060c79f6d919
Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents:
41235
diff
changeset
|
2056 /* testlist_comp: test (comp_for | (',' test)* [',']) */ |
|
35798
857f68fba275
Write a separate ast_for_testlist_gexp() function instead of overloading
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35771
diff
changeset
|
2057 /* testlist: test (',' test)* [','] */ |
| 35742 | 2058 assert(NCH(n) > 0); |
|
41464
060c79f6d919
Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents:
41235
diff
changeset
|
2059 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
|
2060 if (NCH(n) > 1) |
|
41464
060c79f6d919
Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents:
41235
diff
changeset
|
2061 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
|
2062 } |
|
857f68fba275
Write a separate ast_for_testlist_gexp() function instead of overloading
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35771
diff
changeset
|
2063 else { |
|
857f68fba275
Write a separate ast_for_testlist_gexp() function instead of overloading
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35771
diff
changeset
|
2064 assert(TYPE(n) == testlist || |
|
56237
7ee140b7ef93
fix an ambiguity in the grammar from the implementation of extended unpacking
Benjamin Peterson <benjamin@python.org>
parents:
55912
diff
changeset
|
2065 TYPE(n) == testlist_star_expr); |
|
35798
857f68fba275
Write a separate ast_for_testlist_gexp() function instead of overloading
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35771
diff
changeset
|
2066 } |
| 35742 | 2067 if (NCH(n) == 1) |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2068 return ast_for_expr(c, CHILD(n, 0)); |
| 35742 | 2069 else { |
| 2070 asdl_seq *tmp = seq_for_testlist(c, n); | |
| 2071 if (!tmp) | |
| 2072 return NULL; | |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2073 return Tuple(tmp, Load, LINENO(n), n->n_col_offset, c->c_arena); |
| 35742 | 2074 } |
|
35798
857f68fba275
Write a separate ast_for_testlist_gexp() function instead of overloading
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35771
diff
changeset
|
2075 } |
|
857f68fba275
Write a separate ast_for_testlist_gexp() function instead of overloading
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35771
diff
changeset
|
2076 |
| 35742 | 2077 static stmt_ty |
| 2078 ast_for_expr_stmt(struct compiling *c, const node *n) | |
| 2079 { | |
| 2080 REQ(n, expr_stmt); | |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
56951
diff
changeset
|
2081 /* expr_stmt: testlist_star_expr (augassign (yield_expr|testlist) |
| 35742 | 2082 | ('=' (yield_expr|testlist))*) |
|
56237
7ee140b7ef93
fix an ambiguity in the grammar from the implementation of extended unpacking
Benjamin Peterson <benjamin@python.org>
parents:
55912
diff
changeset
|
2083 testlist_star_expr: (test|star_expr) (',' test|star_expr)* [','] |
| 35742 | 2084 augassign: '+=' | '-=' | '*=' | '/=' | '%=' | '&=' | '|=' | '^=' |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2085 | '<<=' | '>>=' | '**=' | '//=' |
| 35742 | 2086 test: ... here starts the operator precendence dance |
| 2087 */ | |
| 2088 | |
| 2089 if (NCH(n) == 1) { | |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2090 expr_ty e = ast_for_testlist(c, CHILD(n, 0)); |
| 35742 | 2091 if (!e) |
| 2092 return NULL; | |
| 2093 | |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2094 return Expr(e, LINENO(n), n->n_col_offset, c->c_arena); |
| 35742 | 2095 } |
| 2096 else if (TYPE(CHILD(n, 1)) == augassign) { | |
| 2097 expr_ty expr1, expr2; | |
|
37879
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36877
diff
changeset
|
2098 operator_ty newoperator; |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2099 node *ch = CHILD(n, 0); |
| 35742 | 2100 |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2101 expr1 = ast_for_testlist(c, ch); |
| 35742 | 2102 if (!expr1) |
| 2103 return NULL; | |
|
46664
031dfe455585
Merged revisions 62425-62429,62434-62436,62441,62444,62446-62448,62450-62455,62463,62465-62466,62469,62474,62476-62478,62480,62485,62492,62497-62498,62500,62507,62513-62514,62516,62521,62531,62535,62545-62546,62548-62551,62553-62559,62569,62574,62577,62593,62595,62604-62606,62608,62616,62626-62627,62636,62638,62644-62645,62647-62648,62651-62653,62656,62661,62663,62680,62686-62687,62696,62699-62703,62711 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
46306
diff
changeset
|
2104 if(!set_context(c, expr1, Store, ch)) |
|
031dfe455585
Merged revisions 62425-62429,62434-62436,62441,62444,62446-62448,62450-62455,62463,62465-62466,62469,62474,62476-62478,62480,62485,62492,62497-62498,62500,62507,62513-62514,62516,62521,62531,62535,62545-62546,62548-62551,62553-62559,62569,62574,62577,62593,62595,62604-62606,62608,62616,62626-62627,62636,62638,62644-62645,62647-62648,62651-62653,62656,62661,62663,62680,62686-62687,62696,62699-62703,62711 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
46306
diff
changeset
|
2105 return NULL; |
|
56362
45fce310d479
Merged revisions 75223 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
56238
diff
changeset
|
2106 /* set_context checks that most expressions are not the left side. |
|
45fce310d479
Merged revisions 75223 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
56238
diff
changeset
|
2107 Augmented assignments can only have a name, a subscript, or an |
|
45fce310d479
Merged revisions 75223 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
56238
diff
changeset
|
2108 attribute on the left, though, so we have to explicitly check for |
|
45fce310d479
Merged revisions 75223 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
56238
diff
changeset
|
2109 those. */ |
|
45fce310d479
Merged revisions 75223 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
56238
diff
changeset
|
2110 switch (expr1->kind) { |
|
45fce310d479
Merged revisions 75223 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
56238
diff
changeset
|
2111 case Name_kind: |
|
45fce310d479
Merged revisions 75223 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
56238
diff
changeset
|
2112 case Attribute_kind: |
|
45fce310d479
Merged revisions 75223 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
56238
diff
changeset
|
2113 case Subscript_kind: |
|
45fce310d479
Merged revisions 75223 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
56238
diff
changeset
|
2114 break; |
|
45fce310d479
Merged revisions 75223 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
56238
diff
changeset
|
2115 default: |
|
45fce310d479
Merged revisions 75223 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
56238
diff
changeset
|
2116 ast_error(ch, "illegal expression for augmented assignment"); |
|
45fce310d479
Merged revisions 75223 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
56238
diff
changeset
|
2117 return NULL; |
|
45fce310d479
Merged revisions 75223 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
56238
diff
changeset
|
2118 } |
| 35742 | 2119 |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2120 ch = CHILD(n, 2); |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2121 if (TYPE(ch) == testlist) |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2122 expr2 = ast_for_testlist(c, ch); |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2123 else |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2124 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
|
2125 if (!expr2) |
| 35742 | 2126 return NULL; |
| 2127 | |
|
46664
031dfe455585
Merged revisions 62425-62429,62434-62436,62441,62444,62446-62448,62450-62455,62463,62465-62466,62469,62474,62476-62478,62480,62485,62492,62497-62498,62500,62507,62513-62514,62516,62521,62531,62535,62545-62546,62548-62551,62553-62559,62569,62574,62577,62593,62595,62604-62606,62608,62616,62626-62627,62636,62638,62644-62645,62647-62648,62651-62653,62656,62661,62663,62680,62686-62687,62696,62699-62703,62711 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
46306
diff
changeset
|
2128 newoperator = ast_for_augassign(c, CHILD(n, 1)); |
|
37879
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36877
diff
changeset
|
2129 if (!newoperator) |
| 35742 | 2130 return NULL; |
| 2131 | |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2132 return AugAssign(expr1, newoperator, expr2, LINENO(n), n->n_col_offset, c->c_arena); |
| 35742 | 2133 } |
| 2134 else { | |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2135 int i; |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2136 asdl_seq *targets; |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2137 node *value; |
| 35742 | 2138 expr_ty expression; |
| 2139 | |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2140 /* a normal assignment */ |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2141 REQ(CHILD(n, 1), EQUAL); |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2142 targets = asdl_seq_new(NCH(n) / 2, c->c_arena); |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2143 if (!targets) |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2144 return NULL; |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2145 for (i = 0; i < NCH(n) - 2; i += 2) { |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2146 expr_ty e; |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2147 node *ch = CHILD(n, i); |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2148 if (TYPE(ch) == yield_expr) { |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2149 ast_error(ch, "assignment to yield expression not possible"); |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2150 return NULL; |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2151 } |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2152 e = ast_for_testlist(c, ch); |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
56951
diff
changeset
|
2153 if (!e) |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2154 return NULL; |
| 35742 | 2155 |
|
64178
5fd26f452600
Check the return values for all functions returning an ast node.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
62373
diff
changeset
|
2156 /* set context to assign */ |
|
46664
031dfe455585
Merged revisions 62425-62429,62434-62436,62441,62444,62446-62448,62450-62455,62463,62465-62466,62469,62474,62476-62478,62480,62485,62492,62497-62498,62500,62507,62513-62514,62516,62521,62531,62535,62545-62546,62548-62551,62553-62559,62569,62574,62577,62593,62595,62604-62606,62608,62616,62626-62627,62636,62638,62644-62645,62647-62648,62651-62653,62656,62661,62663,62680,62686-62687,62696,62699-62703,62711 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
46306
diff
changeset
|
2157 if (!set_context(c, e, Store, CHILD(n, i))) |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2158 return NULL; |
| 35742 | 2159 |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2160 asdl_seq_SET(targets, i / 2, e); |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2161 } |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2162 value = CHILD(n, NCH(n) - 1); |
|
56237
7ee140b7ef93
fix an ambiguity in the grammar from the implementation of extended unpacking
Benjamin Peterson <benjamin@python.org>
parents:
55912
diff
changeset
|
2163 if (TYPE(value) == testlist_star_expr) |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2164 expression = ast_for_testlist(c, value); |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2165 else |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2166 expression = ast_for_expr(c, value); |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2167 if (!expression) |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2168 return NULL; |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2169 return Assign(targets, expression, LINENO(n), n->n_col_offset, c->c_arena); |
| 35742 | 2170 } |
| 2171 } | |
| 2172 | |
|
54918
0704f15b4682
Merged revisions 73376,73393,73398,73400,73404-73405,73409,73419-73421,73432,73457,73460,73485-73486,73488-73489,73501-73502,73513-73514 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
54703
diff
changeset
|
2173 |
| 35742 | 2174 static asdl_seq * |
|
37879
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36877
diff
changeset
|
2175 ast_for_exprlist(struct compiling *c, const node *n, expr_context_ty context) |
| 35742 | 2176 { |
| 2177 asdl_seq *seq; | |
| 2178 int i; | |
| 2179 expr_ty e; | |
| 2180 | |
| 2181 REQ(n, exprlist); | |
| 2182 | |
|
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
|
2183 seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena); |
| 35742 | 2184 if (!seq) |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2185 return NULL; |
| 35742 | 2186 for (i = 0; i < NCH(n); i += 2) { |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2187 e = ast_for_expr(c, CHILD(n, i)); |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2188 if (!e) |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2189 return NULL; |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2190 asdl_seq_SET(seq, i / 2, e); |
|
46664
031dfe455585
Merged revisions 62425-62429,62434-62436,62441,62444,62446-62448,62450-62455,62463,62465-62466,62469,62474,62476-62478,62480,62485,62492,62497-62498,62500,62507,62513-62514,62516,62521,62531,62535,62545-62546,62548-62551,62553-62559,62569,62574,62577,62593,62595,62604-62606,62608,62616,62626-62627,62636,62638,62644-62645,62647-62648,62651-62653,62656,62661,62663,62680,62686-62687,62696,62699-62703,62711 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
46306
diff
changeset
|
2191 if (context && !set_context(c, e, context, CHILD(n, i))) |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2192 return NULL; |
| 35742 | 2193 } |
| 2194 return seq; | |
| 2195 } | |
| 2196 | |
| 2197 static stmt_ty | |
| 2198 ast_for_del_stmt(struct compiling *c, const node *n) | |
| 2199 { | |
| 2200 asdl_seq *expr_list; | |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
56951
diff
changeset
|
2201 |
| 35742 | 2202 /* del_stmt: 'del' exprlist */ |
| 2203 REQ(n, del_stmt); | |
| 2204 | |
| 2205 expr_list = ast_for_exprlist(c, CHILD(n, 1), Del); | |
| 2206 if (!expr_list) | |
| 2207 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
|
2208 return Delete(expr_list, LINENO(n), n->n_col_offset, c->c_arena); |
| 35742 | 2209 } |
| 2210 | |
| 2211 static stmt_ty | |
| 2212 ast_for_flow_stmt(struct compiling *c, const node *n) | |
| 2213 { | |
| 2214 /* | |
| 2215 flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt | |
| 2216 | yield_stmt | |
| 2217 break_stmt: 'break' | |
| 2218 continue_stmt: 'continue' | |
| 2219 return_stmt: 'return' [testlist] | |
| 2220 yield_stmt: yield_expr | |
| 2221 yield_expr: 'yield' testlist | |
| 2222 raise_stmt: 'raise' [test [',' test [',' test]]] | |
| 2223 */ | |
| 2224 node *ch; | |
| 2225 | |
| 2226 REQ(n, flow_stmt); | |
| 2227 ch = CHILD(n, 0); | |
| 2228 switch (TYPE(ch)) { | |
| 2229 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
|
2230 return Break(LINENO(n), n->n_col_offset, c->c_arena); |
| 35742 | 2231 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
|
2232 return Continue(LINENO(n), n->n_col_offset, c->c_arena); |
| 35742 | 2233 case yield_stmt: { /* will reduce to yield_expr */ |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2234 expr_ty exp = ast_for_expr(c, CHILD(ch, 0)); |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2235 if (!exp) |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2236 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
|
2237 return Expr(exp, LINENO(n), n->n_col_offset, c->c_arena); |
| 35742 | 2238 } |
| 2239 case return_stmt: | |
| 2240 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
|
2241 return Return(NULL, LINENO(n), n->n_col_offset, c->c_arena); |
| 35742 | 2242 else { |
|
35798
857f68fba275
Write a separate ast_for_testlist_gexp() function instead of overloading
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35771
diff
changeset
|
2243 expr_ty expression = ast_for_testlist(c, CHILD(ch, 1)); |
| 35742 | 2244 if (!expression) |
| 2245 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
|
2246 return Return(expression, LINENO(n), n->n_col_offset, c->c_arena); |
| 35742 | 2247 } |
| 2248 case raise_stmt: | |
| 2249 if (NCH(ch) == 1) | |
|
43102
981ecc526703
Issue #1066: implement PEP 3109, 2/3 of PEP 3134.
Collin Winter <collinw@gmail.com>
parents:
42597
diff
changeset
|
2250 return Raise(NULL, NULL, LINENO(n), n->n_col_offset, c->c_arena); |
|
981ecc526703
Issue #1066: implement PEP 3109, 2/3 of PEP 3134.
Collin Winter <collinw@gmail.com>
parents:
42597
diff
changeset
|
2251 else if (NCH(ch) >= 2) { |
|
981ecc526703
Issue #1066: implement PEP 3109, 2/3 of PEP 3134.
Collin Winter <collinw@gmail.com>
parents:
42597
diff
changeset
|
2252 expr_ty cause = NULL; |
| 35742 | 2253 expr_ty expression = ast_for_expr(c, CHILD(ch, 1)); |
| 2254 if (!expression) | |
| 2255 return NULL; | |
|
43102
981ecc526703
Issue #1066: implement PEP 3109, 2/3 of PEP 3134.
Collin Winter <collinw@gmail.com>
parents:
42597
diff
changeset
|
2256 if (NCH(ch) == 4) { |
|
981ecc526703
Issue #1066: implement PEP 3109, 2/3 of PEP 3134.
Collin Winter <collinw@gmail.com>
parents:
42597
diff
changeset
|
2257 cause = ast_for_expr(c, CHILD(ch, 3)); |
|
981ecc526703
Issue #1066: implement PEP 3109, 2/3 of PEP 3134.
Collin Winter <collinw@gmail.com>
parents:
42597
diff
changeset
|
2258 if (!cause) |
|
981ecc526703
Issue #1066: implement PEP 3109, 2/3 of PEP 3134.
Collin Winter <collinw@gmail.com>
parents:
42597
diff
changeset
|
2259 return NULL; |
|
981ecc526703
Issue #1066: implement PEP 3109, 2/3 of PEP 3134.
Collin Winter <collinw@gmail.com>
parents:
42597
diff
changeset
|
2260 } |
|
981ecc526703
Issue #1066: implement PEP 3109, 2/3 of PEP 3134.
Collin Winter <collinw@gmail.com>
parents:
42597
diff
changeset
|
2261 return Raise(expression, cause, LINENO(n), n->n_col_offset, c->c_arena); |
| 35742 | 2262 } |
| 2263 default: | |
|
35870
f531c05cd240
Convert all internal errors from Exception to SystemError
Neal Norwitz <nnorwitz@gmail.com>
parents:
35868
diff
changeset
|
2264 PyErr_Format(PyExc_SystemError, |
| 35742 | 2265 "unexpected flow_stmt: %d", TYPE(ch)); |
| 2266 return NULL; | |
| 2267 } | |
|
36072
de16a85c3b4c
Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents:
36066
diff
changeset
|
2268 |
|
de16a85c3b4c
Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents:
36066
diff
changeset
|
2269 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
|
2270 return NULL; |
| 35742 | 2271 } |
| 2272 | |
| 2273 static alias_ty | |
|
54918
0704f15b4682
Merged revisions 73376,73393,73398,73400,73404-73405,73409,73419-73421,73432,73457,73460,73485-73486,73488-73489,73501-73502,73513-73514 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
54703
diff
changeset
|
2274 alias_for_import_name(struct compiling *c, const node *n, int store) |
| 35742 | 2275 { |
| 2276 /* | |
|
36742
658f8ff2a442
Make 'as' an actual keyword when with's future statement is used. Not
Thomas Wouters <thomas@python.org>
parents:
36718
diff
changeset
|
2277 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
|
2278 dotted_as_name: dotted_name ['as' NAME] |
| 35742 | 2279 dotted_name: NAME ('.' NAME)* |
| 2280 */ | |
|
54918
0704f15b4682
Merged revisions 73376,73393,73398,73400,73404-73405,73409,73419-73421,73432,73457,73460,73485-73486,73488-73489,73501-73502,73513-73514 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
54703
diff
changeset
|
2281 identifier 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
|
2282 |
| 35742 | 2283 loop: |
| 2284 switch (TYPE(n)) { | |
|
54918
0704f15b4682
Merged revisions 73376,73393,73398,73400,73404-73405,73409,73419-73421,73432,73457,73460,73485-73486,73488-73489,73501-73502,73513-73514 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
54703
diff
changeset
|
2285 case import_as_name: { |
|
0704f15b4682
Merged revisions 73376,73393,73398,73400,73404-73405,73409,73419-73421,73432,73457,73460,73485-73486,73488-73489,73501-73502,73513-73514 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
54703
diff
changeset
|
2286 node *name_node = CHILD(n, 0); |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
2287 str = NULL; |
|
54918
0704f15b4682
Merged revisions 73376,73393,73398,73400,73404-73405,73409,73419-73421,73432,73457,73460,73485-73486,73488-73489,73501-73502,73513-73514 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
54703
diff
changeset
|
2288 name = NEW_IDENTIFIER(name_node); |
|
0704f15b4682
Merged revisions 73376,73393,73398,73400,73404-73405,73409,73419-73421,73432,73457,73460,73485-73486,73488-73489,73501-73502,73513-73514 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
54703
diff
changeset
|
2289 if (!name) |
|
0704f15b4682
Merged revisions 73376,73393,73398,73400,73404-73405,73409,73419-73421,73432,73457,73460,73485-73486,73488-73489,73501-73502,73513-73514 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
54703
diff
changeset
|
2290 return NULL; |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
2291 if (NCH(n) == 3) { |
|
54918
0704f15b4682
Merged revisions 73376,73393,73398,73400,73404-73405,73409,73419-73421,73432,73457,73460,73485-73486,73488-73489,73501-73502,73513-73514 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
54703
diff
changeset
|
2292 node *str_node = CHILD(n, 2); |
|
0704f15b4682
Merged revisions 73376,73393,73398,73400,73404-73405,73409,73419-73421,73432,73457,73460,73485-73486,73488-73489,73501-73502,73513-73514 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
54703
diff
changeset
|
2293 str = NEW_IDENTIFIER(str_node); |
|
49844
40b7277e665f
Merged revisions 67373 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
49797
diff
changeset
|
2294 if (!str) |
|
40b7277e665f
Merged revisions 67373 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
49797
diff
changeset
|
2295 return NULL; |
|
54954
c45e331238ae
In most cases, the parser will protect True, False, and None from being assign to.
Benjamin Peterson <benjamin@python.org>
parents:
54918
diff
changeset
|
2296 if (store && forbidden_name(str, str_node, 0)) |
|
54918
0704f15b4682
Merged revisions 73376,73393,73398,73400,73404-73405,73409,73419-73421,73432,73457,73460,73485-73486,73488-73489,73501-73502,73513-73514 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
54703
diff
changeset
|
2297 return NULL; |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
2298 } |
|
54918
0704f15b4682
Merged revisions 73376,73393,73398,73400,73404-73405,73409,73419-73421,73432,73457,73460,73485-73486,73488-73489,73501-73502,73513-73514 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
54703
diff
changeset
|
2299 else { |
|
54954
c45e331238ae
In most cases, the parser will protect True, False, and None from being assign to.
Benjamin Peterson <benjamin@python.org>
parents:
54918
diff
changeset
|
2300 if (forbidden_name(name, name_node, 0)) |
|
54918
0704f15b4682
Merged revisions 73376,73393,73398,73400,73404-73405,73409,73419-73421,73432,73457,73460,73485-73486,73488-73489,73501-73502,73513-73514 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
54703
diff
changeset
|
2301 return NULL; |
|
0704f15b4682
Merged revisions 73376,73393,73398,73400,73404-73405,73409,73419-73421,73432,73457,73460,73485-73486,73488-73489,73501-73502,73513-73514 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
54703
diff
changeset
|
2302 } |
|
49844
40b7277e665f
Merged revisions 67373 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
49797
diff
changeset
|
2303 return alias(name, str, c->c_arena); |
|
54918
0704f15b4682
Merged revisions 73376,73393,73398,73400,73404-73405,73409,73419-73421,73432,73457,73460,73485-73486,73488-73489,73501-73502,73513-73514 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
54703
diff
changeset
|
2304 } |
| 35742 | 2305 case dotted_as_name: |
| 2306 if (NCH(n) == 1) { | |
| 2307 n = CHILD(n, 0); | |
| 2308 goto loop; | |
| 2309 } | |
| 2310 else { | |
|
54918
0704f15b4682
Merged revisions 73376,73393,73398,73400,73404-73405,73409,73419-73421,73432,73457,73460,73485-73486,73488-73489,73501-73502,73513-73514 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
54703
diff
changeset
|
2311 node *asname_node = CHILD(n, 2); |
|
0704f15b4682
Merged revisions 73376,73393,73398,73400,73404-73405,73409,73419-73421,73432,73457,73460,73485-73486,73488-73489,73501-73502,73513-73514 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
54703
diff
changeset
|
2312 alias_ty a = alias_for_import_name(c, CHILD(n, 0), 0); |
|
39610
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
39442
diff
changeset
|
2313 if (!a) |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
39442
diff
changeset
|
2314 return NULL; |
| 35742 | 2315 assert(!a->asname); |
|
54918
0704f15b4682
Merged revisions 73376,73393,73398,73400,73404-73405,73409,73419-73421,73432,73457,73460,73485-73486,73488-73489,73501-73502,73513-73514 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
54703
diff
changeset
|
2316 a->asname = NEW_IDENTIFIER(asname_node); |
|
49844
40b7277e665f
Merged revisions 67373 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
49797
diff
changeset
|
2317 if (!a->asname) |
|
40b7277e665f
Merged revisions 67373 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
49797
diff
changeset
|
2318 return NULL; |
|
54954
c45e331238ae
In most cases, the parser will protect True, False, and None from being assign to.
Benjamin Peterson <benjamin@python.org>
parents:
54918
diff
changeset
|
2319 if (forbidden_name(a->asname, asname_node, 0)) |
|
54918
0704f15b4682
Merged revisions 73376,73393,73398,73400,73404-73405,73409,73419-73421,73432,73457,73460,73485-73486,73488-73489,73501-73502,73513-73514 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
54703
diff
changeset
|
2320 return NULL; |
| 35742 | 2321 return a; |
| 2322 } | |
| 2323 break; | |
| 2324 case dotted_name: | |
|
49844
40b7277e665f
Merged revisions 67373 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
49797
diff
changeset
|
2325 if (NCH(n) == 1) { |
|
54918
0704f15b4682
Merged revisions 73376,73393,73398,73400,73404-73405,73409,73419-73421,73432,73457,73460,73485-73486,73488-73489,73501-73502,73513-73514 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
54703
diff
changeset
|
2326 node *name_node = CHILD(n, 0); |
|
0704f15b4682
Merged revisions 73376,73393,73398,73400,73404-73405,73409,73419-73421,73432,73457,73460,73485-73486,73488-73489,73501-73502,73513-73514 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
54703
diff
changeset
|
2327 name = NEW_IDENTIFIER(name_node); |
|
49844
40b7277e665f
Merged revisions 67373 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
49797
diff
changeset
|
2328 if (!name) |
|
40b7277e665f
Merged revisions 67373 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
49797
diff
changeset
|
2329 return NULL; |
|
54954
c45e331238ae
In most cases, the parser will protect True, False, and None from being assign to.
Benjamin Peterson <benjamin@python.org>
parents:
54918
diff
changeset
|
2330 if (store && forbidden_name(name, name_node, 0)) |
|
54918
0704f15b4682
Merged revisions 73376,73393,73398,73400,73404-73405,73409,73419-73421,73432,73457,73460,73485-73486,73488-73489,73501-73502,73513-73514 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
54703
diff
changeset
|
2331 return NULL; |
|
49844
40b7277e665f
Merged revisions 67373 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
49797
diff
changeset
|
2332 return alias(name, NULL, c->c_arena); |
|
40b7277e665f
Merged revisions 67373 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
49797
diff
changeset
|
2333 } |
| 35742 | 2334 else { |
| 2335 /* 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
|
2336 int i; |
|
36241
557e9b6ee7e1
alias_for_import_name(): Dueling compiler warnings ;-)
Tim Peters <tim.peters@gmail.com>
parents:
36231
diff
changeset
|
2337 size_t len; |
| 35742 | 2338 char *s; |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
56951
diff
changeset
|
2339 PyObject *uni; |
| 35742 | 2340 |
| 2341 len = 0; | |
| 2342 for (i = 0; i < NCH(n); i += 2) | |
| 2343 /* length of string plus one for the dot */ | |
| 2344 len += strlen(STR(CHILD(n, i))) + 1; | |
| 2345 len--; /* the last name doesn't have a dot */ | |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
46664
diff
changeset
|
2346 str = PyBytes_FromStringAndSize(NULL, len); |
| 35742 | 2347 if (!str) |
| 2348 return NULL; | |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
46664
diff
changeset
|
2349 s = PyBytes_AS_STRING(str); |
| 35742 | 2350 if (!s) |
| 2351 return NULL; | |
| 2352 for (i = 0; i < NCH(n); i += 2) { | |
| 2353 char *sch = STR(CHILD(n, i)); | |
| 2354 strcpy(s, STR(CHILD(n, i))); | |
| 2355 s += strlen(sch); | |
| 2356 *s++ = '.'; | |
| 2357 } | |
| 2358 --s; | |
| 2359 *s = '\0'; | |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
56951
diff
changeset
|
2360 uni = PyUnicode_DecodeUTF8(PyBytes_AS_STRING(str), |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
56951
diff
changeset
|
2361 PyBytes_GET_SIZE(str), |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
56951
diff
changeset
|
2362 NULL); |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
56951
diff
changeset
|
2363 Py_DECREF(str); |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
56951
diff
changeset
|
2364 if (!uni) |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
56951
diff
changeset
|
2365 return NULL; |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
56951
diff
changeset
|
2366 str = uni; |
|
42013
cd125fe83051
Make identifiers str (not str8) objects throughout.
Martin v. Löwis <martin@v.loewis.de>
parents:
41969
diff
changeset
|
2367 PyUnicode_InternInPlace(&str); |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2368 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
|
2369 return alias(str, NULL, c->c_arena); |
| 35742 | 2370 } |
| 2371 break; | |
| 2372 case STAR: | |
|
42013
cd125fe83051
Make identifiers str (not str8) objects throughout.
Martin v. Löwis <martin@v.loewis.de>
parents:
41969
diff
changeset
|
2373 str = PyUnicode_InternFromString("*"); |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2374 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
|
2375 return alias(str, NULL, c->c_arena); |
| 35742 | 2376 default: |
|
35870
f531c05cd240
Convert all internal errors from Exception to SystemError
Neal Norwitz <nnorwitz@gmail.com>
parents:
35868
diff
changeset
|
2377 PyErr_Format(PyExc_SystemError, |
| 35742 | 2378 "unexpected import name: %d", TYPE(n)); |
| 2379 return NULL; | |
| 2380 } | |
|
36072
de16a85c3b4c
Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents:
36066
diff
changeset
|
2381 |
|
de16a85c3b4c
Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents:
36066
diff
changeset
|
2382 PyErr_SetString(PyExc_SystemError, "unhandled import name condition"); |
| 35742 | 2383 return NULL; |
| 2384 } | |
| 2385 | |
| 2386 static stmt_ty | |
| 2387 ast_for_import_stmt(struct compiling *c, const node *n) | |
| 2388 { | |
| 2389 /* | |
| 2390 import_stmt: import_name | import_from | |
| 2391 import_name: 'import' dotted_as_names | |
|
41235
6c8c33a43a15
"from ... import x" should not be a syntax error... make
Georg Brandl <georg@python.org>
parents:
41230
diff
changeset
|
2392 import_from: 'from' (('.' | '...')* dotted_name | ('.' | '...')+) |
|
6c8c33a43a15
"from ... import x" should not be a syntax error... make
Georg Brandl <georg@python.org>
parents:
41230
diff
changeset
|
2393 'import' ('*' | '(' import_as_names ')' | import_as_names) |
| 35742 | 2394 */ |
|
36786
57322cb2ca11
Patch #1440601: Add col_offset attribute to AST nodes.
Martin v. Löwis <martin@v.loewis.de>
parents:
36780
diff
changeset
|
2395 int lineno; |
|
57322cb2ca11
Patch #1440601: Add col_offset attribute to AST nodes.
Martin v. Löwis <martin@v.loewis.de>
parents:
36780
diff
changeset
|
2396 int col_offset; |
| 35742 | 2397 int i; |
| 2398 asdl_seq *aliases; | |
| 2399 | |
| 2400 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
|
2401 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
|
2402 col_offset = n->n_col_offset; |
| 35742 | 2403 n = CHILD(n, 0); |
|
36682
52cbc447e1c9
And some more cleanup.
Thomas Wouters <thomas@python.org>
parents:
36681
diff
changeset
|
2404 if (TYPE(n) == import_name) { |
| 35742 | 2405 n = CHILD(n, 1); |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2406 REQ(n, dotted_as_names); |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2407 aliases = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena); |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2408 if (!aliases) |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2409 return NULL; |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2410 for (i = 0; i < NCH(n); i += 2) { |
|
54918
0704f15b4682
Merged revisions 73376,73393,73398,73400,73404-73405,73409,73419-73421,73432,73457,73460,73485-73486,73488-73489,73501-73502,73513-73514 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
54703
diff
changeset
|
2411 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
|
2412 if (!import_alias) |
| 35742 | 2413 return NULL; |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2414 asdl_seq_SET(aliases, i / 2, import_alias); |
| 35742 | 2415 } |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2416 return Import(aliases, lineno, col_offset, c->c_arena); |
| 35742 | 2417 } |
|
36682
52cbc447e1c9
And some more cleanup.
Thomas Wouters <thomas@python.org>
parents:
36681
diff
changeset
|
2418 else if (TYPE(n) == import_from) { |
| 35742 | 2419 int n_children; |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2420 int idx, ndots = 0; |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2421 alias_ty mod = NULL; |
|
54918
0704f15b4682
Merged revisions 73376,73393,73398,73400,73404-73405,73409,73419-73421,73432,73457,73460,73485-73486,73488-73489,73501-73502,73513-73514 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
54703
diff
changeset
|
2422 identifier modname = NULL; |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
56951
diff
changeset
|
2423 |
|
36712
5c36c222e7ca
SF patch #1438387, PEP 328: relative and absolute imports.
Thomas Wouters <thomas@python.org>
parents:
36695
diff
changeset
|
2424 /* 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
|
2425 optional module name */ |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2426 for (idx = 1; idx < NCH(n); idx++) { |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2427 if (TYPE(CHILD(n, idx)) == dotted_name) { |
|
54918
0704f15b4682
Merged revisions 73376,73393,73398,73400,73404-73405,73409,73419-73421,73432,73457,73460,73485-73486,73488-73489,73501-73502,73513-73514 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
54703
diff
changeset
|
2428 mod = alias_for_import_name(c, CHILD(n, idx), 0); |
|
0704f15b4682
Merged revisions 73376,73393,73398,73400,73404-73405,73409,73419-73421,73432,73457,73460,73485-73486,73488-73489,73501-73502,73513-73514 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
54703
diff
changeset
|
2429 if (!mod) |
|
0704f15b4682
Merged revisions 73376,73393,73398,73400,73404-73405,73409,73419-73421,73432,73457,73460,73485-73486,73488-73489,73501-73502,73513-73514 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
54703
diff
changeset
|
2430 return NULL; |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2431 idx++; |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2432 break; |
|
41235
6c8c33a43a15
"from ... import x" should not be a syntax error... make
Georg Brandl <georg@python.org>
parents:
41230
diff
changeset
|
2433 } else if (TYPE(CHILD(n, idx)) == ELLIPSIS) { |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
56951
diff
changeset
|
2434 /* three consecutive dots are tokenized as one ELLIPSIS */ |
|
41235
6c8c33a43a15
"from ... import x" should not be a syntax error... make
Georg Brandl <georg@python.org>
parents:
41230
diff
changeset
|
2435 ndots += 3; |
|
6c8c33a43a15
"from ... import x" should not be a syntax error... make
Georg Brandl <georg@python.org>
parents:
41230
diff
changeset
|
2436 continue; |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2437 } else if (TYPE(CHILD(n, idx)) != DOT) { |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2438 break; |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2439 } |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2440 ndots++; |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2441 } |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2442 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
|
2443 switch (TYPE(CHILD(n, idx))) { |
|
36681
e9f0db429f2b
Clean up from-import handling.
Thomas Wouters <thomas@python.org>
parents:
36679
diff
changeset
|
2444 case STAR: |
|
e9f0db429f2b
Clean up from-import handling.
Thomas Wouters <thomas@python.org>
parents:
36679
diff
changeset
|
2445 /* from ... import * */ |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2446 n = CHILD(n, idx); |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2447 n_children = 1; |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2448 break; |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2449 case LPAR: |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2450 /* from ... import (x, y, z) */ |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2451 n = CHILD(n, idx + 1); |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2452 n_children = NCH(n); |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2453 break; |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2454 case import_as_names: |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2455 /* from ... import x, y, z */ |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2456 n = CHILD(n, idx); |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2457 n_children = NCH(n); |
|
36681
e9f0db429f2b
Clean up from-import handling.
Thomas Wouters <thomas@python.org>
parents:
36679
diff
changeset
|
2458 if (n_children % 2 == 0) { |
| 35742 | 2459 ast_error(n, "trailing comma not allowed without" |
| 2460 " surrounding parentheses"); | |
| 2461 return NULL; | |
| 2462 } | |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2463 break; |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2464 default: |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2465 ast_error(n, "Unexpected node-type in from-import"); |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2466 return NULL; |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2467 } |
| 35742 | 2468 |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2469 aliases = asdl_seq_new((n_children + 1) / 2, c->c_arena); |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2470 if (!aliases) |
| 35742 | 2471 return NULL; |
| 2472 | |
| 2473 /* 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
|
2474 if (TYPE(n) == STAR) { |
|
54918
0704f15b4682
Merged revisions 73376,73393,73398,73400,73404-73405,73409,73419-73421,73432,73457,73460,73485-73486,73488-73489,73501-73502,73513-73514 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
54703
diff
changeset
|
2475 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
|
2476 if (!import_alias) |
| 35742 | 2477 return NULL; |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2478 asdl_seq_SET(aliases, 0, import_alias); |
| 35742 | 2479 } |
|
36718
45e6f901902c
Remove asdl_seq_APPEND() and simplify asdl seq implementation.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36712
diff
changeset
|
2480 else { |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2481 for (i = 0; i < NCH(n); i += 2) { |
|
54918
0704f15b4682
Merged revisions 73376,73393,73398,73400,73404-73405,73409,73419-73421,73432,73457,73460,73485-73486,73488-73489,73501-73502,73513-73514 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
54703
diff
changeset
|
2482 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
|
2483 if (!import_alias) |
|
45e6f901902c
Remove asdl_seq_APPEND() and simplify asdl seq implementation.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36712
diff
changeset
|
2484 return NULL; |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2485 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
|
2486 } |
| 35742 | 2487 } |
|
36712
5c36c222e7ca
SF patch #1438387, PEP 328: relative and absolute imports.
Thomas Wouters <thomas@python.org>
parents:
36695
diff
changeset
|
2488 if (mod != NULL) |
|
5c36c222e7ca
SF patch #1438387, PEP 328: relative and absolute imports.
Thomas Wouters <thomas@python.org>
parents:
36695
diff
changeset
|
2489 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
|
2490 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
|
2491 c->c_arena); |
| 35742 | 2492 } |
|
35870
f531c05cd240
Convert all internal errors from Exception to SystemError
Neal Norwitz <nnorwitz@gmail.com>
parents:
35868
diff
changeset
|
2493 PyErr_Format(PyExc_SystemError, |
| 35742 | 2494 "unknown import statement: starts with command '%s'", |
| 2495 STR(CHILD(n, 0))); | |
| 2496 return NULL; | |
| 2497 } | |
| 2498 | |
| 2499 static stmt_ty | |
| 2500 ast_for_global_stmt(struct compiling *c, const node *n) | |
| 2501 { | |
| 2502 /* global_stmt: 'global' NAME (',' NAME)* */ | |
| 2503 identifier name; | |
| 2504 asdl_seq *s; | |
| 2505 int i; | |
| 2506 | |
| 2507 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
|
2508 s = asdl_seq_new(NCH(n) / 2, c->c_arena); |
| 35742 | 2509 if (!s) |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2510 return NULL; |
| 35742 | 2511 for (i = 1; i < NCH(n); i += 2) { |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2512 name = NEW_IDENTIFIER(CHILD(n, i)); |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2513 if (!name) |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2514 return NULL; |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2515 asdl_seq_SET(s, i / 2, name); |
| 35742 | 2516 } |
|
36786
57322cb2ca11
Patch #1440601: Add col_offset attribute to AST nodes.
Martin v. Löwis <martin@v.loewis.de>
parents:
36780
diff
changeset
|
2517 return Global(s, LINENO(n), n->n_col_offset, c->c_arena); |
| 35742 | 2518 } |
| 2519 | |
| 2520 static stmt_ty | |
|
40965
30e5cb3a55ac
Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
40941
diff
changeset
|
2521 ast_for_nonlocal_stmt(struct compiling *c, const node *n) |
|
30e5cb3a55ac
Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
40941
diff
changeset
|
2522 { |
|
30e5cb3a55ac
Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
40941
diff
changeset
|
2523 /* nonlocal_stmt: 'nonlocal' NAME (',' NAME)* */ |
|
30e5cb3a55ac
Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
40941
diff
changeset
|
2524 identifier name; |
|
30e5cb3a55ac
Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
40941
diff
changeset
|
2525 asdl_seq *s; |
|
30e5cb3a55ac
Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
40941
diff
changeset
|
2526 int i; |
|
30e5cb3a55ac
Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
40941
diff
changeset
|
2527 |
|
30e5cb3a55ac
Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
40941
diff
changeset
|
2528 REQ(n, nonlocal_stmt); |
|
30e5cb3a55ac
Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
40941
diff
changeset
|
2529 s = asdl_seq_new(NCH(n) / 2, c->c_arena); |
|
30e5cb3a55ac
Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
40941
diff
changeset
|
2530 if (!s) |
|
30e5cb3a55ac
Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
40941
diff
changeset
|
2531 return NULL; |
|
30e5cb3a55ac
Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
40941
diff
changeset
|
2532 for (i = 1; i < NCH(n); i += 2) { |
|
30e5cb3a55ac
Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
40941
diff
changeset
|
2533 name = NEW_IDENTIFIER(CHILD(n, i)); |
|
30e5cb3a55ac
Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
40941
diff
changeset
|
2534 if (!name) |
|
30e5cb3a55ac
Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
40941
diff
changeset
|
2535 return NULL; |
|
30e5cb3a55ac
Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
40941
diff
changeset
|
2536 asdl_seq_SET(s, i / 2, name); |
|
30e5cb3a55ac
Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
40941
diff
changeset
|
2537 } |
|
30e5cb3a55ac
Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
40941
diff
changeset
|
2538 return Nonlocal(s, LINENO(n), n->n_col_offset, c->c_arena); |
|
30e5cb3a55ac
Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
40941
diff
changeset
|
2539 } |
|
30e5cb3a55ac
Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
40941
diff
changeset
|
2540 |
|
30e5cb3a55ac
Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
40941
diff
changeset
|
2541 static stmt_ty |
| 35742 | 2542 ast_for_assert_stmt(struct compiling *c, const node *n) |
| 2543 { | |
| 2544 /* assert_stmt: 'assert' test [',' test] */ | |
| 2545 REQ(n, assert_stmt); | |
| 2546 if (NCH(n) == 2) { | |
| 2547 expr_ty expression = ast_for_expr(c, CHILD(n, 1)); | |
| 2548 if (!expression) | |
| 2549 return NULL; | |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2550 return Assert(expression, NULL, LINENO(n), n->n_col_offset, c->c_arena); |
| 35742 | 2551 } |
| 2552 else if (NCH(n) == 4) { | |
| 2553 expr_ty expr1, expr2; | |
| 2554 | |
| 2555 expr1 = ast_for_expr(c, CHILD(n, 1)); | |
| 2556 if (!expr1) | |
| 2557 return NULL; | |
| 2558 expr2 = ast_for_expr(c, CHILD(n, 3)); | |
| 2559 if (!expr2) | |
| 2560 return NULL; | |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
56951
diff
changeset
|
2561 |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2562 return Assert(expr1, expr2, LINENO(n), n->n_col_offset, c->c_arena); |
| 35742 | 2563 } |
|
35870
f531c05cd240
Convert all internal errors from Exception to SystemError
Neal Norwitz <nnorwitz@gmail.com>
parents:
35868
diff
changeset
|
2564 PyErr_Format(PyExc_SystemError, |
| 35742 | 2565 "improper number of parts to 'assert' statement: %d", |
| 2566 NCH(n)); | |
| 2567 return NULL; | |
| 2568 } | |
| 2569 | |
| 2570 static asdl_seq * | |
| 2571 ast_for_suite(struct compiling *c, const node *n) | |
| 2572 { | |
| 2573 /* 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
|
2574 asdl_seq *seq; |
| 35742 | 2575 stmt_ty s; |
| 2576 int i, total, num, end, pos = 0; | |
| 2577 node *ch; | |
| 2578 | |
| 2579 REQ(n, suite); | |
| 2580 | |
| 2581 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
|
2582 seq = asdl_seq_new(total, c->c_arena); |
| 35742 | 2583 if (!seq) |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2584 return NULL; |
| 35742 | 2585 if (TYPE(CHILD(n, 0)) == simple_stmt) { |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2586 n = CHILD(n, 0); |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2587 /* simple_stmt always ends with a NEWLINE, |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
56951
diff
changeset
|
2588 and may have a trailing SEMI |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2589 */ |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2590 end = NCH(n) - 1; |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2591 if (TYPE(CHILD(n, end - 1)) == SEMI) |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2592 end--; |
| 35742 | 2593 /* loop by 2 to skip semi-colons */ |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2594 for (i = 0; i < end; i += 2) { |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2595 ch = CHILD(n, i); |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2596 s = ast_for_stmt(c, ch); |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2597 if (!s) |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2598 return NULL; |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2599 asdl_seq_SET(seq, pos++, s); |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2600 } |
| 35742 | 2601 } |
| 2602 else { | |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2603 for (i = 2; i < (NCH(n) - 1); i++) { |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2604 ch = CHILD(n, i); |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2605 REQ(ch, stmt); |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2606 num = num_stmts(ch); |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2607 if (num == 1) { |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2608 /* small_stmt or compound_stmt with only one child */ |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2609 s = ast_for_stmt(c, ch); |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2610 if (!s) |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2611 return NULL; |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2612 asdl_seq_SET(seq, pos++, s); |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2613 } |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2614 else { |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2615 int j; |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2616 ch = CHILD(ch, 0); |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2617 REQ(ch, simple_stmt); |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2618 for (j = 0; j < NCH(ch); j += 2) { |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2619 /* statement terminates with a semi-colon ';' */ |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2620 if (NCH(CHILD(ch, j)) == 0) { |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2621 assert((j + 1) == NCH(ch)); |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2622 break; |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2623 } |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2624 s = ast_for_stmt(c, CHILD(ch, j)); |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2625 if (!s) |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2626 return NULL; |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2627 asdl_seq_SET(seq, pos++, s); |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2628 } |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2629 } |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2630 } |
| 35742 | 2631 } |
| 2632 assert(pos == seq->size); | |
| 2633 return seq; | |
| 2634 } | |
| 2635 | |
| 2636 static stmt_ty | |
| 2637 ast_for_if_stmt(struct compiling *c, const node *n) | |
| 2638 { | |
| 2639 /* if_stmt: 'if' test ':' suite ('elif' test ':' suite)* | |
| 2640 ['else' ':' suite] | |
| 2641 */ | |
| 2642 char *s; | |
| 2643 | |
| 2644 REQ(n, if_stmt); | |
| 2645 | |
| 2646 if (NCH(n) == 4) { | |
| 2647 expr_ty expression; | |
| 2648 asdl_seq *suite_seq; | |
| 2649 | |
| 2650 expression = ast_for_expr(c, CHILD(n, 1)); | |
| 2651 if (!expression) | |
| 2652 return NULL; | |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
56951
diff
changeset
|
2653 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
|
2654 if (!suite_seq) |
| 35742 | 2655 return NULL; |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
56951
diff
changeset
|
2656 |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
2657 return If(expression, suite_seq, NULL, LINENO(n), n->n_col_offset, |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
2658 c->c_arena); |
| 35742 | 2659 } |
|
36072
de16a85c3b4c
Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents:
36066
diff
changeset
|
2660 |
| 35742 | 2661 s = STR(CHILD(n, 4)); |
| 2662 /* s[2], the third character in the string, will be | |
| 2663 's' for el_s_e, or | |
| 2664 'i' for el_i_f | |
| 2665 */ | |
| 2666 if (s[2] == 's') { | |
| 2667 expr_ty expression; | |
| 2668 asdl_seq *seq1, *seq2; | |
| 2669 | |
| 2670 expression = ast_for_expr(c, CHILD(n, 1)); | |
| 2671 if (!expression) | |
| 2672 return NULL; | |
| 2673 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
|
2674 if (!seq1) |
| 35742 | 2675 return NULL; |
| 2676 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
|
2677 if (!seq2) |
| 35742 | 2678 return NULL; |
| 2679 | |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
2680 return If(expression, seq1, seq2, LINENO(n), n->n_col_offset, |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
2681 c->c_arena); |
| 35742 | 2682 } |
| 2683 else if (s[2] == 'i') { | |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2684 int i, n_elif, has_else = 0; |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
2685 expr_ty expression; |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
2686 asdl_seq *suite_seq; |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2687 asdl_seq *orelse = NULL; |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2688 n_elif = NCH(n) - 4; |
| 35742 | 2689 /* must reference the child n_elif+1 since 'else' token is third, |
| 2690 not fourth, child from the end. */ | |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2691 if (TYPE(CHILD(n, (n_elif + 1))) == NAME |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2692 && STR(CHILD(n, (n_elif + 1)))[2] == 's') { |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2693 has_else = 1; |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2694 n_elif -= 3; |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2695 } |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2696 n_elif /= 4; |
| 35742 | 2697 |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2698 if (has_else) { |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
2699 asdl_seq *suite_seq2; |
| 35742 | 2700 |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2701 orelse = asdl_seq_new(1, c->c_arena); |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2702 if (!orelse) |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2703 return NULL; |
| 35742 | 2704 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
|
2705 if (!expression) |
| 35742 | 2706 return NULL; |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
2707 suite_seq = ast_for_suite(c, CHILD(n, NCH(n) - 4)); |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
2708 if (!suite_seq) |
|
36072
de16a85c3b4c
Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents:
36066
diff
changeset
|
2709 return NULL; |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
2710 suite_seq2 = ast_for_suite(c, CHILD(n, NCH(n) - 1)); |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
2711 if (!suite_seq2) |
| 35742 | 2712 return NULL; |
| 2713 | |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
56951
diff
changeset
|
2714 asdl_seq_SET(orelse, 0, |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
56951
diff
changeset
|
2715 If(expression, suite_seq, suite_seq2, |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
2716 LINENO(CHILD(n, NCH(n) - 6)), |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
2717 CHILD(n, NCH(n) - 6)->n_col_offset, |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
2718 c->c_arena)); |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2719 /* the just-created orelse handled the last elif */ |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2720 n_elif--; |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2721 } |
| 35742 | 2722 |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2723 for (i = 0; i < n_elif; i++) { |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2724 int off = 5 + (n_elif - i - 1) * 4; |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2725 asdl_seq *newobj = asdl_seq_new(1, c->c_arena); |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2726 if (!newobj) |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2727 return NULL; |
| 35742 | 2728 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
|
2729 if (!expression) |
| 35742 | 2730 return NULL; |
| 2731 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
|
2732 if (!suite_seq) |
| 35742 | 2733 return NULL; |
| 2734 | |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2735 asdl_seq_SET(newobj, 0, |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
56951
diff
changeset
|
2736 If(expression, suite_seq, orelse, |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
2737 LINENO(CHILD(n, off)), |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
2738 CHILD(n, off)->n_col_offset, c->c_arena)); |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2739 orelse = newobj; |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2740 } |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
2741 expression = ast_for_expr(c, CHILD(n, 1)); |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
2742 if (!expression) |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
2743 return NULL; |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
2744 suite_seq = ast_for_suite(c, CHILD(n, 3)); |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
2745 if (!suite_seq) |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
2746 return NULL; |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
2747 return If(expression, suite_seq, orelse, |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
2748 LINENO(n), n->n_col_offset, c->c_arena); |
| 35742 | 2749 } |
|
36072
de16a85c3b4c
Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents:
36066
diff
changeset
|
2750 |
|
de16a85c3b4c
Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents:
36066
diff
changeset
|
2751 PyErr_Format(PyExc_SystemError, |
|
de16a85c3b4c
Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents:
36066
diff
changeset
|
2752 "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
|
2753 return NULL; |
| 35742 | 2754 } |
| 2755 | |
| 2756 static stmt_ty | |
| 2757 ast_for_while_stmt(struct compiling *c, const node *n) | |
| 2758 { | |
| 2759 /* while_stmt: 'while' test ':' suite ['else' ':' suite] */ | |
| 2760 REQ(n, while_stmt); | |
| 2761 | |
| 2762 if (NCH(n) == 4) { | |
| 2763 expr_ty expression; | |
| 2764 asdl_seq *suite_seq; | |
| 2765 | |
| 2766 expression = ast_for_expr(c, CHILD(n, 1)); | |
| 2767 if (!expression) | |
| 2768 return NULL; | |
| 2769 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
|
2770 if (!suite_seq) |
| 35742 | 2771 return NULL; |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2772 return While(expression, suite_seq, NULL, LINENO(n), n->n_col_offset, c->c_arena); |
| 35742 | 2773 } |
| 2774 else if (NCH(n) == 7) { | |
| 2775 expr_ty expression; | |
| 2776 asdl_seq *seq1, *seq2; | |
| 2777 | |
| 2778 expression = ast_for_expr(c, CHILD(n, 1)); | |
| 2779 if (!expression) | |
| 2780 return NULL; | |
| 2781 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
|
2782 if (!seq1) |
| 35742 | 2783 return NULL; |
| 2784 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
|
2785 if (!seq2) |
| 35742 | 2786 return NULL; |
| 2787 | |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2788 return While(expression, seq1, seq2, LINENO(n), n->n_col_offset, c->c_arena); |
| 35742 | 2789 } |
|
36072
de16a85c3b4c
Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents:
36066
diff
changeset
|
2790 |
|
de16a85c3b4c
Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents:
36066
diff
changeset
|
2791 PyErr_Format(PyExc_SystemError, |
|
de16a85c3b4c
Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents:
36066
diff
changeset
|
2792 "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
|
2793 NCH(n)); |
|
de16a85c3b4c
Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents:
36066
diff
changeset
|
2794 return NULL; |
| 35742 | 2795 } |
| 2796 | |
| 2797 static stmt_ty | |
| 2798 ast_for_for_stmt(struct compiling *c, const node *n) | |
| 2799 { | |
|
36072
de16a85c3b4c
Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents:
36066
diff
changeset
|
2800 asdl_seq *_target, *seq = NULL, *suite_seq; |
| 35742 | 2801 expr_ty expression; |
|
55912
d4e3af343e32
Merged revisions 74464 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
54954
diff
changeset
|
2802 expr_ty target, first; |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
2803 const node *node_target; |
| 35742 | 2804 /* for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite] */ |
| 2805 REQ(n, for_stmt); | |
| 2806 | |
| 2807 if (NCH(n) == 9) { | |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2808 seq = ast_for_suite(c, CHILD(n, 8)); |
| 35742 | 2809 if (!seq) |
| 2810 return NULL; | |
| 2811 } | |
| 2812 | |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
2813 node_target = CHILD(n, 1); |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
2814 _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
|
2815 if (!_target) |
| 35742 | 2816 return NULL; |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
2817 /* Check the # of children rather than the length of _target, since |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
2818 for x, in ... has 1 element in _target, but still requires a Tuple. */ |
|
55912
d4e3af343e32
Merged revisions 74464 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
54954
diff
changeset
|
2819 first = (expr_ty)asdl_seq_GET(_target, 0); |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
2820 if (NCH(node_target) == 1) |
|
55912
d4e3af343e32
Merged revisions 74464 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
54954
diff
changeset
|
2821 target = first; |
| 35742 | 2822 else |
|
55912
d4e3af343e32
Merged revisions 74464 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
54954
diff
changeset
|
2823 target = Tuple(_target, Store, first->lineno, first->col_offset, c->c_arena); |
| 35742 | 2824 |
|
35798
857f68fba275
Write a separate ast_for_testlist_gexp() function instead of overloading
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35771
diff
changeset
|
2825 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
|
2826 if (!expression) |
| 35742 | 2827 return NULL; |
| 2828 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
|
2829 if (!suite_seq) |
| 35742 | 2830 return NULL; |
| 2831 | |
|
37879
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36877
diff
changeset
|
2832 return For(target, expression, suite_seq, seq, LINENO(n), n->n_col_offset, |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36877
diff
changeset
|
2833 c->c_arena); |
| 35742 | 2834 } |
| 2835 | |
| 2836 static excepthandler_ty | |
| 2837 ast_for_except_clause(struct compiling *c, const node *exc, node *body) | |
| 2838 { | |
|
41824
24b60622b944
Merged revisions 55407-55513 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41717
diff
changeset
|
2839 /* except_clause: 'except' [test ['as' test]] */ |
| 35742 | 2840 REQ(exc, except_clause); |
| 2841 REQ(body, suite); | |
| 2842 | |
| 2843 if (NCH(exc) == 1) { | |
| 2844 asdl_seq *suite_seq = ast_for_suite(c, body); | |
| 2845 if (!suite_seq) | |
| 2846 return NULL; | |
| 2847 | |
|
46231
81ee270a6eb5
Merged revisions 62047 via svnmerge from
Neal Norwitz <nnorwitz@gmail.com>
parents:
46120
diff
changeset
|
2848 return ExceptHandler(NULL, NULL, suite_seq, LINENO(exc), |
|
37879
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36877
diff
changeset
|
2849 exc->n_col_offset, c->c_arena); |
| 35742 | 2850 } |
| 2851 else if (NCH(exc) == 2) { | |
| 2852 expr_ty expression; | |
| 2853 asdl_seq *suite_seq; | |
| 2854 | |
| 2855 expression = ast_for_expr(c, CHILD(exc, 1)); | |
| 2856 if (!expression) | |
| 2857 return NULL; | |
| 2858 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
|
2859 if (!suite_seq) |
| 35742 | 2860 return NULL; |
| 2861 | |
|
46231
81ee270a6eb5
Merged revisions 62047 via svnmerge from
Neal Norwitz <nnorwitz@gmail.com>
parents:
46120
diff
changeset
|
2862 return ExceptHandler(expression, NULL, suite_seq, LINENO(exc), |
|
37879
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36877
diff
changeset
|
2863 exc->n_col_offset, c->c_arena); |
| 35742 | 2864 } |
| 2865 else if (NCH(exc) == 4) { | |
| 2866 asdl_seq *suite_seq; | |
| 2867 expr_ty expression; | |
|
40588
4bab65fcca8e
Some more changes related to the new except syntax and semantics,
Guido van Rossum <guido@python.org>
parents:
40529
diff
changeset
|
2868 identifier e = NEW_IDENTIFIER(CHILD(exc, 3)); |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2869 if (!e) |
| 35742 | 2870 return NULL; |
|
54954
c45e331238ae
In most cases, the parser will protect True, False, and None from being assign to.
Benjamin Peterson <benjamin@python.org>
parents:
54918
diff
changeset
|
2871 if (forbidden_name(e, CHILD(exc, 3), 0)) |
|
c45e331238ae
In most cases, the parser will protect True, False, and None from being assign to.
Benjamin Peterson <benjamin@python.org>
parents:
54918
diff
changeset
|
2872 return NULL; |
|
36072
de16a85c3b4c
Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents:
36066
diff
changeset
|
2873 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
|
2874 if (!expression) |
|
de16a85c3b4c
Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents:
36066
diff
changeset
|
2875 return NULL; |
| 35742 | 2876 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
|
2877 if (!suite_seq) |
| 35742 | 2878 return NULL; |
| 2879 | |
|
46231
81ee270a6eb5
Merged revisions 62047 via svnmerge from
Neal Norwitz <nnorwitz@gmail.com>
parents:
46120
diff
changeset
|
2880 return ExceptHandler(expression, e, suite_seq, LINENO(exc), |
|
37879
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36877
diff
changeset
|
2881 exc->n_col_offset, c->c_arena); |
| 35742 | 2882 } |
|
36072
de16a85c3b4c
Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents:
36066
diff
changeset
|
2883 |
|
de16a85c3b4c
Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents:
36066
diff
changeset
|
2884 PyErr_Format(PyExc_SystemError, |
|
de16a85c3b4c
Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents:
36066
diff
changeset
|
2885 "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
|
2886 NCH(exc)); |
|
de16a85c3b4c
Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents:
36066
diff
changeset
|
2887 return NULL; |
| 35742 | 2888 } |
| 2889 | |
| 2890 static stmt_ty | |
| 2891 ast_for_try_stmt(struct compiling *c, const node *n) | |
| 2892 { | |
|
36066
719ba02d2e2c
SF patch #1355913, PEP 341 - Unification of try/except and try/finally
Neal Norwitz <nnorwitz@gmail.com>
parents:
36065
diff
changeset
|
2893 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
|
2894 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
|
2895 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
|
2896 |
| 35742 | 2897 REQ(n, try_stmt); |
| 2898 | |
|
36066
719ba02d2e2c
SF patch #1355913, PEP 341 - Unification of try/except and try/finally
Neal Norwitz <nnorwitz@gmail.com>
parents:
36065
diff
changeset
|
2899 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
|
2900 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
|
2901 return NULL; |
|
719ba02d2e2c
SF patch #1355913, PEP 341 - Unification of try/except and try/finally
Neal Norwitz <nnorwitz@gmail.com>
parents:
36065
diff
changeset
|
2902 |
|
719ba02d2e2c
SF patch #1355913, PEP 341 - Unification of try/except and try/finally
Neal Norwitz <nnorwitz@gmail.com>
parents:
36065
diff
changeset
|
2903 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
|
2904 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
|
2905 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
|
2906 /* 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
|
2907 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
|
2908 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
|
2909 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
|
2910 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
|
2911 return NULL; |
|
719ba02d2e2c
SF patch #1355913, PEP 341 - Unification of try/except and try/finally
Neal Norwitz <nnorwitz@gmail.com>
parents:
36065
diff
changeset
|
2912 n_except--; |
|
719ba02d2e2c
SF patch #1355913, PEP 341 - Unification of try/except and try/finally
Neal Norwitz <nnorwitz@gmail.com>
parents:
36065
diff
changeset
|
2913 } |
|
719ba02d2e2c
SF patch #1355913, PEP 341 - Unification of try/except and try/finally
Neal Norwitz <nnorwitz@gmail.com>
parents:
36065
diff
changeset
|
2914 |
|
719ba02d2e2c
SF patch #1355913, PEP 341 - Unification of try/except and try/finally
Neal Norwitz <nnorwitz@gmail.com>
parents:
36065
diff
changeset
|
2915 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
|
2916 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
|
2917 return NULL; |
|
719ba02d2e2c
SF patch #1355913, PEP 341 - Unification of try/except and try/finally
Neal Norwitz <nnorwitz@gmail.com>
parents:
36065
diff
changeset
|
2918 n_except--; |
|
719ba02d2e2c
SF patch #1355913, PEP 341 - Unification of try/except and try/finally
Neal Norwitz <nnorwitz@gmail.com>
parents:
36065
diff
changeset
|
2919 } |
|
719ba02d2e2c
SF patch #1355913, PEP 341 - Unification of try/except and try/finally
Neal Norwitz <nnorwitz@gmail.com>
parents:
36065
diff
changeset
|
2920 else { |
|
719ba02d2e2c
SF patch #1355913, PEP 341 - Unification of try/except and try/finally
Neal Norwitz <nnorwitz@gmail.com>
parents:
36065
diff
changeset
|
2921 /* 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
|
2922 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
|
2923 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
|
2924 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
|
2925 return NULL; |
|
719ba02d2e2c
SF patch #1355913, PEP 341 - Unification of try/except and try/finally
Neal Norwitz <nnorwitz@gmail.com>
parents:
36065
diff
changeset
|
2926 n_except--; |
|
719ba02d2e2c
SF patch #1355913, PEP 341 - Unification of try/except and try/finally
Neal Norwitz <nnorwitz@gmail.com>
parents:
36065
diff
changeset
|
2927 } |
| 35742 | 2928 } |
|
36066
719ba02d2e2c
SF patch #1355913, PEP 341 - Unification of try/except and try/finally
Neal Norwitz <nnorwitz@gmail.com>
parents:
36065
diff
changeset
|
2929 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
|
2930 ast_error(n, "malformed 'try' statement"); |
| 35742 | 2931 return NULL; |
| 2932 } | |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
56951
diff
changeset
|
2933 |
|
36066
719ba02d2e2c
SF patch #1355913, PEP 341 - Unification of try/except and try/finally
Neal Norwitz <nnorwitz@gmail.com>
parents:
36065
diff
changeset
|
2934 if (n_except > 0) { |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2935 int i; |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2936 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
|
2937 /* 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
|
2938 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
|
2939 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
|
2940 return NULL; |
|
719ba02d2e2c
SF patch #1355913, PEP 341 - Unification of try/except and try/finally
Neal Norwitz <nnorwitz@gmail.com>
parents:
36065
diff
changeset
|
2941 |
|
719ba02d2e2c
SF patch #1355913, PEP 341 - Unification of try/except and try/finally
Neal Norwitz <nnorwitz@gmail.com>
parents:
36065
diff
changeset
|
2942 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
|
2943 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
|
2944 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
|
2945 if (!e) |
|
719ba02d2e2c
SF patch #1355913, PEP 341 - Unification of try/except and try/finally
Neal Norwitz <nnorwitz@gmail.com>
parents:
36065
diff
changeset
|
2946 return NULL; |
|
719ba02d2e2c
SF patch #1355913, PEP 341 - Unification of try/except and try/finally
Neal Norwitz <nnorwitz@gmail.com>
parents:
36065
diff
changeset
|
2947 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
|
2948 } |
|
719ba02d2e2c
SF patch #1355913, PEP 341 - Unification of try/except and try/finally
Neal Norwitz <nnorwitz@gmail.com>
parents:
36065
diff
changeset
|
2949 |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2950 except_st = TryExcept(body, handlers, orelse, LINENO(n), |
|
37879
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36877
diff
changeset
|
2951 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
|
2952 if (!finally) |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2953 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
|
2954 |
|
719ba02d2e2c
SF patch #1355913, PEP 341 - Unification of try/except and try/finally
Neal Norwitz <nnorwitz@gmail.com>
parents:
36065
diff
changeset
|
2955 /* 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
|
2956 TryFinally to emulate try ... except ... finally */ |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2957 body = asdl_seq_new(1, c->c_arena); |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2958 if (body == NULL) |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2959 return NULL; |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2960 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
|
2961 } |
|
719ba02d2e2c
SF patch #1355913, PEP 341 - Unification of try/except and try/finally
Neal Norwitz <nnorwitz@gmail.com>
parents:
36065
diff
changeset
|
2962 |
|
719ba02d2e2c
SF patch #1355913, PEP 341 - Unification of try/except and try/finally
Neal Norwitz <nnorwitz@gmail.com>
parents:
36065
diff
changeset
|
2963 /* 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
|
2964 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
|
2965 return TryFinally(body, finally, LINENO(n), n->n_col_offset, c->c_arena); |
| 35742 | 2966 } |
| 2967 | |
|
54338
287ce30d08b5
Merged revisions 72924 via svnmerge from
Georg Brandl <georg@python.org>
parents:
53707
diff
changeset
|
2968 /* with_item: test ['as' expr] */ |
|
36695
b6d2f6fd3e11
PEP 343 -- the with-statement.
Guido van Rossum <guido@python.org>
parents:
36686
diff
changeset
|
2969 static stmt_ty |
|
54338
287ce30d08b5
Merged revisions 72924 via svnmerge from
Georg Brandl <georg@python.org>
parents:
53707
diff
changeset
|
2970 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
|
2971 { |
|
b6d2f6fd3e11
PEP 343 -- the with-statement.
Guido van Rossum <guido@python.org>
parents:
36686
diff
changeset
|
2972 expr_ty context_expr, optional_vars = NULL; |
|
b6d2f6fd3e11
PEP 343 -- the with-statement.
Guido van Rossum <guido@python.org>
parents:
36686
diff
changeset
|
2973 |
|
54338
287ce30d08b5
Merged revisions 72924 via svnmerge from
Georg Brandl <georg@python.org>
parents:
53707
diff
changeset
|
2974 REQ(n, with_item); |
|
287ce30d08b5
Merged revisions 72924 via svnmerge from
Georg Brandl <georg@python.org>
parents:
53707
diff
changeset
|
2975 context_expr = ast_for_expr(c, CHILD(n, 0)); |
|
64178
5fd26f452600
Check the return values for all functions returning an ast node.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
62373
diff
changeset
|
2976 if (!context_expr) |
|
5fd26f452600
Check the return values for all functions returning an ast node.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
62373
diff
changeset
|
2977 return NULL; |
|
54338
287ce30d08b5
Merged revisions 72924 via svnmerge from
Georg Brandl <georg@python.org>
parents:
53707
diff
changeset
|
2978 if (NCH(n) == 3) { |
|
287ce30d08b5
Merged revisions 72924 via svnmerge from
Georg Brandl <georg@python.org>
parents:
53707
diff
changeset
|
2979 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
|
2980 |
|
b6d2f6fd3e11
PEP 343 -- the with-statement.
Guido van Rossum <guido@python.org>
parents:
36686
diff
changeset
|
2981 if (!optional_vars) { |
|
b6d2f6fd3e11
PEP 343 -- the with-statement.
Guido van Rossum <guido@python.org>
parents:
36686
diff
changeset
|
2982 return NULL; |
|
b6d2f6fd3e11
PEP 343 -- the with-statement.
Guido van Rossum <guido@python.org>
parents:
36686
diff
changeset
|
2983 } |
|
46664
031dfe455585
Merged revisions 62425-62429,62434-62436,62441,62444,62446-62448,62450-62455,62463,62465-62466,62469,62474,62476-62478,62480,62485,62492,62497-62498,62500,62507,62513-62514,62516,62521,62531,62535,62545-62546,62548-62551,62553-62559,62569,62574,62577,62593,62595,62604-62606,62608,62616,62626-62627,62636,62638,62644-62645,62647-62648,62651-62653,62656,62661,62663,62680,62686-62687,62696,62699-62703,62711 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
46306
diff
changeset
|
2984 if (!set_context(c, optional_vars, Store, n)) { |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2985 return NULL; |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2986 } |
|
36695
b6d2f6fd3e11
PEP 343 -- the with-statement.
Guido van Rossum <guido@python.org>
parents:
36686
diff
changeset
|
2987 } |
|
b6d2f6fd3e11
PEP 343 -- the with-statement.
Guido van Rossum <guido@python.org>
parents:
36686
diff
changeset
|
2988 |
|
54338
287ce30d08b5
Merged revisions 72924 via svnmerge from
Georg Brandl <georg@python.org>
parents:
53707
diff
changeset
|
2989 return With(context_expr, optional_vars, content, LINENO(n), |
|
287ce30d08b5
Merged revisions 72924 via svnmerge from
Georg Brandl <georg@python.org>
parents:
53707
diff
changeset
|
2990 n->n_col_offset, c->c_arena); |
|
287ce30d08b5
Merged revisions 72924 via svnmerge from
Georg Brandl <georg@python.org>
parents:
53707
diff
changeset
|
2991 } |
|
287ce30d08b5
Merged revisions 72924 via svnmerge from
Georg Brandl <georg@python.org>
parents:
53707
diff
changeset
|
2992 |
|
287ce30d08b5
Merged revisions 72924 via svnmerge from
Georg Brandl <georg@python.org>
parents:
53707
diff
changeset
|
2993 /* with_stmt: 'with' with_item (',' with_item)* ':' suite */ |
|
287ce30d08b5
Merged revisions 72924 via svnmerge from
Georg Brandl <georg@python.org>
parents:
53707
diff
changeset
|
2994 static stmt_ty |
|
287ce30d08b5
Merged revisions 72924 via svnmerge from
Georg Brandl <georg@python.org>
parents:
53707
diff
changeset
|
2995 ast_for_with_stmt(struct compiling *c, const node *n) |
|
287ce30d08b5
Merged revisions 72924 via svnmerge from
Georg Brandl <georg@python.org>
parents:
53707
diff
changeset
|
2996 { |
|
287ce30d08b5
Merged revisions 72924 via svnmerge from
Georg Brandl <georg@python.org>
parents:
53707
diff
changeset
|
2997 int i; |
|
287ce30d08b5
Merged revisions 72924 via svnmerge from
Georg Brandl <georg@python.org>
parents:
53707
diff
changeset
|
2998 stmt_ty ret; |
|
287ce30d08b5
Merged revisions 72924 via svnmerge from
Georg Brandl <georg@python.org>
parents:
53707
diff
changeset
|
2999 asdl_seq *inner; |
|
287ce30d08b5
Merged revisions 72924 via svnmerge from
Georg Brandl <georg@python.org>
parents:
53707
diff
changeset
|
3000 |
|
287ce30d08b5
Merged revisions 72924 via svnmerge from
Georg Brandl <georg@python.org>
parents:
53707
diff
changeset
|
3001 REQ(n, with_stmt); |
|
287ce30d08b5
Merged revisions 72924 via svnmerge from
Georg Brandl <georg@python.org>
parents:
53707
diff
changeset
|
3002 |
|
287ce30d08b5
Merged revisions 72924 via svnmerge from
Georg Brandl <georg@python.org>
parents:
53707
diff
changeset
|
3003 /* process the with items inside-out */ |
|
287ce30d08b5
Merged revisions 72924 via svnmerge from
Georg Brandl <georg@python.org>
parents:
53707
diff
changeset
|
3004 i = NCH(n) - 1; |
|
287ce30d08b5
Merged revisions 72924 via svnmerge from
Georg Brandl <georg@python.org>
parents:
53707
diff
changeset
|
3005 /* the suite of the innermost with item is the suite of the with stmt */ |
|
287ce30d08b5
Merged revisions 72924 via svnmerge from
Georg Brandl <georg@python.org>
parents:
53707
diff
changeset
|
3006 inner = ast_for_suite(c, CHILD(n, i)); |
|
287ce30d08b5
Merged revisions 72924 via svnmerge from
Georg Brandl <georg@python.org>
parents:
53707
diff
changeset
|
3007 if (!inner) |
|
36695
b6d2f6fd3e11
PEP 343 -- the with-statement.
Guido van Rossum <guido@python.org>
parents:
36686
diff
changeset
|
3008 return NULL; |
|
54338
287ce30d08b5
Merged revisions 72924 via svnmerge from
Georg Brandl <georg@python.org>
parents:
53707
diff
changeset
|
3009 |
|
287ce30d08b5
Merged revisions 72924 via svnmerge from
Georg Brandl <georg@python.org>
parents:
53707
diff
changeset
|
3010 for (;;) { |
|
287ce30d08b5
Merged revisions 72924 via svnmerge from
Georg Brandl <georg@python.org>
parents:
53707
diff
changeset
|
3011 i -= 2; |
|
287ce30d08b5
Merged revisions 72924 via svnmerge from
Georg Brandl <georg@python.org>
parents:
53707
diff
changeset
|
3012 ret = ast_for_with_item(c, CHILD(n, i), inner); |
|
287ce30d08b5
Merged revisions 72924 via svnmerge from
Georg Brandl <georg@python.org>
parents:
53707
diff
changeset
|
3013 if (!ret) |
|
287ce30d08b5
Merged revisions 72924 via svnmerge from
Georg Brandl <georg@python.org>
parents:
53707
diff
changeset
|
3014 return NULL; |
|
287ce30d08b5
Merged revisions 72924 via svnmerge from
Georg Brandl <georg@python.org>
parents:
53707
diff
changeset
|
3015 /* was this the last item? */ |
|
287ce30d08b5
Merged revisions 72924 via svnmerge from
Georg Brandl <georg@python.org>
parents:
53707
diff
changeset
|
3016 if (i == 1) |
|
287ce30d08b5
Merged revisions 72924 via svnmerge from
Georg Brandl <georg@python.org>
parents:
53707
diff
changeset
|
3017 break; |
|
287ce30d08b5
Merged revisions 72924 via svnmerge from
Georg Brandl <georg@python.org>
parents:
53707
diff
changeset
|
3018 /* if not, wrap the result so far in a new sequence */ |
|
287ce30d08b5
Merged revisions 72924 via svnmerge from
Georg Brandl <georg@python.org>
parents:
53707
diff
changeset
|
3019 inner = asdl_seq_new(1, c->c_arena); |
|
287ce30d08b5
Merged revisions 72924 via svnmerge from
Georg Brandl <georg@python.org>
parents:
53707
diff
changeset
|
3020 if (!inner) |
|
287ce30d08b5
Merged revisions 72924 via svnmerge from
Georg Brandl <georg@python.org>
parents:
53707
diff
changeset
|
3021 return NULL; |
|
287ce30d08b5
Merged revisions 72924 via svnmerge from
Georg Brandl <georg@python.org>
parents:
53707
diff
changeset
|
3022 asdl_seq_SET(inner, 0, ret); |
|
36695
b6d2f6fd3e11
PEP 343 -- the with-statement.
Guido van Rossum <guido@python.org>
parents:
36686
diff
changeset
|
3023 } |
|
54338
287ce30d08b5
Merged revisions 72924 via svnmerge from
Georg Brandl <georg@python.org>
parents:
53707
diff
changeset
|
3024 |
|
287ce30d08b5
Merged revisions 72924 via svnmerge from
Georg Brandl <georg@python.org>
parents:
53707
diff
changeset
|
3025 return ret; |
|
36695
b6d2f6fd3e11
PEP 343 -- the with-statement.
Guido van Rossum <guido@python.org>
parents:
36686
diff
changeset
|
3026 } |
|
b6d2f6fd3e11
PEP 343 -- the with-statement.
Guido van Rossum <guido@python.org>
parents:
36686
diff
changeset
|
3027 |
| 35742 | 3028 static stmt_ty |
|
41824
24b60622b944
Merged revisions 55407-55513 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41717
diff
changeset
|
3029 ast_for_classdef(struct compiling *c, const node *n, asdl_seq *decorator_seq) |
| 35742 | 3030 { |
|
41226
19f7ff443718
Implement PEP 3115 -- new metaclass syntax and semantics.
Guido van Rossum <guido@python.org>
parents:
40965
diff
changeset
|
3031 /* classdef: 'class' NAME ['(' arglist ')'] ':' suite */ |
|
49844
40b7277e665f
Merged revisions 67373 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
49797
diff
changeset
|
3032 PyObject *classname; |
|
41226
19f7ff443718
Implement PEP 3115 -- new metaclass syntax and semantics.
Guido van Rossum <guido@python.org>
parents:
40965
diff
changeset
|
3033 asdl_seq *s; |
|
49852
30d01a5f517e
check the return value of NEW_IDENTIFIER in some more places
Benjamin Peterson <benjamin@python.org>
parents:
49847
diff
changeset
|
3034 expr_ty call; |
|
41226
19f7ff443718
Implement PEP 3115 -- new metaclass syntax and semantics.
Guido van Rossum <guido@python.org>
parents:
40965
diff
changeset
|
3035 |
| 35742 | 3036 REQ(n, classdef); |
| 3037 | |
|
41226
19f7ff443718
Implement PEP 3115 -- new metaclass syntax and semantics.
Guido van Rossum <guido@python.org>
parents:
40965
diff
changeset
|
3038 if (NCH(n) == 4) { /* class NAME ':' suite */ |
| 35742 | 3039 s = ast_for_suite(c, CHILD(n, 3)); |
| 3040 if (!s) | |
| 3041 return NULL; | |
|
49844
40b7277e665f
Merged revisions 67373 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
49797
diff
changeset
|
3042 classname = NEW_IDENTIFIER(CHILD(n, 1)); |
|
40b7277e665f
Merged revisions 67373 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
49797
diff
changeset
|
3043 if (!classname) |
|
40b7277e665f
Merged revisions 67373 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
49797
diff
changeset
|
3044 return NULL; |
|
54954
c45e331238ae
In most cases, the parser will protect True, False, and None from being assign to.
Benjamin Peterson <benjamin@python.org>
parents:
54918
diff
changeset
|
3045 if (forbidden_name(classname, CHILD(n, 3), 0)) |
|
c45e331238ae
In most cases, the parser will protect True, False, and None from being assign to.
Benjamin Peterson <benjamin@python.org>
parents:
54918
diff
changeset
|
3046 return NULL; |
|
49844
40b7277e665f
Merged revisions 67373 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
49797
diff
changeset
|
3047 return ClassDef(classname, NULL, NULL, NULL, NULL, s, decorator_seq, |
|
40b7277e665f
Merged revisions 67373 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
49797
diff
changeset
|
3048 LINENO(n), n->n_col_offset, c->c_arena); |
| 35742 | 3049 } |
|
41226
19f7ff443718
Implement PEP 3115 -- new metaclass syntax and semantics.
Guido van Rossum <guido@python.org>
parents:
40965
diff
changeset
|
3050 |
|
19f7ff443718
Implement PEP 3115 -- new metaclass syntax and semantics.
Guido van Rossum <guido@python.org>
parents:
40965
diff
changeset
|
3051 if (TYPE(CHILD(n, 3)) == RPAR) { /* class NAME '(' ')' ':' suite */ |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
3052 s = ast_for_suite(c, CHILD(n,5)); |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
3053 if (!s) |
|
49844
40b7277e665f
Merged revisions 67373 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
49797
diff
changeset
|
3054 return NULL; |
|
40b7277e665f
Merged revisions 67373 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
49797
diff
changeset
|
3055 classname = NEW_IDENTIFIER(CHILD(n, 1)); |
|
40b7277e665f
Merged revisions 67373 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
49797
diff
changeset
|
3056 if (!classname) |
|
40b7277e665f
Merged revisions 67373 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
49797
diff
changeset
|
3057 return NULL; |
|
54954
c45e331238ae
In most cases, the parser will protect True, False, and None from being assign to.
Benjamin Peterson <benjamin@python.org>
parents:
54918
diff
changeset
|
3058 if (forbidden_name(classname, CHILD(n, 3), 0)) |
|
c45e331238ae
In most cases, the parser will protect True, False, and None from being assign to.
Benjamin Peterson <benjamin@python.org>
parents:
54918
diff
changeset
|
3059 return NULL; |
|
49844
40b7277e665f
Merged revisions 67373 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
49797
diff
changeset
|
3060 return ClassDef(classname, NULL, NULL, NULL, NULL, s, decorator_seq, |
|
40b7277e665f
Merged revisions 67373 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
49797
diff
changeset
|
3061 LINENO(n), n->n_col_offset, c->c_arena); |
| 35742 | 3062 } |
| 3063 | |
|
41226
19f7ff443718
Implement PEP 3115 -- new metaclass syntax and semantics.
Guido van Rossum <guido@python.org>
parents:
40965
diff
changeset
|
3064 /* class NAME '(' arglist ')' ':' suite */ |
|
19f7ff443718
Implement PEP 3115 -- new metaclass syntax and semantics.
Guido van Rossum <guido@python.org>
parents:
40965
diff
changeset
|
3065 /* build up a fake Call node so we can extract its pieces */ |
|
49852
30d01a5f517e
check the return value of NEW_IDENTIFIER in some more places
Benjamin Peterson <benjamin@python.org>
parents:
49847
diff
changeset
|
3066 { |
|
30d01a5f517e
check the return value of NEW_IDENTIFIER in some more places
Benjamin Peterson <benjamin@python.org>
parents:
49847
diff
changeset
|
3067 PyObject *dummy_name; |
|
30d01a5f517e
check the return value of NEW_IDENTIFIER in some more places
Benjamin Peterson <benjamin@python.org>
parents:
49847
diff
changeset
|
3068 expr_ty dummy; |
|
30d01a5f517e
check the return value of NEW_IDENTIFIER in some more places
Benjamin Peterson <benjamin@python.org>
parents:
49847
diff
changeset
|
3069 dummy_name = NEW_IDENTIFIER(CHILD(n, 1)); |
|
30d01a5f517e
check the return value of NEW_IDENTIFIER in some more places
Benjamin Peterson <benjamin@python.org>
parents:
49847
diff
changeset
|
3070 if (!dummy_name) |
|
30d01a5f517e
check the return value of NEW_IDENTIFIER in some more places
Benjamin Peterson <benjamin@python.org>
parents:
49847
diff
changeset
|
3071 return NULL; |
|
30d01a5f517e
check the return value of NEW_IDENTIFIER in some more places
Benjamin Peterson <benjamin@python.org>
parents:
49847
diff
changeset
|
3072 dummy = Name(dummy_name, Load, LINENO(n), n->n_col_offset, c->c_arena); |
|
30d01a5f517e
check the return value of NEW_IDENTIFIER in some more places
Benjamin Peterson <benjamin@python.org>
parents:
49847
diff
changeset
|
3073 call = ast_for_call(c, CHILD(n, 3), dummy); |
|
30d01a5f517e
check the return value of NEW_IDENTIFIER in some more places
Benjamin Peterson <benjamin@python.org>
parents:
49847
diff
changeset
|
3074 if (!call) |
|
30d01a5f517e
check the return value of NEW_IDENTIFIER in some more places
Benjamin Peterson <benjamin@python.org>
parents:
49847
diff
changeset
|
3075 return NULL; |
|
30d01a5f517e
check the return value of NEW_IDENTIFIER in some more places
Benjamin Peterson <benjamin@python.org>
parents:
49847
diff
changeset
|
3076 } |
| 35742 | 3077 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
|
3078 if (!s) |
| 35742 | 3079 return NULL; |
|
49844
40b7277e665f
Merged revisions 67373 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
49797
diff
changeset
|
3080 classname = NEW_IDENTIFIER(CHILD(n, 1)); |
|
40b7277e665f
Merged revisions 67373 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
49797
diff
changeset
|
3081 if (!classname) |
|
40b7277e665f
Merged revisions 67373 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
49797
diff
changeset
|
3082 return NULL; |
|
54954
c45e331238ae
In most cases, the parser will protect True, False, and None from being assign to.
Benjamin Peterson <benjamin@python.org>
parents:
54918
diff
changeset
|
3083 if (forbidden_name(classname, CHILD(n, 1), 0)) |
|
c45e331238ae
In most cases, the parser will protect True, False, and None from being assign to.
Benjamin Peterson <benjamin@python.org>
parents:
54918
diff
changeset
|
3084 return NULL; |
|
41226
19f7ff443718
Implement PEP 3115 -- new metaclass syntax and semantics.
Guido van Rossum <guido@python.org>
parents:
40965
diff
changeset
|
3085 |
|
49844
40b7277e665f
Merged revisions 67373 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
49797
diff
changeset
|
3086 return ClassDef(classname, call->v.Call.args, call->v.Call.keywords, |
|
41226
19f7ff443718
Implement PEP 3115 -- new metaclass syntax and semantics.
Guido van Rossum <guido@python.org>
parents:
40965
diff
changeset
|
3087 call->v.Call.starargs, call->v.Call.kwargs, s, |
|
41824
24b60622b944
Merged revisions 55407-55513 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41717
diff
changeset
|
3088 decorator_seq, LINENO(n), n->n_col_offset, c->c_arena); |
| 35742 | 3089 } |
| 3090 | |
| 3091 static stmt_ty | |
| 3092 ast_for_stmt(struct compiling *c, const node *n) | |
| 3093 { | |
| 3094 if (TYPE(n) == stmt) { | |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
3095 assert(NCH(n) == 1); |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
3096 n = CHILD(n, 0); |
| 35742 | 3097 } |
| 3098 if (TYPE(n) == simple_stmt) { | |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
3099 assert(num_stmts(n) == 1); |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
3100 n = CHILD(n, 0); |
| 35742 | 3101 } |
| 3102 if (TYPE(n) == small_stmt) { | |
|
40529
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
3103 n = CHILD(n, 0); |
|
40965
30e5cb3a55ac
Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
40941
diff
changeset
|
3104 /* small_stmt: expr_stmt | del_stmt | pass_stmt | flow_stmt |
|
30e5cb3a55ac
Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
40941
diff
changeset
|
3105 | import_stmt | global_stmt | nonlocal_stmt | assert_stmt |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
3106 */ |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
3107 switch (TYPE(n)) { |
| 35742 | 3108 case expr_stmt: |
| 3109 return ast_for_expr_stmt(c, n); | |
| 3110 case del_stmt: | |
| 3111 return ast_for_del_stmt(c, n); | |
| 3112 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
|
3113 return Pass(LINENO(n), n->n_col_offset, c->c_arena); |
| 35742 | 3114 case flow_stmt: |
| 3115 return ast_for_flow_stmt(c, n); | |
| 3116 case import_stmt: | |
| 3117 return ast_for_import_stmt(c, n); | |
| 3118 case global_stmt: | |
| 3119 return ast_for_global_stmt(c, n); | |
|
40965
30e5cb3a55ac
Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
40941
diff
changeset
|
3120 case nonlocal_stmt: |
|
30e5cb3a55ac
Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
40941
diff
changeset
|
3121 return ast_for_nonlocal_stmt(c, n); |
| 35742 | 3122 case assert_stmt: |
| 3123 return ast_for_assert_stmt(c, n); | |
| 3124 default: | |
|
35870
f531c05cd240
Convert all internal errors from Exception to SystemError
Neal Norwitz <nnorwitz@gmail.com>
parents:
35868
diff
changeset
|
3125 PyErr_Format(PyExc_SystemError, |
| 35742 | 3126 "unhandled small_stmt: TYPE=%d NCH=%d\n", |
| 3127 TYPE(n), NCH(n)); | |
| 3128 return NULL; | |
| 3129 } | |
| 3130 } | |
| 3131 else { | |
| 3132 /* compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | |
|
41824
24b60622b944
Merged revisions 55407-55513 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41717
diff
changeset
|
3133 | funcdef | classdef | decorated |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
3134 */ |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
3135 node *ch = CHILD(n, 0); |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
3136 REQ(n, compound_stmt); |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
3137 switch (TYPE(ch)) { |
| 35742 | 3138 case if_stmt: |
| 3139 return ast_for_if_stmt(c, ch); | |
| 3140 case while_stmt: | |
| 3141 return ast_for_while_stmt(c, ch); | |
| 3142 case for_stmt: | |
| 3143 return ast_for_for_stmt(c, ch); | |
| 3144 case try_stmt: | |
| 3145 return ast_for_try_stmt(c, ch); | |
|
36695
b6d2f6fd3e11
PEP 343 -- the with-statement.
Guido van Rossum <guido@python.org>
parents:
36686
diff
changeset
|
3146 case with_stmt: |
|
b6d2f6fd3e11
PEP 343 -- the with-statement.
Guido van Rossum <guido@python.org>
parents:
36686
diff
changeset
|
3147 return ast_for_with_stmt(c, ch); |
| 35742 | 3148 case funcdef: |
|
41824
24b60622b944
Merged revisions 55407-55513 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41717
diff
changeset
|
3149 return ast_for_funcdef(c, ch, NULL); |
| 35742 | 3150 case classdef: |
|
41824
24b60622b944
Merged revisions 55407-55513 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41717
diff
changeset
|
3151 return ast_for_classdef(c, ch, NULL); |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
56951
diff
changeset
|
3152 case decorated: |
|
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
56951
diff
changeset
|
3153 return ast_for_decorated(c, ch); |
| 35742 | 3154 default: |
|
35870
f531c05cd240
Convert all internal errors from Exception to SystemError
Neal Norwitz <nnorwitz@gmail.com>
parents:
35868
diff
changeset
|
3155 PyErr_Format(PyExc_SystemError, |
| 35742 | 3156 "unhandled small_stmt: TYPE=%d NCH=%d\n", |
| 3157 TYPE(n), NCH(n)); | |
| 3158 return NULL; | |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
3159 } |
| 35742 | 3160 } |
| 3161 } | |
| 3162 | |
| 3163 static PyObject * | |
|
46664
031dfe455585
Merged revisions 62425-62429,62434-62436,62441,62444,62446-62448,62450-62455,62463,62465-62466,62469,62474,62476-62478,62480,62485,62492,62497-62498,62500,62507,62513-62514,62516,62521,62531,62535,62545-62546,62548-62551,62553-62559,62569,62574,62577,62593,62595,62604-62606,62608,62616,62626-62627,62636,62638,62644-62645,62647-62648,62651-62653,62656,62661,62663,62680,62686-62687,62696,62699-62703,62711 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
46306
diff
changeset
|
3164 parsenumber(struct compiling *c, const char *s) |
| 35742 | 3165 { |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3166 const char *end; |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3167 long x; |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3168 double dx; |
|
46664
031dfe455585
Merged revisions 62425-62429,62434-62436,62441,62444,62446-62448,62450-62455,62463,62465-62466,62469,62474,62476-62478,62480,62485,62492,62497-62498,62500,62507,62513-62514,62516,62521,62531,62535,62545-62546,62548-62551,62553-62559,62569,62574,62577,62593,62595,62604-62606,62608,62616,62626-62627,62636,62638,62644-62645,62647-62648,62651-62653,62656,62661,62663,62680,62686-62687,62696,62699-62703,62711 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
46306
diff
changeset
|
3169 Py_complex compl; |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3170 int imflag; |
| 35742 | 3171 |
|
50035
4c9844cf2a3f
Merged revisions 67595 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
49852
diff
changeset
|
3172 assert(s != NULL); |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3173 errno = 0; |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3174 end = s + strlen(s) - 1; |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3175 imflag = *end == 'j' || *end == 'J'; |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3176 if (s[0] == '0') { |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3177 x = (long) PyOS_strtoul((char *)s, (char **)&end, 0); |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3178 if (x < 0 && errno == 0) { |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3179 return PyLong_FromString((char *)s, |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3180 (char **)0, |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3181 0); |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
3182 } |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3183 } |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3184 else |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3185 x = PyOS_strtol((char *)s, (char **)&end, 0); |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3186 if (*end == '\0') { |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3187 if (errno != 0) |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3188 return PyLong_FromString((char *)s, (char **)0, 0); |
|
44099
f324631462a2
Cleanup: Replaced most PyInt_ aliases with PyLong_ and disabled the aliases in intobject.h
Christian Heimes <christian@cheimes.de>
parents:
44025
diff
changeset
|
3189 return PyLong_FromLong(x); |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3190 } |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3191 /* XXX Huge floats may silently fail */ |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3192 if (imflag) { |
|
46664
031dfe455585
Merged revisions 62425-62429,62434-62436,62441,62444,62446-62448,62450-62455,62463,62465-62466,62469,62474,62476-62478,62480,62485,62492,62497-62498,62500,62507,62513-62514,62516,62521,62531,62535,62545-62546,62548-62551,62553-62559,62569,62574,62577,62593,62595,62604-62606,62608,62616,62626-62627,62636,62638,62644-62645,62647-62648,62651-62653,62656,62661,62663,62680,62686-62687,62696,62699-62703,62711 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
46306
diff
changeset
|
3193 compl.real = 0.; |
|
53707
13a82a710f19
Issue #5914: Add new C-API function PyOS_string_to_double, to complement
Mark Dickinson <dickinsm@gmail.com>
parents:
50886
diff
changeset
|
3194 compl.imag = PyOS_string_to_double(s, (char **)&end, NULL); |
|
13a82a710f19
Issue #5914: Add new C-API function PyOS_string_to_double, to complement
Mark Dickinson <dickinsm@gmail.com>
parents:
50886
diff
changeset
|
3195 if (compl.imag == -1.0 && PyErr_Occurred()) |
|
13a82a710f19
Issue #5914: Add new C-API function PyOS_string_to_double, to complement
Mark Dickinson <dickinsm@gmail.com>
parents:
50886
diff
changeset
|
3196 return NULL; |
|
13a82a710f19
Issue #5914: Add new C-API function PyOS_string_to_double, to complement
Mark Dickinson <dickinsm@gmail.com>
parents:
50886
diff
changeset
|
3197 return PyComplex_FromCComplex(compl); |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3198 } |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3199 else |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3200 { |
|
53707
13a82a710f19
Issue #5914: Add new C-API function PyOS_string_to_double, to complement
Mark Dickinson <dickinsm@gmail.com>
parents:
50886
diff
changeset
|
3201 dx = PyOS_string_to_double(s, NULL, NULL); |
|
13a82a710f19
Issue #5914: Add new C-API function PyOS_string_to_double, to complement
Mark Dickinson <dickinsm@gmail.com>
parents:
50886
diff
changeset
|
3202 if (dx == -1.0 && PyErr_Occurred()) |
|
13a82a710f19
Issue #5914: Add new C-API function PyOS_string_to_double, to complement
Mark Dickinson <dickinsm@gmail.com>
parents:
50886
diff
changeset
|
3203 return NULL; |
|
13a82a710f19
Issue #5914: Add new C-API function PyOS_string_to_double, to complement
Mark Dickinson <dickinsm@gmail.com>
parents:
50886
diff
changeset
|
3204 return PyFloat_FromDouble(dx); |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3205 } |
| 35742 | 3206 } |
| 3207 | |
| 3208 static PyObject * | |
|
46664
031dfe455585
Merged revisions 62425-62429,62434-62436,62441,62444,62446-62448,62450-62455,62463,62465-62466,62469,62474,62476-62478,62480,62485,62492,62497-62498,62500,62507,62513-62514,62516,62521,62531,62535,62545-62546,62548-62551,62553-62559,62569,62574,62577,62593,62595,62604-62606,62608,62616,62626-62627,62636,62638,62644-62645,62647-62648,62651-62653,62656,62661,62663,62680,62686-62687,62696,62699-62703,62711 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
46306
diff
changeset
|
3209 decode_utf8(struct compiling *c, const char **sPtr, const char *end, char* encoding) |
| 35742 | 3210 { |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3211 PyObject *u, *v; |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3212 char *s, *t; |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3213 t = s = (char *)*sPtr; |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3214 /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */ |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3215 while (s < end && (*s & 0x80)) s++; |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3216 *sPtr = s; |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3217 u = PyUnicode_DecodeUTF8(t, s - t, NULL); |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3218 if (u == NULL) |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3219 return NULL; |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3220 v = PyUnicode_AsEncodedString(u, encoding, NULL); |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3221 Py_DECREF(u); |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3222 return v; |
| 35742 | 3223 } |
| 3224 | |
| 3225 static PyObject * | |
|
46664
031dfe455585
Merged revisions 62425-62429,62434-62436,62441,62444,62446-62448,62450-62455,62463,62465-62466,62469,62474,62476-62478,62480,62485,62492,62497-62498,62500,62507,62513-62514,62516,62521,62531,62535,62545-62546,62548-62551,62553-62559,62569,62574,62577,62593,62595,62604-62606,62608,62616,62626-62627,62636,62638,62644-62645,62647-62648,62651-62653,62656,62661,62663,62680,62686-62687,62696,62699-62703,62711 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
46306
diff
changeset
|
3226 decode_unicode(struct compiling *c, const char *s, size_t len, int rawmode, const char *encoding) |
| 35742 | 3227 { |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3228 PyObject *v, *u; |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3229 char *buf; |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3230 char *p; |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3231 const char *end; |
|
43819
068655054b0d
Minimal change that disables (AFAICT) the interpolation of \u and \U inside
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
3232 |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3233 if (encoding == NULL) { |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3234 buf = (char *)s; |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3235 u = NULL; |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3236 } else { |
|
48330
ac854cde3135
Merged revisions 64114 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
47301
diff
changeset
|
3237 /* check for integer overflow */ |
|
56951
db4dfdc74029
in wide builds, avoid storing high unicode characters from source code with surrogates
Benjamin Peterson <benjamin@python.org>
parents:
56594
diff
changeset
|
3238 if (len > PY_SIZE_MAX / 6) |
|
48330
ac854cde3135
Merged revisions 64114 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
47301
diff
changeset
|
3239 return NULL; |
|
56951
db4dfdc74029
in wide builds, avoid storing high unicode characters from source code with surrogates
Benjamin Peterson <benjamin@python.org>
parents:
56594
diff
changeset
|
3240 /* "ä" (2 bytes) may become "\U000000E4" (10 bytes), or 1:5 |
|
db4dfdc74029
in wide builds, avoid storing high unicode characters from source code with surrogates
Benjamin Peterson <benjamin@python.org>
parents:
56594
diff
changeset
|
3241 "\ä" (3 bytes) may become "\u005c\U000000E4" (16 bytes), or ~1:6 */ |
|
db4dfdc74029
in wide builds, avoid storing high unicode characters from source code with surrogates
Benjamin Peterson <benjamin@python.org>
parents:
56594
diff
changeset
|
3242 u = PyBytes_FromStringAndSize((char *)NULL, len * 6); |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3243 if (u == NULL) |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3244 return NULL; |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
46664
diff
changeset
|
3245 p = buf = PyBytes_AsString(u); |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3246 end = s + len; |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3247 while (s < end) { |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3248 if (*s == '\\') { |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3249 *p++ = *s++; |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3250 if (*s & 0x80) { |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3251 strcpy(p, "u005c"); |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3252 p += 5; |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
3253 } |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3254 } |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3255 if (*s & 0x80) { /* XXX inefficient */ |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3256 PyObject *w; |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3257 char *r; |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3258 Py_ssize_t rn, i; |
|
56951
db4dfdc74029
in wide builds, avoid storing high unicode characters from source code with surrogates
Benjamin Peterson <benjamin@python.org>
parents:
56594
diff
changeset
|
3259 w = decode_utf8(c, &s, end, "utf-32-be"); |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3260 if (w == NULL) { |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3261 Py_DECREF(u); |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3262 return NULL; |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3263 } |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
46664
diff
changeset
|
3264 r = PyBytes_AS_STRING(w); |
|
44351
86a2679dca46
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents:
44246
diff
changeset
|
3265 rn = Py_SIZE(w); |
|
56951
db4dfdc74029
in wide builds, avoid storing high unicode characters from source code with surrogates
Benjamin Peterson <benjamin@python.org>
parents:
56594
diff
changeset
|
3266 assert(rn % 4 == 0); |
|
db4dfdc74029
in wide builds, avoid storing high unicode characters from source code with surrogates
Benjamin Peterson <benjamin@python.org>
parents:
56594
diff
changeset
|
3267 for (i = 0; i < rn; i += 4) { |
|
db4dfdc74029
in wide builds, avoid storing high unicode characters from source code with surrogates
Benjamin Peterson <benjamin@python.org>
parents:
56594
diff
changeset
|
3268 sprintf(p, "\\U%02x%02x%02x%02x", |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3269 r[i + 0] & 0xFF, |
|
56951
db4dfdc74029
in wide builds, avoid storing high unicode characters from source code with surrogates
Benjamin Peterson <benjamin@python.org>
parents:
56594
diff
changeset
|
3270 r[i + 1] & 0xFF, |
|
db4dfdc74029
in wide builds, avoid storing high unicode characters from source code with surrogates
Benjamin Peterson <benjamin@python.org>
parents:
56594
diff
changeset
|
3271 r[i + 2] & 0xFF, |
|
db4dfdc74029
in wide builds, avoid storing high unicode characters from source code with surrogates
Benjamin Peterson <benjamin@python.org>
parents:
56594
diff
changeset
|
3272 r[i + 3] & 0xFF); |
|
db4dfdc74029
in wide builds, avoid storing high unicode characters from source code with surrogates
Benjamin Peterson <benjamin@python.org>
parents:
56594
diff
changeset
|
3273 p += 10; |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3274 } |
|
56951
db4dfdc74029
in wide builds, avoid storing high unicode characters from source code with surrogates
Benjamin Peterson <benjamin@python.org>
parents:
56594
diff
changeset
|
3275 /* Should be impossible to overflow */ |
|
db4dfdc74029
in wide builds, avoid storing high unicode characters from source code with surrogates
Benjamin Peterson <benjamin@python.org>
parents:
56594
diff
changeset
|
3276 assert(p - buf <= Py_SIZE(u)); |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3277 Py_DECREF(w); |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3278 } else { |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3279 *p++ = *s++; |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3280 } |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
3281 } |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3282 len = p - buf; |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3283 s = buf; |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3284 } |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3285 if (rawmode) |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3286 v = PyUnicode_DecodeRawUnicodeEscape(s, len, NULL); |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3287 else |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3288 v = PyUnicode_DecodeUnicodeEscape(s, len, NULL); |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3289 Py_XDECREF(u); |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3290 return v; |
| 35742 | 3291 } |
| 3292 | |
| 3293 /* s is a Python string literal, including the bracketing quote characters, | |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
3294 * and r &/or b prefixes (if any), and embedded escape sequences (if any). |
| 35742 | 3295 * parsestr parses it, and returns the decoded Python string object. |
| 3296 */ | |
| 3297 static PyObject * | |
|
46120
78504d604b44
Merged revisions 61952-61953 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45931
diff
changeset
|
3298 parsestr(struct compiling *c, const node *n, int *bytesmode) |
| 35742 | 3299 { |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3300 size_t len; |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3301 const char *s = STR(n); |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3302 int quote = Py_CHARMASK(*s); |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3303 int rawmode = 0; |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3304 int need_encoding; |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
3305 if (isalpha(quote)) { |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3306 if (quote == 'b' || quote == 'B') { |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3307 quote = *++s; |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3308 *bytesmode = 1; |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
56951
diff
changeset
|
3309 } |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3310 if (quote == 'r' || quote == 'R') { |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3311 quote = *++s; |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3312 rawmode = 1; |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
3313 } |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3314 } |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3315 if (quote != '\'' && quote != '\"') { |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3316 PyErr_BadInternalCall(); |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3317 return NULL; |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3318 } |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3319 s++; |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3320 len = strlen(s); |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3321 if (len > INT_MAX) { |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
56951
diff
changeset
|
3322 PyErr_SetString(PyExc_OverflowError, |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3323 "string to parse is too long"); |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3324 return NULL; |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3325 } |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3326 if (s[--len] != quote) { |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3327 PyErr_BadInternalCall(); |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3328 return NULL; |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3329 } |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3330 if (len >= 4 && s[0] == quote && s[1] == quote) { |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3331 s += 2; |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3332 len -= 2; |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3333 if (s[--len] != quote || s[--len] != quote) { |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3334 PyErr_BadInternalCall(); |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3335 return NULL; |
| 40894 | 3336 } |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3337 } |
|
46306
b10292eafb07
Reverted r62128 on Guido's orders
Benjamin Peterson <benjamin@python.org>
parents:
46272
diff
changeset
|
3338 if (!*bytesmode && !rawmode) { |
|
46664
031dfe455585
Merged revisions 62425-62429,62434-62436,62441,62444,62446-62448,62450-62455,62463,62465-62466,62469,62474,62476-62478,62480,62485,62492,62497-62498,62500,62507,62513-62514,62516,62521,62531,62535,62545-62546,62548-62551,62553-62559,62569,62574,62577,62593,62595,62604-62606,62608,62616,62626-62627,62636,62638,62644-62645,62647-62648,62651-62653,62656,62661,62663,62680,62686-62687,62696,62699-62703,62711 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
46306
diff
changeset
|
3339 return decode_unicode(c, s, len, rawmode, c->c_encoding); |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3340 } |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3341 if (*bytesmode) { |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3342 /* Disallow non-ascii characters (but not escapes) */ |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3343 const char *c; |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3344 for (c = s; *c; c++) { |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3345 if (Py_CHARMASK(*c) >= 0x80) { |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3346 ast_error(n, "bytes can only contain ASCII " |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3347 "literal characters."); |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3348 return NULL; |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3349 } |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3350 } |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3351 } |
|
46120
78504d604b44
Merged revisions 61952-61953 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45931
diff
changeset
|
3352 need_encoding = (!*bytesmode && c->c_encoding != NULL && |
|
49539
481ad3d9ffd1
Latin-1 source code was not being properly decoded when passed through
Brett Cannon <bcannon@gmail.com>
parents:
48777
diff
changeset
|
3353 strcmp(c->c_encoding, "utf-8") != 0); |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3354 if (rawmode || strchr(s, '\\') == NULL) { |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3355 if (need_encoding) { |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3356 PyObject *v, *u = PyUnicode_DecodeUTF8(s, len, NULL); |
|
43819
068655054b0d
Minimal change that disables (AFAICT) the interpolation of \u and \U inside
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
3357 if (u == NULL || !*bytesmode) |
|
068655054b0d
Minimal change that disables (AFAICT) the interpolation of \u and \U inside
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
3358 return u; |
|
46120
78504d604b44
Merged revisions 61952-61953 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45931
diff
changeset
|
3359 v = PyUnicode_AsEncodedString(u, c->c_encoding, NULL); |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3360 Py_DECREF(u); |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3361 return v; |
|
43819
068655054b0d
Minimal change that disables (AFAICT) the interpolation of \u and \U inside
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
3362 } else if (*bytesmode) { |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
46664
diff
changeset
|
3363 return PyBytes_FromStringAndSize(s, len); |
|
46120
78504d604b44
Merged revisions 61952-61953 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45931
diff
changeset
|
3364 } else if (strcmp(c->c_encoding, "utf-8") == 0) { |
|
43819
068655054b0d
Minimal change that disables (AFAICT) the interpolation of \u and \U inside
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
3365 return PyUnicode_FromStringAndSize(s, len); |
|
61466
cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
56951
diff
changeset
|
3366 } else { |
|
43819
068655054b0d
Minimal change that disables (AFAICT) the interpolation of \u and \U inside
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
3367 return PyUnicode_DecodeLatin1(s, len, NULL); |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
3368 } |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3369 } |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
46664
diff
changeset
|
3370 return PyBytes_DecodeEscape(s, len, NULL, 1, |
|
46120
78504d604b44
Merged revisions 61952-61953 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45931
diff
changeset
|
3371 need_encoding ? c->c_encoding : NULL); |
| 35742 | 3372 } |
| 3373 | |
|
43819
068655054b0d
Minimal change that disables (AFAICT) the interpolation of \u and \U inside
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
3374 /* Build a Python string object out of a STRING+ atom. This takes care of |
| 35742 | 3375 * compile-time literal catenation, calling parsestr() on each piece, and |
| 3376 * pasting the intermediate results together. | |
| 3377 */ | |
| 3378 static PyObject * | |
| 40894 | 3379 parsestrplus(struct compiling *c, const node *n, int *bytesmode) |
| 35742 | 3380 { |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3381 PyObject *v; |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3382 int i; |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3383 REQ(CHILD(n, 0), STRING); |
|
46120
78504d604b44
Merged revisions 61952-61953 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45931
diff
changeset
|
3384 v = parsestr(c, CHILD(n, 0), bytesmode); |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3385 if (v != NULL) { |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3386 /* String literal concatenation */ |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3387 for (i = 1; i < NCH(n); i++) { |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3388 PyObject *s; |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3389 int subbm = 0; |
|
46120
78504d604b44
Merged revisions 61952-61953 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45931
diff
changeset
|
3390 s = parsestr(c, CHILD(n, i), &subbm); |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3391 if (s == NULL) |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3392 goto onError; |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3393 if (*bytesmode != subbm) { |
|
43819
068655054b0d
Minimal change that disables (AFAICT) the interpolation of \u and \U inside
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
3394 ast_error(n, "cannot mix bytes and nonbytes literals"); |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3395 goto onError; |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3396 } |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
46664
diff
changeset
|
3397 if (PyBytes_Check(v) && PyBytes_Check(s)) { |
|
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
46664
diff
changeset
|
3398 PyBytes_ConcatAndDel(&v, s); |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3399 if (v == NULL) |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3400 goto onError; |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3401 } |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3402 else { |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3403 PyObject *temp = PyUnicode_Concat(v, s); |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3404 Py_DECREF(s); |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3405 Py_DECREF(v); |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3406 v = temp; |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3407 if (v == NULL) |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3408 goto onError; |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3409 } |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
3410 } |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3411 } |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3412 return v; |
| 35742 | 3413 |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3414 onError: |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3415 Py_XDECREF(v); |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3416 return NULL; |
| 35742 | 3417 } |
