This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author michael.foord
Recipients kushal.das, michael.foord
Date 2014-04-16.17:56:44
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1397671005.18.0.0743040241878.issue21258@psf.upfronthosting.co.za>
In-reply-to
Content
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.
History
Date User Action Args
2014-04-16 17:56:45michael.foordsetrecipients: + michael.foord, kushal.das
2014-04-16 17:56:45michael.foordsetmessageid: <1397671005.18.0.0743040241878.issue21258@psf.upfronthosting.co.za>
2014-04-16 17:56:45michael.foordlinkissue21258 messages
2014-04-16 17:56:44michael.foordcreate