mirror of
https://github.com/RustPython/RustPython.git
synced 2026-10-28 18:59:04 +08:00
Merge pull request #4057 from youknowone/stepped-init
staged initialization
This commit is contained in:
4 files changed
+27
-23
No files matched your search
@@ -210,7 +210,6 @@ impl CodecsRegistry {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "encodings"))]
|
||||
pub(crate) fn register_manual(&self, name: &str, codec: PyCodec) -> PyResult<()> {
|
||||
self.inner
|
||||
.write()
|
||||
|
||||
@@ -731,6 +731,8 @@ impl ExceptionZoo {
|
||||
|
||||
extend_exception!(PyImportError, ctx, excs.import_error, {
|
||||
"msg" => ctx.new_readonly_getset("msg", excs.import_error, make_arg_getter(0)),
|
||||
"name" => ctx.none(),
|
||||
"path" => ctx.none(),
|
||||
});
|
||||
extend_exception!(PyModuleNotFoundError, ctx, excs.module_not_found_error);
|
||||
|
||||
|
||||
@@ -12,17 +12,6 @@ use crate::{
|
||||
};
|
||||
use rand::Rng;
|
||||
|
||||
pub(crate) fn init_importlib(
|
||||
vm: &mut VirtualMachine,
|
||||
allow_external_library: bool,
|
||||
) -> PyResult<()> {
|
||||
let importlib = init_importlib_base(vm)?;
|
||||
if allow_external_library && cfg!(feature = "rustpython-compiler") {
|
||||
init_importlib_package(vm, importlib)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) fn init_importlib_base(vm: &mut VirtualMachine) -> PyResult<PyObjectRef> {
|
||||
flame_guard!("init importlib");
|
||||
|
||||
|
||||
+25
-11
@@ -204,11 +204,14 @@ impl VirtualMachine {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// init only utf-8 encoding
|
||||
#[cfg(not(feature = "encodings"))]
|
||||
fn import_encodings(&mut self) -> PyResult<()> {
|
||||
fn import_utf8_encodings(&mut self) -> PyResult<()> {
|
||||
import::import_frozen(self, "codecs")?;
|
||||
let encoding_module = import::import_frozen(self, "encodings_utf_8")?;
|
||||
let encoding_module_name = if cfg!(feature = "freeze-stdlib") {
|
||||
"encodings.utf_8"
|
||||
} else {
|
||||
"encodings_utf_8"
|
||||
};
|
||||
let encoding_module = import::import_frozen(self, encoding_module_name)?;
|
||||
let getregentry = encoding_module.get_attr("getregentry", self)?;
|
||||
let codec_info = self.invoke(&getregentry, ())?;
|
||||
self.state
|
||||
@@ -227,12 +230,11 @@ impl VirtualMachine {
|
||||
stdlib::builtins::make_module(self, self.builtins.clone().into());
|
||||
stdlib::sys::init_module(self, self.sys_module.as_ref(), self.builtins.as_ref());
|
||||
|
||||
let mut inner_init = || -> PyResult<()> {
|
||||
let mut essential_init = || -> PyResult {
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
import::import_builtin(self, "_signal")?;
|
||||
import::init_importlib(self, self.state.settings.allow_external_library)?;
|
||||
|
||||
self.import_encodings()?;
|
||||
let importlib = import::init_importlib_base(self)?;
|
||||
self.import_utf8_encodings()?;
|
||||
|
||||
#[cfg(any(not(target_arch = "wasm32"), target_os = "wasi"))]
|
||||
{
|
||||
@@ -263,12 +265,24 @@ impl VirtualMachine {
|
||||
self.builtins.set_attr("open", io_open, self)?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
Ok(importlib)
|
||||
};
|
||||
|
||||
let res = inner_init();
|
||||
let res = essential_init();
|
||||
let importlib = self.expect_pyresult(res, "essential initialization failed");
|
||||
|
||||
self.expect_pyresult(res, "initialization failed");
|
||||
if self.state.settings.allow_external_library && cfg!(feature = "rustpython-compiler") {
|
||||
if let Err(e) = import::init_importlib_package(self, importlib) {
|
||||
eprintln!("importlib initialization failed. This is critical for many complicated packages.");
|
||||
self.print_exception(e);
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "encodings")]
|
||||
if let Err(e) = self.import_encodings() {
|
||||
eprintln!("encodings initialization failed. Only utf-8 encoding will be supported.");
|
||||
self.print_exception(e);
|
||||
}
|
||||
|
||||
self.initialized = true;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user