Skip to content

Commit 019388b

Browse files
committed
Remove old import
1 parent d430943 commit 019388b

File tree

3 files changed

+3
-70
lines changed

3 files changed

+3
-70
lines changed

vm/src/import.rs

+1-63
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,10 @@
22
* Import mechanics
33
*/
44

5-
use std::path::PathBuf;
6-
75
use crate::bytecode::CodeObject;
86
use crate::frame::Scope;
9-
use crate::obj::{objcode, objsequence, objstr};
7+
use crate::obj::objcode;
108
use crate::pyobject::{ItemProtocol, PyResult, PyValue};
11-
use crate::util;
129
use crate::vm::VirtualMachine;
1310
#[cfg(feature = "rustpython-compiler")]
1411
use rustpython_compiler::compile;
@@ -46,39 +43,6 @@ pub fn import_builtin(vm: &VirtualMachine, module_name: &str) -> PyResult {
4643
})
4744
}
4845

49-
pub fn import_module(vm: &VirtualMachine, current_path: PathBuf, module_name: &str) -> PyResult {
50-
// Cached modules:
51-
let sys_modules = vm.get_attribute(vm.sys_module.clone(), "modules").unwrap();
52-
53-
// First, see if we already loaded the module:
54-
if let Ok(module) = sys_modules.get_item(module_name.to_string(), vm) {
55-
Ok(module)
56-
} else if vm.frozen.borrow().contains_key(module_name) {
57-
import_frozen(vm, module_name)
58-
} else if vm.stdlib_inits.borrow().contains_key(module_name) {
59-
import_builtin(vm, module_name)
60-
} else if cfg!(feature = "rustpython-compiler") {
61-
let notfound_error = &vm.ctx.exceptions.module_not_found_error;
62-
let import_error = &vm.ctx.exceptions.import_error;
63-
64-
// Time to search for module in any place:
65-
let file_path = find_source(vm, current_path, module_name)
66-
.map_err(|e| vm.new_exception(notfound_error.clone(), e))?;
67-
let source = util::read_file(file_path.as_path())
68-
.map_err(|e| vm.new_exception(import_error.clone(), e.to_string()))?;
69-
70-
import_file(
71-
vm,
72-
module_name,
73-
file_path.to_str().unwrap().to_string(),
74-
source,
75-
)
76-
} else {
77-
let notfound_error = &vm.ctx.exceptions.module_not_found_error;
78-
Err(vm.new_exception(notfound_error.clone(), module_name.to_string()))
79-
}
80-
}
81-
8246
#[cfg(feature = "rustpython-compiler")]
8347
pub fn import_file(
8448
vm: &VirtualMachine,
@@ -115,29 +79,3 @@ pub fn import_codeobj(
11579
)?;
11680
Ok(module)
11781
}
118-
119-
fn find_source(vm: &VirtualMachine, current_path: PathBuf, name: &str) -> Result<PathBuf, String> {
120-
let sys_path = vm.get_attribute(vm.sys_module.clone(), "path").unwrap();
121-
let mut paths: Vec<PathBuf> = objsequence::get_elements_list(&sys_path)
122-
.iter()
123-
.map(|item| PathBuf::from(objstr::get_value(item)))
124-
.collect();
125-
126-
paths.insert(0, current_path);
127-
128-
let rel_name = name.replace('.', "/");
129-
let suffixes = [".py", "/__init__.py"];
130-
let mut file_paths = vec![];
131-
for path in paths {
132-
for suffix in suffixes.iter() {
133-
let mut file_path = path.clone();
134-
file_path.push(format!("{}{}", rel_name, suffix));
135-
file_paths.push(file_path);
136-
}
137-
}
138-
139-
match file_paths.iter().find(|p| p.exists()) {
140-
Some(path) => Ok(path.to_path_buf()),
141-
None => Err(format!("No module named '{}'", name)),
142-
}
143-
}

vm/src/stdlib/io.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,11 @@ use std::io::prelude::*;
66
use std::io::Cursor;
77
use std::io::SeekFrom;
88

9-
use std::path::PathBuf;
10-
119
use num_bigint::ToBigInt;
1210
use num_traits::ToPrimitive;
1311

1412
use super::os;
1513
use crate::function::{OptionalArg, PyFuncArgs};
16-
use crate::import;
1714
use crate::obj::objbytearray::PyByteArray;
1815
use crate::obj::objbytes;
1916
use crate::obj::objbytes::PyBytes;
@@ -532,7 +529,7 @@ pub fn io_open(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
532529
}
533530
};
534531

535-
let io_module = import::import_module(vm, PathBuf::default(), "io").unwrap();
532+
let io_module = vm.import("_io", &vm.ctx.new_tuple(vec![]), 0)?;
536533

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

vm/src/stdlib/thread.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,8 @@
22
/// support threading
33
use super::super::pyobject::PyObjectRef;
44
use crate::function::PyFuncArgs;
5-
use crate::import;
65
use crate::pyobject::PyResult;
76
use crate::vm::VirtualMachine;
8-
use std::path::PathBuf;
97

108
fn rlock_acquire(vm: &VirtualMachine, _args: PyFuncArgs) -> PyResult {
119
Ok(vm.get_none())
@@ -38,7 +36,7 @@ fn get_ident(_vm: &VirtualMachine) -> u32 {
3836
}
3937

4038
fn allocate_lock(vm: &VirtualMachine) -> PyResult {
41-
let module = import::import_module(vm, PathBuf::default(), "_thread")?;
39+
let module = vm.import("_thread", &vm.ctx.new_tuple(vec![]), 0)?;
4240
let lock_class = vm.get_attribute(module.clone(), "RLock")?;
4341
vm.invoke(lock_class, vec![])
4442
}

0 commit comments

Comments
 (0)