diff --git a/Lib/unittest/mock.py b/Lib/unittest/mock.py index 9d9b3e2..979f657 100644 --- a/Lib/unittest/mock.py +++ b/Lib/unittest/mock.py @@ -2265,7 +2265,14 @@ def _iterate_read_data(read_data): # Helper for mock_open: # Retrieve lines from read_data via a generator so that separate calls to # readline, read, and readlines are properly interleaved - data_as_list = ['{}\n'.format(l) for l in read_data.split('\n')] + + if type(read_data) == bytes: + data_as_list = [l + b'\n' for l in + read_data.split(b'\n')] + else: + data_as_list = [l + '\n' for l in + read_data.split('\n')] + if data_as_list[-1] == '\n': # If the last line ended in a newline, the list comprehension will have an @@ -2300,7 +2307,7 @@ def mock_open(mock=None, read_data=''): def _read_side_effect(*args, **kwargs): if handle.read.return_value is not None: return handle.read.return_value - return ''.join(_data) + return type(read_data)().join(_data) def _readline_side_effect(): if handle.readline.return_value is not None: diff --git a/Lib/unittest/test/testmock/testwith.py b/Lib/unittest/test/testmock/testwith.py index f54e051..b9e316f 100644 --- a/Lib/unittest/test/testmock/testwith.py +++ b/Lib/unittest/test/testmock/testwith.py @@ -210,6 +210,20 @@ class TestMockOpen(unittest.TestCase): self.assertEqual(result, ['foo\n', 'bar\n', 'baz']) + def test_read_bytes(self): + m = mock_open(read_data=b'\xc6') + with patch('%s.open' % __name__, m, create=True) : + with open('abc', 'rb') as f: + self.assertEqual(f.read(), b'\xc6') + + + def test_readlines_bytes(self): + m = mock_open(read_data=b'abc\ndef\nghi') + with patch('%s.open' % __name__, m, create=True) : + with open('abc', 'rb') as f: + self.assertEqual(f.readlines(), [b'abc\n', b'def\n', b'ghi']) + + def test_mock_open_read_with_argument(self): # At one point calling read with an argument was broken # for mocks returned by mock_open