Skip to content

Commit

Permalink
support empty _in param. closes #427
Browse files Browse the repository at this point in the history
  • Loading branch information
amoffat committed Apr 25, 2020
1 parent 781c27a commit 0764716
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
15 changes: 8 additions & 7 deletions sh.py
Original file line number Diff line number Diff line change
Expand Up @@ -2119,7 +2119,7 @@ def __init__(self, command, parent_log, cmd, stdin, stdout, stderr,
# to prevent race conditions
self.exit_code = None

self.stdin = stdin or Queue()
self.stdin = stdin

# _pipe_queue is used internally to hand off stdout from one process
# to another. by default, all stdout from a process gets dumped
Expand Down Expand Up @@ -2152,15 +2152,10 @@ def __init__(self, command, parent_log, cmd, stdin, stdout, stderr,
attr[3] &= ~termios.ECHO
termios.tcsetattr(self._stdin_parent_fd, termios.TCSANOW, attr)

# we're only going to create a stdin thread iff we have potential
# for stdin to come in. this would be through a stdout callback or
# through an object we've passed in for stdin
potentially_has_input = callable(stdout) or stdin

# this represents the connection from a Queue object (or whatever
# we're using to feed STDIN) to the process's STDIN fd
self._stdin_stream = None
if self._stdin_parent_fd and potentially_has_input:
if self._stdin_parent_fd:
log = self.log.get_child("streamwriter", "stdin")
self._stdin_stream = StreamWriter(log, self._stdin_parent_fd,
self.stdin, ca["in_bufsize"], ca["encoding"],
Expand Down Expand Up @@ -2651,6 +2646,12 @@ def determine_how_to_read_input(input_obj):
log_msg = "generator"
get_chunk = get_iter_chunk_reader(iter(input_obj))

elif input_obj is None:
log_msg = "None"
def raise_():
raise DoneReadingForever
get_chunk = raise_

else:
try:
it = iter(input_obj)
Expand Down
11 changes: 11 additions & 0 deletions test.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,17 @@ def test_number_arg(self):
out = python(py.name, 3).strip()
self.assertEqual(out, "3")

def test_empty_stdin_no_hang(self):
py = create_tmp_test("""
import sys
data = sys.stdin.read()
sys.stdout.write("no hang")
""")
out = python(py.name, _in="", _timeout=2)
self.assertEqual(out, "no hang")

out = python(py.name, _in=None, _timeout=2)
self.assertEqual(out, "no hang")

def test_exit_code(self):
from sh import ErrorReturnCode
Expand Down

0 comments on commit 0764716

Please sign in to comment.