[Python-ideas] IMAP4.__exit__ counterintuitive for with blocks

Drew dwarwick96 at gmail.com
Mon Oct 30 13:20:35 EDT 2017


IMAP4.close closes the selected inbox and commits changes such as deletions. It is not called on IMAP4.__exit__ (only logout is, which doesn't call close in its call stack) however, so:

with imaplib.IMAP4_SSL(...) as i:
    ...

would fail to commit those changes. close must be explicitly invoked i.e.

with imaplib.IMAP4_SSL(...) as i:
    ...
    i.close()

This is counterintuitive however, as the with statement is meant to automatically clean up. Another programmer might come along and delete i.close() because it seems unnecessary. Now changes aren't being committed and the programmer doesn't realize it's because of this weird Python idiosyncracy. 

Python IO such as open commits changes automatically, so I'm not sure why IMAP4 doesn't and only logs out. 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20171030/55eb5176/attachment-0001.html>


More information about the Python-ideas mailing list