Skip to content

gh-87587: Fix os.device_encoding() for any console on Windows - #155410

Open
serhiy-storchaka wants to merge 1 commit into
python:mainfrom
serhiy-storchaka:gh-87587-device-encoding
Open

gh-87587: Fix os.device_encoding() for any console on Windows#155410
serhiy-storchaka wants to merge 1 commit into
python:mainfrom
serhiy-storchaka:gh-87587-device-encoding

Conversation

@serhiy-storchaka

Copy link
Copy Markdown
Member

The file handle is now queried to detect a console, as suggested by @eryksun, so any console file descriptor is supported, and other character devices are not mistaken for a console:

>>> f = open('CONOUT$', 'w')
>>> os.device_encoding(f.fileno())
'cp866'                             # was None
>>> n = open('NUL', 'w')
>>> os.dup2(n.fileno(), 1)
>>> os.device_encoding(1)
None                                # was 'cp866'

The UTF-8 code page is also now reported as "utf-8" instead of "cp65001".

It was hard coded to map file descriptors 0, 1 and 2 to the console code
page, but a console can be opened as any file descriptor, and other
character devices, like NUL, are not consoles.

The file handle is now queried.  The UTF-8 code page is also now reported
as "utf-8" instead of "cp65001".
@serhiy-storchaka serhiy-storchaka added needs backport to 3.13 bugs and security fixes needs backport to 3.14 bugs and security fixes needs backport to 3.15 pre-release feature fixes, bugs and security fixes labels Aug 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting core review needs backport to 3.13 bugs and security fixes needs backport to 3.14 bugs and security fixes needs backport to 3.15 pre-release feature fixes, bugs and security fixes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant