Unexpected exceptions when `email.policy.Policy.max_line_length` is falsey · Issue #135307 · python/cpython · GitHub
Skip to content

Unexpected exceptions when email.policy.Policy.max_line_length is falsey  #135307

Description

@ptwales

Bug report

Bug description:

From the documentation

max_line_length

The maximum length of any line in the serialized output, not counting the end of line character(s). Default is 78, per RFC 5322. A value of 0 or None indicates that no line wrapping should be done at all.

If I follow this documentation then set_content will fail. If I use the provided email.policy.HTTP, which disables word wrapping, it also fails.

from email import policy
from email.message import EmailMessage

msg = EmailMessage(policy=policy.default.clone(max_line_length=None))
msg["From"] = "noreply@example.com"
msg["To"] = "noreply@example.com"
msg["Subject"] = "Email no wrap fails"
msg.set_content("This line fails")
print(msg)

Stack trace:

  File "/usr/lib/python3.13/email/message.py", line 1207, in set_content
    super().set_content(*args, **kw)
    ~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^
  File "/usr/lib/python3.13/email/message.py", line 1137, in set_content
    content_manager.set_content(self, *args, **kw)
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.13/email/contentmanager.py", line 37, in set_content
    handler(msg, obj, *args, **kw)
    ~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.13/email/contentmanager.py", line 187, in set_text_content
    cte, payload = _encode_text(string, charset, cte, msg.policy)
                   ~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.13/email/contentmanager.py", line 151, in _encode_text
    if max((len(x) for x in lines), default=0) <= policy.max_line_length:
       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: '<=' not supported between instances of 'int' and 'NoneType'

Using max_line_length=0 gives a different error:

  File "/usr/lib/python3.13/email/message.py", line 1207, in set_content
    super().set_content(*args, **kw)
    ~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^
  File "/usr/lib/python3.13/email/message.py", line 1137, in set_content
    content_manager.set_content(self, *args, **kw)
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.13/email/contentmanager.py", line 37, in set_content
    handler(msg, obj, *args, **kw)
    ~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.13/email/contentmanager.py", line 187, in set_text_content
    cte, payload = _encode_text(string, charset, cte, msg.policy)
                   ~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.13/email/contentmanager.py", line 159, in _encode_text
    sniff_qp = quoprimime.body_encode(sniff.decode('latin-1'),
                                      policy.max_line_length)
  File "/usr/lib/python3.13/email/quoprimime.py", line 173, in body_encode
    raise ValueError("maxlinelen must be at least 4")

CPython versions tested on:

3.13

Operating systems tested on:

Linux

Linked PRs

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.13bugs and security fixes3.14bugs and security fixes3.15pre-release feature fixes, bugs and security fixesstdlibStandard Library Python modules in the Lib/ directorytopic-emailtype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions