diff --git a/CHANGELOG.md b/CHANGELOG.md index 947d575c..0e467701 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## 1.2 - +* bugfix for python3+ where binary data was passed into a process's stdin [#325](https://github.com/amoffat/sh/issues/325) * Introduced execution contexts which allow baking of common special keyword arguments into all commands [#269](https://github.com/amoffat/sh/issues/269) * `Command` and `which` now can take an optional `paths` parameter which specifies the search paths [#226](https://github.com/amoffat/sh/issues/226) * `_preexec_fn` option for executing a function after the child process forks but before it execs [#260](https://github.com/amoffat/sh/issues/260) diff --git a/sh.py b/sh.py index d3bb81a0..59fd1de5 100644 --- a/sh.py +++ b/sh.py @@ -1725,6 +1725,10 @@ def determine_how_to_read_input(input_obj): log_msg = "string" get_chunk = get_iter_string_reader(input_obj) + elif isinstance(input_obj, bytes): + log_msg = "bytes" + get_chunk = get_iter_string_reader(input_obj) + else: log_msg = "general iterable" get_chunk = get_iter_chunk_reader(iter(input_obj)) diff --git a/test.py b/test.py index 19a40161..2b4c9ace 100644 --- a/test.py +++ b/test.py @@ -640,6 +640,16 @@ def test_with_context_args(self): self.assertTrue(out == "") + def test_binary_input(self): + py = create_tmp_test(""" +import sys +data = sys.stdin.read() +sys.stdout.write(data) +""") + data = b'1234' + out = python(py.name, _in=data) + self.assertEqual(out, "1234") + def test_err_to_out(self): py = create_tmp_test(""" @@ -652,7 +662,7 @@ def test_err_to_out(self): sys.stderr.flush() """) stdout = python(py.name, _err_to_out=True) - self.assertTrue(stdout == "stdoutstderr") + self.assertEqual(stdout, "stdoutstderr") def test_err_piped(self):