Skip to content
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

Closed
vadmium opened this issue Feb 8, 2017 · 12 comments
Closed

email.policy.Compat32(max_line_length=None) not as documented #73664

vadmium opened this issue Feb 8, 2017 · 12 comments
Labels
3.7 (EOL) end of life topic-email type-bug An unexpected behavior, bug, or error

Comments

@vadmium
Copy link
Member

vadmium commented Feb 8, 2017

BPO 29478
Nosy @warsaw, @bitdancer, @vadmium, @Mariatta, @mircea-cosbuc
PRs
  • [email] bpo-29478: Fix passing max_line_length=None from Compat32 policy #595
  • [3.6] bpo-29478: Fix passing max_line_length=None from Compat32 policy #2024
  • [3.5] bpo-29478: Fix passing max_line_length=None from Compat32 policy #2025
  • [3.6] [email] bpo-29478: Fix passing max_line_length=None from Compat… #2233
  • [3.5] [email] bpo-29478: Fix passing max_line_length=None from Compat… #2234
  • 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:

    assignee = None
    closed_at = <Date 2017-06-16.14:21:56.977>
    created_at = <Date 2017-02-08.10:10:45.594>
    labels = ['type-bug', '3.7', 'expert-email']
    title = 'email.policy.Compat32(max_line_length=None) not as documented'
    updated_at = <Date 2017-06-16.14:21:56.976>
    user = 'https://github.com/vadmium'

    bugs.python.org fields:

    activity = <Date 2017-06-16.14:21:56.976>
    actor = 'Mariatta'
    assignee = 'none'
    closed = True
    closed_date = <Date 2017-06-16.14:21:56.977>
    closer = 'Mariatta'
    components = ['email']
    creation = <Date 2017-02-08.10:10:45.594>
    creator = 'martin.panter'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 29478
    keywords = []
    message_count = 12.0
    messages = ['287294', '287310', '289368', '289383', '289385', '289387', '289421', '295491', '295526', '295748', '296146', '296194']
    nosy_count = 5.0
    nosy_names = ['barry', 'r.david.murray', 'martin.panter', 'Mariatta', 'mcosbuc']
    pr_nums = ['595', '2024', '2025', '2233', '2234']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue29478'
    versions = ['Python 3.5', 'Python 3.6', 'Python 3.7']

    @vadmium
    Copy link
    Member Author

    vadmium commented Feb 8, 2017

    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.

    @vadmium vadmium added 3.7 (EOL) end of life topic-email type-bug An unexpected behavior, bug, or error labels Feb 8, 2017
    @bitdancer
    Copy link
    Member

    That sounds reasonable to me. Clearly there is a missing test :)

    @bitdancer
    Copy link
    Member

    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.

    @mircea-cosbuc
    Copy link
    Mannequin

    mircea-cosbuc mannequin commented Mar 10, 2017

    Thanks for the prompt feedback. In Python 3.2, the closest equivalent for the illustrated issue I could find is:

    >> from email.message import Message
    >> from email.generator import Generator
    >> from sys import stdout
    >> m = Message()
    >> m["Field"] = "x" * 100
    >> g0 = Generator(stdout, maxheaderlen=0)
    >> gn = Generator(stdout, maxheaderlen=None)

    >>> 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 max_line_length=None for the policy is not the default value. Please advise further if I should just update the documentation and modify the test to guard for future changes.

    @bitdancer
    Copy link
    Member

    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 :)

    @mircea-cosbuc
    Copy link
    Mannequin

    mircea-cosbuc mannequin commented Mar 10, 2017

    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 max_line_length to the header class.

    @bitdancer
    Copy link
    Member

    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.

    @Mariatta
    Copy link
    Member

    Mariatta commented Jun 9, 2017

    Is the PR ready for merging?
    Does this need misc/news entry?

    I can help with the backport.

    @bitdancer
    Copy link
    Member

    Looks like it just needs a NEWS entry.

    @Mariatta
    Copy link
    Member

    New changeset b459f74 by Mariatta (mircea-cosbuc) in branch 'master':
    [email] bpo-29478: Fix passing max_line_length=None from Compat32 policy (GH-595)
    b459f74

    @Mariatta
    Copy link
    Member

    New changeset 820b714 by Mariatta in branch '3.5':
    [email] bpo-29478: Fix passing max_line_length=None from Compat32 policy (GH-595) (GH-2234)
    820b714

    @Mariatta
    Copy link
    Member

    New changeset e9f4d8d by Mariatta in branch '3.6':
    [email] bpo-29478: Fix passing max_line_length=None from Compat32 policy (GH-595) (GH-2233)
    e9f4d8d

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.7 (EOL) end of life topic-email type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    3 participants