[issue43834] Use context manager in StringIO example

2021-04-19 Thread Benjamin Peterson


Benjamin Peterson  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 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue43834] Use context manager in StringIO example

2021-04-19 Thread Raymond Hettinger


Raymond Hettinger  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 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue43834] Use context manager in StringIO example

2021-04-19 Thread Raymond Hettinger


Raymond Hettinger  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 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue43834] Use context manager in StringIO example

2021-04-16 Thread Terry J. Reedy


Change by Terry J. Reedy :


--
nosy: +benjamin.peterson, stutzbach

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue43834] Use context manager in StringIO example

2021-04-14 Thread John Hagen


Change by John Hagen :


--
keywords: +patch
pull_requests: +24133
stage:  -> patch review
pull_request: https://github.com/python/cpython/pull/25401

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue43834] Use context manager in StringIO example

2021-04-13 Thread John Hagen


New submission from John Hagen :

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 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com