Skip to content

imaplib: search/sort/thread don't encode criteria to the declared CHARSET #153494

Closed

Description

@serhiy-storchaka

IMAP4.search(charset, *criteria) (and sort, thread, and the uid SORT/THREAD variants) send a CHARSET <name> token but serialize the criteria strings with the connection encoding (bytes(arg, self._encoding), ASCII by default). So the declared charset and the actual byte-encoding of the criteria are decoupled: a non-ASCII str criterion with a charset raises UnicodeEncodeError, and the only way to search non-ASCII text today is to pass the criteria as pre-encoded bytes and remember to match them to the charset by hand.

This is a Python 2 legacy. The module was written when str was 8-bit and search criteria were byte strings passed straight through to the socket -- no encoding step was needed, and CHARSET was just an opaque label forwarded to the server. Under Python 3 that model leaves str criteria unencoded (or failing), so the CHARSET argument no longer does what it appears to.

str criteria should be encoded to the declared charset. bytes criteria -- necessary for server-only charsets that Python has no codec for (e.g. the ones this module has always forwarded blindly) -- should pass through unchanged.

Linked PRs

Activity

  1. added a commit that references this issue on Jul 10, 2026
  2. added
    type-bugAn unexpected behavior, bug, or error
    stdlibStandard Library Python modules in the Lib/ directory
    3.16new features, bugs and security fixes
    on Jul 10, 2026
  3. serhiy-storchaka commented on Jul 10, 2026

    @serhiy-storchaka
    MemberAuthor

    This is a bug, but it was in Python 3 from the beginning. And it's effect is that you simply cannot use charset with string arguments (you can use it with bytes, which is not documented), not that you get wrong result or unexpected error (it always fail). So for users it looks like a new feature.

  4. added 2 commits that reference this issue on Jul 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.16new features, bugs and security fixesstdlibStandard Library Python modules in the Lib/ directorytopic-emailtype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions