[Python-Dev] cpython: Closes issue 17467. Add readline and readlines support to

Michael Foord fuzzyman at voidspace.org.uk
Wed Mar 20 08:50:27 CET 2013


On 20 Mar 2013, at 00:09, Antoine Pitrou <solipsis at pitrou.net> wrote:

> On Tue, 19 Mar 2013 21:44:15 -0700
> Michael Foord <fuzzyman at voidspace.org.uk> wrote:
>> 
>> mock_open makes it easy to put a StringIO in place if that's what you want. It's just a simple helper function for providing some known data *along with the Mock api* to make asserts that it was used correctly. It isn't presenting a full file-system. My suggestion to the implementor of the patch was that read / readline / readlines be disconnected - but the patch provided allows them to be interleaved and I saw no reason to undo that.
>> 
>> If users want more complex behaviour (like universal newline support) they can use mock_open along with a StringIO.
> 
> This is not about complex behaviour but simply correct behaviour.
> For the record, universal newlines are enabled by default in Python 3:
> 
>>>> with open("foo", "wb") as f: f.write(b"a\r\nb\rc\n")
> ... 
> 7
>>>> with open("foo", "r") as f: print(list(f))
> ... 
> ['a\n', 'b\n', 'c\n']
> 

mock_open is •not• presenting a mock filesystem, but is about providing a mock object to avoid •either• reading or writing to the real filesystem. You don't •tend• to do both with a single file handle  - I know it's possible but mock_open is a convenience function for the common case.

This commit simply adds support for readline and readlines, whereas before only read was supported.

If you want to add support for additional functionality feel free to propose a patch.

Michael



> 
> Regards
> 
> Antoine.
> _______________________________________________
> Python-Dev mailing list
> Python-Dev at python.org
> http://mail.python.org/mailman/listinfo/python-dev
> Unsubscribe: http://mail.python.org/mailman/options/python-dev/fuzzyman%40voidspace.org.uk


More information about the Python-Dev mailing list