Skip to content

unselect_folder() causes IllegalStateError #639

Description

@lumpi101

I get an IllegalStateError when executing enable(...) after unselect_folder(). It seems to me that unselect_folder() does not update the state imaplib.IMAP4.state from "SELECTED" to "AUTH" (imaplib.IMAP4.unselect does this but imapclient.IMAPClient.unselect_folder not).
close_folder() works without problems.

Versions:
Python 3.13
imapclient 3.1.0

Code to reproduce:

import os
from imapclient import IMAPClient

with IMAPClient(os.environ["EMAIL_IMAP_SERVER"]) as imap:
    imap.login(os.environ["EMAIL_ADDRESS"], os.environ["EMAIL_PASSWORD"])
    imap.select_folder("INBOX")
    imap.unselect_folder()
    imap.enable("UTF8=ACCEPT")

Error:

---------------------------------------------------------------------------
IllegalStateError                         Traceback (most recent call last)
Cell In[30], [line 8](vscode-notebook-cell:?execution_count=30&line=8)
      4 with IMAPClient(os.environ["EMAIL_IMAP_SERVER"]) as imap:
      5     imap.login(os.environ["EMAIL_ADDRESS"], os.environ["EMAIL_PASSWORD"])
      6     imap.select_folder("INBOX")
      7     imap.unselect_folder()
----> [8]     imap.enable("UTF8=ACCEPT")

File xxx/lib/python3.13/site-packages/imapclient/imapclient.py:179, in require_capability.<locals>.actual_decorator.<locals>.wrapper(client, *args, **kwargs)
    175 if not client.has_capability(capability):
    176     raise exceptions.CapabilityError(
    177         "Server does not support {} capability".format(capability)
    178     )
--> [179] return func(client, *args, **kwargs)

File xxx/lib/python3.13/site-packages/imapclient/imapclient.py:554, in IMAPClient.enable(self, *capabilities)
    538 """Activate one or more server side capability extensions.
    539 
    540 Most capabilities do not need to be enabled. This is only
   (...)    551 See :rfc:`5161` for more details.
    552 """
    553 if self._imap.state != "AUTH":
--> [554]     raise exceptions.IllegalStateError(
    555         "ENABLE command illegal in state %s" % self._imap.state
    556     )
    558 resp = self._raw_command_untagged(
    559     b"ENABLE",
    560     [to_bytes(c) for c in capabilities],
   (...)    563     unpack=True,
    564 )
    565 if not resp:

IllegalStateError: ENABLE command illegal in state SELECTED

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions