Reset IMAP state to AUTH after unselect_folder() (fixes #639)#644
Open
apoorvdarshan wants to merge 1 commit into
Open
Reset IMAP state to AUTH after unselect_folder() (fixes #639)#644apoorvdarshan wants to merge 1 commit into
apoorvdarshan wants to merge 1 commit into
Conversation
unselect_folder() called _simple_command('UNSELECT') directly but never
reset self._imap.state back to 'AUTH', unlike imaplib.IMAP4.unselect().
The client therefore stayed in the SELECTED state, so a subsequent
enable() raised IllegalStateError.
Set state to 'AUTH' after a successful UNSELECT, mirroring imaplib.
Fixes mjs#639.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
unselect_folder()leaves the connection in theSELECTEDstate, so a subsequentenable()raisesIllegalStateError(issue #639):The root cause is that
unselect_folder()callsself._imap._simple_command("UNSELECT")directly and never resetsself._imap.stateback to"AUTH". In contrast,imaplib.IMAP4.unselect()setsself.state = "AUTH"when the command succeeds, andclose_folder()(which goes throughimaplib'sclose) works correctly for the same reason.Fix
After a successful
UNSELECT(typ == "OK"), setself._imap.state = "AUTH", mirroringimaplib.IMAP4.unselect(). This returns the connection to the authenticated state soenable()and other AUTH-state commands are legal again. The change is localized tounselect_folder()inimapclient/imapclient.py.Tests
Added
test_unselect_resets_state_to_authintests/test_imapclient.py(alongside the existingtest_unselect). It sets the mock state to"SELECTED", callsunselect_folder(), and asserts the state is reset to"AUTH". The test fails on the pristine code and passes with the fix. Full suite: 268 tests pass.black,isort, andflake8are clean on the changed files.Disclosure: prepared with AI assistance; reviewed and verified locally.