Skip to content
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
3 changes: 1 addition & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
66 changes: 2 additions & 64 deletions vm/src/import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -20,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![])?;
Expand Down Expand Up @@ -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") {
Copy link
Contributor

Choose a reason for hiding this comment

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

The point of this feature flag was to check if rustpython was built with bytecode compiler support. If not, the module cannot be loaded from disc. I think importlib.py has a similar mechanism. Please refer to the function imp_exec_builtin in imp.rs. Maybe the logic deleted here, can be replicated over there?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Only the external_importers use compile now. So I changed init_importlib to not install them if there is not 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,
Expand Down Expand Up @@ -118,29 +82,3 @@ pub fn import_codeobj(
)?;
Ok(module)
}

fn find_source(vm: &VirtualMachine, current_path: PathBuf, name: &str) -> Result<PathBuf, String> {
let sys_path = vm.get_attribute(vm.sys_module.clone(), "path").unwrap();
let mut paths: Vec<PathBuf> = 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)),
}
}
5 changes: 1 addition & 4 deletions vm/src/stdlib/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
Expand Down
7 changes: 2 additions & 5 deletions vm/src/stdlib/thread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down Expand Up @@ -38,9 +36,8 @@ fn get_ident(_vm: &VirtualMachine) -> u32 {
}

fn allocate_lock(vm: &VirtualMachine) -> PyResult {
let module = import::import_module(vm, PathBuf::default(), "_thread")?;
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 {
Expand Down