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
Add __iter__ support for mock_open #65457
Comments
mock_open returns a mock object suitable for using as a mock file handle. File handles support iteration, so mock_open should support that. If possible it should be integrated with the current read/readlines support (only if possible), so the suggested patch may not be enough.
What is the expected output? What do you see instead? The __iter__ is allowed on the returned file handle What version of the product are you using? On what operating system? latest Please provide any additional information below. Patch would have mock_open setup handle.__iter__.return_value to a passed in parm or if none, possibly a iter(StringIO(read_data)) on the existing read_data parm to keep the interface unchanged. |
I created a fix for this. This also fixes a second issue in mock_open, which is that readline() raises StopIteration at EOF rather than returning empty strings. See attached diff. |
This is the corresponding patch to the test suite. |
I noticed this issue too, thanks for fixing it! |
Provided path did not work for me. Probably because lack of __next__ handler. I noticed that issue when interacting with cvs.reader and cvs.DictReader. After simple modification it seems to work fine. |
For working around this issue on Python 3.5 it is sufficient to overwrite just the This also works in combination with |
The lack of dunder_iter support on mock_open has been resolved in bpo-32933 (Git Hub 5974). Can I suggest that once the above PR is merged into 3.8 (due imminently allegedly <smile>), that we should then backport that fix into 3.5, 3.6 & 3.7 as a minimum ? I am by no means an expert though. |
Closing this as a duplicate of bpo-32933. Let's discuss backporting to maintenance branches there. |
Can someone explain how to use the functionality where you can give an iterator for the results of the open mock? I have a function like
I want to return n lines for the
or something like that. I'm using Python 3.8. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: