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

Skip to content

Commit e66c8c7

Browse files
committed
"from ... import x" should not be a syntax error... make
import_stmt accept ELLIPSes and DOTs.
1 parent d16e81a commit e66c8c7

2 files changed

Lines changed: 8 additions & 3 deletions

File tree

Grammar/Grammar

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ yield_stmt: yield_expr
5555
raise_stmt: 'raise' [test [',' test [',' test]]]
5656
import_stmt: import_name | import_from
5757
import_name: 'import' dotted_as_names
58-
import_from: ('from' ('.'* dotted_name | '.'+)
58+
# note below: the ('.' | '...') is necessary because '...' is tokenized as ELLIPSIS
59+
import_from: ('from' (('.' | '...')* dotted_name | ('.' | '...')+)
5960
'import' ('*' | '(' import_as_names ')' | import_as_names))
6061
import_as_name: NAME ['as' NAME]
6162
dotted_as_name: dotted_name ['as' NAME]

Python/ast.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2406,8 +2406,8 @@ ast_for_import_stmt(struct compiling *c, const node *n)
24062406
/*
24072407
import_stmt: import_name | import_from
24082408
import_name: 'import' dotted_as_names
2409-
import_from: 'from' ('.'* dotted_name | '.') 'import'
2410-
('*' | '(' import_as_names ')' | import_as_names)
2409+
import_from: 'from' (('.' | '...')* dotted_name | ('.' | '...')+)
2410+
'import' ('*' | '(' import_as_names ')' | import_as_names)
24112411
*/
24122412
int lineno;
24132413
int col_offset;
@@ -2445,6 +2445,10 @@ ast_for_import_stmt(struct compiling *c, const node *n)
24452445
mod = alias_for_import_name(c, CHILD(n, idx));
24462446
idx++;
24472447
break;
2448+
} else if (TYPE(CHILD(n, idx)) == ELLIPSIS) {
2449+
/* three consecutive dots are tokenized as one ELLIPSIS */
2450+
ndots += 3;
2451+
continue;
24482452
} else if (TYPE(CHILD(n, idx)) != DOT) {
24492453
break;
24502454
}

0 commit comments

Comments
 (0)