Skip to content

gh-134759: UnboundLocalError in email.message.Message.get_payload #136071

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

Merged
merged 13 commits into from
Jul 12, 2025

Conversation

skv0zsneg
Copy link
Contributor

@skv0zsneg skv0zsneg commented Jun 28, 2025

Hello!

I worked on #134759 and was able to reproduce the UnboundLocalError by passing bytes payload directly to _payload. I'm not sure can we do it in a "public" way, like throw .set_payload() but if payload some how become bytes (and content-transfer-encoding is equal quoted-printable) it is now not falling :)

I'm also add myself to ACKS list, but not sure is my changes enough for it. If you say I'll remove myself with no questions.

@skv0zsneg skv0zsneg requested a review from a team as a code owner June 28, 2025 11:26
@python-cla-bot
Copy link

python-cla-bot bot commented Jun 28, 2025

All commit authors signed the Contributor License Agreement.

CLA signed

@bedevere-app
Copy link

bedevere-app bot commented Jun 28, 2025

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

@picnixz picnixz added needs backport to 3.13 bugs and security fixes needs backport to 3.14 bugs and security fixes labels Jun 28, 2025
Copy link
Member

@sobolevn sobolevn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

Comment on lines 1058 to 1059
def test_get_bytes_payload_with_quoted_printable_encoding(self):
payload = b'Some payload'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def test_get_bytes_payload_with_quoted_printable_encoding(self):
payload = b'Some payload'
def test_get_bytes_payload_with_quoted_printable_encoding(self):
# See https://github.com/python/cpython/issues/134759.
payload = b'Some payload'

This comment was marked as resolved.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can use the following for only using public methods:

        payload = memoryview(b'Some payload')
        m = self._make_message()
        m.add_header('Content-Transfer-Encoding', 'quoted-printable')
        m.set_payload(payload)
        self.assertEqual(m.get_payload(decode=True), payload)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I was trying to reproduce bug with "public" method .set_payload(). I'm achieved that by passing BytesIO(b"Some payload") buffer but was not sure about correct arg type of .set_payload() (typeshed want object only with the .decode() method), so came up to put bytes directly.

I'll change the way you suggest, no problem :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, decode() is not present on memoryview but it's a buffer-like protocol so I think it should be fine. However, this may be a side-effect of the current implementation :) (also, the current implementation only does duck typing, not true isinstance() checks when setting the payload)

@picnixz
Copy link
Member

picnixz commented Jul 1, 2025

As a bugfix I think we can keep this but in a follow-up PR I think we should ensure that we are only having a list, str or bytes or handle buffer objects by wrapping them into bytes.

@skv0zsneg
Copy link
Contributor Author

As a bugfix I think we can keep this but in a follow-up PR I think we should ensure that we are only having a list, str or bytes or handle buffer objects by wrapping them into bytes.

Got it. If nobody mind I can open an issue (or keep in #134759 ?) and take it in work :)

@picnixz
Copy link
Member

picnixz commented Jul 1, 2025

I'm travelling for now but a separate issue for discussion would be better. It could break backwards compatibility so we should be careful.

@picnixz picnixz enabled auto-merge (squash) July 12, 2025 13:05
@picnixz
Copy link
Member

picnixz commented Jul 12, 2025

I'm also add myself to ACKS list, but not sure is my changes enough for it. If you say I'll remove myself with no questions.

IMO even contributors fixing a single typo can be added to this file so no worry!

@picnixz picnixz merged commit 25335d2 into python:main Jul 12, 2025
41 checks passed
@miss-islington-app
Copy link

Thanks @skv0zsneg for the PR, and @picnixz for merging it 🌮🎉.. I'm working now to backport this PR to: 3.13, 3.14.
🐍🍒⛏🤖

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Jul 12, 2025
…t_payload` (pythonGH-136071)

(cherry picked from commit 25335d2)

Co-authored-by: Kliment Lamonov <klimentlamonov@yandex.ru>
Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
@bedevere-app
Copy link

bedevere-app bot commented Jul 12, 2025

GH-136579 is a backport of this pull request to the 3.14 branch.

@bedevere-app bedevere-app bot removed the needs backport to 3.14 bugs and security fixes label Jul 12, 2025
miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Jul 12, 2025
…t_payload` (pythonGH-136071)

(cherry picked from commit 25335d2)

Co-authored-by: Kliment Lamonov <klimentlamonov@yandex.ru>
Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
@bedevere-app
Copy link

bedevere-app bot commented Jul 12, 2025

GH-136580 is a backport of this pull request to the 3.13 branch.

@bedevere-app bedevere-app bot removed the needs backport to 3.13 bugs and security fixes label Jul 12, 2025
picnixz added a commit that referenced this pull request Jul 12, 2025
…et_payload` (GH-136071) (#136580)

gh-134759: fix `UnboundLocalError` in `email.message.Message.get_payload` (GH-136071)
(cherry picked from commit 25335d2)

Co-authored-by: Kliment Lamonov <klimentlamonov@yandex.ru>
Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
picnixz added a commit that referenced this pull request Jul 12, 2025
…et_payload` (GH-136071) (#136579)

gh-134759: fix `UnboundLocalError` in `email.message.Message.get_payload` (GH-136071)
(cherry picked from commit 25335d2)

Co-authored-by: Kliment Lamonov <klimentlamonov@yandex.ru>
Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
Pranjal095 pushed a commit to Pranjal095/cpython that referenced this pull request Jul 12, 2025
…t_payload` (python#136071)

Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy