Skip to content

Remove console encoding support from TextIOWrapper. #91526

Description

@methane

Currently, TextIOWrapper.__init__ calls os.device_encoding(file.fileno()) when encoding is not specified and fileno is 0-2 (e.g. open(0)).

However, sys.stdin, stdout, and stderr don't use it even when PYTHONLEGACYWINDOWSSTDIO=1.
config->stdio_encoding is initialized by GetACP(), and create_stdio() passes config->stdio_encoding to TextIOWrapper.

How about removing os.device_encoding(file.fileno()) from TextIOWrapper.__init__()?

  • Thanks to WindowsConsoleIO, most user don't use the console encoding at all.
  • WindowsConsoleIO require UTF-8. So TextIOWrapper(sys.stdout) cause mojibake when sys.stdout is WindowsConsoleIO
  • The only use case of PYTHONLEGACYWINDOWSSTDIO I know is this. But this use case don't create TextIOWrapper for fd=0,1,2.
  • This TextIOWrapper behavior is never documented. It makes TextIOWrapper.__init__ complicated and inconsistent.
  • If we want to use console encoding for stdio when PYTHONLEGACYWINDOWSSTDIO is set, we can fix it in create_stdio(). create_stdio() has special case for WindowsConsoleIO already. (here)

Ping: @vstinner @eryksun @zooba

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions