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