Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 34k
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
cjw296 merged 4 commits into python:master from remilapeyre:mock_open-not-compatible-with-read(n)May 7, 2019
Uh oh!
There was an error while loading. Please reload this page.
Merged
Changes from all commits
Commits
Show all changes
4 commits Select commit Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -25,6 +25,7 @@ | ||
| __version__ = '1.0' | ||
| import io | ||
| import inspect | ||
| import pprint | ||
| import sys | ||
| @@ -2346,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): | ||
| return io.BytesIO(read_data) | ||
| else: | ||
| return io.StringIO(read_data) | ||
| def mock_open(mock=None, read_data=''): | ||
| @@ -2379,20 +2367,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: | ||
| @@ -2412,8 +2403,6 @@ def _iter_side_effect(): | ||
| handle = MagicMock(spec=file_spec) | ||
| handle.__enter__.return_value = handle | ||
| _state = [_iterate_read_data(read_data), None] | ||
remilapeyre marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading. Please reload this page. | ||
| handle.write.return_value = None | ||
| handle.read.return_value = None | ||
| handle.readline.return_value = None | ||
| @@ -2426,7 +2415,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() | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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]) | ||
remilapeyre marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading. Please reload this page. | ||
| 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): | ||
2 changes: 2 additions & 0 deletions 2 Misc/NEWS.d/next/Library/2019-01-11-17-09-15.bpo-31855.PlhfsX.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| :func:`unittest.mock.mock_open` results now respects the argument of read([size]). | ||
| Patch contributed by Rémi Lapeyre. |
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 .
There was a problem hiding this comment.
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 :-)
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.