New submission from John Hagen johnthagen@gmail.com:
The example for StringIO currently manually closes the object rather than using a context manager. Since this is likely the first code that a new user encounters and context managers reduce error-prone situations, I think it would be helpful to show usage as a context manager.
https://docs.python.org/3/library/io.html#io.StringIO.getvalue
Something like:
import io
with io.StringIO() as output: output.write('First line.\n') print('Second line.', file=output)
# Retrieve file contents -- this will be # 'First line.\nSecond line.\n' contents = output.getvalue()
# Context manager will automatically close # object and discard memory buffer -- # .getvalue() will now raise an exception.
---------- assignee: docs@python components: Documentation messages: 391000 nosy: John Hagen, docs@python priority: normal severity: normal status: open title: Use context manager in StringIO example type: enhancement versions: Python 3.10
_______________________________________ Python tracker report@bugs.python.org https://bugs.python.org/issue43834 _______________________________________
Change by John Hagen johnthagen@gmail.com:
---------- keywords: +patch pull_requests: +24133 stage: -> patch review pull_request: https://github.com/python/cpython/pull/25401
_______________________________________ Python tracker report@bugs.python.org https://bugs.python.org/issue43834 _______________________________________
Change by Terry J. Reedy tjreedy@udel.edu:
---------- nosy: +benjamin.peterson, stutzbach
_______________________________________ Python tracker report@bugs.python.org https://bugs.python.org/issue43834 _______________________________________
Raymond Hettinger raymond.hettinger@gmail.com added the comment:
Let's leave the example as-is. The principal use case for these objects is to pass them into other APIs that require file-like objects. Those can't always be put in a context manager. For this example, we mainly want to communicate that getvalue() must be called before the object is closed. The current form communicates that clearly.
Thanks for the suggestion.
---------- nosy: +rhettinger resolution: -> rejected stage: patch review -> resolved status: open -> closed
_______________________________________ Python tracker report@bugs.python.org https://bugs.python.org/issue43834 _______________________________________
Raymond Hettinger raymond.hettinger@gmail.com added the comment:
Except for that, the PR looks fine. Leaving this open to see what Benjamin thinks.
---------- resolution: rejected -> status: closed -> open
_______________________________________ Python tracker report@bugs.python.org https://bugs.python.org/issue43834 _______________________________________
Benjamin Peterson benjamin@python.org added the comment:
I agree that closing or using a context manager with StringIO (or BytesIO) is not something one normally has to do, so it doesn't need to be in the example.
---------- resolution: -> rejected status: open -> closed
_______________________________________ Python tracker report@bugs.python.org https://bugs.python.org/issue43834 _______________________________________