Skip to content

UnboundLocalError in email.message.Message.get_payload #134759

Closed

Description

@thib-d

Bug report

Bug description:

    html_body = part.get_payload(decode=True).decode()
  File "/usr/local/lib/python3.8/email/message.py", line 282, in get_payload
    return quopri.decodestring(bpayload)

CPython versions tested on:

CPython main branch

Operating systems tested on:

Linux

Linked PRs

Activity

  1. added
    type-bugAn unexpected behavior, bug, or error
    on May 26, 2025
  2. changed the title [-]UnboundLocalError: local variable 'bpayload' referenced before assignment[/-] [+]`UnboundLocalError` in `email.message.Message.get_payload`[/+] on May 26, 2025
  3. added
    stdlibStandard Library Python modules in the Lib/ directory
    on May 26, 2025
  4. picnixz commented on May 26, 2025

    @picnixz
    Member

    The error is here:

    if cte == 'quoted-printable':
    return quopri.decodestring(bpayload)

    cc @bitdancer

  5. makridenko commented on May 27, 2025

    @makridenko
    Contributor

    hi, can i work on this?

  6. skv0zsneg commented on May 27, 2025

    @skv0zsneg
    Contributor

    Hello! I would like to work on it but did not work with CPython at all. Will it be a problem? If not where I should start with?

  7. sobolevn commented on May 27, 2025

    @sobolevn
    Member

    Hi, @skv0zsneg and @makridenko! Thanks a lot for your interest.

    @makridenko since you were first, you can surely work on this. Because it looks like that no one else has started yet.

    @skv0zsneg we value new contributors, please, don't feel left out :) You can pick any other issue from the opened ones 🤝

  8. skv0zsneg commented on Jun 27, 2025

    @skv0zsneg
    Contributor

    Hey everyone!

    Trying to reproduce this bug UnboundLocalError but it want falling.

    What I'm doing in REPL:

    >>> raw_email = """Content-Type: text/plain; charset="utf-8"
    ... Content-Transfer-Encoding: quoted-printable
    ... 
    ... Hey!"""
    >>> from email import message_from_string
    >>> msg = message_from_string(raw_email)
    >>> msg.get_payload(decode=True).decode()
    'Hey!'
    >>> msg._headers
    [('Content-Type', 'text/plain; charset="utf-8"'), ('Content-Transfer-Encoding', 'quoted-printable')]

    As mentioned by @picnixz, the error must be on line 317. To get here I use email message with quoted-printable encoding and body message surely get into quopri.decodestring but (luckly?) it does not fall. Am I miss something?

    OS: Linux (Ubuntu 24.04.2 LTS)
    Python: Python 3.15.0a0

  9. picnixz commented on Jun 27, 2025

    @picnixz
    Member

    The "bad" code path is only taken when payload is not a string, cte == 'quoted-printable' and decode is True. Considering there is

            if i is not None and not isinstance(self._payload, list):

    We can't just use a list because if is_multipart() would return true and we would early exit. So we need to use a bytes payload with decode = True (I think).

  10. added a commit that references this issue on Jul 12, 2025
  11. added 2 commits that reference this issue on Jul 12, 2025
  12. added 2 commits that reference this issue on Jul 12, 2025
  13. added a commit that references this issue on Jul 12, 2025
  14. added a commit that references this issue on Jul 13, 2025
  15. added a commit that references this issue on Aug 4, 2025
  16. added a commit that references this issue on Aug 19, 2025
  17. added a commit that references this issue on Sep 9, 2025
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