Skip to content

Support relative import #1050

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions tests/snippets/dir_module/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from .relative import value
from .dir_module_inner import value2
3 changes: 3 additions & 0 deletions tests/snippets/dir_module/dir_module_inner/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from ..relative import value

value2 = value + 2
1 change: 1 addition & 0 deletions tests/snippets/dir_module/relative.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
value = 5
3 changes: 3 additions & 0 deletions tests/snippets/import_module.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import dir_module
assert dir_module.value == 5
assert dir_module.value2 == 7
7 changes: 5 additions & 2 deletions vm/src/frame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -912,7 +912,9 @@ impl Frame {
.iter()
.map(|symbol| vm.ctx.new_str(symbol.to_string()))
.collect();
let module = vm.import(module, &vm.ctx.new_tuple(from_list))?;
let level = module.chars().take_while(|char| *char == '.').count();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of splitting of the level here, it's better to modify the parser to be able to handle this such that have AST nodes with a level inside. This way, the level is already known at parse time, and is passed in as a second argument. See also: https://greentreesnakes.readthedocs.io/en/latest/nodes.html#imports

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might be a big change tough, so it can be done in a follow up pull request.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure. I will do that in a different PR.

let module_name = &module[level..];
let module = vm.import(module_name, &vm.ctx.new_tuple(from_list), level)?;

if symbols.is_empty() {
self.push_value(module);
Expand All @@ -928,7 +930,8 @@ impl Frame {
}

fn import_star(&self, vm: &VirtualMachine, module: &str) -> FrameResult {
let module = vm.import(module, &vm.ctx.new_tuple(vec![]))?;
let level = module.chars().take_while(|char| *char == '.').count();
let module = vm.import(module, &vm.ctx.new_tuple(vec![]), level)?;

// Grab all the names from the module and put them in the context
if let Some(dict) = &module.dict {
Expand Down
13 changes: 9 additions & 4 deletions vm/src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,15 +136,15 @@ impl VirtualMachine {

pub fn try_class(&self, module: &str, class: &str) -> PyResult<PyClassRef> {
let class = self
.get_attribute(self.import(module, &self.ctx.new_tuple(vec![]))?, class)?
.get_attribute(self.import(module, &self.ctx.new_tuple(vec![]), 0)?, class)?
.downcast()
.expect("not a class");
Ok(class)
}

pub fn class(&self, module: &str, class: &str) -> PyClassRef {
let module = self
.import(module, &self.ctx.new_tuple(vec![]))
.import(module, &self.ctx.new_tuple(vec![]), 0)
.unwrap_or_else(|_| panic!("unable to import {}", module));
let class = self
.get_attribute(module.clone(), class)
Expand Down Expand Up @@ -302,7 +302,7 @@ impl VirtualMachine {
TryFromObject::try_from_object(self, repr)
}

pub fn import(&self, module: &str, from_list: &PyObjectRef) -> PyResult {
pub fn import(&self, module: &str, from_list: &PyObjectRef, level: usize) -> PyResult {
let sys_modules = self
.get_attribute(self.sys_module.clone(), "modules")
.unwrap();
Expand All @@ -314,9 +314,14 @@ impl VirtualMachine {
func,
vec![
self.ctx.new_str(module.to_string()),
self.get_none(),
if self.current_frame().is_some() {
self.get_locals().into_object()
} else {
self.get_none()
},
self.get_none(),
from_list.clone(),
self.ctx.new_int(level),
],
),
Err(_) => Err(self.new_exception(
Expand Down