Skip to content

Commit a457153

Browse files
committed
Consider imports relative to their source
Currently they are considered relative to the current working directory of the interpreter, which is incorrect. Fixes RustPython#83.
1 parent 767ceae commit a457153

File tree

3 files changed

+15
-5
lines changed

3 files changed

+15
-5
lines changed

tests/test_snippets.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ def perform_test(filename, method, test_type):
5454
def run_via_cpython(filename):
5555
""" Simply invoke python itself on the script """
5656
env = os.environ.copy()
57-
env['PYTHONPATH'] = '.'
5857
subprocess.check_call([sys.executable, filename], env=env)
5958

6059

@@ -78,7 +77,6 @@ def run_via_rustpython(filename, test_type):
7877
log_level = 'info' if test_type == _TestType.benchmark else 'trace'
7978
env['RUST_LOG'] = '{},cargo=error,jobserver=error'.format(log_level)
8079
env['RUST_BACKTRACE'] = '1'
81-
env['PYTHONPATH'] = os.path.dirname(filename)
8280
subprocess.check_call(
8381
['cargo', 'run', '--release', filename], env=env)
8482

vm/src/import.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ pub fn import(vm: &mut VirtualMachine, module: &String, symbol: &Option<String>)
7070

7171
fn find_source(vm: &VirtualMachine, name: &String) -> io::Result<PathBuf> {
7272
let sys_path = vm.sys_module.get_item("path").unwrap();
73-
let paths: Vec<PathBuf> = match sys_path.borrow().kind {
73+
let mut paths: Vec<PathBuf> = match sys_path.borrow().kind {
7474
PyObjectKind::List { ref elements } => elements
7575
.iter()
7676
.filter_map(|item| match item.borrow().kind {
@@ -80,6 +80,19 @@ fn find_source(vm: &VirtualMachine, name: &String) -> io::Result<PathBuf> {
8080
_ => panic!("sys.path unexpectedly not a list"),
8181
};
8282

83+
let source_path = &vm.current_frame().code.source_path;
84+
paths.insert(
85+
0,
86+
match source_path {
87+
Some(source_path) => {
88+
let mut source_pathbuf = PathBuf::from(source_path);
89+
source_pathbuf.pop();
90+
source_pathbuf
91+
}
92+
None => PathBuf::from("."),
93+
},
94+
);
95+
8396
let suffixes = [".py", "/__init__.py"];
8497
let mut filepaths = vec![];
8598
for path in paths {

vm/src/sysmodule.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,12 @@ use std::env;
88
use super::pyobject::{DictProtocol, PyContext, PyObjectRef};
99

1010
pub fn mk_module(ctx: &PyContext) -> PyObjectRef {
11-
let mut path_list = match env::var_os("PYTHONPATH") {
11+
let path_list = match env::var_os("PYTHONPATH") {
1212
Some(paths) => env::split_paths(&paths)
1313
.map(|path| ctx.new_str(path.to_str().unwrap().to_string()))
1414
.collect(),
1515
None => vec![],
1616
};
17-
path_list.insert(0, ctx.new_str("".to_string()));
1817
let path = ctx.new_list(path_list);
1918
let modules = ctx.new_dict();
2019
let sys_name = "sys".to_string();

0 commit comments

Comments
 (0)