diff options
author | Robert Collins <rbtcollins@hp.com> | 2016-05-16 03:22:01 (GMT) |
---|---|---|
committer | Robert Collins <rbtcollins@hp.com> | 2016-05-16 03:22:01 (GMT) |
commit | 9549a3e3d4be2a15c222996abff8cb97180ee9be (patch) | |
tree | 6459b7352282cce393976499fa5830aa14b9d96c /Lib/unittest | |
parent | 33a8fb9920efc4e4f8c6afa1062c966b27f222a9 (diff) | |
download | cpython-9549a3e3d4be2a15c222996abff8cb97180ee9be.zip cpython-9549a3e3d4be2a15c222996abff8cb97180ee9be.tar.gz cpython-9549a3e3d4be2a15c222996abff8cb97180ee9be.tar.bz2 |
Issue #26807: mock_open 'files' no longer error on readline at end of file.
Patch from Yolanda Robla.
Diffstat (limited to 'Lib/unittest')
-rw-r--r-- | Lib/unittest/mock.py | 2 | ||||
-rw-r--r-- | Lib/unittest/test/testmock/testmock.py | 12 |
2 files changed, 14 insertions, 0 deletions
diff --git a/Lib/unittest/mock.py b/Lib/unittest/mock.py index cabae15..86a5a3d 100644 --- a/Lib/unittest/mock.py +++ b/Lib/unittest/mock.py @@ -2323,6 +2323,8 @@ def mock_open(mock=None, read_data=''): yield handle.readline.return_value for line in _state[0]: yield line + while True: + yield type(read_data)() global file_spec diff --git a/Lib/unittest/test/testmock/testmock.py b/Lib/unittest/test/testmock/testmock.py index 03c95de..5f82b82 100644 --- a/Lib/unittest/test/testmock/testmock.py +++ b/Lib/unittest/test/testmock/testmock.py @@ -1419,6 +1419,18 @@ class MockTest(unittest.TestCase): self.assertEqual('abc', first) self.assertEqual('abc', second) + def test_mock_open_after_eof(self): + # read, readline and readlines should work after end of file. + _open = mock.mock_open(read_data='foo') + h = _open('bar') + h.read() + self.assertEqual('', h.read()) + self.assertEqual('', h.read()) + self.assertEqual('', h.readline()) + self.assertEqual('', h.readline()) + self.assertEqual([], h.readlines()) + self.assertEqual([], h.readlines()) + def test_mock_parents(self): for Klass in Mock, MagicMock: m = Klass() |