-
-
Notifications
You must be signed in to change notification settings - Fork 33.8k
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
Merged
Changes from 1 commit
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
Jump to
Jump to file
Failed to load files.
Loading
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 | ||
|
|
@@ -2379,20 +2380,27 @@ 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) | ||
|
|
||
| _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 +2420,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 | ||
|
|
@@ -2426,7 +2432,10 @@ def _iter_side_effect(): | |
| handle.__iter__.side_effect = _iter_side_effect | ||
|
|
||
| def reset_data(*args, **kwargs): | ||
| _state[0] = _iterate_read_data(read_data) | ||
| if isinstance(read_data, bytes): | ||
|
||
| _state[0] = io.BytesIO(read_data) | ||
| else: | ||
| _state[0] = io.StringIO(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
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 @@ | ||
| unittest.mock.mock_open() results now respects the argument of read([size]). | ||
remilapeyre marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| 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.
Uh oh!
There was an error while loading. Please reload this page.