Skip to content

gh-66788: Add the utf-7-imap codec#153149

Merged
serhiy-storchaka merged 4 commits into
python:mainfrom
serhiy-storchaka:imap4-utf7-codec
Jul 8, 2026
Merged

gh-66788: Add the utf-7-imap codec#153149
serhiy-storchaka merged 4 commits into
python:mainfrom
serhiy-storchaka:imap4-utf7-codec

Conversation

@serhiy-storchaka

@serhiy-storchaka serhiy-storchaka commented Jul 5, 2026

Copy link
Copy Markdown
Member

Add the utf-7-imap codec, implementing the modified UTF-7 encoding used for international IMAP4 mailbox names (RFC 3501, section 5.1.3).

It differs from UTF-7 (RFC 2152):

  • & (not +) is the shift character, and &- encodes a literal &;
  • , is used instead of / in the modified Base64 alphabet;
  • only printable US-ASCII characters (except &) are represented directly, so all other characters, including other controls, are Base64-encoded.

The aliases csUTF7IMAP and mUTF-7 are also registered.

This is the codec dependency of gh-49555 (international mailbox names in imaplib). Automatic encoding/decoding of mailbox names in imaplib itself is a separate question, left for later — modern servers accept raw UTF-8 names via UTF8=ACCEPT (RFC 6855), and auto-conversion has non-trivial API implications.

🤖 Generated with Claude Code

Implement the modified UTF-7 encoding used for international IMAP4
mailbox names (RFC 3501, section 5.1.3).  It differs from UTF-7:
"&" is the shift character ("&-" encodes a literal "&"), "," replaces
"/" in the Base64 alphabet, and all non-printable-ASCII characters are
Base64-encoded.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@read-the-docs-community

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

Copy link
Copy Markdown

@vstinner

vstinner commented Jul 7, 2026

Copy link
Copy Markdown
Member

Add the imap4-utf-7 codec, implementing the modified UTF-7 encoding used for international IMAP4 mailbox names (RFC 3501, section 5.1.3).

IANA uses UTF-7-IMAP name for this codec. What do you think of using utf-7-imap name?

@malemburg malemburg left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like Victor's suggestion of using "utf-7-imap" for this codec.

@serhiy-storchaka

Copy link
Copy Markdown
Member Author

Great idea! It doesn't have particular name in RFC 2152, and on Wikipedia it is called mUTF-7, which looks too generic.

Rename the modified-UTF-7 codec from imap4-utf-7 to utf-7-imap and
register the aliases csUTF7IMAP and mUTF-7.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@serhiy-storchaka serhiy-storchaka changed the title gh-66788: Add the imap4-utf-7 codec gh-66788: Add the utf-7-imap codec Jul 7, 2026
serhiy-storchaka and others added 2 commits July 7, 2026 20:00
Convert the parametrized utf-7-imap codec tests to the subTests()
decorator.  Add round-trip cases for "+", "+-" (literal, unlike in UTF-7)
and a "," in the Base64 run, and replace the duplicate decode-error cases
with a "&" at the end of the input and an unterminated but otherwise valid
Base64 sequence.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
…7-imap

Decode the modified Base64 with binascii.a2b_base64() and the modified
alphabet directly, instead of base64.b64decode() with altchars.  With
strict_mode the "/" character (used by standard Base64 but not by the
modified alphabet, which uses ",") is now rejected rather than accepted
with a DeprecationWarning.  Encoding likewise uses binascii.b2a_base64().

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@serhiy-storchaka serhiy-storchaka merged commit 562606d into python:main Jul 8, 2026
99 of 100 checks passed
@serhiy-storchaka serhiy-storchaka deleted the imap4-utf7-codec branch July 8, 2026 11:55
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.

3 participants