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

未關閉
#100,261 4 則留言 3 個 reaction 已指派 0 人 在 GitHub 檢視

還沒有人認領這個 Issue。

評估

難度
3/5
預估耗時
1-2 天
新手友好度
35/100
Issue 類型
缺陷
描述清晰度
描述清楚
活躍度
停滯
技術堆疊
python
領域
backend

研究方向

先使用 email.message.Message、email.policy.SMTP 和 email.header.Header 執行提供的重現程式。透過 email/message.py、email/policy.py、email/headerregistry.py 和 email/_header_value_parser.py 追蹤故障,然後與預設 policy 的行為進行比較。當 SMTP policy 範例不再引發所回報的 TypeError,並保留文件中所述的輸出行為時,即表示完成。

由索引模型根據 Issue 內容生成。

描述

topic-email type-bug

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)
主要語言
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 摘要。