diff --git a/Lib/unittest/mock.py b/Lib/unittest/mock.py --- a/Lib/unittest/mock.py +++ b/Lib/unittest/mock.py @@ -973,7 +973,10 @@ raise effect if not _callable(effect): - result = next(effect) + try: + result = next(effect) + except StopIteration: + result = None if _is_exception(result): raise result if result is DEFAULT: diff --git a/Lib/unittest/test/testmock/testmock.py b/Lib/unittest/test/testmock/testmock.py --- a/Lib/unittest/test/testmock/testmock.py +++ b/Lib/unittest/test/testmock/testmock.py @@ -1419,6 +1419,15 @@ 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.readline()) + self.assertEqual([], h.readlines()) + def test_mock_parents(self): for Klass in Mock, MagicMock: m = Klass() diff --git a/Misc/ACKS b/Misc/ACKS --- a/Misc/ACKS +++ b/Misc/ACKS @@ -1222,6 +1222,7 @@ Mark Roberts Andy Robinson Jim Robinson +Yolanda Robla Daniel Rocco Mark Roddy Kevin Rodgers diff --git a/Misc/NEWS b/Misc/NEWS --- a/Misc/NEWS +++ b/Misc/NEWS @@ -107,6 +107,9 @@ Library ------- +- Issue #26807: mock_open 'files' no longer error on readline at end of file. + Patch from Yolanda Robla. + - Issue #26837: assertSequenceEqual() now correctly outputs non-stringified differing items (like bytes in the -b mode). This affects assertListEqual() and assertTupleEqual().