mirror of
https://github.com/RustPython/RustPython.git
synced 2026-10-29 03:04:38 +08:00
Delay from_list tuple create in import
This commit is contained in:
1 parent
39f54aa592
commit
3d61129cfd
6 files changed
+17
-18
No files matched your search
+2
-2
@@ -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(())
|
||||
|
||||
+1
-5
@@ -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)
|
||||
|
||||
+1
-2
@@ -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 {
|
||||
|
||||
@@ -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()])
|
||||
}
|
||||
|
||||
+1
-1
@@ -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.
|
||||
|
||||
+11
-7
@@ -260,7 +260,7 @@ 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)
|
||||
@@ -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),
|
||||
],
|
||||
)
|
||||
|
||||
Reference in new issue
Block a user