diff --git a/Lib/unittest/mock.py b/Lib/unittest/mock.py index 9d9b3e2..a8e01bf 100644 --- a/Lib/unittest/mock.py +++ b/Lib/unittest/mock.py @@ -2265,7 +2265,12 @@ 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')] + newline = '\n' + if type(read_data) == bytes: + newline = b'\n' + + data_as_list = ['{}\n'.format(l) for l in + read_data.split(newline)] if data_as_list[-1] == '\n': # If the last line ended in a newline, the list comprehension will have an diff --git a/Lib/unittest/test/testmock/testmock.py b/Lib/unittest/test/testmock/testmock.py index 3a104cb..a6c0893 100644 --- a/Lib/unittest/test/testmock/testmock.py +++ b/Lib/unittest/test/testmock/testmock.py @@ -8,7 +8,7 @@ from unittest.mock import ( call, DEFAULT, patch, sentinel, MagicMock, Mock, NonCallableMock, NonCallableMagicMock, _CallList, - create_autospec + create_autospec, mock_open ) @@ -1444,5 +1444,11 @@ class MockTest(unittest.TestCase): mock.foo + def test_mock_open(self): + m = mock_open(read_data= b'abc') + with patch('%s.open' % __name__, m, create=True) : + with open('abc', 'rb') as f: + f.read() + if __name__ == '__main__': unittest.main()