Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Remove code duplication
  • Loading branch information
Rémi Lapeyre committed May 6, 2019
commit 59b08e65f96109049b20e51a6562911a879194b2
34 changes: 7 additions & 27 deletions Lib/unittest/mock.py
Original file line number Diff line number Diff line change
Expand Up @@ -2347,25 +2347,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):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code LGTM. I am slightly concerned if there might be any problems with backport where in Python 2 bytes is like an alias for str. So, even with BytesIO it would still be string as shown below and work as expected. Maybe I am just overthinking this.

It would be helpful if @cjw296 and @voidspace can review this since this is a good bug to be fixed. Thanks @remilapeyre .

$ python2
Python 2.7.6 (default, Sep  9 2014, 15:04:36)
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.39)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> isinstance('a', bytes)
True
>>> isinstance(b'a', str)
True
>>> import io
>>> io.BytesIO('a').readline()
'a'
>>> type(io.BytesIO('a').readline())
<type 'str'>

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't worry, we can make this work in the backport :-)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tirkarthi wouldn't it still be correct? The test would be useless be the mock would still work right?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, in Python 2 str and bytes are the same. I will just keep on the CI once the backport lands in mock repo.

return io.BytesIO(read_data)
else:
return io.StringIO(read_data)


def mock_open(mock=None, read_data=''):
Expand All @@ -2380,11 +2367,7 @@ 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.
"""
if isinstance(read_data, bytes):
_read_data = io.BytesIO(read_data)
else:
_read_data = io.StringIO(read_data)

_read_data = _to_stream(read_data)
_state = [_read_data, None]

def _readlines_side_effect(*args, **kwargs):
Expand Down Expand Up @@ -2432,10 +2415,7 @@ def _iter_side_effect():
handle.__iter__.side_effect = _iter_side_effect

def reset_data(*args, **kwargs):
if isinstance(read_data, bytes):
_state[0] = io.BytesIO(read_data)
else:
_state[0] = io.StringIO(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()
Expand Down