MCPcopy Index your code
hub / github.com/RustPython/RustPython / import_encodings

Method import_encodings

crates/vm/src/vm/mod.rs:779–814  ·  view source on GitHub ↗
(&mut self)

Source from the content-addressed store, hash-verified

777 /// init_importlib must be called before this call
778 #[cfg(feature = "encodings")]
779 fn import_encodings(&mut self) -> PyResult<()> {
780 self.import("encodings", 0).map_err(|import_err| {
781 let rustpythonpath_env = std::env::var("RUSTPYTHONPATH").ok();
782 let pythonpath_env = std::env::var("PYTHONPATH").ok();
783 let env_set = rustpythonpath_env.as_ref().is_some() || pythonpath_env.as_ref().is_some();
784 let path_contains_env = self.state.config.paths.module_search_paths.iter().any(|s| {
785 Some(s.as_str()) == rustpythonpath_env.as_deref() || Some(s.as_str()) == pythonpath_env.as_deref()
786 });
787
788 let guide_message = if cfg!(feature = "freeze-stdlib") {
789 "`rustpython_pylib` may not be set while using `freeze-stdlib` feature. Try using `rustpython::InterpreterBuilder::init_stdlib` or manually call `builder.add_frozen_modules(rustpython_pylib::FROZEN_STDLIB)` in `rustpython_vm::Interpreter::builder()`."
790 } else if !env_set {
791 "Neither RUSTPYTHONPATH nor PYTHONPATH is set. Try setting one of them to the stdlib directory."
792 } else if path_contains_env {
793 "RUSTPYTHONPATH or PYTHONPATH is set, but it doesn't contain the encodings library. If you are customizing the RustPython vm/interpreter, try adding the stdlib directory to the path. If you are developing the RustPython interpreter, it might be a bug during development."
794 } else {
795 "RUSTPYTHONPATH or PYTHONPATH is set, but it wasn't loaded to `PyConfig::paths::module_search_paths`. If you are going to customize the RustPython vm/interpreter, those environment variables are not loaded in the Settings struct by default. Please try creating a customized instance of the Settings struct. If you are developing the RustPython interpreter, it might be a bug during development."
796 };
797
798 let mut msg = format!(
799 "RustPython could not import the encodings module. It usually means something went wrong. Please carefully read the following messages and follow the steps.\n\
800 \n\
801 {guide_message}");
802 if !cfg!(feature = "freeze-stdlib") {
803 msg += "\n\
804 If you don't have access to a consistent external environment (e.g. targeting wasm, embedding \
805 rustpython in another application), try enabling the `freeze-stdlib` feature.\n\
806 If this is intended and you want to exclude the encodings module from your interpreter, please remove the `encodings` feature from `rustpython-vm` crate.";
807 }
808
809 let err = self.new_runtime_error(msg);
810 err.set___cause__(Some(import_err));
811 err
812 })?;
813 Ok(())
814 }
815
816 fn import_ascii_utf8_encodings(&mut self) -> PyResult<()> {
817 // Use the Python import machinery (FrozenImporter) so modules get

Callers 1

initializeMethod · 0.80

Calls 7

okMethod · 0.80
set___cause__Method · 0.80
SomeClass · 0.50
importMethod · 0.45
as_refMethod · 0.45
iterMethod · 0.45
as_strMethod · 0.45

Tested by

no test coverage detected