python / python/cpython

`email.mime.image.MIMEImage`'s `policy` argument doesn't take effect

未关闭
#133,311 2 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

stdlib topic-email type-bug
主要语言
Python
星标
77.2k
派生
35.9k
PR 合并指标
PR 指标待抓取

描述

Bug report

Bug description:

I'm comparing the creation of two MIME parts, one using MIMEImage and one using MIMEPart, with the same policy object, which asks for max_line_length=35.

the MIMEImage does not respect the max_line_length when base64-encoding, but the MIMEPart does:

#!/usr/bin/python3

from io import BytesIO
from email.policy import EmailPolicy
from email.mime.image import MIMEImage
from email.message import MIMEPart
from PIL import Image, ImageDraw

img = Image.new('RGBA', (20,20))
d = ImageDraw.Draw(img)
d.regular_polygon((10,6,6), 3, fill='red')
d.line([(16,10), (10,16)], fill='blue', width=2)
d.line([(16,16), (10,10)], fill='blue', width=2)
b = BytesIO()
img.save(b, format='png', optimize=True)
imgbytes = b.getvalue()

policy = EmailPolicy(max_line_length=35)

f = MIMEImage(imgbytes, policy=policy)
print(f)

g = MIMEPart(policy=policy)
g.set_content(imgbytes, 'image', 'png')
print(g)

The output is:

Content-Type: image/png
MIME-Version: 1.0
Content-Transfer-Encoding: base64

iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAYAAACNiR0NAAAAcElEQVR42uVTOxbAMAgS739nO3Tp
Rw20dqpbfARQEjOywiwYnCtkDKnbcLk66sqlT+zt9cidkE+6KwkZsgrzfcqVMpL2jo0447gYDpeA
rk+OnJHkIhAfTPRicihAf5YJrw7vjv0ZWRWM/ulivdPf1QZ2kDD9xppd8wAAAABJRU5ErkJggg==

Content-Type: image/png
Content-Transfer-Encoding: base64

iVBORw0KGgoAAAANSUhEUgAAABQAAAAU
CAYAAACNiR0NAAAAcElEQVR42uVTOxbA
MAgS739nO3TpRw20dqpbfARQEjOywiwY
nCtkDKnbcLk66sqlT+zt9cidkE+6KwkZ
sgrzfcqVMpL2jo0447gYDpeArk+OnJHk
IhAfTPRicihAf5YJrw7vjv0ZWRWM/uli
vdPf1QZ2kDD9xppd8wAAAABJRU5ErkJg
gg==

CPython versions tested on:

3.13

Operating systems tested on:

Linux

Linked PRs
  • gh-133322

贡献指南

打开贡献指南

从这里开始

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

调研方向

首先比较 email.mime.image.MIMEImage 和 email.message.MIMEPart 的实现,重点关注它们各自在 base64 编码期间如何使用提供的 EmailPolicy。使用 max_line_length=35 重现该示例,并确认 MIMEImage 输出的行长度不超过 policy 允许的长度;关联的 PR gh-133322 表明相关工作已经在进行中。

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

评估

技术栈
python
领域
backend
Issue 类型
缺陷
难度
2/5
预计耗时
1-3 小时
活跃度
停滞
描述清晰度
基本清楚
新手友好度
30/100

把新 issue 发到你的邮箱

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