-
-
Notifications
You must be signed in to change notification settings - Fork 32.2k
Unexpected exceptions when email.policy.Policy.max_line_length
is falsey
#135307
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
email.policy.Policy.max_line_length
is falsey
We have lots of similar issues so I don't know if this one was one that was recently fixed. EDIT: Ok this seems a new issue. |
For the fix: I suggest converting cc @bitdancer |
Note: a temporary fix, I believe, is to use |
For personal perspective, I agree with you @picnixz. I think it does make sense to treat By RFC 2822 Ch 2.1.1., we have the following statement:
Is that a solution as we set Also, it seems like no better way to overwrite the policy abstract class than to use the Reference from Python Docs
|
We previously fixed a similar issue in Using 998 is not appropriate, as that limit applies only the RFC compliant emails and the email package actually aims to be applicable more widely than just email. That is, unlimited line length is explicitly supported, since that is what is used in http contexts. |
Thanks for the clarification, yes that makes sense, we also need to consider non-RFC formats. I looked up --- i/Lib/email/contentmanager.py
+++ w/Lib/email/contentmanager.py
@@ -1,3 +1,4 @@
+import sys
import binascii
import email.charset
import email.message
@@ -142,13 +143,15 @@ def _encode_base64(data, max_line_length):
def _encode_text(string, charset, cte, policy):
+ # max_line_length 0/None means no limit, ie: infinitely long.
+ maxlen = policy.max_line_length or sys.maxsize
lines = string.encode(charset).splitlines()
linesep = policy.linesep.encode('ascii')
def embedded_body(lines): return linesep.join(lines) + linesep
def normal_body(lines): return b'\n'.join(lines) + b'\n'
if cte is None:
# Use heuristics to decide on the "best" encoding.
- if max((len(x) for x in lines), default=0) <= policy.max_line_length:
+ if max((len(x) for x in lines), default=0) <= maxlen:
try:
return '7bit', normal_body(lines).decode('ascii')
except UnicodeDecodeError:
@@ -157,7 +160,7 @@ def normal_body(lines): return b'\n'.join(lines) + b'\n'
return '8bit', normal_body(lines).decode('ascii', 'surrogateescape')
sniff = embedded_body(lines[:10])
sniff_qp = quoprimime.body_encode(sniff.decode('latin-1'),
- policy.max_line_length)
+ maxlen)
sniff_base64 = binascii.b2a_base64(sniff)
# This is a little unfair to qp; it includes lineseps, base64 doesn't.
if len(sniff_qp) > len(sniff_base64):
@@ -172,9 +175,9 @@ def normal_body(lines): return b'\n'.join(lines) + b'\n'
data = normal_body(lines).decode('ascii', 'surrogateescape')
elif cte == 'quoted-printable':
data = quoprimime.body_encode(normal_body(lines).decode('latin-1'),
- policy.max_line_length)
+ maxlen)
elif cte == 'base64':
- data = _encode_base64(embedded_body(lines), policy.max_line_length)
+ data = _encode_base64(embedded_body(lines), maxlen)
else:
raise ValueError("Unknown content transfer encoding {}".format(cte))
return cte, data
|
Yes, that's what I had in mind. Now we need a PR with tests and news...and a similar fix in |
Got it, if you don't mind. I would like to implement PR now :). Because I believe the idea was suggested by you. |
Uh oh!
There was an error while loading. Please reload this page.
Bug report
Bug description:
From the documentation
If I follow this documentation then
set_content
will fail. If I use the providedemail.policy.HTTP
, which disables word wrapping, it also fails.Stack trace:
Using
max_line_length=0
gives a different error:CPython versions tested on:
3.13
Operating systems tested on:
Linux
Linked PRs
The text was updated successfully, but these errors were encountered: