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
email.policy.Compat32(max_line_length=None) not as documented #73664
Comments
By default, the email package turns single-line header fields into multi-line ones to try and limit the length of each line. The documentation <https://docs.python.org/release/3.5.2/library/email.policy.html#email.policy.Policy.max_line_length\> says that setting the policy’s max_line_length attribute to None should prevent line wrapping. But this does not work: >>> from email.policy import Compat32
>>> from email.message import Message
>>> from email.generator import Generator
>>> from sys import stdout
>>> p = Compat32(max_line_length=None)
>>> m = Message(p)
>>> m["Field"] = "x" * 100
>>> Generator(stdout).flatten(m) # Field is split across two lines
Field:
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
A workaround is to specify zero instead: >>> p = Compat32(max_line_length=0)
>>> Generator(stdout, policy=p).flatten(m) # All on one line
Field: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx Quickly looking at the code, Compat32._fold() passes max_line_length straight to Header.encode(), which is documented as using None as a placeholder for its real default value of 76. So I think the solution would be to add a special case in _fold() to call encode(maxlinelen=0) if max_line_length is None. |
That sounds reasonable to me. Clearly there is a missing test :) |
Thanks for the PR. However, rereading this: since compat32 is providing backward compatibility with the behavior of the python 3.2 email package, we need to check what it would do in this situation before changing the behavior. What we may need instead is a doc fix, unfortunately :(. But a test for this is needed either way. |
Thanks for the prompt feedback. In Python 3.2, the closest equivalent for the illustrated issue I could find is:
>>> g0.flatten(m)
Field: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
>>> gn.flatten(m)
Field:
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx It may be the case that a documentation change is all that is needed. I'm not sure that this change would break compatibility since |
So what happens when you do that same operation in 3.5/6 with your change in place? Does the behavior change? (I haven't looked back at the code to see if I think it will :) |
Just to be sure, I performed the same operations with my changes in place, there's no change in behaviour. I think it's expected since I only modified how the Compat32 policy passes |
OK. This looks good to me. I haven't figured out the new commit process, though (like how to do misc news and backports), so I'm not going to be the one to merge it, I'm afraid. At least not until I do find time to learn. |
Is the PR ready for merging? I can help with the backport. |
Looks like it just needs a NEWS entry. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: