python / python/cpython

email.policy.SMTP TypeError: 'Header' object is not subscriptable

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

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

topic-email type-bug
主要言語
Python
スター
77.2k
フォーク
36k
平均マージ
1日 9時間
マージ済み PR(30日)
558

説明

Bug report

Code snippet is taken directly from documentation https://docs.python.org/3/library/email.header.html
The only change - policy changed to email.policy.SMTP.
Documentation (https://docs.python.org/3/library/email.policy.html#email.policy.SMTP) states that the only difference between email.policy.default and email.policy.SMTP - linesep used.

from email.message import Message
from email.header import Header
from email.policy import SMTP

msg = Message()
h = Header('p\xf6stal', 'iso-8859-1')
msg['Subject'] = h
print(msg.as_string())

msg = Message(policy=SMTP)
h = Header('p\xf6stal', 'iso-8859-1')
msg['Subject'] = h
print(msg.as_string())

Output is:

Subject: =?iso-8859-1?q?p=F6stal?=


Traceback (most recent call last):
  File "C:\Users\barsk\AppData\Roaming\JetBrains\PyCharm2022.3\scratches\scratch_3.py", line 12, in <module>
    msg['Subject'] = h
  File "C:\Program Files (x86)\Python37-32\lib\email\message.py", line 409, in __setitem__
    self._headers.append(self.policy.header_store_parse(name, val))
  File "C:\Program Files (x86)\Python37-32\lib\email\policy.py", line 148, in header_store_parse
    return (name, self.header_factory(name, value))
  File "C:\Program Files (x86)\Python37-32\lib\email\headerregistry.py", line 589, in __call__
    return self[name](name, value)
  File "C:\Program Files (x86)\Python37-32\lib\email\headerregistry.py", line 197, in __new__
    cls.parse(value, kwds)
  File "C:\Program Files (x86)\Python37-32\lib\email\headerregistry.py", line 272, in parse
    kwds['parse_tree'] = cls.value_parser(value)
  File "C:\Program Files (x86)\Python37-32\lib\email\_header_value_parser.py", line 1105, in get_unstructured
    if value[0] in WSP:
TypeError: 'Header' object is not subscriptable

Your environment

Reproduced on windows on

  • Python 3.11 (64-bit)
  • Python 3.10 (64-bit)
  • Python 3.7 (32-bit)

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

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

はじめの一歩

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

調査の方向性

まず、提供された再現コードを email.message.Message、email.policy.SMTP、email.header.Header とともに実行します。email/message.py、email/policy.py、email/headerregistry.py、email/_header_value_parser.py をたどって失敗の原因を追跡し、その後、デフォルトポリシーの動作と比較します。SMTP ポリシーの例で報告された TypeError が発生しなくなり、ドキュメントに記載された出力動作が維持されれば完了です。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
python
領域
backend
issue の種類
バグ
難易度
3/5
見積もり時間
1〜2日
活発さ
停滞
明瞭さ
明確に書かれている
初心者へのやさしさ
35/100

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

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