mirror of
https://github.com/RustPython/RustPython.git
synced 2026-10-28 18:59:04 +08:00
Address relative import with 3 dots. Closes #1052.
This commit is contained in:
1 parent
0741c36bb8
commit
ff762d7109
3 files changed
+21
-6
No files matched your search
@@ -218,14 +218,19 @@ ImportStatement: ast::LocatedStatement = {
|
||||
};
|
||||
|
||||
ImportFromLocation: (usize, Option<String>) = {
|
||||
<dots: "."*> <name:DottedName> => {
|
||||
(dots.len(), Some(name))
|
||||
<dots: ImportDots*> <name:DottedName> => {
|
||||
(dots.iter().sum(), Some(name))
|
||||
},
|
||||
<dots: "."+> => {
|
||||
(dots.len(), None)
|
||||
<dots: ImportDots+> => {
|
||||
(dots.iter().sum(), None)
|
||||
},
|
||||
};
|
||||
|
||||
ImportDots: usize = {
|
||||
"..." => 3,
|
||||
"." => 1,
|
||||
};
|
||||
|
||||
ImportAsNames: Vec<ast::ImportSymbol> = {
|
||||
<i:Comma<ImportPart<Identifier>>> => i,
|
||||
"(" <i:Comma<ImportPart<Identifier>>> ")" => i,
|
||||
|
||||
@@ -28,3 +28,13 @@ assert 'Lt' in str(n.body[0].value.ops[0])
|
||||
assert 'Gt' in str(n.body[0].value.ops[1])
|
||||
assert n.body[0].value.comparators[0].n == 4
|
||||
assert n.body[0].value.comparators[1].n == 5
|
||||
|
||||
|
||||
n = ast.parse('from ... import a\n')
|
||||
print(n)
|
||||
i = n.body[0]
|
||||
assert i.level == 3
|
||||
assert i.module is None
|
||||
assert i.names[0].name == 'a'
|
||||
assert i.names[0].asname is None
|
||||
|
||||
@@ -222,8 +222,8 @@ fn statement_to_ast(
|
||||
|
||||
fn alias_to_ast(vm: &VirtualMachine, alias: &ast::ImportSymbol) -> PyResult<AstNodeRef> {
|
||||
Ok(node!(vm, alias, {
|
||||
symbol => vm.ctx.new_str(alias.symbol.to_string()),
|
||||
alias => optional_string_to_py_obj(vm, &alias.alias)
|
||||
name => vm.ctx.new_str(alias.symbol.to_string()),
|
||||
asname => optional_string_to_py_obj(vm, &alias.alias)
|
||||
}))
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user