|
70 | 70 | #define DUPLICATE_TYPE_PARAM \
|
71 | 71 | "duplicate type parameter '%U'"
|
72 | 72 |
|
| 73 | +#define ASYNC_WITH_OUTISDE_ASYNC_FUNC \ |
| 74 | +"'async with' outside async function" |
| 75 | + |
| 76 | +#define ASYNC_FOR_OUTISDE_ASYNC_FUNC \ |
| 77 | +"'async for' outside async function" |
73 | 78 |
|
74 | 79 | #define LOCATION(x) SRC_LOCATION_FROM_AST(x)
|
75 | 80 |
|
@@ -251,6 +256,7 @@ static int symtable_visit_withitem(struct symtable *st, withitem_ty item);
|
251 | 256 | static int symtable_visit_match_case(struct symtable *st, match_case_ty m);
|
252 | 257 | static int symtable_visit_pattern(struct symtable *st, pattern_ty s);
|
253 | 258 | static int symtable_raise_if_annotation_block(struct symtable *st, const char *, expr_ty);
|
| 259 | +static int symtable_raise_if_not_coroutine(struct symtable *st, const char *msg, _Py_SourceLocation loc); |
254 | 260 | static int symtable_raise_if_comprehension_block(struct symtable *st, expr_ty);
|
255 | 261 |
|
256 | 262 | /* For debugging purposes only */
|
@@ -2048,11 +2054,17 @@ symtable_visit_stmt(struct symtable *st, stmt_ty s)
|
2048 | 2054 | }
|
2049 | 2055 | case AsyncWith_kind:
|
2050 | 2056 | maybe_set_ste_coroutine_for_module(st, s);
|
| 2057 | + if (!symtable_raise_if_not_coroutine(st, ASYNC_WITH_OUTISDE_ASYNC_FUNC, LOCATION(s))) { |
| 2058 | + VISIT_QUIT(st, 0); |
| 2059 | + } |
2051 | 2060 | VISIT_SEQ(st, withitem, s->v.AsyncWith.items);
|
2052 | 2061 | VISIT_SEQ(st, stmt, s->v.AsyncWith.body);
|
2053 | 2062 | break;
|
2054 | 2063 | case AsyncFor_kind:
|
2055 | 2064 | maybe_set_ste_coroutine_for_module(st, s);
|
| 2065 | + if (!symtable_raise_if_not_coroutine(st, ASYNC_FOR_OUTISDE_ASYNC_FUNC, LOCATION(s))) { |
| 2066 | + VISIT_QUIT(st, 0); |
| 2067 | + } |
2056 | 2068 | VISIT(st, expr, s->v.AsyncFor.target);
|
2057 | 2069 | VISIT(st, expr, s->v.AsyncFor.iter);
|
2058 | 2070 | VISIT_SEQ(st, stmt, s->v.AsyncFor.body);
|
@@ -2865,6 +2877,16 @@ symtable_raise_if_comprehension_block(struct symtable *st, expr_ty e) {
|
2865 | 2877 | VISIT_QUIT(st, 0);
|
2866 | 2878 | }
|
2867 | 2879 |
|
| 2880 | +static int |
| 2881 | +symtable_raise_if_not_coroutine(struct symtable *st, const char *msg, _Py_SourceLocation loc) { |
| 2882 | + if (!st->st_cur->ste_coroutine) { |
| 2883 | + PyErr_SetString(PyExc_SyntaxError, msg); |
| 2884 | + SET_ERROR_LOCATION(st->st_filename, loc); |
| 2885 | + return 0; |
| 2886 | + } |
| 2887 | + return 1; |
| 2888 | +} |
| 2889 | + |
2868 | 2890 | struct symtable *
|
2869 | 2891 | _Py_SymtableStringObjectFlags(const char *str, PyObject *filename,
|
2870 | 2892 | int start, PyCompilerFlags *flags)
|
|
0 commit comments