Skip to content

gh-86427: Determine the stdio encoding for every standard stream on Windows - #155416

Merged
serhiy-storchaka merged 3 commits into
python:mainfrom
serhiy-storchaka:gh-86427-per-stream-encoding
Sep 18, 2026
Merged

serhiy-storchaka merged 3 commits into
python:mainfrom
serhiy-storchaka:gh-86427-per-stream-encoding

Conversation

@serhiy-storchaka

Copy link
Copy Markdown
Member

PyConfig.stdio_encoding is now left undefined in the legacy Windows stdio mode, and the encoding of every standard stream is determined from the device it is connected to. sys.stdin.encoding and sys.stdout.encoding can therefore differ, and a redirected stream uses the locale encoding.

This makes the member optional, which @vstinner objected to in the issue. #155415 is the minimal fix which is suitable for backporting.

It was the ANSI code page instead of the encoding of the device the stream
is connected to, as in 3.7.

The stdio encoding is now left undefined in this mode and determined for
every standard stream.
@serhiy-storchaka
serhiy-storchaka force-pushed the gh-86427-per-stream-encoding branch from 669b34a to b61cebd Compare August 29, 2026 19:50

@vstinner vstinner left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. This change is a simple fix.

I saw other more complicated options being discussed like having one encoding member per stream in PyConfig. It would work since it sounds more complicated. So yeah, I like this approach of delegating the encoding choice to create_stdio() using _Py_device_encoding(fd).

Also, test_cmd_line checks properly that two sys streams can have different encodings.

Comment thread Python/initconfig.c Outdated
}
}
else
/* gh-86427: it is determined for each stream. */

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you mention that create_stdio() calls _Py_device_encoding() to get the device encoding in this case?

@serhiy-storchaka
serhiy-storchaka merged commit 92db151 into python:main Sep 18, 2026
55 checks passed
@serhiy-storchaka
serhiy-storchaka deleted the gh-86427-per-stream-encoding branch September 18, 2026 17:03
@serhiy-storchaka

Copy link
Copy Markdown
Member Author

Thank you for your review. This change is not backportable, because it can crash a program that did not expect PyConfig.stdio_encoding to be NULL.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants