@@ -261,17 +261,28 @@ impl VirtualMachine {
261261 Ok ( ( ) )
262262 }
263263
264- fn import_utf8_encodings ( & mut self ) -> PyResult < ( ) > {
264+ fn import_ascii_utf8_encodings ( & mut self ) -> PyResult < ( ) > {
265265 import:: import_frozen ( self , "codecs" ) ?;
266- // FIXME: See corresponding part of `core_frozen_inits`
267- // let encoding_module_name = if cfg!(feature = "freeze-stdlib") {
268- // "encodings.utf_8"
269- // } else {
270- // "encodings_utf_8"
271- // };
272- let encoding_module_name = "encodings_utf_8" ;
273- let encoding_module = import:: import_frozen ( self , encoding_module_name) ?;
274- let getregentry = encoding_module. get_attr ( "getregentry" , self ) ?;
266+
267+ // Use dotted names when freeze-stdlib is enabled (modules come from Lib/encodings/),
268+ // otherwise use underscored names (modules come from core_modules/).
269+ let ( ascii_module_name, utf8_module_name) = if cfg ! ( feature = "freeze-stdlib" ) {
270+ ( "encodings.ascii" , "encodings.utf_8" )
271+ } else {
272+ ( "encodings_ascii" , "encodings_utf_8" )
273+ } ;
274+
275+ // Register ascii encoding
276+ let ascii_module = import:: import_frozen ( self , ascii_module_name) ?;
277+ let getregentry = ascii_module. get_attr ( "getregentry" , self ) ?;
278+ let codec_info = getregentry. call ( ( ) , self ) ?;
279+ self . state
280+ . codec_registry
281+ . register_manual ( "ascii" , codec_info. try_into_value ( self ) ?) ?;
282+
283+ // Register utf-8 encoding
284+ let utf8_module = import:: import_frozen ( self , utf8_module_name) ?;
285+ let getregentry = utf8_module. get_attr ( "getregentry" , self ) ?;
275286 let codec_info = getregentry. call ( ( ) , self ) ?;
276287 self . state
277288 . codec_registry
@@ -298,7 +309,7 @@ impl VirtualMachine {
298309 #[ cfg( not( feature = "threading" ) ) ]
299310 import:: import_frozen ( self , "_thread" ) ?;
300311 let importlib = import:: init_importlib_base ( self ) ?;
301- self . import_utf8_encodings ( ) ?;
312+ self . import_ascii_utf8_encodings ( ) ?;
302313
303314 #[ cfg( any( not( target_arch = "wasm32" ) , target_os = "wasi" ) ) ]
304315 {
@@ -327,17 +338,25 @@ impl VirtualMachine {
327338 let line_buffering = buffered_stdio && ( isatty || fd == 2 ) ;
328339
329340 let newline = if cfg ! ( windows) { None } else { Some ( "\n " ) } ;
330- // stderr uses backslashreplace error handler
331- let errors: Option < & str > = if fd == 2 {
341+ let encoding = self . state . config . settings . stdio_encoding . as_deref ( ) ;
342+ // stderr always uses backslashreplace (ignores stdio_errors)
343+ let errors = if fd == 2 {
332344 Some ( "backslashreplace" )
333345 } else {
334- None
346+ self . state . config . settings . stdio_errors . as_deref ( )
335347 } ;
336348
337349 let stdio = self . call_method (
338350 & io,
339351 "TextIOWrapper" ,
340- ( buf, ( ) , errors, newline, line_buffering, write_through) ,
352+ (
353+ buf,
354+ encoding,
355+ errors,
356+ newline,
357+ line_buffering,
358+ write_through,
359+ ) ,
341360 ) ?;
342361 let mode = if write { "w" } else { "r" } ;
343362 stdio. set_attr ( "mode" , self . ctx . new_str ( mode) , self ) ?;
@@ -1007,6 +1026,8 @@ pub fn resolve_frozen_alias(name: &str) -> &str {
10071026 match name {
10081027 "_frozen_importlib" => "importlib._bootstrap" ,
10091028 "_frozen_importlib_external" => "importlib._bootstrap_external" ,
1029+ "encodings_ascii" => "encodings.ascii" ,
1030+ "encodings_utf_8" => "encodings.utf_8" ,
10101031 _ => name,
10111032 }
10121033}
0 commit comments