gh-153494: Encode imaplib search criteria to the declared CHARSET#153495
Merged
serhiy-storchaka merged 4 commits intoJul 10, 2026
Conversation
IMAP4.search(), sort(), thread() and the uid SORT/THREAD variants now encode str search criteria to the declared charset, so international search text can be passed as ordinary str. A criterion passed as bytes is sent unchanged, for use with a charset that Python has no codec for. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Documentation build overview
6 files changed ·
|
The iconv-based codec (pythongh-152997) now resolves NF_Z_62-010 on platforms that provide iconv(3), so it no longer triggers LookupError. Use a name that neither Python nor iconv knows. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
# Conflicts: # Doc/library/imaplib.rst # Lib/imaplib.py # Lib/test/test_imaplib.py
Document that str search criteria are encoded to the declared charset only when a charset is given; under UTF8=ACCEPT the charset is None and criteria are sent using the connection encoding. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
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.
IMAP4.search(),sort(),thread()and theuidSORT/THREADvariants sent aCHARSET <name>token but serialized the criteria with the connection encoding (bytes(arg, self._encoding), ASCII by default), so a non-ASCIIstrcriterion with a charset raisedUnicodeEncodeError— the declared charset and the criteria byte-encoding were decoupled. This is a Python 2 legacy, from whenstrwas 8-bit and criteria were bytes forwarded straight to the socket.Now a
strcriterion is encoded to the declared charset (which must name a codec known to Python). Abytescriterion is sent unchanged, for use with a charset that Python has no codec for.uid('SEARCH', ...)is left unchanged: its charset is unstructured (the caller passesCHARSET <name>as positional arguments), so imaplib cannot tell which argument is the charset.