Skip to content

Commit

Permalink
Merge pull request #683 from peendebak/master
Browse files Browse the repository at this point in the history
Make the isatty method of OutStream return True
  • Loading branch information
blink1073 authored Jun 9, 2021
2 parents fda1156 + abbcc0e commit 2ec4cbf
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
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()

0 comments on commit 2ec4cbf

Please sign in to comment.