Skip to content

[Windows] os.device_encoding(fd) should support any console fd #87587

Description

@eryksun
BPO 43421
Nosy @pfmoore, @tjguk, @zware, @eryksun, @zooba

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = None
created_at = <Date 2021-03-06.15:04:20.125>
labels = ['3.8', '3.9', 'extension-modules', 'interpreter-core', 'type-bug', 'expert-IO', '3.10', 'OS-windows']
title = 'os.device_encoding(fd) should support any console fd in Windows'
updated_at = <Date 2021-03-06.15:04:20.125>
user = 'https://github.com/eryksun'

bugs.python.org fields:

activity = <Date 2021-03-06.15:04:20.125>
actor = 'eryksun'
assignee = 'none'
closed = False
closed_date = None
closer = None
components = ['Extension Modules', 'Interpreter Core', 'Windows', 'IO']
creation = <Date 2021-03-06.15:04:20.125>
creator = 'eryksun'
dependencies = []
files = []
hgrepos = []
issue_num = 43421
keywords = []
message_count = 1.0
messages = ['388201']
nosy_count = 5.0
nosy_names = ['paul.moore', 'tim.golden', 'zach.ware', 'eryksun', 'steve.dower']
pr_nums = []
priority = 'normal'
resolution = None
stage = None
status = 'open'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue43421'
versions = ['Python 3.8', 'Python 3.9', 'Python 3.10']

Linked PRs

Activity

  1. eryksun commented on Mar 6, 2021

    @eryksun
    ContributorAuthor

    In Windows, os.device_encoding() is hard coded to map file descriptor 0 and descriptors 1 and 2 respectively to the console's input and output code page if isatty(fd) is true. But isatty() is true for any character device, such as "NUL". Also any fd might be a console, by way of dup(), dup2() -- or open() with the device names "CON", "CONIN$", and "CONOUT$".

    The correct device encoding of a console file is needed for use with os.read() and os.write(). It's also necessary for io.TextIOWrapper() if PYTHONLEGACYWINDOWSSTDIO is set.

    _Py_device_encoding() in Python/fileutils.c should use _get_osfhandle() to get the OS handle, and, if it's a character-device file, determine the code page to return, if any, depending on whether it's an input or output console file. For example:

    PyObject *
    _Py_device_encoding(int fd)
    {
    #if defined(MS_WINDOWS)
        HANDLE handle;
        DWORD temp;
        UINT cp = 0;
    
        _Py_BEGIN_SUPPRESS_IPH
            handle = (HANDLE)_get_osfhandle(fd);
        _Py_END_SUPPRESS_IPH
        if (handle == INVALID_HANDLE_VALUE ||
                GetFileType(handle) != FILE_TYPE_CHAR)
            Py_RETURN_NONE;
    
        Py_BEGIN_ALLOW_THREADS
        /* GetConsoleMode requires a console handle. */
        if (!GetConsoleMode(handle, &temp)) {
            /* Assume access denied implies output. */
            if (GetLastError() == ERROR_ACCESS_DENIED)
                cp = GetConsoleOutputCP();
        } else {
            if (GetNumberOfConsoleInputEvents(handle, &temp)) {
                cp = GetConsoleCP();
            } else {
                cp = GetConsoleOutputCP();
            }
        }
        Py_END_ALLOW_THREADS
    
        if (cp == CP_UTF8) {
            return PyUnicode_FromString("UTF-8");
        } else if (cp != 0) {
            return PyUnicode_FromFormat("cp%u", (unsigned int)cp);
        } else {
            Py_RETURN_NONE;
        }
    #else
        if (isatty(fd)) {
            return _Py_GetLocaleEncodingObject();
        } else {
            Py_RETURN_NONE;
        }
    #endif  /* defined(MS_WINDOWS) */
    }
  2. transferred this issue fromon Apr 10, 2022
  3. changed the title [-]os.device_encoding(fd) should support any console fd in Windows[/-] [+][Windows] os.device_encoding(fd) should support any console fd[/+] on Aug 8, 2022
  4. added a commit that references this issue on Aug 29, 2026
  5. added 2 commits that reference this issue on Aug 29, 2026
  6. added a commit that references this issue on Sep 12, 2026
  7. added a commit that references this issue on Oct 10, 2026
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