This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author John Hagen
Recipients John Hagen, docs@python
Date 2021-04-13.20:02:34
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <>
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.

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.
Date User Action Args
2021-04-13 20:02:35John Hagensetrecipients: + John Hagen, docs@python
2021-04-13 20:02:35John Hagensetmessageid: <>
2021-04-13 20:02:34John Hagenlinkissue43834 messages
2021-04-13 20:02:34John Hagencreate