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.

classification
Title: inconstent stdin buffering/seeking behaviour
Type: behavior Stage: resolved
Components: Windows Versions:
process
Status: closed Resolution: duplicate
Dependencies: Superseder: seekable() returns True on pipe objects in Windows
View: 42602
Assigned To: Nosy List: PeterJCLaw, paul.moore, steve.dower, tim.golden, zach.ware
Priority: normal Keywords:

Created on 2020-05-06 22:32 by PeterJCLaw, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (1)
msg368306 - (view) Author: Peter Law (PeterJCLaw) * Date: 2020-05-06 22:32
`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
2022-04-11 14:59:30adminsetgithub: 84720
2021-03-12 20:40:47eryksunsetstatus: open -> closed
resolution: duplicate
superseder: seekable() returns True on pipe objects in Windows
stage: resolved
2020-05-06 22:32:58PeterJCLawcreate