This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author eryksun
Recipients Cosimo Lupo, akira, eryksun, paul.moore, serhiy.storchaka, steve.dower, tim.golden, vstinner, zach.ware
Date 2015-08-22.03:01:04
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1440212467.1.0.336759619419.issue24881@psf.upfronthosting.co.za>
In-reply-to
Content
> It is not clear why the absence of _setmode(fd, os.O_BINARY)
> is not detected by tests.

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'
History
Date User Action Args
2015-08-22 03:01:07eryksunsetrecipients: + eryksun, paul.moore, vstinner, tim.golden, akira, zach.ware, serhiy.storchaka, steve.dower, Cosimo Lupo
2015-08-22 03:01:07eryksunsetmessageid: <1440212467.1.0.336759619419.issue24881@psf.upfronthosting.co.za>
2015-08-22 03:01:07eryksunlinkissue24881 messages
2015-08-22 03:01:04eryksuncreate