diff options
| author | Berker Peksag <berker.peksag@gmail.com> | 2015-08-06 10:17:27 (GMT) |
|---|---|---|
| committer | Berker Peksag <berker.peksag@gmail.com> | 2015-08-06 10:17:27 (GMT) |
| commit | 2b347a8f1d5b44d546965ff658a3670662ac0dd2 (patch) | |
| tree | aede91b14cf376e0479ae6d214f9f17461871bb4 /Lib/unittest/mock.py | |
| parent | dadf33a2b447fd69b11aa74960c5df03c4361353 (diff) | |
| parent | abfaecdfdd9c10732e221eada3c259523fd14a8f (diff) | |
| download | cpython-2b347a8f1d5b44d546965ff658a3670662ac0dd2.zip cpython-2b347a8f1d5b44d546965ff658a3670662ac0dd2.tar.gz cpython-2b347a8f1d5b44d546965ff658a3670662ac0dd2.tar.bz2 | |
Issue #23004: mock_open() now reads binary data correctly when the type of read_data is bytes.
Initial patch by Aaron Hill.
Diffstat (limited to 'Lib/unittest/mock.py')
| -rw-r--r-- | Lib/unittest/mock.py | 7 |
1 files changed, 4 insertions, 3 deletions
diff --git a/Lib/unittest/mock.py b/Lib/unittest/mock.py index efe5763..4c58639 100644 --- a/Lib/unittest/mock.py +++ b/Lib/unittest/mock.py @@ -2271,9 +2271,10 @@ 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 - data_as_list = ['{}\n'.format(l) for l in read_data.split('\n')] + 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] == '\n': + 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] @@ -2307,7 +2308,7 @@ def mock_open(mock=None, read_data=''): def _read_side_effect(*args, **kwargs): if handle.read.return_value is not None: return handle.read.return_value - return ''.join(_state[0]) + return type(read_data)().join(_state[0]) def _readline_side_effect(): if handle.readline.return_value is not None: |
