You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
_pyio checks that os.name == 'win32' instead of 'nt' #69069
the _pyio module at line 16 tries to check whether it is running on Windows platform, by doing:
if os.name == 'win32':
from msvcrt import setmode as _setmode
else:
_setmode = None
However, the string returned by os.name is 'nt' and not 'win32' (the latter is returned by sys.platform). Therefore, the value is always False and the setmode function from mscvrt module is never imported.
FWIW this patch broke the _pyio module on Cygwin, as the msvcrt module is not built on Cygwin. AFAICT this is only a problem for Python built with MSVCRT, which Cygwin does not use. When test case works as expected on Cygwin without this.
Activity
CosimoLupo commentedon Aug 17, 2015
the
_pyiomodule at line 16 tries to check whether it is running on Windows platform, by doing:However, the string returned by os.name is 'nt' and not 'win32' (the latter is returned by
sys.platform). Therefore, the value is always False and the setmode function from mscvrt module is never imported.Thank you.
Cheers,
Cosimo
4kir4 commentedon Aug 21, 2015
To make _pyio correspond to the C version I've added
sys.platform in {'win32', 'cygwin'}
condition. See the attached pyio_setmode.diff
It is not clear why the absence of _setmode(fd, os.O_BINARY) is not detected by tests.
(a) a corresponding test should be added
(b) OR _setmode() call should be removed from both Python and C io versions
eryksun commentedon Aug 22, 2015
It's only a problem when an existing text-mode file descriptor is passed in. For example, in text mode the CRT handles b'\x1a' as an EOF marker:
Python 3.5.0b4 (v3.5.0b4:c0d641054635, Jul 26 2015, 07:11:12) [MSC v.1900 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> import os, _pyio >>> _ = open('testfile', 'wb').write(b'abc\x1adef') >>> fd = os.open('testfile', os.O_RDONLY|os.O_TEXT) >>> f = _pyio.open(fd, 'rb') >>> f.read() b'abc' >>> f.close() >>> f = _pyio.open('testfile', 'rb') >>> f.read() b'abc\x1adef'The setmode call ensures a file descriptor is in the expected binary mode:
>>> import msvcrt >>> fd = os.open('testfile', os.O_RDONLY|os.O_TEXT) >>> old_mode = msvcrt.setmode(fd, os.O_BINARY) >>> old_mode == os.O_TEXT True >>> f = _pyio.open(fd, 'rb') >>> f.read() b'abc\x1adef'python-dev commentedon Aug 28, 2015
New changeset 687da8760a58 by Serhiy Storchaka in branch '3.5':
Issue bpo-24881: Fixed setting binary mode in Python implementation of FileIO
https://hg.python.org/cpython/rev/687da8760a58
New changeset 2dd9294f679d by Serhiy Storchaka in branch 'default':
Issue bpo-24881: Fixed setting binary mode in Python implementation of FileIO
https://hg.python.org/cpython/rev/2dd9294f679d
serhiy-storchaka commentedon Aug 28, 2015
Good catch. Thank you for your report Cosimo. Thank you for your patch Akira.
embray commentedon Oct 17, 2016
FWIW this patch broke the _pyio module on Cygwin, as the msvcrt module is not built on Cygwin. AFAICT this is only a problem for Python built with MSVCRT, which Cygwin does not use. When test case works as expected on Cygwin without this.
vstinner commentedon Oct 17, 2016
"FWIW this patch broke the _pyio module on Cygwin,"
Please open a new issue, this one is closed.
vstinner commentedon May 15, 2026
I wrote PR gh-149899 to fix the regression.