Skip to content

email.message_from_string.get() fails parses the special text #4937

Description

@xiaxinmeng

email.message_from_string.get() fails parses the special text "Subject:=?us-ascii?X?value?=", See the following example.

test.py

import email
import email.policy

mytext = "Subject:=?us-ascii?X?value?="
em = email.message_from_string(mytext, policy=email.policy.default)
em.get('Subject')

Behaviors on CPython 3.8.0,3.9.0,3.11.3: work normally

Behaviors on RustPython 0.2.0:

Traceback (most recent call last):
  File "/home/xxm/Desktop/RustPython/test.py", line 569, in <module>
    em.get('Subject')
  File "/home/xxm/.cargo/git/checkouts/rustpython-f8ef4d934ac33cd8/0284059/pylib/Lib/email/message.py", line 471, in get
    return self.policy.header_fetch_parse(k, v)
  File "/home/xxm/.cargo/git/checkouts/rustpython-f8ef4d934ac33cd8/0284059/pylib/Lib/email/policy.py", line 162, in header_fetch_parse
    return self.header_factory(name, value)
  File "/home/xxm/.cargo/git/checkouts/rustpython-f8ef4d934ac33cd8/0284059/pylib/Lib/email/headerregistry.py", line 586, in __call__
    return self[name](name, value)
  File "/home/xxm/.cargo/git/checkouts/rustpython-f8ef4d934ac33cd8/0284059/pylib/Lib/email/headerregistry.py", line 197, in __new__
    cls.parse(value, kwds)
  File "/home/xxm/.cargo/git/checkouts/rustpython-f8ef4d934ac33cd8/0284059/pylib/Lib/email/headerregistry.py", line 269, in parse
    kwds['parse_tree'] = cls.value_parser(value)
  File "/home/xxm/.cargo/git/checkouts/rustpython-f8ef4d934ac33cd8/0284059/pylib/Lib/email/_header_value_parser.py", line 1498, in get_unstructured
    pass
  File "/home/xxm/.cargo/git/checkouts/rustpython-f8ef4d934ac33cd8/0284059/pylib/Lib/email/_header_value_parser.py", line 1494, in get_unstructured
    token, value = get_encoded_word(value)
  File "/home/xxm/.cargo/git/checkouts/rustpython-f8ef4d934ac33cd8/0284059/pylib/Lib/email/_header_value_parser.py", line 1447, in get_encoded_word
    "encoded word format invalid: '{}'".format(ew.cte))
  File "/home/xxm/.cargo/git/checkouts/rustpython-f8ef4d934ac33cd8/0284059/pylib/Lib/email/_header_value_parser.py", line 1444, in get_encoded_word
    text, charset, lang, defects = _ew.decode('=?' + tok + '?=')
  File "/home/xxm/.cargo/git/checkouts/rustpython-f8ef4d934ac33cd8/0284059/pylib/Lib/email/_encoded_words.py", line 166, in decode
    bstring, defects = _cte_decoders[cte](bstring)
KeyError: x

Environment:
Ubuntu 18.04
RustPython 0.2.0 0284059
CPython 3.8.0, 3.9.0, 3.11.3

Activity

  1. DimitrisJim commented on May 3, 2023

    @DimitrisJim
    Member

    this is a Python level error so first place I'd look is if maybe email is not up to date?

  2. youknowone commented on Oct 6, 2026

    @youknowone
    Member

    The originally reported problem no longer reproduces on current main. Closing per the recheck in #8982.

    — commented by Grok 4.6

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions