summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Lib/unittest/mock.py2
-rw-r--r--Lib/unittest/test/testmock/testmock.py12
-rw-r--r--Misc/ACKS1
-rw-r--r--Misc/NEWS3
4 files changed, 18 insertions, 0 deletions
diff --git a/Lib/unittest/mock.py b/Lib/unittest/mock.py
index 7400fb7a64..123c156a96 100644
--- a/Lib/unittest/mock.py
+++ b/Lib/unittest/mock.py
@@ -2341,6 +2341,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 526eeab714..9910ab92d1 100644
--- a/Lib/unittest/test/testmock/testmock.py
+++ b/Lib/unittest/test/testmock/testmock.py
@@ -1440,6 +1440,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()
diff --git a/Misc/ACKS b/Misc/ACKS
index 65d70402a1..adec8dc646 100644
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -1231,6 +1231,7 @@ Ben Roberts
Mark Roberts
Andy Robinson
Jim Robinson
+Yolanda Robla
Daniel Rocco
Mark Roddy
Kevin Rodgers
diff --git a/Misc/NEWS b/Misc/NEWS
index 80996bdcc1..5745cc322e 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -290,6 +290,9 @@ Library
- Issue #22274: In the subprocess module, allow stderr to be redirected to
stdout even when stdout is not redirected. Patch by Akira Li.
+- Issue #26807: mock_open 'files' no longer error on readline at end of file.
+ Patch from Yolanda Robla.
+
- Issue #25745: Fixed leaking a userptr in curses panel destructor.
- Issue #26977: Removed unnecessary, and ignored, call to sum of squares helper