Skip to content

Commit 3d61129

Browse files
committed
Delay from_list tuple create in import
1 parent 39f54aa commit 3d61129

File tree

6 files changed

+17
-18
lines changed

6 files changed

+17
-18
lines changed

src/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ fn run_rustpython(vm: &VirtualMachine, matches: &ArgMatches) -> PyResult<()> {
317317
vm.get_attribute(vm.sys_module.clone(), "modules")?
318318
.set_item("__main__", main_module, vm)?;
319319

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

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

367367
fn run_module(vm: &VirtualMachine, module: &str) -> PyResult<()> {
368368
debug!("Running module {}", module);
369-
let runpy = vm.import("runpy", &vm.ctx.new_tuple(vec![]), 0)?;
369+
let runpy = vm.import("runpy", &[], 0)?;
370370
let run_module_as_main = vm.get_attribute(runpy, "_run_module_as_main")?;
371371
vm.invoke(&run_module_as_main, vec![vm.new_str(module.to_owned())])?;
372372
Ok(())

vm/src/frame.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -539,11 +539,7 @@ impl Frame {
539539
level: usize,
540540
) -> FrameResult {
541541
let module = module.clone().unwrap_or_default();
542-
let from_list = symbols
543-
.iter()
544-
.map(|symbol| vm.ctx.new_str(symbol.to_string()))
545-
.collect();
546-
let module = vm.import(&module, &vm.ctx.new_tuple(from_list), level)?;
542+
let module = vm.import(&module, symbols, level)?;
547543

548544
self.push_value(module);
549545
Ok(None)

vm/src/import.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@ pub fn init_importlib(vm: &VirtualMachine, external: bool) -> PyResult {
2626
vm.get_attribute(importlib.clone(), "_install_external_importers")?;
2727
vm.invoke(&install_external, vec![])?;
2828
// Set pyc magic number to commit hash. Should be changed when bytecode will be more stable.
29-
let importlib_external =
30-
vm.import("_frozen_importlib_external", &vm.ctx.new_tuple(vec![]), 0)?;
29+
let importlib_external = vm.import("_frozen_importlib_external", &[], 0)?;
3130
let mut magic = get_git_revision().into_bytes();
3231
magic.truncate(4);
3332
if magic.len() != 4 {

vm/src/obj/objmodule.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ impl PyModuleRef {
7474
}
7575

7676
fn repr(self, vm: &VirtualMachine) -> PyResult {
77-
let importlib = vm.import("_frozen_importlib", &vm.ctx.new_tuple(vec![]), 0)?;
77+
let importlib = vm.import("_frozen_importlib", &[], 0)?;
7878
let module_repr = vm.get_attribute(importlib, "_module_repr")?;
7979
vm.invoke(&module_repr, vec![self.into_object()])
8080
}

vm/src/stdlib/io.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -648,7 +648,7 @@ pub fn io_open(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
648648
}
649649
};
650650

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

653653
// Construct a FileIO (subclass of RawIOBase)
654654
// This is subsequently consumed by a Buffered Class.

vm/src/vm.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -260,15 +260,15 @@ impl VirtualMachine {
260260

261261
pub fn try_class(&self, module: &str, class: &str) -> PyResult<PyClassRef> {
262262
let class = self
263-
.get_attribute(self.import(module, &self.ctx.new_tuple(vec![]), 0)?, class)?
263+
.get_attribute(self.import(module, &[], 0)?, class)?
264264
.downcast()
265265
.expect("not a class");
266266
Ok(class)
267267
}
268268

269269
pub fn class(&self, module: &str, class: &str) -> PyClassRef {
270270
let module = self
271-
.import(module, &self.ctx.new_tuple(vec![]), 0)
271+
.import(module, &[], 0)
272272
.unwrap_or_else(|_| panic!("unable to import {}", module));
273273
let class = self
274274
.get_attribute(module.clone(), class)
@@ -461,12 +461,10 @@ impl VirtualMachine {
461461
Ok(self.new_str(ascii))
462462
}
463463

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

471469
let cached_module = if weird {
472470
None
@@ -490,13 +488,19 @@ impl VirtualMachine {
490488
} else {
491489
(self.get_none(), self.get_none())
492490
};
491+
let from_list = self.ctx.new_tuple(
492+
from_list
493+
.iter()
494+
.map(|name| self.new_str(name.to_string()))
495+
.collect(),
496+
);
493497
self.invoke(
494498
&import_func,
495499
vec![
496500
self.new_str(module.to_owned()),
497501
globals,
498502
locals,
499-
from_list.clone(),
503+
from_list,
500504
self.ctx.new_int(level),
501505
],
502506
)

0 commit comments

Comments
 (0)