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 PeterJCLaw
Recipients PeterJCLaw, paul.moore, steve.dower, tim.golden, zach.ware
Date 2020-05-06.22:32:57
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1588804378.46.0.584037601686.issue40540@roundup.psfhosted.org>
In-reply-to
Content
`sys.stdin` (on Windows, tested Pythons 3.6-3.8) appears to have different seeking behaviour depending on the source of the incoming data. This seems arguably reasonable given that `stdin` isn't always seekable, however even in the failing case `sys.stdin.seekable()` returns `True`.

Given the `reader.py` source:

``` python
import sys


def coding_check(lines, default='utf-8'):
    for line_number, line in enumerate(lines, 1):
        print((line_number, line))
        if line_number > 2:
            break
    return default


stdin = sys.stdin
print(stdin.seekable())

stdin.seek(0)

coding_check(stdin)

stdin.seek(0)
print(stdin.read())
```

then for two similar invocations we get differing results:

```
> python reader.py < reader.py
True
(1, 'import sys\n')
(2, '\n')
(3, '\n')
import sys


def coding_check(lines, default='utf-8'):

<... etc. redacted for brevity>

>
```

```
> type reader.py | python reader.py
True
(1, 'import sys\n')
(2, '\n')
(3, '\n')

>
```

I realise that raw standard input isn't always seekable, however I would hope that in the case that it isn't seekable that `.seekable()` would tell us that.

I also tried wrapping `stdin.buffer` in an `io.BufferedReader` and using that, however for short files (three lines or less) this issue is still present. I'm not sure if this is something I'm misunderstanding around buffered readers, a variation on this issue or another issue though.
History
Date User Action Args
2020-05-06 22:32:58PeterJCLawsetrecipients: + PeterJCLaw, paul.moore, tim.golden, zach.ware, steve.dower
2020-05-06 22:32:58PeterJCLawsetmessageid: <1588804378.46.0.584037601686.issue40540@roundup.psfhosted.org>
2020-05-06 22:32:58PeterJCLawlinkissue40540 messages
2020-05-06 22:32:57PeterJCLawcreate