New submission from Michael Foord: 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.
1. Want to mock this: with open(source_file, 'r') as f: for line_num, line_text in enumerate(f): print line_text 2. Tried this: with patch('__builtin__.open', mock_open(read_data='text'), create=True) as p: 3. enumerate causes a call to __iter__() which is not handled by the mock_open code. 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. ---------- assignee: michael.foord components: Library (Lib) files: 213.patch keywords: patch messages: 216522 nosy: kushal.das, michael.foord priority: normal severity: normal stage: patch review status: open title: Add __iter__ support for mock_open type: behavior versions: Python 3.5 Added file: http://bugs.python.org/file34917/213.patch _______________________________________ Python tracker <rep...@bugs.python.org> <http://bugs.python.org/issue21258> _______________________________________ _______________________________________________ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com