Skip to content

Commit b872e2a

Browse files
sthibauljonathanslenders
authored andcommitted
output/vt100: Cope with stdout not having a working fileno()
When the application is using an io.StringIO(), its isatty() properly returns False but its fileno() raises io.UnsupportedOperation, so we have to cope with it. Fixes #1136
1 parent cbac5d5 commit b872e2a

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

prompt_toolkit/output/vt100.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"""
99
import array
1010
import errno
11+
import io
1112
import os
1213
import sys
1314
from typing import (
@@ -452,16 +453,21 @@ def from_pty(
452453
(This will take the dimensions by reading the pseudo
453454
terminal attributes.)
454455
"""
456+
fd: Optional[int]
455457
# Normally, this requires a real TTY device, but people instantiate
456458
# this class often during unit tests as well. For convenience, we print
457459
# an error message, use standard dimensions, and go on.
458-
fd = stdout.fileno()
460+
try:
461+
fd = stdout.fileno()
462+
except io.UnsupportedOperation:
463+
fd = None
459464

460-
if not stdout.isatty() and fd not in cls._fds_not_a_terminal:
465+
if not stdout.isatty() and (fd is None or fd not in cls._fds_not_a_terminal):
461466
msg = "Warning: Output is not a terminal (fd=%r).\n"
462467
sys.stderr.write(msg % fd)
463468
sys.stderr.flush()
464-
cls._fds_not_a_terminal.add(fd)
469+
if fd is not None:
470+
cls._fds_not_a_terminal.add(fd)
465471

466472
def get_size() -> Size:
467473
# If terminal (incorrectly) reports its size as 0, pick a

0 commit comments

Comments
 (0)