From ee4ee5456fd248dbe6ba00fa7e3fb8224154d123 Mon Sep 17 00:00:00 2001 From: Aviv Palivoda Date: Sun, 30 Jun 2019 20:26:42 +0300 Subject: [PATCH 1/4] Use vm.import when running with -m --- src/main.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/main.rs b/src/main.rs index c9f74fea0a0..d1b33ab4e05 100644 --- a/src/main.rs +++ b/src/main.rs @@ -98,8 +98,7 @@ fn run_command(vm: &VirtualMachine, mut source: String) -> PyResult { fn run_module(vm: &VirtualMachine, module: &str) -> PyResult { debug!("Running module {}", module); - let current_path = PathBuf::from("."); - import::import_module(vm, current_path, module) + vm.import(module, &vm.ctx.new_tuple(vec![]), 0) } fn run_script(vm: &VirtualMachine, script_file: &str) -> PyResult { From 42d17138ebb56fdb1b37e4e6ac21437c5c01e01c Mon Sep 17 00:00:00 2001 From: Aviv Palivoda Date: Wed, 3 Jul 2019 19:06:36 +0300 Subject: [PATCH 2/4] Remove old import --- vm/src/import.rs | 64 +---------------------------------------- vm/src/stdlib/io.rs | 5 +--- vm/src/stdlib/thread.rs | 4 +-- 3 files changed, 3 insertions(+), 70 deletions(-) diff --git a/vm/src/import.rs b/vm/src/import.rs index 37fcca3385a..4d94a3f1089 100644 --- a/vm/src/import.rs +++ b/vm/src/import.rs @@ -2,13 +2,10 @@ * Import mechanics */ -use std::path::PathBuf; - use crate::bytecode::CodeObject; use crate::frame::Scope; -use crate::obj::{objcode, objsequence, objstr}; +use crate::obj::objcode; use crate::pyobject::{ItemProtocol, PyResult, PyValue}; -use crate::util; use crate::vm::VirtualMachine; #[cfg(feature = "rustpython-compiler")] use rustpython_compiler::compile; @@ -49,39 +46,6 @@ pub fn import_builtin(vm: &VirtualMachine, module_name: &str) -> PyResult { }) } -pub fn import_module(vm: &VirtualMachine, current_path: PathBuf, module_name: &str) -> PyResult { - // Cached modules: - let sys_modules = vm.get_attribute(vm.sys_module.clone(), "modules").unwrap(); - - // First, see if we already loaded the module: - if let Ok(module) = sys_modules.get_item(module_name.to_string(), vm) { - Ok(module) - } else if vm.frozen.borrow().contains_key(module_name) { - import_frozen(vm, module_name) - } else if vm.stdlib_inits.borrow().contains_key(module_name) { - import_builtin(vm, module_name) - } else if cfg!(feature = "rustpython-compiler") { - let notfound_error = &vm.ctx.exceptions.module_not_found_error; - let import_error = &vm.ctx.exceptions.import_error; - - // Time to search for module in any place: - let file_path = find_source(vm, current_path, module_name) - .map_err(|e| vm.new_exception(notfound_error.clone(), e))?; - let source = util::read_file(file_path.as_path()) - .map_err(|e| vm.new_exception(import_error.clone(), e.to_string()))?; - - import_file( - vm, - module_name, - file_path.to_str().unwrap().to_string(), - source, - ) - } else { - let notfound_error = &vm.ctx.exceptions.module_not_found_error; - Err(vm.new_exception(notfound_error.clone(), module_name.to_string())) - } -} - #[cfg(feature = "rustpython-compiler")] pub fn import_file( vm: &VirtualMachine, @@ -118,29 +82,3 @@ pub fn import_codeobj( )?; Ok(module) } - -fn find_source(vm: &VirtualMachine, current_path: PathBuf, name: &str) -> Result { - let sys_path = vm.get_attribute(vm.sys_module.clone(), "path").unwrap(); - let mut paths: Vec = objsequence::get_elements_list(&sys_path) - .iter() - .map(|item| PathBuf::from(objstr::get_value(item))) - .collect(); - - paths.insert(0, current_path); - - let rel_name = name.replace('.', "/"); - let suffixes = [".py", "/__init__.py"]; - let mut file_paths = vec![]; - for path in paths { - for suffix in suffixes.iter() { - let mut file_path = path.clone(); - file_path.push(format!("{}{}", rel_name, suffix)); - file_paths.push(file_path); - } - } - - match file_paths.iter().find(|p| p.exists()) { - Some(path) => Ok(path.to_path_buf()), - None => Err(format!("No module named '{}'", name)), - } -} diff --git a/vm/src/stdlib/io.rs b/vm/src/stdlib/io.rs index 2d0b2286c15..f092953f306 100644 --- a/vm/src/stdlib/io.rs +++ b/vm/src/stdlib/io.rs @@ -6,14 +6,11 @@ use std::io::prelude::*; use std::io::Cursor; use std::io::SeekFrom; -use std::path::PathBuf; - use num_bigint::ToBigInt; use num_traits::ToPrimitive; use super::os; use crate::function::{OptionalArg, PyFuncArgs}; -use crate::import; use crate::obj::objbytearray::PyByteArray; use crate::obj::objbytes; use crate::obj::objbytes::PyBytes; @@ -532,7 +529,7 @@ pub fn io_open(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult { } }; - let io_module = import::import_module(vm, PathBuf::default(), "io").unwrap(); + let io_module = vm.import("_io", &vm.ctx.new_tuple(vec![]), 0)?; // Construct a FileIO (subclass of RawIOBase) // This is subsequently consumed by a Buffered Class. diff --git a/vm/src/stdlib/thread.rs b/vm/src/stdlib/thread.rs index 6155365490a..d6df4cb761a 100644 --- a/vm/src/stdlib/thread.rs +++ b/vm/src/stdlib/thread.rs @@ -2,10 +2,8 @@ /// support threading use super::super::pyobject::PyObjectRef; use crate::function::PyFuncArgs; -use crate::import; use crate::pyobject::PyResult; use crate::vm::VirtualMachine; -use std::path::PathBuf; fn rlock_acquire(vm: &VirtualMachine, _args: PyFuncArgs) -> PyResult { Ok(vm.get_none()) @@ -38,7 +36,7 @@ fn get_ident(_vm: &VirtualMachine) -> u32 { } fn allocate_lock(vm: &VirtualMachine) -> PyResult { - let module = import::import_module(vm, PathBuf::default(), "_thread")?; + let module = vm.import("_thread", &vm.ctx.new_tuple(vec![]), 0)?; let lock_class = vm.get_attribute(module.clone(), "RLock")?; vm.invoke(lock_class, vec![]) } From bb30d0fce0c8042f8e076eb256f44ea8b55dbc54 Mon Sep 17 00:00:00 2001 From: Aviv Palivoda Date: Thu, 4 Jul 2019 17:36:27 +0300 Subject: [PATCH 3/4] Don't install external imports if there is no compiler --- vm/src/import.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vm/src/import.rs b/vm/src/import.rs index 4d94a3f1089..28727b6bfd2 100644 --- a/vm/src/import.rs +++ b/vm/src/import.rs @@ -17,7 +17,7 @@ pub fn init_importlib(vm: &VirtualMachine, external: bool) -> PyResult { vm.invoke(install, vec![vm.sys_module.clone(), impmod])?; vm.import_func .replace(vm.get_attribute(importlib.clone(), "__import__")?); - if external { + if external && cfg!(feature = "rustpython-compiler") { let install_external = vm.get_attribute(importlib.clone(), "_install_external_importers")?; vm.invoke(install_external, vec![])?; From f740845babf0c8d3d18ae67487c371153cf3781f Mon Sep 17 00:00:00 2001 From: Aviv Palivoda Date: Thu, 4 Jul 2019 17:38:55 +0300 Subject: [PATCH 4/4] Use vm.class --- vm/src/stdlib/thread.rs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/vm/src/stdlib/thread.rs b/vm/src/stdlib/thread.rs index d6df4cb761a..414e7d72e77 100644 --- a/vm/src/stdlib/thread.rs +++ b/vm/src/stdlib/thread.rs @@ -36,9 +36,8 @@ fn get_ident(_vm: &VirtualMachine) -> u32 { } fn allocate_lock(vm: &VirtualMachine) -> PyResult { - let module = vm.import("_thread", &vm.ctx.new_tuple(vec![]), 0)?; - let lock_class = vm.get_attribute(module.clone(), "RLock")?; - vm.invoke(lock_class, vec![]) + let lock_class = vm.class("_thread", "RLock"); + vm.invoke(lock_class.into_object(), vec![]) } pub fn make_module(vm: &VirtualMachine) -> PyObjectRef {