Skip to content

gh-153494: Encode imaplib search criteria to the declared CHARSET#153495

Merged
serhiy-storchaka merged 4 commits into
python:mainfrom
serhiy-storchaka:imaplib-search-charset
Jul 10, 2026
Merged

gh-153494: Encode imaplib search criteria to the declared CHARSET#153495
serhiy-storchaka merged 4 commits into
python:mainfrom
serhiy-storchaka:imaplib-search-charset

Conversation

@serhiy-storchaka

Copy link
Copy Markdown
Member

IMAP4.search(), sort(), thread() and the uid SORT/THREAD variants sent a CHARSET <name> token but serialized the criteria with the connection encoding (bytes(arg, self._encoding), ASCII by default), so a non-ASCII str criterion with a charset raised UnicodeEncodeError — the declared charset and the criteria byte-encoding were decoupled. This is a Python 2 legacy, from when str was 8-bit and criteria were bytes forwarded straight to the socket.

Now a str criterion is encoded to the declared charset (which must name a codec known to Python). A bytes criterion 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 passes CHARSET <name> as positional arguments), so imaplib cannot tell which argument is the charset.

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>
@read-the-docs-community

read-the-docs-community Bot commented Jul 10, 2026

Copy link
Copy Markdown

Documentation build overview

📚 cpython-previews | 🛠️ Build #33531036 | 📁 Comparing 9ac8485 against main (9324f84)

  🔍 Preview build  

6 files changed · ± 6 modified

± Modified

serhiy-storchaka and others added 3 commits July 10, 2026 14:34
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>
@serhiy-storchaka serhiy-storchaka merged commit 5aafbea into python:main Jul 10, 2026
51 checks passed
@serhiy-storchaka serhiy-storchaka deleted the imaplib-search-charset branch July 10, 2026 15:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant