Skip to content

Commit 42d1713

Browse files
committed
Remove old import
1 parent ee4ee54 commit 42d1713

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;
@@ -49,39 +46,6 @@ pub fn import_builtin(vm: &VirtualMachine, module_name: &str) -> PyResult {
4946
})
5047
}
5148

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

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)