Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion ipykernel/iostream.py
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ def _watch_pipe_fd(self):
self._exc = sys.exc_info()

def __init__(
self, session, pub_thread, name, pipe=None, echo=None, *, watchfd=True
self, session, pub_thread, name, pipe=None, echo=None, *, watchfd=True, isatty=False,
):
"""
Parameters
Expand All @@ -333,6 +333,8 @@ def __init__(
the file descriptor by its number. It will spawn a watching thread,
that will swap the give file descriptor for a pipe, read from the
pipe, and insert this into the current Stream.
isatty : bool (default, False)
Indication of whether this stream has termimal capabilities (e.g. can handle colors)

"""
if pipe is not None:
Expand Down Expand Up @@ -364,6 +366,7 @@ def __init__(
self._io_loop = pub_thread.io_loop
self._new_buffer()
self.echo = None
self._isatty = bool(isatty)

if (
watchfd
Expand All @@ -381,6 +384,14 @@ def __init__(
else:
raise ValueError("echo argument must be a file like object")

def isatty(self):
"""Return a bool indicating whether this is an 'interactive' stream.

Returns:
Boolean
"""
return self._isatty

def _setup_stream_redirects(self, name):
pr, pw = os.pipe()
fno = getattr(sys, name).fileno()
Expand Down
10 changes: 10 additions & 0 deletions ipykernel/tests/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,13 @@ def test_io_api():
stream.seek(0)
with nt.assert_raises(io.UnsupportedOperation):
stream.tell()

def test_io_isatty():
session = Session()
ctx = zmq.Context()
pub = ctx.socket(zmq.PUB)
thread = IOPubThread(pub)
thread.start()

stream = OutStream(session, thread, 'stdout', isatty=True)
assert stream.isatty()