[Python-ideas] Add a context manager to keep stream position unchanged
Skip Montanaro
skip.montanaro at gmail.com
Sun Mar 29 23:00:28 CEST 2015
Interesting idea. It should raise an exception as early as possible if the
stream doesn't support seeking...
Skip
On Mar 29, 2015 12:04 PM, "Dmitry Kazakov" <jsbfox at 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.
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20150329/5ff5c3d0/attachment.html>
More information about the Python-ideas
mailing list