Title: confusing documentation for IOBase.__exit__
Type: Stage: resolved
Components: IO Versions: Python 3.7
Status: closed Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: dholth, serhiy.storchaka, terry.reedy
Priority: normal Keywords:

Created on 2020-04-09 03:47 by dholth, last changed 2020-04-21 19:47 by dholth. This issue is now closed.

Messages (5)
msg366032 - (view) Author: Daniel Holth (dholth) * Date: 2020-04-09 03:47
The io documentation says:

IOBase is also a context manager and therefore supports the with statement. In this example, file is closed after the with statement’s suite is finished—even if an exception occurs:

with open('spam.txt', 'w') as file:
    file.write('Spam and eggs!')

I read this to mean that my own subclass of io.BufferedIOBase would call close() when used as a context manager.

Instead, it is necessary to provide an implementation of __exit__ that calls close() to get this behavior.

The documentation lists Mixin Methods, but I couldn't find a definition of the term "Mixin Methods" in the docs.
msg366189 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2020-04-11 03:48
Please post or attach *minimal* and complete (runnable as is) code that does not do what you expect.
msg366193 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2020-04-11 07:43
I cannot reproduce the issue. __exit__() calls close().

>>> import io
>>> class F(io.IOBase):
...     def close(self):
...         print('close')
...         super().close()  # set closed = True
>>> with F(): pass
msg366208 - (view) Author: Daniel Holth (dholth) * Date: 2020-04-11 12:27
Thanks, I'm sorry I didn't save when I was having my strange problem.

The io module is still practically undocumented. It should have examples subclasses for each class, perhaps a link to the PEP and a link to a Python implementation of the module.
msg366937 - (view) Author: Daniel Holth (dholth) * Date: 2020-04-21 19:47
Possibly the best io module example I've found
Date User Action Args
2020-04-21 19:47:06dholthsetmessages: + msg366937
2020-04-11 12:27:21dholthsetstatus: pending -> closed

messages: + msg366208
stage: resolved
2020-04-11 07:43:49serhiy.storchakasetstatus: open -> pending
nosy: + serhiy.storchaka
messages: + msg366193

2020-04-11 03:48:02terry.reedysetnosy: + terry.reedy
messages: + msg366189
2020-04-09 03:47:21dholthcreate