Skip to content

email.utils.encode_rfc2231() writes 'None' as the charset when only language is given #161222

Open

Description

@andresdelfino

Bug report

Bug description:

email.utils.encode_rfc2231() substitutes an empty string when language is omitted, but not when charset is omitted. When only language is given, None is interpolated into the result as the literal charset name:

>>> import email.utils
>>> email.utils.encode_rfc2231('abc', language='en')
"None'en'abc"

The same happens when a (None, language, value) tuple is passed as a parameter value to Message.add_header():

>>> from email.message import Message
>>> m = Message()
>>> m.add_header('Content-Disposition', 'attachment', filename=(None, 'en', 'report.txt'))
>>> m['Content-Disposition']
"attachment; filename*=None'en'report.txt"

RFC 2231 section 4 says "it is perfectly permissible to leave either the character set or language field blank", so the expected result is "'en'abc". The email package's own parser reads the value back correctly only because None is not a known codec, so it falls back to the undecoded text. Other consumers see a bogus charset name.

This is separate from gh-85014, which covers the case where both charset and language are None.

CPython versions tested on:

3.15, CPython main branch

Operating systems tested on:

Linux

Linked PRs

Activity

  1. added
    type-bugAn unexpected behavior, bug, or error
    stdlibStandard Library Python modules in the Lib/ directory
    on Nov 3, 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

    stdlibStandard 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