[Python-ideas] Add a context manager to keep stream position unchanged

Chris Angelico rosuav at gmail.com
Mon Mar 30 15:59:10 CEST 2015


On Tue, Mar 31, 2015 at 12:52 AM, Andrew Barnert
<abarnert at yahoo.com.dmarc.invalid> wrote:
>> If that is being done, anyway, I propose an optional parameter that
>> would allow the context to internally use itertools.tee if the stream
>> can't seek itself.
>
> That sounds cool, but... If you have an actual file object with seek and tell, you're often using other methods besides iterating lines (like read), and tee won't help with those, so it might be a little misleading.
>

That shouldn't be an issue, though, as those will just use seek/tell.
I'm more wondering about the ones that _do_ need the tee'ing; how,
after the context manager completes, do you have the file object
return something from the other half of the tee? I'm not sure this
mode will work. If the stream can't use seek(), this context manager
should simply let the exception bubble IMO.

ChrisA


More information about the Python-ideas mailing list