Skip to content

Delay from_list tuple create in import #1330

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 1 commit into from
Aug 31, 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
4 changes: 2 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ fn run_rustpython(vm: &VirtualMachine, matches: &ArgMatches) -> PyResult<()> {
vm.get_attribute(vm.sys_module.clone(), "modules")?
.set_item("__main__", main_module, vm)?;

let site_result = vm.import("site", &vm.ctx.new_tuple(vec![]), 0);
let site_result = vm.import("site", &[], 0);

if site_result.is_err() {
warn!(
Expand Down Expand Up @@ -366,7 +366,7 @@ fn run_command(vm: &VirtualMachine, scope: Scope, source: String) -> PyResult<()

fn run_module(vm: &VirtualMachine, module: &str) -> PyResult<()> {
debug!("Running module {}", module);
let runpy = vm.import("runpy", &vm.ctx.new_tuple(vec![]), 0)?;
let runpy = vm.import("runpy", &[], 0)?;
let run_module_as_main = vm.get_attribute(runpy, "_run_module_as_main")?;
vm.invoke(&run_module_as_main, vec![vm.new_str(module.to_owned())])?;
Ok(())
Expand Down
6 changes: 1 addition & 5 deletions vm/src/frame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -539,11 +539,7 @@ impl Frame {
level: usize,
) -> FrameResult {
let module = module.clone().unwrap_or_default();
let from_list = symbols
.iter()
.map(|symbol| vm.ctx.new_str(symbol.to_string()))
.collect();
let module = vm.import(&module, &vm.ctx.new_tuple(from_list), level)?;
let module = vm.import(&module, symbols, level)?;

self.push_value(module);
Ok(None)
Expand Down
3 changes: 1 addition & 2 deletions vm/src/import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ pub fn init_importlib(vm: &VirtualMachine, external: bool) -> PyResult {
vm.get_attribute(importlib.clone(), "_install_external_importers")?;
vm.invoke(&install_external, vec![])?;
// Set pyc magic number to commit hash. Should be changed when bytecode will be more stable.
let importlib_external =
vm.import("_frozen_importlib_external", &vm.ctx.new_tuple(vec![]), 0)?;
let importlib_external = vm.import("_frozen_importlib_external", &[], 0)?;
let mut magic = get_git_revision().into_bytes();
magic.truncate(4);
if magic.len() != 4 {
Expand Down
2 changes: 1 addition & 1 deletion vm/src/obj/objmodule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ impl PyModuleRef {
}

fn repr(self, vm: &VirtualMachine) -> PyResult {
let importlib = vm.import("_frozen_importlib", &vm.ctx.new_tuple(vec![]), 0)?;
let importlib = vm.import("_frozen_importlib", &[], 0)?;
let module_repr = vm.get_attribute(importlib, "_module_repr")?;
vm.invoke(&module_repr, vec![self.into_object()])
}
Expand Down
2 changes: 1 addition & 1 deletion vm/src/stdlib/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -648,7 +648,7 @@ pub fn io_open(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
}
};

let io_module = vm.import("_io", &vm.ctx.new_tuple(vec![]), 0)?;
let io_module = vm.import("_io", &[], 0)?;

// Construct a FileIO (subclass of RawIOBase)
// This is subsequently consumed by a Buffered Class.
Expand Down
18 changes: 11 additions & 7 deletions vm/src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,15 +260,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![]), 0)?, class)?
.get_attribute(self.import(module, &[], 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![]), 0)
.import(module, &[], 0)
.unwrap_or_else(|_| panic!("unable to import {}", module));
let class = self
.get_attribute(module.clone(), class)
Expand Down Expand Up @@ -461,12 +461,10 @@ impl VirtualMachine {
Ok(self.new_str(ascii))
}

pub fn import(&self, module: &str, from_list: &PyObjectRef, level: usize) -> PyResult {
pub fn import(&self, module: &str, from_list: &[String], level: usize) -> PyResult {
// if the import inputs seem weird, e.g a package import or something, rather than just
// a straight `import ident`
let weird = module.contains('.')
|| level != 0
|| objbool::boolval(self, from_list.clone()).unwrap_or(true);
let weird = module.contains('.') || level != 0 || !from_list.is_empty();

let cached_module = if weird {
None
Expand All @@ -490,13 +488,19 @@ impl VirtualMachine {
} else {
(self.get_none(), self.get_none())
};
let from_list = self.ctx.new_tuple(
from_list
.iter()
.map(|name| self.new_str(name.to_string()))
.collect(),
);
self.invoke(
&import_func,
vec![
self.new_str(module.to_owned()),
globals,
locals,
from_list.clone(),
from_list,
self.ctx.new_int(level),
],
)
Expand Down