From 3d61129cfd13f736be0a55b359a2c4964d0a2406 Mon Sep 17 00:00:00 2001 From: Aviv Palivoda Date: Sat, 31 Aug 2019 11:21:23 +0300 Subject: [PATCH] Delay from_list tuple create in import --- src/main.rs | 4 ++-- vm/src/frame.rs | 6 +----- vm/src/import.rs | 3 +-- vm/src/obj/objmodule.rs | 2 +- vm/src/stdlib/io.rs | 2 +- vm/src/vm.rs | 18 +++++++++++------- 6 files changed, 17 insertions(+), 18 deletions(-) diff --git a/src/main.rs b/src/main.rs index b35bd3f9b0..c35dea2333 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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!( @@ -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(()) diff --git a/vm/src/frame.rs b/vm/src/frame.rs index fc9c5bcc06..b9a912e3ac 100644 --- a/vm/src/frame.rs +++ b/vm/src/frame.rs @@ -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) diff --git a/vm/src/import.rs b/vm/src/import.rs index 267adfc865..cbbc9fce2d 100644 --- a/vm/src/import.rs +++ b/vm/src/import.rs @@ -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 { diff --git a/vm/src/obj/objmodule.rs b/vm/src/obj/objmodule.rs index 9024184984..7fc6c13687 100644 --- a/vm/src/obj/objmodule.rs +++ b/vm/src/obj/objmodule.rs @@ -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()]) } diff --git a/vm/src/stdlib/io.rs b/vm/src/stdlib/io.rs index 22e91012c0..63fa95d9e6 100644 --- a/vm/src/stdlib/io.rs +++ b/vm/src/stdlib/io.rs @@ -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. diff --git a/vm/src/vm.rs b/vm/src/vm.rs index 61adeed92f..0494eadd75 100644 --- a/vm/src/vm.rs +++ b/vm/src/vm.rs @@ -260,7 +260,7 @@ impl VirtualMachine { pub fn try_class(&self, module: &str, class: &str) -> PyResult { 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) @@ -268,7 +268,7 @@ impl VirtualMachine { 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) @@ -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 @@ -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), ], )