mirror of
https://github.com/python/cpython.git
synced 2025-07-16 07:45:20 +00:00
Issue #23004: mock_open() now reads binary data correctly when the type of read_data is bytes.
Initial patch by Aaron Hill.
This commit is contained in:
parent
0b2833eaf2
commit
86b34da5ef
3 changed files with 35 additions and 3 deletions
|
@ -2250,9 +2250,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]
|
||||
|
@ -2286,7 +2287,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:
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue