Skip to content

Commit f97cff4

Browse files
committed
Issue #26807: mock_open 'files' no longer error on readline at end of file.
Patch from Yolanda Robla.
1 parent 835a53d commit f97cff4

4 files changed

Lines changed: 18 additions & 0 deletions

File tree

Lib/unittest/mock.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2323,6 +2323,8 @@ def _readline_side_effect():
23232323
yield handle.readline.return_value
23242324
for line in _state[0]:
23252325
yield line
2326+
while True:
2327+
yield type(read_data)()
23262328

23272329

23282330
global file_spec

Lib/unittest/test/testmock/testmock.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1419,6 +1419,18 @@ def test_mock_open_alter_readline(self):
14191419
self.assertEqual('abc', first)
14201420
self.assertEqual('abc', second)
14211421

1422+
def test_mock_open_after_eof(self):
1423+
# read, readline and readlines should work after end of file.
1424+
_open = mock.mock_open(read_data='foo')
1425+
h = _open('bar')
1426+
h.read()
1427+
self.assertEqual('', h.read())
1428+
self.assertEqual('', h.read())
1429+
self.assertEqual('', h.readline())
1430+
self.assertEqual('', h.readline())
1431+
self.assertEqual([], h.readlines())
1432+
self.assertEqual([], h.readlines())
1433+
14221434
def test_mock_parents(self):
14231435
for Klass in Mock, MagicMock:
14241436
m = Klass()

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1223,6 +1223,7 @@ Ben Roberts
12231223
Mark Roberts
12241224
Andy Robinson
12251225
Jim Robinson
1226+
Yolanda Robla
12261227
Daniel Rocco
12271228
Mark Roddy
12281229
Kevin Rodgers

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,9 @@ Library
124124
- Issue #22274: In the subprocess module, allow stderr to be redirected to
125125
stdout even when stdout is not redirected. Patch by Akira Li.
126126

127+
- Issue #26807: mock_open 'files' no longer error on readline at end of file.
128+
Patch from Yolanda Robla.
129+
127130
- Issue #25745: Fixed leaking a userptr in curses panel destructor.
128131

129132
- Issue #26977: Removed unnecessary, and ignored, call to sum of squares helper

0 commit comments

Comments
 (0)