Skip to content

UnboundLocalError in email.message.Message.get_payload #134759

Open
@thib-d

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

added
type-bugAn unexpected behavior, bug, or error
on May 26, 2025
changed the title [-]UnboundLocalError: local variable 'bpayload' referenced before assignment[/-] [+]`UnboundLocalError` in `email.message.Message.get_payload`[/+] on May 26, 2025
picnixz

picnixz commented on May 26, 2025

@picnixz
Member

The error is here:

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

cc @bitdancer

makridenko

makridenko commented on May 27, 2025

@makridenko
Contributor

hi, can i work on this?

skv0zsneg

skv0zsneg commented on May 27, 2025

@skv0zsneg

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?

sobolevn

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 🤝

skv0zsneg

skv0zsneg commented on Jun 27, 2025

@skv0zsneg

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

picnixz

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

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

    stdlibPython modules in the Lib dirtopic-emailtype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      `UnboundLocalError` in `email.message.Message.get_payload` · Issue #134759 · python/cpython

      Follow Lee on X/Twitter - Father, Husband, Serial builder creating AI, crypto, games & web tools. We are friends :) AI Will Come To Life!

      Check out: eBank.nz (Art Generator) | Netwrck.com (AI Tools) | Text-Generator.io (AI API) | BitBank.nz (Crypto AI) | ReadingTime (Kids Reading) | RewordGame | BigMultiplayerChess | WebFiddle | How.nz | Helix AI Assistant