@@ -187,6 +187,36 @@ impl VirtualMachine {
187187 vm
188188 }
189189
190+ /// set up the encodings search function
191+ /// init_importlib must be called before this call
192+ #[ cfg( feature = "encodings" ) ]
193+ fn import_encodings ( & mut self ) -> PyResult < ( ) > {
194+ self . import ( "encodings" , None , 0 ) . map_err ( |import_err| {
195+ let err = self . new_runtime_error (
196+ "Could not import encodings. Is your RUSTPYTHONPATH set? If you don't have \
197+ access to a consistent external environment (e.g. if you're embedding \
198+ rustpython in another application), try enabling the freeze-stdlib feature"
199+ . to_owned ( ) ,
200+ ) ;
201+ err. set_cause ( Some ( import_err) ) ;
202+ err
203+ } ) ?;
204+ Ok ( ( ) )
205+ }
206+
207+ /// init only utf-8 encoding
208+ #[ cfg( not( feature = "encodings" ) ) ]
209+ fn import_encodings ( & mut self ) -> PyResult < ( ) > {
210+ import:: import_frozen ( self , "codecs" ) ?;
211+ let encoding_module = import:: import_frozen ( self , "encodings_utf_8" ) ?;
212+ let getregentry = encoding_module. get_attr ( "getregentry" , self ) ?;
213+ let codec_info = self . invoke ( & getregentry, ( ) ) ?;
214+ self . state
215+ . codec_registry
216+ . register_manual ( "utf-8" , codec_info. try_into_value ( self ) ?) ?;
217+ Ok ( ( ) )
218+ }
219+
190220 fn initialize ( & mut self ) {
191221 flame_guard ! ( "init VirtualMachine" ) ;
192222
@@ -202,17 +232,7 @@ impl VirtualMachine {
202232 import:: import_builtin ( self , "_signal" ) ?;
203233 import:: init_importlib ( self , self . state . settings . allow_external_library ) ?;
204234
205- // set up the encodings search function
206- self . import ( "encodings" , None , 0 ) . map_err ( |import_err| {
207- let err = self . new_runtime_error (
208- "Could not import encodings. Is your RUSTPYTHONPATH set? If you don't have \
209- access to a consistent external environment (e.g. if you're embedding \
210- rustpython in another application), try enabling the freeze-stdlib feature"
211- . to_owned ( ) ,
212- ) ;
213- err. set_cause ( Some ( import_err) ) ;
214- err
215- } ) ?;
235+ self . import_encodings ( ) ?;
216236
217237 #[ cfg( any( not( target_arch = "wasm32" ) , target_os = "wasi" ) ) ]
218238 {
0 commit comments