On Mar 30, 2015, at 05:52, Joao S. O. Bueno <jsbueno@python.org.br> wrote:
On 29 March 2015 at 19:01, Tim Delaney <timothy.c.delaney@gmail.com> wrote:
On 30 March 2015 at 04:04, Dmitry Kazakov <jsbfox@gmail.com> wrote:
I propose adding a context manager to contextlib module, which would restore the stream position of its argument at exit:
with contextlib.closing(file_like_object) as file: file.read(1) old = file.tell() with contextlib.keep_stream_position(file) as f: assert file is f assert file_like_object is f # Do something with the file(-like) object e.g. feed it # to `PIL.Image.open` (will consume the whole stream)
assert file.tell() == old
This CM would allow us to reuse streams efficiently, without requiring to call seek() explicitly. I will open a new issue and submit a patch in case of favorable responses.
I like the idea. Might need some bikeshedding on the name (maybe restore_position?).
To test for seeking, I would suggest having a seek to current position during construction. Not perfect (e.g. there may be streams that can only seek forwards, not backwards) but I think it's the best non-destructive option.
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.
Maybe with even three states: "strict": raises an exception if the stream is not [back]seekable "default": creates a tee cache if the stream is not back-seekable "cache": always cache.
(And maybe a maxbuffer parameter to go along? )
Tim Delaney
_______________________________________________ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/
Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/