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