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

オープン
#101,104 コメント 4 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

評価

難易度
4/5
見積もり時間
3〜5日
初心者へのやさしさ
35/100
issue の種類
バグ
明瞭さ
説明が足りない
活発さ
停滞
技術スタック
python
領域
backend

調査の方向性

提供された再現コードを実行し、リンクされている parser と EmailMessage のドキュメントとともに、エントリポイント email.message_from_bytes を調査してください。意図された修正が返される型の変更なのか、ドキュメントの修正なのかを判断し、その後、実証された動作をカバーするテストを追加して、ドキュメント化された 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時間
マージ済み PR(30日)
558

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

python/cpython のほかの issue

python/cpython の issue をすべて見る

似ている issue

Python の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。