Skip to content

Commit

Permalink
Issue 96, don't crash on closed stdin
Browse files Browse the repository at this point in the history
  • Loading branch information
kvalakuzhyzp committed Jun 23, 2020
1 parent 3a8fd88 commit 0e5e68c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
15 changes: 12 additions & 3 deletions src/zelos/handles/base_handles.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,9 +317,18 @@ def __init__(self, processes, hook_manager, file_system):

def init_handles(p):
# Add some default system handles
if not sys.stdin.isatty() and isinstance(
sys.stdin, io.TextIOWrapper
):
try:
passthrough_stdin = not sys.stdin.isatty() and isinstance(
sys.stdin, io.TextIOWrapper
)
except ValueError: # stdin was closed
self.logger.notice(
"stdin was closed, not redirecting to emulated stdin"
)
passthrough_stdin = False

if passthrough_stdin:
self.logger.info("Passing stdin to emulated program")
self.new_file(
"stdin_redirect",
handle_num=0,
Expand Down
10 changes: 10 additions & 0 deletions tests/test_handles.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,16 @@ def test_stdin_redirect(self):
z.start()
self.assertIn("string is: test data", stdout.getvalue())

def test_issue_96_closing_stdin(self):
# Zelos shouldn't crash if stdin is closed before zelos is
# initialized.
new_stdin = io.TextIOWrapper(
io.BufferedReader(io.BytesIO(b"test data"))
)
with patch("sys.stdin", new=new_stdin):
new_stdin.close()
Zelos(None)


def main():
unittest.main()
Expand Down

0 comments on commit 0e5e68c

Please sign in to comment.