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