The email parser class returns the wrong type for the parsed message

未关闭
#101,104 4 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
4/5
预计耗时
3-5 天
新手友好度
35/100
Issue 类型
缺陷
描述清晰度
需要澄清
活跃度
停滞
技术栈
python
领域
backend

调研方向

运行提供的复现程序,并检查入口点 email.message_from_bytes,以及相关的 parser 和 EmailMessage 文档。确定预期的修复是更改返回类型还是更正文档,然后为所演示的行为添加覆盖测试,并验证文档化的 API 与运行时结果一致。

由索引模型根据 Issue 内容生成。

描述

topic-email type-bug

Summary

According to the documentation,

The email package provides a standard parser that understands most email document structures, including MIME documents. You can pass the parser a bytes, string or file object, and the parser will return to you the root EmailMessage instance of the object structure.

In fact, the parser returns an object of the legacy Message class. Code which takes the documentation at face value will blow up if it attempts to invoke methods which the newer class has but the older class does not.

Environment

Python 3.11.1 (v3.11.1:a7a450f84a, Dec 6 2022, 15:24:06) [Clang 13.0.0 (clang-1300.0.29.30)] on darwin

Repro

Here is a small program which demonstrates the problem.

Code
#!/usr/bin/env python3

import email

message_bytes = (
    b'Content-type: multipart/form-data; boundary=------abracadabra\r\n'
    b'\r\n'
    b'------abracadabra\r\n'
    b'Content-Disposition: form-data; name="f"; filename="f1"\r\n'
    b'Content-Type: application/octet-stream\r\n'
    b'\r\n'
    b'\x01\r\n'
    b'------abracadabra--\r\n'
)

message = email.message_from_bytes(message_bytes)
print(f'type of message is {type(message)}')
print('have get_content()?', hasattr(message, 'get_content'))
Output
% ./email-message-repro.py
type of message is <class 'email.message.Message'>
have get_content()? False
主要语言
Python
星标
77.2k
派生
36k
平均合并
1 天 9 小时
30 天内合并 PR
558

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

python/cpython 的其他 Issue

查看 python/cpython 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。