8000 Address relative import with 3 dots. Closes #1052. · rmliddle/RustPython@ff762d7 · GitHub
[go: up one dir, main page]

Skip to content

Commit ff762d7

Browse files
committed
Address relative import with 3 dots. Closes RustPython#1052.
1 parent 0741c36 commit ff762d7

File tree

3 files changed

+21
-6
lines changed

3 files changed

+21
-6
lines changed

parser/src/python.lalrpop

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -218,14 +218,19 @@ ImportStatement: ast::LocatedStatement = {
218218
};
219219

220220
ImportFromLocation: (usize, Option<String>) = {
221-
<dots: "."*> <name:DottedName> => {
222-
(dots.len(), Some(name))
221+
<dots: ImportDots*> <name:DottedName> => {
222+
(dots.iter().sum(), Some(name))
223223
},
224-
<dots: "."+> => {
225-
(dots.len(), None)
224+
<dots: ImportDots+> => {
225+
(dots.iter().sum(), None)
226226
},
227227
};
228228

229+
ImportDots: usize = {
230+
"..." => 3,
231+
"." => 1,
232+
};
233+
229234
ImportAsNames: Vec<ast::ImportSymbol> = {
230235
<i:Comma<ImportPart<Identifier>>> => i,
231236
"(" <i:Comma<ImportPart<Identifier>>> ")" => i,

tests/snippets/ast_snippet.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,13 @@ def foo():
2828
assert 'Gt' in str(n.body[0].value.ops[1])
2929
assert n.body[0].value.comparators[0].n == 4
3030
assert n.body[0].value.comparators[1].n == 5
31+
32+
33+
n = ast.parse('from ... import a\n')
34+
print(n)
35+
i = n.body[0]
36+
assert i.level == 3
37+
assert i.module is None
38+
assert i.names[0].name == 'a'
39+
assert i.names[0].asname is None
40+

vm/src/stdlib/ast.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -222,8 +222,8 @@ fn statement_to_ast(
222222

223223
fn alias_to_ast(vm: &VirtualMachine, alias: &ast::ImportSymbol) -> PyResult<AstNodeRef> {
224224
Ok(node!(vm, alias, {
225-
symbol => vm.ctx.new_str(alias.symbol.to_string()),
226-
alias => optional_string_to_py_obj(vm, &alias.alias)
225+
name => vm.ctx.new_str(alias.symbol.to_string()),
226+
asname => optional_string_to_py_obj(vm, &alias.alias)
227227
}))
228228
}
229229

0 commit comments

Comments
 (0)
0