Skip to content

email: set_content() raises AttributeError for memoryview with 7bit, 8bit or binary CTE #159858

Description

@NikitaMikhailov

Bug description

email.contentmanager.raw_data_manager explicitly registers memoryview with the same handler as bytes and bytearray, but EmailMessage.set_content() raises AttributeError for a memoryview payload when the content transfer encoding is 7bit, 8bit, or binary.

from email.message import EmailMessage

for cte in ('7bit', '8bit', 'binary'):
    message = EmailMessage()
    try:
        message.set_content(
            memoryview(b'ASCII-only message.\n'),
            maintype='application', subtype='octet-stream', cte=cte,
        )
    except AttributeError as exc:
        print(cte, exc)

All three calls report:

'memoryview' object has no attribute 'decode'

The same calls succeed with bytes or bytearray payloads. Expected: support the registered memoryview input just as for those other bytes-like inputs, preserving the payload. For 7bit, non-ASCII input should still raise UnicodeDecodeError, as it does with bytes.

The failure comes from calling data.decode() in set_bytes_content(). The existing base64 memoryview path is already covered by test_set_image_jpg in test_email.test_contentmanager.

This reproduces on CPython main at ddc647a, Python 3.16.0a1+dev, on macOS arm64.

Rechecked on October 20: the content manager from current main (4c73f50) still raises AttributeError in all three cases. The affected implementation and test files are unchanged since ddc647a.

AI assistance: Codex helped investigate the failure, prepare this report, and test a fix.

Linked PRs

Activity

  1. added a commit that references this issue on Oct 20, 2026
  2. added
    type-bugAn unexpected behavior, bug, or error
    stdlibStandard Library Python modules in the Lib/ directory
    on Oct 20, 2026
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

    stdlibStandard 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