[Python-checkins] bpo-31855: unittest.mock.mock_open() results now respects the argument of read([size]) (GH-11521) (#13152)

Chris Withers webhook-mailer at python.org
Tue May 7 08:35:04 EDT 2019


https://github.com/python/cpython/commit/a6516f89aa0f416c7514ac364bb48ac7d1455487
commit: a6516f89aa0f416c7514ac364bb48ac7d1455487
branch: 3.7
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: Chris Withers <chris at withers.org>
date: 2019年05月07日T13:34:48+01:00
summary:
bpo-31855: unittest.mock.mock_open() results now respects the argument of read([size]) (GH-11521) (#13152)
unittest.mock.mock_open() results now respects the argument of read([size])
Co-Authored-By: remilapeyre <remi.lapeyre at henki.fr>
(cherry picked from commit 11a8832c98b3db78727312154dd1d3ba76d639ec)
Co-authored-by: Rémi Lapeyre <remi.lapeyre at henki.fr>
files:
A Misc/NEWS.d/next/Library/2019-01-11-17-09-15.bpo-31855.PlhfsX.rst
M Lib/unittest/mock.py
M Lib/unittest/test/testmock/testwith.py
diff --git a/Lib/unittest/mock.py b/Lib/unittest/mock.py
index db99585c33d2..f71f1a6fbed4 100644
--- a/Lib/unittest/mock.py
+++ b/Lib/unittest/mock.py
@@ -25,6 +25,7 @@
 __version__ = '1.0'
 
 
+import io
 import inspect
 import pprint
 import sys
@@ -2331,25 +2332,12 @@ def __init__(self, spec, spec_set=False, parent=None,
 
 file_spec = None
 
-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
- sep = b'\n' if isinstance(read_data, bytes) else '\n'
- data_as_list = [l + sep for l in read_data.split(sep)]
-
- if data_as_list[-1] == sep:
- # If the last line ended in a newline, the list comprehension will have an
- # extra entry that's just a newline. Remove this.
- data_as_list = data_as_list[:-1]
- else:
- # If there wasn't an extra newline by itself, then the file being
- # emulated doesn't have a newline to end the last line remove the
- # newline that our naive format() added
- data_as_list[-1] = data_as_list[-1][:-1]
 
- for line in data_as_list:
- yield line
+def _to_stream(read_data):
+ if isinstance(read_data, bytes):
+ return io.BytesIO(read_data)
+ else:
+ return io.StringIO(read_data)
 
 
 def mock_open(mock=None, read_data=''):
@@ -2364,20 +2352,23 @@ def mock_open(mock=None, read_data=''):
 `read_data` is a string for the `read`, `readline` and `readlines` of the
 file handle to return. This is an empty string by default.
 """
+ _read_data = _to_stream(read_data)
+ _state = [_read_data, None]
+
 def _readlines_side_effect(*args, **kwargs):
 if handle.readlines.return_value is not None:
 return handle.readlines.return_value
- return list(_state[0])
+ return _state[0].readlines(*args, **kwargs)
 
 def _read_side_effect(*args, **kwargs):
 if handle.read.return_value is not None:
 return handle.read.return_value
- return type(read_data)().join(_state[0])
+ return _state[0].read(*args, **kwargs)
 
- def _readline_side_effect():
+ def _readline_side_effect(*args, **kwargs):
 yield from _iter_side_effect()
 while True:
- yield type(read_data)()
+ yield _state[0].readline(*args, **kwargs)
 
 def _iter_side_effect():
 if handle.readline.return_value is not None:
@@ -2397,8 +2388,6 @@ def _iter_side_effect():
 handle = MagicMock(spec=file_spec)
 handle.__enter__.return_value = handle
 
- _state = [_iterate_read_data(read_data), None]
-
 handle.write.return_value = None
 handle.read.return_value = None
 handle.readline.return_value = None
@@ -2411,7 +2400,7 @@ def _iter_side_effect():
 handle.__iter__.side_effect = _iter_side_effect
 
 def reset_data(*args, **kwargs):
- _state[0] = _iterate_read_data(read_data)
+ _state[0] = _to_stream(read_data)
 if handle.readline.side_effect == _state[1]:
 # Only reset the side effect if the user hasn't overridden it.
 _state[1] = _readline_side_effect()
diff --git a/Lib/unittest/test/testmock/testwith.py b/Lib/unittest/test/testmock/testwith.py
index ec4e540dcfd9..0fa42e18eca6 100644
--- a/Lib/unittest/test/testmock/testwith.py
+++ b/Lib/unittest/test/testmock/testwith.py
@@ -286,7 +286,12 @@ def test_mock_open_read_with_argument(self):
 # for mocks returned by mock_open
 some_data = 'foo\nbar\nbaz'
 mock = mock_open(read_data=some_data)
- self.assertEqual(mock().read(10), some_data)
+ self.assertEqual(mock().read(10), some_data[:10])
+ self.assertEqual(mock().read(10), some_data[:10])
+
+ f = mock()
+ self.assertEqual(f.read(10), some_data[:10])
+ self.assertEqual(f.read(10), some_data[10:])
 
 
 def test_interleaved_reads(self):
diff --git a/Misc/NEWS.d/next/Library/2019-01-11-17-09-15.bpo-31855.PlhfsX.rst b/Misc/NEWS.d/next/Library/2019-01-11-17-09-15.bpo-31855.PlhfsX.rst
new file mode 100644
index 000000000000..0da9c4997e1a
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2019-01-11-17-09-15.bpo-31855.PlhfsX.rst
@@ -0,0 +1,2 @@
+:func:`unittest.mock.mock_open` results now respects the argument of read([size]).
+Patch contributed by Rémi Lapeyre.


More information about the Python-checkins mailing list

AltStyle によって変換されたページ (->オリジナル) /