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

Method import_ascii_utf8_encodings

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

Source from the content-addressed store, hash-verified

814 }
815
816 fn import_ascii_utf8_encodings(&mut self) -> PyResult<()> {
817 // Use the Python import machinery (FrozenImporter) so modules get
818 // proper __spec__ and __loader__ attributes.
819 self.import("codecs", 0)?;
820
821 // Use dotted names when freeze-stdlib is enabled (modules come from Lib/encodings/),
822 // otherwise use underscored names (modules come from core_modules/).
823 let (ascii_module_name, utf8_module_name) = if cfg!(feature = "freeze-stdlib") {
824 ("encodings.ascii", "encodings.utf_8")
825 } else {
826 ("encodings_ascii", "encodings_utf_8")
827 };
828
829 // Register ascii encoding
830 // __import__("encodings.ascii") returns top-level "encodings", so
831 // look up the actual submodule in sys.modules.
832 self.import(ascii_module_name, 0)?;
833 let sys_modules = self.sys_module.get_attr(identifier!(self, modules), self)?;
834 let ascii_module = sys_modules.get_item(ascii_module_name, self)?;
835 let getregentry = ascii_module.get_attr("getregentry", self)?;
836 let codec_info = getregentry.call((), self)?;
837 self.state
838 .codec_registry
839 .register_manual("ascii", codec_info.try_into_value(self)?)?;
840
841 // Register utf-8 encoding (also as "utf8" alias since normalize_encoding_name
842 // maps "utf-8""utf_8" but leaves "utf8" as-is)
843 self.import(utf8_module_name, 0)?;
844 let utf8_module = sys_modules.get_item(utf8_module_name, self)?;
845 let getregentry = utf8_module.get_attr("getregentry", self)?;
846 let codec_info = getregentry.call((), self)?;
847 let utf8_codec: crate::codecs::PyCodec = codec_info.try_into_value(self)?;
848 self.state
849 .codec_registry
850 .register_manual("utf-8", utf8_codec.clone())?;
851 self.state
852 .codec_registry
853 .register_manual("utf8", utf8_codec)?;
854
855 // Register latin-1 / iso8859-1 aliases needed very early for stdio
856 // bootstrap (e.g. PYTHONIOENCODING=latin-1).
857 if cfg!(feature = "freeze-stdlib") {
858 self.import("encodings.latin_1", 0)?;
859 let latin1_module = sys_modules.get_item("encodings.latin_1", self)?;
860 let getregentry = latin1_module.get_attr("getregentry", self)?;
861 let codec_info = getregentry.call((), self)?;
862 let latin1_codec: crate::codecs::PyCodec = codec_info.try_into_value(self)?;
863 for name in ["latin-1", "latin_1", "latin1", "iso8859-1", "iso8859_1"] {
864 self.state
865 .codec_registry
866 .register_manual(name, latin1_codec.clone())?;
867 }
868 }
869 Ok(())
870 }
871
872 fn initialize(&mut self) {
873 flame_guard!("init VirtualMachine");

Callers 1

initializeMethod · 0.80

Calls 7

register_manualMethod · 0.80
try_into_valueMethod · 0.80
importMethod · 0.45
get_attrMethod · 0.45
get_itemMethod · 0.45
callMethod · 0.45
cloneMethod · 0.45

Tested by

no test coverage detected