Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 106203c

Browse files
committed
Clean up from-import handling.
1 parent b79afb6 commit 106203c

1 file changed

Lines changed: 21 additions & 22 deletions

File tree

Python/ast.c

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2170,44 +2170,43 @@ ast_for_import_stmt(struct compiling *c, const node *n)
21702170
}
21712171
else if (STR(CHILD(n, 0))[0] == 'f') { /* from */
21722172
int n_children;
2173-
const char *from_modules;
21742173
int lineno = LINENO(n);
21752174
alias_ty mod = alias_for_import_name(c, CHILD(n, 1));
21762175
if (!mod)
21772176
return NULL;
21782177

2179-
/* XXX this needs to be cleaned up */
2180-
2181-
from_modules = STR(CHILD(n, 3));
2182-
if (!from_modules) {
2183-
n = CHILD(n, 3); /* from ... import x, y, z */
2184-
if (NCH(n) % 2 == 0) {
2185-
/* it ends with a comma, not valid but the parser allows it */
2178+
switch (TYPE(CHILD(n, 3))) {
2179+
case STAR:
2180+
/* from ... import * */
2181+
n = CHILD(n, 3);
2182+
n_children = 1;
2183+
break;
2184+
case LPAR:
2185+
/* from ... import (x, y, z) */
2186+
n = CHILD(n, 4);
2187+
n_children = NCH(n);
2188+
break;
2189+
case import_as_names:
2190+
/* from ... import x, y, z */
2191+
n = CHILD(n, 3);
2192+
n_children = NCH(n);
2193+
if (n_children % 2 == 0) {
21862194
ast_error(n, "trailing comma not allowed without"
21872195
" surrounding parentheses");
21882196
return NULL;
21892197
}
2190-
}
2191-
else if (from_modules[0] == '*') {
2192-
n = CHILD(n, 3); /* from ... import * */
2193-
}
2194-
else if (from_modules[0] == '(')
2195-
n = CHILD(n, 4); /* from ... import (x, y, z) */
2196-
else {
2197-
/* XXX: don't we need to call ast_error(n, "..."); */
2198-
return NULL;
2198+
break;
2199+
default:
2200+
ast_error(n, "Unexpected node-type in from-import");
2201+
return NULL;
21992202
}
22002203

2201-
n_children = NCH(n);
2202-
if (from_modules && from_modules[0] == '*')
2203-
n_children = 1;
2204-
22052204
aliases = asdl_seq_new((n_children + 1) / 2, c->c_arena);
22062205
if (!aliases)
22072206
return NULL;
22082207

22092208
/* handle "from ... import *" special b/c there's no children */
2210-
if (from_modules && from_modules[0] == '*') {
2209+
if (TYPE(n) == STAR) {
22112210
alias_ty import_alias = alias_for_import_name(c, n);
22122211
if (!import_alias)
22132212
return NULL;

0 commit comments

Comments
 (0)